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

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

possibilite de travailler en float (suite) cmv 11/09/2008

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