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

Last change on this file since 3363 was 3325, checked in by cmv, 18 years ago

Mise en conformite / SOPHYA lib:

  1. on enleve #include "sopnamsp.h" dans les .cc de la librairie
  2. on encadre par "namespace SOPHYA { ... }" tout le code des .cc de la librairie y compris les fonctions
  3. on met les fcts des .h dans le "namespace SOPHYA { ... }"
  4. on met #include "sopnamsp.h" dans tous les cmv*.cc cad les main programs

cmv le mauvais eleve (sur les conseils de Reza) 13/09/2007

File size: 12.4 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 : no compute Otot from other
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 _Omatter0 = univ._Omatter0;
70 _Obaryon0 = univ._Obaryon0;
71 _Orelat0 = univ._Orelat0;
72 _Otot0 = univ._Otot0;
73 _Ocurv0 = univ._Ocurv0;
74 _Dhubble = univ._Dhubble;
75
76 _dperc = univ._dperc;
77 _dzinc = univ._dzinc;
78 _dzmax = univ._dzmax;
79 _glorder = univ._glorder;
80 if(_xgausl.size()>0) {
81 _xgausl.resize(0); _wgausl.resize(0);
82 for(int i=0;i<(int)_xgausl.size();i++) {
83 _xgausl.push_back(univ._xgausl[i]);
84 _wgausl.push_back(univ._wgausl[i]);
85 }
86 }
87
88 _zold = -1.;
89 _integval = -1.;
90
91 _usespline = univ._usespline;
92 _computespl = true;
93 _zmax = univ._zmax;
94 _nspl = 0;
95 if(_spline) delete _spline; _spline = NULL;
96 if(_xspl) delete [] _xspl; _xspl = NULL;
97 if(_yspl) delete [] _yspl; _yspl = NULL;
98}
99
100//----------------------------------------------------------
101// On va couper l'intervalle entre [0,zmax]:
102// On parcourt [0,zmax] par pas de dzinc
103// et on cree un intervalle
104// - si la fonction varie de plus de "dperc"
105// - ou si l'increment en z est superieur a "dzmax"
106void CosmoCalc::SetInteg(double dperc,double dzinc,double dzmax,unsigned short order)
107{
108 _dperc = dperc; _dzinc = dzinc; _dzmax = dzmax;
109
110 if(_dperc<=0.) _dperc = 0.01;
111
112 if(_dzinc<=0.) _dzinc = _zmax/100.;
113 if(_dzinc>=_zmax/2.) _dzinc = _zmax/2.; // Protection against big dzinc
114
115 if(_dzmax<=0.) _dzmax = _zmax;
116 if(_dzmax<_dzinc) _dzmax = _dzinc;
117
118 _glorder = order;
119 if(_glorder<=1) _glorder = 4;
120 Compute_GaussLeg(_glorder,_xgausl,_wgausl,0.,1.);
121
122 _computespl=true;
123}
124
125
126void CosmoCalc::PrtInteg(void)
127{
128 printf("CosmoCalc::PrtInteg: dperc=%g dzinc=%g dzmax=%g glorder=%hu\n",_dperc,_dzinc,_dzmax,_glorder);
129}
130
131void CosmoCalc::SetObaryon0(double v)
132{
133 _Obaryon0 = v;
134 if(_Obaryon0<0.) {
135 cout<<"CosmoCalc::SetObaryon0: Error bad _Obaryon0 value: "<<_Obaryon0<<endl;
136 throw ParmError("CosmoCalc::SetObaryon0: Error bad _Obaryon0 value");
137 }
138}
139
140void CosmoCalc::SetDynParam(double h100,double om0,double or0,double ol0,double w0)
141{
142 _computespl=true;
143
144 _h100 = h100;
145 _H0 = 100. * _h100 ;
146 _Dhubble = SpeedOfLight_Cst / _H0;
147
148 _Omatter0 = om0;
149 _Orelat0 = or0;
150 _Olambda0 = ol0;
151 _W0 = w0;
152 _Otot0 = _Olambda0 + _Omatter0 + _Orelat0;
153 _Ocurv0 = 1. - _Otot0;
154 if( _h100<0. || _Omatter0<0. || _Orelat0<0. || _Olambda0<0. ) {
155 cout<<"CosmoCalc::SetDynParam: Error bad parameter value"<<endl;
156 throw ParmError("CosmoCalc::SetDynParam: Error bad parameter value");
157 }
158 if(_flat==0) return;
159
160 _Otot0 = 1.; _Ocurv0 = 0.;
161 if(_flat==1) {
162 _Omatter0 = _Otot0 - _Olambda0 - _Orelat0;
163 if( _Omatter0<0. ) {
164 cout<<"CosmoCalc::SetDynParam: Error bad _Omatter0 value: "<<_Omatter0<<endl;
165 throw ParmError("CosmoCalc::SetDynParam: Error bad _Omatter0 value");
166 }
167 return;
168 }
169
170 //--
171 if(_flat==2) {
172 _Olambda0 = _Otot0 - _Omatter0 - _Orelat0;
173 if( _Olambda0<0. ) {
174 cout<<"CosmoCalc::SetDynParam: Error bad _Olambda0 value: "<<_Olambda0<<endl;
175 throw ParmError("CosmoCalc::SetDynParam: Error bad _Olambda0 value");
176 }
177 return;
178 }
179
180 //--
181 if(_flat==3) {
182 _Orelat0 = _Otot0 - _Omatter0 - _Olambda0;
183 if( _Orelat0<0. ) {
184 cout<<"CosmoCalc::SetDynParam: Error bad _Orelat0 value: "<<_Orelat0<<endl;
185 throw ParmError("CosmoCalc::SetDynParam: Error bad _Orelat0 value");
186 }
187 return;
188 }
189
190 cout<<"CosmoCalc::SetDynParam: Error bad _flat value: "<<_flat<<endl;
191 throw ParmError("CosmoCalc::SetDynParam: Error bad _flat value");
192
193}
194
195void CosmoCalc::DefaultParam(void)
196{
197 _computespl=true;
198
199 double h100 = 0.71;
200 double ol0 = 0.73, w0 = -1.;
201 double om0 = 0.135/(h100*h100);
202 // Relat = photons (2.725 K) + neutrinos (1.9 K)
203 double or0 = 2.47e-5 * (1. + 21./8.*pow(T_NU_Par/T_CMB_Par,4.)) / (h100*h100);
204 SetDynParam(h100,om0,or0,ol0,w0);
205 _Obaryon0 = 0.0224/(h100*h100);
206
207 return;
208}
209
210//----------------------------------------------------------
211void CosmoCalc::Print(double z)
212{
213 if(z<0.) z = 0.;
214
215 printf("CosmoCalc::Print(spl=%d,zmax=%g,flat=%u) for z=%g\n",int(_usespline),ZMax(),Flat(),z);
216 printf("h100=%g H0=%g Dhub=%g H(z)=%g Rhoc=%g g/cm^3 =%g Msol/Mpc^3\n"
217 ,h100(),H0(),Dhubble(),H(z),Rhoc(z),Rhoc(z)*GCm3toMsolMpc3_Cst);
218 printf("Olambda=%g W0=%g\n",Olambda(z),_W0);
219 printf("Omatter=%g Obaryon=%g\n",Omatter(z),Obaryon(z));
220 printf("Orelat=%g\n",Orelat(z));
221 printf("Otot=%g Ocurv=%g\n",Otot(z),Ocurv(z));
222 if(z <= 0.) return;
223 printf("Distance comoving: los=%g Mpc, transv=%g Mpc\n",Dloscom(z),Dtrcom(z));
224 printf("Distance: angular=%g Mpc, lum=%g Mpc\n",Dang(z),Dlum(z));
225 printf("Volume comoving element: %g Mpc^3/sr/dz=1\n",dVol(z));
226 printf("Volume comoving in [0,z] for 4Pi sr: %g Mpc^3\n",Vol4Pi(z));
227
228}
229//----------------------------------------------------------
230double CosmoCalc::Olambda(double z)
231{
232 double v = _Olambda0;
233 if(_W0 != -1.) v *= pow((1.+z),3.*(1+_W0));
234 return v / E2(z);
235}
236
237double CosmoCalc::Omatter(double z)
238{
239 return _Omatter0 * (1.+z)*(1.+z)*(1.+z) /E2(z);
240}
241
242double CosmoCalc::Obaryon(double z)
243{
244 return _Obaryon0 * (1.+z)*(1.+z)*(1.+z) /E2(z);
245}
246
247double CosmoCalc::Orelat(double z)
248{
249 double z2 = (1.+z)*(1.+z);
250 return _Orelat0 * z2*z2 /E2(z);
251}
252
253double CosmoCalc::Ocurv(double z)
254{
255 return _Ocurv0 * (1.+z)*(1.+z) /E2(z);
256}
257
258double CosmoCalc::Otot(double z)
259{
260 return Olambda(z)+Omatter(z)+Orelat(z);
261}
262
263double CosmoCalc::Rhoc(double z)
264// Densite critique au redshift "z" en "g/cm^3"
265{
266 double h2 = H(z) / MpctoMeters_Cst; h2 *= h2;
267 return 3.* h2 / (8.*M_PI*G_Newton_Cst) * 1000.;
268}
269
270//----------------------------------------------------------
271double CosmoCalc::Dloscom(double z) /* Mpc comobile */
272{
273 return _Dhubble * NInteg(z);
274}
275
276double CosmoCalc::Dtrcom(double z) /* Mpc comobile */
277{
278 double v = NInteg(z); // Zero curvature
279
280 if(_flat) return _Dhubble * v;
281
282 if(_Ocurv0>0.) { // hyperbolique
283 double s = sqrt(_Ocurv0);
284 v = sinh(s*v)/s;
285 } else if(_Ocurv0<0.) { // spherique
286 double s = sqrt(-_Ocurv0);
287 v = sin(s*v)/s;
288 }
289
290 return _Dhubble * v;
291
292}
293
294double CosmoCalc::Dang(double z) /* Mpc */
295{
296 return Dtrcom(z) / (1.+z);
297}
298
299double CosmoCalc::Dlum(double z) /* Mpc */
300{
301 return (1.+z) * Dtrcom(z);
302}
303
304double CosmoCalc::dVol(double z) /* Mpc^3/ sr / unite z */
305{
306 double d = Dtrcom(z);
307 return _Dhubble * d*d / E(z);
308}
309
310//----------------------------------------------------------
311double CosmoCalc::Vol4Pi(double z) /* Mpc^3 pour 4Pi sr entre [0,z] */
312 // --- on pose x = dm/dh = (1+z)*da/dh, s = sqrt(|Ok|)
313 // Ok=0 : V(z)/dh^3 = (4*Pi/3) * x^3
314 // Ok>0 : V(z)/dh^3 = (4*Pi/(2*Ok)) * (x*sqrt(1+Ok*x^2) - 1/s * asinh(s*x))
315 // Ok<0 : V(z)/dh^3 = (4*Pi/(2*Ok)) * (x*sqrt(1+Ok*x^2) - 1/s * asin(s*x) )
316 // --- on pose y = s*x (y>0)
317 // Ok>0 : V(z)/dh^3 = (4*Pi/(2*Ok*s)) * (y*sqrt(1+y^2) - asinh(y))
318 // Ok<0 : V(z)/dh^3 = (4*Pi/(2*Ok*s)) * (y*sqrt(1-y^2) - asin(y) )
319 // --- a petit "z" on a pour "y -> 0+" en faisant le DL
320 // Ok>0 : V(z)/dh^3 = 4*Pi*y^3 / (3*Ok*s) * (1 - 3*y^2/10) + O(y^7)
321 // Ok<0 : V(z)/dh^3 = -4*Pi*y^3 / (3*Ok*s) * (1 + 3*y^2/10) + O(y^7)
322 // (remarque: Ok^(3/2) = s*Ok)
323{
324 double v,x = Dtrcom(z)/_Dhubble;
325
326 if(_flat) {
327 v = 4.*M_PI/3. * x*x*x;
328 } else if(_Ocurv0>0.) {
329 double s = sqrt(_Ocurv0), y = s*x, y2 = y*y;
330 if(y<1e-6) {
331 v = 1. - 3.*y2/10.;
332 v *= 4.*M_PI*y*y2 / (3.*_Ocurv0*s);
333 } else {
334 v = y*sqrt(1.+y2) - asinh(y);
335 v *= 4.*M_PI/(2.*_Ocurv0*s);
336 }
337 } else if (_Ocurv0<0.) {
338 double s = sqrt(-_Ocurv0), y = s*x, y2 = y*y;
339 if(y<1e-6) {
340 v = 1. + 3.*y2/10.;
341 v *= -4.*M_PI*y*y2 / (3.*_Ocurv0*s);
342 } else {
343 v = y*sqrt(1.-y2) - asin(y);
344 v *= 4.*M_PI/(2.*_Ocurv0*s);
345 }
346 } else {
347 v = 4.*M_PI/3. * x*x*x;
348 }
349
350 return v * _Dhubble*_Dhubble*_Dhubble;
351}
352
353double CosmoCalc::Vol4Pi(double z1,double z2) /* Mpc^3 pour 4Pi sr entre [z1,z2] */
354{
355 return Vol4Pi(z2) - Vol4Pi(z1);
356}
357
358double CosmoCalc::E2(double z) const
359{
360 z += 1.;
361 double oldum = _Olambda0;
362
363 if(oldum>0. && _W0!=-1.) oldum *= pow(z,3.*(1.+_W0));
364
365 return oldum + z*z*(_Ocurv0 + z*(_Omatter0+z*_Orelat0));
366}
367
368//----------------------------------------------------------
369double CosmoCalc::NInteg(double z)
370{
371 if(z<0.) {
372 cout<<"CosmoCalc::NInteg: Error bad z value z="<<z<<endl;
373 throw ParmError("CosmoCalc::NInteg: Error bad z value");
374 }
375
376 // On utilise le spline
377 if(_usespline) {
378 if( (!_computespl) && (z==_zold) ) return _integval;
379 if(z>_zmax) {_zmax = z+_dzmax; _computespl = true;}
380 if(_computespl) Init_Spline();
381 _integval = _spline->CSplineInt(z);
382 _zold = z;
383 return _integval;
384 }
385
386 // On calcule l'integrale
387 if(z != _zold) {
388 _integval = IntegrateFunc(*this,0.,z,_dperc,_dzinc,_dzmax,_glorder);
389 _zold = z;
390 }
391 return _integval;
392
393}
394
395int_4 CosmoCalc::Init_Spline(void)
396{
397 if(_spline!=NULL)
398 {delete _spline; delete [] _xspl; delete [] _yspl;}
399
400 //-- Look for intervalles and integrate
401 vector<double> x,y;
402 x.push_back(0.); y.push_back(0.);
403 double zbas=0., fbas = Integrand(zbas);
404 for(double z=_dzinc;;z+=_dzinc) {
405 double f = Integrand(z);
406 //cout<<fbas<<","<<f<<" : "<<fabs(f-fbas)<<" >? "<<_dperc*fabs(fbas)<<endl;
407 if( z>_zmax || z-zbas>_dzmax || fabs(f-fbas)>_dperc*fabs(fbas) ) {
408 double sum=0., dz=z-zbas;
409 for(unsigned short i=0;i<_glorder;i++) sum += _wgausl[i]*Integrand(zbas+_xgausl[i]*dz);
410 x.push_back(z); y.push_back(sum*dz);
411 //cout<<"...set... "<<zbas<<","<<z<<" , "<<fbas<<","<<f<<endl;
412 zbas = z; fbas = f;
413 if( z>_zmax ) break;
414 }
415 }
416
417 //-- Protection car il faut au moins 4 points pour un spline cubique
418 if(x.size()<4) {
419 x.resize(0); y.resize(0);
420 x.push_back(0.); y.push_back(0.);
421 for(int i=1;i<4;i++) {
422 x.push_back(i*_zmax/3.);
423 double sum=0., dz=x[i]-x[i-1];
424 for(unsigned short i=0;i<_glorder;i++) sum += _wgausl[i]*Integrand(x[i-1]+_xgausl[i]*dz);
425 y.push_back(sum*dz);
426 }
427 }
428
429 //-- Fill spline
430 _nspl = x.size();
431 _xspl = new double[_nspl];
432 _yspl = new double[_nspl];
433 for(int i=0;i<_nspl;i++) {
434 _xspl[i] = x[i];
435 _yspl[i] = y[i];
436 if(i!=0) _yspl[i] += _yspl[i-1];
437 }
438
439#if 1
440 cout<<"CosmoCalc::Init_Spline called: (zmax="<<_zmax<<") _nspl="<<_nspl<<endl;
441 int n = (_nspl>5)? 5: _nspl;
442 cout<<_nspl<<"...";
443 for(int i=0;i<n;i++) cout<<_xspl[i]<<" ("<<_yspl[i]<<") ";
444 cout<<"\n... ";
445 n = (_nspl<5)? 0: _nspl-5;
446 for(int i=n;i<_nspl;i++) cout<<_xspl[i]<<" ("<<_yspl[i]<<") ";
447 cout<<endl;
448#endif
449
450 double yp1 = Integrand(_xspl[0]);
451 double ypn = Integrand(_xspl[_nspl-1]);
452 _spline = new CSpline(_nspl,_xspl,_yspl,yp1,ypn,CSpline::NoNatural,false);
453 _spline->ComputeCSpline();
454 _computespl = false;
455
456 return _nspl;
457}
458
459} // Fin namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.