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

Last change on this file since 4070 was 3806, checked in by cmv, 15 years ago

suite de la mise au point pour lecture fichiers CAMB, cmv 24/07/2010

File size: 26.5 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//******************** InitialPowerLaw ******************//
19///////////////////////////////////////////////////////////
20
21InitialPowerLaw::InitialPowerLaw(double n,double a)
22 : n_(n), A_(a)
23{
24}
25
26InitialPowerLaw::InitialPowerLaw(InitialPowerLaw& pkinf)
27 : n_(pkinf.n_), A_(pkinf.A_)
28{
29}
30
31InitialPowerLaw::~InitialPowerLaw(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(void)
298: kmin_(1.) , kmax_(-1.) , interptyp_(0)
299{
300 k_.resize(0);
301 tf_.resize(0);
302}
303
304TransfertTabulate::TransfertTabulate(TransfertTabulate& tf)
305: kmin_(tf.kmin_) , kmax_(tf.kmax_) , 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,double h100,double OmegaCDM0,double OmegaBaryon0)
326{
327 FILE *file = fopen(filename.c_str(),"r");
328 if(file==NULL) return -1;
329 cout<<"TransfertTabulate::ReadCMBFast: fn="<<filename<<" h100="<<h100
330 <<" OmegaCDM0="<<OmegaCDM0<<" OmegaBaryon0="<<OmegaBaryon0<<endl;
331
332 const int lenline = 512;
333 char *line = new char[lenline];
334
335 k_.resize(0); tf_.resize(0);
336 double tmax = -1.;
337 while ( fgets(line,lenline,file) != NULL ) {
338 double k,tc,tb,tf;
339 sscanf(line,"%lf %lf %lf",&k,&tc,&tb);
340 k *= h100; // convert h Mpc^-1 -> Mpc^-1
341 tf = (OmegaCDM0*tc+OmegaBaryon0*tb)/(OmegaCDM0+OmegaBaryon0);
342 if(tf>tmax) tmax = tf;
343 k_.push_back(k);
344 tf_.push_back(tf);
345 }
346
347 cout<<"TransfertTabulate::ReadCMBFast: nread="<<tf_.size()<<" tf_max="<<tmax<<endl;
348 delete [] line;
349 if(tf_.size()==0) return (int)tf_.size();
350
351 for(unsigned int i=0;i<tf_.size();i++) tf_[i] /= tmax;
352
353 return (int)tf_.size();
354}
355
356int TransfertTabulate::ReadCAMB(string filename, double h100)
357{
358 FILE *file = fopen(filename.c_str(),"r");
359 if(file==NULL) return -1;
360 cout<<"TransfertTabulate::ReadCAMB: fn="<<filename<<" h100="<<h100<<endl;
361
362 const int lenline = 512;
363 char *line = new char[lenline];
364
365 k_.resize(0); tf_.resize(0);
366 double tmax = -1.;
367 while ( fgets(line,lenline,file) != NULL ) {
368 double k,tcdm,tbar,tph,trel,tnu,ttot, tf;
369 sscanf(line,"%lf %lf %lf %lf %lf %lf %lf",&k,&tcdm,&tbar,&tph,&trel,&tnu,&ttot);
370 k *= h100; // convert h Mpc^-1 -> Mpc^-1
371 tf = ttot;
372 if(tf>tmax) tmax = tf;
373 k_.push_back(k);
374 tf_.push_back(tf);
375 }
376
377 cout<<"TransfertTabulate::ReadCAMB nread="<<tf_.size()<<" tf_max="<<tmax<<endl;
378 delete [] line;
379 if(tf_.size()==0) return (int)tf_.size();
380
381 for(unsigned int i=0;i<tf_.size();i++) tf_[i] /= tmax;
382
383 return (int)tf_.size();
384}
385
386
387///////////////////////////////////////////////////////////
388//********************* GrowthFactor ********************//
389///////////////////////////////////////////////////////////
390double GrowthFactor::DsDz(double z, double)
391{
392 cout<<"GrowthFactor::DsDz_Error not implemented"<<endl;
393 throw AllocationError(" GrowthFactor::DsDz_Error not implemented");
394}
395
396///////////////////////////////////////////////////////////
397//********************* GrowthEisenstein ********************//
398///////////////////////////////////////////////////////////
399
400// From Eisenstein & Hu ApJ 496:605-614 1998 April 1
401// Pour avoir D(z) = 1/(1+z) faire: OmegaMatter0=1 OmegaLambda0=0
402GrowthEisenstein::GrowthEisenstein(double OmegaMatter0,double OmegaLambda0)
403 : O0_(OmegaMatter0) , Ol_(OmegaLambda0)
404{
405 if(OmegaMatter0==0.) {
406 cout<<"GrowthEisenstein::GrowthEisenstein_Error: bad OmegaMatter0 value : "<<OmegaMatter0<<endl;
407 throw ParmError("GrowthEisenstein::GrowthEisenstein_Error: bad OmegaMatter0 value");
408 }
409}
410
411GrowthEisenstein::GrowthEisenstein(GrowthEisenstein& d1)
412 : O0_(d1.O0_) , Ol_(d1.Ol_)
413{
414}
415
416GrowthEisenstein::~GrowthEisenstein(void)
417{
418}
419
420double GrowthEisenstein::operator() (double z)
421// see Formulae A4 + A5 + A6 page 614
422{
423 z += 1.;
424 double z2 = z*z, z3 = z2*z;
425
426 // Calcul de la normalisation (pour z=0 -> growth=1.)
427 double D1z0 = pow(O0_,4./7.) - Ol_ + (1.+O0_/2.)*(1.+Ol_/70.);
428 D1z0 = 2.5*O0_ / D1z0;
429
430 // Calcul du growthfactor pour z
431 double Ok = 1. - O0_ - Ol_;
432 double den = Ol_ + Ok*z2 + O0_*z3;
433 double o0z = O0_ *z3 / den;
434 double olz = Ol_ / den;
435
436 double D1z = pow(o0z,4./7.) - olz + (1.+o0z/2.)*(1.+olz/70.);
437 D1z = 2.5*o0z / z / D1z;
438
439 return D1z / D1z0;
440}
441
442double GrowthEisenstein::DsDz(double z,double dzinc)
443// y-y0 = a*(x-x0)^2 + b*(x-x0)
444// dy = a*dx^2 + b*dx avec dx = x-x0
445// dy'(dx) = 2*a*dx + b -> pour x=x0 on a dy'(0) = b
446{
447 if(z<0. || dzinc<=0.) {
448 cout<<"GrowthEisenstein::DsDz_Error: z<0 or dzinc<=0. !"<<endl;
449 throw ParmError("GrowthEisenstein::DsDz_Error: z<0 or dzinc<=0. !");
450 }
451
452 double z1, z2;
453 if(z>dzinc/2.) {
454 // cas ou z est suffisamment loin de zero
455 // resolution avec 2 points encadrant x0=z
456 z1 = z - dzinc; if(z1<0.) z1 = 0.;
457 z2 = z + dzinc;
458 } else {
459 // cas ou z est proche de zero
460 // resolution avec 2 points au dessus, x0=z1
461 z1 = z + dzinc;
462 z2 = z + 2.*dzinc;
463 }
464
465 double gz = (*this)(z);
466 double dgz1 = (*this)(z1) - gz;
467 double dgz2 = (*this)(z2) - gz;
468
469 z1 -= z;
470 z2 -= z;
471 return (dgz2*z1*z1 - dgz1*z2*z2)/(z1*z2*(z1-z2));
472}
473
474void GrowthEisenstein::SetParTo(double OmegaMatter0,double OmegaLambda0)
475{
476 if(OmegaMatter0>0.) O0_ = OmegaMatter0;
477 Ol_ = OmegaLambda0;
478}
479
480bool GrowthEisenstein::SetParTo(double OmegaMatter0)
481// idem precedent sans changer OmegaLambda0
482{
483 if(OmegaMatter0<=0.) return false;
484 O0_ = OmegaMatter0;
485 return true;
486}
487
488
489///////////////////////////////////////////////////////////
490//********************** PkSpectrum *********************//
491///////////////////////////////////////////////////////////
492
493PkSpectrum::PkSpectrum(void)
494 : zref_(0.) , scale_(1.) , typspec_(PK)
495{
496}
497
498PkSpectrum::PkSpectrum(PkSpectrum& pk)
499 : zref_(pk.zref_) , scale_(pk.scale_) , typspec_(pk.typspec_)
500{
501}
502
503
504///////////////////////////////////////////////////////////
505//********************** PkSpecCalc *********************//
506///////////////////////////////////////////////////////////
507
508PkSpecCalc::PkSpecCalc(InitialSpectrum& pkinf,TransfertFunction& tf,GrowthFactor& d1,double zref)
509 : pkinf_(pkinf) , tf_(tf) , d1_(d1)
510{
511 zref_ = zref;
512}
513
514PkSpecCalc::PkSpecCalc(PkSpecCalc& pkz)
515 : pkinf_(pkz.pkinf_) , tf_(pkz.tf_) , d1_(pkz.d1_)
516{
517}
518
519PkSpecCalc::~PkSpecCalc(void)
520{
521}
522
523double PkSpecCalc::operator() (double k)
524{
525 return (*this)(k,zref_);
526}
527
528double PkSpecCalc::operator() (double k,double z)
529{
530 double tf = tf_(k);
531 double pkinf = pkinf_(k);
532 double d1 = d1_(z);
533
534 double v = pkinf * (tf*tf) * (d1*d1);
535 if(typspec_ == DELTA) v *= k*k*k/(2.*M_PI*M_PI);
536
537 return scale_ * v;
538}
539
540///////////////////////////////////////////////////////////
541//********************** PkTabulate *********************//
542///////////////////////////////////////////////////////////
543
544PkTabulate::PkTabulate(void)
545 : kmin_(1.) , kmax_(-1.) , interptyp_(0), d1_(NULL)
546{
547 k_.resize(0);
548 pk_.resize(0);
549}
550
551PkTabulate::PkTabulate(PkTabulate& pkz)
552 : kmin_(pkz.kmin_) , kmax_(pkz.kmax_) , interptyp_(pkz.interptyp_)
553 , k_(pkz.k_) , pk_(pkz.pk_)
554 , d1_(pkz.d1_)
555{
556}
557
558PkTabulate::~PkTabulate(void)
559{
560}
561
562void PkTabulate ::SetInterpTyp(int typ)
563// see comment in InterpTab
564{
565 if(typ<0) typ=0; else if(typ>2) typ=2;
566 interptyp_ = typ;
567}
568
569double PkTabulate::operator() (double k)
570{
571 double v = InterpTab(k,k_,pk_,interptyp_);
572 if(typspec_ == DELTA) v *= k*k*k/(2.*M_PI*M_PI);
573 return scale_ * v;
574}
575
576double PkTabulate::operator() (double k,double z)
577{
578 cout<<"PkTabulate::operator(double k,double z)_Error: not implemented"<<endl;
579 throw AllocationError("PkTabulate::operator(double k,double z)_Error: not implemented");
580}
581
582void PkTabulate::SetZ(double z)
583{
584 if(d1_ == NULL) {
585 cout<<"PkTabulate::SetZ_Error: d1==NULL, no possible redshift change for tabulated Pk"<<endl;
586 throw ParmError("PkTabulate::SetZ_Error: d1==NULL, no possible redshift change for tabulated Pk");
587 }
588 if(pk_.size() == 0) {
589 cout<<"PkTabulate::SetZ_Error: pk_.size()==0, no possible redshift change for tabulated Pk"<<endl;
590 throw ParmError("PkTabulate::SetZ_Error: pk_.size()==0, no possible redshift change for tabulated Pk");
591 }
592
593 double zold = zref_;
594 if(fabs(z-zold)<1.e-4) return;
595
596 zref_ = z;
597 double d0 = (*d1_)(zold);
598 double d1 = (*d1_)(zref_);
599 double conv = d1*d1 / (d0*d0);
600 cout<<"PkTabulate::SetZ: change redshift from "<<zold<<" (d="<<d0
601 <<") to "<<zref_<<" (d="<<d1<<") conv="<<conv<<endl;
602 for(unsigned int i=0;i<pk_.size();i++) pk_[i] *= conv;
603}
604
605int PkTabulate::ReadCAMB(string filename, double h100tab, double zreftab)
606{
607 FILE *file = fopen(filename.c_str(),"r");
608 if(file==NULL) return -1;
609 cout<<"PkTabulate::ReadCAMB: fn="<<filename<<" h100="<<h100tab<<" zreftab = "<<zreftab<<endl;
610
611 const int lenline = 512;
612 char *line = new char[lenline];
613 double h = h100tab, h3 = pow(h100tab,3.);
614
615 k_.resize(0); pk_.resize(0);
616 double kmax = 0., pkmax = 0.;
617 while ( fgets(line,lenline,file) != NULL ) {
618 double k, pk;
619 sscanf(line,"%lf %lf",&k,&pk);
620 k *= h; // convert h Mpc^-1 -> Mpc^-1
621 pk /= h3; // convert h^-3 Mpc^3 -> Mpc^3
622 if(pk>pkmax) {pkmax = pk; kmax = k;}
623 k_.push_back(k);
624 pk_.push_back(pk);
625 }
626
627 zref_ = zreftab;
628 cout<<" nread="<<pk_.size()<<" zref="<<GetZ()<<" , k,pk: max="<<kmax<<","<<pkmax;
629 if(pk_.size()>0) cout<<" [0]="<<k_[0]<<","<<pk_[0]
630 <<" [n]="<<k_[pk_.size()-1]<<","<<pk_[pk_.size()-1];
631 cout<<endl;
632
633 delete [] line;
634
635 return (int)pk_.size();
636}
637
638///////////////////////////////////////////////////////////
639//********************* PkEisenstein ********************//
640///////////////////////////////////////////////////////////
641
642PkEisenstein::PkEisenstein(InitialPowerLaw& pkinf,TransfertEisenstein& tf,GrowthEisenstein& d1,double zref)
643 : pkinf_(pkinf) , tf_(tf) , d1_(d1)
644{
645 zref_ = zref;
646}
647
648PkEisenstein::PkEisenstein(PkEisenstein& pkz)
649 : pkinf_(pkz.pkinf_) , tf_(pkz.tf_) , d1_(pkz.d1_)
650{
651}
652
653PkEisenstein::~PkEisenstein(void)
654{
655}
656
657double PkEisenstein::operator() (double k)
658{
659 return (*this)(k,zref_);
660}
661
662double PkEisenstein::operator() (double k,double z)
663{
664 double tf = tf_(k);
665 double pkinf = pkinf_(k);
666 double d1 = d1_(z);
667
668 double v = pkinf * (tf*tf) * (d1*d1);
669 if(typspec_ == DELTA) v *= k*k*k/(2.*M_PI*M_PI);
670
671 return scale_ * v;
672}
673
674
675///////////////////////////////////////////////////////////
676//******************* VarianceSpectrum ******************//
677///////////////////////////////////////////////////////////
678
679VarianceSpectrum::VarianceSpectrum(GenericFunc& pk,double R,TypeFilter typfilter)
680 : pk_(pk)
681{
682 SetRadius(R);
683 SetFilter(typfilter);
684}
685
686VarianceSpectrum::VarianceSpectrum(VarianceSpectrum& vpk)
687 : pk_(vpk.pk_) , R_(vpk.R_)
688{
689 SetFilter(vpk.typfilter_);
690}
691
692VarianceSpectrum::~VarianceSpectrum(void)
693{
694}
695
696void VarianceSpectrum::SetRadius(double R)
697// R = taille du filter top-hat ou gaussien
698{
699 if(R<=0.) {
700 cout<<"VarianceSpectrum::SetRadius_Error: R<=0"<<endl;
701 throw ParmError("VarianceSpectrum::SetRadius_Error: R<=0");
702 }
703 R_ = R;
704}
705
706//------------------------------------
707void VarianceSpectrum::SetFilter(TypeFilter typfilter)
708// typfilter = TOPHAT : spherical 3D top-hat
709// = GAUSSIAN : spherical 3D gaussian
710// = NOFILTER : no filter juste integrate spectrum)
711// Remarque:
712// la meilleure approximation du filtre top-hat (R) est un filtre gaussien avec (Rg=R/sqrt(5))
713{
714 typfilter_ = typfilter;
715}
716
717void VarianceSpectrum::SetInteg(double dperc,double dlogkinc,double dlogkmax,unsigned short glorder)
718// ATTENTION: on n'integre pas f(k)*dk mais k*f(k)*d(log10(k))
719// see argument details in function IntegrateFuncLog (geneutils.cc)
720{
721 dperc_ = dperc; if(dperc_<=0.) dperc_ = 0.1;
722 dlogkinc_ = dlogkinc;
723 dlogkmax_ = dlogkmax;
724 glorder_ = glorder;
725}
726
727
728//------------------------------------
729double VarianceSpectrum::Filter2(double x)
730// ATTENTION: c'est le filtre au carre qui est renvoye
731{
732 // Just integrate the spectrum without filtering
733 if(typfilter_ == NOFILTER) return 1.;
734
735 double x2 = x*x;
736 // Filtre gaussien G(x) = exp(-x^2/2)
737 // remarque G(x)^2 = exp(-x^2)
738 // on prend le DL de G(x)^2 pour x->0 a l'ordre O(x^6)
739 // DL(x) = 1-x^2*(1-x^2/2)
740 // pour x<0.01 |DL(x)-G(X)^2|<2.0e-13
741 if(typfilter_ == GAUSSIAN)
742 {if(x<0.01) return 1.-x2*(1.-x2/2.); else return exp(-x2);}
743
744 // Filtre top-hat T(x) = 3*(sin(x)-x*cos(x))/x^3
745 // --- Gestion de la pseudo-divergence pour x->0
746 // on prend le DL de T(x)^2 pour x->0 a l'ordre O(x^7)
747 // DL(x) = 1-x^2/5*(1-3*x^2/35*(1-4*x^2/81))
748 // pour x<0.1 |DL(x)-T(X)^2|<2.5e-13
749 double f2=0.;
750 if(x<0.1) {
751 f2 = 1.-x2/5.*(1.-3.*x2/35.*(1.-4.*x2/81.));
752 } else {
753 f2 = 3.*(sin(x)-x*cos(x))/(x2*x);
754 f2 *= f2;
755 }
756 return f2;
757
758}
759
760double VarianceSpectrum::Variance(double kmin,double kmax)
761// Compute variance of spectrum pk_ by integration
762// Input:
763// kmin,kmax = bornes en k de l'integrale pour calculer la variance
764// Return:
765// valeur de la variance (sigma^2)
766// Remarque:
767// la variance renvoyee est la variance de la masse
768{
769 if(kmin<=0 || kmax<=0. || kmin>=kmax) {
770 cout<<"VarianceSpectrum::Variance_Error: kmin<=0 or kmax<=0 or kmin>=kmax"<<endl;
771 throw ParmError("VarianceSpectrum::Variance_Error: kmin<=0 or kmax<=0 or kmin>=kmax");
772 }
773
774 double lkmin = log10(kmin), lkmax = log10(kmax);
775
776 double var = IntegrateFuncLog(*this,lkmin,lkmax,dperc_,dlogkinc_,dlogkmax_,glorder_);
777
778 return var;
779}
780
781//------------------------------------
782double VarianceSpectrum::FindMaximum(double kmin,double kmax,double eps)
783// Retourne le maximum de la fonction a integrer
784// La recherche a lieu entre [kmin,kmax] par pas logarithmiques
785// Input:
786// kmin,kmax : intervalle de recherche
787// eps : precision requise sur les valeurs
788// Return:
789// position (en k) du maximum
790{
791 if(kmin<=0 || kmax<=0. || kmin>=kmax) {
792 cout<<"VarianceSpectrum::FindMaximum_Error: kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0"<<endl;
793 throw ParmError("VarianceSpectrum::FindMaximum_Error: kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0");
794 }
795
796 int n = 10; // toujours >2
797 double lkmin = log10(kmin), lkmax = log10(kmax), dlk = (lkmax-lkmin)/n;
798
799 double lkfind=lkmin, pkfind=-1.;
800 while(1) {
801 for(int i=0; i<=n; i++) {
802 double lk = lkmin + i*dlk;
803 double v = (*this)(pow(10.,lk));
804 if(v<pkfind) continue;
805 pkfind = v; lkfind = lk;
806 }
807 //cout<<"VarianceSpectrum::FindMaximum: lkfind="<<lkfind<<" pkfind="<<pkfind
808 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
809 // --- Convergence si l'encadrement de "kfind" est tel que "dk/kfind<eps"
810 // On a dk = 10^(lkfind+dlk) - 10^(lkfind-dlk) = kfind * (10^(dlk) - 10^(-dlk))
811 if( pow(10.,dlk)-pow(10.,-dlk) < eps ) break;
812 if(lkfind-dlk>lkmin) lkmin = lkfind-dlk;
813 if(lkfind+dlk<lkmax) lkmax = lkfind+dlk;
814 dlk = (lkmax-lkmin)/n;
815 }
816
817 return pow(10.,lkfind);
818}
819
820int VarianceSpectrum::FindLimits(double high,double &kmin,double &kmax,double eps)
821// Retourne "[kmin,kmax]" tel que la fonction a integrer soit "f(k) <= high"
822// La recherche a lieu entre [kmin,kmax] par pas logarithmiques
823// Input:
824// kmin,kmax : intervalle de recherche
825// eps : precision requise sur les valeurs kmin et kmax
826// Output:
827// kmin,kmax telles que "f(k) <= high"
828// Return:
829// rc = 0 si OK
830// rc |= 1 "f(kmin) >= high" (bit0 =1)
831// rc |= 2 "f(kmax) >= high" (bit1 =1)
832// rc |= 4 "f(k) < high pour tout k" (bit2 =1)
833{
834 if(kmin<=0 || kmax<=0. || kmin>=kmax || eps<=0.) {
835 cout<<"VarianceSpectrum::FindLimits_Error: kmin<=0 or kmax<=0 or kmin>=kmax or eps<=0"<<endl;
836 throw ParmError("VarianceSpectrum::FindLimits_Error: kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0");
837 }
838
839 int n = 10; // toujours >2
840
841 int rc = 0;
842 double lkmin,lkmax,dlk,lkfind;
843
844 // --- Find kmin
845 lkmin=log10(kmin); lkmax=log10(kmax); dlk=(lkmax-lkmin)/n;
846 while(1) {
847 lkfind = lkmin;
848 for(int i=0;i<=n;i++) {
849 if( (*this)(pow(10,lkfind)) >= high ) break;
850 lkfind = lkmin + i*dlk;
851 }
852 //cout<<"VarianceSpectrum::FindLimits[kmin]: lkfind="<<lkfind
853 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
854 if(fabs(lkfind-lkmax)<dlk/2.) {rc |= 4; return rc;} // protect against f(k)<high for all k
855 if( pow(10.,dlk)-pow(10.,-dlk) < eps ) break;
856 if(lkfind-dlk>lkmin) lkmin = lkfind-dlk;
857 if(lkfind+dlk<lkmax) lkmax = lkfind+dlk;
858 dlk = (lkmax-lkmin)/n;
859 }
860 if(lkfind-lkmin<dlk/2.) rc |= 1; // f(kmin) >= high
861 else kmin = pow(10.,lkmin);
862 //cout<<"rc="<<rc<<" lkmin="<<lkmin<<" pk="<<(*this)(pow(10.,lkmin))<<endl;
863
864 // --- Find kmax
865 lkmin=log10(kmin); lkmax=log10(kmax); dlk=(lkmax-lkmin)/n;
866 while(1) {
867 lkfind=lkmax;
868 for(int i=0;i<=n;i++) {
869 if( (*this)(pow(10,lkfind)) >= high ) break;
870 lkfind -= dlk;
871 lkfind = lkmax - i*dlk;
872 }
873 //cout<<"VarianceSpectrum::FindLimits[kmax]: lkfind="<<lkfind
874 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
875 if( pow(10.,dlk)-pow(10.,-dlk) < eps ) break;
876 if(lkfind-dlk>lkmin) lkmin = lkfind-dlk;
877 if(lkfind+dlk<lkmax) lkmax = lkfind+dlk;
878 dlk = (lkmax-lkmin)/n;
879 }
880 if(lkmax-lkfind<dlk/2.) rc |= 2; // f(kmax) >= high
881 else kmax = pow(10.,lkmax);
882 //cout<<"rc="<<rc<<" lkmax="<<lkmax<<" pk="<<(*this)(pow(10.,lkmax))<<endl;
883
884 return rc;
885}
886
887} // Fin namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.