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

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

intro SchechterMassDist pour accelerer la simulations cmv 05/09/2007

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