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

Last change on this file since 3797 was 3768, checked in by cmv, 15 years ago
  • refonte du code pour creer uniquement des conditions initiales
  • introduction du tirage des vitesse LOS pour les redshift-distortion

cmv 03/05/2010

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