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

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

chgt HProf->HistoErr + spectre 2D cmv 17/01/2007

File size: 24.9 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)
34 : T_(T) , array_allocated_(false)
[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 int lp=2;
260
261 // --- Initialisation de fftw3 (attention data est sur-ecrit a l'init)
262 if(lp>1) PrtTim("--- ComputeFourier0: before fftw_plan ---");
263#ifdef FFTW_THREAD
264 if(nthread_>0) {
265 cout<<"Computing with "<<nthread_<<" threads"<<endl;
266 fftw_init_threads();
267 fftw_plan_with_nthreads(nthread_);
268 }
269#endif
[3141]270 pf_ = fftw_plan_dft_r2c_3d(Nx_,Ny_,Nz_,data_,fdata_,FFTW_ESTIMATE);
271 pb_ = fftw_plan_dft_c2r_3d(Nx_,Ny_,Nz_,fdata_,data_,FFTW_ESTIMATE);
[3115]272 if(lp>1) PrtTim("--- ComputeFourier0: after fftw_plan ---");
273
274 // --- realisation d'un tableau de tirage gaussiens
275 if(lp>1) PrtTim("--- ComputeFourier0: before gaussian filling ---");
276 // On tient compte du pb de normalisation de FFTW3
277 double sntot = sqrt((double)NRtot_);
[3129]278 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]279 int_8 ip = IndexR(i,j,l);
280 data_[ip] = NorRand()/sntot;
[3115]281 }
282 if(lp>1) PrtTim("--- ComputeFourier0: after gaussian filling ---");
283
284 // --- realisation d'un tableau de tirage gaussiens
285 if(lp>1) PrtTim("--- ComputeFourier0: before fft real ---");
286 fftw_execute(pf_);
287 if(lp>1) PrtTim("--- ComputeFourier0: after fft real ---");
288
289 // --- On remplit avec une realisation
290 if(lp>1) PrtTim("--- ComputeFourier0: before Fourier realization filling ---");
291 T_(0,0,0) = complex<r_8>(0.); // on coupe le continue et on l'initialise
[3129]292 long lmod = Nx_/10; if(lmod<1) lmod=1;
293 for(long i=0;i<Nx_;i++) {
294 long ii = (i>Nx_/2) ? Nx_-i : i;
[3115]295 double kx = ii*Dkx_; kx *= kx;
296 if(lp>1 && i%lmod==0) cout<<"i="<<i<<" ii="<<ii<<endl;
[3129]297 for(long j=0;j<Ny_;j++) {
298 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]299 double ky = jj*Dky_; ky *= ky;
[3129]300 for(long l=0;l<NCz_;l++) {
[3115]301 double kz = l*Dkz_; kz *= kz;
302 if(i==0 && j==0 && l==0) continue; // Suppression du continu
303 double k = sqrt(kx+ky+kz);
304 // cf normalisation: Peacock, Cosmology, formule 16.38 p504
[3141]305 double pk = pk_at_z(k)/Vol_;
[3115]306 // ici pas de "/2" a cause de la remarque ci-dessus
307 T_(l,j,i) *= sqrt(pk);
308 }
309 }
310 }
311 if(lp>1) PrtTim("--- ComputeFourier0: after Fourier realization filling ---");
312
313 double p = compute_power_carte();
314 cout<<"Puissance dans la realisation: "<<p<<endl;
315 if(lp>1) PrtTim("--- ComputeFourier0: after Computing power ---");
316
317}
318
319//-------------------------------------------------------
[3141]320void GeneFluct3D::ComputeFourier(GenericFunc& pk_at_z)
321// Calcule une realisation du spectre "pk_at_z"
[3115]322// Attention: dans TArray le premier indice varie le + vite
323// Explication normalisation: see Coles & Lucchin, Cosmology, p264-265
324// FFTW3: on note N=Nx*Ny*Nz
325// f --(FFT)--> F = TF(f) --(FFT^-1)--> fb = TF^-1(F) = TF^-1(TF(f))
326// sum(f(x_i)^2) = S
327// sum(F(nu_i)^2) = S*N
328// sum(fb(x_i)^2) = S*N^2
329{
330 int lp=2;
331
332 // --- Initialisation de fftw3 (attention data est sur-ecrit a l'init)
333 if(lp>1) PrtTim("--- ComputeFourier: before fftw_plan ---");
334#ifdef FFTW_THREAD
335 if(nthread_>0) {
336 cout<<"Computing with "<<nthread_<<" threads"<<endl;
337 fftw_init_threads();
338 fftw_plan_with_nthreads(nthread_);
339 }
340#endif
[3141]341 pf_ = fftw_plan_dft_r2c_3d(Nx_,Ny_,Nz_,data_,fdata_,FFTW_ESTIMATE);
342 pb_ = fftw_plan_dft_c2r_3d(Nx_,Ny_,Nz_,fdata_,data_,FFTW_ESTIMATE);
[3115]343 //fftw_print_plan(pb_); cout<<endl;
344 if(lp>1) PrtTim("--- ComputeFourier: after fftw_plan ---");
345
346 // --- RaZ du tableau
347 T_ = complex<r_8>(0.);
348
349 // --- On remplit avec une realisation
350 if(lp>1) PrtTim("--- ComputeFourier: before Fourier realization filling ---");
[3129]351 long lmod = Nx_/10; if(lmod<1) lmod=1;
352 for(long i=0;i<Nx_;i++) {
353 long ii = (i>Nx_/2) ? Nx_-i : i;
[3115]354 double kx = ii*Dkx_; kx *= kx;
355 if(lp>1 && i%lmod==0) cout<<"i="<<i<<" ii="<<ii<<endl;
[3129]356 for(long j=0;j<Ny_;j++) {
357 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]358 double ky = jj*Dky_; ky *= ky;
[3129]359 for(long l=0;l<NCz_;l++) {
[3115]360 double kz = l*Dkz_; kz *= kz;
361 if(i==0 && j==0 && l==0) continue; // Suppression du continu
362 double k = sqrt(kx+ky+kz);
363 // cf normalisation: Peacock, Cosmology, formule 16.38 p504
[3141]364 double pk = pk_at_z(k)/Vol_;
[3115]365 // Explication de la division par 2: voir perandom.cc
366 // ou egalement Coles & Lucchin, Cosmology formula 13.7.2 p279
367 T_(l,j,i) = ComplexGaussRan(sqrt(pk/2.));
368 }
369 }
370 }
371 if(lp>1) PrtTim("--- ComputeFourier: after Fourier realization filling ---");
372
373 manage_coefficients(); // gros effet pour les spectres que l'on utilise !
374 if(lp>1) PrtTim("--- ComputeFourier: after managing coefficients ---");
375
376 double p = compute_power_carte();
377 cout<<"Puissance dans la realisation: "<<p<<endl;
378 if(lp>1) PrtTim("--- ComputeFourier: after before Computing power ---");
379
380}
381
[3129]382long GeneFluct3D::manage_coefficients(void)
[3115]383// Take into account the real and complexe conjugate coefficients
384// because we want a realization of a real data in real space
385{
[3141]386 check_array_alloc();
[3115]387
388 // 1./ Le Continu et Nyquist sont reels
[3129]389 long nreal = 0;
390 for(long kk=0;kk<2;kk++) {
391 long k=0; // continu
[3115]392 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]393 for(long jj=0;jj<2;jj++) {
394 long j=0;
[3115]395 if(jj==1) {if( Ny_%2!=0) continue; else j = Ny_/2;}
[3129]396 for(long ii=0;ii<2;ii++) {
397 long i=0;
[3115]398 if(ii==1) {if( Nx_%2!=0) continue; else i = Nx_/2;}
[3141]399 int_8 ip = IndexC(i,j,k);
400 //cout<<"i="<<i<<" j="<<j<<" k="<<k<<" = ("<<fdata_[ip][0]<<","<<fdata_[ip][1]<<")"<<endl;
401 fdata_[ip][1] = 0.; fdata_[ip][0] *= M_SQRT2;
[3115]402 nreal++;
403 }
404 }
405 }
406 cout<<"Number of forced real number ="<<nreal<<endl;
407
408 // 2./ Les elements complexe conjugues (tous dans le plan k=0,Nyquist)
409
410 // a./ les lignes et colonnes du continu et de nyquist
[3129]411 long nconj1 = 0;
412 for(long kk=0;kk<2;kk++) {
413 long k=0; // continu
[3115]414 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]415 for(long jj=0;jj<2;jj++) { // selon j
416 long j=0;
[3115]417 if(jj==1) {if( Ny_%2!=0) continue; else j = Ny_/2;}
[3129]418 for(long i=1;i<(Nx_+1)/2;i++) {
[3141]419 int_8 ip = IndexC(i,j,k);
420 int_8 ip1 = IndexC(Nx_-i,j,k);
421 fdata_[ip1][0] = fdata_[ip][0]; fdata_[ip1][1] = -fdata_[ip][1];
[3115]422 nconj1++;
423 }
424 }
[3129]425 for(long ii=0;ii<2;ii++) {
426 long i=0;
[3115]427 if(ii==1) {if( Nx_%2!=0) continue; else i = Nx_/2;}
[3129]428 for(long j=1;j<(Ny_+1)/2;j++) {
[3141]429 int_8 ip = IndexC(i,j,k);
430 int_8 ip1 = IndexC(i,Ny_-j,k);
431 fdata_[ip1][0] = fdata_[ip][0]; fdata_[ip1][1] = -fdata_[ip][1];
[3115]432 nconj1++;
433 }
434 }
435 }
436 cout<<"Number of forced conjugate on cont+nyq ="<<nconj1<<endl;
437
438 // b./ les lignes et colonnes hors continu et de nyquist
[3129]439 long nconj2 = 0;
440 for(long kk=0;kk<2;kk++) {
441 long k=0; // continu
[3115]442 if(kk==1) {if(Nz_%2!=0) continue; else k = Nz_/2;} // Nyquist
[3129]443 for(long j=1;j<(Ny_+1)/2;j++) {
[3115]444 if(Ny_%2==0 && j==Ny_/2) continue; // on ne retraite pas nyquist en j
[3129]445 for(long i=1;i<Nx_;i++) {
[3115]446 if(Nx_%2==0 && i==Nx_/2) continue; // on ne retraite pas nyquist en i
[3141]447 int_8 ip = IndexC(i,j,k);
448 int_8 ip1 = IndexC(Nx_-i,Ny_-j,k);
449 fdata_[ip1][0] = fdata_[ip][0]; fdata_[ip1][1] = -fdata_[ip][1];
[3115]450 nconj2++;
451 }
452 }
453 }
454 cout<<"Number of forced conjugate hors cont+nyq ="<<nconj2<<endl;
455
456 cout<<"Check: ddl= "<<NRtot_<<" =?= "<<2*(Nx_*Ny_*NCz_-nconj1-nconj2)-8<<endl;
457
458 return nreal+nconj1+nconj2;
459}
460
461double GeneFluct3D::compute_power_carte(void)
462// Calcul la puissance de la realisation du spectre Pk
463{
[3141]464 check_array_alloc();
465
[3115]466 double s2 = 0.;
[3129]467 for(long l=0;l<NCz_;l++)
468 for(long j=0;j<Ny_;j++)
469 for(long i=0;i<Nx_;i++) s2 += MODULE2(T_(l,j,i));
[3115]470
471 double s20 = 0.;
[3129]472 for(long j=0;j<Ny_;j++)
473 for(long i=0;i<Nx_;i++) s20 += MODULE2(T_(0,j,i));
[3115]474
475 double s2n = 0.;
476 if(Nz_%2==0)
[3129]477 for(long j=0;j<Ny_;j++)
478 for(long i=0;i<Nx_;i++) s2n += MODULE2(T_(NCz_-1,j,i));
[3115]479
480 return 2.*s2 -s20 -s2n;
481}
482
483//-------------------------------------------------------------------
484void GeneFluct3D::FilterByPixel(void)
485// Filtrage par la fonction fenetre du pixel (parallelepipede)
[3120]486// TF = 1/(dx*dy*dz)*Int[{-dx/2,dx/2},{-dy/2,dy/2},{-dz/2,dz/2}]
[3115]487// e^(ik_x*x) e^(ik_y*y) e^(ik_z*z) dxdydz
[3120]488// = 2/(k_x*dx) * sin(k_x*dx/2) * (idem y) * (idem z)
489// Gestion divergence en 0: sin(y)/y = 1 - y^2/6*(1-y^2/20)
490// avec y = k_x*dx/2
[3115]491{
492 int lp=2;
[3141]493 check_array_alloc();
494
[3115]495 if(lp>1) PrtTim("--- FilterByPixel: before ---");
496
[3129]497 for(long i=0;i<Nx_;i++) {
498 long ii = (i>Nx_/2) ? Nx_-i : i;
[3120]499 double kx = ii*Dkx_ *Dx_/2;
[3141]500 double pk_x = pixelfilter(kx);
[3129]501 for(long j=0;j<Ny_;j++) {
502 long jj = (j>Ny_/2) ? Ny_-j : j;
[3120]503 double ky = jj*Dky_ *Dy_/2;
[3141]504 double pk_y = pixelfilter(ky);
[3129]505 for(long l=0;l<NCz_;l++) {
[3120]506 double kz = l*Dkz_ *Dz_/2;
[3141]507 double pk_z = pixelfilter(kz);
508 T_(l,j,i) *= pk_x*pk_y*pk_z;
[3115]509 }
510 }
511 }
512
513 if(lp>1) PrtTim("--- FilterByPixel: after ---");
514}
515
516//-------------------------------------------------------------------
517void GeneFluct3D::ComputeReal(void)
518// Calcule une realisation dans l'espace reel
519{
520 int lp=2;
[3141]521 check_array_alloc();
[3115]522
523 // On fait la FFT
524 if(lp>1) PrtTim("--- ComputeReal: before fftw backward ---");
525 fftw_execute(pb_);
526 if(lp>1) PrtTim("--- ComputeReal: after fftw backward ---");
527}
528
529//-------------------------------------------------------------------
530void GeneFluct3D::ReComputeFourier(void)
531{
532 int lp=2;
[3141]533 check_array_alloc();
[3115]534
535 // On fait la FFT
536 if(lp>1) PrtTim("--- ComputeReal: before fftw forward ---");
537 fftw_execute(pf_);
538 // On corrige du pb de la normalisation de FFTW3
539 double v = (double)NRtot_;
[3129]540 for(long i=0;i<Nx_;i++)
541 for(long j=0;j<Ny_;j++)
542 for(long l=0;l<NCz_;l++) T_(l,j,i) /= complex<r_8>(v);
[3115]543
544 if(lp>1) PrtTim("--- ComputeReal: after fftw forward ---");
545}
546
547//-------------------------------------------------------------------
[3141]548int GeneFluct3D::ComputeSpectrum(HistoErr& herr)
549// Compute spectrum from "T" and fill HistoErr "herr"
[3115]550// T : dans le format standard de GeneFuct3D: T(nz,ny,nx)
551// cad T(kz,ky,kx) avec 0<kz<kz_nyq -ky_nyq<ky<ky_nyq -kx_nyq<kx<kx_nyq
552{
[3141]553 check_array_alloc();
[3115]554
[3141]555 if(herr.NBins()<0) return -1;
556 herr.Zero();
[3115]557
558 // Attention a l'ordre
[3129]559 for(long i=0;i<Nx_;i++) {
560 long ii = (i>Nx_/2) ? Nx_-i : i;
[3115]561 double kx = ii*Dkx_; kx *= kx;
[3129]562 for(long j=0;j<Ny_;j++) {
563 long jj = (j>Ny_/2) ? Ny_-j : j;
[3115]564 double ky = jj*Dky_; ky *= ky;
[3129]565 for(long l=0;l<NCz_;l++) {
[3115]566 double kz = l*Dkz_; kz *= kz;
567 double k = sqrt(kx+ky+kz);
568 double pk = MODULE2(T_(l,j,i));
[3141]569 herr.Add(k,pk);
[3115]570 }
571 }
572 }
[3141]573 herr.ToCorrel();
[3115]574
575 // renormalize to directly compare to original spectrum
576 double norm = Vol_;
[3141]577 herr *= norm;
[3115]578
579 return 0;
580}
581
[3141]582int GeneFluct3D::ComputeSpectrum2D(Histo2DErr& herr)
583{
584 check_array_alloc();
585
586 if(herr.NBinX()<0 || herr.NBinY()<0) return -1;
587 herr.Zero();
588
589 // Attention a l'ordre
590 for(long i=0;i<Nx_;i++) {
591 long ii = (i>Nx_/2) ? Nx_-i : i;
592 double kx = ii*Dkx_; kx *= kx;
593 for(long j=0;j<Ny_;j++) {
594 long jj = (j>Ny_/2) ? Ny_-j : j;
595 double ky = jj*Dky_; ky *= ky;
596 double kt = sqrt(kx+ky);
597 for(long l=0;l<NCz_;l++) {
598 double kz = l*Dkz_;
599 double pk = MODULE2(T_(l,j,i));
600 herr.Add(kt,kz,pk);
601 }
602 }
603 }
604 herr.ToCorrel();
605
606 // renormalize to directly compare to original spectrum
607 double norm = Vol_;
608 herr *= norm;
609
610 return 0;
611}
612
[3115]613//-------------------------------------------------------
[3134]614int_8 GeneFluct3D::VarianceFrReal(double R,double& var)
[3115]615// Recompute MASS variance in spherical top-hat (rayon=R)
616{
617 int lp=2;
[3141]618 check_array_alloc();
619
[3115]620 if(lp>1) PrtTim("--- VarianceFrReal: before computation ---");
621
[3129]622 long dnx = long(R/Dx_+0.5); if(dnx<=0) dnx = 1;
623 long dny = long(R/Dy_+0.5); if(dny<=0) dny = 1;
624 long dnz = long(R/Dz_+0.5); if(dnz<=0) dnz = 1;
[3115]625 cout<<"dnx="<<dnx<<" dny="<<dny<<" dnz="<<dnz<<endl;
626
[3134]627 double sum=0., sum2=0., r2 = R*R; int_8 nsum=0;
[3115]628
[3129]629 for(long i=dnx;i<Nx_-dnx;i+=dnx) {
630 for(long j=dny;j<Ny_-dny;j+=dny) {
631 for(long l=dnz;l<Nz_-dnz;l+=dnz) {
[3134]632 double s=0.; int_8 n=0;
[3129]633 for(long ii=i-dnx;ii<=i+dnx;ii++) {
[3115]634 double x = (ii-i)*Dx_; x *= x;
[3129]635 for(long jj=j-dny;jj<=j+dny;jj++) {
[3115]636 double y = (jj-j)*Dy_; y *= y;
[3129]637 for(long ll=l-dnz;ll<=l+dnz;ll++) {
[3115]638 double z = (ll-l)*Dz_; z *= z;
639 if(x+y+z>r2) continue;
[3141]640 int_8 ip = IndexR(ii,jj,ll);
641 s += 1.+data_[ip];
[3115]642 n++;
643 }
644 }
645 }
646 if(n>0) {sum += s; sum2 += s*s; nsum++;}
647 //cout<<i<<","<<j<<","<<l<<" n="<<n<<" s="<<s<<" sum="<<sum<<" sum2="<<sum2<<endl;
648 }
649 }
650 }
651
652 if(nsum<=1) {var=0.; return nsum;}
653
654 sum /= nsum;
655 sum2 = sum2/nsum - sum*sum;
656 if(lp) cout<<"VarianceFrReal: nsum="<<nsum<<" <M>="<<sum<<" <(M-<M>)^2>="<<sum2<<endl;
657
658 var = sum2/(sum*sum); // <dM>^2/<M>^2
659 if(lp) cout<<"sigmaR^2="<<var<<" -> "<<sqrt(var)<<endl;
660
661 if(lp>1) PrtTim("--- VarianceFrReal: after computation ---");
662 return nsum;
663}
664
665//-------------------------------------------------------
[3134]666int_8 GeneFluct3D::NumberOfBad(double vmin,double vmax)
[3115]667// number of pixels outside of ]vmin,vmax[ extremites exclues
668// -> vmin and vmax are considered as bad
669{
[3141]670 check_array_alloc();
[3115]671
[3134]672 int_8 nbad = 0;
[3129]673 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]674 int_8 ip = IndexR(i,j,l);
675 double v = data_[ip];
[3115]676 if(v<=vmin || v>=vmax) nbad++;
677 }
678
679 return nbad;
680}
681
[3134]682int_8 GeneFluct3D::MeanSigma2(double& rm,double& rs2,double vmin,double vmax)
[3115]683// mean,sigma^2 pour pixels avec valeurs ]vmin,vmax[ extremites exclues
684// -> mean and sigma^2 are NOT computed with pixels values vmin and vmax
685{
[3141]686 check_array_alloc();
[3115]687
[3134]688 int_8 n = 0;
[3115]689 rm = rs2 = 0.;
690
[3129]691 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]692 int_8 ip = IndexR(i,j,l);
693 double v = data_[ip];
[3115]694 if(v<=vmin || v>=vmax) continue;
695 rm += v;
696 rs2 += v*v;
697 n++;
698 }
699
700 if(n>1) {
701 rm /= (double)n;
702 rs2 = rs2/(double)n - rm*rm;
703 }
704
705 return n;
706}
707
[3134]708int_8 GeneFluct3D::SetToVal(double vmin, double vmax,double val0)
[3115]709// set to "val0" if out of range ]vmin,vmax[ extremites exclues
710// -> vmin and vmax are set to val0
711{
[3141]712 check_array_alloc();
[3115]713
[3134]714 int_8 nbad = 0;
[3129]715 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]716 int_8 ip = IndexR(i,j,l);
717 double v = data_[ip];
718 if(v<=vmin || v>=vmax) {data_[ip] = val0; nbad++;}
[3115]719 }
720
721 return nbad;
722}
723
724//-------------------------------------------------------
725void GeneFluct3D::TurnFluct2Mass(void)
726// d_rho/rho -> Mass (add one!)
727{
728 int lp=2;
[3141]729 check_array_alloc();
730
[3115]731 if(lp>1) PrtTim("--- TurnFluct2Mass: before computation ---");
732
[3129]733 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]734 int_8 ip = IndexR(i,j,l);
735 data_[ip] += 1.;
[3115]736 }
737
738 if(lp>1) PrtTim("--- TurnFluct2Mass: after computation ---");
739}
740
741double GeneFluct3D::TurnMass2MeanNumber(double n_by_mpc3)
742// do NOT treate negative or nul values
743{
744 int lp=2;
745 if(lp>1) PrtTim("--- TurnMass2MeanNumber: before computation ---");
746
747 double m,s2;
[3134]748 int_8 ngood = MeanSigma2(m,s2,0.,1e+200);
[3115]749 if(lp) cout<<"TurnMass2MeanNumber: ngood="<<ngood
750 <<" m="<<m<<" s2="<<s2<<" -> "<<sqrt(s2)<<endl;
751
752 // On doit mettre n*Vol galaxies dans notre survey
753 // On en met uniquement dans les pixels de masse >0.
754 // On NE met PAS a zero les pixels <0
755 // On renormalise sur les pixels>0 pour qu'on ait n*Vol galaxies
756 // comme on ne prend que les pixels >0, on doit normaliser
757 // a la moyenne de <1+d_rho/rho> sur ces pixels
758 // (rappel sur tout les pixels <1+d_rho/rho>=1)
759 double dn = n_by_mpc3*Vol_/m /(double)ngood; // nb de gal a mettre ds 1 px
760 if(lp) cout<<"galaxy density move from "
761 <<n_by_mpc3*Vol_/double(NRtot_)<<" to "<<dn<<" / pixel"<<endl;
762 double sum = 0.;
[3129]763 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]764 int_8 ip = IndexR(i,j,l);
765 data_[ip] *= dn; // par coherence on multiplie aussi les <=0
766 if(data_[ip]>0.) sum += data_[ip]; // mais on ne les compte pas
[3115]767 }
768 if(lp) cout<<sum<<" galaxies put into survey / "<<n_by_mpc3*Vol_<<endl;
769
770 if(lp>1) PrtTim("--- TurnMass2MeanNumber: after computation ---");
771 return sum;
772}
773
774double GeneFluct3D::ApplyPoisson(void)
775// do NOT treate negative or nul mass -> let it as it is
776{
777 int lp=2;
[3141]778 check_array_alloc();
779
[3115]780 if(lp>1) PrtTim("--- ApplyPoisson: before computation ---");
781
782 double sum = 0.;
[3129]783 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]784 int_8 ip = IndexR(i,j,l);
785 double v = data_[ip];
[3115]786 if(v>0.) {
787 unsigned long dn = PoissRandLimit(v,10.);
[3141]788 data_[ip] = (double)dn;
[3115]789 sum += (double)dn;
790 }
791 }
792 if(lp) cout<<sum<<" galaxies put into survey"<<endl;
793
794 if(lp>1) PrtTim("--- ApplyPoisson: before computation ---");
795 return sum;
796}
797
798double GeneFluct3D::TurnNGal2Mass(FunRan& massdist,bool axeslog)
799// do NOT treate negative or nul mass -> let it as it is
800// INPUT:
801// massdist : distribution de masse (m*dn/dm)
802// axeslog = false : retourne la masse
803// = true : retourne le log10(mass)
804// RETURN la masse totale
805{
806 int lp=2;
[3141]807 check_array_alloc();
808
[3115]809 if(lp>1) PrtTim("--- TurnNGal2Mass: before computation ---");
810
811 double sum = 0.;
[3129]812 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]813 int_8 ip = IndexR(i,j,l);
814 double v = data_[ip];
[3115]815 if(v>0.) {
[3129]816 long ngal = long(v+0.1);
[3141]817 data_[ip] = 0.;
[3129]818 for(long i=0;i<ngal;i++) {
[3115]819 double m = massdist.RandomInterp(); // massdist.Random();
820 if(axeslog) m = pow(10.,m);
[3141]821 data_[ip] += m;
[3115]822 }
[3141]823 sum += data_[ip];
[3115]824 }
825 }
826 if(lp) cout<<sum<<" MSol HI mass put into survey"<<endl;
827
828 if(lp>1) PrtTim("--- TurnNGal2Mass: after computation ---");
829 return sum;
830}
831
832void GeneFluct3D::AddNoise2Real(double snoise)
833// add noise to every pixels (meme les <=0 !)
834{
835 int lp=2;
[3141]836 check_array_alloc();
837
[3115]838 if(lp>1) PrtTim("--- AddNoise2Real: before computation ---");
839
[3129]840 for(long i=0;i<Nx_;i++) for(long j=0;j<Ny_;j++) for(long l=0;l<Nz_;l++) {
[3141]841 int_8 ip = IndexR(i,j,l);
842 data_[ip] += snoise*NorRand();
[3115]843 }
844
845 if(lp>1) PrtTim("--- AddNoise2Real: after computation ---");
846}
Note: See TracBrowser for help on using the repository browser.