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

Last change on this file since 3345 was 3329, checked in by cmv, 18 years ago

possibilite de ne pas faire poisson sur Ngal cmv 27/09/2007

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