-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmass_function.py
564 lines (469 loc) · 19.8 KB
/
mass_function.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import cosmology
import defaults
import numpy
from scipy import integrate
from scipy.interpolate import InterpolatedUnivariateSpline
"""Classes for encoding basic cosmological parameters and quantities.
Given a cosmology, we can derive a mass function object. This should be able
to return the abundance of halo at a given mass as well as the mean halo bias.
Likewise, these quantities should be available as a function of nu:
nu = (delta_c/sigma(M))^2
This is the ratio of the over-density necessary for linear, spherical
collapse and the RMS density fluctuations as a function of mass. M*, the mass
at which nu == 1, represents the break between halos that are mostly collapsed
and those where fluctuations large enough for collapse are exponentially
suppressed.
"""
__author__ = ("Chris Morrison <[email protected]>"+
"Ryan Scranton <[email protected]>")
class MassFunction(object):
"""Object representing a mass function for a given input cosmology.
A MassFunction object can return a properly normalized halo abundance or
halo bias as a function of halo mass or as a function of nu, as well as
translate between mass and nu. Current definition is from Sheth & Torman
Attributes:
redshift: float redshift at which to compute the mass function
cosmo_single_epoch: SingleEpoch cosmology object from cosmology.py
halo_dict: dictionary of floats defining halo and mass function
parameters (see defualts.py for details)
"""
def __init__(self, redshift=0.0, cosmo_single_epoch=None,
halo_dict=None, **kws):
self._redshift = redshift
#self.cosmo = cosmology.SingleEpoch(self._redshift, cosmo_dict)
if cosmo_single_epoch is None:
cosmo_single_epoch = cosmology.SingleEpoch(self._redshift)
self.cosmo = cosmo_single_epoch
self.cosmo.set_redshift(self._redshift)
self.delta_c = self.cosmo.delta_c()
if halo_dict is None:
halo_dict = defaults.default_halo_dict
self.halo_dict = halo_dict
self.delta_v = self.halo_dict['delta_v']
if self.delta_v == -1:
self.delta_v = self.cosmo.delta_v()
self.stq = halo_dict["stq"]
self.st_little_a = halo_dict["st_little_a"]
self.c0 = halo_dict["c0"]/(1.0 + redshift)
self._set_mass_limits()
self._initialize_splines()
self._normalize()
def get_redshift(self):
"""
Return the internal redshift valriable.
"""
return self._redshift
def set_redshift(self, redshift):
"""
Reset mass function parameters at redshift.
Args:
redshift: float value of redshift
cosmo_dict: dictionary of floats defining a cosmology (see
defaults.py for details)
"""
self._redshift = redshift
self.cosmo.set_redshift(redshift)
self.delta_c = self.cosmo.delta_c()
self.c0 = self.halo_dict["c0"]/(1.0 + redshift)
self.delta_v = self.halo_dict['delta_v']
if self.delta_v == -1:
self.delta_v = self.cosmo.delta_v()
self._set_mass_limits()
self._initialize_splines()
self._normalize()
def get_cosmology(self):
"""
Return the internal cosmology dictionary.
"""
return self.cosmo.get_cosmology()
def set_cosmology(self, cosmo_dict, redshift = None):
"""
Reset mass function parameters for cosmology cosmo_dict.
Args:
cosmo_dict: dictionary of floats defining a cosmology (see
defaults.py for details)
redshift: float value of redshift
"""
if redshift is None:
redshift = self._redshift
self.cosmo.set_cosmology(cosmo_dict, redshift)
self.delta_c = self.cosmo.delta_c()
self.delta_v = self.halo_dict['delta_v']
if self.delta_v == -1:
self.delta_v = self.cosmo.delta_v()
self.c0 = self.halo_dict["c0"]/(1.0 + redshift)
self._set_mass_limits()
self._initialize_splines()
self._normalize()
def set_cosmology_object(self, cosmo_single_epoch):
self._redshift = cosmo_single_epoch.redshift()
self.cosmo = cosmo_single_epoch
self.delta_c = self.cosmo.delta_c()
self.delta_v = self.halo_dict['delta_v']
if self.delta_v == -1:
self.delta_v = self.cosmo.delta_v()
self.c0 = self.halo_dict["c0"]/(1.0 + self._redshift)
self._set_mass_limits()
self._initialize_splines()
self._normalize()
def get_halo(self):
"""
Return the internal dictionary defining a halo.
"""
return self.halo_dict
def set_halo(self, halo_dict):
"""
Reset mass function parameters for halo_dict.
Args:
halo_dict: dictionary of floats defining halos (see
defaults.py for details)
"""
self.halo_dict = halo_dict
self.stq = self.halo_dict["stq"]
self.st_little_a = self.halo_dict["st_little_a"]
self.c0 = self.halo_dict["c0"]/(1.0 + self._redshift)
self.delta_v = self.halo_dict['delta_v']
if self.delta_v == -1:
self.delta_v = self.cosmo.delta_v()
self._normalize()
def _set_mass_limits(self):
mass_min = 1.0e9
mass_max = 1.0e16
if (defaults.default_limits["mass_min"] > 0 and
defaults.default_limits["mass_max"] > 0):
self.ln_mass_min = numpy.log(defaults.default_limits["mass_min"])
self.ln_mass_max = numpy.log(defaults.default_limits["mass_max"])
self._ln_mass_array = numpy.linspace(
self.ln_mass_min, self.ln_mass_max,
defaults.default_precision["mass_npoints"])
return None
mass_limit_not_set = True
while mass_limit_not_set:
if 0.1*(1.0+0.05) < self.cosmo.nu_m(mass_min):
#print "Min mass", mass_min,"too high..."
mass_min = mass_min/1.05
#print "\tSetting to",mass_min,"..."
continue
elif 0.1*(1.0-0.05) > self.cosmo.nu_m(mass_min):
#print "Min mass", mass_min,"too low..."
mass_min = mass_min*1.05
#print "\tSetting to",mass_min,"..."
continue
if 50.0*(1.0-0.05) > self.cosmo.nu_m(mass_max):
#print "Max mass", mass_max,"too low..."
mass_max = mass_max*1.05
#print "\tSetting to",mass_max,"..."
continue
elif 50.0*(1.0+0.05) < self.cosmo.nu_m(mass_max):
#print "Max mass", mass_max,"too high..."
mass_max = mass_max/1.05
#print "\tSetting to",mass_max,"..."
continue
mass_limit_not_set = False
#print "Mass Limits:",mass_min*(0.95),"-",mass_max*(1.05)
self.ln_mass_min = numpy.log(mass_min)
self.ln_mass_max = numpy.log(mass_max)
self._ln_mass_array = numpy.linspace(
self.ln_mass_min, self.ln_mass_max,
defaults.default_precision["mass_npoints"])
def _initialize_splines(self):
self._nu_array = numpy.zeros_like(self._ln_mass_array)
for idx in xrange(self._ln_mass_array.size):
mass = numpy.exp(self._ln_mass_array[idx])
self._nu_array[idx] = self.cosmo.nu_m(mass)
self.nu_min = 1.001*self._nu_array[0]
self.nu_max = 0.999*self._nu_array[-1]
#print "nu_min:",self.nu_min,"nu_max:",self.nu_max
self._nu_spline = InterpolatedUnivariateSpline(
self._ln_mass_array, self._nu_array)
self._ln_mass_spline = InterpolatedUnivariateSpline(
self._nu_array, self._ln_mass_array)
# Set M_star, the mass for which nu == 1
self.m_star = self.mass(1.0)
def _normalize(self):
self.f_norm = 1.0
norm = integrate.romberg(
self.f_nu, self.nu_min, self.nu_max, vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["mass_precision"],
divmax=defaults.default_precision["divmax"])
self.f_norm = 1.0/norm
self.bias_norm = 1.0
norm = integrate.romberg(
lambda x: self.f_nu(x)*self.bias_nu(x),
self.nu_min, self.nu_max, vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["mass_precision"],
divmax=defaults.default_precision["divmax"])
self.bias_norm = 1.0/norm
def f_nu(self, nu):
"""
Halo mass function as a function of normalized mass over-density nu
Args:
nu: float array normalized mass over-density nu
Returns:
float array number of halos
"""
nu_prime = nu*self.st_little_a
return (
self.f_norm*(1.0 + nu_prime**(-1.0*self.stq))*
numpy.sqrt(nu_prime)*numpy.exp(-0.5*nu_prime)/nu)
def f_m(self, mass):
"""
Halo mass function as a function of halo mass
Args:
mass: float array halo mass
Returns:
float array number of halos
"""
return self.f_nu(self.nu(mass))
def dndm(self, mass):
"""
Convenience function for computing the number of halos per mass.
Args:
mass: float value or array of halo mass in M_solar/h
Returns:
float value or array number of halos per mass per (Mpc/h)^3
"""
try:
_dndm = numpy.empty(len(mass))
for idx, m in enumerate(mass):
_dndm[idx] = 0.5*(self.cosmo.rho_bar()/(m*m)*
self.f_m(m)*
self._nu_spline.derivatives(numpy.log(m))[1])
return _dndm
except TypeError:
return 0.5(self.cosmo.rho_bar()/(mass*mass)*
self.f_m(mass)*
self._nu_spline.derivatives(numpy.log(mass))[1])
def bias_nu(self, nu):
"""
Halo bias as a function of nu.
Args:
mass: float array mass over-density mu
Returns:
float array halo bias
"""
nu_prime = nu*self.st_little_a
return self.bias_norm*(
1.0 + (nu_prime - 1.0)/self.delta_c +
2.0*self.stq/(self.delta_c*(1.0 + nu_prime**self.stq)))
def bias_m(self, mass):
"""
Halo bias as a function of mass.
Args:
mass: float array halo mass
Returns:
float array halo bias
"""
return self.bias_nu(self.nu(mass))
def nu(self, mass):
"""
nu as a function of halo mass.
Args:
nu: float array mass M [M_Solar]
Returns:
float array
"""
return self._nu_spline(numpy.log(mass))
def ln_mass(self, nu):
"""
Natural log of halo mass as a function of nu.
Args:
nu: float array normalized mass over-density
Returns:
float array natural log mass [M_Solar]
"""
return self._ln_mass_spline(nu)
def mass(self, nu):
"""
Halo mass as a function of nu.
Args:
nu: float array normalized mass over-density
Returns:
float array halo mass [M_Solar]
"""
return numpy.exp(self.ln_mass(nu))
def write(self, output_file_name):
"""
Write current mass function values
Args:
output_file_name: string file name to write mass function parameters
"""
print "M* = 10^%1.4f M_sun" % numpy.log10(self.m_star)
output_file = open(output_file_name, "w")
output_file.write("#ttype1 = mass [M_solar/h]\n#ttype2 = nu\n"
"#ttype3 = f(nu)\n#ttype4 = bias(nu)\n")
for ln_mass, nu, in zip(self._ln_mass_array, self._nu_array):
output_file.write("%1.10f %1.10f %1.10f %1.10f\n" % (
numpy.exp(ln_mass), nu, self.f_nu(nu), self.bias_nu(nu)))
output_file.close()
class MassFunctionSecondOrder(MassFunction):
def __init__(self, redshift=0.0, cosmo_single_epoch=None,
halo_dict=None, **kws):
MassFunction.__init__(self, redshift, cosmo_single_epoch,
halo_dict, **kws)
def _initialize_splines(self):
self._nu_array = numpy.zeros_like(self._ln_mass_array)
self._sigma_array = numpy.zeros_like(self._ln_mass_array)
for idx in xrange(self._ln_mass_array.size):
mass = numpy.exp(self._ln_mass_array[idx])
sigma = self.cosmo.sigma_m(mass)
self._sigma_array[idx] = sigma
self._nu_array[idx] = self.delta_c/sigma*self.delta_c/sigma
self.nu_min = 1.001*self._nu_array[0]
self.nu_max = 0.999*self._nu_array[-1]
#print "nu_min:",self.nu_min,"nu_max:",self.nu_max
self._nu_spline = InterpolatedUnivariateSpline(
self._ln_mass_array, self._nu_array)
self._ln_mass_spline = InterpolatedUnivariateSpline(
self._nu_array, self._ln_mass_array)
self._sigma_spline = InterpolatedUnivariateSpline(
self._nu_array, self._sigma_array)
# Set M_star, the mass for which nu == 1
self.m_star = self.mass(1.0)
def _normalize(self):
self.f_norm = 1.0
norm = integrate.romberg(
self.f_nu, self.nu_min, self.nu_max, vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["mass_precision"],
divmax=defaults.default_precision["divmax"])
self.f_norm = 1.0/norm
self.bias_norm = 1.0
norm = integrate.romberg(
lambda x: self.f_nu(x)*self.bias_nu(x),
self.nu_min, self.nu_max, vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["mass_precision"],
divmax=defaults.default_precision["divmax"])
self.bias_norm = 1.0/norm
self.bias_2_norm = 0.0
norm = integrate.romberg(
lambda x: self.f_nu(x)*self.bias_2_nu(x),
self.nu_min, self.nu_max, vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["mass_precision"],
divmax=defaults.default_precision["divmax"])
self.bias_2_norm = -norm
def bias_2_nu(self, nu):
sigma = self._sigma_spline(nu)
nu_prime = nu*self.st_little_a
return self.bias_2_norm + (
8.0/21.0 * (self.bias_nu(nu) - 1.0) + (nu - 3.0)/(sigma*sigma) +
2.0*self.stq/(self.delta_c**2 *(1.0 + nu_prime**self.stq)) * (
2.0*self.stq + 2*nu_prime - 1.0))
def bias_2_mass(self, mass):
return self.bias_2_nu(self.nu(mass))
class TinkerMassFunction(MassFunction):
"""
Derived mass function expressing the functional form from Tinker et al.
2010.
Attributes:
redshift: float redshift at which to compute the mass function
cosmo_single_epoch: SingleEpoch cosmology object from cosmology.py
halo_dict: dictionary of floats defining halo and mass function
parameters (see defualts.py for details)
"""
def __init__(self, redshift=0.0, cosmo_single_epoch=None,
halo_dict=None, **kws):
delta_list = [200, 300, 400, 600, 800, 1200, 1600, 2400, 3200]
alpha_list = [0.368, 0.363, 0.385, 0.389, 0.393,
0.365, 0.379, 0.355, 0.327]
beta_list = [0.589, 0.585, 0.544, 0.543, 0.564,
0.632, 0.637, 0.673, 0.702]
gamma_list = [0.864, 0.922, 0.987, 1.09, 1.20, 1.34, 1.50, 1.68, 1.81]
phi_list = [-0.729, -0.789, -0.910, -1.05,
-1.20, -1.26, -1.45, -1.50, -1.49]
eta_list = [-0.243, -0.261, -0.261, -0.273,
-0.278, -0.301, -0.301, -0.319, -0.336]
self._alpha0_spline = InterpolatedUnivariateSpline(
numpy.log(delta_list), alpha_list)
self._beta0_spline = InterpolatedUnivariateSpline(
numpy.log(delta_list), beta_list)
self._gamma0_spline = InterpolatedUnivariateSpline(
numpy.log(delta_list), gamma_list)
self._phi0_spline = InterpolatedUnivariateSpline(
numpy.log(delta_list), phi_list)
self._eta0_spline = InterpolatedUnivariateSpline(
numpy.log(delta_list), eta_list)
self._k_min = 0.001
self._k_max = 100.0
self._redshift = redshift
#self.cosmo = cosmology.SingleEpoch(self._redshift, cosmo_dict)
if cosmo_single_epoch is None:
cosmo_single_epoch = cosmology.SingleEpoch(self._redshift)
self.cosmo = cosmo_single_epoch
self.cosmo.set_redshift(self._redshift)
self.delta_c = self.cosmo.delta_c()
if halo_dict is None:
halo_dict = defaults.default_halo_dict
self.halo_dict = halo_dict
self.delta_v = self.halo_dict['delta_v']
if self.delta_v == -1:
self.delta_v = self.cosmo.delta_v()
self._set_mass_limits()
self._initialize_splines()
self._normalize()
def f_nu(self, nu):
"""
Halo mass function as a function of normalized mass over-density nu.
Note: Tinker2010 defines nu as [delta_c/sigma] where our definition
is the square of that.
Args:
nu: float array normalized mass over-density nu
Returns:
float array number of halos
"""
sqrtnu = numpy.sqrt(nu)
return (self._alpha()*(
1 + numpy.power(self._beta()*sqrtnu,-2*self._phi()))*
numpy.power(nu, self._eta())*
numpy.exp(-self._gamma()*nu/2.0)/sqrtnu)
def bias_nu(self, nu):
"""
Halo bias as a function of nu. Note: Tinker2010 defines nu as
[delta_c/sigma] where our definition is the square of that.
Args:
mass: float array mass over-density mu
Returns:
float array halo bias
"""
sqrtnu = numpy.sqrt(nu)
y = numpy.log10(self.delta_v)
A = 1 + 0.24*y*numpy.exp(-(4.0/y)**4)
a = 0.44*y - 0.88
B = 0.183
b = 1.5
C = 0.019 + 0.107*y + 0.19*numpy.exp(-(4.0/y)**4)
c = 2.4
return self.bias_norm*(1 - A*sqrtnu**a/(sqrtnu**a + self.delta_c**a) +
B*sqrtnu**b + C*sqrtnu**c)
def _normalize(self):
"""
The Tinker mass function does not require normalization of the mass
function proper as this amplitude is fit for. It does however require
the bias to be normalized.
"""
self.bias_norm = 1.0
norm = integrate.romberg(
lambda x: self.f_nu(x)*self.bias_nu(x),
self.nu_min, self.nu_max, vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["mass_precision"],
divmax=defaults.default_precision["divmax"])
self.bias_norm = 1.0/norm
def _alpha(self):
return self._alpha0_spline(numpy.log(self.delta_v))
def _beta(self):
return self._beta0_spline(numpy.log(self.delta_v))*numpy.power(
1 + self._redshift, 0.20)
def _phi(self):
return self._phi0_spline(numpy.log(self.delta_v))*numpy.power(
1 + self._redshift, -0.08)
def _eta(self):
return self._eta0_spline(numpy.log(self.delta_v))*numpy.power(
1 + self._redshift, 0.27)
def _gamma(self):
return self._gamma0_spline(numpy.log(self.delta_v))*numpy.power(
1 + self._redshift, -0.01)