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

Last change on this file since 3793 was 3768, checked in by cmv, 15 years ago
  • refonte du code pour creer uniquement des conditions initiales
  • introduction du tirage des vitesse LOS pour les redshift-distortion

cmv 03/05/2010

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