-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhalo_utilities.py
279 lines (201 loc) · 6.28 KB
/
halo_utilities.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
278
"""
cosmology utility functions relating to dark matter haloes
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
from astropy import units as u
from astropy import constants as const
from .default_cosmo import default_cosmo # define a default cosology for utilities
__all__=('delta_vir', 'virial_radius', 'virial_halo_mass',
'halo_mass_conversion', 'dynamical_time')
__author__=('Duncan Campbell')
def delta_vir(z, cosmo='default', wrt='background'):
"""
The average over-density of a collapsed dark matter halo.
fitting function from Bryan & Norman (1998)
Paramaters
----------
z : array_like
redshift
cosmo : astropy.cosmology object
Returns
-------
delta_vir : numpy.array
average density with respect to the mean density of the Universe
"""
if cosmo=='default':
cosmo = default_cosmo
z = np.atleast_1d(z)
x = cosmo.Om(z)-1.0
if wrt=='critical':
return (18.0*np.pi**2 + 82.0*x - 39.0*x**2)
elif wrt=='background':
return (18.0*np.pi**2 + 82.0*x - 39.0*x**2)/cosmo.Om(z)
def virial_radius(m, z=0.0, cosmo='default'):
"""
The virial radius of a collapsed dark matter halo
Paramaters
----------
m : array_like
halo mass
z : array_like
redshift
cosmo : astropy.cosmology object
Returns
-------
r_vir : numpy.array
virial radius in h^{-1}Mpc
"""
if cosmo=='default':
cosmo = default_cosmo
z = np.atleast_1d(z)
m = (np.atleast_1d(m)*u.Msun)/cosmo.h
dvir = delta_vir(z, cosmo, wrt='background')
r = (m / ((4.0/3.0)*np.pi*dvir*mean_density(z, cosmo)))**(1.0/3.0)
r = r.to(u.Mpc)*cosmo.h
return r.value
def virial_halo_mass(m_h, c, delta_h=200, z=0.0, cosmo='default',
wrt='background'):
"""
Convert halo mass to virial halo mass
fitting function from Hu \& Kravtsov (2003).
Parameters
----------
m_h : array_like
halo mass
c : array_like
concentration
delta_h : float
density contrast
delta_vir : float, optional
virial over-density wrt the mean density. If given, cosmology is ignored
when calculating delta_vir.
cosmo : astropy.cosmology object, optional
cosmology used to calculated the virial over-density
wrt : string
halo over-density wrt respect to the
'background' density or 'critical' density
Returns
-------
m_vir : array_like
virial halo mass in h^{-1}M_{\odot}
"""
m_h = np.atleast_1d(m_h)
c = np.atleast_1d(c)
z = np.atleast_1d(z)
if cosmo=='default':
cosmo = default_cosmo
if wrt=='background':
pass
elif wrt=='critical':
delta_h = delta_h/cosmo.Om(z)
else:
msg = 'mean density wrt paramater not recognized.'
raise ValueError(msg)
def f_x(x):
"""
eq. C3
"""
return x**3*(np.log(1.0+1.0/x)-1.0/(1.0+x))
def x_f(f):
"""
fitting function to inverse of f_x, eq. C11
"""
a1 = 0.5116
a2 = -0.4283
a3 = -3.13*10**(-3)
a4 = -3.52*10**(-5)
p = a2 +a3*np.log(f) + a4*np.log(f)**2
return (a1*f**(2.0*p) + (3/4)**2)**(-0.5) + 2.0*f
f_h = delta_h/delta_vir(z, cosmo)
r_ratio = x_f(f_h*f_x(1.0/c))
f = (f_h)*r_ratio**(-3)*(1.0/c)**3.0
return m_h/f
def halo_mass_conversion(m_h, c, delta_h=200, z=0.0, cosmo='default',
wrt_h='background', delta_new=360.0, wrt_new='background'):
"""
Converrt between halo mass definitions
fitting function from Hu \& Kravtsov (2003).
Parameters
----------
m_h : array_like
halo mass
c : array_like
concentration
delta_h : float
over-density
cosmo : astropy.cosmology object, optional
cosmology used to calculated the virial over-density
wrt_h : string
halo over-density wrt respect to the
'background' density or 'critical' density
delta_new : float
convert to over-density
wrt_new : string
covert to halo over-density wrt respect to the
'background' density or 'critical' density
Returns
-------
m_vir : array_like
virial halo mass in h^{-1}M_{\odot}
"""
m_h = np.atleast_1d(m_h)
c = np.atleast_1d(c)
z = np.atleast_1d(z)
if cosmo=='default':
cosmo = default_cosmo
if wrt_h=='background':
pass
elif wrt_h=='critical':
delta_h = delta_h/cosmo.Om(z)
else:
msg = 'mean density wrt paramater not recognized.'
raise ValueError(msg)
if wrt_new=='background':
pass
elif wrt_new=='critical':
delta_new = delta_new/cosmo.Om(z)
else:
msg = 'mean density wrt paramater not recognized.'
raise ValueError(msg)
def f_x(x):
"""
eq. C3
"""
return x**3*(np.log(1.0+1.0/x)-1.0/(1.0+x))
def x_f(f):
"""
fitting function to inverse of f_x, eq. C11
"""
a1 = 0.5116
a2 = -0.4283
a3 = -3.13*10**(-3)
a4 = -3.52*10**(-5)
p = a2 +a3*np.log(f) + a4*np.log(f)**2
return (a1*f**(2.0*p) + (3/4)**2)**(-0.5) + 2.0*f
f_h = delta_h/delta_vir(z, cosmo)
r_ratio = x_f(f_h*f_x(1.0/c))
f_1 = (f_h)*r_ratio**(-3)*(1.0/c)**3.0
f_h = delta_new/delta_vir(z, cosmo)
r_ratio = x_f(f_h*f_x(1.0/c))
f_2 = (f_h)*r_ratio**(-3)*(1.0/c)**3.0
f = f_1/f_2
return m_h/f
def dynamical_time(z, cosmo='default'):
"""
dynamical time
Paramaters
----------
z : array_like
redshift
cosmo : astropy.cosmology object
Returns
-------
t : array_like
dynamical time at redshift z h^-1 Gyr
"""
if cosmo=='default':
cosmo = default_cosmo
z = np.atleast_1d(z)
t_dyn = 1.628*((delta_vir(z, cosmo)/178.0)**(-0.5)) * ((cosmo.H(z)/cosmo.H0)**(-1.0))
return t_dyn.value