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

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

char* -> const char* pour regler les problemes de deprecated string const... + comparaison unsigned signed + suppression EVOL_PLANCK rz+cmv 07/02/2009

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