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

Last change on this file since 3267 was 3267, checked in by cmv, 18 years ago

cmv 14/06/2007

File size: 36.1 KB
RevLine 
[3115]1#include "sopnamsp.h"
2#include "machdefs.h"
3#include <iostream>
4#include <stdlib.h>
5#include <stdio.h>
6#include <string.h>
7#include <math.h>
8#include <unistd.h>
9
10#include "tarray.h"
11#include "pexceptions.h"
12#include "perandom.h"
13#include "srandgen.h"
14
[3141]15#include "fabtcolread.h"
16#include "fabtwriter.h"
17#include "fioarr.h"
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
27//#define FFTW_THREAD
28
29#define MODULE2(_x_) ((double)((_x_).real()*(_x_).real() + (_x_).imag()*(_x_).imag()))
30
31//-------------------------------------------------------
[3141]32GeneFluct3D::GeneFluct3D(TArray< complex<r_8 > >& T)
[3154]33 : T_(T) , Nx_(0) , Ny_(0) , Nz_(0) , array_allocated_(false) , lp_(0)
[3157]34 , redshref_(-999.) , kredshref_(0.) , cosmo_(NULL) , growth_(NULL)
35 , loscom_ref_(-999.), loscom_min_(-999.), loscom_max_(-999.)
[3199]36 , loscom2zred_min_(0.) , loscom2zred_max_(0.)
[3115]37{
[3157]38 xobs_[0] = xobs_[1] = xobs_[2] = 0.;
39 zred_.resize(0);
40 loscom_.resize(0);
[3199]41 loscom2zred_.resize(0);
[3115]42 SetNThread();
43}
44
45GeneFluct3D::~GeneFluct3D(void)
46{
47 fftw_destroy_plan(pf_);
48 fftw_destroy_plan(pb_);
49#ifdef FFTW_THREAD
50 if(nthread_>0) fftw_cleanup_threads();
51#endif
52}
53
54//-------------------------------------------------------
[3129]55void GeneFluct3D::SetSize(long nx,long ny,long nz,double dx,double dy,double dz)
[3115]56{
[3141]57 setsize(nx,ny,nz,dx,dy,dz);
58 setalloc();
59 setpointers(false);
[3154]60 init_fftw();
[3141]61}
62
[3154]63void GeneFluct3D::SetObservator(double redshref,double kredshref)
64// L'observateur est au redshift z=0
65// est situe sur la "perpendiculaire" a la face x,y
66// issue du centre de cette face
67// Il faut positionner le cube sur l'axe des z cad des redshifts:
68// redshref = redshift de reference
69// Si redshref<0 alors redshref=0
70// kredshref = indice (en double) correspondant a ce redshift
[3267]71// Si kredshref<0 alors kredshref=nz/2 (milieu du cube)
[3157]72// Exemple: redshref=1.5 kredshref=250.75
73// -> Le pixel i=nx/2 j=ny/2 k=250.75 est au redshift 1.5
[3154]74{
75 if(redshref<0.) redshref = 0.;
[3267]76 if(kredshref<0.) {
77 if(Nz_<=0) {
78 char *bla = "GeneFluct3D::SetObservator_Error: for kredshref<0 SetSize should be called first";
79 cout<<bla<<endl; throw ParmError(bla);
80 }
81 kredshref = Nz_/2.;
82 }
[3157]83 redshref_ = redshref;
[3154]84 kredshref_ = kredshref;
[3199]85 if(lp_>0)
86 cout<<"--- GeneFluct3D::SetObservator zref="<<redshref_<<" kref="<<kredshref_<<endl;
[3154]87}
88
[3157]89void GeneFluct3D::SetCosmology(CosmoCalc& cosmo)
90{
91 cosmo_ = &cosmo;
92 if(lp_>1) cosmo_->Print();
93}
94
95void GeneFluct3D::SetGrowthFactor(GrowthFactor& growth)
96{
97 growth_ = &growth;
98}
99
[3141]100void GeneFluct3D::setsize(long nx,long ny,long nz,double dx,double dy,double dz)
101{
[3155]102 if(lp_>1) cout<<"--- GeneFluct3D::setsize: N="<<nx<<","<<ny<<","<<nz
103 <<" D="<<dx<<","<<dy<<","<<dz<<endl;
[3141]104 if(nx<=0 || dx<=0.) {
[3267]105 char *bla = "GeneFluct3D::setsize_Error: bad value(s) for nn/dx";
[3199]106 cout<<bla<<endl; throw ParmError(bla);
[3115]107 }
108
[3141]109 // Les tailles des tableaux
[3115]110 Nx_ = nx;
111 Ny_ = ny; if(Ny_ <= 0) Ny_ = Nx_;
112 Nz_ = nz; if(Nz_ <= 0) Nz_ = Nx_;
[3141]113 N_.resize(0); N_.push_back(Nx_); N_.push_back(Ny_); N_.push_back(Nz_);
[3115]114 NRtot_ = Nx_*Ny_*Nz_; // nombre de pixels dans le survey
115 NCz_ = Nz_/2 +1;
116 NTz_ = 2*NCz_;
117
118 // le pas dans l'espace (Mpc)
119 Dx_ = dx;
120 Dy_ = dy; if(Dy_ <= 0.) Dy_ = Dx_;
121 Dz_ = dz; if(Dz_ <= 0.) Dz_ = Dx_;
[3141]122 D_.resize(0); D_.push_back(Dx_); D_.push_back(Dy_); D_.push_back(Dz_);
[3115]123 dVol_ = Dx_*Dy_*Dz_;
124 Vol_ = (Nx_*Dx_)*(Ny_*Dy_)*(Nz_*Dz_);
125
126 // Le pas dans l'espace de Fourier (Mpc^-1)
127 Dkx_ = 2.*M_PI/(Nx_*Dx_);
128 Dky_ = 2.*M_PI/(Ny_*Dy_);
129 Dkz_ = 2.*M_PI/(Nz_*Dz_);
[3141]130 Dk_.resize(0); Dk_.push_back(Dkx_); Dk_.push_back(Dky_); Dk_.push_back(Dkz_);
[3115]131 Dk3_ = Dkx_*Dky_*Dkz_;
132
133 // La frequence de Nyquist en k (Mpc^-1)
134 Knyqx_ = M_PI/Dx_;
135 Knyqy_ = M_PI/Dy_;
136 Knyqz_ = M_PI/Dz_;
[3141]137 Knyq_.resize(0); Knyq_.push_back(Knyqx_); Knyq_.push_back(Knyqy_); Knyq_.push_back(Knyqz_);
138}
[3115]139
[3141]140void GeneFluct3D::setalloc(void)
141{
[3155]142 if(lp_>1) cout<<"--- GeneFluct3D::setalloc ---"<<endl;
[3141]143 // Dimensionnement du tableau complex<r_8>
144 // ATTENTION: TArray adresse en memoire a l'envers du C
145 // Tarray(n1,n2,n3) == Carray[n3][n2][n1]
146 sa_size_t SzK_[3] = {NCz_,Ny_,Nx_}; // a l'envers
147 try {
148 T_.ReSize(3,SzK_);
149 array_allocated_ = true;
[3255]150 if(lp_>1) cout<<" allocating: "<<T_.Size()*sizeof(complex<r_8>)/1.e6<<" Mo"<<endl;
[3141]151 } catch (...) {
[3155]152 cout<<"GeneFluct3D::setalloc_Error: Problem allocating T_"<<endl;
[3141]153 }
154 T_.SetMemoryMapping(BaseArray::CMemoryMapping);
[3115]155}
156
[3141]157void GeneFluct3D::setpointers(bool from_real)
158{
[3155]159 if(lp_>1) cout<<"--- GeneFluct3D::setpointers ---"<<endl;
[3141]160 if(from_real) T_ = ArrCastR2C(R_);
161 else R_ = ArrCastC2R(T_);
162 // On remplit les pointeurs
163 fdata_ = (fftw_complex *) (&T_(0,0,0));
164 data_ = (double *) (&R_(0,0,0));
165}
166
167void GeneFluct3D::check_array_alloc(void)
168// Pour tester si le tableau T_ est alloue
169{
170 if(array_allocated_) return;
171 char bla[90];
172 sprintf(bla,"GeneFluct3D::check_array_alloc_Error: array is not allocated");
[3199]173 cout<<bla<<endl; throw ParmError(bla);
[3141]174}
175
[3154]176void GeneFluct3D::init_fftw(void)
177{
178 // --- Initialisation de fftw3 (attention data est sur-ecrit a l'init)
[3155]179 if(lp_>1) cout<<"--- GeneFluct3D::init_fftw ---"<<endl;
[3154]180#ifdef FFTW_THREAD
181 if(nthread_>0) {
[3155]182 cout<<"...Computing with "<<nthread_<<" threads"<<endl;
[3154]183 fftw_init_threads();
184 fftw_plan_with_nthreads(nthread_);
185 }
186#endif
[3155]187 if(lp_>1) cout<<"...forward plan"<<endl;
[3154]188 pf_ = fftw_plan_dft_r2c_3d(Nx_,Ny_,Nz_,data_,fdata_,FFTW_ESTIMATE);
[3155]189 if(lp_>1) cout<<"...backward plan"<<endl;
[3154]190 pb_ = fftw_plan_dft_c2r_3d(Nx_,Ny_,Nz_,fdata_,data_,FFTW_ESTIMATE);
191}
[3141]192
[3157]193//-------------------------------------------------------
[3199]194long GeneFluct3D::LosComRedshift(double zinc,long npoints)
[3157]195// Given a position of the cube relative to the observer
196// and a cosmology
197// (SetObservator() and SetCosmology() should have been called !)
198// This routine filled:
199// the vector "zred_" of scanned redshift (by zinc increments)
200// the vector "loscom_" of corresponding los comoving distance
[3199]201// -- Input:
202// zinc : redshift increment for computation
203// npoints : number of points required for inverting loscom -> zred
[3157]204//
205{
[3199]206 if(lp_>0) cout<<"--- LosComRedshift: zinc="<<zinc<<" , npoints="<<npoints<<endl;
[3154]207
[3157]208 if(cosmo_ == NULL || redshref_<0.) {
[3199]209 char *bla = "GeneFluct3D::LosComRedshift_Error: set Observator and Cosmology first";
210 cout<<bla<<endl; throw ParmError(bla);
[3157]211 }
212
[3199]213 // On calcule les coordonnees de l'observateur dans le repere du cube
214 // cad dans le repere ou l'origine est au centre du pixel i=j=l=0.
215 // L'observateur est sur un axe centre sur le milieu de la face Oxy
[3157]216 double loscom_ref_ = cosmo_->Dloscom(redshref_);
217 xobs_[0] = Nx_/2.*Dx_;
218 xobs_[1] = Ny_/2.*Dy_;
219 xobs_[2] = kredshref_*Dz_ - loscom_ref_;
220
221 // L'observateur est-il dans le cube?
222 bool obs_in_cube = false;
223 if(xobs_[2]>=0. && xobs_[2]<=Nz_*Dz_) obs_in_cube = true;
224
225 // Find MINIMUM los com distance to the observer:
226 // c'est le centre de la face a k=0
227 // (ou zero si l'observateur est dans le cube)
228 loscom_min_ = 0.;
229 if(!obs_in_cube) loscom_min_ = -xobs_[2];
230
231 // Find MAXIMUM los com distance to the observer:
232 // ou que soit positionne l'observateur, la distance
233 // maximal est sur un des coins du cube
234 loscom_max_ = 0.;
235 for(long i=0;i<=1;i++) {
[3267]236 double dx2 = xobs_[0] - i*(Nx_-1)*Dx_; dx2 *= dx2;
[3157]237 for(long j=0;j<=1;j++) {
[3267]238 double dy2 = xobs_[1] - j*(Ny_-1)*Dy_; dy2 *= dy2;
[3157]239 for(long k=0;k<=1;k++) {
[3267]240 double dz2 = xobs_[2] - k*(Nz_-1)*Dz_; dz2 *= dz2;
[3157]241 dz2 = sqrt(dx2+dy2+dz2);
242 if(dz2>loscom_max_) loscom_max_ = dz2;
243 }
244 }
245 }
246 if(lp_>0) {
247 cout<<"...zref="<<redshref_<<" kzref="<<kredshref_<<" losref="<<loscom_ref_<<" Mpc\n"
248 <<" xobs="<<xobs_[0]<<" , "<<xobs_[1]<<" , "<<xobs_[2]<<" Mpc "
249 <<" in_cube="<<obs_in_cube
250 <<" loscom_min="<<loscom_min_<<" loscom_max="<<loscom_max_<<" Mpc "<<endl;
251 }
252
[3199]253 // Fill the corresponding vectors for loscom and zred
[3267]254 // Be shure to have one dlc <loscom_min and one >loscom_max
[3199]255 if(zinc<=0.) zinc = 0.01;
[3157]256 for(double z=0.; ; z+=zinc) {
257 double dlc = cosmo_->Dloscom(z);
258 if(dlc<loscom_min_) {zred_.resize(0); loscom_.resize(0);}
259 zred_.push_back(z);
260 loscom_.push_back(dlc);
261 z += zinc;
[3199]262 if(dlc>loscom_max_) break; // on sort apres avoir stoque un dlc>dlcmax
[3157]263 }
264
265 if(lp_>0) {
[3199]266 long n = zred_.size();
267 cout<<"...zred/loscom tables[zinc="<<zinc<<"]: n="<<n;
[3157]268 if(n>0) cout<<" z="<<zred_[0]<<" -> d="<<loscom_[0];
269 if(n>1) cout<<" , z="<<zred_[n-1]<<" -> d="<<loscom_[n-1];
270 cout<<endl;
271 }
272
[3199]273 // Compute the parameters and tables needed for inversion loscom->zred
274 if(npoints<3) npoints = zred_.size();
275 InverseFunc invfun(zred_,loscom_);
276 invfun.ComputeParab(npoints,loscom2zred_);
277 loscom2zred_min_ = invfun.YMin();
278 loscom2zred_max_ = invfun.YMax();
279
280 if(lp_>0) {
281 long n = loscom2zred_.size();
282 cout<<"...loscom -> zred[npoints="<<npoints<<"]: n="<<n
283 <<" los_min="<<loscom2zred_min_
284 <<" los_max="<<loscom2zred_max_
285 <<" -> zred=[";
286 if(n>0) cout<<loscom2zred_[0];
287 cout<<",";
288 if(n>1) cout<<loscom2zred_[n-1];
289 cout<<"]"<<endl;
290 if(lp_>1 && n>0)
291 for(int i=0;i<n;i++)
292 if(i==0 || abs(i-n/2)<2 || i==n-1)
293 cout<<" "<<i<<" "<<loscom2zred_[i]<<endl;
294 }
295
296 return zred_.size();
[3157]297}
298
[3115]299//-------------------------------------------------------
[3141]300void GeneFluct3D::WriteFits(string cfname,int bitpix)
301{
[3155]302 cout<<"--- GeneFluct3D::WriteFits: Writing Cube to "<<cfname<<endl;
[3141]303 try {
304 FitsImg3DWriter fwrt(cfname.c_str(),bitpix,5);
305 fwrt.WriteKey("NX",Nx_," axe transverse 1");
306 fwrt.WriteKey("NY",Ny_," axe transverse 2");
307 fwrt.WriteKey("NZ",Nz_," axe longitudinal (redshift)");
308 fwrt.WriteKey("DX",Dx_," Mpc");
309 fwrt.WriteKey("DY",Dy_," Mpc");
310 fwrt.WriteKey("DZ",Dz_," Mpc");
311 fwrt.WriteKey("DKX",Dkx_," Mpc^-1");
312 fwrt.WriteKey("DKY",Dky_," Mpc^-1");
313 fwrt.WriteKey("DKZ",Dkz_," Mpc^-1");
[3154]314 fwrt.WriteKey("ZREF",redshref_," reference redshift");
315 fwrt.WriteKey("KZREF",kredshref_," reference redshift on z axe");
[3141]316 fwrt.Write(R_);
317 } catch (PThrowable & exc) {
318 cout<<"Exception : "<<(string)typeid(exc).name()
319 <<" - Msg= "<<exc.Msg()<<endl;
320 return;
321 } catch (...) {
322 cout<<" some other exception was caught !"<<endl;
323 return;
324 }
325}
326
327void GeneFluct3D::ReadFits(string cfname)
328{
[3155]329 cout<<"--- GeneFluct3D::ReadFits: Reading Cube from "<<cfname<<endl;
[3141]330 try {
331 FitsImg3DRead fimg(cfname.c_str(),0,5);
332 fimg.Read(R_);
333 long nx = fimg.ReadKeyL("NX");
334 long ny = fimg.ReadKeyL("NY");
335 long nz = fimg.ReadKeyL("NZ");
336 double dx = fimg.ReadKey("DX");
337 double dy = fimg.ReadKey("DY");
338 double dz = fimg.ReadKey("DZ");
[3154]339 double zref = fimg.ReadKey("ZREF");
340 double kzref = fimg.ReadKey("KZREF");
[3141]341 setsize(nx,ny,nz,dx,dy,dz);
342 setpointers(true);
[3154]343 init_fftw();
344 SetObservator(zref,kzref);
[3141]345 } catch (PThrowable & exc) {
346 cout<<"Exception : "<<(string)typeid(exc).name()
347 <<" - Msg= "<<exc.Msg()<<endl;
348 return;
349 } catch (...) {
350 cout<<" some other exception was caught !"<<endl;
351 return;
352 }
353}
354
355void GeneFluct3D::WritePPF(string cfname,bool write_real)
356// On ecrit soit le TArray<r_8> ou le TArray<complex <r_8> >
357{
[3155]358 cout<<"--- GeneFluct3D::WritePPF: Writing Cube (real="<<write_real<<") to "<<cfname<<endl;
[3141]359 try {
360 R_.Info()["NX"] = (int_8)Nx_;
361 R_.Info()["NY"] = (int_8)Ny_;
362 R_.Info()["NZ"] = (int_8)Nz_;
363 R_.Info()["DX"] = (r_8)Dx_;
364 R_.Info()["DY"] = (r_8)Dy_;
365 R_.Info()["DZ"] = (r_8)Dz_;
[3154]366 R_.Info()["ZREF"] = (r_8)redshref_;
367 R_.Info()["KZREF"] = (r_8)kredshref_;
[3141]368 POutPersist pos(cfname.c_str());
369 if(write_real) pos << PPFNameTag("rgen") << R_;
370 else pos << PPFNameTag("pkgen") << T_;
371 } catch (PThrowable & exc) {
372 cout<<"Exception : "<<(string)typeid(exc).name()
373 <<" - Msg= "<<exc.Msg()<<endl;
374 return;
375 } catch (...) {
376 cout<<" some other exception was caught !"<<endl;
377 return;
378 }
379}
380
381void GeneFluct3D::ReadPPF(string cfname)
382{
[3155]383 cout<<"--- GeneFluct3D::ReadPPF: Reading Cube from "<<cfname<<endl;
[3141]384 try {
385 bool from_real = true;
386 PInPersist pis(cfname.c_str());
387 string name_tag_k = "pkgen";
388 bool found_tag_k = pis.GotoNameTag("pkgen");
389 if(found_tag_k) {
[3262]390 cout<<" ...reading spectrum into TArray<complex <r_8> >"<<endl;
[3141]391 pis >> PPFNameTag("pkgen") >> T_;
392 from_real = false;
393 } else {
394 cout<<" ...reading space into TArray<r_8>"<<endl;
395 pis >> PPFNameTag("rgen") >> R_;
396 }
[3154]397 setpointers(from_real); // a mettre ici pour relire les DVInfo
[3141]398 int_8 nx = R_.Info()["NX"];
399 int_8 ny = R_.Info()["NY"];
400 int_8 nz = R_.Info()["NZ"];
401 r_8 dx = R_.Info()["DX"];
402 r_8 dy = R_.Info()["DY"];
403 r_8 dz = R_.Info()["DZ"];
[3154]404 r_8 zref = R_.Info()["ZREF"];
405 r_8 kzref = R_.Info()["KZREF"];
[3141]406 setsize(nx,ny,nz,dx,dy,dz);
[3154]407 init_fftw();
408 SetObservator(zref,kzref);
[3141]409 } catch (PThrowable & exc) {
410 cout<<"Exception : "<<(string)typeid(exc).name()
411 <<" - Msg= "<<exc.Msg()<<endl;
412 return;
413 } catch (...) {
414 cout<<" some other exception was caught !"<<endl;
415 return;
416 }
417}
418
419//-------------------------------------------------------
[3115]420void GeneFluct3D::Print(void)
421{
[3141]422 cout<<"GeneFluct3D(T_alloc="<<array_allocated_<<"):"<<endl;
[3115]423 cout<<"Space Size : nx="<<Nx_<<" ny="<<Ny_<<" nz="<<Nz_<<" ("<<NTz_<<") size="
424 <<NRtot_<<endl;
425 cout<<" Resol: dx="<<Dx_<<" dy="<<Dy_<<" dz="<<Dz_<<" Mpc"
426 <<", dVol="<<dVol_<<", Vol="<<Vol_<<" Mpc^3"<<endl;
427 cout<<"Fourier Size : nx="<<Nx_<<" ny="<<Ny_<<" nz="<<NCz_<<endl;
428 cout<<" Resol: dkx="<<Dkx_<<" dky="<<Dky_<<" dkz="<<Dkz_<<" Mpc^-1"
429 <<", Dk3="<<Dk3_<<" Mpc^-3"<<endl;
430 cout<<" (2Pi/k: "<<2.*M_PI/Dkx_<<" "<<2.*M_PI/Dky_<<" "<<2.*M_PI/Dkz_<<" Mpc)"<<endl;
431 cout<<" Nyquist: kx="<<Knyqx_<<" ky="<<Knyqy_<<" kz="<<Knyqz_<<" Mpc^-1"
432 <<", Kmax="<<GetKmax()<<" Mpc^-1"<<endl;
433 cout<<" (2Pi/k: "<<2.*M_PI/Knyqx_<<" "<<2.*M_PI/Knyqy_<<" "<<2.*M_PI/Knyqz_<<" Mpc)"<<endl;
[3154]434 cout<<"Redshift "<<redshref_<<" for z axe at k="<<kredshref_<<endl;
[3115]435}
436
437//-------------------------------------------------------
[3141]438void GeneFluct3D::ComputeFourier0(GenericFunc& pk_at_z)
[3115]439// cf ComputeFourier() mais avec autre methode de realisation du spectre
440// (attention on fait une fft pour realiser le spectre)
441{
442
443 // --- realisation d'un tableau de tirage gaussiens
[3155]444 if(lp_>0) cout<<"--- ComputeFourier0: before gaussian filling ---"<<endl;
[3115]445 // On tient compte du pb de normalisation de FFTW3
446 double sntot = sqrt((double)NRtot_);
[3129]447 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]448 int_8 ip = IndexR(i,j,l);
449 data_[ip] = NorRand()/sntot;
[3115]450 }
451
452 // --- realisation d'un tableau de tirage gaussiens
[3155]453 if(lp_>0) cout<<"...before fft real ---"<<endl;
[3115]454 fftw_execute(pf_);
455
456 // --- On remplit avec une realisation
[3157]457 if(lp_>0) cout<<"...before Fourier realization filling"<<endl;
[3115]458 T_(0,0,0) = complex<r_8>(0.); // on coupe le continue et on l'initialise
[3129]459 long lmod = Nx_/10; if(lmod<1) lmod=1;
460 for(long i=0;i<Nx_;i++) {
461 long ii = (i>Nx_/2) ? Nx_-i : i;
[3115]462 double kx = ii*Dkx_; kx *= kx;
[3155]463 if(lp_>0 && i%lmod==0) cout<<"i="<<i<<" ii="<<ii<<endl;
[3129]464 for(long j=0;j<Ny_;j++) {
465 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]466 double ky = jj*Dky_; ky *= ky;
[3129]467 for(long l=0;l<NCz_;l++) {
[3115]468 double kz = l*Dkz_; kz *= kz;
469 if(i==0 && j==0 && l==0) continue; // Suppression du continu
470 double k = sqrt(kx+ky+kz);
471 // cf normalisation: Peacock, Cosmology, formule 16.38 p504
[3141]472 double pk = pk_at_z(k)/Vol_;
[3115]473 // ici pas de "/2" a cause de la remarque ci-dessus
474 T_(l,j,i) *= sqrt(pk);
475 }
476 }
477 }
478
[3155]479 if(lp_>0) cout<<"...computing power"<<endl;
[3115]480 double p = compute_power_carte();
[3155]481 if(lp_>0) cout<<"Puissance dans la realisation: "<<p<<endl;
[3115]482
483}
484
485//-------------------------------------------------------
[3141]486void GeneFluct3D::ComputeFourier(GenericFunc& pk_at_z)
487// Calcule une realisation du spectre "pk_at_z"
[3115]488// Attention: dans TArray le premier indice varie le + vite
489// Explication normalisation: see Coles & Lucchin, Cosmology, p264-265
490// FFTW3: on note N=Nx*Ny*Nz
491// f --(FFT)--> F = TF(f) --(FFT^-1)--> fb = TF^-1(F) = TF^-1(TF(f))
492// sum(f(x_i)^2) = S
493// sum(F(nu_i)^2) = S*N
494// sum(fb(x_i)^2) = S*N^2
495{
496 // --- RaZ du tableau
497 T_ = complex<r_8>(0.);
498
499 // --- On remplit avec une realisation
[3155]500 if(lp_>0) cout<<"--- ComputeFourier ---"<<endl;
[3129]501 long lmod = Nx_/10; if(lmod<1) lmod=1;
502 for(long i=0;i<Nx_;i++) {
503 long ii = (i>Nx_/2) ? Nx_-i : i;
[3115]504 double kx = ii*Dkx_; kx *= kx;
[3155]505 if(lp_>0 && i%lmod==0) cout<<"i="<<i<<" ii="<<ii<<endl;
[3129]506 for(long j=0;j<Ny_;j++) {
507 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]508 double ky = jj*Dky_; ky *= ky;
[3129]509 for(long l=0;l<NCz_;l++) {
[3115]510 double kz = l*Dkz_; kz *= kz;
511 if(i==0 && j==0 && l==0) continue; // Suppression du continu
512 double k = sqrt(kx+ky+kz);
513 // cf normalisation: Peacock, Cosmology, formule 16.38 p504
[3141]514 double pk = pk_at_z(k)/Vol_;
[3115]515 // Explication de la division par 2: voir perandom.cc
516 // ou egalement Coles & Lucchin, Cosmology formula 13.7.2 p279
517 T_(l,j,i) = ComplexGaussRan(sqrt(pk/2.));
518 }
519 }
520 }
521
522 manage_coefficients(); // gros effet pour les spectres que l'on utilise !
523
[3155]524 if(lp_>0) cout<<"...computing power"<<endl;
[3115]525 double p = compute_power_carte();
[3155]526 if(lp_>0) cout<<"Puissance dans la realisation: "<<p<<endl;
[3115]527
528}
529
[3129]530long GeneFluct3D::manage_coefficients(void)
[3115]531// Take into account the real and complexe conjugate coefficients
532// because we want a realization of a real data in real space
533{
[3155]534 if(lp_>1) cout<<"...managing coefficients"<<endl;
[3141]535 check_array_alloc();
[3115]536
537 // 1./ Le Continu et Nyquist sont reels
[3129]538 long nreal = 0;
539 for(long kk=0;kk<2;kk++) {
540 long k=0; // continu
[3115]541 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]542 for(long jj=0;jj<2;jj++) {
543 long j=0;
[3115]544 if(jj==1) {if( Ny_%2!=0) continue; else j = Ny_/2;}
[3129]545 for(long ii=0;ii<2;ii++) {
546 long i=0;
[3115]547 if(ii==1) {if( Nx_%2!=0) continue; else i = Nx_/2;}
[3141]548 int_8 ip = IndexC(i,j,k);
549 //cout<<"i="<<i<<" j="<<j<<" k="<<k<<" = ("<<fdata_[ip][0]<<","<<fdata_[ip][1]<<")"<<endl;
550 fdata_[ip][1] = 0.; fdata_[ip][0] *= M_SQRT2;
[3115]551 nreal++;
552 }
553 }
554 }
[3155]555 if(lp_>1) cout<<"Number of forced real number ="<<nreal<<endl;
[3115]556
557 // 2./ Les elements complexe conjugues (tous dans le plan k=0,Nyquist)
558
559 // a./ les lignes et colonnes du continu et de nyquist
[3129]560 long nconj1 = 0;
561 for(long kk=0;kk<2;kk++) {
562 long k=0; // continu
[3115]563 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]564 for(long jj=0;jj<2;jj++) { // selon j
565 long j=0;
[3115]566 if(jj==1) {if( Ny_%2!=0) continue; else j = Ny_/2;}
[3129]567 for(long i=1;i<(Nx_+1)/2;i++) {
[3141]568 int_8 ip = IndexC(i,j,k);
569 int_8 ip1 = IndexC(Nx_-i,j,k);
570 fdata_[ip1][0] = fdata_[ip][0]; fdata_[ip1][1] = -fdata_[ip][1];
[3115]571 nconj1++;
572 }
573 }
[3129]574 for(long ii=0;ii<2;ii++) {
575 long i=0;
[3115]576 if(ii==1) {if( Nx_%2!=0) continue; else i = Nx_/2;}
[3129]577 for(long j=1;j<(Ny_+1)/2;j++) {
[3141]578 int_8 ip = IndexC(i,j,k);
579 int_8 ip1 = IndexC(i,Ny_-j,k);
580 fdata_[ip1][0] = fdata_[ip][0]; fdata_[ip1][1] = -fdata_[ip][1];
[3115]581 nconj1++;
582 }
583 }
584 }
[3155]585 if(lp_>1) cout<<"Number of forced conjugate on cont+nyq ="<<nconj1<<endl;
[3115]586
587 // b./ les lignes et colonnes hors continu et de nyquist
[3129]588 long nconj2 = 0;
589 for(long kk=0;kk<2;kk++) {
590 long k=0; // continu
[3115]591 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]592 for(long j=1;j<(Ny_+1)/2;j++) {
[3115]593 if(Ny_%2==0 && j==Ny_/2) continue; // on ne retraite pas nyquist en j
[3129]594 for(long i=1;i<Nx_;i++) {
[3115]595 if(Nx_%2==0 && i==Nx_/2) continue; // on ne retraite pas nyquist en i
[3141]596 int_8 ip = IndexC(i,j,k);
597 int_8 ip1 = IndexC(Nx_-i,Ny_-j,k);
598 fdata_[ip1][0] = fdata_[ip][0]; fdata_[ip1][1] = -fdata_[ip][1];
[3115]599 nconj2++;
600 }
601 }
602 }
[3155]603 if(lp_>1) cout<<"Number of forced conjugate hors cont+nyq ="<<nconj2<<endl;
[3115]604
[3155]605 if(lp_>1) cout<<"Check: ddl= "<<NRtot_<<" =?= "<<2*(Nx_*Ny_*NCz_-nconj1-nconj2)-8<<endl;
[3115]606
607 return nreal+nconj1+nconj2;
608}
609
610double GeneFluct3D::compute_power_carte(void)
611// Calcul la puissance de la realisation du spectre Pk
612{
[3141]613 check_array_alloc();
614
[3115]615 double s2 = 0.;
[3129]616 for(long l=0;l<NCz_;l++)
617 for(long j=0;j<Ny_;j++)
618 for(long i=0;i<Nx_;i++) s2 += MODULE2(T_(l,j,i));
[3115]619
620 double s20 = 0.;
[3129]621 for(long j=0;j<Ny_;j++)
622 for(long i=0;i<Nx_;i++) s20 += MODULE2(T_(0,j,i));
[3115]623
624 double s2n = 0.;
625 if(Nz_%2==0)
[3129]626 for(long j=0;j<Ny_;j++)
627 for(long i=0;i<Nx_;i++) s2n += MODULE2(T_(NCz_-1,j,i));
[3115]628
629 return 2.*s2 -s20 -s2n;
630}
631
632//-------------------------------------------------------------------
633void GeneFluct3D::FilterByPixel(void)
634// Filtrage par la fonction fenetre du pixel (parallelepipede)
[3120]635// TF = 1/(dx*dy*dz)*Int[{-dx/2,dx/2},{-dy/2,dy/2},{-dz/2,dz/2}]
[3115]636// e^(ik_x*x) e^(ik_y*y) e^(ik_z*z) dxdydz
[3120]637// = 2/(k_x*dx) * sin(k_x*dx/2) * (idem y) * (idem z)
638// Gestion divergence en 0: sin(y)/y = 1 - y^2/6*(1-y^2/20)
639// avec y = k_x*dx/2
[3115]640{
[3155]641 if(lp_>0) cout<<"--- FilterByPixel ---"<<endl;
[3141]642 check_array_alloc();
643
[3129]644 for(long i=0;i<Nx_;i++) {
645 long ii = (i>Nx_/2) ? Nx_-i : i;
[3120]646 double kx = ii*Dkx_ *Dx_/2;
[3141]647 double pk_x = pixelfilter(kx);
[3129]648 for(long j=0;j<Ny_;j++) {
649 long jj = (j>Ny_/2) ? Ny_-j : j;
[3120]650 double ky = jj*Dky_ *Dy_/2;
[3141]651 double pk_y = pixelfilter(ky);
[3129]652 for(long l=0;l<NCz_;l++) {
[3120]653 double kz = l*Dkz_ *Dz_/2;
[3141]654 double pk_z = pixelfilter(kz);
655 T_(l,j,i) *= pk_x*pk_y*pk_z;
[3115]656 }
657 }
658 }
659
660}
661
662//-------------------------------------------------------------------
[3199]663void GeneFluct3D::ApplyGrowthFactor(void)
[3157]664// Apply Growth to real space
665// Using the correspondance between redshift and los comoving distance
666// describe in vector "zred_" "loscom_"
667{
[3199]668 if(lp_>0) cout<<"--- ApplyGrowthFactor ---"<<endl;
[3157]669 check_array_alloc();
670
671 if(growth_ == NULL) {
[3199]672 char *bla = "GeneFluct3D::ApplyGrowthFactor_Error: set GrowthFactor first";
673 cout<<bla<<endl; throw ParmError(bla);
[3157]674 }
675
[3199]676 InterpFunc interpinv(loscom2zred_min_,loscom2zred_max_,loscom2zred_);
[3157]677 unsigned short ok;
678
679 //CHECK: Histo hgr(0.9*zred_[0],1.1*zred_[n-1],1000);
680 for(long i=0;i<Nx_;i++) {
681 double dx2 = xobs_[0] - i*Dx_; dx2 *= dx2;
682 for(long j=0;j<Ny_;j++) {
683 double dy2 = xobs_[1] - j*Dy_; dy2 *= dy2;
684 for(long l=0;l<Nz_;l++) {
685 double dz2 = xobs_[2] - l*Dz_; dz2 *= dz2;
686 dz2 = sqrt(dx2+dy2+dz2);
687 double z = interpinv(dz2);
688 //CHECK: hgr.Add(z);
689 double dzgr = (*growth_)(z); // interpolation par morceau
690 //double dzgr = growth_->Linear(z,ok); // interpolation lineaire
691 //double dzgr = growth_->Parab(z,ok); // interpolation parabolique
692 int_8 ip = IndexR(i,j,l);
693 data_[ip] *= dzgr;
694 }
695 }
696 }
697
698 //CHECK: {POutPersist pos("applygrowth.ppf"); string tag="hgr"; pos.PutObject(hgr,tag);}
699
700}
701
702//-------------------------------------------------------------------
[3115]703void GeneFluct3D::ComputeReal(void)
704// Calcule une realisation dans l'espace reel
705{
[3155]706 if(lp_>0) cout<<"--- ComputeReal ---"<<endl;
[3141]707 check_array_alloc();
[3115]708
709 // On fait la FFT
710 fftw_execute(pb_);
711}
712
713//-------------------------------------------------------------------
714void GeneFluct3D::ReComputeFourier(void)
715{
[3155]716 if(lp_>0) cout<<"--- ReComputeFourier ---"<<endl;
[3141]717 check_array_alloc();
[3115]718
719 // On fait la FFT
720 fftw_execute(pf_);
721 // On corrige du pb de la normalisation de FFTW3
722 double v = (double)NRtot_;
[3129]723 for(long i=0;i<Nx_;i++)
724 for(long j=0;j<Ny_;j++)
725 for(long l=0;l<NCz_;l++) T_(l,j,i) /= complex<r_8>(v);
[3115]726
727}
728
729//-------------------------------------------------------------------
[3141]730int GeneFluct3D::ComputeSpectrum(HistoErr& herr)
731// Compute spectrum from "T" and fill HistoErr "herr"
[3115]732// T : dans le format standard de GeneFuct3D: T(nz,ny,nx)
733// cad T(kz,ky,kx) avec 0<kz<kz_nyq -ky_nyq<ky<ky_nyq -kx_nyq<kx<kx_nyq
734{
[3155]735 if(lp_>0) cout<<"--- ComputeSpectrum ---"<<endl;
[3141]736 check_array_alloc();
[3115]737
[3141]738 if(herr.NBins()<0) return -1;
739 herr.Zero();
[3115]740
741 // Attention a l'ordre
[3129]742 for(long i=0;i<Nx_;i++) {
743 long ii = (i>Nx_/2) ? Nx_-i : i;
[3115]744 double kx = ii*Dkx_; kx *= kx;
[3129]745 for(long j=0;j<Ny_;j++) {
746 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]747 double ky = jj*Dky_; ky *= ky;
[3129]748 for(long l=0;l<NCz_;l++) {
[3115]749 double kz = l*Dkz_; kz *= kz;
750 double k = sqrt(kx+ky+kz);
751 double pk = MODULE2(T_(l,j,i));
[3141]752 herr.Add(k,pk);
[3115]753 }
754 }
755 }
[3150]756 herr.ToVariance();
[3115]757
758 // renormalize to directly compare to original spectrum
759 double norm = Vol_;
[3141]760 herr *= norm;
[3115]761
762 return 0;
763}
764
[3141]765int GeneFluct3D::ComputeSpectrum2D(Histo2DErr& herr)
766{
[3155]767 if(lp_>0) cout<<"--- ComputeSpectrum2D ---"<<endl;
[3141]768 check_array_alloc();
769
770 if(herr.NBinX()<0 || herr.NBinY()<0) return -1;
771 herr.Zero();
772
773 // Attention a l'ordre
774 for(long i=0;i<Nx_;i++) {
775 long ii = (i>Nx_/2) ? Nx_-i : i;
776 double kx = ii*Dkx_; kx *= kx;
777 for(long j=0;j<Ny_;j++) {
778 long jj = (j>Ny_/2) ? Ny_-j : j;
779 double ky = jj*Dky_; ky *= ky;
780 double kt = sqrt(kx+ky);
781 for(long l=0;l<NCz_;l++) {
782 double kz = l*Dkz_;
783 double pk = MODULE2(T_(l,j,i));
784 herr.Add(kt,kz,pk);
785 }
786 }
787 }
[3150]788 herr.ToVariance();
[3141]789
790 // renormalize to directly compare to original spectrum
791 double norm = Vol_;
792 herr *= norm;
793
794 return 0;
795}
796
[3115]797//-------------------------------------------------------
[3134]798int_8 GeneFluct3D::VarianceFrReal(double R,double& var)
[3115]799// Recompute MASS variance in spherical top-hat (rayon=R)
800{
[3262]801 if(lp_>0) cout<<"--- VarianceFrReal R="<<R<<endl;
[3141]802 check_array_alloc();
803
[3129]804 long dnx = long(R/Dx_+0.5); if(dnx<=0) dnx = 1;
805 long dny = long(R/Dy_+0.5); if(dny<=0) dny = 1;
806 long dnz = long(R/Dz_+0.5); if(dnz<=0) dnz = 1;
[3155]807 if(lp_>0) cout<<"dnx="<<dnx<<" dny="<<dny<<" dnz="<<dnz<<endl;
[3115]808
[3134]809 double sum=0., sum2=0., r2 = R*R; int_8 nsum=0;
[3115]810
[3129]811 for(long i=dnx;i<Nx_-dnx;i+=dnx) {
812 for(long j=dny;j<Ny_-dny;j+=dny) {
813 for(long l=dnz;l<Nz_-dnz;l+=dnz) {
[3134]814 double s=0.; int_8 n=0;
[3129]815 for(long ii=i-dnx;ii<=i+dnx;ii++) {
[3115]816 double x = (ii-i)*Dx_; x *= x;
[3129]817 for(long jj=j-dny;jj<=j+dny;jj++) {
[3115]818 double y = (jj-j)*Dy_; y *= y;
[3129]819 for(long ll=l-dnz;ll<=l+dnz;ll++) {
[3115]820 double z = (ll-l)*Dz_; z *= z;
821 if(x+y+z>r2) continue;
[3141]822 int_8 ip = IndexR(ii,jj,ll);
823 s += 1.+data_[ip];
[3115]824 n++;
825 }
826 }
827 }
828 if(n>0) {sum += s; sum2 += s*s; nsum++;}
829 //cout<<i<<","<<j<<","<<l<<" n="<<n<<" s="<<s<<" sum="<<sum<<" sum2="<<sum2<<endl;
830 }
831 }
832 }
833
834 if(nsum<=1) {var=0.; return nsum;}
835
836 sum /= nsum;
837 sum2 = sum2/nsum - sum*sum;
[3262]838 if(lp_>0) cout<<"...nsum="<<nsum<<" <M>="<<sum<<" <(M-<M>)^2>="<<sum2<<endl;
[3115]839
840 var = sum2/(sum*sum); // <dM>^2/<M>^2
[3262]841 if(lp_>0) cout<<"...sigmaR^2="<<var<<" -> "<<sqrt(var)<<endl;
[3115]842
843 return nsum;
844}
845
846//-------------------------------------------------------
[3134]847int_8 GeneFluct3D::NumberOfBad(double vmin,double vmax)
[3115]848// number of pixels outside of ]vmin,vmax[ extremites exclues
849// -> vmin and vmax are considered as bad
850{
[3141]851 check_array_alloc();
[3115]852
[3134]853 int_8 nbad = 0;
[3129]854 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]855 int_8 ip = IndexR(i,j,l);
856 double v = data_[ip];
[3115]857 if(v<=vmin || v>=vmax) nbad++;
858 }
859
[3262]860 if(lp_>0) cout<<"--- NumberOfBad "<<nbad<<" px out of ]"<<vmin<<","<<vmax<<"["<<endl;
[3115]861 return nbad;
862}
863
[3261]864int_8 GeneFluct3D::MeanSigma2(double& rm,double& rs2,double vmin,double vmax
865 ,bool useout,double vout)
866// Calcul de mean,sigma2 dans le cube reel avec valeurs ]vmin,vmax[ extremites exclues
867// useout = false: ne pas utiliser les pixels hors limites pour calculer mean,sigma2
868// true : utiliser les pixels hors limites pour calculer mean,sigma2
869// en remplacant leurs valeurs par "vout"
[3115]870{
[3261]871 bool tstval = (vmax>vmin)? true: false;
872 if(lp_>0) {
[3262]873 cout<<"--- MeanSigma2";
874 if(tstval) cout<<" range=]"<<vmin<<","<<vmax<<"[";
[3261]875 if(useout) cout<<", useout="<<useout<<" vout="<<vout;
876 cout<<endl;
877 }
[3141]878 check_array_alloc();
[3115]879
[3134]880 int_8 n = 0;
[3115]881 rm = rs2 = 0.;
882
[3129]883 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]884 int_8 ip = IndexR(i,j,l);
885 double v = data_[ip];
[3261]886 if(tstval) {
887 if(v<=vmin || v>=vmax) {if(useout) v=vout; else continue;}
888 }
[3115]889 rm += v;
890 rs2 += v*v;
891 n++;
892 }
893
894 if(n>1) {
895 rm /= (double)n;
896 rs2 = rs2/(double)n - rm*rm;
897 }
898
[3261]899 if(lp_>0) cout<<" n="<<n<<" m="<<rm<<" s2="<<rs2<<" s="<<sqrt(fabs(rs2))<<endl;
900
[3115]901 return n;
902}
903
[3134]904int_8 GeneFluct3D::SetToVal(double vmin, double vmax,double val0)
[3115]905// set to "val0" if out of range ]vmin,vmax[ extremites exclues
[3261]906// cad set to "val0" if in [vmin,vmax] -> vmin and vmax are set to val0
[3115]907{
[3141]908 check_array_alloc();
[3115]909
[3134]910 int_8 nbad = 0;
[3129]911 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]912 int_8 ip = IndexR(i,j,l);
913 double v = data_[ip];
914 if(v<=vmin || v>=vmax) {data_[ip] = val0; nbad++;}
[3115]915 }
916
[3262]917 if(lp_>0) cout<<"--- SetToVal "<<nbad<<" px set to="<<val0
918 <<" because out of range=]"<<vmin<<","<<vmax<<"["<<endl;
[3115]919 return nbad;
920}
921
922//-------------------------------------------------------
923void GeneFluct3D::TurnFluct2Mass(void)
924// d_rho/rho -> Mass (add one!)
925{
[3155]926 if(lp_>0) cout<<"--- TurnFluct2Mass ---"<<endl;
[3141]927 check_array_alloc();
928
[3115]929
[3129]930 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]931 int_8 ip = IndexR(i,j,l);
932 data_[ip] += 1.;
[3115]933 }
934}
935
936double GeneFluct3D::TurnMass2MeanNumber(double n_by_mpc3)
937// do NOT treate negative or nul values
938{
[3155]939 if(lp_>0) cout<<"--- TurnMass2MeanNumber ---"<<endl;
[3115]940
[3262]941 double mall=0., mgood=0.;
942 int_8 nall=0, ngood=0;
943 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
944 int_8 ip = IndexR(i,j,l);
945 mall += data_[ip]; nall++;
946 if(data_[ip]>0.) {mgood += data_[ip]; ngood++;}
947 }
948 if(ngood>0) mgood /= (double)ngood;
949 if(nall>0) mall /= (double)nall;
950 if(lp_>0) cout<<"...ngood="<<ngood<<" mgood="<<mgood
951 <<", nall="<<nall<<" mall="<<mall<<endl;
952 if(ngood<=0 || mall<=0.) {
953 cout<<"TurnMass2MeanNumber_Error: ngood="<<ngood<<" <=0 || mall="<<mall<<" <=0"<<endl;
954 throw RangeCheckError("TurnMass2MeanNumber_Error: ngood<=0 || mall<=0");
955 }
[3115]956
957 // On doit mettre n*Vol galaxies dans notre survey
958 // On en met uniquement dans les pixels de masse >0.
959 // On NE met PAS a zero les pixels <0
960 // On renormalise sur les pixels>0 pour qu'on ait n*Vol galaxies
961 // comme on ne prend que les pixels >0, on doit normaliser
962 // a la moyenne de <1+d_rho/rho> sur ces pixels
963 // (rappel sur tout les pixels <1+d_rho/rho>=1)
[3262]964 // nb de gal a mettre ds 1 px:
965 double dn = n_by_mpc3*Vol_/ (mgood/mall) /(double)ngood;
[3155]966 if(lp_>0) cout<<"...galaxy density move from "
967 <<n_by_mpc3*Vol_/double(NRtot_)<<" to "<<dn<<" / pixel"<<endl;
[3262]968
[3115]969 double sum = 0.;
[3129]970 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]971 int_8 ip = IndexR(i,j,l);
972 data_[ip] *= dn; // par coherence on multiplie aussi les <=0
973 if(data_[ip]>0.) sum += data_[ip]; // mais on ne les compte pas
[3115]974 }
[3262]975
[3155]976 if(lp_>0) cout<<sum<<"...galaxies put into survey / "<<n_by_mpc3*Vol_<<endl;
[3115]977
978 return sum;
979}
980
981double GeneFluct3D::ApplyPoisson(void)
982// do NOT treate negative or nul mass -> let it as it is
983{
[3155]984 if(lp_>0) cout<<"--- ApplyPoisson ---"<<endl;
[3141]985 check_array_alloc();
986
[3115]987 double sum = 0.;
[3129]988 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]989 int_8 ip = IndexR(i,j,l);
990 double v = data_[ip];
[3115]991 if(v>0.) {
992 unsigned long dn = PoissRandLimit(v,10.);
[3141]993 data_[ip] = (double)dn;
[3115]994 sum += (double)dn;
995 }
996 }
[3155]997 if(lp_>0) cout<<sum<<" galaxies put into survey"<<endl;
[3115]998
999 return sum;
1000}
1001
1002double GeneFluct3D::TurnNGal2Mass(FunRan& massdist,bool axeslog)
1003// do NOT treate negative or nul mass -> let it as it is
1004// INPUT:
1005// massdist : distribution de masse (m*dn/dm)
1006// axeslog = false : retourne la masse
1007// = true : retourne le log10(mass)
1008// RETURN la masse totale
1009{
[3155]1010 if(lp_>0) cout<<"--- TurnNGal2Mass ---"<<endl;
[3141]1011 check_array_alloc();
1012
[3115]1013 double sum = 0.;
[3129]1014 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]1015 int_8 ip = IndexR(i,j,l);
1016 double v = data_[ip];
[3115]1017 if(v>0.) {
[3129]1018 long ngal = long(v+0.1);
[3141]1019 data_[ip] = 0.;
[3129]1020 for(long i=0;i<ngal;i++) {
[3115]1021 double m = massdist.RandomInterp(); // massdist.Random();
1022 if(axeslog) m = pow(10.,m);
[3141]1023 data_[ip] += m;
[3115]1024 }
[3141]1025 sum += data_[ip];
[3115]1026 }
1027 }
[3155]1028 if(lp_>0) cout<<sum<<" MSol HI mass put into survey"<<endl;
[3115]1029
1030 return sum;
1031}
1032
[3199]1033void GeneFluct3D::AddAGN(double lfjy,double lsigma,double powlaw)
[3196]1034// Add AGN flux into simulation:
1035// --- Procedure:
1036// 1. lancer "cmvdefsurv" avec les parametres du survey
[3199]1037// (au redshift de reference du survey)
[3196]1038// et recuperer l'angle solide "angsol sr" du pixel elementaire
1039// au centre du cube.
1040// 2. lancer "cmvtstagn" pour cet angle solide -> cmvtstagn.ppf
1041// 3. regarder l'histo "hlfang" et en deduire un equivalent gaussienne
1042// cad une moyenne <log10(S)> et un sigma "sig"
[3199]1043// Attention: la distribution n'est pas gaussienne les "mean,sigma"
1044// de l'histo ne sont pas vraiment ce que l'on veut
[3196]1045// --- Limitations actuelle du code:
[3199]1046// . les AGN sont supposes en loi de puissance IDENTIQUE pour tout theta,phi
1047// . le flux des AGN est mis dans une colonne Oz (indice k) et pas sur la ligne de visee
1048// . la distribution est approximee a une gaussienne
1049// ... C'est une approximation pour un observateur loin du centre du cube
1050// et pour un cube peu epais / distance observateur
[3196]1051// --- Parametres de la routine:
[3199]1052// llfy : c'est le <log10(S)> du flux depose dans un pixel par les AGN
[3196]1053// lsigma : c'est le sigma de la distribution
[3199]1054// powlaw : c'est la pente de ls distribution cad que le flux "lmsol"
1055// et considere comme le flux a 1.4GHz et qu'on suppose une loi
1056// F(nu) = (1.4GHz/nu)^powlaw * F(1.4GHz)
[3196]1057// - Comme on est en echelle log10():
1058// on tire log10(Msol) + X
1059// ou X est une realisation sur une gaussienne de variance "sig^2"
1060// La masse realisee est donc: Msol*10^X
1061// - Pas de probleme de pixel negatif car on a une multiplication!
1062{
[3199]1063 if(lp_>0) cout<<"--- AddAGN: <log10(S Jy)> = "<<lfjy<<" , sigma = "<<lsigma<<endl;
[3196]1064 check_array_alloc();
1065
[3199]1066 if(cosmo_ == NULL || redshref_<0.| loscom2zred_.size()<1) {
1067 char *bla = "GeneFluct3D::AddAGN_Error: set Observator and Cosmology first";
1068 cout<<bla<<endl; throw ParmError(bla);
1069 }
[3196]1070
[3199]1071 // La distance angulaire/luminosite/Dnu au centre
1072 double dangref = cosmo_->Dang(redshref_);
1073 double dlumref = cosmo_->Dlum(redshref_);
1074 double dredref = Dz_/(cosmo_->Dhubble()/cosmo_->E(redshref_));
1075 double dnuref = Fr_HyperFin_Par *dredref/pow(1.+redshref_,2.); // GHz
1076 double fagnref = pow(10.,lfjy)*(dnuref*1.e9); // Jy.Hz
1077 double magnref = FluxHI2Msol(fagnref*Jansky2Watt_cst,dlumref); // Msol
1078 if(lp_>0) {
1079 cout<<"Au centre: z="<<redshref_<<", dredref="<<dredref<<", dnuref="<<dnuref<<" GHz"<<endl
1080 <<" dang="<<dangref<<" Mpc, dlum="<<dlumref<<" Mpc,"
1081 <<" fagnref="<<fagnref<<" Jy.Hz (a 1.4GHz), magnref="<<magnref<<" Msol"<<endl;
1082 }
[3196]1083
[3199]1084 if(powlaw!=0.) {
1085 // F(nu) = (nu GHz/1.4 Ghz)^p * F(1.4GHz) et nu = 1.4 GHz / (1+z)
1086 magnref *= pow(1/(1.+redshref_),powlaw);
1087 if(lp_>0) cout<<" powlaw="<<powlaw<<" -> change magnref to "<<magnref<<" Msol"<<endl;
1088 }
1089
1090 // Les infos en fonction de l'indice "l" selon Oz
1091 vector<double> correction;
1092 InterpFunc interpinv(loscom2zred_min_,loscom2zred_max_,loscom2zred_);
1093 for(long l=0;l<Nz_;l++) {
1094 double z = fabs(xobs_[2] - l*Dz_);
1095 double zred = interpinv(z);
1096 double dang = cosmo_->Dang(zred); // pour variation angle solide
1097 double dlum = cosmo_->Dlum(zred); // pour variation conversion mass HI
1098 double dred = Dz_/(cosmo_->Dhubble()/cosmo_->E(zred));
1099 double dnu = Fr_HyperFin_Par *dred/pow(1.+zred,2.); // pour variation dNu
1100 double corr = dnu/dnuref*pow(dangref/dang*dlum/dlumref,2.);
1101 if(powlaw!=0.) corr *= pow((1.+redshref_)/(1.+zred),powlaw);
1102 correction.push_back(corr);
1103 if(lp_>0 && (l==0 || abs(l-Nz_/2)<2 || l==Nz_-1)) {
1104 cout<<"l="<<l<<" z="<<z<<" red="<<zred
1105 <<" da="<<dang<<" dlu="<<dlum<<" dred="<<dred
1106 <<" dnu="<<dnu<<" -> corr="<<corr<<endl;
1107 }
1108 }
1109
1110 double sum=0., sum2=0., nsum=0.;
1111 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) {
1112 double a = lsigma*NorRand();
1113 a = magnref*pow(10.,a);
1114 // On met le meme tirage le long de Oz (indice k)
1115 for(long l=0;l<Nz_;l++) {
1116 int_8 ip = IndexR(i,j,l);
1117 data_[ip] += a*correction[l];
1118 }
1119 sum += a; sum2 += a*a; nsum += 1.;
1120 }
1121
1122 if(lp_>0 && nsum>1.) {
[3196]1123 sum /= nsum;
1124 sum2 = sum2/nsum - sum*sum;
1125 cout<<"...Mean mass="<<sum<<" Msol , s^2="<<sum2<<" s="<<sqrt(fabs(sum2))<<endl;
1126 }
1127
1128}
1129
[3267]1130void GeneFluct3D::AddNoise2Real(double snoise,bool with_evol)
[3115]1131// add noise to every pixels (meme les <=0 !)
1132{
[3267]1133 if(lp_>0) cout<<"--- AddNoise2Real: snoise = "<<snoise<<" evol="<<with_evol<<endl;
[3141]1134 check_array_alloc();
1135
[3199]1136 for(long i=0;i<Nx_;i++) {
1137 for(long j=0;j<Ny_;j++) {
[3267]1138 for(long l=0;l<Nz_;l++) {
1139 int_8 ip = IndexR(i,j,l);
1140 data_[ip] += snoise*NorRand();
[3199]1141 }
1142 }
1143 }
1144
1145}
1146
Note: See TracBrowser for help on using the repository browser.