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

Last change on this file since 3315 was 3314, checked in by cmv, 18 years ago

correction bug (petit) dans fct transfert cmv 24/08/2007

File size: 19.0 KB
Line 
1#include "sopnamsp.h"
2#include "machdefs.h"
3#include <iostream>
4#include <stdlib.h>
5#include <stdio.h>
6#include <string.h>
7#include <math.h>
8#include <unistd.h>
9
10#include "pexceptions.h"
11
12#include "constcosmo.h"
13#include "geneutils.h"
14#include "pkspectrum.h"
15
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
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)
55{
56 zero_();
57 Init_();
58}
59
60TransfertEisenstein::TransfertEisenstein(TransfertEisenstein& tf)
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_)
64{
65 zero_();
66 Init_();
67}
68
69void TransfertEisenstein::Init_(void)
70{
71
72 O0_ = Oc_ + Ob_;
73 if(nobaryon_) {O0_ = Oc_; Ob_ = 0.;}
74 if(lp_) cout<<"h100="<<h_<<" Omatter="<<O0_<<" Ocdm="<<Oc_<<" Ob="<<Ob_<<endl;
75
76 double H0 = 100. * h_, h2 = h_*h_;
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
169TransfertEisenstein::~TransfertEisenstein(void)
170{
171}
172
173void TransfertEisenstein::SetNoOscEnv(unsigned short nooscenv)
174// To obtain an approximate form of the non-oscillatory part of the transfert function
175// nooscenv = 0 : use the baryon oscillatory part of transfert function (full tf)
176// nooscenv = 1 : use approx. paragraph 3.3 p610 (middle of right column)
177// Replace j0(k*stilde) -> [1+(k*stilde)^4]^(-1/4)
178// nooscenv = 2 : use formulae 29+30+31 page 612
179// The value of an approximate transfer function that captures
180// the non-oscillatory part of a partial baryon transfer function.
181// In other words, the baryon oscillations are left out,
182// but the suppression of power below the sound horizon is included.
183{
184 if(nooscenv!=1 && nooscenv!=2) nooscenv = 0;
185 nooscenv_ = nooscenv;
186}
187
188void TransfertEisenstein::SetReturnPart(unsigned short retpart)
189// To return only baryon or CDM part part of transfert function
190// retpart = 1 : return only CDM part of transfert function
191// retpart = 2 : return only Baryon part of transfert function
192// retpart = anything else: return only full transfert function
193// WARNING: only relevant for nobaryon_=false AND nooscenv!=2
194{
195 if(retpart!=1 && retpart!=2) retpart = 0;
196 retpart_ = retpart;
197}
198
199double TransfertEisenstein::T0tild(double k,double alphac,double betac)
200{
201 // Formule 10 p 608
202 //double q = k*th2p7_*th2p7_/(O0_*h_*h_);
203 double q = k/(13.41*keq_);
204 // Formule 20 p 610
205 double C = (14.2/alphac) + 386./(1.+69.9*pow(q,1.08));
206 // Formule 19 p 610
207 double x = log(M_E+1.8*betac*q);
208 return x / (x + C*q*q);
209}
210
211double TransfertEisenstein::operator() (double k)
212{
213
214 // --- Pour zero baryon
215 // OU Pour function lissee sans oscillation baryon
216 if(nobaryon_ || nooscenv_ == 2) {
217 double gamma = O0_*h_;
218 // Calcul de Gamma_eff, formule 30 page 612 (pour fct lissee)
219 if( nobaryon_==false && nooscenv_ == 2 )
220 gamma = O0_*h_*(alphag_ + (1.-alphag_)/(1.+pow(0.43*k*sfit_,4.))); // Gamma_eff
221 // Formule 28 page 612 : qui est est equivalent a:
222 // q = k / h_ * th2p7_*th2p7_ / gamma;
223 // qui est est equivalent a:
224 // q = k / (13.41 * keq) pour Ob=0
225 // q = k / (13.41 * keq) * (O0*h/Gamma) pour le spectre lisse
226 // Les resultats sont legerement differents a cause des valeurs approx.
227 // des constantes numeriques: on prend comme W.Hu (tf_fit.c)
228 //double q = k / h_ * th2p7_*th2p7_ / gamma; // Mpc^-1
229 double q = k/(13.41*keq_) * (O0_*h_/gamma); // Mpc^-1
230 // Formules 29 page 612
231 double l0 = log(2.*M_E + 1.8*q);
232 double c0 = 14.2 + 731./(1.+62.5*q);
233 return l0 / (l0 + c0*q*q);
234 }
235
236 // --- Pour CDM + Baryons
237 // --- CDM
238 double f = 1. / (1. + pow(k*s_/5.4,4.));
239 double Tc = f*T0tild(k,1.,betac_) + (1.-f)*T0tild(k,alphac_,betac_);
240 if(retpart_ == 1) return Tc;
241
242 // --- Baryons
243 // Formule 22 page 610
244 double stilde, ksbnode = k*s_/bnode_;
245 if(ksbnode<0.001) stilde =s_ * ksbnode;
246 else stilde = s_ / pow(1. + pow(1./ksbnode,3.), 1./3.);
247 // Formule 21 page 610
248 double j0kst = 0.;
249 if(nooscenv_ == 1) {
250 j0kst = pow(1.+pow(k*stilde,4.) , -1./4.); //lissee sans oscillation baryon
251 } else {
252 double x = k*stilde;
253 if(x<0.01) j0kst = 1. - x*x/6.*(1.-x*x/20.);
254 else j0kst = sin(x)/x;
255 //cout<<"DEBUG: k="<<k<<" stilde="<<stilde<<" x="<<x<<" j0kst="<<j0kst<<endl;
256 }
257 double Tb = T0tild(k,1.,1.) / (1. + pow(k*s_/5.2,2.));
258 Tb += alphab_/(1.+pow(betab_/(k*s_),3.)) * exp(-pow(k/ksilk_,1.4));
259 Tb *= j0kst;
260 if(retpart_ == 2) return Tb;
261
262 // --- Total
263 double T = (Ob_/O0_)*Tb + (Oc_/O0_)*Tc;
264
265 return T;
266}
267
268double TransfertEisenstein::KPeak(void)
269// Position du premier pic acoustic
270{
271 if(nobaryon_) return -1.;
272 return kpeak_;
273}
274
275
276///////////////////////////////////////////////////////////
277//********************* GrowthFactor ********************//
278///////////////////////////////////////////////////////////
279
280// From Eisenstein & Hu ApJ 496:605-614 1998 April 1
281// Pour avoir D(z) = 1/(1+z) faire: OmegaMatter0=1 OmegaLambda0=0
282GrowthFactor::GrowthFactor(double OmegaMatter0,double OmegaLambda0)
283 : O0_(OmegaMatter0) , Ol_(OmegaLambda0) , Ok_(1.-OmegaMatter0-OmegaLambda0)
284{
285 if(OmegaMatter0==0.) {
286 cout<<"GrowthFactor::GrowthFactor: Error bad OmegaMatter0 value : "<<OmegaMatter0<<endl;
287 throw ParmError("GrowthFactor::GrowthFactor: Error badOmegaMatter0 value");
288 }
289 norm_ = 1.; // puisque (*this)(0.) a besoin de norm_
290 norm_ = (*this)(0.);
291 cout<<"GrowthFactor::GrowthFactor : norm="<<norm_<<endl;
292}
293
294GrowthFactor::GrowthFactor(GrowthFactor& d1)
295 : O0_(d1.O0_) , Ol_(d1.Ol_) , Ok_(d1.Ok_) , norm_(d1.norm_)
296{
297}
298
299GrowthFactor::~GrowthFactor(void)
300{
301}
302
303double GrowthFactor::operator() (double z)
304// see Formulae A4 + A5 + A6 page 614
305{
306 z += 1.;
307 double z2 = z*z, z3 = z2*z;
308 double den = Ol_ + Ok_*z2 + O0_*z3;
309 double o0z = O0_ *z3 / den;
310 double olz = Ol_ / den;
311
312 // 4./7. = 0.571429
313 double D1z = pow(o0z,0.571429) - olz + (1.+o0z/2.)*(1.+olz/70.);
314 D1z = 2.5*o0z / z / D1z;
315
316 return D1z / norm_;
317}
318
319
320///////////////////////////////////////////////////////////
321//************** PkSpectrum0 et PkSpectrumZ *************//
322///////////////////////////////////////////////////////////
323
324PkSpectrum0::PkSpectrum0(InitialSpectrum& pkinf,TransfertEisenstein& tf)
325 : pkinf_(pkinf) , tf_(tf)
326{
327}
328
329PkSpectrum0::PkSpectrum0(PkSpectrum0& pk0)
330 : pkinf_(pk0.pkinf_) , tf_(pk0.tf_)
331{
332}
333
334PkSpectrum0::~PkSpectrum0(void)
335{
336}
337
338double PkSpectrum0::operator() (double k)
339{
340 double tf = tf_(k);
341 double pkinf = pkinf_(k);
342 return pkinf *tf*tf;
343}
344
345//------------------------------------
346PkSpectrumZ::PkSpectrumZ(PkSpectrum0& pk0,GrowthFactor& d1,double zref)
347 : pk0_(pk0) , d1_(d1) , zref_(zref) , scale_(1.) , typspec_(0)
348 , zold_(-1.) , d1old_(1.)
349{
350}
351
352PkSpectrumZ::PkSpectrumZ(PkSpectrumZ& pkz)
353 : pk0_(pkz.pk0_) , d1_(pkz.d1_) , zref_(pkz.zref_) , scale_(pkz.scale_) , typspec_(0)
354 , zold_(pkz.zold_) , d1old_(pkz.d1old_)
355{
356}
357
358PkSpectrumZ::~PkSpectrumZ(void)
359{
360}
361
362void PkSpectrumZ::SetTypSpec(unsigned short typspec)
363 // typsec = 0 : compute Pk(k)
364 // = 1 : compute Delta^2(k) = k^3*Pk(k)/2Pi^2
365{
366 if(typspec>1) {
367 cout<<"PkSpectrumZ::SetTypSpec: Error bad typspec value: "<<typspec<<endl;
368 throw ParmError("PkSpectrumZ::SetTypSpec: Error bad typspec value");
369 }
370 typspec_ = typspec;
371}
372
373double PkSpectrumZ::operator() (double k)
374{
375 return (*this)(k,zref_);
376}
377
378double PkSpectrumZ::operator() (double k,double z)
379{
380 double d1;
381 if(z == zold_) d1 = d1old_;
382 else {d1 = d1old_ = d1_(z); zold_ = z;}
383
384 double v = pk0_(k) * d1*d1;
385 if(typspec_) v *= k*k*k/(2.*M_PI*M_PI);
386
387 return scale_ * v;
388}
389
390
391
392///////////////////////////////////////////////////////////
393//******************* VarianceSpectrum ******************//
394///////////////////////////////////////////////////////////
395
396VarianceSpectrum::VarianceSpectrum(GenericFunc& pk,unsigned short typfilter=0)
397 : pk_(pk) , R_(0.)
398{
399 SetFilter(typfilter);
400}
401
402VarianceSpectrum::VarianceSpectrum(VarianceSpectrum& vpk)
403 : pk_(vpk.pk_) , R_(vpk.R_)
404{
405 SetFilter(vpk.typfilter_);
406}
407
408VarianceSpectrum::~VarianceSpectrum(void)
409{
410}
411
412//------------------------------------
413void VarianceSpectrum::SetFilter(unsigned short typfilter)
414// typfilter = 0 : spherical 3D top-hat
415// = 1 : spherical 3D gaussian
416// = 2 : no filter juste integrate spectrum)
417{
418 if(typfilter>2) {
419 cout<<"VarianceSpectrum::SetFilter: Error bad value for type of filter"<<endl;
420 throw ParmError("VarianceSpectrum::SetFilter: Error bad value for type of filter");
421 }
422 typfilter_ = typfilter;
423}
424
425void VarianceSpectrum::SetInteg(double dperc,double dlogkinc,double dlogkmax,unsigned short glorder)
426// ATTENTION: on n'integre pas f(k)*dk mais k*f(k)*d(log10(k))
427// see argument details in function IntegrateFuncLog (geneutils.cc)
428{
429 dperc_ = dperc; if(dperc_<=0.) dperc_ = 0.1;
430 dlogkinc_ = dlogkinc;
431 dlogkmax_ = dlogkmax;
432 glorder_ = glorder;
433}
434
435
436//------------------------------------
437double VarianceSpectrum::Filter2(double x)
438// ATTENTION: c'est le filtre au carre qui est renvoye
439{
440 // Just integrate the spectrum without filtering
441 if(typfilter_ == 2) return 1.;
442
443 double x2 = x*x;
444 // Filtre gaussien G(x) = exp(-x^2/2)
445 // remarque G(x)^2 = exp(-x^2)
446 // on prend le DL de G(x)^2 pour x->0 a l'ordre O(x^6)
447 // DL(x) = 1-x^2*(1-x^2/2)
448 // pour x<0.01 |DL(x)-G(X)^2|<2.0e-13
449 if(typfilter_ == 1)
450 if(x<0.01) return 1.-x2*(1.-x2/2.); else return exp(-x2);
451
452 // Filtre top-hat T(x) = 3*(sin(x)-x*cos(x))/x^3
453 // --- Gestion de la pseudo-divergence pour x->0
454 // on prend le DL de T(x)^2 pour x->0 a l'ordre O(x^7)
455 // DL(x) = 1-x^2/5*(1-3*x^2/35*(1-4*x^2/81))
456 // pour x<0.1 |DL(x)-T(X)^2|<2.5e-13
457 double f2=0.;
458 if(x<0.1) {
459 f2 = 1.-x2/5.*(1.-3.*x2/35.*(1.-4.*x2/81.));
460 } else {
461 f2 = 3.*(sin(x)-x*cos(x))/(x2*x);
462 f2 *= f2;
463 }
464 return f2;
465
466}
467
468double VarianceSpectrum::Variance(double R,double kmin,double kmax)
469// Compute variance of spectrum pk_ by integration
470// Input:
471// R = taille du filter top-hat ou gaussien
472// kmin,kmax = bornes en k de l'integrale pour calculer la variance
473// Return:
474// valeur de la variance (sigma^2)
475// Remarque:
476// la meilleure approximation du filtre top-hat (R) est un filtre gaussien avec (Rg=R/sqrt(5))
477// la variance renvoyee est la variance de la masse
478{
479 if(R<=0. || kmin<=0 || kmax<=0. || kmin>=kmax) {
480 cout<<"VarianceSpectrum::Variance: Error R<=0 or kmin<=0 or kmax<=0 or kmin>=kmax"<<endl;
481 throw ParmError("VarianceSpectrum::Variance: Error R<=0 or kmin<=0 or kmax<=0 or kmin>=kmax");
482 }
483
484 R_ = R;
485 double lkmin = log10(kmin), lkmax = log10(kmax);
486
487 double var = IntegrateFuncLog(*this,lkmin,lkmax,dperc_,dlogkinc_,dlogkmax_,glorder_);
488
489 return var;
490}
491
492//------------------------------------
493double VarianceSpectrum::FindMaximum(double R,double kmin,double kmax,double eps)
494// Retourne le maximum de la fonction a integrer
495// La recherche a lieu entre [kmin,kmax] par pas logarithmiques
496// Input:
497// R : taille du filter top-hat ou gaussien
498// kmin,kmax : intervalle de recherche
499// eps : precision requise sur les valeurs
500// Return:
501// position (en k) du maximum
502{
503 if(R<=0. || kmin<=0 || kmax<=0. || kmin>=kmax) {
504 cout<<"VarianceSpectrum::FindMaximum: Error R<=0 or kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0"<<endl;
505 throw ParmError("VarianceSpectrum::FindMaximum: Error R<=0 or kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0");
506 }
507
508 R_ = R;
509
510 int n = 10; // toujours >2
511 double lkmin = log10(kmin), lkmax = log10(kmax), dlk = (lkmax-lkmin)/n;
512
513 double lkfind=lkmin, pkfind=-1.;
514 while(1) {
515 for(int i=0; i<=n; i++) {
516 double lk = lkmin + i*dlk;
517 double v = (*this)(pow(10.,lk));
518 if(v<pkfind) continue;
519 pkfind = v; lkfind = lk;
520 }
521 //cout<<"VarianceSpectrum::FindMaximum: lkfind="<<lkfind<<" pkfind="<<pkfind
522 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
523 // --- Convergence si l'encadrement de "kfind" est tel que "dk/kfind<eps"
524 // On a dk = 10^(lkfind+dlk) - 10^(lkfind-dlk) = kfind * (10^(dlk) - 10^(-dlk))
525 if( pow(10.,dlk)-pow(10.,-dlk) < eps ) break;
526 if(lkfind-dlk>lkmin) lkmin = lkfind-dlk;
527 if(lkfind+dlk<lkmax) lkmax = lkfind+dlk;
528 dlk = (lkmax-lkmin)/n;
529 }
530
531 return pow(10.,lkfind);
532}
533
534int VarianceSpectrum::FindLimits(double R,double high,double &kmin,double &kmax,double eps)
535// Retourne "[kmin,kmax]" tel que la fonction a integrer soit "f(k) <= high"
536// La recherche a lieu entre [kmin,kmax] par pas logarithmiques
537// Input:
538// R : taille du filter top-hat ou gaussien
539// kmin,kmax : intervalle de recherche
540// eps : precision requise sur les valeurs kmin et kmax
541// Output:
542// kmin,kmax telles que "f(k) <= high"
543// Return:
544// rc = 0 si OK
545// rc |= 1 "f(kmin) >= high" (bit0 =1)
546// rc |= 2 "f(kmax) >= high" (bit1 =1)
547// rc |= 4 "f(k) < high pour tout k" (bit2 =1)
548{
549 if(R<=0. || kmin<=0 || kmax<=0. || kmin>=kmax || eps<=0.) {
550 cout<<"VarianceSpectrum::FindLimits: Error R<=0 or kmin<=0 or kmax<=0 or kmin>=kmax or eps<=0"<<endl;
551 throw ParmError("VarianceSpectrum::FindLimits: Error R<=0 or kmin<=0 or kmax<=0 or kmin>=kmax || eps<=0");
552 }
553
554 R_ = R;
555 int n = 10; // toujours >2
556
557 int rc = 0;
558 double lkmin,lkmax,dlk,lkfind;
559
560 // --- Find kmin
561 lkmin=log10(kmin); lkmax=log10(kmax); dlk=(lkmax-lkmin)/n;
562 while(1) {
563 lkfind = lkmin;
564 for(int i=0;i<=n;i++) {
565 if( (*this)(pow(10,lkfind)) >= high ) break;
566 lkfind = lkmin + i*dlk;
567 }
568 //cout<<"VarianceSpectrum::FindLimits[kmin]: lkfind="<<lkfind
569 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
570 if(fabs(lkfind-lkmax)<dlk/2.) {rc |= 4; return rc;} // protect against f(k)<high for all k
571 if( pow(10.,dlk)-pow(10.,-dlk) < eps ) break;
572 if(lkfind-dlk>lkmin) lkmin = lkfind-dlk;
573 if(lkfind+dlk<lkmax) lkmax = lkfind+dlk;
574 dlk = (lkmax-lkmin)/n;
575 }
576 if(lkfind-lkmin<dlk/2.) rc |= 1; // f(kmin) >= high
577 else kmin = pow(10.,lkmin);
578 //cout<<"rc="<<rc<<" lkmin="<<lkmin<<" pk="<<(*this)(pow(10.,lkmin))<<endl;
579
580 // --- Find kmax
581 lkmin=log10(kmin); lkmax=log10(kmax); dlk=(lkmax-lkmin)/n;
582 while(1) {
583 lkfind=lkmax;
584 for(int i=0;i<=n;i++) {
585 if( (*this)(pow(10,lkfind)) >= high ) break;
586 lkfind -= dlk;
587 lkfind = lkmax - i*dlk;
588 }
589 //cout<<"VarianceSpectrum::FindLimits[kmax]: lkfind="<<lkfind
590 // <<" lkmin,max="<<lkmin<<","<<lkmax<<" dlk="<<dlk<<endl;
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 if(lkmax-lkfind<dlk/2.) rc |= 2; // f(kmax) >= high
597 else kmax = pow(10.,lkmax);
598 //cout<<"rc="<<rc<<" lkmax="<<lkmax<<" pk="<<(*this)(pow(10.,lkmax))<<endl;
599
600 return rc;
601}
Note: See TracBrowser for help on using the repository browser.