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
RevLine 
[3115]1#include "sopnamsp.h"
2#include "machdefs.h"
3#include <iostream>
4#include <stdlib.h>
5#include <stdio.h>
6#include <string.h>
7#include <math.h>
8#include <unistd.h>
9
10#include "timing.h"
11#include "tarray.h"
12#include "pexceptions.h"
13#include "perandom.h"
14#include "srandgen.h"
15
[3141]16#include "fabtcolread.h"
17#include "fabtwriter.h"
18#include "fioarr.h"
19
20#include "arrctcast.h"
21
[3115]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//-------------------------------------------------------
[3141]33GeneFluct3D::GeneFluct3D(TArray< complex<r_8 > >& T)
[3150]34 : T_(T) , array_allocated_(false) , lp_(0)
[3115]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//-------------------------------------------------------
[3129]49void GeneFluct3D::SetSize(long nx,long ny,long nz,double dx,double dy,double dz)
[3115]50{
[3141]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.) {
[3115]59 cout<<"GeneFluct3D::SetSize_Error: bad value(s)"<<endl;
60 throw ParmError("GeneFluct3D::SetSize_Error: bad value(s)");
61 }
62
[3141]63 // Les tailles des tableaux
[3115]64 Nx_ = nx;
65 Ny_ = ny; if(Ny_ <= 0) Ny_ = Nx_;
66 Nz_ = nz; if(Nz_ <= 0) Nz_ = Nx_;
[3141]67 N_.resize(0); N_.push_back(Nx_); N_.push_back(Ny_); N_.push_back(Nz_);
[3115]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_;
[3141]76 D_.resize(0); D_.push_back(Dx_); D_.push_back(Dy_); D_.push_back(Dz_);
[3115]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_);
[3141]84 Dk_.resize(0); Dk_.push_back(Dkx_); Dk_.push_back(Dky_); Dk_.push_back(Dkz_);
[3115]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_;
[3141]91 Knyq_.resize(0); Knyq_.push_back(Knyqx_); Knyq_.push_back(Knyqy_); Knyq_.push_back(Knyqz_);
92}
[3115]93
[3141]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);
[3115]107}
108
[3141]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
[3115]129//-------------------------------------------------------
[3141]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//-------------------------------------------------------
[3115]238void GeneFluct3D::Print(void)
239{
[3141]240 cout<<"GeneFluct3D(T_alloc="<<array_allocated_<<"):"<<endl;
[3115]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//-------------------------------------------------------
[3141]255void GeneFluct3D::ComputeFourier0(GenericFunc& pk_at_z)
[3115]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)
[3150]260 if(lp_>1) PrtTim("--- ComputeFourier0: before fftw_plan ---");
[3115]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
[3141]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);
[3150]270 if(lp_>1) PrtTim("--- ComputeFourier0: after fftw_plan ---");
[3115]271
272 // --- realisation d'un tableau de tirage gaussiens
[3150]273 if(lp_>1) PrtTim("--- ComputeFourier0: before gaussian filling ---");
[3115]274 // On tient compte du pb de normalisation de FFTW3
275 double sntot = sqrt((double)NRtot_);
[3129]276 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]277 int_8 ip = IndexR(i,j,l);
278 data_[ip] = NorRand()/sntot;
[3115]279 }
[3150]280 if(lp_>1) PrtTim("--- ComputeFourier0: after gaussian filling ---");
[3115]281
282 // --- realisation d'un tableau de tirage gaussiens
[3150]283 if(lp_>1) PrtTim("--- ComputeFourier0: before fft real ---");
[3115]284 fftw_execute(pf_);
[3150]285 if(lp_>1) PrtTim("--- ComputeFourier0: after fft real ---");
[3115]286
287 // --- On remplit avec une realisation
[3150]288 if(lp_>1) PrtTim("--- ComputeFourier0: before Fourier realization filling ---");
[3115]289 T_(0,0,0) = complex<r_8>(0.); // on coupe le continue et on l'initialise
[3129]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;
[3115]293 double kx = ii*Dkx_; kx *= kx;
[3150]294 if(lp_>1 && i%lmod==0) cout<<"i="<<i<<" ii="<<ii<<endl;
[3129]295 for(long j=0;j<Ny_;j++) {
296 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]297 double ky = jj*Dky_; ky *= ky;
[3129]298 for(long l=0;l<NCz_;l++) {
[3115]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
[3141]303 double pk = pk_at_z(k)/Vol_;
[3115]304 // ici pas de "/2" a cause de la remarque ci-dessus
305 T_(l,j,i) *= sqrt(pk);
306 }
307 }
308 }
[3150]309 if(lp_>1) PrtTim("--- ComputeFourier0: after Fourier realization filling ---");
[3115]310
311 double p = compute_power_carte();
312 cout<<"Puissance dans la realisation: "<<p<<endl;
[3150]313 if(lp_>1) PrtTim("--- ComputeFourier0: after Computing power ---");
[3115]314
315}
316
317//-------------------------------------------------------
[3141]318void GeneFluct3D::ComputeFourier(GenericFunc& pk_at_z)
319// Calcule une realisation du spectre "pk_at_z"
[3115]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)
[3150]329 if(lp_>1) PrtTim("--- ComputeFourier: before fftw_plan ---");
[3115]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
[3141]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);
[3115]339 //fftw_print_plan(pb_); cout<<endl;
[3150]340 if(lp_>1) PrtTim("--- ComputeFourier: after fftw_plan ---");
[3115]341
342 // --- RaZ du tableau
343 T_ = complex<r_8>(0.);
344
345 // --- On remplit avec une realisation
[3150]346 if(lp_>1) PrtTim("--- ComputeFourier: before Fourier realization filling ---");
[3129]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;
[3115]350 double kx = ii*Dkx_; kx *= kx;
[3150]351 if(lp_>1 && i%lmod==0) cout<<"i="<<i<<" ii="<<ii<<endl;
[3129]352 for(long j=0;j<Ny_;j++) {
353 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]354 double ky = jj*Dky_; ky *= ky;
[3129]355 for(long l=0;l<NCz_;l++) {
[3115]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
[3141]360 double pk = pk_at_z(k)/Vol_;
[3115]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 }
[3150]367 if(lp_>1) PrtTim("--- ComputeFourier: after Fourier realization filling ---");
[3115]368
369 manage_coefficients(); // gros effet pour les spectres que l'on utilise !
[3150]370 if(lp_>1) PrtTim("--- ComputeFourier: after managing coefficients ---");
[3115]371
372 double p = compute_power_carte();
373 cout<<"Puissance dans la realisation: "<<p<<endl;
[3150]374 if(lp_>1) PrtTim("--- ComputeFourier: after before Computing power ---");
[3115]375
376}
377
[3129]378long GeneFluct3D::manage_coefficients(void)
[3115]379// Take into account the real and complexe conjugate coefficients
380// because we want a realization of a real data in real space
381{
[3141]382 check_array_alloc();
[3115]383
384 // 1./ Le Continu et Nyquist sont reels
[3129]385 long nreal = 0;
386 for(long kk=0;kk<2;kk++) {
387 long k=0; // continu
[3115]388 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]389 for(long jj=0;jj<2;jj++) {
390 long j=0;
[3115]391 if(jj==1) {if( Ny_%2!=0) continue; else j = Ny_/2;}
[3129]392 for(long ii=0;ii<2;ii++) {
393 long i=0;
[3115]394 if(ii==1) {if( Nx_%2!=0) continue; else i = Nx_/2;}
[3141]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;
[3115]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
[3129]407 long nconj1 = 0;
408 for(long kk=0;kk<2;kk++) {
409 long k=0; // continu
[3115]410 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]411 for(long jj=0;jj<2;jj++) { // selon j
412 long j=0;
[3115]413 if(jj==1) {if( Ny_%2!=0) continue; else j = Ny_/2;}
[3129]414 for(long i=1;i<(Nx_+1)/2;i++) {
[3141]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];
[3115]418 nconj1++;
419 }
420 }
[3129]421 for(long ii=0;ii<2;ii++) {
422 long i=0;
[3115]423 if(ii==1) {if( Nx_%2!=0) continue; else i = Nx_/2;}
[3129]424 for(long j=1;j<(Ny_+1)/2;j++) {
[3141]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];
[3115]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
[3129]435 long nconj2 = 0;
436 for(long kk=0;kk<2;kk++) {
437 long k=0; // continu
[3115]438 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]439 for(long j=1;j<(Ny_+1)/2;j++) {
[3115]440 if(Ny_%2==0 && j==Ny_/2) continue; // on ne retraite pas nyquist en j
[3129]441 for(long i=1;i<Nx_;i++) {
[3115]442 if(Nx_%2==0 && i==Nx_/2) continue; // on ne retraite pas nyquist en i
[3141]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];
[3115]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{
[3141]460 check_array_alloc();
461
[3115]462 double s2 = 0.;
[3129]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));
[3115]466
467 double s20 = 0.;
[3129]468 for(long j=0;j<Ny_;j++)
469 for(long i=0;i<Nx_;i++) s20 += MODULE2(T_(0,j,i));
[3115]470
471 double s2n = 0.;
472 if(Nz_%2==0)
[3129]473 for(long j=0;j<Ny_;j++)
474 for(long i=0;i<Nx_;i++) s2n += MODULE2(T_(NCz_-1,j,i));
[3115]475
476 return 2.*s2 -s20 -s2n;
477}
478
479//-------------------------------------------------------------------
480void GeneFluct3D::FilterByPixel(void)
481// Filtrage par la fonction fenetre du pixel (parallelepipede)
[3120]482// TF = 1/(dx*dy*dz)*Int[{-dx/2,dx/2},{-dy/2,dy/2},{-dz/2,dz/2}]
[3115]483// e^(ik_x*x) e^(ik_y*y) e^(ik_z*z) dxdydz
[3120]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
[3115]487{
[3141]488 check_array_alloc();
489
[3150]490 if(lp_>1) PrtTim("--- FilterByPixel: before ---");
[3115]491
[3129]492 for(long i=0;i<Nx_;i++) {
493 long ii = (i>Nx_/2) ? Nx_-i : i;
[3120]494 double kx = ii*Dkx_ *Dx_/2;
[3141]495 double pk_x = pixelfilter(kx);
[3129]496 for(long j=0;j<Ny_;j++) {
497 long jj = (j>Ny_/2) ? Ny_-j : j;
[3120]498 double ky = jj*Dky_ *Dy_/2;
[3141]499 double pk_y = pixelfilter(ky);
[3129]500 for(long l=0;l<NCz_;l++) {
[3120]501 double kz = l*Dkz_ *Dz_/2;
[3141]502 double pk_z = pixelfilter(kz);
503 T_(l,j,i) *= pk_x*pk_y*pk_z;
[3115]504 }
505 }
506 }
507
[3150]508 if(lp_>1) PrtTim("--- FilterByPixel: after ---");
[3115]509}
510
511//-------------------------------------------------------------------
512void GeneFluct3D::ComputeReal(void)
513// Calcule une realisation dans l'espace reel
514{
[3141]515 check_array_alloc();
[3115]516
517 // On fait la FFT
[3150]518 if(lp_>1) PrtTim("--- ComputeReal: before fftw backward ---");
[3115]519 fftw_execute(pb_);
[3150]520 if(lp_>1) PrtTim("--- ComputeReal: after fftw backward ---");
[3115]521}
522
523//-------------------------------------------------------------------
524void GeneFluct3D::ReComputeFourier(void)
525{
[3141]526 check_array_alloc();
[3115]527
528 // On fait la FFT
[3150]529 if(lp_>1) PrtTim("--- ComputeReal: before fftw forward ---");
[3115]530 fftw_execute(pf_);
531 // On corrige du pb de la normalisation de FFTW3
532 double v = (double)NRtot_;
[3129]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);
[3115]536
[3150]537 if(lp_>1) PrtTim("--- ComputeReal: after fftw forward ---");
[3115]538}
539
540//-------------------------------------------------------------------
[3141]541int GeneFluct3D::ComputeSpectrum(HistoErr& herr)
542// Compute spectrum from "T" and fill HistoErr "herr"
[3115]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{
[3141]546 check_array_alloc();
[3150]547 if(lp_>1) PrtTim("--- ComputeSpectrum: before ---");
[3115]548
[3141]549 if(herr.NBins()<0) return -1;
550 herr.Zero();
[3115]551
552 // Attention a l'ordre
[3129]553 for(long i=0;i<Nx_;i++) {
554 long ii = (i>Nx_/2) ? Nx_-i : i;
[3115]555 double kx = ii*Dkx_; kx *= kx;
[3129]556 for(long j=0;j<Ny_;j++) {
557 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]558 double ky = jj*Dky_; ky *= ky;
[3129]559 for(long l=0;l<NCz_;l++) {
[3115]560 double kz = l*Dkz_; kz *= kz;
561 double k = sqrt(kx+ky+kz);
562 double pk = MODULE2(T_(l,j,i));
[3141]563 herr.Add(k,pk);
[3115]564 }
565 }
566 }
[3150]567 herr.ToVariance();
[3115]568
569 // renormalize to directly compare to original spectrum
570 double norm = Vol_;
[3141]571 herr *= norm;
[3115]572
[3150]573 if(lp_>1) PrtTim("--- ComputeSpectrum: after ---");
574
[3115]575 return 0;
576}
577
[3141]578int GeneFluct3D::ComputeSpectrum2D(Histo2DErr& herr)
579{
580 check_array_alloc();
[3150]581 if(lp_>1) PrtTim("--- ComputeSpectrum2D: before ---");
[3141]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 }
[3150]601 herr.ToVariance();
[3141]602
603 // renormalize to directly compare to original spectrum
604 double norm = Vol_;
605 herr *= norm;
606
[3150]607 if(lp_>1) PrtTim("--- ComputeSpectrum2D: after ---");
608
[3141]609 return 0;
610}
611
[3115]612//-------------------------------------------------------
[3134]613int_8 GeneFluct3D::VarianceFrReal(double R,double& var)
[3115]614// Recompute MASS variance in spherical top-hat (rayon=R)
615{
[3141]616 check_array_alloc();
617
[3150]618 if(lp_>1) PrtTim("--- VarianceFrReal: before computation ---");
[3115]619
[3129]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;
[3115]623 cout<<"dnx="<<dnx<<" dny="<<dny<<" dnz="<<dnz<<endl;
624
[3134]625 double sum=0., sum2=0., r2 = R*R; int_8 nsum=0;
[3115]626
[3129]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) {
[3134]630 double s=0.; int_8 n=0;
[3129]631 for(long ii=i-dnx;ii<=i+dnx;ii++) {
[3115]632 double x = (ii-i)*Dx_; x *= x;
[3129]633 for(long jj=j-dny;jj<=j+dny;jj++) {
[3115]634 double y = (jj-j)*Dy_; y *= y;
[3129]635 for(long ll=l-dnz;ll<=l+dnz;ll++) {
[3115]636 double z = (ll-l)*Dz_; z *= z;
637 if(x+y+z>r2) continue;
[3141]638 int_8 ip = IndexR(ii,jj,ll);
639 s += 1.+data_[ip];
[3115]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;
[3150]654 if(lp_) cout<<"VarianceFrReal: nsum="<<nsum<<" <M>="<<sum<<" <(M-<M>)^2>="<<sum2<<endl;
[3115]655
656 var = sum2/(sum*sum); // <dM>^2/<M>^2
[3150]657 if(lp_) cout<<"sigmaR^2="<<var<<" -> "<<sqrt(var)<<endl;
[3115]658
[3150]659 if(lp_>1) PrtTim("--- VarianceFrReal: after computation ---");
[3115]660 return nsum;
661}
662
663//-------------------------------------------------------
[3134]664int_8 GeneFluct3D::NumberOfBad(double vmin,double vmax)
[3115]665// number of pixels outside of ]vmin,vmax[ extremites exclues
666// -> vmin and vmax are considered as bad
667{
[3141]668 check_array_alloc();
[3115]669
[3134]670 int_8 nbad = 0;
[3129]671 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]672 int_8 ip = IndexR(i,j,l);
673 double v = data_[ip];
[3115]674 if(v<=vmin || v>=vmax) nbad++;
675 }
676
677 return nbad;
678}
679
[3134]680int_8 GeneFluct3D::MeanSigma2(double& rm,double& rs2,double vmin,double vmax)
[3115]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{
[3141]684 check_array_alloc();
[3115]685
[3134]686 int_8 n = 0;
[3115]687 rm = rs2 = 0.;
688
[3129]689 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]690 int_8 ip = IndexR(i,j,l);
691 double v = data_[ip];
[3115]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
[3134]706int_8 GeneFluct3D::SetToVal(double vmin, double vmax,double val0)
[3115]707// set to "val0" if out of range ]vmin,vmax[ extremites exclues
708// -> vmin and vmax are set to val0
709{
[3141]710 check_array_alloc();
[3115]711
[3134]712 int_8 nbad = 0;
[3129]713 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]714 int_8 ip = IndexR(i,j,l);
715 double v = data_[ip];
716 if(v<=vmin || v>=vmax) {data_[ip] = val0; nbad++;}
[3115]717 }
718
719 return nbad;
720}
721
722//-------------------------------------------------------
723void GeneFluct3D::TurnFluct2Mass(void)
724// d_rho/rho -> Mass (add one!)
725{
[3141]726 check_array_alloc();
727
[3150]728 if(lp_>1) PrtTim("--- TurnFluct2Mass: before computation ---");
[3115]729
[3129]730 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]731 int_8 ip = IndexR(i,j,l);
732 data_[ip] += 1.;
[3115]733 }
734
[3150]735 if(lp_>1) PrtTim("--- TurnFluct2Mass: after computation ---");
[3115]736}
737
738double GeneFluct3D::TurnMass2MeanNumber(double n_by_mpc3)
739// do NOT treate negative or nul values
740{
[3150]741 if(lp_>1) PrtTim("--- TurnMass2MeanNumber: before computation ---");
[3115]742
743 double m,s2;
[3134]744 int_8 ngood = MeanSigma2(m,s2,0.,1e+200);
[3150]745 if(lp_) cout<<"TurnMass2MeanNumber: ngood="<<ngood
[3115]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
[3150]756 if(lp_) cout<<"galaxy density move from "
[3115]757 <<n_by_mpc3*Vol_/double(NRtot_)<<" to "<<dn<<" / pixel"<<endl;
758 double sum = 0.;
[3129]759 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]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
[3115]763 }
[3150]764 if(lp_) cout<<sum<<" galaxies put into survey / "<<n_by_mpc3*Vol_<<endl;
[3115]765
[3150]766 if(lp_>1) PrtTim("--- TurnMass2MeanNumber: after computation ---");
[3115]767 return sum;
768}
769
770double GeneFluct3D::ApplyPoisson(void)
771// do NOT treate negative or nul mass -> let it as it is
772{
[3141]773 check_array_alloc();
774
[3150]775 if(lp_>1) PrtTim("--- ApplyPoisson: before computation ---");
[3115]776
777 double sum = 0.;
[3129]778 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]779 int_8 ip = IndexR(i,j,l);
780 double v = data_[ip];
[3115]781 if(v>0.) {
782 unsigned long dn = PoissRandLimit(v,10.);
[3141]783 data_[ip] = (double)dn;
[3115]784 sum += (double)dn;
785 }
786 }
[3150]787 if(lp_) cout<<sum<<" galaxies put into survey"<<endl;
[3115]788
[3150]789 if(lp_>1) PrtTim("--- ApplyPoisson: before computation ---");
[3115]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{
[3141]801 check_array_alloc();
802
[3150]803 if(lp_>1) PrtTim("--- TurnNGal2Mass: before computation ---");
[3115]804
805 double sum = 0.;
[3129]806 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]807 int_8 ip = IndexR(i,j,l);
808 double v = data_[ip];
[3115]809 if(v>0.) {
[3129]810 long ngal = long(v+0.1);
[3141]811 data_[ip] = 0.;
[3129]812 for(long i=0;i<ngal;i++) {
[3115]813 double m = massdist.RandomInterp(); // massdist.Random();
814 if(axeslog) m = pow(10.,m);
[3141]815 data_[ip] += m;
[3115]816 }
[3141]817 sum += data_[ip];
[3115]818 }
819 }
[3150]820 if(lp_) cout<<sum<<" MSol HI mass put into survey"<<endl;
[3115]821
[3150]822 if(lp_>1) PrtTim("--- TurnNGal2Mass: after computation ---");
[3115]823 return sum;
824}
825
826void GeneFluct3D::AddNoise2Real(double snoise)
827// add noise to every pixels (meme les <=0 !)
828{
[3141]829 check_array_alloc();
830
[3150]831 if(lp_>1) PrtTim("--- AddNoise2Real: before computation ---");
[3115]832
[3129]833 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]834 int_8 ip = IndexR(i,j,l);
835 data_[ip] += snoise*NorRand();
[3115]836 }
837
[3150]838 if(lp_>1) PrtTim("--- AddNoise2Real: after computation ---");
[3115]839}
Note: See TracBrowser for help on using the repository browser.