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

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

suite modifs structure code + bug HistoErr/2D dans ComputeSpectrum cmv 18/01/2007

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