-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.py
277 lines (215 loc) · 10.2 KB
/
background.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import numpy as np
from dataclasses import dataclass, field
from helpers import get_constants, check_redshift_valid_array, integration_wrapper, get_cosmologies
from conversion_functions import convert_unit
constants = get_constants()
cosmologies = get_cosmologies('Planck-2018')
@dataclass
class distanceData:
"""
Class that computes distance measures from the given input parameters.
"""
redshift: float
H0: float #= cosmologies['Hubble0']
ΩM: float #= cosmologies['matter-density']
ΩDE: float# = cosmologies['DE-density']
ΩR: float #= cosmologies['rad-density']
w0: float #= cosmologies['w0']
wa: float #= cosmologies['wa']
comoving_distance: float = field(init = False)
transverse_comoving_distance: float = field(init = False)
angular_diameter_distance: float = field(init = False)
luminosity_distance: float = field(init = False)
comoving_volume: float = field(init = False)
lookback_time: float = field(init = False)
proper_separation: float = field(init = False)
def __post_init__(self):
self.comoving_distance = get_comoving_distance(self.redshift, self.H0, self.ΩM, self.ΩDE,
self.ΩR, self.w0, self.wa)
self.transverse_comoving_distance = get_transverse_comoving_distance(self.redshift, self.H0,
self.ΩM, self.ΩDE,
self.ΩR, self.w0, self.wa)
self.angular_diameter_distance = get_angular_diameter_distance(self.redshift, self.H0, self.ΩM,
self.ΩDE, self.ΩR, self.w0, self.wa)
self.luminosity_distance = get_luminosity_distance(self.redshift, self.H0, self.ΩM, self.ΩDE,
self.ΩR, self.w0, self.wa)
self.comoving_volume = 1e-9*get_comoving_volume(self.redshift, self.H0, self.ΩM, self.ΩDE,
self.ΩR, self.w0, self.wa)
self.proper_separation = get_proper_separation(convert_unit(1, "arcsec", "radian"),
self.redshift, self.H0, self.ΩM,
self.ΩDE, self.ΩR, self.w0, self.wa)
self.lookback_time = get_lookback_time(self.redshift, self.H0, self.ΩM, self.ΩDE,
self.ΩR, self.w0, self.wa)
def get_E_z(z, ΩM=cosmologies['matter-density'], ΩDE=cosmologies['DE-density'],
ΩR=cosmologies['rad-density'], w0=cosmologies['w0'], wa=cosmologies['wa']):
"""
Method to compute the adimensional Hubble rate in the w0waCDm cosmology
"""
_ = check_redshift_valid_array(z)
ΩK = 1 - ΩM - ΩDE - ΩR
return np.sqrt(ΩM*(1+z)**3+ΩR*(1+z)**4+ΩDE*(1+z)**(3*(1+w0+wa))*np.exp(-3*wa*z/(1+z))+ΩK*(1+z)**2)
def get_H_z(z, H0=cosmologies['Hubble0'], ΩM=cosmologies['matter-density'],
ΩDE=cosmologies['DE-density'], ΩR=cosmologies['rad-density'],
w0=cosmologies['w0'], wa=cosmologies['wa']):
"""
Method to compute the Hubble rate in the w0waCDm cosmology
"""
_ = check_redshift_valid_array(z)
return H0*get_E_z(z, ΩM, ΩDE, ΩR, w0, wa)
def get_comoving_distance(z, H0=cosmologies['Hubble0'], ΩM=cosmologies['matter-density'],
ΩDE=cosmologies['DE-density'], ΩR=cosmologies['rad-density'],
w0=cosmologies['w0'], wa=cosmologies['wa']):
"""
Method to compute the comoving distance
"""
integrand = lambda x: 1/get_E_z(x, ΩM, ΩDE, ΩR, w0, wa)
is_array = check_redshift_valid_array(z)
if is_array:
result = np.vectorize(lambda x: integration_wrapper(integrand, x))(z)
else:
result = integration_wrapper(integrand, z)
c0 = constants['speed-of-light']
return c0/H0*result
def get_transverse_comoving_distance(z, H0=cosmologies['Hubble0'], ΩM=cosmologies['matter-density'],
ΩDE=cosmologies['DE-density'], ΩR=cosmologies['rad-density'],
w0=cosmologies['w0'], wa=cosmologies['wa']):
"""
Compute the transverse comoving distance
"""
_ = check_redshift_valid_array(z)
D_c = get_comoving_distance(z, H0, ΩM, ΩDE, ΩR, w0, wa)
ΩK = 1 - ΩM - ΩDE - ΩR
c0 = constants['speed-of-light']
x = np.sqrt(-ΩK + 0j) * D_c / (c0 / H0)
if ΩDE == 0:
D_m = c0 / H0 * 2 * (2 - ΩM(1-z) - (2-ΩM)*np.sqrt(1+ΩM*z)) / (ΩM**2*(1+z))
else:
D_m = D_c * np.sinc(x / np.pi)
#if ΩK >= 0:
# D_m = D_c * np.sinh(x) / x
#if ΩK <= 0:
# D_m = D_c * np.sin(x) / x
return D_m.real
def get_angular_diameter_distance(z, H0=cosmologies['Hubble0'], ΩM=cosmologies['matter-density'],
ΩDE=cosmologies['DE-density'], ΩR=cosmologies['rad-density'],
w0=cosmologies['w0'], wa=cosmologies['wa']):
"""
Compute the angular diameter distance
"""
return get_transverse_comoving_distance(z, H0, ΩM, ΩDE, ΩR, w0, wa) / (1 + z)
def get_luminosity_distance(z, H0=cosmologies['Hubble0'], ΩM=cosmologies['matter-density'],
ΩDE=cosmologies['DE-density'], ΩR=cosmologies['rad-density'],
w0=cosmologies['w0'], wa=cosmologies['wa']):
'''
Compute the angular diameter distance d_l
'''
d_l = get_transverse_comoving_distance(z, H0, ΩM, ΩDE, ΩR, w0, wa) * (1 + z)
return d_l
def get_comoving_volume(z, H0=cosmologies['Hubble0'], ΩM=cosmologies['matter-density'],
ΩDE=cosmologies['DE-density'], ΩR=cosmologies['rad-density'],
w0=cosmologies['w0'], wa=cosmologies['wa']):
"""
Compute the comoving volume
"""
c0 = constants['speed-of-light']
Dm = get_transverse_comoving_distance(z, H0, ΩM, ΩDE, ΩR, w0, wa)
Dh = c0 / H0
ΩK = round(1 - ΩM - ΩDE - ΩR, 7)
# the rounding is necessary since the expression is quite sensitive to the Universe geometry and,
# due to the floating point precision, we are nver gonna really obtain ΩK = 0
if ΩK > 0:
return (4*np.pi*Dh**3/(2*ΩK))*(Dm/Dh*np.sqrt(1+ΩK*(Dm/Dh)**2)-1/np.sqrt(ΩK)*np.arcsinh(np.sqrt(ΩK)*Dm/Dh))
elif ΩK < 0:
return (4*np.pi*Dh**3/(2*ΩK))*(Dm/Dh*np.sqrt(1+ΩK*(Dm/Dh)**2)-1/np.sqrt(-ΩK)*np.arcsin(np.sqrt(-ΩK)*Dm/Dh))
else:
return 4*np.pi/3*Dm**3
def hubble_time(H0=cosmologies['Hubble0']):
"""
Method to compute the Hubble time in Gyrs
"""
return 1e-9 * convert_unit(1, "megaparsec", "kilometer") * convert_unit(1, "second", "year") / H0
def get_lookback_time(z, H0=cosmologies['Hubble0'], ΩM=cosmologies['matter-density'],
ΩDE=cosmologies['DE-density'], ΩR=cosmologies['rad-density'],
w0=cosmologies['w0'], wa=cosmologies['wa']):
"""
Method to compute the lookback time in Gyrs
"""
integrand = lambda x: 1/(get_E_z(x, ΩM, ΩDE, ΩR, w0, wa)*(1+x))
is_array = check_redshift_valid_array(z)
if is_array:
result = np.vectorize(lambda x: integration_wrapper(integrand, x))(z)
else:
result = integration_wrapper(integrand, z)
c0 = constants['speed-of-light']
return result*hubble_time(H0)
def calculate_distance_modulus(d):
'''
Method to compute the distance modulus for a given distance.
Distance modulus mu is defined as the difference between the apparent magnitude m and
the absolute magnitude M of an astronomical object, mu = m - M.
The distance modulus is then: mu = m - M = 5 * log10(d/{10 pc})
Args: distance d in parsecs
Returns: the distance modulus
'''
mu = 5 * np.log10(d/10)
return mu
def calculate_absolute_magnitude_from_distance_modulus(m, d):
'''
Method to compute the absolute magnitude M from the distance modulus
given the apparent magnitude m and distance d (in parsecs).
Args: apparent magnitude m and distance d (in parsecs)
Returns: the absolute magnitude M
'''
M = m - 5 * np.log10(d/10)
return M
def calculate_apparent_magnitude_from_distance_modulus(M, d):
'''
Method to compute the apparent magnitude m from the distance modulus
given the absolute magnitude M and distance d (in parsecs).
Args: absolute magnitude M and distance d (in parsecs)
Returns: the apparent magnitude m
'''
m = M + 5 * np.log10(d/10)
return m
def calculate_distance_from_distance_modulus_for_given_nu(nu):
'''
Method to compute distance d (in parsecs) from distance modulus nu.
Args: distance modulus mu (dimensionless)
Returns: distance in parsecs
'''
d = 10**((nu/5) + 1)
return d
def calculate_distance_from_distance_modulus_for_given_nu(nu):
'''
Method to compute distance d (in parsecs) from distance modulus nu.
Args: distance modulus mu (dimensionless)
Returns: distance in parsecs
'''
d = 10**((nu/5) + 1)
return d
def calculate_distance_from_distance_modulus_for_given_M_and_m(M, m):
'''
Method to compute the distance d (in parsecs) from distance modulus given
the absolute and apparent magnitudes.
Args: absolute magnitude M and apparent magnitude m
Returns: distance in parsecs
'''
d = 10**((m-M)/5 + 1)
return d
def hubble_distance(H0=cosmologies['Hubble0']):
DH = c/H0
return DH
def get_proper_separation(θ, z, H0=cosmologies['Hubble0'], ΩM=cosmologies['matter-density'],
ΩDE=cosmologies['DE-density'], ΩR=cosmologies['rad-density'],
w0=cosmologies['w0'], wa=cosmologies['wa']):
'''
Computes the spatial separation of a distant object
Args:
angular separation θ,
redshift z,
Hubble constant H0,
energy densities ΩM, ΩDE, ΩR
EOS parameters w0, wa
Returns: spatial separation in kpc
'''
return 1e3 * np.tan(θ) * get_angular_diameter_distance(z, H0, ΩM, ΩDE, ΩR, w0, wa)