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

Last change on this file since 3157 was 3157, checked in by cmv, 19 years ago

intro du facteur de croissance dans la simul cmv 25/01/2007

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