-
Notifications
You must be signed in to change notification settings - Fork 2
/
distance.c
97 lines (81 loc) · 2.18 KB
/
distance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <math.h>
#include <assert.h>
#include "config_vars.h"
#include "hubble.h"
#define MAX_Z 300.0
#define Z_BINS 1000.0
#define TOTAL_BINS (((int)MAX_Z)*((int)Z_BINS))
#define h h0
double Dh;
double _Dc[TOTAL_BINS];
#ifndef __APPLE__
#ifndef isfinite
#define isfinite(x) finitef(x)
#endif
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288 /* From math.h */
#endif /* M_PI */
#define _E(z) (hubble_scaling(z))
void init_cosmology(void)
{
int i;
double z;
double Dc_int = 0;
Dh = 2997.92458 / h; //In Mpc (Speed of light / H0)
for (i=0; i<TOTAL_BINS; i++) {
z = (float)(i+0.5) / Z_BINS;
_Dc[i] = Dc_int * Dh;
Dc_int += 1.0 / (_E(z)*Z_BINS);
}
}
double redshift(double a) { return (1.0/a-1.0); }
double scale_factor(double z) { return (1.0/(1.0+z)); }
double comoving_distance(double z) {
double f = z*Z_BINS;
int bin = (int)f;
if (z<0) return 0;
if (bin>(TOTAL_BINS-2)) return (_Dc[TOTAL_BINS-1]);
f -= bin;
return (_Dc[bin]*(1.0-f) + _Dc[bin+1]*f);
}
double comoving_distance_h(double z) { return (comoving_distance(z)*h); }
double transverse_distance(double z) { return (comoving_distance(z)); }
double angular_diameter_distance(double z) {
return (transverse_distance(z) / (1.0 + z));
}
double luminosity_distance(double z) {
return ((1.0+z)*transverse_distance(z));
}
double comoving_volume_element(double z) {
double z1da = (1.0+z)*angular_diameter_distance(z);
return (Dh*(z1da*z1da)/_E(z));
}
double comoving_volume(double z) {
double r = transverse_distance(z);
return (4.0*M_PI*r*r*r/3.0);
}
double comoving_distance_to_redshift(double r)
{
if (r<=0) return 0;
double z = 1;
double dz = 0.1;
double rt;
while (dz > 1e-7) {
rt = transverse_distance(z);
dz = ((r - rt) * dz)/(transverse_distance(z+dz) - transverse_distance(z));
if (!isfinite(dz)) return z;
if (z+dz < 0) z /= 3.0;
else z+=dz;
dz = fabs(dz);
if (dz>0.1) dz = 0.1;
}
return z;
}
double comoving_volume_to_redshift(double Vc) {
double r = cbrt(Vc*(3.0/(4.0*M_PI)));
return (comoving_distance_to_redshift(r));
}
double comoving_distance_h_to_redshift(double r) {
return (comoving_distance_to_redshift(r/h));
}