source: Sophya/trunk/Cosmo/SimLSS/cosmocalc.cc@ 4043

Last change on this file since 4043 was 3970, checked in by cmv, 14 years ago

qques ameliorations, cmv, 31/03/2011

File size: 15.6 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#include "cspline.h"
11
12#include "constcosmo.h"
13#include "cosmocalc.h"
14#include "geneutils.h"
15
16namespace SOPHYA {
17
18///////////////////////////////////////////////////////////
19//*********************** CosmoCalc *********************//
20///////////////////////////////////////////////////////////
21
22//----------------------------------------------------------
23// flat = 0 : do not set Otot=1, compute Otot from others
24// = 1 : change Omatter to get Otot=1
25// = 2 : change Olambda to get Otot=1
26// = 3 : change Orelat to get Otot=1
27// usespline = "true" : compute integrale then use spline to extrapolate
28// = "false" : compute integrale everytime
29// zmax : maximum redshift(only if usespline=true)
30
31CosmoCalc::CosmoCalc(unsigned short flat,bool usespline,double zmax)
32 : _flat(flat),
33 _zold(-1.) , _integval(0.),
34 _usespline(usespline), _computespl(true), _zmax(zmax), _spline(NULL),
35 _nspl(0), _xspl(NULL), _yspl(NULL)
36{
37 if(_usespline && _zmax<=0.) {
38 cout<<"CosmoCalc::CosmoCalc: Error bad _zmax value zmax="<< _zmax<<endl;
39 throw ParmError("CosmoCalc::UseSpline: Error bad _zmax value");
40 }
41 DefaultParam();
42 SetInteg();
43}
44
45CosmoCalc::CosmoCalc(CosmoCalc& univ)
46 : _spline(NULL), _xspl(NULL), _yspl(NULL)
47{
48 Clone(univ);
49 if(_usespline && _zmax<=0.) {
50 cout<<"CosmoCalc::CosmoCalc: Error bad _zmax value zmax="<< _zmax<<endl;
51 throw ParmError("CosmoCalc::UseSpline: Error bad _zmax value");
52 }
53}
54
55CosmoCalc::~CosmoCalc(void)
56{
57 if(_spline != NULL) delete _spline; _spline = NULL;
58 if(_xspl != NULL) delete [] _xspl; _xspl = NULL;
59 if(_yspl != NULL) delete [] _yspl; _yspl = NULL;
60}
61
62void CosmoCalc::Clone(CosmoCalc& univ)
63{
64 _flat = univ._flat;
65 _h100 = univ._h100;
66 _H0 = univ._H0;
67 _Olambda0 = univ._Olambda0;
68 _W0 = univ._W0;
69 _Wa = univ._Wa;
70 _Omatter0 = univ._Omatter0;
71 _Obaryon0 = univ._Obaryon0;
72 _Orelat0 = univ._Orelat0;
73 _Otot0 = univ._Otot0;
74 _Ocurv0 = univ._Ocurv0;
75 _Dhubble = univ._Dhubble;
76
77 _dperc = univ._dperc;
78 _dzinc = univ._dzinc;
79 _dzmax = univ._dzmax;
80 _glorder = univ._glorder;
81 if(_xgausl.size()>0) {
82 _xgausl.resize(0); _wgausl.resize(0);
83 for(int i=0;i<(int)_xgausl.size();i++) {
84 _xgausl.push_back(univ._xgausl[i]);
85 _wgausl.push_back(univ._wgausl[i]);
86 }
87 }
88
89 _zold = -1.;
90 _integval = -1.;
91
92 _usespline = univ._usespline;
93 _computespl = true;
94 _zmax = univ._zmax;
95 _nspl = 0;
96 if(_spline) delete _spline; _spline = NULL;
97 if(_xspl) delete [] _xspl; _xspl = NULL;
98 if(_yspl) delete [] _yspl; _yspl = NULL;
99}
100
101//----------------------------------------------------------
102// On change le zmax de la cosmologie
103void CosmoCalc::ChangeZmax(double zmax,double dzinc,double dzmax)
104{
105 _zmax = zmax;
106 if(dzinc<=0. ) dzinc = _dzinc;
107 if(dzmax<=0.) dzmax = _dzmax;
108 cout<<"CosmoCalc::ChangeZmax: zmax="<<_zmax<<" dzinc="<<dzinc<<" dzmax="<<dzmax<<endl;
109 SetInteg(_dperc,dzinc,_dzmax,_glorder);
110}
111
112//----------------------------------------------------------
113// On va couper l'intervalle entre [0,zmax]:
114// On parcourt [0,zmax] par pas de dzinc
115// et on cree un intervalle
116// - si la fonction varie de plus de "dperc"
117// - ou si l'increment en z est superieur a "dzmax"
118void CosmoCalc::SetInteg(double dperc,double dzinc,double dzmax,unsigned short order)
119{
120 _dperc = dperc; _dzinc = dzinc; _dzmax = dzmax;
121
122 if(_dperc<=0.) _dperc = 0.01;
123
124 if(_dzinc<=0.) _dzinc = _zmax/100.;
125 if(_dzinc>=_zmax/2.) _dzinc = _zmax/2.; // Protection against big dzinc
126
127 if(_dzmax<=0.) _dzmax = _zmax;
128 if(_dzmax<_dzinc) _dzmax = _dzinc;
129
130 _glorder = order;
131 if(_glorder<=1) _glorder = 4;
132 Compute_GaussLeg(_glorder,_xgausl,_wgausl,0.,1.);
133
134 _computespl=true;
135}
136
137
138void CosmoCalc::PrtInteg(void)
139{
140 printf("CosmoCalc::PrtInteg: dperc=%g dzinc=%g dzmax=%g glorder=%hu\n",_dperc,_dzinc,_dzmax,_glorder);
141}
142
143void CosmoCalc::SetObaryon0(double v)
144{
145 _Obaryon0 = v;
146 if(_Obaryon0<0.) {
147 cout<<"CosmoCalc::SetObaryon0: Error bad _Obaryon0 value: "<<_Obaryon0<<endl;
148 throw ParmError("CosmoCalc::SetObaryon0: Error bad _Obaryon0 value");
149 }
150}
151
152void CosmoCalc::SetDynParam(double h100,double om0,double or0,double ol0,double w0,double wa)
153{
154 _computespl=true;
155
156 _h100 = h100;
157 _H0 = 100. * _h100 ;
158 _Dhubble = SpeedOfLight_Cst / _H0;
159
160 _Omatter0 = om0;
161 _Orelat0 = or0;
162 _Olambda0 = ol0;
163 _W0 = w0;
164 _Wa = wa;
165 _Otot0 = _Olambda0 + _Omatter0 + _Orelat0;
166 _Ocurv0 = 1. - _Otot0;
167 if( _h100<0. || _Omatter0<0. || _Orelat0<0. || _Olambda0<0. ) {
168 cout<<"CosmoCalc::SetDynParam: Error bad parameter value"<<endl;
169 throw ParmError("CosmoCalc::SetDynParam: Error bad parameter value");
170 }
171 if(_flat==0) return;
172
173 _Otot0 = 1.; _Ocurv0 = 0.;
174 if(_flat==1) {
175 _Omatter0 = _Otot0 - _Olambda0 - _Orelat0;
176 if( _Omatter0<0. ) {
177 cout<<"CosmoCalc::SetDynParam: Error bad _Omatter0 value: "<<_Omatter0<<endl;
178 throw ParmError("CosmoCalc::SetDynParam: Error bad _Omatter0 value");
179 }
180 return;
181 }
182
183 //--
184 if(_flat==2) {
185 _Olambda0 = _Otot0 - _Omatter0 - _Orelat0;
186 if( _Olambda0<0. ) {
187 cout<<"CosmoCalc::SetDynParam: Error bad _Olambda0 value: "<<_Olambda0<<endl;
188 throw ParmError("CosmoCalc::SetDynParam: Error bad _Olambda0 value");
189 }
190 return;
191 }
192
193 //--
194 if(_flat==3) {
195 _Orelat0 = _Otot0 - _Omatter0 - _Olambda0;
196 if( _Orelat0<0. ) {
197 cout<<"CosmoCalc::SetDynParam: Error bad _Orelat0 value: "<<_Orelat0<<endl;
198 throw ParmError("CosmoCalc::SetDynParam: Error bad _Orelat0 value");
199 }
200 return;
201 }
202
203 cout<<"CosmoCalc::SetDynParam: Error bad _flat value: "<<_flat<<endl;
204 throw ParmError("CosmoCalc::SetDynParam: Error bad _flat value");
205
206}
207
208void CosmoCalc::DefaultParam(void)
209{
210 _computespl=true;
211
212 double h100 = 0.71;
213 double ol0 = 0.73, w0 = -1., wa = 0.;
214 double om0 = 0.135/(h100*h100);
215 // Relat = photons (2.725 K) + neutrinos (1.9 K)
216 double or0 = 2.47e-5 * (1. + 21./8.*pow(T_NU_Par/T_CMB_Par,4.)) / (h100*h100);
217 SetDynParam(h100,om0,or0,ol0,w0,wa);
218 _Obaryon0 = 0.0224/(h100*h100);
219
220 return;
221}
222
223//----------------------------------------------------------
224void CosmoCalc::Print(double z)
225{
226 if(z<0.) z = 0.;
227
228 printf("CosmoCalc::Print(spl=%d,zmax=%g,flat=%u) for z=%g (a=%g)\n"
229 ,int(_usespline),ZMax(),Flat(),z,1./(1.+z));
230 printf("h100=%g H0=%g Dhub=%g H(z)=%g Rhoc=%g g/cm^3 =%g Msol/Mpc^3\n"
231 ,h100(),H0(),Dhubble(),H(z),Rhoc(z),Rhoc(z)*GCm3toMsolMpc3_Cst);
232 printf("Olambda=%g W=%g (%g+(%g)*(1-a))\n",Olambda(z),W(z),_W0,_Wa);
233 printf("Omatter=%g Obaryon=%g\n",Omatter(z),Obaryon(z));
234 printf("Orelat=%g\n",Orelat(z));
235 printf("Otot=%g Ocurv=%g\n",Otot(z),Ocurv(z));
236 if(z <= 0.) return;
237 printf("Distance comoving: los=%g Mpc, transv=%g Mpc\n",Dloscom(z),Dtrcom(z));
238 printf("Distance: angular=%g Mpc, lum=%g Mpc\n",Dang(z),Dlum(z));
239 printf("Volume comoving element: %g Mpc^3/sr/dz=1\n",dVol(z));
240 printf("Volume comoving in [0,z] for 4Pi sr: %g Mpc^3\n",Vol4Pi(z));
241
242}
243//----------------------------------------------------------
244double CosmoCalc::Olambda(double z)
245// Equation d'etat de l'energie noire: P=W(z)*Rho*C^2
246// avec W(z) = W0 + Wa*(1-a) = W0 + Wa * z/(1+z) et a=1/(1+z)
247{
248 double zp1 = 1. + z;
249 double v = _Olambda0 * pow(zp1,3.*(1.+_W0+_Wa));
250 if(_Wa!=0.) v *= exp(-3.*_Wa*z/zp1);
251 return v / E2(z);
252}
253
254double CosmoCalc::Omatter(double z)
255{
256 double zp1 = 1. + z;
257 return _Omatter0 * zp1*zp1*zp1 /E2(z);
258}
259
260double CosmoCalc::Obaryon(double z)
261{
262 double zp1 = 1. + z;
263 return _Obaryon0 * zp1*zp1*zp1 /E2(z);
264}
265
266double CosmoCalc::Orelat(double z)
267{
268 double z2 = (1.+z)*(1.+z);
269 return _Orelat0 * z2*z2 /E2(z);
270}
271
272double CosmoCalc::Ocurv(double z)
273{
274 return _Ocurv0 * (1.+z)*(1.+z) /E2(z);
275}
276
277double CosmoCalc::Otot(double z)
278{
279 return Olambda(z) + Omatter(z) + Orelat(z);
280}
281
282double CosmoCalc::Rhoc(double z)
283// Densite critique au redshift "z" en "g/cm^3"
284// Attention: c'est une densite cad une masse par volume (non-comobile)
285{
286 double h2 = H(z) / MpctoMeters_Cst; h2 *= h2;
287 return 3.* h2 / (8.*M_PI*G_Newton_Cst) * 1000.;
288}
289
290//----------------------------------------------------------
291double CosmoCalc::Dloscom(double z) /* Mpc comobile */
292{
293 return _Dhubble * NInteg(z);
294}
295
296double CosmoCalc::Dtrcom(double z) /* Mpc comobile */
297{
298 double v = NInteg(z); // Zero curvature
299
300 if(_flat) return _Dhubble * v;
301
302 if(_Ocurv0>0.) { // hyperbolique
303 double s = sqrt(_Ocurv0);
304 v = sinh(s*v)/s;
305 } else if(_Ocurv0<0.) { // spherique
306 double s = sqrt(-_Ocurv0);
307 v = sin(s*v)/s;
308 }
309
310 return _Dhubble * v;
311
312}
313
314double CosmoCalc::Dang(double z) /* Mpc */
315{
316 return Dtrcom(z) / (1.+z);
317}
318
319double CosmoCalc::Dlum(double z) /* Mpc */
320{
321 return (1.+z) * Dtrcom(z);
322}
323
324double CosmoCalc::dVol(double z) /* Mpc^3/ sr / unite z */
325{
326 double d = Dtrcom(z);
327 return _Dhubble * d*d / E(z);
328}
329
330//----------------------------------------------------------
331double CosmoCalc::Vol4Pi(double z) /* Mpc^3 pour 4Pi sr entre [0,z] */
332 // --- on pose x = dm/dh = (1+z)*da/dh, s = sqrt(|Ok|)
333 // Ok=0 : V(z)/dh^3 = (4*Pi/3) * x^3
334 // Ok>0 : V(z)/dh^3 = (4*Pi/(2*Ok)) * (x*sqrt(1+Ok*x^2) - 1/s * asinh(s*x))
335 // Ok<0 : V(z)/dh^3 = (4*Pi/(2*Ok)) * (x*sqrt(1+Ok*x^2) - 1/s * asin(s*x) )
336 // --- on pose y = s*x (y>0)
337 // Ok>0 : V(z)/dh^3 = (4*Pi/(2*Ok*s)) * (y*sqrt(1+y^2) - asinh(y))
338 // Ok<0 : V(z)/dh^3 = (4*Pi/(2*Ok*s)) * (y*sqrt(1-y^2) - asin(y) )
339 // --- a petit "z" on a pour "y -> 0+" en faisant le DL
340 // Ok>0 : V(z)/dh^3 = 4*Pi*y^3 / (3*Ok*s) * (1 - 3*y^2/10) + O(y^7)
341 // Ok<0 : V(z)/dh^3 = -4*Pi*y^3 / (3*Ok*s) * (1 + 3*y^2/10) + O(y^7)
342 // (remarque: Ok^(3/2) = s*Ok)
343{
344 double v,x = Dtrcom(z)/_Dhubble;
345
346 if(_flat) {
347 v = 4.*M_PI/3. * x*x*x;
348 } else if(_Ocurv0>0.) {
349 double s = sqrt(_Ocurv0), y = s*x, y2 = y*y;
350 if(y<1e-6) {
351 v = 1. - 3.*y2/10.;
352 v *= 4.*M_PI*y*y2 / (3.*_Ocurv0*s);
353 } else {
354 v = y*sqrt(1.+y2) - asinh(y);
355 v *= 4.*M_PI/(2.*_Ocurv0*s);
356 }
357 } else if (_Ocurv0<0.) {
358 double s = sqrt(-_Ocurv0), y = s*x, y2 = y*y;
359 if(y<1e-6) {
360 v = 1. + 3.*y2/10.;
361 v *= -4.*M_PI*y*y2 / (3.*_Ocurv0*s);
362 } else {
363 v = y*sqrt(1.-y2) - asin(y);
364 v *= 4.*M_PI/(2.*_Ocurv0*s);
365 }
366 } else {
367 v = 4.*M_PI/3. * x*x*x;
368 }
369
370 return v * _Dhubble*_Dhubble*_Dhubble;
371}
372
373double CosmoCalc::Vol4Pi(double z1,double z2) /* Mpc^3 pour 4Pi sr entre [z1,z2] */
374{
375 return Vol4Pi(z2) - Vol4Pi(z1);
376}
377
378double CosmoCalc::E2(double z) const
379{
380 double zp1 = 1. + z;
381
382 double oldum = _Olambda0;
383 if(oldum>0.) {
384 oldum *= pow(zp1,3.*(1.+_W0+_Wa));
385 if(_Wa!=0.) oldum *= exp(-3.*_Wa*z/zp1);
386 }
387
388 return oldum + zp1*zp1*(_Ocurv0 + zp1*(_Omatter0+zp1*_Orelat0));
389}
390
391//----------------------------------------------------------
392double CosmoCalc::ZFrLos(double loscom /* Mpc com */, int niter)
393// Recherche du redshift correspondant a une distance comobile
394// le long de la ligne de visee (radiale) egale a "loscom" Mpc
395// niter = nomber of iterations for precision measurement
396{
397 if(niter<3) niter = 6;
398 double dz = ZMax()/10.; if(dz<=0.) dz = 0.1;
399 double zmin=0., zmax=0.;
400 while(Dloscom(zmax)<loscom) zmax += dz;
401 if(zmax==0.) return 0.;
402 for(int i=0; i<niter; i++) {
403 zmin=zmax-dz; if(zmin<0.) zmin=0.;
404 dz /= 10.;
405 for(double z=zmin; z<zmax+dz; z+=dz) {
406 double d = Dloscom(z);
407 if(d<loscom) continue;
408 zmax = z;
409 //cout<<"ZFrLos: z="<<zmax<<" d="<<d<<" / "<<loscom<<endl;
410 break;
411 }
412 }
413 return zmax;
414}
415
416//----------------------------------------------------------
417double CosmoCalc::NInteg(double z)
418{
419 if(z<0.) {
420 cout<<"CosmoCalc::NInteg: Error bad z value z="<<z<<endl;
421 throw ParmError("CosmoCalc::NInteg: Error bad z value");
422 }
423
424 // On utilise le spline
425 if(_usespline) {
426 if( (!_computespl) && (z==_zold) ) return _integval;
427 if(z>_zmax) {_zmax = z+_dzmax; _computespl = true;}
428 if(_computespl) Init_Spline();
429 _integval = _spline->CSplineInt(z);
430 _zold = z;
431 return _integval;
432 }
433
434 // On calcule l'integrale
435 if(z != _zold) {
436 _integval = IntegrateFunc(*this,0.,z,_dperc,_dzinc,_dzmax,_glorder);
437 _zold = z;
438 }
439 return _integval;
440
441}
442
443int_4 CosmoCalc::Init_Spline(void)
444{
445 if(_spline!=NULL)
446 {delete _spline; delete [] _xspl; delete [] _yspl;}
447
448 //-- Look for intervalles and integrate
449 vector<double> x,y;
450 x.push_back(0.); y.push_back(0.);
451 double zbas=0., fbas = Integrand(zbas);
452 for(double z=_dzinc;;z+=_dzinc) {
453 double f = Integrand(z);
454 //cout<<fbas<<","<<f<<" : "<<fabs(f-fbas)<<" >? "<<_dperc*fabs(fbas)<<endl;
455 if( z>_zmax || z-zbas>_dzmax || fabs(f-fbas)>_dperc*fabs(fbas) ) {
456 double sum=0., dz=z-zbas;
457 for(unsigned short i=0;i<_glorder;i++) sum += _wgausl[i]*Integrand(zbas+_xgausl[i]*dz);
458 x.push_back(z); y.push_back(sum*dz);
459 //cout<<"...set... "<<zbas<<","<<z<<" , "<<fbas<<","<<f<<endl;
460 zbas = z; fbas = f;
461 if( z>_zmax ) break;
462 }
463 }
464
465 //-- Protection car il faut au moins 4 points pour un spline cubique
466 if(x.size()<4) {
467 x.resize(0); y.resize(0);
468 x.push_back(0.); y.push_back(0.);
469 for(int i=1;i<4;i++) {
470 x.push_back(i*_zmax/3.);
471 double sum=0., dz=x[i]-x[i-1];
472 for(unsigned short i=0;i<_glorder;i++) sum += _wgausl[i]*Integrand(x[i-1]+_xgausl[i]*dz);
473 y.push_back(sum*dz);
474 }
475 }
476
477 //-- Fill spline
478 _nspl = x.size();
479 _xspl = new double[_nspl];
480 _yspl = new double[_nspl];
481 for(int i=0;i<_nspl;i++) {
482 _xspl[i] = x[i];
483 _yspl[i] = y[i];
484 if(i!=0) _yspl[i] += _yspl[i-1];
485 }
486
487#if 1
488 cout<<"CosmoCalc::Init_Spline called: (zmax="<<_zmax<<") _nspl="<<_nspl<<endl;
489 int n = (_nspl>5)? 5: _nspl;
490 cout<<_nspl<<"...";
491 for(int i=0;i<n;i++) cout<<_xspl[i]<<" ("<<_yspl[i]<<") ";
492 cout<<"\n... ";
493 n = (_nspl<5)? 0: _nspl-5;
494 for(int i=n;i<_nspl;i++) cout<<_xspl[i]<<" ("<<_yspl[i]<<") ";
495 cout<<endl;
496#endif
497
498 double yp1 = Integrand(_xspl[0]);
499 double ypn = Integrand(_xspl[_nspl-1]);
500 _spline = new CSpline(_nspl,_xspl,_yspl,yp1,ypn,CSpline::NoNatural,false);
501 _spline->ComputeCSpline();
502 _computespl = false;
503
504 return _nspl;
505}
506
507//==========================================================================
508//==========================================================================
509//==========================================================================
510double LargeurDoppler(double v, double nu)
511// largeur doppler pour une vitesse v en km/s et une frequence nu
512{
513 return v / SpeedOfLight_Cst * nu;
514}
515
516double DzFrV(double v, double zred)
517// largeur en redshift pour une vitesse v en km/s au redshift zred
518{
519 return v / SpeedOfLight_Cst * (1. + zred);
520}
521
522double DNuFrDz(double dzred,double nu_at_0,double zred)
523// Largeur DNu pour une largeur en redshift "dzred" au redshift "zred"
524// pour la frequence "nu_at_0" a z=0
525// nu = NuHi(z=0)/(1.+z0)
526// dnu = NuHi(z=0)/(1.+z0-dz/2) - NuHi/(1.+z0+dz/2)
527// = NuHi(z=0)*dz/[ (1+z0)^2 - (dz/2)^2 ]
528// = NuHi(z=0)*dz/(1.+z0)^2 / [ 1 - [dz/(1+z0)/2)]^2 ]
529// = NuHi(z=0)*dz/(1.+z0)^2 / [1 - dz/(1+z0)/2] / [1 + dz/(1+z0)/2]
530// ~= NuHi(z=0)*dz/(1.+z0)^2 (approx. pour dz<<z0 a l'ordre (dz/z0)^2)
531{
532 double zp1 = 1.+zred;
533 return nu_at_0*dzred/(zp1*zp1)/(1.-dzred/zp1/2.)/(1.+dzred/zp1/2.);
534}
535
536double DzFrDNu(double dnu_at_0,double nu_at_0,double zred)
537// Largeur en redshift au redshift "zred" pour une largeur
538// en frequence "dnu_at_0" a la frequence "nu_at_0" a z=0
539{
540 if(dnu_at_0<=0.) return 0.;
541 double zp1 = 1.+zred;
542 double dnusnu0 = dnu_at_0/nu_at_0;
543 return 2./dnusnu0 * (sqrt(1.+(dnusnu0*zp1)*(dnusnu0*zp1)) - 1.);
544}
545double DzFrDNuApprox(double dnu_at_0,double nu_at_0,double zred)
546// idem DzFrDNu mais on utilise l'approximation: dnu=NuHi(z=0)*dz/(1.+z0)^2
547{
548 double zp1 = 1.+zred;
549 return dnu_at_0/nu_at_0 *(zp1*zp1);
550}
551
552} // Fin namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.