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

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

possibilite de ne pas faire poisson sur Ngal cmv 27/09/2007

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