Skip to content

Commit 52ca009

Browse files
authored
Merge pull request #83 from eelregit/eelregit_f_de_patch
Change f_de parametrization to avoid division by log(1)
2 parents d5c7464 + 09d6b09 commit 52ca009

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

jax_cosmo/background.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def w(cosmo, a):
4848
4949
.. math::
5050
51-
w(a) = w_0 + w (1 -a)
51+
w(a) = w_0 + w_a (1 - a)
5252
"""
5353
return cosmo.w0 + (1.0 - a) * cosmo.wa # Equation (6) in Linder (2003)
5454

@@ -75,22 +75,19 @@ def f_de(cosmo, a):
7575
7676
.. math::
7777
78-
\rho_{de}(a) \propto a^{f(a)}
78+
\rho_{de}(a) = \rho_{de}(a=1) e^{f(a)}
7979
80-
(see :cite:`2005:Percival`) where :math:`f(a)` is computed as
81-
:math:`f(a) = \frac{-3}{\ln(a)} \int_0^{\ln(a)} [1 + w(a^\prime)]
82-
d \ln(a^\prime)`. In the case of Linder's parametrisation for the
83-
dark energy in Eq. :eq:`linderParam` :math:`f(a)` becomes:
80+
(see :cite:`2005:Percival` and note the difference in the exponent base
81+
in the parametrizations) where :math:`f(a)` is computed as
82+
:math:`f(a) = -3 \int_0^{\ln(a)} [1 + w(a')] d \ln(a')`.
83+
In the case of Linder's parametrisation for the dark energy
84+
in Eq. :eq:`linderParam` :math:`f(a)` becomes:
8485
8586
.. math::
8687
87-
f(a) = -3(1 + w_0) + 3 w \left[ \frac{a - 1}{ \ln(a) } - 1 \right]
88+
f(a) = -3 (1 + w_0 + w_a) \ln(a) + 3 w_a (a - 1)
8889
"""
89-
# Just to make sure we are not diving by 0
90-
epsilon = np.finfo(np.float32).eps
91-
return -3.0 * (1.0 + cosmo.w0) + 3.0 * cosmo.wa * (
92-
(a - 1.0) / np.log(a - epsilon) - 1.0
93-
)
90+
return -3.0 * (1.0 + cosmo.w0 + cosmo.wa) * np.log(a) + 3.0 * cosmo.wa * (a - 1.0)
9491

9592

9693
def Esqr(cosmo, a):
@@ -117,15 +114,15 @@ def Esqr(cosmo, a):
117114
118115
.. math::
119116
120-
E^2(a) = \Omega_m a^{-3} + \Omega_k a^{-2} + \Omega_{de} a^{f(a)}
117+
E^2(a) = \Omega_m a^{-3} + \Omega_k a^{-2} + \Omega_{de} e^{f(a)}
121118
122119
where :math:`f(a)` is the Dark Energy evolution parameter computed
123120
by :py:meth:`.f_de`.
124121
"""
125122
return (
126123
cosmo.Omega_m * np.power(a, -3)
127124
+ cosmo.Omega_k * np.power(a, -2)
128-
+ cosmo.Omega_de * np.power(a, f_de(cosmo, a))
125+
+ cosmo.Omega_de * np.exp(f_de(cosmo, a))
129126
)
130127

131128

@@ -191,12 +188,12 @@ def Omega_de_a(cosmo, a):
191188
192189
.. math::
193190
194-
\Omega_{de}(a) = \frac{\Omega_{de} a^{f(a)}}{E^2(a)}
191+
\Omega_{de}(a) = \frac{\Omega_{de} e^{f(a)}}{E^2(a)}
195192
196193
where :math:`f(a)` is the Dark Energy evolution parameter computed by
197194
:py:meth:`.f_de` (see :cite:`2005:Percival` Eq. (6)).
198195
"""
199-
return cosmo.Omega_de * np.power(a, f_de(cosmo, a)) / Esqr(cosmo, a)
196+
return cosmo.Omega_de * np.exp(f_de(cosmo, a)) / Esqr(cosmo, a)
200197

201198

202199
def radial_comoving_distance(cosmo, a, log10_amin=-3, steps=256):

tests/test_background.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,39 @@
77
from jax_cosmo import Cosmology
88

99

10+
def test_H():
11+
# We first define equivalent CCL and jax_cosmo cosmologies
12+
cosmo_ccl = ccl.Cosmology(
13+
Omega_c=0.3,
14+
Omega_b=0.05,
15+
h=0.7,
16+
sigma8=0.8,
17+
n_s=0.96,
18+
Neff=0,
19+
transfer_function="eisenstein_hu",
20+
matter_power_spectrum="linear",
21+
wa=2.0, # non-zero wa
22+
)
23+
24+
cosmo_jax = Cosmology(
25+
Omega_c=0.3,
26+
Omega_b=0.05,
27+
h=0.7,
28+
sigma8=0.8,
29+
n_s=0.96,
30+
Omega_k=0.0,
31+
w0=-1.0,
32+
wa=2.0, # non-zero wa
33+
)
34+
35+
# Test array of scale factors
36+
a = np.linspace(0.01, 1.0)
37+
38+
H_ccl = ccl.h_over_h0(cosmo_ccl, a)
39+
H_jax = bkgrd.H(cosmo_jax, a) / 100.0
40+
assert_allclose(H_ccl, H_jax, rtol=1.0e-3)
41+
42+
1043
def test_distances_flat():
1144
# We first define equivalent CCL and jax_cosmo cosmologies
1245
cosmo_ccl = ccl.Cosmology(

0 commit comments

Comments
 (0)