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

Last change on this file since 4086 was 4047, checked in by cmv, 14 years ago

details..., cmv 12/01/2012

File size: 15.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#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 (dlos/dz=%g), transv=%g Mpc\n",Dloscom(z),DloscomDz(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::DloscomDz(double z)
297// d(Dloscom)/dz : derivee de la distance LOS comobile par rapport a z
298{
299 return SpeedOfLight_Cst / H(z);
300}
301
302double CosmoCalc::Dtrcom(double z) /* Mpc comobile */
303{
304 double v = NInteg(z); // Zero curvature
305
306 if(_flat) return _Dhubble * v;
307
308 if(_Ocurv0>0.) { // hyperbolique
309 double s = sqrt(_Ocurv0);
310 v = sinh(s*v)/s;
311 } else if(_Ocurv0<0.) { // spherique
312 double s = sqrt(-_Ocurv0);
313 v = sin(s*v)/s;
314 }
315
316 return _Dhubble * v;
317
318}
319
320double CosmoCalc::Dang(double z) /* Mpc */
321{
322 return Dtrcom(z) / (1.+z);
323}
324
325double CosmoCalc::Dlum(double z) /* Mpc */
326{
327 return (1.+z) * Dtrcom(z);
328}
329
330double CosmoCalc::dVol(double z) /* Mpc^3/ sr / unite z */
331{
332 double d = Dtrcom(z);
333 return _Dhubble * d*d / E(z);
334}
335
336//----------------------------------------------------------
337double CosmoCalc::Vol4Pi(double z) /* Mpc^3 pour 4Pi sr entre [0,z] */
338 // --- on pose x = dm/dh = (1+z)*da/dh, s = sqrt(|Ok|)
339 // Ok=0 : V(z)/dh^3 = (4*Pi/3) * x^3
340 // Ok>0 : V(z)/dh^3 = (4*Pi/(2*Ok)) * (x*sqrt(1+Ok*x^2) - 1/s * asinh(s*x))
341 // Ok<0 : V(z)/dh^3 = (4*Pi/(2*Ok)) * (x*sqrt(1+Ok*x^2) - 1/s * asin(s*x) )
342 // --- on pose y = s*x (y>0)
343 // Ok>0 : V(z)/dh^3 = (4*Pi/(2*Ok*s)) * (y*sqrt(1+y^2) - asinh(y))
344 // Ok<0 : V(z)/dh^3 = (4*Pi/(2*Ok*s)) * (y*sqrt(1-y^2) - asin(y) )
345 // --- a petit "z" on a pour "y -> 0+" en faisant le DL
346 // Ok>0 : V(z)/dh^3 = 4*Pi*y^3 / (3*Ok*s) * (1 - 3*y^2/10) + O(y^7)
347 // Ok<0 : V(z)/dh^3 = -4*Pi*y^3 / (3*Ok*s) * (1 + 3*y^2/10) + O(y^7)
348 // (remarque: Ok^(3/2) = s*Ok)
349{
350 double v,x = Dtrcom(z)/_Dhubble;
351
352 if(_flat) {
353 v = 4.*M_PI/3. * x*x*x;
354 } else if(_Ocurv0>0.) {
355 double s = sqrt(_Ocurv0), y = s*x, y2 = y*y;
356 if(y<1e-6) {
357 v = 1. - 3.*y2/10.;
358 v *= 4.*M_PI*y*y2 / (3.*_Ocurv0*s);
359 } else {
360 v = y*sqrt(1.+y2) - asinh(y);
361 v *= 4.*M_PI/(2.*_Ocurv0*s);
362 }
363 } else if (_Ocurv0<0.) {
364 double s = sqrt(-_Ocurv0), y = s*x, y2 = y*y;
365 if(y<1e-6) {
366 v = 1. + 3.*y2/10.;
367 v *= -4.*M_PI*y*y2 / (3.*_Ocurv0*s);
368 } else {
369 v = y*sqrt(1.-y2) - asin(y);
370 v *= 4.*M_PI/(2.*_Ocurv0*s);
371 }
372 } else {
373 v = 4.*M_PI/3. * x*x*x;
374 }
375
376 return v * _Dhubble*_Dhubble*_Dhubble;
377}
378
379double CosmoCalc::Vol4Pi(double z1,double z2) /* Mpc^3 pour 4Pi sr entre [z1,z2] */
380{
381 return Vol4Pi(z2) - Vol4Pi(z1);
382}
383
384double CosmoCalc::E2(double z) const
385{
386 double zp1 = 1. + z;
387
388 double oldum = _Olambda0;
389 if(oldum>0.) {
390 oldum *= pow(zp1,3.*(1.+_W0+_Wa));
391 if(_Wa!=0.) oldum *= exp(-3.*_Wa*z/zp1);
392 }
393
394 return oldum + zp1*zp1*(_Ocurv0 + zp1*(_Omatter0+zp1*_Orelat0));
395}
396
397//----------------------------------------------------------
398double CosmoCalc::ZFrLos(double loscom /* Mpc com */, int niter)
399// Recherche du redshift correspondant a une distance comobile
400// le long de la ligne de visee (radiale) egale a "loscom" Mpc
401// niter = nomber of iterations for precision measurement
402{
403 if(niter<3) niter = 6;
404 double dz = ZMax()/10.; if(dz<=0.) dz = 0.1;
405 double zmin=0., zmax=0.;
406 while(Dloscom(zmax)<loscom) zmax += dz;
407 if(zmax==0.) return 0.;
408 for(int i=0; i<niter; i++) {
409 zmin=zmax-dz; if(zmin<0.) zmin=0.;
410 dz /= 10.;
411 for(double z=zmin; z<zmax+dz; z+=dz) {
412 double d = Dloscom(z);
413 if(d<loscom) continue;
414 zmax = z;
415 //cout<<"ZFrLos: z="<<zmax<<" d="<<d<<" / "<<loscom<<endl;
416 break;
417 }
418 }
419 return zmax;
420}
421
422//----------------------------------------------------------
423double CosmoCalc::NInteg(double z)
424{
425 if(z<0.) {
426 cout<<"CosmoCalc::NInteg: Error bad z value z="<<z<<endl;
427 throw ParmError("CosmoCalc::NInteg: Error bad z value");
428 }
429
430 // On utilise le spline
431 if(_usespline) {
432 if( (!_computespl) && (z==_zold) ) return _integval;
433 if(z>_zmax) {_zmax = z+_dzmax; _computespl = true;}
434 if(_computespl) Init_Spline();
435 _integval = _spline->CSplineInt(z);
436 _zold = z;
437 return _integval;
438 }
439
440 // On calcule l'integrale
441 if(z != _zold) {
442 _integval = IntegrateFunc(*this,0.,z,_dperc,_dzinc,_dzmax,_glorder);
443 _zold = z;
444 }
445 return _integval;
446
447}
448
449int_4 CosmoCalc::Init_Spline(void)
450{
451 if(_spline!=NULL)
452 {delete _spline; delete [] _xspl; delete [] _yspl;}
453
454 //-- Look for intervalles and integrate
455 vector<double> x,y;
456 x.push_back(0.); y.push_back(0.);
457 double zbas=0., fbas = Integrand(zbas);
458 for(double z=_dzinc;;z+=_dzinc) {
459 double f = Integrand(z);
460 //cout<<fbas<<","<<f<<" : "<<fabs(f-fbas)<<" >? "<<_dperc*fabs(fbas)<<endl;
461 if( z>_zmax || z-zbas>_dzmax || fabs(f-fbas)>_dperc*fabs(fbas) ) {
462 double sum=0., dz=z-zbas;
463 for(unsigned short i=0;i<_glorder;i++) sum += _wgausl[i]*Integrand(zbas+_xgausl[i]*dz);
464 x.push_back(z); y.push_back(sum*dz);
465 //cout<<"...set... "<<zbas<<","<<z<<" , "<<fbas<<","<<f<<endl;
466 zbas = z; fbas = f;
467 if( z>_zmax ) break;
468 }
469 }
470
471 //-- Protection car il faut au moins 4 points pour un spline cubique
472 if(x.size()<4) {
473 x.resize(0); y.resize(0);
474 x.push_back(0.); y.push_back(0.);
475 for(int i=1;i<4;i++) {
476 x.push_back(i*_zmax/3.);
477 double sum=0., dz=x[i]-x[i-1];
478 for(unsigned short i=0;i<_glorder;i++) sum += _wgausl[i]*Integrand(x[i-1]+_xgausl[i]*dz);
479 y.push_back(sum*dz);
480 }
481 }
482
483 //-- Fill spline
484 _nspl = x.size();
485 _xspl = new double[_nspl];
486 _yspl = new double[_nspl];
487 for(int i=0;i<_nspl;i++) {
488 _xspl[i] = x[i];
489 _yspl[i] = y[i];
490 if(i!=0) _yspl[i] += _yspl[i-1];
491 }
492
493#if 1
494 cout<<"CosmoCalc::Init_Spline called: (zmax="<<_zmax<<") _nspl="<<_nspl<<endl;
495 int n = (_nspl>5)? 5: _nspl;
496 cout<<_nspl<<"...";
497 for(int i=0;i<n;i++) cout<<_xspl[i]<<" ("<<_yspl[i]<<") ";
498 cout<<"\n... ";
499 n = (_nspl<5)? 0: _nspl-5;
500 for(int i=n;i<_nspl;i++) cout<<_xspl[i]<<" ("<<_yspl[i]<<") ";
501 cout<<endl;
502#endif
503
504 double yp1 = Integrand(_xspl[0]);
505 double ypn = Integrand(_xspl[_nspl-1]);
506 _spline = new CSpline(_nspl,_xspl,_yspl,yp1,ypn,CSpline::NoNatural,false);
507 _spline->ComputeCSpline();
508 _computespl = false;
509
510 return _nspl;
511}
512
513//==========================================================================
514//==========================================================================
515//==========================================================================
516double LargeurDoppler(double v, double nu)
517// largeur doppler pour une vitesse v en km/s et une frequence nu
518{
519 return v / SpeedOfLight_Cst * nu;
520}
521
522double DzFrV(double v, double zred)
523// largeur en redshift pour une vitesse v en km/s au redshift zred
524{
525 return v / SpeedOfLight_Cst * (1. + zred);
526}
527
528double DNuFrDz(double dzred,double nu_at_0,double zred)
529// Largeur DNu pour une largeur en redshift "dzred" au redshift "zred"
530// pour la frequence "nu_at_0" a z=0
531// nu = NuHi(z=0)/(1.+z0)
532// dnu = NuHi(z=0)/(1.+z0-dz/2) - NuHi/(1.+z0+dz/2)
533// = NuHi(z=0)*dz/[ (1+z0)^2 - (dz/2)^2 ]
534// = NuHi(z=0)*dz/(1.+z0)^2 / [ 1 - [dz/(1+z0)/2)]^2 ]
535// = NuHi(z=0)*dz/(1.+z0)^2 / [1 - dz/(1+z0)/2] / [1 + dz/(1+z0)/2]
536// ~= NuHi(z=0)*dz/(1.+z0)^2 (approx. pour dz<<z0 a l'ordre (dz/z0)^2)
537{
538 double zp1 = 1.+zred;
539 return nu_at_0*dzred/(zp1*zp1)/(1.-dzred/zp1/2.)/(1.+dzred/zp1/2.);
540}
541
542double DzFrDNu(double dnu_at_0,double nu_at_0,double zred)
543// Largeur en redshift au redshift "zred" pour une largeur
544// en frequence "dnu_at_0" a la frequence "nu_at_0" a z=0
545{
546 if(dnu_at_0<=0.) return 0.;
547 double zp1 = 1.+zred;
548 double dnusnu0 = dnu_at_0/nu_at_0;
549 return 2./dnusnu0 * (sqrt(1.+(dnusnu0*zp1)*(dnusnu0*zp1)) - 1.);
550}
551double DzFrDNuApprox(double dnu_at_0,double nu_at_0,double zred)
552// idem DzFrDNu mais on utilise l'approximation: dnu=NuHi(z=0)*dz/(1.+z0)^2
553{
554 double zp1 = 1.+zred;
555 return dnu_at_0/nu_at_0 *(zp1*zp1);
556}
557
558} // Fin namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.