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

Last change on this file since 3377 was 3348, checked in by cmv, 18 years ago
  • definition des options par enum
  • mise en variable privee du rayon R du filtre de VarianceSpectrum Elle disparait des arguments des methodes: Variance FindMaximum FindLimits

cmv 11/10/2007

File size: 20.3 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)
[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_)
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
[3348]194void TransfertEisenstein::SetReturnPart(ReturnPart retpart)
[3314]195// To return only baryon or CDM part part of transfert function
[3348]196// retpart = ALL: return full transfert function
197// = CDM : return only CDM part of transfert function
198// = BARYON : return only Baryon part of transfert function
[3314]199// WARNING: only relevant for nobaryon_=false AND nooscenv!=2
200{
201 retpart_ = retpart;
202}
203
[3115]204double TransfertEisenstein::T0tild(double k,double alphac,double betac)
205{
206 // Formule 10 p 608
207 //double q = k*th2p7_*th2p7_/(O0_*h_*h_);
208 double q = k/(13.41*keq_);
209 // Formule 20 p 610
210 double C = (14.2/alphac) + 386./(1.+69.9*pow(q,1.08));
211 // Formule 19 p 610
212 double x = log(M_E+1.8*betac*q);
213 return x / (x + C*q*q);
214}
215
216double TransfertEisenstein::operator() (double k)
217{
218
219 // --- Pour zero baryon
220 // OU Pour function lissee sans oscillation baryon
221 if(nobaryon_ || nooscenv_ == 2) {
222 double gamma = O0_*h_;
[3314]223 // Calcul de Gamma_eff, formule 30 page 612 (pour fct lissee)
[3115]224 if( nobaryon_==false && nooscenv_ == 2 )
[3314]225 gamma = O0_*h_*(alphag_ + (1.-alphag_)/(1.+pow(0.43*k*sfit_,4.))); // Gamma_eff
226 // Formule 28 page 612 : qui est est equivalent a:
227 // q = k / h_ * th2p7_*th2p7_ / gamma;
228 // qui est est equivalent a:
229 // q = k / (13.41 * keq) pour Ob=0
230 // q = k / (13.41 * keq) * (O0*h/Gamma) pour le spectre lisse
231 // Les resultats sont legerement differents a cause des valeurs approx.
232 // des constantes numeriques: on prend comme W.Hu (tf_fit.c)
233 //double q = k / h_ * th2p7_*th2p7_ / gamma; // Mpc^-1
234 double q = k/(13.41*keq_) * (O0_*h_/gamma); // Mpc^-1
[3115]235 // Formules 29 page 612
236 double l0 = log(2.*M_E + 1.8*q);
237 double c0 = 14.2 + 731./(1.+62.5*q);
238 return l0 / (l0 + c0*q*q);
239 }
240
[3314]241 // --- Pour CDM + Baryons
[3115]242 // --- CDM
243 double f = 1. / (1. + pow(k*s_/5.4,4.));
244 double Tc = f*T0tild(k,1.,betac_) + (1.-f)*T0tild(k,alphac_,betac_);
[3348]245 if(retpart_ == CDM) return Tc;
[3115]246
247 // --- Baryons
248 // Formule 22 page 610
[3314]249 double stilde, ksbnode = k*s_/bnode_;
250 if(ksbnode<0.001) stilde =s_ * ksbnode;
251 else stilde = s_ / pow(1. + pow(1./ksbnode,3.), 1./3.);
[3115]252 // Formule 21 page 610
253 double j0kst = 0.;
[3314]254 if(nooscenv_ == 1) {
255 j0kst = pow(1.+pow(k*stilde,4.) , -1./4.); //lissee sans oscillation baryon
256 } else {
257 double x = k*stilde;
[3115]258 if(x<0.01) j0kst = 1. - x*x/6.*(1.-x*x/20.);
259 else j0kst = sin(x)/x;
[3314]260 //cout<<"DEBUG: k="<<k<<" stilde="<<stilde<<" x="<<x<<" j0kst="<<j0kst<<endl;
[3115]261 }
262 double Tb = T0tild(k,1.,1.) / (1. + pow(k*s_/5.2,2.));
[3314]263 Tb += alphab_/(1.+pow(betab_/(k*s_),3.)) * exp(-pow(k/ksilk_,1.4));
[3115]264 Tb *= j0kst;
[3348]265 if(retpart_ == BARYON) return Tb;
[3115]266
267 // --- Total
268 double T = (Ob_/O0_)*Tb + (Oc_/O0_)*Tc;
269
270 return T;
271}
272
273double TransfertEisenstein::KPeak(void)
274// Position du premier pic acoustic
275{
276 if(nobaryon_) return -1.;
277 return kpeak_;
278}
279
280
281///////////////////////////////////////////////////////////
[3318]282//******************* TransfertTabulate *****************//
283///////////////////////////////////////////////////////////
284
285TransfertTabulate::TransfertTabulate(double h100,double OmegaCDM0,double OmegaBaryon0)
286: Oc_(OmegaCDM0) , Ob_(OmegaBaryon0) , h_(h100) , kmin_(1.) , kmax_(-1.)
287, interptyp_(0)
288{
289}
290
291TransfertTabulate::TransfertTabulate(TransfertTabulate& tf)
292: Oc_(tf.Oc_) , Ob_(tf.Ob_) , h_(tf.h_) , kmin_(tf.kmin_) , kmax_(tf.kmax_)
293, interptyp_(tf.interptyp_) , k_(tf.k_) , tf_(tf.tf_)
294{
295}
296
297TransfertTabulate::~TransfertTabulate(void)
298{
299}
300
301void TransfertTabulate::SetInterpTyp(int typ)
302// see comment in InterpTab
303{
304 if(typ<0) typ=0; else if(typ>2) typ=2;
305 interptyp_ = typ;
306}
307
308double TransfertTabulate::operator() (double k)
309{
310 return InterpTab(k,k_,tf_,interptyp_);
311}
312
313int TransfertTabulate::ReadCMBFast(string filename)
314{
315 FILE *file = fopen(filename.c_str(),"r");
316 if(file==NULL) return -1;
317
318 const int lenline = 512;
319 char *line = new char[lenline];
320
321 int nread = 0;
322 double tmax = -1.;
323 while ( fgets(line,lenline,file) != NULL ) {
324 double k,tc,tb,tf;
325 sscanf(line,"%lf %lf %lf",&k,&tc,&tb);
326 k *= h_; // convert h^-1 Mpc -> Mpc
327 tf = (Oc_*tc+Ob_*tb)/(Oc_+Ob_);
328 if(tf>tmax) tmax = tf;
329 k_.push_back(k);
330 tf_.push_back(tf);
331 nread++;
332 }
333
334 cout<<"TransfertTabulate::ReadCMBFast: nread="<<nread<<" tf_max="<<tmax<<endl;
335 delete [] line;
336 if(nread==0) return nread;
337
338 for(unsigned int i=0;i<tf_.size();i++) tf_[i] /= tmax;
339
340 return nread;
341}
342
343///////////////////////////////////////////////////////////
[3115]344//********************* GrowthFactor ********************//
345///////////////////////////////////////////////////////////
346
347// From Eisenstein & Hu ApJ 496:605-614 1998 April 1
[3193]348// Pour avoir D(z) = 1/(1+z) faire: OmegaMatter0=1 OmegaLambda0=0
[3115]349GrowthFactor::GrowthFactor(double OmegaMatter0,double OmegaLambda0)
350 : O0_(OmegaMatter0) , Ol_(OmegaLambda0) , Ok_(1.-OmegaMatter0-OmegaLambda0)
351{
352 if(OmegaMatter0==0.) {
353 cout<<"GrowthFactor::GrowthFactor: Error bad OmegaMatter0 value : "<<OmegaMatter0<<endl;
354 throw ParmError("GrowthFactor::GrowthFactor: Error badOmegaMatter0 value");
355 }
356 norm_ = 1.; // puisque (*this)(0.) a besoin de norm_
357 norm_ = (*this)(0.);
358 cout<<"GrowthFactor::GrowthFactor : norm="<<norm_<<endl;
359}
360
361GrowthFactor::GrowthFactor(GrowthFactor& d1)
362 : O0_(d1.O0_) , Ol_(d1.Ol_) , Ok_(d1.Ok_) , norm_(d1.norm_)
363{
364}
365
366GrowthFactor::~GrowthFactor(void)
367{
368}
369
370double GrowthFactor::operator() (double z)
371// see Formulae A4 + A5 + A6 page 614
372{
373 z += 1.;
374 double z2 = z*z, z3 = z2*z;
375 double den = Ol_ + Ok_*z2 + O0_*z3;
376 double o0z = O0_ *z3 / den;
377 double olz = Ol_ / den;
378
379 // 4./7. = 0.571429
380 double D1z = pow(o0z,0.571429) - olz + (1.+o0z/2.)*(1.+olz/70.);
381 D1z = 2.5*o0z / z / D1z;
382
383 return D1z / norm_;
384}
385
386
387///////////////////////////////////////////////////////////
388//************** PkSpectrum0 et PkSpectrumZ *************//
389///////////////////////////////////////////////////////////
390
391PkSpectrum0::PkSpectrum0(InitialSpectrum& pkinf,TransfertEisenstein& tf)
392 : pkinf_(pkinf) , tf_(tf)
393{
394}
395
396PkSpectrum0::PkSpectrum0(PkSpectrum0& pk0)
397 : pkinf_(pk0.pkinf_) , tf_(pk0.tf_)
398{
399}
400
401PkSpectrum0::~PkSpectrum0(void)
402{
403}
404
405double PkSpectrum0::operator() (double k)
406{
407 double tf = tf_(k);
408 double pkinf = pkinf_(k);
409 return pkinf *tf*tf;
410}
411
412//------------------------------------
413PkSpectrumZ::PkSpectrumZ(PkSpectrum0& pk0,GrowthFactor& d1,double zref)
[3348]414 : pk0_(pk0) , d1_(d1) , zref_(zref) , scale_(1.) , typspec_(PK)
[3115]415 , zold_(-1.) , d1old_(1.)
416{
417}
418
419PkSpectrumZ::PkSpectrumZ(PkSpectrumZ& pkz)
[3348]420 : pk0_(pkz.pk0_) , d1_(pkz.d1_) , zref_(pkz.zref_) , scale_(pkz.scale_) , typspec_(PK)
[3115]421 , zold_(pkz.zold_) , d1old_(pkz.d1old_)
422{
423}
424
425PkSpectrumZ::~PkSpectrumZ(void)
426{
427}
428
[3348]429void PkSpectrumZ::SetTypSpec(ReturnSpectrum typspec)
430// typsec = PK : compute Pk(k)
431// = DELTA : compute Delta^2(k) = k^3*Pk(k)/2Pi^2
[3115]432{
433 typspec_ = typspec;
434}
435
436double PkSpectrumZ::operator() (double k)
437{
438 return (*this)(k,zref_);
439}
440
441double PkSpectrumZ::operator() (double k,double z)
442{
443 double d1;
444 if(z == zold_) d1 = d1old_;
445 else {d1 = d1old_ = d1_(z); zold_ = z;}
446
447 double v = pk0_(k) * d1*d1;
[3348]448 if(typspec_==DELTA) v *= k*k*k/(2.*M_PI*M_PI);
[3115]449
450 return scale_ * v;
451}
452
453
454
455///////////////////////////////////////////////////////////
456//******************* VarianceSpectrum ******************//
457///////////////////////////////////////////////////////////
458
[3348]459VarianceSpectrum::VarianceSpectrum(GenericFunc& pk,double R,TypeFilter typfilter)
460 : pk_(pk)
[3115]461{
[3348]462 SetRadius(R);
[3115]463 SetFilter(typfilter);
464}
465
466VarianceSpectrum::VarianceSpectrum(VarianceSpectrum& vpk)
467 : pk_(vpk.pk_) , R_(vpk.R_)
468{
469 SetFilter(vpk.typfilter_);
470}
471
472VarianceSpectrum::~VarianceSpectrum(void)
473{
474}
475
[3348]476void VarianceSpectrum::SetRadius(double R)
477// R = taille du filter top-hat ou gaussien
478{
479 if(R<=0.) {
480 cout<<"VarianceSpectrum::SetRadius: Error R<=0"<<endl;
481 throw ParmError("VarianceSpectrum::SetRadius: Error R<=0");
482 }
483 R_ = R;
484}
485
[3115]486//------------------------------------
[3348]487void VarianceSpectrum::SetFilter(TypeFilter typfilter)
488// typfilter = TOPHAT : spherical 3D top-hat
489// = GAUSSIAN : spherical 3D gaussian
490// = NOFILTER : no filter juste integrate spectrum)
491// Remarque:
492// la meilleure approximation du filtre top-hat (R) est un filtre gaussien avec (Rg=R/sqrt(5))
[3115]493{
494 typfilter_ = typfilter;
495}
496
497void VarianceSpectrum::SetInteg(double dperc,double dlogkinc,double dlogkmax,unsigned short glorder)
498// ATTENTION: on n'integre pas f(k)*dk mais k*f(k)*d(log10(k))
[3196]499// see argument details in function IntegrateFuncLog (geneutils.cc)
[3115]500{
501 dperc_ = dperc; if(dperc_<=0.) dperc_ = 0.1;
502 dlogkinc_ = dlogkinc;
503 dlogkmax_ = dlogkmax;
504 glorder_ = glorder;
505}
506
507
508//------------------------------------
509double VarianceSpectrum::Filter2(double x)
510// ATTENTION: c'est le filtre au carre qui est renvoye
511{
512 // Just integrate the spectrum without filtering
[3348]513 if(typfilter_ == NOFILTER) return 1.;
[3115]514
515 double x2 = x*x;
516 // Filtre gaussien G(x) = exp(-x^2/2)
517 // remarque G(x)^2 = exp(-x^2)
518 // on prend le DL de G(x)^2 pour x->0 a l'ordre O(x^6)
519 // DL(x) = 1-x^2*(1-x^2/2)
520 // pour x<0.01 |DL(x)-G(X)^2|<2.0e-13
[3348]521 if(typfilter_ == GAUSSIAN)
[3115]522 if(x<0.01) return 1.-x2*(1.-x2/2.); else return exp(-x2);
523
524 // Filtre top-hat T(x) = 3*(sin(x)-x*cos(x))/x^3
525 // --- Gestion de la pseudo-divergence pour x->0
526 // on prend le DL de T(x)^2 pour x->0 a l'ordre O(x^7)
527 // DL(x) = 1-x^2/5*(1-3*x^2/35*(1-4*x^2/81))
528 // pour x<0.1 |DL(x)-T(X)^2|<2.5e-13
529 double f2=0.;
530 if(x<0.1) {
531 f2 = 1.-x2/5.*(1.-3.*x2/35.*(1.-4.*x2/81.));
532 } else {
533 f2 = 3.*(sin(x)-x*cos(x))/(x2*x);
534 f2 *= f2;
535 }
536 return f2;
537
538}
539
[3348]540double VarianceSpectrum::Variance(double kmin,double kmax)
[3115]541// Compute variance of spectrum pk_ by integration
542// Input:
543// kmin,kmax = bornes en k de l'integrale pour calculer la variance
544// Return:
545// valeur de la variance (sigma^2)
546// Remarque:
547// la variance renvoyee est la variance de la masse
548{
[3348]549 if(kmin<=0 || kmax<=0. || kmin>=kmax) {
550 cout<<"VarianceSpectrum::Variance: Error kmin<=0 or kmax<=0 or kmin>=kmax"<<endl;
551 throw ParmError("VarianceSpectrum::Variance: Error kmin<=0 or kmax<=0 or kmin>=kmax");
[3115]552 }
553
554 double lkmin = log10(kmin), lkmax = log10(kmax);
555
556 double var = IntegrateFuncLog(*this,lkmin,lkmax,dperc_,dlogkinc_,dlogkmax_,glorder_);
557
558 return var;
559}
560
561//------------------------------------
[3348]562double VarianceSpectrum::FindMaximum(double kmin,double kmax,double eps)
[3115]563// Retourne le maximum de la fonction a integrer
564// La recherche a lieu entre [kmin,kmax] par pas logarithmiques
565// Input:
566// kmin,kmax : intervalle de recherche
567// eps : precision requise sur les valeurs
568// Return:
569// position (en k) du maximum
570{
[3348]571 if(kmin<=0 || kmax<=0. || kmin>=kmax) {
572 cout<<"VarianceSpectrum::FindMaximum: Error kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0"<<endl;
573 throw ParmError("VarianceSpectrum::FindMaximum: Error kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0");
[3115]574 }
575
576 int n = 10; // toujours >2
577 double lkmin = log10(kmin), lkmax = log10(kmax), dlk = (lkmax-lkmin)/n;
578
579 double lkfind=lkmin, pkfind=-1.;
580 while(1) {
581 for(int i=0; i<=n; i++) {
582 double lk = lkmin + i*dlk;
583 double v = (*this)(pow(10.,lk));
584 if(v<pkfind) continue;
585 pkfind = v; lkfind = lk;
586 }
587 //cout<<"VarianceSpectrum::FindMaximum: lkfind="<<lkfind<<" pkfind="<<pkfind
588 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
589 // --- Convergence si l'encadrement de "kfind" est tel que "dk/kfind<eps"
590 // On a dk = 10^(lkfind+dlk) - 10^(lkfind-dlk) = kfind * (10^(dlk) - 10^(-dlk))
591 if( pow(10.,dlk)-pow(10.,-dlk) < eps ) break;
592 if(lkfind-dlk>lkmin) lkmin = lkfind-dlk;
593 if(lkfind+dlk<lkmax) lkmax = lkfind+dlk;
594 dlk = (lkmax-lkmin)/n;
595 }
596
597 return pow(10.,lkfind);
598}
599
[3348]600int VarianceSpectrum::FindLimits(double high,double &kmin,double &kmax,double eps)
[3115]601// Retourne "[kmin,kmax]" tel que la fonction a integrer soit "f(k) <= high"
602// La recherche a lieu entre [kmin,kmax] par pas logarithmiques
603// Input:
604// kmin,kmax : intervalle de recherche
605// eps : precision requise sur les valeurs kmin et kmax
606// Output:
607// kmin,kmax telles que "f(k) <= high"
608// Return:
609// rc = 0 si OK
610// rc |= 1 "f(kmin) >= high" (bit0 =1)
611// rc |= 2 "f(kmax) >= high" (bit1 =1)
612// rc |= 4 "f(k) < high pour tout k" (bit2 =1)
613{
[3348]614 if(kmin<=0 || kmax<=0. || kmin>=kmax || eps<=0.) {
615 cout<<"VarianceSpectrum::FindLimits: Error kmin<=0 or kmax<=0 or kmin>=kmax or eps<=0"<<endl;
616 throw ParmError("VarianceSpectrum::FindLimits: Error kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0");
[3115]617 }
618
619 int n = 10; // toujours >2
620
621 int rc = 0;
622 double lkmin,lkmax,dlk,lkfind;
623
624 // --- Find kmin
625 lkmin=log10(kmin); lkmax=log10(kmax); dlk=(lkmax-lkmin)/n;
626 while(1) {
627 lkfind = lkmin;
628 for(int i=0;i<=n;i++) {
[3314]629 if( (*this)(pow(10,lkfind)) >= high ) break;
[3115]630 lkfind = lkmin + i*dlk;
631 }
632 //cout<<"VarianceSpectrum::FindLimits[kmin]: lkfind="<<lkfind
633 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
634 if(fabs(lkfind-lkmax)<dlk/2.) {rc |= 4; return rc;} // protect against f(k)<high for all k
635 if( pow(10.,dlk)-pow(10.,-dlk) < eps ) break;
636 if(lkfind-dlk>lkmin) lkmin = lkfind-dlk;
637 if(lkfind+dlk<lkmax) lkmax = lkfind+dlk;
638 dlk = (lkmax-lkmin)/n;
639 }
640 if(lkfind-lkmin<dlk/2.) rc |= 1; // f(kmin) >= high
641 else kmin = pow(10.,lkmin);
642 //cout<<"rc="<<rc<<" lkmin="<<lkmin<<" pk="<<(*this)(pow(10.,lkmin))<<endl;
643
644 // --- Find kmax
645 lkmin=log10(kmin); lkmax=log10(kmax); dlk=(lkmax-lkmin)/n;
646 while(1) {
647 lkfind=lkmax;
648 for(int i=0;i<=n;i++) {
[3314]649 if( (*this)(pow(10,lkfind)) >= high ) break;
[3115]650 lkfind -= dlk;
651 lkfind = lkmax - i*dlk;
652 }
653 //cout<<"VarianceSpectrum::FindLimits[kmax]: lkfind="<<lkfind
654 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
655 if( pow(10.,dlk)-pow(10.,-dlk) < eps ) break;
656 if(lkfind-dlk>lkmin) lkmin = lkfind-dlk;
657 if(lkfind+dlk<lkmax) lkmax = lkfind+dlk;
658 dlk = (lkmax-lkmin)/n;
659 }
660 if(lkmax-lkfind<dlk/2.) rc |= 2; // f(kmax) >= high
661 else kmax = pow(10.,lkmax);
662 //cout<<"rc="<<rc<<" lkmax="<<lkmax<<" pk="<<(*this)(pow(10.,lkmax))<<endl;
663
664 return rc;
665}
[3325]666
667} // Fin namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.