source: Sophya/trunk/Cosmo/SimLSS/pkspectrum.cc@ 3380

Last change on this file since 3380 was 3378, checked in by cmv, 18 years ago

modif sur remplissage NTuple/Minos, possibilite de changer les parametres O0,Om,Ol etc... , cmv 09/11/2007

File size: 21.4 KB
RevLine 
[3115]1#include "machdefs.h"
2#include <iostream>
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <math.h>
7#include <unistd.h>
8
9#include "pexceptions.h"
10
11#include "constcosmo.h"
[3196]12#include "geneutils.h"
[3115]13#include "pkspectrum.h"
14
[3325]15namespace SOPHYA {
[3115]16
17///////////////////////////////////////////////////////////
18//******************** InitialSpectrum ******************//
19///////////////////////////////////////////////////////////
20
21InitialSpectrum::InitialSpectrum(double n,double a)
22 : n_(n), A_(a)
23{
24}
25
26InitialSpectrum::InitialSpectrum(InitialSpectrum& pkinf)
27 : n_(pkinf.n_), A_(pkinf.A_)
28{
29}
30
31InitialSpectrum::~InitialSpectrum(void)
32{
33}
34
35void InitialSpectrum::SetNorm(double a)
36{
37 A_ = a;
38}
39
40void InitialSpectrum::SetSlope(double n)
41{
42 n_ = n;
43}
44
45
46///////////////////////////////////////////////////////////
47//****************** TransfertEisenstein ****************//
48///////////////////////////////////////////////////////////
49
[3314]50// From Eisenstein & Hu ApJ 496:605-614 1998 April 1 (ou astro-ph/9709112)
51TransfertEisenstein::TransfertEisenstein(double h100,double OmegaCDM0,double OmegaBaryon0,double tcmb,bool nobaryon,int lp)
52 : lp_(lp)
[3378]53 , Oc_(OmegaCDM0) , Ob_(OmegaBaryon0) , h100_(h100) , tcmb_(tcmb)
[3348]54 , nobaryon_(nobaryon) , nooscenv_(0), retpart_(ALL)
[3115]55{
[3314]56 zero_();
57 Init_();
[3115]58}
59
60TransfertEisenstein::TransfertEisenstein(TransfertEisenstein& tf)
[3314]61 : lp_(tf.lp_)
[3378]62 ,Oc_(tf.Oc_) , Ob_(tf.Ob_) , h100_(tf.h100_) , tcmb_(tf.tcmb_)
[3314]63 , nobaryon_(tf.nobaryon_) , nooscenv_(tf.nooscenv_), retpart_(tf.retpart_)
[3115]64{
[3314]65 zero_();
66 Init_();
[3115]67}
68
[3378]69TransfertEisenstein::~TransfertEisenstein(void)
70{
71}
72
[3318]73void TransfertEisenstein::zero_(void)
74{
75 th2p7_=zeq_=keq_=zd_=Req_=Rd_=s_=ksilk_=alphac_=betac_=bnode_
76 =alphab_=betab_=alphag_=sfit_=kpeak_=1.e99;
77}
78
[3314]79void TransfertEisenstein::Init_(void)
[3115]80{
81
82 O0_ = Oc_ + Ob_;
[3314]83 if(nobaryon_) {O0_ = Oc_; Ob_ = 0.;}
[3378]84 double H0 = 100. * h100_, h2 = h100_*h100_;
85 if(lp_) cout<<"h100="<<h100_<<" H0="<<H0<<") Omatter="<<O0_<<" Ocdm="<<Oc_<<" Ob="<<Ob_<<endl;
[3115]86
[3329]87
[3314]88 if(tcmb_<0.) tcmb_ = T_CMB_Par;
[3115]89 th2p7_ = tcmb_/2.7;
90 double th2p7P4 = th2p7_*th2p7_*th2p7_*th2p7_;
[3314]91 if(lp_) cout<<"tcmb = "<<tcmb_<<" K = "<<th2p7_<<" *2.7K "<<endl;
[3115]92
93 // Formule 2 p 606
94 zeq_ = 2.50e4 * O0_ * h2 / th2p7P4;
[3314]95 if(lp_) cout<<"zeq = "<<zeq_<<" (redshift of matter-radiation equality)"<<endl;
[3115]96
97 // Formule 3 p 607
98 // (attention ici C=1 : H0 -> H0/C si on utilise la premiere formule)
99 // keq_ = sqrt(2.*O0_*H0*H0*zeq_) / SpeedOfLight_Cst;
100 keq_ = 7.46e-2 * O0_ * h2 / (th2p7_*th2p7_);
[3314]101 if(lp_) cout<<"keq = "<<keq_<<" Mpc^-1 (scale of equality)"<<endl;
[3115]102
[3314]103 // On s'arrete ici si pas de baryons
104 if(nobaryon_) return;
105
[3115]106 // Formule 4 p 607
107 double b1_eq4 = 0.313*pow(O0_*h2,-0.419)*(1. + 0.607*pow(O0_*h2,0.674));
108 double b2_eq4 = 0.238*pow(O0_*h2,0.223);
[3314]109 zd_ = 1291. * pow(O0_*h2,0.251) / (1.+0.659* pow(O0_*h2,0.828))
110 * (1. + b1_eq4*pow(Ob_*h2,b2_eq4));
111 if(lp_) cout<<"zd = "<<zd_<<" (Redshift of drag epoch)"<<endl;
[3115]112
[3314]113 // Formule 5 page 607 (R = 3*rho_baryon/4*rho_gamma)
[3115]114 Req_ = 31.5*Ob_*h2 / th2p7P4 * (1.e3/zeq_);
[3314]115 //WARNING: W.Hu code (tf_fit.c) en des-accord avec l'article: zd -> (1+zd)
[3115]116 Rd_ = 31.5*Ob_*h2 / th2p7P4 * (1.e3/zd_);
[3314]117 //in tf_fit.c: Rd_ = 31.5*Ob_*h2 / th2p7P4 * (1.e3/(1.+zd_));
118 if(lp_) {
119 cout<<"Req = "<<Req_<<" Rd = "<<Rd_
120 <<" (Photon-baryon ratio at equality/drag epoch)"<<endl;
121 cout<<"Sound speed at equality "<<1./sqrt(3.*(1.+Req_))
122 <<", at drag "<<1./sqrt(3.*(1.+Rd_))<<" in unit of C"<<endl;
123 }
[3115]124
125 // Formule 6 p 607
126 s_ = 2./(3.*keq_) * sqrt(6./Req_)
127 * log( (sqrt(1.+Rd_) + sqrt(Rd_+Req_)) / (1.+sqrt(Req_)) );
[3314]128 if(lp_) cout<<"s = "<<s_<<" Mpc (sound horizon at drag epoch)"<<endl;
[3115]129
130 // Formule 7 page 607
131 ksilk_ = 1.6*pow(Ob_*h2,0.52)*pow(O0_*h2,0.73) * (1. + pow(10.4*O0_*h2,-0.95));
[3314]132 if(lp_) cout<<"ksilk = "<<ksilk_<<" Mpc^-1 (silk damping scale)"<<endl;
[3115]133
134 // Formules 10 page 608
135 double a1 = pow(46.9*O0_*h2,0.670) * (1. + pow(32.1*O0_*h2,-0.532));
136 double a2 = pow(12.0*O0_*h2,0.424) * (1. + pow(45.0*O0_*h2,-0.582));
137 alphac_ = pow(a1,-Ob_/O0_) * pow(a2,-pow(Ob_/O0_,3.));
138 double b1 = 0.944 / (1. + pow(458.*O0_*h2,-0.708));
139 double b2 = pow(0.395*O0_*h2,-0.0266);
140 betac_ = 1 / ( 1. + b1*(pow(Oc_/O0_,b2) - 1.) );
[3314]141 if(lp_) cout<<"alphac = "<<alphac_<<" betac = "<<betac_
142 <<" (CDM suppression/log shift)"<<endl;
[3115]143
144 // Formule 23 page 610
145 bnode_ = 8.41 * pow(O0_*h2,0.435);
[3314]146 if(lp_) cout<<"bnode = "<<bnode_<<" (sound horizon shift)"<<endl;
[3115]147
148 // Formule 14 page 608
[3314]149 //WARNING: W.Hu code (tf_fit.c) en des-accord avec l'article: (1+zeq) -> zeq
[3115]150 double y = (1.+zeq_)/(1.+zd_);
[3314]151 //in tf_fit.c: double y = zeq_/(1.+zd_);
[3115]152 double s1py = sqrt(1.+y);
153 double Gy = y*( -6.*s1py + (2.+3.*y)*log((s1py+1.)/(s1py-1.)) );
154 alphab_ = 2.07*keq_*s_*pow(1.+Rd_,-3./4.)*Gy;
155
156 // Formule 24 page 610
157 betab_ = 0.5 + Ob_/O0_
158 + (3.-2.*Ob_/O0_) * sqrt(pow(17.2*O0_*h2,2.) + 1.);
[3314]159 if(lp_) cout<<"alphab = "<<alphab_<<" betab = "<<betab_
160 <<" (Baryon suppression/envelope shift)"<<endl;
[3115]161
162 // Formule 31 page 612
163 alphag_ = 1.
164 - 0.328*log(431.*O0_*h2)*Ob_/O0_
165 + 0.38*log(22.3*O0_*h2)*pow(Ob_/O0_,2.);
[3314]166 if(lp_) cout<<"alphag = "<<alphag_<<" (gamma suppression in approximate TF)"<<endl;
[3115]167
[3314]168 // The approximate value of the sound horizon, formule 26 page 611
169 sfit_ = 44.5*log(9.83/(O0_*h2)) / sqrt(1.+10.*pow(Ob_*h2,3./4.)); // Mpc
170 if(lp_) cout<<"sfit="<<sfit_<<" Mpc (fit to sound horizon)"<<endl;
[3115]171
[3314]172 // La positoin du premier pic acoustique, formule 25 page 611
173 kpeak_ = 5*M_PI/(2.*sfit_) * (1.+0.217*O0_*h2); // 1/Mpc
174 if(lp_) cout<<"kpeak="<<kpeak_<<" Mpc^-1 (fit to wavenumber of first peak)"<<endl;
175
[3115]176 return;
177}
178
[3378]179bool TransfertEisenstein::SetParTo(double h100,double OmegaCDM0,double OmegaBaryon0,double tcmb,bool nobaryon)
180// Changement des valeurs des parametres (suivi de re-init eventuel)
[3115]181{
[3378]182 bool haschanged = false;
183
184 if(h100>0.) {h100_ = h100; haschanged = true;}
185 if(OmegaCDM0>0.) {Oc_ = OmegaCDM0; haschanged = true;}
186 if(OmegaBaryon0>0.) {Ob_ = OmegaBaryon0; haschanged = true;}
187 if(tcmb>0.) {tcmb_ = tcmb; haschanged = true;}
188 if(nobaryon!=nobaryon_) {nobaryon_ = nobaryon; haschanged = true;}
189
190 // et on recalcule les initialisations
191 if(haschanged) Init_();
192
193 return haschanged;
[3115]194}
195
196void TransfertEisenstein::SetNoOscEnv(unsigned short nooscenv)
[3314]197// To obtain an approximate form of the non-oscillatory part of the transfert function
198// nooscenv = 0 : use the baryon oscillatory part of transfert function (full tf)
[3115]199// nooscenv = 1 : use approx. paragraph 3.3 p610 (middle of right column)
[3314]200// Replace j0(k*stilde) -> [1+(k*stilde)^4]^(-1/4)
[3115]201// nooscenv = 2 : use formulae 29+30+31 page 612
[3314]202// The value of an approximate transfer function that captures
203// the non-oscillatory part of a partial baryon transfer function.
204// In other words, the baryon oscillations are left out,
205// but the suppression of power below the sound horizon is included.
[3115]206{
[3314]207 if(nooscenv!=1 && nooscenv!=2) nooscenv = 0;
208 nooscenv_ = nooscenv;
[3115]209}
210
[3348]211void TransfertEisenstein::SetReturnPart(ReturnPart retpart)
[3314]212// To return only baryon or CDM part part of transfert function
[3348]213// retpart = ALL: return full transfert function
214// = CDM : return only CDM part of transfert function
215// = BARYON : return only Baryon part of transfert function
[3314]216// WARNING: only relevant for nobaryon_=false AND nooscenv!=2
217{
218 retpart_ = retpart;
219}
220
[3378]221void TransfertEisenstein::SetPrintLevel(int lp)
222{
223 lp_ = lp;
224}
225
226
[3115]227double TransfertEisenstein::T0tild(double k,double alphac,double betac)
228{
229 // Formule 10 p 608
[3378]230 //double q = k*th2p7_*th2p7_/(O0_*h100_*h100_);
[3115]231 double q = k/(13.41*keq_);
232 // Formule 20 p 610
233 double C = (14.2/alphac) + 386./(1.+69.9*pow(q,1.08));
234 // Formule 19 p 610
235 double x = log(M_E+1.8*betac*q);
236 return x / (x + C*q*q);
237}
238
239double TransfertEisenstein::operator() (double k)
240{
241
242 // --- Pour zero baryon
243 // OU Pour function lissee sans oscillation baryon
244 if(nobaryon_ || nooscenv_ == 2) {
[3378]245 double gamma = O0_*h100_;
[3314]246 // Calcul de Gamma_eff, formule 30 page 612 (pour fct lissee)
[3115]247 if( nobaryon_==false && nooscenv_ == 2 )
[3378]248 gamma = O0_*h100_*(alphag_ + (1.-alphag_)/(1.+pow(0.43*k*sfit_,4.))); // Gamma_eff
[3314]249 // Formule 28 page 612 : qui est est equivalent a:
[3378]250 // q = k / h100_ * th2p7_*th2p7_ / gamma;
[3314]251 // qui est est equivalent a:
252 // q = k / (13.41 * keq) pour Ob=0
253 // q = k / (13.41 * keq) * (O0*h/Gamma) pour le spectre lisse
254 // Les resultats sont legerement differents a cause des valeurs approx.
255 // des constantes numeriques: on prend comme W.Hu (tf_fit.c)
[3378]256 //double q = k / h100_ * th2p7_*th2p7_ / gamma; // Mpc^-1
257 double q = k/(13.41*keq_) * (O0_*h100_/gamma); // Mpc^-1
[3115]258 // Formules 29 page 612
259 double l0 = log(2.*M_E + 1.8*q);
260 double c0 = 14.2 + 731./(1.+62.5*q);
261 return l0 / (l0 + c0*q*q);
262 }
263
[3314]264 // --- Pour CDM + Baryons
[3115]265 // --- CDM
266 double f = 1. / (1. + pow(k*s_/5.4,4.));
267 double Tc = f*T0tild(k,1.,betac_) + (1.-f)*T0tild(k,alphac_,betac_);
[3348]268 if(retpart_ == CDM) return Tc;
[3115]269
270 // --- Baryons
271 // Formule 22 page 610
[3314]272 double stilde, ksbnode = k*s_/bnode_;
273 if(ksbnode<0.001) stilde =s_ * ksbnode;
274 else stilde = s_ / pow(1. + pow(1./ksbnode,3.), 1./3.);
[3115]275 // Formule 21 page 610
276 double j0kst = 0.;
[3314]277 if(nooscenv_ == 1) {
278 j0kst = pow(1.+pow(k*stilde,4.) , -1./4.); //lissee sans oscillation baryon
279 } else {
280 double x = k*stilde;
[3115]281 if(x<0.01) j0kst = 1. - x*x/6.*(1.-x*x/20.);
282 else j0kst = sin(x)/x;
[3314]283 //cout<<"DEBUG: k="<<k<<" stilde="<<stilde<<" x="<<x<<" j0kst="<<j0kst<<endl;
[3115]284 }
285 double Tb = T0tild(k,1.,1.) / (1. + pow(k*s_/5.2,2.));
[3314]286 Tb += alphab_/(1.+pow(betab_/(k*s_),3.)) * exp(-pow(k/ksilk_,1.4));
[3115]287 Tb *= j0kst;
[3348]288 if(retpart_ == BARYON) return Tb;
[3115]289
290 // --- Total
291 double T = (Ob_/O0_)*Tb + (Oc_/O0_)*Tc;
292
293 return T;
294}
295
296double TransfertEisenstein::KPeak(void)
297// Position du premier pic acoustic
298{
299 if(nobaryon_) return -1.;
300 return kpeak_;
301}
302
303
304///////////////////////////////////////////////////////////
[3318]305//******************* TransfertTabulate *****************//
306///////////////////////////////////////////////////////////
307
308TransfertTabulate::TransfertTabulate(double h100,double OmegaCDM0,double OmegaBaryon0)
[3378]309: Oc_(OmegaCDM0) , Ob_(OmegaBaryon0) , h100_(h100) , kmin_(1.) , kmax_(-1.)
[3318]310, interptyp_(0)
311{
312}
313
314TransfertTabulate::TransfertTabulate(TransfertTabulate& tf)
[3378]315: Oc_(tf.Oc_) , Ob_(tf.Ob_) , h100_(tf.h100_) , kmin_(tf.kmin_) , kmax_(tf.kmax_)
[3318]316, interptyp_(tf.interptyp_) , k_(tf.k_) , tf_(tf.tf_)
317{
318}
319
320TransfertTabulate::~TransfertTabulate(void)
321{
322}
323
324void TransfertTabulate::SetInterpTyp(int typ)
325// see comment in InterpTab
326{
327 if(typ<0) typ=0; else if(typ>2) typ=2;
328 interptyp_ = typ;
329}
330
331double TransfertTabulate::operator() (double k)
332{
333 return InterpTab(k,k_,tf_,interptyp_);
334}
335
336int TransfertTabulate::ReadCMBFast(string filename)
337{
338 FILE *file = fopen(filename.c_str(),"r");
339 if(file==NULL) return -1;
340
341 const int lenline = 512;
342 char *line = new char[lenline];
343
344 int nread = 0;
345 double tmax = -1.;
346 while ( fgets(line,lenline,file) != NULL ) {
347 double k,tc,tb,tf;
348 sscanf(line,"%lf %lf %lf",&k,&tc,&tb);
[3378]349 k *= h100_; // convert h^-1 Mpc -> Mpc
[3318]350 tf = (Oc_*tc+Ob_*tb)/(Oc_+Ob_);
351 if(tf>tmax) tmax = tf;
352 k_.push_back(k);
353 tf_.push_back(tf);
354 nread++;
355 }
356
357 cout<<"TransfertTabulate::ReadCMBFast: nread="<<nread<<" tf_max="<<tmax<<endl;
358 delete [] line;
359 if(nread==0) return nread;
360
361 for(unsigned int i=0;i<tf_.size();i++) tf_[i] /= tmax;
362
363 return nread;
364}
365
366///////////////////////////////////////////////////////////
[3115]367//********************* GrowthFactor ********************//
368///////////////////////////////////////////////////////////
369
370// From Eisenstein & Hu ApJ 496:605-614 1998 April 1
[3193]371// Pour avoir D(z) = 1/(1+z) faire: OmegaMatter0=1 OmegaLambda0=0
[3115]372GrowthFactor::GrowthFactor(double OmegaMatter0,double OmegaLambda0)
373 : O0_(OmegaMatter0) , Ol_(OmegaLambda0) , Ok_(1.-OmegaMatter0-OmegaLambda0)
374{
375 if(OmegaMatter0==0.) {
376 cout<<"GrowthFactor::GrowthFactor: Error bad OmegaMatter0 value : "<<OmegaMatter0<<endl;
377 throw ParmError("GrowthFactor::GrowthFactor: Error badOmegaMatter0 value");
378 }
379 norm_ = 1.; // puisque (*this)(0.) a besoin de norm_
380 norm_ = (*this)(0.);
381 cout<<"GrowthFactor::GrowthFactor : norm="<<norm_<<endl;
382}
383
384GrowthFactor::GrowthFactor(GrowthFactor& d1)
385 : O0_(d1.O0_) , Ol_(d1.Ol_) , Ok_(d1.Ok_) , norm_(d1.norm_)
386{
387}
388
389GrowthFactor::~GrowthFactor(void)
390{
391}
392
393double GrowthFactor::operator() (double z)
394// see Formulae A4 + A5 + A6 page 614
395{
396 z += 1.;
397 double z2 = z*z, z3 = z2*z;
398 double den = Ol_ + Ok_*z2 + O0_*z3;
399 double o0z = O0_ *z3 / den;
400 double olz = Ol_ / den;
401
402 // 4./7. = 0.571429
403 double D1z = pow(o0z,0.571429) - olz + (1.+o0z/2.)*(1.+olz/70.);
404 D1z = 2.5*o0z / z / D1z;
405
406 return D1z / norm_;
407}
408
[3378]409bool GrowthFactor::SetParTo(double OmegaMatter0,double OmegaLambda0)
410{
411 bool haschanged = false;
[3115]412
[3378]413 if(OmegaMatter0>0.) {O0_ = OmegaMatter0; haschanged = true;}
414 if(fabs(OmegaLambda0+12345.)>1e-6) {Ol_ = OmegaLambda0; haschanged = true;}
415
416 // et on recalcule les initialisations
417 if(haschanged) {
418 Ok_ = 1. - O0_ - Ol_;
419 norm_ = 1.; // puisque (*this)(0.) a besoin de norm_
420 norm_ = (*this)(0.);
421 }
422
423 return haschanged;
424}
425
426
[3115]427///////////////////////////////////////////////////////////
428//************** PkSpectrum0 et PkSpectrumZ *************//
429///////////////////////////////////////////////////////////
430
431PkSpectrum0::PkSpectrum0(InitialSpectrum& pkinf,TransfertEisenstein& tf)
432 : pkinf_(pkinf) , tf_(tf)
433{
434}
435
436PkSpectrum0::PkSpectrum0(PkSpectrum0& pk0)
437 : pkinf_(pk0.pkinf_) , tf_(pk0.tf_)
438{
439}
440
441PkSpectrum0::~PkSpectrum0(void)
442{
443}
444
445double PkSpectrum0::operator() (double k)
446{
447 double tf = tf_(k);
448 double pkinf = pkinf_(k);
449 return pkinf *tf*tf;
450}
451
452//------------------------------------
453PkSpectrumZ::PkSpectrumZ(PkSpectrum0& pk0,GrowthFactor& d1,double zref)
[3348]454 : pk0_(pk0) , d1_(d1) , zref_(zref) , scale_(1.) , typspec_(PK)
[3115]455 , zold_(-1.) , d1old_(1.)
456{
457}
458
459PkSpectrumZ::PkSpectrumZ(PkSpectrumZ& pkz)
[3348]460 : pk0_(pkz.pk0_) , d1_(pkz.d1_) , zref_(pkz.zref_) , scale_(pkz.scale_) , typspec_(PK)
[3115]461 , zold_(pkz.zold_) , d1old_(pkz.d1old_)
462{
463}
464
465PkSpectrumZ::~PkSpectrumZ(void)
466{
467}
468
[3348]469void PkSpectrumZ::SetTypSpec(ReturnSpectrum typspec)
470// typsec = PK : compute Pk(k)
471// = DELTA : compute Delta^2(k) = k^3*Pk(k)/2Pi^2
[3115]472{
473 typspec_ = typspec;
474}
475
476double PkSpectrumZ::operator() (double k)
477{
478 return (*this)(k,zref_);
479}
480
481double PkSpectrumZ::operator() (double k,double z)
482{
483 double d1;
484 if(z == zold_) d1 = d1old_;
485 else {d1 = d1old_ = d1_(z); zold_ = z;}
486
487 double v = pk0_(k) * d1*d1;
[3348]488 if(typspec_==DELTA) v *= k*k*k/(2.*M_PI*M_PI);
[3115]489
490 return scale_ * v;
491}
492
493
494
495///////////////////////////////////////////////////////////
496//******************* VarianceSpectrum ******************//
497///////////////////////////////////////////////////////////
498
[3348]499VarianceSpectrum::VarianceSpectrum(GenericFunc& pk,double R,TypeFilter typfilter)
500 : pk_(pk)
[3115]501{
[3348]502 SetRadius(R);
[3115]503 SetFilter(typfilter);
504}
505
506VarianceSpectrum::VarianceSpectrum(VarianceSpectrum& vpk)
507 : pk_(vpk.pk_) , R_(vpk.R_)
508{
509 SetFilter(vpk.typfilter_);
510}
511
512VarianceSpectrum::~VarianceSpectrum(void)
513{
514}
515
[3348]516void VarianceSpectrum::SetRadius(double R)
517// R = taille du filter top-hat ou gaussien
518{
519 if(R<=0.) {
520 cout<<"VarianceSpectrum::SetRadius: Error R<=0"<<endl;
521 throw ParmError("VarianceSpectrum::SetRadius: Error R<=0");
522 }
523 R_ = R;
524}
525
[3115]526//------------------------------------
[3348]527void VarianceSpectrum::SetFilter(TypeFilter typfilter)
528// typfilter = TOPHAT : spherical 3D top-hat
529// = GAUSSIAN : spherical 3D gaussian
530// = NOFILTER : no filter juste integrate spectrum)
531// Remarque:
532// la meilleure approximation du filtre top-hat (R) est un filtre gaussien avec (Rg=R/sqrt(5))
[3115]533{
534 typfilter_ = typfilter;
535}
536
537void VarianceSpectrum::SetInteg(double dperc,double dlogkinc,double dlogkmax,unsigned short glorder)
538// ATTENTION: on n'integre pas f(k)*dk mais k*f(k)*d(log10(k))
[3196]539// see argument details in function IntegrateFuncLog (geneutils.cc)
[3115]540{
541 dperc_ = dperc; if(dperc_<=0.) dperc_ = 0.1;
542 dlogkinc_ = dlogkinc;
543 dlogkmax_ = dlogkmax;
544 glorder_ = glorder;
545}
546
547
548//------------------------------------
549double VarianceSpectrum::Filter2(double x)
550// ATTENTION: c'est le filtre au carre qui est renvoye
551{
552 // Just integrate the spectrum without filtering
[3348]553 if(typfilter_ == NOFILTER) return 1.;
[3115]554
555 double x2 = x*x;
556 // Filtre gaussien G(x) = exp(-x^2/2)
557 // remarque G(x)^2 = exp(-x^2)
558 // on prend le DL de G(x)^2 pour x->0 a l'ordre O(x^6)
559 // DL(x) = 1-x^2*(1-x^2/2)
560 // pour x<0.01 |DL(x)-G(X)^2|<2.0e-13
[3348]561 if(typfilter_ == GAUSSIAN)
[3115]562 if(x<0.01) return 1.-x2*(1.-x2/2.); else return exp(-x2);
563
564 // Filtre top-hat T(x) = 3*(sin(x)-x*cos(x))/x^3
565 // --- Gestion de la pseudo-divergence pour x->0
566 // on prend le DL de T(x)^2 pour x->0 a l'ordre O(x^7)
567 // DL(x) = 1-x^2/5*(1-3*x^2/35*(1-4*x^2/81))
568 // pour x<0.1 |DL(x)-T(X)^2|<2.5e-13
569 double f2=0.;
570 if(x<0.1) {
571 f2 = 1.-x2/5.*(1.-3.*x2/35.*(1.-4.*x2/81.));
572 } else {
573 f2 = 3.*(sin(x)-x*cos(x))/(x2*x);
574 f2 *= f2;
575 }
576 return f2;
577
578}
579
[3348]580double VarianceSpectrum::Variance(double kmin,double kmax)
[3115]581// Compute variance of spectrum pk_ by integration
582// Input:
583// kmin,kmax = bornes en k de l'integrale pour calculer la variance
584// Return:
585// valeur de la variance (sigma^2)
586// Remarque:
587// la variance renvoyee est la variance de la masse
588{
[3348]589 if(kmin<=0 || kmax<=0. || kmin>=kmax) {
590 cout<<"VarianceSpectrum::Variance: Error kmin<=0 or kmax<=0 or kmin>=kmax"<<endl;
591 throw ParmError("VarianceSpectrum::Variance: Error kmin<=0 or kmax<=0 or kmin>=kmax");
[3115]592 }
593
594 double lkmin = log10(kmin), lkmax = log10(kmax);
595
596 double var = IntegrateFuncLog(*this,lkmin,lkmax,dperc_,dlogkinc_,dlogkmax_,glorder_);
597
598 return var;
599}
600
601//------------------------------------
[3348]602double VarianceSpectrum::FindMaximum(double kmin,double kmax,double eps)
[3115]603// Retourne le maximum de la fonction a integrer
604// La recherche a lieu entre [kmin,kmax] par pas logarithmiques
605// Input:
606// kmin,kmax : intervalle de recherche
607// eps : precision requise sur les valeurs
608// Return:
609// position (en k) du maximum
610{
[3348]611 if(kmin<=0 || kmax<=0. || kmin>=kmax) {
612 cout<<"VarianceSpectrum::FindMaximum: Error kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0"<<endl;
613 throw ParmError("VarianceSpectrum::FindMaximum: Error kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0");
[3115]614 }
615
616 int n = 10; // toujours >2
617 double lkmin = log10(kmin), lkmax = log10(kmax), dlk = (lkmax-lkmin)/n;
618
619 double lkfind=lkmin, pkfind=-1.;
620 while(1) {
621 for(int i=0; i<=n; i++) {
622 double lk = lkmin + i*dlk;
623 double v = (*this)(pow(10.,lk));
624 if(v<pkfind) continue;
625 pkfind = v; lkfind = lk;
626 }
627 //cout<<"VarianceSpectrum::FindMaximum: lkfind="<<lkfind<<" pkfind="<<pkfind
628 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
629 // --- Convergence si l'encadrement de "kfind" est tel que "dk/kfind<eps"
630 // On a dk = 10^(lkfind+dlk) - 10^(lkfind-dlk) = kfind * (10^(dlk) - 10^(-dlk))
631 if( pow(10.,dlk)-pow(10.,-dlk) < eps ) break;
632 if(lkfind-dlk>lkmin) lkmin = lkfind-dlk;
633 if(lkfind+dlk<lkmax) lkmax = lkfind+dlk;
634 dlk = (lkmax-lkmin)/n;
635 }
636
637 return pow(10.,lkfind);
638}
639
[3348]640int VarianceSpectrum::FindLimits(double high,double &kmin,double &kmax,double eps)
[3115]641// Retourne "[kmin,kmax]" tel que la fonction a integrer soit "f(k) <= high"
642// La recherche a lieu entre [kmin,kmax] par pas logarithmiques
643// Input:
644// kmin,kmax : intervalle de recherche
645// eps : precision requise sur les valeurs kmin et kmax
646// Output:
647// kmin,kmax telles que "f(k) <= high"
648// Return:
649// rc = 0 si OK
650// rc |= 1 "f(kmin) >= high" (bit0 =1)
651// rc |= 2 "f(kmax) >= high" (bit1 =1)
652// rc |= 4 "f(k) < high pour tout k" (bit2 =1)
653{
[3348]654 if(kmin<=0 || kmax<=0. || kmin>=kmax || eps<=0.) {
655 cout<<"VarianceSpectrum::FindLimits: Error kmin<=0 or kmax<=0 or kmin>=kmax or eps<=0"<<endl;
656 throw ParmError("VarianceSpectrum::FindLimits: Error kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0");
[3115]657 }
658
659 int n = 10; // toujours >2
660
661 int rc = 0;
662 double lkmin,lkmax,dlk,lkfind;
663
664 // --- Find kmin
665 lkmin=log10(kmin); lkmax=log10(kmax); dlk=(lkmax-lkmin)/n;
666 while(1) {
667 lkfind = lkmin;
668 for(int i=0;i<=n;i++) {
[3314]669 if( (*this)(pow(10,lkfind)) >= high ) break;
[3115]670 lkfind = lkmin + i*dlk;
671 }
672 //cout<<"VarianceSpectrum::FindLimits[kmin]: lkfind="<<lkfind
673 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
674 if(fabs(lkfind-lkmax)<dlk/2.) {rc |= 4; return rc;} // protect against f(k)<high for all k
675 if( pow(10.,dlk)-pow(10.,-dlk) < eps ) break;
676 if(lkfind-dlk>lkmin) lkmin = lkfind-dlk;
677 if(lkfind+dlk<lkmax) lkmax = lkfind+dlk;
678 dlk = (lkmax-lkmin)/n;
679 }
680 if(lkfind-lkmin<dlk/2.) rc |= 1; // f(kmin) >= high
681 else kmin = pow(10.,lkmin);
682 //cout<<"rc="<<rc<<" lkmin="<<lkmin<<" pk="<<(*this)(pow(10.,lkmin))<<endl;
683
684 // --- Find kmax
685 lkmin=log10(kmin); lkmax=log10(kmax); dlk=(lkmax-lkmin)/n;
686 while(1) {
687 lkfind=lkmax;
688 for(int i=0;i<=n;i++) {
[3314]689 if( (*this)(pow(10,lkfind)) >= high ) break;
[3115]690 lkfind -= dlk;
691 lkfind = lkmax - i*dlk;
692 }
693 //cout<<"VarianceSpectrum::FindLimits[kmax]: lkfind="<<lkfind
694 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
695 if( pow(10.,dlk)-pow(10.,-dlk) < eps ) break;
696 if(lkfind-dlk>lkmin) lkmin = lkfind-dlk;
697 if(lkfind+dlk<lkmax) lkmax = lkfind+dlk;
698 dlk = (lkmax-lkmin)/n;
699 }
700 if(lkmax-lkfind<dlk/2.) rc |= 2; // f(kmax) >= high
701 else kmax = pow(10.,lkmax);
702 //cout<<"rc="<<rc<<" lkmax="<<lkmax<<" pk="<<(*this)(pow(10.,lkmax))<<endl;
703
704 return rc;
705}
[3325]706
707} // Fin namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.