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