From 5e0e437d1b9da43e4e97abeccfbd88b6c1552c8a Mon Sep 17 00:00:00 2001 From: Yin Li Date: Wed, 12 Jan 2022 18:50:03 -0500 Subject: [PATCH 1/3] Change f_de parametri. to avoid division by log(1) --- jax_cosmo/background.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/jax_cosmo/background.py b/jax_cosmo/background.py index 6cec947..7a90e68 100644 --- a/jax_cosmo/background.py +++ b/jax_cosmo/background.py @@ -47,7 +47,7 @@ def w(cosmo, a): .. math:: - w(a) = w_0 + w (1 -a) + w(a) = w_0 + w_a (1 - a) """ return cosmo.w0 + (1.0 - a) * cosmo.wa # Equation (6) in Linder (2003) @@ -74,22 +74,19 @@ def f_de(cosmo, a): .. math:: - \rho_{de}(a) \propto a^{f(a)} + \rho_{de}(a) = \rho_{de}(a=1) e^{f(a)} - (see :cite:`2005:Percival`) where :math:`f(a)` is computed as - :math:`f(a) = \frac{-3}{\ln(a)} \int_0^{\ln(a)} [1 + w(a^\prime)] - d \ln(a^\prime)`. In the case of Linder's parametrisation for the - dark energy in Eq. :eq:`linderParam` :math:`f(a)` becomes: + (see :cite:`2005:Percival` and note the difference in the exponent base + in the parametrizations) where :math:`f(a)` is computed as + :math:`f(a) = -3 \int_0^{\ln(a)} [1 + w(a')] d \ln(a')`. + In the case of Linder's parametrisation for the dark energy + in Eq. :eq:`linderParam` :math:`f(a)` becomes: .. math:: - f(a) = -3(1 + w_0) + 3 w \left[ \frac{a - 1}{ \ln(a) } - 1 \right] + f(a) = -3 (1 + w_0 + w_a) \ln(a) + 3 w_a (a - 1) """ - # Just to make sure we are not diving by 0 - epsilon = np.finfo(np.float32).eps - return -3.0 * (1.0 + cosmo.w0) + 3.0 * cosmo.wa * ( - (a - 1.0) / np.log(a - epsilon) - 1.0 - ) + return -3.0 * (1.0 + cosmo.w0 + cosmo.wa) * np.log(a) + 3.0 * cosmo.wa * (a - 1.0) def Esqr(cosmo, a): @@ -116,7 +113,7 @@ def Esqr(cosmo, a): .. math:: - E^2(a) = \Omega_m a^{-3} + \Omega_k a^{-2} + \Omega_{de} a^{f(a)} + E^2(a) = \Omega_m a^{-3} + \Omega_k a^{-2} + \Omega_{de} e^{f(a)} where :math:`f(a)` is the Dark Energy evolution parameter computed by :py:meth:`.f_de`. @@ -124,7 +121,7 @@ def Esqr(cosmo, a): return ( cosmo.Omega_m * np.power(a, -3) + cosmo.Omega_k * np.power(a, -2) - + cosmo.Omega_de * np.power(a, f_de(cosmo, a)) + + cosmo.Omega_de * np.exp(f_de(cosmo, a)) ) @@ -190,12 +187,12 @@ def Omega_de_a(cosmo, a): .. math:: - \Omega_{de}(a) = \frac{\Omega_{de} a^{f(a)}}{E^2(a)} + \Omega_{de}(a) = \frac{\Omega_{de} e^{f(a)}}{E^2(a)} where :math:`f(a)` is the Dark Energy evolution parameter computed by :py:meth:`.f_de` (see :cite:`2005:Percival` Eq. (6)). """ - return cosmo.Omega_de * np.power(a, f_de(cosmo, a)) / Esqr(cosmo, a) + return cosmo.Omega_de * np.exp(f_de(cosmo, a)) / Esqr(cosmo, a) def radial_comoving_distance(cosmo, a, log10_amin=-3, steps=256): From d465d6c39b19799a7ec6b641e728dffbef43c9cc Mon Sep 17 00:00:00 2001 From: EiffL Date: Thu, 20 Jan 2022 19:40:44 +0100 Subject: [PATCH 2/3] Adding test of Hubble parameter --- tests/test_background.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/test_background.py b/tests/test_background.py index 4e973a2..b5e1172 100644 --- a/tests/test_background.py +++ b/tests/test_background.py @@ -6,6 +6,37 @@ import jax_cosmo.background as bkgrd from jax_cosmo import Cosmology +def test_H(): + # We first define equivalent CCL and jax_cosmo cosmologies + cosmo_ccl = ccl.Cosmology( + Omega_c=0.3, + Omega_b=0.05, + h=0.7, + sigma8=0.8, + n_s=0.96, + Neff=0, + transfer_function="eisenstein_hu", + matter_power_spectrum="linear", + wa=2. # non-zero wa + ) + + cosmo_jax = Cosmology( + Omega_c=0.3, + Omega_b=0.05, + h=0.7, + sigma8=0.8, + n_s=0.96, + Omega_k=0.0, + w0=-1.0, + wa=2.0, # non-zero wa + ) + + # Test array of scale factors + a = np.linspace(0.01, 1.0) + + H_ccl = ccl.h_over_h0(cosmo_ccl, a) + H_jax = bkgrd.H(cosmo_jax, a) / 100. + assert_allclose(H_ccl, H_jax, rtol=1.e-3) def test_distances_flat(): # We first define equivalent CCL and jax_cosmo cosmologies From 09d6b09fcb1b985e25596f2b5750f3d75c7687d9 Mon Sep 17 00:00:00 2001 From: EiffL Date: Thu, 20 Jan 2022 19:42:24 +0100 Subject: [PATCH 3/3] make the code nicely formatted --- tests/test_background.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/test_background.py b/tests/test_background.py index b5e1172..4f86a91 100644 --- a/tests/test_background.py +++ b/tests/test_background.py @@ -6,8 +6,9 @@ import jax_cosmo.background as bkgrd from jax_cosmo import Cosmology + def test_H(): - # We first define equivalent CCL and jax_cosmo cosmologies + # We first define equivalent CCL and jax_cosmo cosmologies cosmo_ccl = ccl.Cosmology( Omega_c=0.3, Omega_b=0.05, @@ -17,7 +18,7 @@ def test_H(): Neff=0, transfer_function="eisenstein_hu", matter_power_spectrum="linear", - wa=2. # non-zero wa + wa=2.0, # non-zero wa ) cosmo_jax = Cosmology( @@ -28,15 +29,16 @@ def test_H(): n_s=0.96, Omega_k=0.0, w0=-1.0, - wa=2.0, # non-zero wa + wa=2.0, # non-zero wa ) # Test array of scale factors a = np.linspace(0.01, 1.0) H_ccl = ccl.h_over_h0(cosmo_ccl, a) - H_jax = bkgrd.H(cosmo_jax, a) / 100. - assert_allclose(H_ccl, H_jax, rtol=1.e-3) + H_jax = bkgrd.H(cosmo_jax, a) / 100.0 + assert_allclose(H_ccl, H_jax, rtol=1.0e-3) + def test_distances_flat(): # We first define equivalent CCL and jax_cosmo cosmologies