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