LALInference  4.1.6.1-89842e6
bayestar_cosmology.py
Go to the documentation of this file.
1 #
2 # Copyright (C) 2017 Leo Singer
3 #
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the
6 # Free Software Foundation; either version 2 of the License, or (at your
7 # option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 # Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 #
18 from __future__ import print_function
19 """
20 Generate Chebyshev coefficients for BAYESTAR uniform-in-comoving-volume prior.
21 """
22 import argparse
23 import os
24 import astropy.cosmology
25 import astropy.units as u
26 import numpy as np
27 import scipy.misc
28 
29 parser = argparse.ArgumentParser()
30 parser.add_argument(
31  'cosmology', choices=astropy.cosmology.parameters.available,
32  default='WMAP9', nargs='?', help='Cosmological model')
33 args = parser.parse_args()
34 
35 cosmo = astropy.cosmology.default_cosmology.get_cosmology_from_string(
36  args.cosmology)
37 
38 
39 def dVC_dVL_for_z(z):
40  """Ratio between the comoving volume element dVC and a
41  DL**2 prior at redshift z."""
42  Ok0 = cosmo.Ok0
43  DH = cosmo.hubble_distance
44  DM_by_DH = (cosmo.comoving_transverse_distance(z) / DH).value
45  DC_by_DH = (cosmo.comoving_distance(z) / DH).value
46  zplus1 = z + 1.0
47  if Ok0 == 0.0:
48  ret = 1.0
49  elif Ok0 > 0.0:
50  ret = np.cosh(np.sqrt(Ok0) * DC_by_DH)
51  else: # Ok0 < 0.0 or Ok0 is nan
52  ret = np.cos(np.sqrt(-Ok0) * DC_by_DH)
53  ret *= zplus1
54  ret += DM_by_DH * cosmo.efunc(z)
55  ret *= np.square(zplus1)
56  return 1.0 / ret
57 
58 
59 @np.vectorize
60 def z_for_DL(DL):
61  return astropy.cosmology.z_at_value(
62  cosmo.luminosity_distance, DL * u.Mpc)
63 
64 
66  return dVC_dVL_for_z(z_for_DL(DL))
67 
68 
69 DL = np.logspace(0, 6, 32)
70 log_DL = np.log(DL)
71 dVC_dVL = dVC_dVL_for_DL(DL)
72 log_dVC_dVL = np.log(dVC_dVL)
73 
74 
75 def func(x):
76  return np.log(dVC_dVL_for_DL(np.exp(x)))
77 
78 
79 high_z_x0 = np.log(1e6)
80 high_z_y0 = func(high_z_x0)
81 high_z_slope = scipy.misc.derivative(func, high_z_x0)
82 high_z_intercept = high_z_y0 - high_z_slope * high_z_x0
83 
84 filename = os.path.basename(__file__)
85 print('/* DO NOT EDIT. Automatically generated by', filename, '*/')
86 print('static const double dVC_dVL_data[] = {')
87 print(*('\t{:+.8e}'.format(c) for c in log_dVC_dVL), sep=',\n')
88 print('};')
89 print('static const double dVC_dVL_tmin = {:.15f};'.format(log_DL[0]))
90 print('static const double dVC_dVL_tmax = {:.15f};'.format(log_DL[-1]))
91 print('static const double dVC_dVL_dt = {:.15f};'.format(
92  np.diff(log_DL)[0]))
93 print('static const double dVC_dVL_high_z_slope = {:.15f};'.format(
94  high_z_slope))
95 print('static const double dVC_dVL_high_z_intercept = {:.15f};'.format(
96  high_z_intercept))
97 
98 
99 def exact_dVC_dVL(DL):
100  """An alternate expression of dVC_dVL_for_DL."""
101  z = z_for_DL(DL)
102  DL = DL * u.Mpc
103  DH = cosmo.hubble_distance
104  DC = cosmo.comoving_distance(z)
105  DM = cosmo.comoving_transverse_distance(z)
106  dVC_dz = cosmo.differential_comoving_volume(z)
107 
108  Ok0 = cosmo.Ok0
109  if Ok0 == 0.0:
110  dDM_dDC = 1.0
111  elif Ok0 > 0.0:
112  dDM_dDC = np.cosh(np.sqrt(Ok0) * DC / DH)
113  else: # Ok0 < 0.0 or Ok0 is nan
114  dDM_dDC = np.cos(np.sqrt(-Ok0) * DC / DH)
115 
116  dDC_dz = DH * cosmo.inv_efunc(z)
117  dDL_dz = DM + (1 + z) * dDM_dDC * dDC_dz
118  dVL_dz = np.square(DL) * dDL_dz / u.sr
119 
120  return dVC_dz / dVL_dz
121 
122 
123 DL = np.logspace(-2, 6, 1000)
124 dVC_dVL = exact_dVC_dVL(DL)
125 
126 print('static const double dVC_dVL_test_x[] = {')
127 print(*('\t{:+.8e}'.format(c) for c in DL), sep=',\n')
128 print('};')
129 print('static const double dVC_dVL_test_y[] = {')
130 print(*('\t{:+.8e}'.format(c) for c in dVC_dVL), sep=',\n')
131 print('};')
def dVC_dVL_for_z(z)
Ratio between the comoving volume element dVC and a DL**2 prior at redshift z.
def exact_dVC_dVL(DL)
An alternate expression of dVC_dVL_for_DL.