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

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

petites mises en formes cmv 06/08/2007

File size: 40.6 KB
Line 
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
15#include "fabtcolread.h"
16#include "fabtwriter.h"
17#include "fioarr.h"
18
19#include "arrctcast.h"
20
21#include "constcosmo.h"
22#include "geneutils.h"
23#include "schechter.h"
24
25#include "genefluct3d.h"
26
27#define FFTW_THREAD
28
29#define MODULE2(_x_) ((double)((_x_).real()*(_x_).real() + (_x_).imag()*(_x_).imag()))
30
31//-------------------------------------------------------
32GeneFluct3D::GeneFluct3D(TArray< complex<r_8 > >& T)
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.)
41 , loscom2zred_min_(0.) , loscom2zred_max_(0.)
42{
43 xobs_[0] = xobs_[1] = xobs_[2] = 0.;
44 zred_.resize(0);
45 loscom_.resize(0);
46 loscom2zred_.resize(0);
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//-------------------------------------------------------
60void GeneFluct3D::SetSize(long nx,long ny,long nz,double dx,double dy,double dz)
61{
62 setsize(nx,ny,nz,dx,dy,dz);
63 setalloc();
64 setpointers(false);
65 init_fftw();
66}
67
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
76// Si kredshref<0 alors kredshref=nz/2 (milieu du cube)
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
79{
80 if(redshref<0.) redshref = 0.;
81 if(kredshref<0.) {
82 if(Nz_<=0) {
83 char *bla = "GeneFluct3D::SetObservator_Error: for kredsh_ref<0 SetSize should be called first";
84 cout<<bla<<endl; throw ParmError(bla);
85 }
86 kredshref = Nz_/2.;
87 }
88 redsh_ref_ = redshref;
89 kredsh_ref_ = kredshref;
90 if(lp_>0)
91 cout<<"--- GeneFluct3D::SetObservator zref="<<redsh_ref_<<" kref="<<kredsh_ref_<<endl;
92}
93
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
105void GeneFluct3D::setsize(long nx,long ny,long nz,double dx,double dy,double dz)
106{
107 if(lp_>1) cout<<"--- GeneFluct3D::setsize: N="<<nx<<","<<ny<<","<<nz
108 <<" D="<<dx<<","<<dy<<","<<dz<<endl;
109 if(nx<=0 || dx<=0.) {
110 char *bla = "GeneFluct3D::setsize_Error: bad value(s) for nn/dx";
111 cout<<bla<<endl; throw ParmError(bla);
112 }
113
114 // Les tailles des tableaux
115 Nx_ = nx;
116 Ny_ = ny; if(Ny_ <= 0) Ny_ = Nx_;
117 Nz_ = nz; if(Nz_ <= 0) Nz_ = Nx_;
118 N_.resize(0); N_.push_back(Nx_); N_.push_back(Ny_); N_.push_back(Nz_);
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_;
127 D_.resize(0); D_.push_back(Dx_); D_.push_back(Dy_); D_.push_back(Dz_);
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_);
135 Dk_.resize(0); Dk_.push_back(Dkx_); Dk_.push_back(Dky_); Dk_.push_back(Dkz_);
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_;
142 Knyq_.resize(0); Knyq_.push_back(Knyqx_); Knyq_.push_back(Knyqy_); Knyq_.push_back(Knyqz_);
143}
144
145void GeneFluct3D::setalloc(void)
146{
147 if(lp_>1) cout<<"--- GeneFluct3D::setalloc ---"<<endl;
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;
155 if(lp_>1) cout<<" allocating: "<<T_.Size()*sizeof(complex<r_8>)/1.e6<<" Mo"<<endl;
156 } catch (...) {
157 cout<<"GeneFluct3D::setalloc_Error: Problem allocating T_"<<endl;
158 }
159 T_.SetMemoryMapping(BaseArray::CMemoryMapping);
160}
161
162void GeneFluct3D::setpointers(bool from_real)
163{
164 if(lp_>1) cout<<"--- GeneFluct3D::setpointers ---"<<endl;
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");
178 cout<<bla<<endl; throw ParmError(bla);
179}
180
181void GeneFluct3D::init_fftw(void)
182{
183 // --- Initialisation de fftw3 (attention data est sur-ecrit a l'init)
184 if(lp_>1) cout<<"--- GeneFluct3D::init_fftw ---"<<endl;
185#ifdef FFTW_THREAD
186 if(nthread_>0) {
187 cout<<"...Computing with "<<nthread_<<" threads"<<endl;
188 fftw_init_threads();
189 fftw_plan_with_nthreads(nthread_);
190 }
191#endif
192 if(lp_>1) cout<<"...forward plan"<<endl;
193 pf_ = fftw_plan_dft_r2c_3d(Nx_,Ny_,Nz_,data_,fdata_,FFTW_ESTIMATE);
194 if(lp_>1) cout<<"...backward plan"<<endl;
195 pb_ = fftw_plan_dft_c2r_3d(Nx_,Ny_,Nz_,fdata_,data_,FFTW_ESTIMATE);
196}
197
198//-------------------------------------------------------
199long GeneFluct3D::LosComRedshift(double zinc,long npoints)
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
206// -- Input:
207// zinc : redshift increment for computation
208// npoints : number of points required for inverting loscom -> zred
209//
210{
211 if(lp_>0) cout<<"--- LosComRedshift: zinc="<<zinc<<" , npoints="<<npoints<<endl;
212
213 if(cosmo_ == NULL || redsh_ref_<0.) {
214 char *bla = "GeneFluct3D::LosComRedshift_Error: set Observator and Cosmology first";
215 cout<<bla<<endl; throw ParmError(bla);
216 }
217
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
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
240 xobs_[0] = Nx_/2.*Dx_;
241 xobs_[1] = Ny_/2.*Dy_;
242 xobs_[2] = kredsh_ref_*Dz_ - loscom_ref_;
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
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
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++) {
266 double dx2 = DXcom(i*(Nx_-1)); dx2 *= dx2;
267 for(long j=0;j<=1;j++) {
268 double dy2 = DYcom(j*(Ny_-1)); dy2 *= dy2;
269 for(long k=0;k<=1;k++) {
270 double dz2 = DZcom(k*(Nz_-1)); dz2 *= dz2;
271 dz2 = sqrt(dx2+dy2+dz2);
272 if(dz2>loscom_max_) loscom_max_ = dz2;
273 }
274 }
275 }
276 if(lp_>0) {
277 cout<<"...zref="<<redsh_ref_<<" kzref="<<kredsh_ref_<<" losref="<<loscom_ref_<<" Mpc\n"
278 <<" xobs="<<xobs_[0]<<" , "<<xobs_[1]<<" , "<<xobs_[2]<<" Mpc "
279 <<" in_cube="<<obs_in_cube
280 <<" loscom_min="<<loscom_min_<<" loscom_max="<<loscom_max_<<" Mpc (com)"<<endl;
281 }
282
283 // Fill the corresponding vectors for loscom and zred
284 // Be shure to have one dlc <loscom_min and one >loscom_max
285 if(zinc<=0.) zinc = 0.01;
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;
292 if(dlc>loscom_max_) break; // on sort apres avoir stoque un dlc>dlcmax
293 }
294
295 if(lp_>0) {
296 long n = zred_.size();
297 cout<<"...zred/loscom tables[zinc="<<zinc<<"]: n="<<n;
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
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();
327}
328
329//-------------------------------------------------------
330void GeneFluct3D::WriteFits(string cfname,int bitpix)
331{
332 cout<<"--- GeneFluct3D::WriteFits: Writing Cube to "<<cfname<<endl;
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");
344 fwrt.WriteKey("ZREF",redsh_ref_," reference redshift");
345 fwrt.WriteKey("KZREF",kredsh_ref_," reference redshift on z axe");
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{
359 cout<<"--- GeneFluct3D::ReadFits: Reading Cube from "<<cfname<<endl;
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");
369 double zref = fimg.ReadKey("ZREF");
370 double kzref = fimg.ReadKey("KZREF");
371 setsize(nx,ny,nz,dx,dy,dz);
372 setpointers(true);
373 init_fftw();
374 SetObservator(zref,kzref);
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{
388 cout<<"--- GeneFluct3D::WritePPF: Writing Cube (real="<<write_real<<") to "<<cfname<<endl;
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_;
396 R_.Info()["ZREF"] = (r_8)redsh_ref_;
397 R_.Info()["KZREF"] = (r_8)kredsh_ref_;
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{
413 cout<<"--- GeneFluct3D::ReadPPF: Reading Cube from "<<cfname<<endl;
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) {
420 cout<<" ...reading spectrum into TArray<complex <r_8> >"<<endl;
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 }
427 setpointers(from_real); // a mettre ici pour relire les DVInfo
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"];
434 r_8 zref = R_.Info()["ZREF"];
435 r_8 kzref = R_.Info()["KZREF"];
436 setsize(nx,ny,nz,dx,dy,dz);
437 init_fftw();
438 SetObservator(zref,kzref);
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
449void GeneFluct3D::WriteSlicePPF(string cfname)
450// On ecrit 3 tranches du cube selon chaque axe
451{
452 cout<<"--- GeneFluct3D::WriteSlicePPF: Writing Cube Slices "<<cfname<<endl;
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;
464 sprintf(str,"z%ld",l);
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;
473 sprintf(str,"y%ld",j);
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;
482 sprintf(str,"x%ld",i);
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
497//-------------------------------------------------------
498void GeneFluct3D::Print(void)
499{
500 cout<<"GeneFluct3D(T_alloc="<<array_allocated_<<"):"<<endl;
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;
512 cout<<"Redshift "<<redsh_ref_<<" for z axe at k="<<kredsh_ref_<<endl;
513}
514
515//-------------------------------------------------------
516void GeneFluct3D::ComputeFourier0(GenericFunc& pk_at_z)
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
522 if(lp_>0) cout<<"--- ComputeFourier0: before gaussian filling ---"<<endl;
523 // On tient compte du pb de normalisation de FFTW3
524 double sntot = sqrt((double)NRtot_);
525 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
526 int_8 ip = IndexR(i,j,l);
527 data_[ip] = NorRand()/sntot;
528 }
529
530 // --- realisation d'un tableau de tirage gaussiens
531 if(lp_>0) cout<<"...before fft real ---"<<endl;
532 fftw_execute(pf_);
533
534 // --- On remplit avec une realisation
535 if(lp_>0) cout<<"...before Fourier realization filling"<<endl;
536 T_(0,0,0) = complex<r_8>(0.); // on coupe le continue et on l'initialise
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;
540 double kx = ii*Dkx_; kx *= kx;
541 if(lp_>0 && i%lmod==0) cout<<"i="<<i<<" ii="<<ii<<endl;
542 for(long j=0;j<Ny_;j++) {
543 long jj = (j>Ny_/2) ? Ny_-j : j;
544 double ky = jj*Dky_; ky *= ky;
545 for(long l=0;l<NCz_;l++) {
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
550 double pk = pk_at_z(k)/Vol_;
551 // ici pas de "/2" a cause de la remarque ci-dessus
552 T_(l,j,i) *= sqrt(pk);
553 }
554 }
555 }
556
557 if(lp_>0) cout<<"...computing power"<<endl;
558 double p = compute_power_carte();
559 if(lp_>0) cout<<"Puissance dans la realisation: "<<p<<endl;
560
561}
562
563//-------------------------------------------------------
564void GeneFluct3D::ComputeFourier(GenericFunc& pk_at_z)
565// Calcule une realisation du spectre "pk_at_z"
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
578 if(lp_>0) cout<<"--- ComputeFourier ---"<<endl;
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;
582 double kx = ii*Dkx_; kx *= kx;
583 if(lp_>0 && i%lmod==0) cout<<"i="<<i<<" ii="<<ii<<endl;
584 for(long j=0;j<Ny_;j++) {
585 long jj = (j>Ny_/2) ? Ny_-j : j;
586 double ky = jj*Dky_; ky *= ky;
587 for(long l=0;l<NCz_;l++) {
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
592 double pk = pk_at_z(k)/Vol_;
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
602 if(lp_>0) cout<<"...computing power"<<endl;
603 double p = compute_power_carte();
604 if(lp_>0) cout<<"Puissance dans la realisation: "<<p<<endl;
605
606}
607
608long GeneFluct3D::manage_coefficients(void)
609// Take into account the real and complexe conjugate coefficients
610// because we want a realization of a real data in real space
611{
612 if(lp_>1) cout<<"...managing coefficients"<<endl;
613 check_array_alloc();
614
615 // 1./ Le Continu et Nyquist sont reels
616 long nreal = 0;
617 for(long kk=0;kk<2;kk++) {
618 long k=0; // continu
619 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
620 for(long jj=0;jj<2;jj++) {
621 long j=0;
622 if(jj==1) {if( Ny_%2!=0) continue; else j = Ny_/2;}
623 for(long ii=0;ii<2;ii++) {
624 long i=0;
625 if(ii==1) {if( Nx_%2!=0) continue; else i = Nx_/2;}
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;
629 nreal++;
630 }
631 }
632 }
633 if(lp_>1) cout<<"Number of forced real number ="<<nreal<<endl;
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
638 long nconj1 = 0;
639 for(long kk=0;kk<2;kk++) {
640 long k=0; // continu
641 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
642 for(long jj=0;jj<2;jj++) { // selon j
643 long j=0;
644 if(jj==1) {if( Ny_%2!=0) continue; else j = Ny_/2;}
645 for(long i=1;i<(Nx_+1)/2;i++) {
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];
649 nconj1++;
650 }
651 }
652 for(long ii=0;ii<2;ii++) {
653 long i=0;
654 if(ii==1) {if( Nx_%2!=0) continue; else i = Nx_/2;}
655 for(long j=1;j<(Ny_+1)/2;j++) {
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];
659 nconj1++;
660 }
661 }
662 }
663 if(lp_>1) cout<<"Number of forced conjugate on cont+nyq ="<<nconj1<<endl;
664
665 // b./ les lignes et colonnes hors continu et de nyquist
666 long nconj2 = 0;
667 for(long kk=0;kk<2;kk++) {
668 long k=0; // continu
669 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
670 for(long j=1;j<(Ny_+1)/2;j++) {
671 if(Ny_%2==0 && j==Ny_/2) continue; // on ne retraite pas nyquist en j
672 for(long i=1;i<Nx_;i++) {
673 if(Nx_%2==0 && i==Nx_/2) continue; // on ne retraite pas nyquist en i
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];
677 nconj2++;
678 }
679 }
680 }
681 if(lp_>1) cout<<"Number of forced conjugate hors cont+nyq ="<<nconj2<<endl;
682
683 if(lp_>1) cout<<"Check: ddl= "<<NRtot_<<" =?= "<<2*(Nx_*Ny_*NCz_-nconj1-nconj2)-8<<endl;
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{
691 check_array_alloc();
692
693 double s2 = 0.;
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));
697
698 double s20 = 0.;
699 for(long j=0;j<Ny_;j++)
700 for(long i=0;i<Nx_;i++) s20 += MODULE2(T_(0,j,i));
701
702 double s2n = 0.;
703 if(Nz_%2==0)
704 for(long j=0;j<Ny_;j++)
705 for(long i=0;i<Nx_;i++) s2n += MODULE2(T_(NCz_-1,j,i));
706
707 return 2.*s2 -s20 -s2n;
708}
709
710//-------------------------------------------------------------------
711void GeneFluct3D::FilterByPixel(void)
712// Filtrage par la fonction fenetre du pixel (parallelepipede)
713// TF = 1/(dx*dy*dz)*Int[{-dx/2,dx/2},{-dy/2,dy/2},{-dz/2,dz/2}]
714// e^(ik_x*x) e^(ik_y*y) e^(ik_z*z) dxdydz
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
718{
719 if(lp_>0) cout<<"--- FilterByPixel ---"<<endl;
720 check_array_alloc();
721
722 for(long i=0;i<Nx_;i++) {
723 long ii = (i>Nx_/2) ? Nx_-i : i;
724 double kx = ii*Dkx_ *Dx_/2;
725 double pk_x = pixelfilter(kx);
726 for(long j=0;j<Ny_;j++) {
727 long jj = (j>Ny_/2) ? Ny_-j : j;
728 double ky = jj*Dky_ *Dy_/2;
729 double pk_y = pixelfilter(ky);
730 for(long l=0;l<NCz_;l++) {
731 double kz = l*Dkz_ *Dz_/2;
732 double pk_z = pixelfilter(kz);
733 T_(l,j,i) *= pk_x*pk_y*pk_z;
734 }
735 }
736 }
737
738}
739
740//-------------------------------------------------------------------
741void GeneFluct3D::ApplyGrowthFactor(void)
742// Apply Growth to real space
743// Using the correspondance between redshift and los comoving distance
744// describe in vector "zred_" "loscom_"
745{
746 if(lp_>0) cout<<"--- ApplyGrowthFactor ---"<<endl;
747 check_array_alloc();
748
749 if(growth_ == NULL) {
750 char *bla = "GeneFluct3D::ApplyGrowthFactor_Error: set GrowthFactor first";
751 cout<<bla<<endl; throw ParmError(bla);
752 }
753
754 InterpFunc interpinv(loscom2zred_min_,loscom2zred_max_,loscom2zred_);
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//-------------------------------------------------------------------
781void GeneFluct3D::ComputeReal(void)
782// Calcule une realisation dans l'espace reel
783{
784 if(lp_>0) cout<<"--- ComputeReal ---"<<endl;
785 check_array_alloc();
786
787 // On fait la FFT
788 fftw_execute(pb_);
789}
790
791//-------------------------------------------------------------------
792void GeneFluct3D::ReComputeFourier(void)
793{
794 if(lp_>0) cout<<"--- ReComputeFourier ---"<<endl;
795 check_array_alloc();
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_;
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);
804
805}
806
807//-------------------------------------------------------------------
808int GeneFluct3D::ComputeSpectrum(HistoErr& herr)
809// Compute spectrum from "T" and fill HistoErr "herr"
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{
813 if(lp_>0) cout<<"--- ComputeSpectrum ---"<<endl;
814 check_array_alloc();
815
816 if(herr.NBins()<0) return -1;
817 herr.Zero();
818
819 // Attention a l'ordre
820 for(long i=0;i<Nx_;i++) {
821 long ii = (i>Nx_/2) ? Nx_-i : i;
822 double kx = ii*Dkx_; kx *= kx;
823 for(long j=0;j<Ny_;j++) {
824 long jj = (j>Ny_/2) ? Ny_-j : j;
825 double ky = jj*Dky_; ky *= ky;
826 for(long l=0;l<NCz_;l++) {
827 double kz = l*Dkz_; kz *= kz;
828 double k = sqrt(kx+ky+kz);
829 double pk = MODULE2(T_(l,j,i));
830 herr.Add(k,pk);
831 }
832 }
833 }
834 herr.ToVariance();
835
836 // renormalize to directly compare to original spectrum
837 double norm = Vol_;
838 herr *= norm;
839
840 return 0;
841}
842
843int GeneFluct3D::ComputeSpectrum2D(Histo2DErr& herr)
844{
845 if(lp_>0) cout<<"--- ComputeSpectrum2D ---"<<endl;
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 }
866 herr.ToVariance();
867
868 // renormalize to directly compare to original spectrum
869 double norm = Vol_;
870 herr *= norm;
871
872 return 0;
873}
874
875//-------------------------------------------------------
876int_8 GeneFluct3D::VarianceFrReal(double R,double& var)
877// Recompute MASS variance in spherical top-hat (rayon=R)
878{
879 if(lp_>0) cout<<"--- VarianceFrReal R="<<R<<endl;
880 check_array_alloc();
881
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;
885 if(lp_>0) cout<<"dnx="<<dnx<<" dny="<<dny<<" dnz="<<dnz<<endl;
886
887 double sum=0., sum2=0., r2 = R*R; int_8 nsum=0;
888
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) {
892 double s=0.; int_8 n=0;
893 for(long ii=i-dnx;ii<=i+dnx;ii++) {
894 double x = (ii-i)*Dx_; x *= x;
895 for(long jj=j-dny;jj<=j+dny;jj++) {
896 double y = (jj-j)*Dy_; y *= y;
897 for(long ll=l-dnz;ll<=l+dnz;ll++) {
898 double z = (ll-l)*Dz_; z *= z;
899 if(x+y+z>r2) continue;
900 int_8 ip = IndexR(ii,jj,ll);
901 s += 1.+data_[ip];
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;
916 if(lp_>0) cout<<"...nsum="<<nsum<<" <M>="<<sum<<" <(M-<M>)^2>="<<sum2<<endl;
917
918 var = sum2/(sum*sum); // <dM>^2/<M>^2
919 if(lp_>0) cout<<"...sigmaR^2="<<var<<" -> "<<sqrt(var)<<endl;
920
921 return nsum;
922}
923
924//-------------------------------------------------------
925int_8 GeneFluct3D::NumberOfBad(double vmin,double vmax)
926// number of pixels outside of ]vmin,vmax[ extremites exclues
927// -> vmin and vmax are considered as bad
928{
929 check_array_alloc();
930
931 int_8 nbad = 0;
932 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
933 int_8 ip = IndexR(i,j,l);
934 double v = data_[ip];
935 if(v<=vmin || v>=vmax) nbad++;
936 }
937
938 if(lp_>0) cout<<"--- NumberOfBad "<<nbad<<" px out of ]"<<vmin<<","<<vmax<<"["<<endl;
939 return nbad;
940}
941
942int_8 GeneFluct3D::MeanSigma2(double& rm,double& rs2,double vmin,double vmax
943 ,bool useout,double vout)
944// Calcul de mean,sigma2 dans le cube reel avec valeurs ]vmin,vmax[ extremites exclues
945// useout = false: ne pas utiliser les pixels hors limites pour calculer mean,sigma2
946// true : utiliser les pixels hors limites pour calculer mean,sigma2
947// en remplacant leurs valeurs par "vout"
948{
949 bool tstval = (vmax>vmin)? true: false;
950 if(lp_>0) {
951 cout<<"--- MeanSigma2";
952 if(tstval) cout<<" range=]"<<vmin<<","<<vmax<<"[";
953 if(useout) cout<<", useout="<<useout<<" vout="<<vout;
954 cout<<endl;
955 }
956 check_array_alloc();
957
958 int_8 n = 0;
959 rm = rs2 = 0.;
960
961 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
962 int_8 ip = IndexR(i,j,l);
963 double v = data_[ip];
964 if(tstval) {
965 if(v<=vmin || v>=vmax) {if(useout) v=vout; else continue;}
966 }
967 rm += v;
968 rs2 += v*v;
969 n++;
970 }
971
972 if(n>1) {
973 rm /= (double)n;
974 rs2 = rs2/(double)n - rm*rm;
975 }
976
977 if(lp_>0) cout<<" n="<<n<<" m="<<rm<<" s2="<<rs2<<" s="<<sqrt(fabs(rs2))<<endl;
978
979 return n;
980}
981
982int_8 GeneFluct3D::SetToVal(double vmin, double vmax,double val0)
983// set to "val0" if out of range ]vmin,vmax[ extremites exclues
984// cad set to "val0" if in [vmin,vmax] -> vmin and vmax are set to val0
985{
986 check_array_alloc();
987
988 int_8 nbad = 0;
989 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
990 int_8 ip = IndexR(i,j,l);
991 double v = data_[ip];
992 if(v<=vmin || v>=vmax) {data_[ip] = val0; nbad++;}
993 }
994
995 if(lp_>0) cout<<"--- SetToVal "<<nbad<<" px set to="<<val0
996 <<" because out of range=]"<<vmin<<","<<vmax<<"["<<endl;
997 return nbad;
998}
999
1000void GeneFluct3D::ScaleOffset(double scalecube,double offsetcube)
1001// Replace "V" by "scalecube * ( V + offsetcube )"
1002{
1003 if(lp_>0) cout<<"--- ScaleCube scale="<<scalecube<<" offset="<<offsetcube<<endl;
1004
1005 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
1006 int_8 ip = IndexR(i,j,l);
1007 data_[ip] = scalecube * ( data_[ip] + offsetcube );
1008 }
1009
1010 return;
1011}
1012
1013//-------------------------------------------------------
1014void GeneFluct3D::TurnFluct2Mass(void)
1015// d_rho/rho -> Mass (add one!)
1016{
1017 if(lp_>0) cout<<"--- TurnFluct2Mass ---"<<endl;
1018 check_array_alloc();
1019
1020
1021 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
1022 int_8 ip = IndexR(i,j,l);
1023 data_[ip] += 1.;
1024 }
1025}
1026
1027double GeneFluct3D::TurnMass2MeanNumber(double n_by_mpc3)
1028// do NOT treate negative or nul values
1029{
1030 if(lp_>0) cout<<"--- TurnMass2MeanNumber ---"<<endl;
1031
1032 double mall=0., mgood=0.;
1033 int_8 nall=0, ngood=0;
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 mall += data_[ip]; nall++;
1037 if(data_[ip]>0.) {mgood += data_[ip]; ngood++;}
1038 }
1039 if(ngood>0) mgood /= (double)ngood;
1040 if(nall>0) mall /= (double)nall;
1041 if(lp_>0) cout<<"...ngood="<<ngood<<" mgood="<<mgood
1042 <<", nall="<<nall<<" mall="<<mall<<endl;
1043 if(ngood<=0 || mall<=0.) {
1044 cout<<"TurnMass2MeanNumber_Error: ngood="<<ngood<<" <=0 || mall="<<mall<<" <=0"<<endl;
1045 throw RangeCheckError("TurnMass2MeanNumber_Error: ngood<=0 || mall<=0");
1046 }
1047
1048 // On doit mettre n*Vol galaxies dans notre survey
1049 // On en met uniquement dans les pixels de masse >0.
1050 // On NE met PAS a zero les pixels <0
1051 // On renormalise sur les pixels>0 pour qu'on ait n*Vol galaxies
1052 // comme on ne prend que les pixels >0, on doit normaliser
1053 // a la moyenne de <1+d_rho/rho> sur ces pixels
1054 // (rappel sur tout les pixels <1+d_rho/rho>=1)
1055 // nb de gal a mettre ds 1 px:
1056 double dn = n_by_mpc3*Vol_/ (mgood/mall) /(double)ngood;
1057 if(lp_>0) cout<<"...galaxy density move from "
1058 <<n_by_mpc3*Vol_/double(NRtot_)<<" to "<<dn<<" / pixel"<<endl;
1059
1060 double sum = 0.;
1061 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
1062 int_8 ip = IndexR(i,j,l);
1063 data_[ip] *= dn; // par coherence on multiplie aussi les <=0
1064 if(data_[ip]>0.) sum += data_[ip]; // mais on ne les compte pas
1065 }
1066
1067 if(lp_>0) cout<<sum<<"...galaxies put into survey / "<<n_by_mpc3*Vol_<<endl;
1068
1069 return sum;
1070}
1071
1072double GeneFluct3D::ApplyPoisson(void)
1073// do NOT treate negative or nul mass -> let it as it is
1074{
1075 if(lp_>0) cout<<"--- ApplyPoisson ---"<<endl;
1076 check_array_alloc();
1077
1078 double sum = 0.;
1079 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
1080 int_8 ip = IndexR(i,j,l);
1081 double v = data_[ip];
1082 if(v>0.) {
1083 unsigned long dn = PoissRandLimit(v,10.);
1084 data_[ip] = (double)dn;
1085 sum += (double)dn;
1086 }
1087 }
1088 if(lp_>0) cout<<sum<<" galaxies put into survey"<<endl;
1089
1090 return sum;
1091}
1092
1093double GeneFluct3D::TurnNGal2Mass(FunRan& massdist,bool axeslog)
1094// do NOT treate negative or nul mass -> let it as it is
1095// INPUT:
1096// massdist : distribution de masse (m*dn/dm)
1097// axeslog = false : retourne la masse
1098// = true : retourne le log10(mass)
1099// RETURN la masse totale
1100{
1101 if(lp_>0) cout<<"--- TurnNGal2Mass ---"<<endl;
1102 check_array_alloc();
1103
1104 double sum = 0.;
1105 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
1106 int_8 ip = IndexR(i,j,l);
1107 double v = data_[ip];
1108 if(v>0.) {
1109 long ngal = long(v+0.1);
1110 data_[ip] = 0.;
1111 for(long i=0;i<ngal;i++) {
1112 double m = massdist.RandomInterp(); // massdist.Random();
1113 if(axeslog) m = pow(10.,m);
1114 data_[ip] += m;
1115 }
1116 sum += data_[ip];
1117 }
1118 }
1119 if(lp_>0) cout<<sum<<" MSol HI mass put into survey"<<endl;
1120
1121 return sum;
1122}
1123
1124void GeneFluct3D::AddAGN(double lfjy,double lsigma,double powlaw)
1125// Add AGN flux into simulation:
1126// --- Procedure:
1127// 1. lancer "cmvdefsurv" avec les parametres du survey
1128// (au redshift de reference du survey)
1129// et recuperer l'angle solide "angsol sr" du pixel elementaire
1130// au centre du cube.
1131// 2. lancer "cmvtstagn" pour cet angle solide -> cmvtstagn.ppf
1132// 3. regarder l'histo "hlfang" et en deduire un equivalent gaussienne
1133// cad une moyenne <log10(S)> et un sigma "sig"
1134// Attention: la distribution n'est pas gaussienne les "mean,sigma"
1135// de l'histo ne sont pas vraiment ce que l'on veut
1136// --- Limitations actuelle du code:
1137// . les AGN sont supposes evoluer avec la meme loi de puissance pour tout theta,phi
1138// . le flux des AGN est mis dans une colonne Oz (indice k) et pas sur la ligne de visee
1139// . la distribution est approximee a une gaussienne
1140// ... C'est une approximation pour un observateur loin du centre du cube
1141// et pour un cube peu epais / distance observateur
1142// --- Parametres de la routine:
1143// llfy : c'est le <log10(S)> du flux depose par les AGN
1144// dans l'angle solide du pixel elementaire de reference du cube
1145// lsigma : c'est le sigma de la distribution des log10(S)
1146// powlaw : c'est la pente de la distribution cad que le flux "lmsol"
1147// et considere comme le flux a 1.4GHz et qu'on suppose une loi
1148// F(nu) = (1.4GHz/nu)^powlaw * F(1.4GHz)
1149// - Comme on est en echelle log10():
1150// on tire log10(Msol) + X
1151// ou X est une realisation sur une gaussienne de variance "sig^2"
1152// La masse realisee est donc: Msol*10^X
1153// - Pas de probleme de pixel negatif car on a une multiplication!
1154{
1155 if(lp_>0) cout<<"--- AddAGN: <log10(S Jy)> = "<<lfjy<<" , sigma = "<<lsigma<<endl;
1156 check_array_alloc();
1157
1158 if(cosmo_ == NULL || redsh_ref_<0.| loscom2zred_.size()<1) {
1159 char *bla = "GeneFluct3D::AddAGN_Error: set Observator and Cosmology first";
1160 cout<<bla<<endl; throw ParmError(bla);
1161 }
1162
1163 // Le flux des AGN en Jy et en mass solaire
1164 double fagnref = pow(10.,lfjy)*(dnu_ref_*1.e9); // Jy.Hz = W/m^2
1165 double magnref = FluxHI2Msol(fagnref*Jansky2Watt_cst,dlum_ref_); // Msol
1166 if(lp_>0)
1167 cout<<"Au pixel de ref: fagnref="<<fagnref
1168 <<" Jy.Hz (a 1.4GHz), magnref="<<magnref<<" Msol"<<endl;
1169
1170 if(powlaw!=0.) {
1171 // 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)
1172 magnref *= pow(1/(1.+redsh_ref_),powlaw);
1173 if(lp_>0) cout<<" powlaw="<<powlaw<<" -> change magnref to "<<magnref<<" Msol"<<endl;
1174 }
1175
1176 // Les infos en fonction de l'indice "l" selon Oz
1177 vector<double> correction;
1178 InterpFunc interpinv(loscom2zred_min_,loscom2zred_max_,loscom2zred_);
1179 long nzmod = ((Nz_>10)?Nz_/10:1);
1180 for(long l=0;l<Nz_;l++) {
1181 double z = fabs(DZcom(l));
1182 double zred = interpinv(z);
1183 double dtrc = cosmo_->Dtrcom(zred); // pour variation angle solide
1184 double dlum = cosmo_->Dlum(zred); // pour variation conversion mass HI
1185 double dred = Dz_/(cosmo_->Dhubble()/cosmo_->E(zred));
1186 double dnu = Fr_HyperFin_Par *dred/pow(1.+zred,2.); // pour variation dNu
1187 // on a: Mass ~ DNu * Dlum^2 / Dtrcom^2
1188 double corr = dnu/dnu_ref_*pow(dtrc_ref_/dtrc*dlum/dlum_ref_,2.);
1189 // 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)
1190 if(powlaw!=0.) corr *= pow((1.+redsh_ref_)/(1.+zred),powlaw);
1191 correction.push_back(corr);
1192 if(lp_>0 && (l==0 || l==Nz_-1 || l%nzmod==0)) {
1193 cout<<"l="<<l<<" z="<<z<<" red="<<zred<<" dred="<<dred<<" dnu="<<dnu
1194 <<" dtrc="<<dtrc<<" dlum="<<dlum
1195 <<" -> cor="<<corr<<endl;
1196 }
1197 }
1198
1199 double sum=0., sum2=0., nsum=0.;
1200 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) {
1201 double a = lsigma*NorRand();
1202 a = magnref*pow(10.,a);
1203 // On met le meme tirage le long de Oz (indice k)
1204 for(long l=0;l<Nz_;l++) {
1205 int_8 ip = IndexR(i,j,l);
1206 data_[ip] += a*correction[l];
1207 }
1208 sum += a; sum2 += a*a; nsum += 1.;
1209 }
1210
1211 if(lp_>0 && nsum>1.) {
1212 sum /= nsum;
1213 sum2 = sum2/nsum - sum*sum;
1214 cout<<"...Mean mass="<<sum<<" Msol , s^2="<<sum2<<" s="<<sqrt(fabs(sum2))<<endl;
1215 }
1216
1217}
1218
1219void GeneFluct3D::AddNoise2Real(double snoise,bool with_evol)
1220// add noise to every pixels (meme les <=0 !)
1221{
1222 if(lp_>0) cout<<"--- AddNoise2Real: snoise = "<<snoise<<" evol="<<with_evol<<endl;
1223 check_array_alloc();
1224
1225 vector<double> correction;
1226 InterpFunc *intercor = NULL;
1227
1228 if(with_evol) {
1229 // Sigma_Noise(en mass) :
1230 // Slim ~ 1/sqrt(DNu) * sqrt(nlobe) en W/m^2Hz
1231 // Flim ~ sqrt(DNu) * sqrt(nlobe) en W/m^2
1232 // Mlim ~ sqrt(DNu) * (Dlum)^2 * sqrt(nlobe) en Msol
1233 // nlobe ~ 1/Dtrcom^2
1234 // Mlim ~ sqrt(DNu) * (Dlum)^2 / Dtrcom
1235 if(cosmo_ == NULL || redsh_ref_<0.| loscom2zred_.size()<1) {
1236 char *bla = "GeneFluct3D::AddNoise2Real_Error: set Observator and Cosmology first";
1237 cout<<bla<<endl; throw ParmError(bla);
1238 }
1239 InterpFunc interpinv(loscom2zred_min_,loscom2zred_max_,loscom2zred_);
1240 long nsz = loscom2zred_.size(), nszmod=((nsz>10)? nsz/10: 1);
1241 for(long i=0;i<nsz;i++) {
1242 double d = interpinv.X(i);
1243 double zred = interpinv(d);
1244 double dtrc = cosmo_->Dtrcom(zred); // pour variation angle solide
1245 double dlum = cosmo_->Dlum(zred); // pour variation conversion mass HI
1246 double dred = Dz_/(cosmo_->Dhubble()/cosmo_->E(zred));
1247 double dnu = Fr_HyperFin_Par *dred/pow(1.+zred,2.); // pour variation dNu
1248 double corr = sqrt(dnu/dnu_ref_) * pow(dlum/dlum_ref_,2.) * dtrc_ref_/dtrc;
1249 if(lp_>0 && (i==0 || i==nsz-1 || i%nszmod==0))
1250 cout<<"i="<<i<<" d="<<d<<" red="<<zred<<" dred="<<dred<<" dnu="<<dnu
1251 <<" dtrc="<<dtrc<<" dlum="<<dlum<<" -> cor="<<corr<<endl;
1252 correction.push_back(corr);
1253 }
1254 intercor = new InterpFunc(loscom2zred_min_,loscom2zred_max_,correction);
1255 }
1256
1257 double dx2=0., dy2=0., dz2=0.;
1258 for(long i=0;i<Nx_;i++) {
1259 dx2 = DXcom(i); dx2 *= dx2;
1260 for(long j=0;j<Ny_;j++) {
1261 dy2 = DYcom(j); dy2 *= dy2;
1262 for(long l=0;l<Nz_;l++) {
1263 double corr = 1.;
1264 if(with_evol) {
1265 dz2 = DZcom(l); dz2 *= dz2; dz2 = sqrt(dx2+dy2+dz2);
1266 corr = (*intercor)(dz2);
1267 }
1268 int_8 ip = IndexR(i,j,l);
1269 data_[ip] += snoise*corr*NorRand();
1270 }
1271 }
1272 }
1273
1274 if(intercor!=NULL) delete intercor;
1275}
Note: See TracBrowser for help on using the repository browser.