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

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

modifs d'options de cmvobserv3d.cc
creation de init_fftw() et deplacement a l'initialisation
creation de SetObservator pour positionner l'observateur + ecriture FITS et PPF

(mais le codage des distance/redshift reste a coder)

cmv 19/01/2007

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