source: Sophya/trunk/Cosmo/SimLSS/genefluct3d.cc@ 3518

Last change on this file since 3518 was 3518, checked in by cmv, 17 years ago

possibilite de travailler en float cmv 11/09/2008

File size: 48.6 KB
RevLine 
[3115]1#include "machdefs.h"
2#include <iostream>
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <math.h>
7#include <unistd.h>
8
9#include "tarray.h"
10#include "pexceptions.h"
11#include "perandom.h"
12#include "srandgen.h"
13
[3141]14#include "fabtcolread.h"
15#include "fabtwriter.h"
16#include "fioarr.h"
17
18#include "arrctcast.h"
19
[3115]20#include "constcosmo.h"
21#include "geneutils.h"
[3199]22#include "schechter.h"
[3115]23
24#include "genefluct3d.h"
25
[3518]26#if defined(GEN3D_FLOAT)
27#define GEN3D_FFTW_INIT_THREADS fftwf_init_threads
28#define GEN3D_FFTW_CLEANUP_THREADS fftwf_cleanup_threads
29#define GEN3D_FFTW_PLAN_WITH_NTHREADS fftwf_plan_with_nthreads
30#define GEN3D_FFTW_PLAN_DFT_R2C_3D fftwf_plan_dft_r2c_3d
31#define GEN3D_FFTW_PLAN_DFT_C2R_3D fftwf_plan_dft_c2r_3d
32#define GEN3D_FFTW_DESTROY_PLAN fftwf_destroy_plan
33#define GEN3D_FFTW_EXECUTE fftwf_execute
34#else
35#define GEN3D_FFTW_INIT_THREADS fftw_init_threads
36#define GEN3D_FFTW_CLEANUP_THREADS fftw_cleanup_threads
37#define GEN3D_FFTW_PLAN_WITH_NTHREADS fftw_plan_with_nthreads
38#define GEN3D_FFTW_PLAN_DFT_R2C_3D fftw_plan_dft_r2c_3d
39#define GEN3D_FFTW_PLAN_DFT_C2R_3D fftw_plan_dft_c2r_3d
40#define GEN3D_FFTW_DESTROY_PLAN fftw_destroy_plan
41#define GEN3D_FFTW_EXECUTE fftw_execute
42#endif
[3115]43
44#define MODULE2(_x_) ((double)((_x_).real()*(_x_).real() + (_x_).imag()*(_x_).imag()))
45
[3325]46namespace SOPHYA {
47
[3115]48//-------------------------------------------------------
[3349]49GeneFluct3D::GeneFluct3D(long nx,long ny,long nz,double dx,double dy,double dz
50 ,unsigned short nthread,int lp)
[3115]51{
[3349]52 init_default();
[3115]53
[3349]54 lp_ = lp;
55 nthread_ = nthread;
[3115]56
[3349]57 setsize(nx,ny,nz,dx,dy,dz);
58 setalloc();
59 setpointers(false);
60 init_fftw();
[3141]61}
62
[3349]63GeneFluct3D::GeneFluct3D(unsigned short nthread)
[3154]64{
[3349]65 init_default();
66 setsize(2,2,2,1.,1.,1.);
67 nthread_ = nthread;
68 setalloc();
69 setpointers(false);
70 init_fftw();
[3154]71}
72
[3349]73GeneFluct3D::~GeneFluct3D(void)
[3157]74{
[3349]75 delete_fftw();
[3157]76}
77
[3349]78//-------------------------------------------------------
79void GeneFluct3D::init_default(void)
[3157]80{
[3349]81 Nx_ = Ny_ = Nz_ = 0;
[3518]82 is_set_fft_plan = false;
[3349]83 nthread_ = 0;
84 lp_ = 0;
85 array_allocated_ = false;
86 cosmo_ = NULL;
87 growth_ = NULL;
88 redsh_ref_ = -999.;
89 kredsh_ref_ = 0.;
90 dred_ref_ = -999.;
91 loscom_ref_ = -999.;
92 dtrc_ref_ = dlum_ref_ = dang_ref_ = -999.;
93 nu_ref_ = dnu_ref_ = -999.;
94 loscom_min_ = loscom_max_ = -999.;
95 loscom2zred_min_ = loscom2zred_max_ = 0.;
96 xobs_[0] = xobs_[1] = xobs_[2] = 0.;
97 zred_.resize(0);
98 loscom_.resize(0);
99 loscom2zred_.resize(0);
[3157]100}
101
[3141]102void GeneFluct3D::setsize(long nx,long ny,long nz,double dx,double dy,double dz)
103{
[3155]104 if(lp_>1) cout<<"--- GeneFluct3D::setsize: N="<<nx<<","<<ny<<","<<nz
105 <<" D="<<dx<<","<<dy<<","<<dz<<endl;
[3141]106 if(nx<=0 || dx<=0.) {
[3267]107 char *bla = "GeneFluct3D::setsize_Error: bad value(s) for nn/dx";
[3199]108 cout<<bla<<endl; throw ParmError(bla);
[3115]109 }
110
[3141]111 // Les tailles des tableaux
[3115]112 Nx_ = nx;
113 Ny_ = ny; if(Ny_ <= 0) Ny_ = Nx_;
114 Nz_ = nz; if(Nz_ <= 0) Nz_ = Nx_;
[3141]115 N_.resize(0); N_.push_back(Nx_); N_.push_back(Ny_); N_.push_back(Nz_);
[3115]116 NRtot_ = Nx_*Ny_*Nz_; // nombre de pixels dans le survey
117 NCz_ = Nz_/2 +1;
118 NTz_ = 2*NCz_;
119
120 // le pas dans l'espace (Mpc)
121 Dx_ = dx;
122 Dy_ = dy; if(Dy_ <= 0.) Dy_ = Dx_;
123 Dz_ = dz; if(Dz_ <= 0.) Dz_ = Dx_;
[3141]124 D_.resize(0); D_.push_back(Dx_); D_.push_back(Dy_); D_.push_back(Dz_);
[3115]125 dVol_ = Dx_*Dy_*Dz_;
126 Vol_ = (Nx_*Dx_)*(Ny_*Dy_)*(Nz_*Dz_);
127
128 // Le pas dans l'espace de Fourier (Mpc^-1)
129 Dkx_ = 2.*M_PI/(Nx_*Dx_);
130 Dky_ = 2.*M_PI/(Ny_*Dy_);
131 Dkz_ = 2.*M_PI/(Nz_*Dz_);
[3141]132 Dk_.resize(0); Dk_.push_back(Dkx_); Dk_.push_back(Dky_); Dk_.push_back(Dkz_);
[3115]133 Dk3_ = Dkx_*Dky_*Dkz_;
134
135 // La frequence de Nyquist en k (Mpc^-1)
136 Knyqx_ = M_PI/Dx_;
137 Knyqy_ = M_PI/Dy_;
138 Knyqz_ = M_PI/Dz_;
[3141]139 Knyq_.resize(0); Knyq_.push_back(Knyqx_); Knyq_.push_back(Knyqy_); Knyq_.push_back(Knyqz_);
140}
[3115]141
[3141]142void GeneFluct3D::setalloc(void)
143{
[3155]144 if(lp_>1) cout<<"--- GeneFluct3D::setalloc ---"<<endl;
[3141]145 // Dimensionnement du tableau complex<r_8>
146 // ATTENTION: TArray adresse en memoire a l'envers du C
147 // Tarray(n1,n2,n3) == Carray[n3][n2][n1]
148 sa_size_t SzK_[3] = {NCz_,Ny_,Nx_}; // a l'envers
149 try {
150 T_.ReSize(3,SzK_);
151 array_allocated_ = true;
[3518]152 if(lp_>1) cout<<" allocating: "<<T_.Size()*sizeof(complex<GEN3D_TYPE>)/1.e6<<" Mo"<<endl;
[3141]153 } catch (...) {
[3155]154 cout<<"GeneFluct3D::setalloc_Error: Problem allocating T_"<<endl;
[3141]155 }
156 T_.SetMemoryMapping(BaseArray::CMemoryMapping);
[3115]157}
158
[3141]159void GeneFluct3D::setpointers(bool from_real)
160{
[3155]161 if(lp_>1) cout<<"--- GeneFluct3D::setpointers ---"<<endl;
[3141]162 if(from_real) T_ = ArrCastR2C(R_);
163 else R_ = ArrCastC2R(T_);
164 // On remplit les pointeurs
[3518]165 fdata_ = (GEN3D_FFTW_COMPLEX *) (&T_(0,0,0));
166 data_ = (GEN3D_TYPE *) (&R_(0,0,0));
[3141]167}
168
[3349]169void GeneFluct3D::init_fftw(void)
[3141]170{
[3518]171 if( is_set_fft_plan ) delete_fftw();
[3141]172
[3154]173 // --- Initialisation de fftw3 (attention data est sur-ecrit a l'init)
[3155]174 if(lp_>1) cout<<"--- GeneFluct3D::init_fftw ---"<<endl;
[3349]175#ifdef WITH_FFTW_THREAD
[3154]176 if(nthread_>0) {
[3155]177 cout<<"...Computing with "<<nthread_<<" threads"<<endl;
[3518]178 GEN3D_FFTW_INIT_THREADS();
179 GEN3D_FFTW_PLAN_WITH_NTHREADS(nthread_);
[3154]180 }
181#endif
[3155]182 if(lp_>1) cout<<"...forward plan"<<endl;
[3518]183 pf_ = GEN3D_FFTW_PLAN_DFT_R2C_3D(Nx_,Ny_,Nz_,data_,fdata_,FFTW_ESTIMATE);
[3155]184 if(lp_>1) cout<<"...backward plan"<<endl;
[3518]185 pb_ = GEN3D_FFTW_PLAN_DFT_C2R_3D(Nx_,Ny_,Nz_,fdata_,data_,FFTW_ESTIMATE);
186 is_set_fft_plan = true;
[3154]187}
[3141]188
[3349]189void GeneFluct3D::delete_fftw(void)
190{
[3518]191 if( !is_set_fft_plan ) return;
192 GEN3D_FFTW_DESTROY_PLAN(pf_);
193 GEN3D_FFTW_DESTROY_PLAN(pb_);
[3349]194#ifdef WITH_FFTW_THREAD
[3518]195 if(nthread_>0) GEN3D_FFTW_CLEANUP_THREADS();
[3349]196#endif
[3518]197 is_set_fft_plan = false;
[3349]198}
199
200void GeneFluct3D::check_array_alloc(void)
201// Pour tester si le tableau T_ est alloue
202{
203 if(array_allocated_) return;
204 char bla[90];
205 sprintf(bla,"GeneFluct3D::check_array_alloc_Error: array is not allocated");
206 cout<<bla<<endl; throw ParmError(bla);
207}
208
[3157]209//-------------------------------------------------------
[3349]210void GeneFluct3D::SetObservator(double redshref,double kredshref)
211// L'observateur est au redshift z=0
212// est situe sur la "perpendiculaire" a la face x,y
213// issue du centre de cette face
214// Il faut positionner le cube sur l'axe des z cad des redshifts:
215// redshref = redshift de reference
216// Si redshref<0 alors redshref=0
217// kredshref = indice (en double) correspondant a ce redshift
218// Si kredshref<0 alors kredshref=nz/2 (milieu du cube)
219// Exemple: redshref=1.5 kredshref=250.75
220// -> Le pixel i=nx/2 j=ny/2 k=250.75 est au redshift 1.5
221{
222 if(redshref<0.) redshref = 0.;
223 if(kredshref<0.) {
224 if(Nz_<=0) {
225 char *bla = "GeneFluct3D::SetObservator_Error: for kredsh_ref<0 define cube geometry first";
226 cout<<bla<<endl; throw ParmError(bla);
227 }
228 kredshref = Nz_/2.;
229 }
230 redsh_ref_ = redshref;
231 kredsh_ref_ = kredshref;
232 if(lp_>0)
233 cout<<"--- GeneFluct3D::SetObservator zref="<<redsh_ref_<<" kref="<<kredsh_ref_<<endl;
234}
235
236void GeneFluct3D::SetCosmology(CosmoCalc& cosmo)
237{
238 cosmo_ = &cosmo;
239 if(lp_>1) cosmo_->Print();
240}
241
242void GeneFluct3D::SetGrowthFactor(GrowthFactor& growth)
243{
244 growth_ = &growth;
245}
246
[3199]247long GeneFluct3D::LosComRedshift(double zinc,long npoints)
[3157]248// Given a position of the cube relative to the observer
249// and a cosmology
250// (SetObservator() and SetCosmology() should have been called !)
251// This routine filled:
252// the vector "zred_" of scanned redshift (by zinc increments)
253// the vector "loscom_" of corresponding los comoving distance
[3199]254// -- Input:
255// zinc : redshift increment for computation
256// npoints : number of points required for inverting loscom -> zred
[3157]257//
258{
[3199]259 if(lp_>0) cout<<"--- LosComRedshift: zinc="<<zinc<<" , npoints="<<npoints<<endl;
[3154]260
[3271]261 if(cosmo_ == NULL || redsh_ref_<0.) {
[3199]262 char *bla = "GeneFluct3D::LosComRedshift_Error: set Observator and Cosmology first";
263 cout<<bla<<endl; throw ParmError(bla);
[3157]264 }
265
[3271]266 // La distance angulaire/luminosite/Dnu au pixel de reference
267 dred_ref_ = Dz_/(cosmo_->Dhubble()/cosmo_->E(redsh_ref_));
268 loscom_ref_ = cosmo_->Dloscom(redsh_ref_);
269 dtrc_ref_ = cosmo_->Dtrcom(redsh_ref_);
270 dlum_ref_ = cosmo_->Dlum(redsh_ref_);
271 dang_ref_ = cosmo_->Dang(redsh_ref_);
272 nu_ref_ = Fr_HyperFin_Par/(1.+redsh_ref_); // GHz
273 dnu_ref_ = Fr_HyperFin_Par *dred_ref_/pow(1.+redsh_ref_,2.); // GHz
274 if(lp_>0) {
275 cout<<"...reference pixel redshref="<<redsh_ref_
276 <<", dredref="<<dred_ref_
277 <<", nuref="<<nu_ref_ <<" GHz"
278 <<", dnuref="<<dnu_ref_ <<" GHz"<<endl
279 <<" dlosc="<<loscom_ref_<<" Mpc com"
280 <<", dtrc="<<dtrc_ref_<<" Mpc com"
281 <<", dlum="<<dlum_ref_<<" Mpc"
282 <<", dang="<<dang_ref_<<" Mpc"<<endl;
283 }
284
[3199]285 // On calcule les coordonnees de l'observateur dans le repere du cube
286 // cad dans le repere ou l'origine est au centre du pixel i=j=l=0.
287 // L'observateur est sur un axe centre sur le milieu de la face Oxy
[3157]288 xobs_[0] = Nx_/2.*Dx_;
289 xobs_[1] = Ny_/2.*Dy_;
[3271]290 xobs_[2] = kredsh_ref_*Dz_ - loscom_ref_;
[3157]291
292 // L'observateur est-il dans le cube?
293 bool obs_in_cube = false;
294 if(xobs_[2]>=0. && xobs_[2]<=Nz_*Dz_) obs_in_cube = true;
295
296 // Find MINIMUM los com distance to the observer:
297 // c'est le centre de la face a k=0
298 // (ou zero si l'observateur est dans le cube)
299 loscom_min_ = 0.;
300 if(!obs_in_cube) loscom_min_ = -xobs_[2];
301
[3271]302 // TO BE FIXED TO BE FIXED TO BE FIXED TO BE FIXED TO BE FIXED TO BE FIXED
303 if(loscom_min_<=1.e-50)
304 for(int i=0;i<50;i++)
305 cout<<"ATTENTION TOUTES LES PARTIES DU CODE NE MARCHENT PAS POUR UN OBSERVATEUR DANS LE CUBE"<<endl;
306 // TO BE FIXED TO BE FIXED TO BE FIXED TO BE FIXED TO BE FIXED TO BE FIXED
307
308
[3157]309 // Find MAXIMUM los com distance to the observer:
310 // ou que soit positionne l'observateur, la distance
311 // maximal est sur un des coins du cube
312 loscom_max_ = 0.;
313 for(long i=0;i<=1;i++) {
[3271]314 double dx2 = DXcom(i*(Nx_-1)); dx2 *= dx2;
[3157]315 for(long j=0;j<=1;j++) {
[3271]316 double dy2 = DYcom(j*(Ny_-1)); dy2 *= dy2;
[3157]317 for(long k=0;k<=1;k++) {
[3271]318 double dz2 = DZcom(k*(Nz_-1)); dz2 *= dz2;
[3157]319 dz2 = sqrt(dx2+dy2+dz2);
320 if(dz2>loscom_max_) loscom_max_ = dz2;
321 }
322 }
323 }
324 if(lp_>0) {
[3271]325 cout<<"...zref="<<redsh_ref_<<" kzref="<<kredsh_ref_<<" losref="<<loscom_ref_<<" Mpc\n"
[3157]326 <<" xobs="<<xobs_[0]<<" , "<<xobs_[1]<<" , "<<xobs_[2]<<" Mpc "
327 <<" in_cube="<<obs_in_cube
[3271]328 <<" loscom_min="<<loscom_min_<<" loscom_max="<<loscom_max_<<" Mpc (com)"<<endl;
[3157]329 }
330
[3199]331 // Fill the corresponding vectors for loscom and zred
[3267]332 // Be shure to have one dlc <loscom_min and one >loscom_max
[3199]333 if(zinc<=0.) zinc = 0.01;
[3157]334 for(double z=0.; ; z+=zinc) {
335 double dlc = cosmo_->Dloscom(z);
336 if(dlc<loscom_min_) {zred_.resize(0); loscom_.resize(0);}
337 zred_.push_back(z);
338 loscom_.push_back(dlc);
339 z += zinc;
[3199]340 if(dlc>loscom_max_) break; // on sort apres avoir stoque un dlc>dlcmax
[3157]341 }
342
343 if(lp_>0) {
[3199]344 long n = zred_.size();
345 cout<<"...zred/loscom tables[zinc="<<zinc<<"]: n="<<n;
[3157]346 if(n>0) cout<<" z="<<zred_[0]<<" -> d="<<loscom_[0];
347 if(n>1) cout<<" , z="<<zred_[n-1]<<" -> d="<<loscom_[n-1];
348 cout<<endl;
349 }
350
[3199]351 // Compute the parameters and tables needed for inversion loscom->zred
352 if(npoints<3) npoints = zred_.size();
353 InverseFunc invfun(zred_,loscom_);
354 invfun.ComputeParab(npoints,loscom2zred_);
355 loscom2zred_min_ = invfun.YMin();
356 loscom2zred_max_ = invfun.YMax();
357
358 if(lp_>0) {
359 long n = loscom2zred_.size();
360 cout<<"...loscom -> zred[npoints="<<npoints<<"]: n="<<n
361 <<" los_min="<<loscom2zred_min_
362 <<" los_max="<<loscom2zred_max_
363 <<" -> zred=[";
364 if(n>0) cout<<loscom2zred_[0];
365 cout<<",";
366 if(n>1) cout<<loscom2zred_[n-1];
367 cout<<"]"<<endl;
368 if(lp_>1 && n>0)
369 for(int i=0;i<n;i++)
[3330]370 if(i<2 || abs(i-n/2)<2 || i>=n-2)
371 cout<<" i="<<i
372 <<" d="<<loscom2zred_min_+i*(loscom2zred_max_-loscom2zred_min_)/(n-1.)
373 <<" Mpc z="<<loscom2zred_[i]<<endl;
[3199]374 }
375
376 return zred_.size();
[3157]377}
378
[3115]379//-------------------------------------------------------
[3141]380void GeneFluct3D::WriteFits(string cfname,int bitpix)
381{
[3155]382 cout<<"--- GeneFluct3D::WriteFits: Writing Cube to "<<cfname<<endl;
[3141]383 try {
384 FitsImg3DWriter fwrt(cfname.c_str(),bitpix,5);
385 fwrt.WriteKey("NX",Nx_," axe transverse 1");
386 fwrt.WriteKey("NY",Ny_," axe transverse 2");
387 fwrt.WriteKey("NZ",Nz_," axe longitudinal (redshift)");
388 fwrt.WriteKey("DX",Dx_," Mpc");
389 fwrt.WriteKey("DY",Dy_," Mpc");
390 fwrt.WriteKey("DZ",Dz_," Mpc");
391 fwrt.WriteKey("DKX",Dkx_," Mpc^-1");
392 fwrt.WriteKey("DKY",Dky_," Mpc^-1");
393 fwrt.WriteKey("DKZ",Dkz_," Mpc^-1");
[3271]394 fwrt.WriteKey("ZREF",redsh_ref_," reference redshift");
395 fwrt.WriteKey("KZREF",kredsh_ref_," reference redshift on z axe");
[3141]396 fwrt.Write(R_);
397 } catch (PThrowable & exc) {
398 cout<<"Exception : "<<(string)typeid(exc).name()
399 <<" - Msg= "<<exc.Msg()<<endl;
400 return;
401 } catch (...) {
402 cout<<" some other exception was caught !"<<endl;
403 return;
404 }
405}
406
407void GeneFluct3D::ReadFits(string cfname)
408{
[3155]409 cout<<"--- GeneFluct3D::ReadFits: Reading Cube from "<<cfname<<endl;
[3141]410 try {
411 FitsImg3DRead fimg(cfname.c_str(),0,5);
412 fimg.Read(R_);
413 long nx = fimg.ReadKeyL("NX");
414 long ny = fimg.ReadKeyL("NY");
415 long nz = fimg.ReadKeyL("NZ");
416 double dx = fimg.ReadKey("DX");
417 double dy = fimg.ReadKey("DY");
418 double dz = fimg.ReadKey("DZ");
[3154]419 double zref = fimg.ReadKey("ZREF");
420 double kzref = fimg.ReadKey("KZREF");
[3141]421 setsize(nx,ny,nz,dx,dy,dz);
422 setpointers(true);
[3154]423 init_fftw();
424 SetObservator(zref,kzref);
[3330]425 array_allocated_ = true;
[3141]426 } catch (PThrowable & exc) {
427 cout<<"Exception : "<<(string)typeid(exc).name()
428 <<" - Msg= "<<exc.Msg()<<endl;
429 return;
430 } catch (...) {
431 cout<<" some other exception was caught !"<<endl;
432 return;
433 }
434}
435
436void GeneFluct3D::WritePPF(string cfname,bool write_real)
437// On ecrit soit le TArray<r_8> ou le TArray<complex <r_8> >
438{
[3155]439 cout<<"--- GeneFluct3D::WritePPF: Writing Cube (real="<<write_real<<") to "<<cfname<<endl;
[3141]440 try {
441 R_.Info()["NX"] = (int_8)Nx_;
442 R_.Info()["NY"] = (int_8)Ny_;
443 R_.Info()["NZ"] = (int_8)Nz_;
444 R_.Info()["DX"] = (r_8)Dx_;
445 R_.Info()["DY"] = (r_8)Dy_;
446 R_.Info()["DZ"] = (r_8)Dz_;
[3271]447 R_.Info()["ZREF"] = (r_8)redsh_ref_;
448 R_.Info()["KZREF"] = (r_8)kredsh_ref_;
[3141]449 POutPersist pos(cfname.c_str());
450 if(write_real) pos << PPFNameTag("rgen") << R_;
451 else pos << PPFNameTag("pkgen") << T_;
452 } catch (PThrowable & exc) {
453 cout<<"Exception : "<<(string)typeid(exc).name()
454 <<" - Msg= "<<exc.Msg()<<endl;
455 return;
456 } catch (...) {
457 cout<<" some other exception was caught !"<<endl;
458 return;
459 }
460}
461
462void GeneFluct3D::ReadPPF(string cfname)
463{
[3155]464 cout<<"--- GeneFluct3D::ReadPPF: Reading Cube from "<<cfname<<endl;
[3141]465 try {
466 bool from_real = true;
467 PInPersist pis(cfname.c_str());
468 string name_tag_k = "pkgen";
469 bool found_tag_k = pis.GotoNameTag("pkgen");
470 if(found_tag_k) {
[3262]471 cout<<" ...reading spectrum into TArray<complex <r_8> >"<<endl;
[3141]472 pis >> PPFNameTag("pkgen") >> T_;
473 from_real = false;
474 } else {
475 cout<<" ...reading space into TArray<r_8>"<<endl;
476 pis >> PPFNameTag("rgen") >> R_;
477 }
[3154]478 setpointers(from_real); // a mettre ici pour relire les DVInfo
[3141]479 int_8 nx = R_.Info()["NX"];
480 int_8 ny = R_.Info()["NY"];
481 int_8 nz = R_.Info()["NZ"];
482 r_8 dx = R_.Info()["DX"];
483 r_8 dy = R_.Info()["DY"];
484 r_8 dz = R_.Info()["DZ"];
[3154]485 r_8 zref = R_.Info()["ZREF"];
486 r_8 kzref = R_.Info()["KZREF"];
[3141]487 setsize(nx,ny,nz,dx,dy,dz);
[3154]488 init_fftw();
489 SetObservator(zref,kzref);
[3330]490 array_allocated_ = true;
[3141]491 } catch (PThrowable & exc) {
492 cout<<"Exception : "<<(string)typeid(exc).name()
493 <<" - Msg= "<<exc.Msg()<<endl;
494 return;
495 } catch (...) {
496 cout<<" some other exception was caught !"<<endl;
497 return;
498 }
499}
500
[3281]501void GeneFluct3D::WriteSlicePPF(string cfname)
502// On ecrit 3 tranches du cube selon chaque axe
503{
[3283]504 cout<<"--- GeneFluct3D::WriteSlicePPF: Writing Cube Slices "<<cfname<<endl;
[3281]505 try {
506
507 POutPersist pos(cfname.c_str());
508 TMatrix<r_4> S;
509 char str[16];
510 long i,j,l;
511
512 // Tranches en Z
513 for(int s=0;s<3;s++) {
514 S.ReSize(Nx_,Ny_);
515 if(s==0) l=0; else if(s==1) l=(Nz_+1)/2; else l=Nz_-1;
[3289]516 sprintf(str,"z%ld",l);
[3281]517 for(i=0;i<Nx_;i++) for(j=0;j<Ny_;j++) S(i,j)=data_[IndexR(i,j,l)];
518 pos<<PPFNameTag(str)<<S; S.RenewObjId();
519 }
520
521 // Tranches en Y
522 for(int s=0;s<3;s++) {
523 S.ReSize(Nz_,Nx_);
524 if(s==0) j=0; else if(s==1) j=(Ny_+1)/2; else j=Ny_-1;
[3289]525 sprintf(str,"y%ld",j);
[3281]526 for(i=0;i<Nx_;i++) for(l=0;l<Nz_;l++) S(l,i)=data_[IndexR(i,j,l)];
527 pos<<PPFNameTag(str)<<S; S.RenewObjId();
528 }
529
530 // Tranches en X
531 for(int s=0;s<3;s++) {
532 S.ReSize(Nz_,Ny_);
533 if(s==0) i=0; else if(s==1) i=(Nx_+1)/2; else i=Nx_-1;
[3289]534 sprintf(str,"x%ld",i);
[3281]535 for(j=0;j<Ny_;j++) for(l=0;l<Nz_;l++) S(l,j)=data_[IndexR(i,j,l)];
536 pos<<PPFNameTag(str)<<S; S.RenewObjId();
537 }
538
539 } catch (PThrowable & exc) {
540 cout<<"Exception : "<<(string)typeid(exc).name()
541 <<" - Msg= "<<exc.Msg()<<endl;
542 return;
543 } catch (...) {
544 cout<<" some other exception was caught !"<<endl;
545 return;
546 }
547}
548
[3141]549//-------------------------------------------------------
[3115]550void GeneFluct3D::Print(void)
551{
[3141]552 cout<<"GeneFluct3D(T_alloc="<<array_allocated_<<"):"<<endl;
[3115]553 cout<<"Space Size : nx="<<Nx_<<" ny="<<Ny_<<" nz="<<Nz_<<" ("<<NTz_<<") size="
554 <<NRtot_<<endl;
555 cout<<" Resol: dx="<<Dx_<<" dy="<<Dy_<<" dz="<<Dz_<<" Mpc"
556 <<", dVol="<<dVol_<<", Vol="<<Vol_<<" Mpc^3"<<endl;
557 cout<<"Fourier Size : nx="<<Nx_<<" ny="<<Ny_<<" nz="<<NCz_<<endl;
558 cout<<" Resol: dkx="<<Dkx_<<" dky="<<Dky_<<" dkz="<<Dkz_<<" Mpc^-1"
559 <<", Dk3="<<Dk3_<<" Mpc^-3"<<endl;
560 cout<<" (2Pi/k: "<<2.*M_PI/Dkx_<<" "<<2.*M_PI/Dky_<<" "<<2.*M_PI/Dkz_<<" Mpc)"<<endl;
561 cout<<" Nyquist: kx="<<Knyqx_<<" ky="<<Knyqy_<<" kz="<<Knyqz_<<" Mpc^-1"
562 <<", Kmax="<<GetKmax()<<" Mpc^-1"<<endl;
563 cout<<" (2Pi/k: "<<2.*M_PI/Knyqx_<<" "<<2.*M_PI/Knyqy_<<" "<<2.*M_PI/Knyqz_<<" Mpc)"<<endl;
[3271]564 cout<<"Redshift "<<redsh_ref_<<" for z axe at k="<<kredsh_ref_<<endl;
[3115]565}
566
567//-------------------------------------------------------
[3141]568void GeneFluct3D::ComputeFourier0(GenericFunc& pk_at_z)
[3115]569// cf ComputeFourier() mais avec autre methode de realisation du spectre
570// (attention on fait une fft pour realiser le spectre)
571{
572
573 // --- realisation d'un tableau de tirage gaussiens
[3155]574 if(lp_>0) cout<<"--- ComputeFourier0: before gaussian filling ---"<<endl;
[3115]575 // On tient compte du pb de normalisation de FFTW3
576 double sntot = sqrt((double)NRtot_);
[3129]577 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]578 int_8 ip = IndexR(i,j,l);
579 data_[ip] = NorRand()/sntot;
[3115]580 }
581
582 // --- realisation d'un tableau de tirage gaussiens
[3155]583 if(lp_>0) cout<<"...before fft real ---"<<endl;
[3518]584 GEN3D_FFTW_EXECUTE(pf_);
[3115]585
586 // --- On remplit avec une realisation
[3157]587 if(lp_>0) cout<<"...before Fourier realization filling"<<endl;
[3518]588 T_(0,0,0) = complex<GEN3D_TYPE>(0.); // on coupe le continue et on l'initialise
[3129]589 long lmod = Nx_/10; if(lmod<1) lmod=1;
590 for(long i=0;i<Nx_;i++) {
591 long ii = (i>Nx_/2) ? Nx_-i : i;
[3115]592 double kx = ii*Dkx_; kx *= kx;
[3155]593 if(lp_>0 && i%lmod==0) cout<<"i="<<i<<" ii="<<ii<<endl;
[3129]594 for(long j=0;j<Ny_;j++) {
595 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]596 double ky = jj*Dky_; ky *= ky;
[3129]597 for(long l=0;l<NCz_;l++) {
[3115]598 double kz = l*Dkz_; kz *= kz;
599 if(i==0 && j==0 && l==0) continue; // Suppression du continu
600 double k = sqrt(kx+ky+kz);
601 // cf normalisation: Peacock, Cosmology, formule 16.38 p504
[3141]602 double pk = pk_at_z(k)/Vol_;
[3115]603 // ici pas de "/2" a cause de la remarque ci-dessus
604 T_(l,j,i) *= sqrt(pk);
605 }
606 }
607 }
608
[3155]609 if(lp_>0) cout<<"...computing power"<<endl;
[3115]610 double p = compute_power_carte();
[3155]611 if(lp_>0) cout<<"Puissance dans la realisation: "<<p<<endl;
[3115]612
613}
614
615//-------------------------------------------------------
[3141]616void GeneFluct3D::ComputeFourier(GenericFunc& pk_at_z)
617// Calcule une realisation du spectre "pk_at_z"
[3115]618// Attention: dans TArray le premier indice varie le + vite
619// Explication normalisation: see Coles & Lucchin, Cosmology, p264-265
620// FFTW3: on note N=Nx*Ny*Nz
621// f --(FFT)--> F = TF(f) --(FFT^-1)--> fb = TF^-1(F) = TF^-1(TF(f))
622// sum(f(x_i)^2) = S
623// sum(F(nu_i)^2) = S*N
624// sum(fb(x_i)^2) = S*N^2
625{
626 // --- RaZ du tableau
[3518]627 T_ = complex<GEN3D_TYPE>(0.);
[3115]628
629 // --- On remplit avec une realisation
[3155]630 if(lp_>0) cout<<"--- ComputeFourier ---"<<endl;
[3129]631 long lmod = Nx_/10; if(lmod<1) lmod=1;
632 for(long i=0;i<Nx_;i++) {
633 long ii = (i>Nx_/2) ? Nx_-i : i;
[3115]634 double kx = ii*Dkx_; kx *= kx;
[3155]635 if(lp_>0 && i%lmod==0) cout<<"i="<<i<<" ii="<<ii<<endl;
[3129]636 for(long j=0;j<Ny_;j++) {
637 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]638 double ky = jj*Dky_; ky *= ky;
[3129]639 for(long l=0;l<NCz_;l++) {
[3115]640 double kz = l*Dkz_; kz *= kz;
641 if(i==0 && j==0 && l==0) continue; // Suppression du continu
642 double k = sqrt(kx+ky+kz);
643 // cf normalisation: Peacock, Cosmology, formule 16.38 p504
[3141]644 double pk = pk_at_z(k)/Vol_;
[3115]645 // Explication de la division par 2: voir perandom.cc
646 // ou egalement Coles & Lucchin, Cosmology formula 13.7.2 p279
647 T_(l,j,i) = ComplexGaussRan(sqrt(pk/2.));
648 }
649 }
650 }
651
652 manage_coefficients(); // gros effet pour les spectres que l'on utilise !
653
[3155]654 if(lp_>0) cout<<"...computing power"<<endl;
[3115]655 double p = compute_power_carte();
[3155]656 if(lp_>0) cout<<"Puissance dans la realisation: "<<p<<endl;
[3115]657
658}
659
[3129]660long GeneFluct3D::manage_coefficients(void)
[3115]661// Take into account the real and complexe conjugate coefficients
662// because we want a realization of a real data in real space
663{
[3155]664 if(lp_>1) cout<<"...managing coefficients"<<endl;
[3141]665 check_array_alloc();
[3115]666
667 // 1./ Le Continu et Nyquist sont reels
[3129]668 long nreal = 0;
669 for(long kk=0;kk<2;kk++) {
670 long k=0; // continu
[3115]671 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]672 for(long jj=0;jj<2;jj++) {
673 long j=0;
[3115]674 if(jj==1) {if( Ny_%2!=0) continue; else j = Ny_/2;}
[3129]675 for(long ii=0;ii<2;ii++) {
676 long i=0;
[3115]677 if(ii==1) {if( Nx_%2!=0) continue; else i = Nx_/2;}
[3141]678 int_8 ip = IndexC(i,j,k);
679 //cout<<"i="<<i<<" j="<<j<<" k="<<k<<" = ("<<fdata_[ip][0]<<","<<fdata_[ip][1]<<")"<<endl;
680 fdata_[ip][1] = 0.; fdata_[ip][0] *= M_SQRT2;
[3115]681 nreal++;
682 }
683 }
684 }
[3155]685 if(lp_>1) cout<<"Number of forced real number ="<<nreal<<endl;
[3115]686
687 // 2./ Les elements complexe conjugues (tous dans le plan k=0,Nyquist)
688
689 // a./ les lignes et colonnes du continu et de nyquist
[3129]690 long nconj1 = 0;
691 for(long kk=0;kk<2;kk++) {
692 long k=0; // continu
[3115]693 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]694 for(long jj=0;jj<2;jj++) { // selon j
695 long j=0;
[3115]696 if(jj==1) {if( Ny_%2!=0) continue; else j = Ny_/2;}
[3129]697 for(long i=1;i<(Nx_+1)/2;i++) {
[3141]698 int_8 ip = IndexC(i,j,k);
699 int_8 ip1 = IndexC(Nx_-i,j,k);
700 fdata_[ip1][0] = fdata_[ip][0]; fdata_[ip1][1] = -fdata_[ip][1];
[3115]701 nconj1++;
702 }
703 }
[3129]704 for(long ii=0;ii<2;ii++) {
705 long i=0;
[3115]706 if(ii==1) {if( Nx_%2!=0) continue; else i = Nx_/2;}
[3129]707 for(long j=1;j<(Ny_+1)/2;j++) {
[3141]708 int_8 ip = IndexC(i,j,k);
709 int_8 ip1 = IndexC(i,Ny_-j,k);
710 fdata_[ip1][0] = fdata_[ip][0]; fdata_[ip1][1] = -fdata_[ip][1];
[3115]711 nconj1++;
712 }
713 }
714 }
[3155]715 if(lp_>1) cout<<"Number of forced conjugate on cont+nyq ="<<nconj1<<endl;
[3115]716
717 // b./ les lignes et colonnes hors continu et de nyquist
[3129]718 long nconj2 = 0;
719 for(long kk=0;kk<2;kk++) {
720 long k=0; // continu
[3115]721 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]722 for(long j=1;j<(Ny_+1)/2;j++) {
[3115]723 if(Ny_%2==0 && j==Ny_/2) continue; // on ne retraite pas nyquist en j
[3129]724 for(long i=1;i<Nx_;i++) {
[3115]725 if(Nx_%2==0 && i==Nx_/2) continue; // on ne retraite pas nyquist en i
[3141]726 int_8 ip = IndexC(i,j,k);
727 int_8 ip1 = IndexC(Nx_-i,Ny_-j,k);
728 fdata_[ip1][0] = fdata_[ip][0]; fdata_[ip1][1] = -fdata_[ip][1];
[3115]729 nconj2++;
730 }
731 }
732 }
[3155]733 if(lp_>1) cout<<"Number of forced conjugate hors cont+nyq ="<<nconj2<<endl;
[3115]734
[3155]735 if(lp_>1) cout<<"Check: ddl= "<<NRtot_<<" =?= "<<2*(Nx_*Ny_*NCz_-nconj1-nconj2)-8<<endl;
[3115]736
737 return nreal+nconj1+nconj2;
738}
739
740double GeneFluct3D::compute_power_carte(void)
741// Calcul la puissance de la realisation du spectre Pk
742{
[3141]743 check_array_alloc();
744
[3115]745 double s2 = 0.;
[3129]746 for(long l=0;l<NCz_;l++)
747 for(long j=0;j<Ny_;j++)
748 for(long i=0;i<Nx_;i++) s2 += MODULE2(T_(l,j,i));
[3115]749
750 double s20 = 0.;
[3129]751 for(long j=0;j<Ny_;j++)
752 for(long i=0;i<Nx_;i++) s20 += MODULE2(T_(0,j,i));
[3115]753
754 double s2n = 0.;
755 if(Nz_%2==0)
[3129]756 for(long j=0;j<Ny_;j++)
757 for(long i=0;i<Nx_;i++) s2n += MODULE2(T_(NCz_-1,j,i));
[3115]758
759 return 2.*s2 -s20 -s2n;
760}
761
762//-------------------------------------------------------------------
763void GeneFluct3D::FilterByPixel(void)
764// Filtrage par la fonction fenetre du pixel (parallelepipede)
[3120]765// TF = 1/(dx*dy*dz)*Int[{-dx/2,dx/2},{-dy/2,dy/2},{-dz/2,dz/2}]
[3115]766// e^(ik_x*x) e^(ik_y*y) e^(ik_z*z) dxdydz
[3120]767// = 2/(k_x*dx) * sin(k_x*dx/2) * (idem y) * (idem z)
768// Gestion divergence en 0: sin(y)/y = 1 - y^2/6*(1-y^2/20)
769// avec y = k_x*dx/2
[3115]770{
[3155]771 if(lp_>0) cout<<"--- FilterByPixel ---"<<endl;
[3141]772 check_array_alloc();
773
[3129]774 for(long i=0;i<Nx_;i++) {
775 long ii = (i>Nx_/2) ? Nx_-i : i;
[3120]776 double kx = ii*Dkx_ *Dx_/2;
[3330]777 double pk_x = pixelfilter(kx);
[3129]778 for(long j=0;j<Ny_;j++) {
779 long jj = (j>Ny_/2) ? Ny_-j : j;
[3120]780 double ky = jj*Dky_ *Dy_/2;
[3330]781 double pk_y = pixelfilter(ky);
[3129]782 for(long l=0;l<NCz_;l++) {
[3120]783 double kz = l*Dkz_ *Dz_/2;
[3141]784 double pk_z = pixelfilter(kz);
785 T_(l,j,i) *= pk_x*pk_y*pk_z;
[3115]786 }
787 }
788 }
789
790}
791
792//-------------------------------------------------------------------
[3331]793void GeneFluct3D::ApplyGrowthFactor(int type_evol)
[3157]794// Apply Growth to real space
795// Using the correspondance between redshift and los comoving distance
796// describe in vector "zred_" "loscom_"
[3516]797// type_evol = 1 : evolution avec la distance a l'observateur
798// 2 : evolution avec la distance du plan Z
[3331]799// (tous les pixels d'un plan Z sont mis au meme redshift z que celui du milieu)
[3157]800{
[3331]801 if(lp_>0) cout<<"--- ApplyGrowthFactor: evol="<<type_evol<<endl;
[3157]802 check_array_alloc();
803
804 if(growth_ == NULL) {
[3199]805 char *bla = "GeneFluct3D::ApplyGrowthFactor_Error: set GrowthFactor first";
806 cout<<bla<<endl; throw ParmError(bla);
[3157]807 }
[3331]808 if(type_evol<1 || type_evol>2) {
809 char *bla = "GeneFluct3D::ApplyGrowthFactor_Error: bad type_evol value";
810 cout<<bla<<endl; throw ParmError(bla);
811 }
[3157]812
[3199]813 InterpFunc interpinv(loscom2zred_min_,loscom2zred_max_,loscom2zred_);
[3157]814 unsigned short ok;
815
816 //CHECK: Histo hgr(0.9*zred_[0],1.1*zred_[n-1],1000);
817 for(long i=0;i<Nx_;i++) {
[3331]818 double dx2 = DXcom(i); dx2 *= dx2;
[3157]819 for(long j=0;j<Ny_;j++) {
[3331]820 double dy2 = DYcom(j); dy2 *= dy2;
[3157]821 for(long l=0;l<Nz_;l++) {
[3331]822 double dz = DZcom(l);
823 if(type_evol==1) dz = sqrt(dx2+dy2+dz*dz);
824 else dz = fabs(dz); // tous les plans Z au meme redshift
825 double z = interpinv(dz);
[3157]826 //CHECK: hgr.Add(z);
827 double dzgr = (*growth_)(z); // interpolation par morceau
828 //double dzgr = growth_->Linear(z,ok); // interpolation lineaire
829 //double dzgr = growth_->Parab(z,ok); // interpolation parabolique
830 int_8 ip = IndexR(i,j,l);
831 data_[ip] *= dzgr;
832 }
833 }
834 }
835
836 //CHECK: {POutPersist pos("applygrowth.ppf"); string tag="hgr"; pos.PutObject(hgr,tag);}
837
838}
839
840//-------------------------------------------------------------------
[3115]841void GeneFluct3D::ComputeReal(void)
842// Calcule une realisation dans l'espace reel
843{
[3155]844 if(lp_>0) cout<<"--- ComputeReal ---"<<endl;
[3141]845 check_array_alloc();
[3115]846
847 // On fait la FFT
[3518]848 GEN3D_FFTW_EXECUTE(pb_);
[3115]849}
850
851//-------------------------------------------------------------------
852void GeneFluct3D::ReComputeFourier(void)
853{
[3155]854 if(lp_>0) cout<<"--- ReComputeFourier ---"<<endl;
[3141]855 check_array_alloc();
[3115]856
857 // On fait la FFT
[3518]858 GEN3D_FFTW_EXECUTE(pf_);
[3115]859 // On corrige du pb de la normalisation de FFTW3
860 double v = (double)NRtot_;
[3129]861 for(long i=0;i<Nx_;i++)
862 for(long j=0;j<Ny_;j++)
863 for(long l=0;l<NCz_;l++) T_(l,j,i) /= complex<r_8>(v);
[3115]864
865}
866
867//-------------------------------------------------------------------
[3141]868int GeneFluct3D::ComputeSpectrum(HistoErr& herr)
869// Compute spectrum from "T" and fill HistoErr "herr"
[3115]870// T : dans le format standard de GeneFuct3D: T(nz,ny,nx)
871// cad T(kz,ky,kx) avec 0<kz<kz_nyq -ky_nyq<ky<ky_nyq -kx_nyq<kx<kx_nyq
872{
[3155]873 if(lp_>0) cout<<"--- ComputeSpectrum ---"<<endl;
[3141]874 check_array_alloc();
[3115]875
[3141]876 if(herr.NBins()<0) return -1;
877 herr.Zero();
[3115]878
879 // Attention a l'ordre
[3129]880 for(long i=0;i<Nx_;i++) {
881 long ii = (i>Nx_/2) ? Nx_-i : i;
[3115]882 double kx = ii*Dkx_; kx *= kx;
[3129]883 for(long j=0;j<Ny_;j++) {
884 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]885 double ky = jj*Dky_; ky *= ky;
[3129]886 for(long l=0;l<NCz_;l++) {
[3330]887 double kz = l*Dkz_;
888 double k = sqrt(kx+ky+kz*kz);
[3115]889 double pk = MODULE2(T_(l,j,i));
[3141]890 herr.Add(k,pk);
[3115]891 }
892 }
893 }
[3150]894 herr.ToVariance();
[3115]895
896 // renormalize to directly compare to original spectrum
897 double norm = Vol_;
[3141]898 herr *= norm;
[3115]899
900 return 0;
901}
902
[3141]903int GeneFluct3D::ComputeSpectrum2D(Histo2DErr& herr)
904{
[3155]905 if(lp_>0) cout<<"--- ComputeSpectrum2D ---"<<endl;
[3141]906 check_array_alloc();
907
908 if(herr.NBinX()<0 || herr.NBinY()<0) return -1;
909 herr.Zero();
910
911 // Attention a l'ordre
912 for(long i=0;i<Nx_;i++) {
913 long ii = (i>Nx_/2) ? Nx_-i : i;
914 double kx = ii*Dkx_; kx *= kx;
915 for(long j=0;j<Ny_;j++) {
916 long jj = (j>Ny_/2) ? Ny_-j : j;
917 double ky = jj*Dky_; ky *= ky;
918 double kt = sqrt(kx+ky);
919 for(long l=0;l<NCz_;l++) {
920 double kz = l*Dkz_;
921 double pk = MODULE2(T_(l,j,i));
922 herr.Add(kt,kz,pk);
923 }
924 }
925 }
[3150]926 herr.ToVariance();
[3141]927
928 // renormalize to directly compare to original spectrum
929 double norm = Vol_;
930 herr *= norm;
931
932 return 0;
933}
934
[3330]935//-------------------------------------------------------------------
936int GeneFluct3D::ComputeSpectrum(HistoErr& herr,double sigma,bool pixcor)
937// Compute spectrum from "T" and fill HistoErr "herr"
938// AVEC la soustraction du niveau de bruit et la correction par filterpixel()
939// Si on ne fait pas ca, alors on obtient un spectre non-isotrope!
940//
941// T : dans le format standard de GeneFuct3D: T(nz,ny,nx)
942// cad T(kz,ky,kx) avec 0<kz<kz_nyq -ky_nyq<ky<ky_nyq -kx_nyq<kx<kx_nyq
943{
944 if(lp_>0) cout<<"--- ComputeSpectrum: sigma="<<sigma<<endl;
945 check_array_alloc();
946
947 if(sigma<=0.) sigma = 0.;
948 double sigma2 = sigma*sigma / (double)NRtot_;
949
950 if(herr.NBins()<0) return -1;
951 herr.Zero();
952
953 TVector<r_8> vfz(NCz_);
954 if(pixcor) // kz = l*Dkz_
955 for(long l=0;l<NCz_;l++) {vfz(l)=pixelfilter(l*Dkz_ *Dz_/2); vfz(l)*=vfz(l);}
956
957 // Attention a l'ordre
958 for(long i=0;i<Nx_;i++) {
959 long ii = (i>Nx_/2) ? Nx_-i : i;
960 double kx = ii*Dkx_;
961 double fx = (pixcor) ? pixelfilter(kx*Dx_/2): 1.;
962 kx *= kx; fx *= fx;
963 for(long j=0;j<Ny_;j++) {
964 long jj = (j>Ny_/2) ? Ny_-j : j;
965 double ky = jj*Dky_;
966 double fy = (pixcor) ? pixelfilter(ky*Dy_/2): 1.;
967 ky *= ky; fy *= fy;
968 for(long l=0;l<NCz_;l++) {
969 double kz = l*Dkz_;
970 double k = sqrt(kx+ky+kz*kz);
971 double pk = MODULE2(T_(l,j,i)) - sigma2;
972 double fz = (pixcor) ? vfz(l): 1.;
973 double f = fx*fy*fz;
974 if(f>0.) herr.Add(k,pk/f);
975 }
976 }
977 }
978 herr.ToVariance();
[3351]979 for(int i=0;i<herr.NBins();i++) herr(i) += sigma2;
[3330]980
981 // renormalize to directly compare to original spectrum
982 double norm = Vol_;
983 herr *= norm;
984
985 return 0;
986}
987
988int GeneFluct3D::ComputeSpectrum2D(Histo2DErr& herr,double sigma,bool pixcor)
989// AVEC la soustraction du niveau de bruit et la correction par filterpixel()
990{
991 if(lp_>0) cout<<"--- ComputeSpectrum2D: sigma="<<sigma<<endl;
992 check_array_alloc();
993
994 if(sigma<=0.) sigma = 0.;
995 double sigma2 = sigma*sigma / (double)NRtot_;
996
997 if(herr.NBinX()<0 || herr.NBinY()<0) return -1;
998 herr.Zero();
999
1000 TVector<r_8> vfz(NCz_);
1001 if(pixcor) // kz = l*Dkz_
1002 for(long l=0;l<NCz_;l++) {vfz(l)=pixelfilter(l*Dkz_ *Dz_/2); vfz(l)*=vfz(l);}
1003
1004 // Attention a l'ordre
1005 for(long i=0;i<Nx_;i++) {
1006 long ii = (i>Nx_/2) ? Nx_-i : i;
1007 double kx = ii*Dkx_;
1008 double fx = (pixcor) ? pixelfilter(kx*Dx_/2) : 1.;
1009 kx *= kx; fx *= fx;
1010 for(long j=0;j<Ny_;j++) {
1011 long jj = (j>Ny_/2) ? Ny_-j : j;
1012 double ky = jj*Dky_;
1013 double fy = (pixcor) ? pixelfilter(ky*Dy_/2) : 1.;
1014 ky *= ky; fy *= fy;
1015 double kt = sqrt(kx+ky);
1016 for(long l=0;l<NCz_;l++) {
1017 double kz = l*Dkz_;
1018 double pk = MODULE2(T_(l,j,i)) - sigma2;
1019 double fz = (pixcor) ? vfz(l): 1.;
1020 double f = fx*fy*fz;
1021 if(f>0.) herr.Add(kt,kz,pk/f);
1022 }
1023 }
1024 }
1025 herr.ToVariance();
[3351]1026 for(int i=0;i<herr.NBinX();i++)
1027 for(int j=0;j<herr.NBinY();j++) herr(i,j) += sigma2;
[3330]1028
1029 // renormalize to directly compare to original spectrum
1030 double norm = Vol_;
1031 herr *= norm;
1032
1033 return 0;
1034}
1035
[3115]1036//-------------------------------------------------------
[3134]1037int_8 GeneFluct3D::VarianceFrReal(double R,double& var)
[3115]1038// Recompute MASS variance in spherical top-hat (rayon=R)
[3353]1039// Par definition: SigmaR^2 = <(M-<M>)^2>/<M>^2
1040// ou M = masse dans sphere de rayon R
[3354]1041// --- ATTENTION: la variance calculee a une tres grande dispersion
1042// (surtout si le volume du cube est petit). Pour verifier
1043// que le sigmaR calcule par cette methode est en accord avec
1044// le sigmaR en input, il faut faire plusieurs simulations (~100)
1045// et regarder la moyenne des sigmaR reconstruits
[3115]1046{
[3262]1047 if(lp_>0) cout<<"--- VarianceFrReal R="<<R<<endl;
[3141]1048 check_array_alloc();
1049
[3353]1050 long dnx = long(R/Dx_)+1; if(dnx<=0) dnx = 1;
1051 long dny = long(R/Dy_)+1; if(dny<=0) dny = 1;
1052 long dnz = long(R/Dz_)+1; if(dnz<=0) dnz = 1;
[3155]1053 if(lp_>0) cout<<"dnx="<<dnx<<" dny="<<dny<<" dnz="<<dnz<<endl;
[3115]1054
[3353]1055 double sum=0., sum2=0., sn=0., r2 = R*R;
1056 int_8 nsum=0;
[3115]1057
[3353]1058 for(long i=dnx;i<Nx_-dnx;i+=2*dnx) {
1059 for(long j=dny;j<Ny_-dny;j+=2*dny) {
1060 for(long l=dnz;l<Nz_-dnz;l+=2*dnz) {
1061 double m=0.; int_8 n=0;
[3129]1062 for(long ii=i-dnx;ii<=i+dnx;ii++) {
[3115]1063 double x = (ii-i)*Dx_; x *= x;
[3129]1064 for(long jj=j-dny;jj<=j+dny;jj++) {
[3115]1065 double y = (jj-j)*Dy_; y *= y;
[3129]1066 for(long ll=l-dnz;ll<=l+dnz;ll++) {
[3115]1067 double z = (ll-l)*Dz_; z *= z;
1068 if(x+y+z>r2) continue;
[3141]1069 int_8 ip = IndexR(ii,jj,ll);
[3353]1070 m += 1.+data_[ip]; // 1+drho/rho
[3115]1071 n++;
1072 }
1073 }
1074 }
[3353]1075 if(n>0) {sum += m; sum2 += m*m; nsum++; sn += n;}
1076 //cout<<i<<","<<j<<","<<l<<" n="<<n<<" m="<<m<<" sum="<<sum<<" sum2="<<sum2<<endl;
[3115]1077 }
1078 }
1079 }
1080
1081 if(nsum<=1) {var=0.; return nsum;}
1082 sum /= nsum;
1083 sum2 = sum2/nsum - sum*sum;
[3353]1084 sn /= nsum;
1085 if(lp_>0) cout<<"...<n>="<<sn<<", nsum="<<nsum<<" <M>="<<sum<<" <(M-<M>)^2>="<<sum2<<endl;
1086 var = sum2/(sum*sum); // <dM^2>/<M>^2
1087 if(lp_>0) cout<<"...sigmaR^2 = <(M-<M>)^2>/<M>^2 = "<<var
1088 <<" -> sigmaR = "<<sqrt(var)<<endl;
[3115]1089
1090 return nsum;
1091}
1092
1093//-------------------------------------------------------
[3134]1094int_8 GeneFluct3D::NumberOfBad(double vmin,double vmax)
[3115]1095// number of pixels outside of ]vmin,vmax[ extremites exclues
1096// -> vmin and vmax are considered as bad
1097{
[3141]1098 check_array_alloc();
[3115]1099
[3134]1100 int_8 nbad = 0;
[3129]1101 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]1102 int_8 ip = IndexR(i,j,l);
1103 double v = data_[ip];
[3115]1104 if(v<=vmin || v>=vmax) nbad++;
1105 }
1106
[3358]1107 if(lp_>0) cout<<"--- NumberOfBad "<<nbad<<" px out of ]"<<vmin<<","<<vmax
1108 <<"[ i.e. frac="<<nbad/(double)NRtot_<<endl;
[3115]1109 return nbad;
1110}
1111
[3320]1112int_8 GeneFluct3D::MinMax(double& xmin,double& xmax,double vmin,double vmax)
1113// Calcul des valeurs xmin et xmax dans le cube reel avec valeurs ]vmin,vmax[ extremites exclues
1114{
1115 bool tstval = (vmax>vmin)? true: false;
1116 if(lp_>0) {
1117 cout<<"--- MinMax";
1118 if(tstval) cout<<" range=]"<<vmin<<","<<vmax<<"[";
1119 cout<<endl;
1120 }
1121 check_array_alloc();
1122
1123 int_8 n = 0;
1124 xmin = xmax = data_[0];
1125
1126 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
1127 int_8 ip = IndexR(i,j,l);
1128 double x = data_[ip];
1129 if(tstval && (x<=vmin || x>=vmax)) continue;
1130 if(x<xmin) xmin = x;
1131 if(x>xmax) xmax = x;
1132 n++;
1133 }
1134
1135 if(lp_>0) cout<<" n="<<n<<" min="<<xmin<<" max="<<xmax<<endl;
1136
1137 return n;
1138}
1139
[3261]1140int_8 GeneFluct3D::MeanSigma2(double& rm,double& rs2,double vmin,double vmax
1141 ,bool useout,double vout)
1142// Calcul de mean,sigma2 dans le cube reel avec valeurs ]vmin,vmax[ extremites exclues
1143// useout = false: ne pas utiliser les pixels hors limites pour calculer mean,sigma2
1144// true : utiliser les pixels hors limites pour calculer mean,sigma2
1145// en remplacant leurs valeurs par "vout"
[3115]1146{
[3261]1147 bool tstval = (vmax>vmin)? true: false;
1148 if(lp_>0) {
[3262]1149 cout<<"--- MeanSigma2";
1150 if(tstval) cout<<" range=]"<<vmin<<","<<vmax<<"[";
[3261]1151 if(useout) cout<<", useout="<<useout<<" vout="<<vout;
1152 cout<<endl;
1153 }
[3141]1154 check_array_alloc();
[3115]1155
[3134]1156 int_8 n = 0;
[3115]1157 rm = rs2 = 0.;
1158
[3129]1159 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]1160 int_8 ip = IndexR(i,j,l);
1161 double v = data_[ip];
[3261]1162 if(tstval) {
1163 if(v<=vmin || v>=vmax) {if(useout) v=vout; else continue;}
1164 }
[3115]1165 rm += v;
1166 rs2 += v*v;
1167 n++;
1168 }
1169
1170 if(n>1) {
1171 rm /= (double)n;
1172 rs2 = rs2/(double)n - rm*rm;
1173 }
1174
[3261]1175 if(lp_>0) cout<<" n="<<n<<" m="<<rm<<" s2="<<rs2<<" s="<<sqrt(fabs(rs2))<<endl;
1176
[3115]1177 return n;
1178}
1179
[3134]1180int_8 GeneFluct3D::SetToVal(double vmin, double vmax,double val0)
[3115]1181// set to "val0" if out of range ]vmin,vmax[ extremites exclues
[3261]1182// cad set to "val0" if in [vmin,vmax] -> vmin and vmax are set to val0
[3115]1183{
[3141]1184 check_array_alloc();
[3115]1185
[3134]1186 int_8 nbad = 0;
[3129]1187 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]1188 int_8 ip = IndexR(i,j,l);
1189 double v = data_[ip];
1190 if(v<=vmin || v>=vmax) {data_[ip] = val0; nbad++;}
[3115]1191 }
1192
[3262]1193 if(lp_>0) cout<<"--- SetToVal "<<nbad<<" px set to="<<val0
1194 <<" because out of range=]"<<vmin<<","<<vmax<<"["<<endl;
[3115]1195 return nbad;
1196}
1197
[3283]1198void GeneFluct3D::ScaleOffset(double scalecube,double offsetcube)
1199// Replace "V" by "scalecube * ( V + offsetcube )"
1200{
[3284]1201 if(lp_>0) cout<<"--- ScaleCube scale="<<scalecube<<" offset="<<offsetcube<<endl;
[3283]1202
1203 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
1204 int_8 ip = IndexR(i,j,l);
1205 data_[ip] = scalecube * ( data_[ip] + offsetcube );
1206 }
1207
1208 return;
1209}
1210
[3115]1211//-------------------------------------------------------
1212void GeneFluct3D::TurnFluct2Mass(void)
1213// d_rho/rho -> Mass (add one!)
1214{
[3155]1215 if(lp_>0) cout<<"--- TurnFluct2Mass ---"<<endl;
[3141]1216 check_array_alloc();
1217
[3115]1218
[3129]1219 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]1220 int_8 ip = IndexR(i,j,l);
1221 data_[ip] += 1.;
[3115]1222 }
1223}
1224
[3358]1225double GeneFluct3D::TurnFluct2MeanNumber(double val_by_mpc3)
[3365]1226// ATTENTION: la gestion des pixels<0 proposee ici induit une perte de variance
1227// sur la carte, le spectre Pk reconstruit sera plus faible!
1228// L'effet sera d'autant plus grand que le nombre de pixels<0 sera grand.
[3329]1229{
[3358]1230 if(lp_>0) cout<<"--- TurnFluct2MeanNumber : "<<val_by_mpc3<<" quantity (gal or mass)/Mpc^3"<<endl;
[3329]1231
[3358]1232 // First convert dRho/Rho into 1+dRho/Rho
1233 int_8 nball = 0; double sumall = 0., sumall2 = 0.;
[3329]1234 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
1235 int_8 ip = IndexR(i,j,l);
[3358]1236 data_[ip] += 1.;
1237 nball++; sumall += data_[ip]; sumall2 += data_[ip]*data_[ip];
[3329]1238 }
[3358]1239 if(nball>2) {
1240 sumall /= (double)nball;
1241 sumall2 = sumall2/(double)nball - sumall*sumall;
1242 if(lp_>0) cout<<"1+dRho/Rho: mean="<<sumall<<" variance="<<sumall2
1243 <<" -> "<<sqrt(fabs(sumall2))<<endl;
[3329]1244 }
1245
[3358]1246 // Find contribution for positive pixels
1247 int_8 nbpos = 0; double sumpos = 0. , sumpos2 = 0.;
1248 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
1249 int_8 ip = IndexR(i,j,l);
1250 double v = data_[ip];
1251 if(data_[ip]>0.) {nbpos++; sumpos += v; sumpos2 += v*v;}
1252 }
1253 if(nbpos<1) {
1254 cout<<"TurnFluct2MeanNumber_Error: nbpos<1"<<endl;
1255 throw RangeCheckError("TurnFluct2MeanNumber_Error: nbpos<1");
1256 }
1257 sumpos2 = sumpos2/nball - sumpos*sumpos/(nball*nball);
1258 if(lp_>0)
1259 cout<<"1+dRho/Rho with v<0 set to zero: mean="<<sumpos/nball
1260 <<" variance="<<sumpos2<<" -> "<<sqrt(fabs(sumpos2))<<endl;
1261 cout<<"Sum of positive values: sumpos="<<sumpos
1262 <<" (n(v>0) = "<<nbpos<<" frac(v>0)="<<nbpos/(double)NRtot_<<")"<<endl;
[3329]1263
[3358]1264 // - Mettre exactement val_by_mpc3*Vol galaxies (ou Msol) dans notre survey
1265 // - Uniquement dans les pixels de masse >0.
1266 // - Mise a zero des pixels <0
1267 double dn = val_by_mpc3 * Vol_ / sumpos;
1268 if(lp_>0) cout<<"...density move from "
1269 <<val_by_mpc3*dVol_<<" to "<<dn<<" / pixel"<<endl;
1270
[3329]1271 double sum = 0.;
1272 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
1273 int_8 ip = IndexR(i,j,l);
1274 if(data_[ip]<=0.) data_[ip] = 0.;
1275 else {
[3349]1276 data_[ip] *= dn;
1277 sum += data_[ip];
[3329]1278 }
1279 }
1280
[3358]1281 if(lp_>0) cout<<"...quantity put into survey "<<sum<<" / "<<val_by_mpc3*Vol_<<endl;
[3329]1282
1283 return sum;
1284}
1285
[3115]1286double GeneFluct3D::ApplyPoisson(void)
1287// do NOT treate negative or nul mass -> let it as it is
1288{
[3155]1289 if(lp_>0) cout<<"--- ApplyPoisson ---"<<endl;
[3141]1290 check_array_alloc();
1291
[3115]1292 double sum = 0.;
[3129]1293 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]1294 int_8 ip = IndexR(i,j,l);
1295 double v = data_[ip];
[3115]1296 if(v>0.) {
1297 unsigned long dn = PoissRandLimit(v,10.);
[3141]1298 data_[ip] = (double)dn;
[3115]1299 sum += (double)dn;
1300 }
1301 }
[3155]1302 if(lp_>0) cout<<sum<<" galaxies put into survey"<<endl;
[3115]1303
1304 return sum;
1305}
1306
1307double GeneFluct3D::TurnNGal2Mass(FunRan& massdist,bool axeslog)
1308// do NOT treate negative or nul mass -> let it as it is
1309// INPUT:
1310// massdist : distribution de masse (m*dn/dm)
1311// axeslog = false : retourne la masse
1312// = true : retourne le log10(mass)
1313// RETURN la masse totale
1314{
[3155]1315 if(lp_>0) cout<<"--- TurnNGal2Mass ---"<<endl;
[3141]1316 check_array_alloc();
1317
[3115]1318 double sum = 0.;
[3129]1319 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]1320 int_8 ip = IndexR(i,j,l);
1321 double v = data_[ip];
[3115]1322 if(v>0.) {
[3129]1323 long ngal = long(v+0.1);
[3141]1324 data_[ip] = 0.;
[3129]1325 for(long i=0;i<ngal;i++) {
[3115]1326 double m = massdist.RandomInterp(); // massdist.Random();
1327 if(axeslog) m = pow(10.,m);
[3141]1328 data_[ip] += m;
[3115]1329 }
[3141]1330 sum += data_[ip];
[3115]1331 }
1332 }
[3155]1333 if(lp_>0) cout<<sum<<" MSol HI mass put into survey"<<endl;
[3115]1334
1335 return sum;
1336}
1337
[3320]1338double GeneFluct3D::TurnNGal2MassQuick(SchechterMassDist& schmdist)
1339// idem TurnNGal2Mass mais beaucoup plus rapide
1340{
1341 if(lp_>0) cout<<"--- TurnNGal2MassQuick ---"<<endl;
1342 check_array_alloc();
1343
1344 double sum = 0.;
1345 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
1346 int_8 ip = IndexR(i,j,l);
1347 double v = data_[ip];
1348 if(v>0.) {
1349 long ngal = long(v+0.1);
1350 data_[ip] = schmdist.TirMass(ngal);
1351 sum += data_[ip];
1352 }
1353 }
1354 if(lp_>0) cout<<sum<<" MSol HI mass put into survey"<<endl;
1355
1356 return sum;
1357}
1358
[3349]1359void GeneFluct3D::AddNoise2Real(double snoise,int type_evol)
1360// add noise to every pixels (meme les <=0 !)
1361// type_evol = 0 : pas d'evolution de la puissance du bruit
1362// 1 : evolution de la puissance du bruit avec la distance a l'observateur
1363// 2 : evolution de la puissance du bruit avec la distance du plan Z
1364// (tous les plans Z sont mis au meme redshift z de leur milieu)
1365{
1366 if(lp_>0) cout<<"--- AddNoise2Real: snoise = "<<snoise<<" evol="<<type_evol<<endl;
1367 check_array_alloc();
1368
1369 if(type_evol<0) type_evol = 0;
1370 if(type_evol>2) {
1371 char *bla = "GeneFluct3D::AddNoise2Real_Error: bad type_evol value";
1372 cout<<bla<<endl; throw ParmError(bla);
1373 }
1374
1375 vector<double> correction;
1376 InterpFunc *intercor = NULL;
1377
1378 if(type_evol>0) {
1379 // Sigma_Noise(en mass) :
1380 // Slim ~ 1/sqrt(DNu) * sqrt(nlobe) en W/m^2Hz
1381 // Flim ~ sqrt(DNu) * sqrt(nlobe) en W/m^2
1382 // Mlim ~ sqrt(DNu) * (Dlum)^2 * sqrt(nlobe) en Msol
1383 // nlobe ~ 1/Dtrcom^2
1384 // Mlim ~ sqrt(DNu) * (Dlum)^2 / Dtrcom
1385 if(cosmo_ == NULL || redsh_ref_<0.| loscom2zred_.size()<1) {
1386 char *bla = "GeneFluct3D::AddNoise2Real_Error: set Observator and Cosmology first";
1387 cout<<bla<<endl; throw ParmError(bla);
1388 }
1389 InterpFunc interpinv(loscom2zred_min_,loscom2zred_max_,loscom2zred_);
1390 long nsz = loscom2zred_.size(), nszmod=((nsz>10)? nsz/10: 1);
1391 for(long i=0;i<nsz;i++) {
1392 double d = interpinv.X(i);
1393 double zred = interpinv(d);
1394 double dtrc = cosmo_->Dtrcom(zred); // pour variation angle solide
1395 double dlum = cosmo_->Dlum(zred); // pour variation conversion mass HI
1396 double dred = Dz_/(cosmo_->Dhubble()/cosmo_->E(zred));
1397 double dnu = Fr_HyperFin_Par *dred/pow(1.+zred,2.); // pour variation dNu
1398 double corr = sqrt(dnu/dnu_ref_) * pow(dlum/dlum_ref_,2.) * dtrc_ref_/dtrc;
1399 if(lp_>0 && (i==0 || i==nsz-1 || i%nszmod==0))
1400 cout<<"i="<<i<<" d="<<d<<" red="<<zred<<" dred="<<dred<<" dnu="<<dnu
1401 <<" dtrc="<<dtrc<<" dlum="<<dlum<<" -> cor="<<corr<<endl;
1402 correction.push_back(corr);
1403 }
1404 intercor = new InterpFunc(loscom2zred_min_,loscom2zred_max_,correction);
1405 }
1406
1407 double corrlim[2] = {1.,1.};
1408 for(long i=0;i<Nx_;i++) {
1409 double dx2 = DXcom(i); dx2 *= dx2;
1410 for(long j=0;j<Ny_;j++) {
1411 double dy2 = DYcom(j); dy2 *= dy2;
1412 for(long l=0;l<Nz_;l++) {
1413 double corr = 1.;
1414 if(type_evol>0) {
1415 double dz = DZcom(l);
1416 if(type_evol==1) dz = sqrt(dx2+dy2+dz*dz);
1417 else dz = fabs(dz); // tous les plans Z au meme redshift
1418 corr = (*intercor)(dz);
1419 if(corr<corrlim[0]) corrlim[0]=corr; else if(corr>corrlim[1]) corrlim[1]=corr;
1420 }
1421 int_8 ip = IndexR(i,j,l);
1422 data_[ip] += snoise*corr*NorRand();
1423 }
1424 }
1425 }
1426 if(type_evol>0)
1427 cout<<"correction factor range: ["<<corrlim[0]<<","<<corrlim[1]<<"]"<<endl;
1428
1429 if(intercor!=NULL) delete intercor;
1430}
1431
1432} // Fin namespace SOPHYA
1433
1434
1435
1436
1437/*********************************************************************
[3199]1438void GeneFluct3D::AddAGN(double lfjy,double lsigma,double powlaw)
[3196]1439// Add AGN flux into simulation:
1440// --- Procedure:
1441// 1. lancer "cmvdefsurv" avec les parametres du survey
[3199]1442// (au redshift de reference du survey)
[3196]1443// et recuperer l'angle solide "angsol sr" du pixel elementaire
1444// au centre du cube.
1445// 2. lancer "cmvtstagn" pour cet angle solide -> cmvtstagn.ppf
1446// 3. regarder l'histo "hlfang" et en deduire un equivalent gaussienne
1447// cad une moyenne <log10(S)> et un sigma "sig"
[3199]1448// Attention: la distribution n'est pas gaussienne les "mean,sigma"
1449// de l'histo ne sont pas vraiment ce que l'on veut
[3196]1450// --- Limitations actuelle du code:
[3271]1451// . les AGN sont supposes evoluer avec la meme loi de puissance pour tout theta,phi
[3199]1452// . le flux des AGN est mis dans une colonne Oz (indice k) et pas sur la ligne de visee
1453// . la distribution est approximee a une gaussienne
1454// ... C'est une approximation pour un observateur loin du centre du cube
1455// et pour un cube peu epais / distance observateur
[3196]1456// --- Parametres de la routine:
[3271]1457// llfy : c'est le <log10(S)> du flux depose par les AGN
1458// dans l'angle solide du pixel elementaire de reference du cube
1459// lsigma : c'est le sigma de la distribution des log10(S)
1460// powlaw : c'est la pente de la distribution cad que le flux "lmsol"
[3199]1461// et considere comme le flux a 1.4GHz et qu'on suppose une loi
1462// F(nu) = (1.4GHz/nu)^powlaw * F(1.4GHz)
[3196]1463// - Comme on est en echelle log10():
1464// on tire log10(Msol) + X
1465// ou X est une realisation sur une gaussienne de variance "sig^2"
1466// La masse realisee est donc: Msol*10^X
1467// - Pas de probleme de pixel negatif car on a une multiplication!
1468{
[3199]1469 if(lp_>0) cout<<"--- AddAGN: <log10(S Jy)> = "<<lfjy<<" , sigma = "<<lsigma<<endl;
[3196]1470 check_array_alloc();
1471
[3271]1472 if(cosmo_ == NULL || redsh_ref_<0.| loscom2zred_.size()<1) {
[3199]1473 char *bla = "GeneFluct3D::AddAGN_Error: set Observator and Cosmology first";
1474 cout<<bla<<endl; throw ParmError(bla);
1475 }
[3196]1476
[3271]1477 // Le flux des AGN en Jy et en mass solaire
1478 double fagnref = pow(10.,lfjy)*(dnu_ref_*1.e9); // Jy.Hz = W/m^2
1479 double magnref = FluxHI2Msol(fagnref*Jansky2Watt_cst,dlum_ref_); // Msol
1480 if(lp_>0)
1481 cout<<"Au pixel de ref: fagnref="<<fagnref
1482 <<" Jy.Hz (a 1.4GHz), magnref="<<magnref<<" Msol"<<endl;
[3196]1483
[3199]1484 if(powlaw!=0.) {
[3271]1485 // F(nu) = F(1.4GHz)*(nu GHz/1.4 Ghz)^p = F(1.4GHz)*(1/(1+z))^p , car nu = 1.4 GHz/(1+z)
1486 magnref *= pow(1/(1.+redsh_ref_),powlaw);
[3199]1487 if(lp_>0) cout<<" powlaw="<<powlaw<<" -> change magnref to "<<magnref<<" Msol"<<endl;
1488 }
1489
1490 // Les infos en fonction de l'indice "l" selon Oz
1491 vector<double> correction;
1492 InterpFunc interpinv(loscom2zred_min_,loscom2zred_max_,loscom2zred_);
[3271]1493 long nzmod = ((Nz_>10)?Nz_/10:1);
[3199]1494 for(long l=0;l<Nz_;l++) {
[3271]1495 double z = fabs(DZcom(l));
[3199]1496 double zred = interpinv(z);
[3271]1497 double dtrc = cosmo_->Dtrcom(zred); // pour variation angle solide
[3199]1498 double dlum = cosmo_->Dlum(zred); // pour variation conversion mass HI
1499 double dred = Dz_/(cosmo_->Dhubble()/cosmo_->E(zred));
1500 double dnu = Fr_HyperFin_Par *dred/pow(1.+zred,2.); // pour variation dNu
[3271]1501 // on a: Mass ~ DNu * Dlum^2 / Dtrcom^2
1502 double corr = dnu/dnu_ref_*pow(dtrc_ref_/dtrc*dlum/dlum_ref_,2.);
1503 // F(nu) = F(1.4GHz)*(nu GHz/1.4 Ghz)^p = F(1.4GHz)*(1/(1+z))^p , car nu = 1.4 GHz/(1+z)
1504 if(powlaw!=0.) corr *= pow((1.+redsh_ref_)/(1.+zred),powlaw);
[3199]1505 correction.push_back(corr);
[3271]1506 if(lp_>0 && (l==0 || l==Nz_-1 || l%nzmod==0)) {
1507 cout<<"l="<<l<<" z="<<z<<" red="<<zred<<" dred="<<dred<<" dnu="<<dnu
1508 <<" dtrc="<<dtrc<<" dlum="<<dlum
1509 <<" -> cor="<<corr<<endl;
[3199]1510 }
1511 }
1512
1513 double sum=0., sum2=0., nsum=0.;
1514 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) {
1515 double a = lsigma*NorRand();
1516 a = magnref*pow(10.,a);
1517 // On met le meme tirage le long de Oz (indice k)
1518 for(long l=0;l<Nz_;l++) {
1519 int_8 ip = IndexR(i,j,l);
1520 data_[ip] += a*correction[l];
1521 }
1522 sum += a; sum2 += a*a; nsum += 1.;
1523 }
1524
1525 if(lp_>0 && nsum>1.) {
[3196]1526 sum /= nsum;
1527 sum2 = sum2/nsum - sum*sum;
1528 cout<<"...Mean mass="<<sum<<" Msol , s^2="<<sum2<<" s="<<sqrt(fabs(sum2))<<endl;
1529 }
1530
1531}
[3349]1532*********************************************************************/
Note: See TracBrowser for help on using the repository browser.