Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_test_growth.py
Go to the documentation of this file.
1 import numpy as np
2 from numpy.testing import assert_allclose, run_module_suite
3 import pyccl as ccl
4 from os.path import dirname, join
5 # Set tolerances
6 GROWTH_TOLERANCE = 1e-4
7 
8 # Set up the cosmological parameters to be used in each of the models
9 # Values that are the same for all 5 models
10 Omega_c = 0.25
11 Omega_b = 0.05
12 Neff = 0.
13 m_nu = 0.
14 h = 0.7
15 A_s = 2.1e-9
16 n_s = 0.96
17 
18 # Values that are different for the different models
19 Omega_v_vals = np.array([0.7, 0.7, 0.7, 0.65, 0.75])
20 w0_vals = np.array([-1.0, -0.9, -0.9, -0.9, -0.9])
21 wa_vals = np.array([0.0, 0.0, 0.1, 0.1, 0.1])
22 
24  """
25  Read the file containing growth factor benchmarks for the low redshifts.
26  """
27  # Load data from file
28  dat = np.genfromtxt(join(dirname(__file__),"benchmark/growth_model1-5.txt")).T
29  assert(dat.shape == (6,6))
30 
31  # Split into redshift column and growth(z) columns
32  z = dat[0]
33  gfac = dat[1:]
34  return z, gfac
35 
37  """
38  Read the file containing growth factor benchmarks for the whole redshift range.
39  """
40  # Load data from file
41  dat = np.genfromtxt(join(dirname(__file__),"benchmark/growth_cosmomad_allz.txt")).T
42  assert(dat.shape == (6,10))
43 
44  # Split into redshift column and growth(z) columns
45  z = dat[0]
46  gfac = dat[1:]
47  return z, gfac
48 
49 # Set-up test data
50 z_lowz, gfac_lowz = read_growth_lowz_benchmark_file()
51 z_allz, gfac_allz = read_growth_allz_benchmark_file()
52 
53 def compare_growth(z, gfac_bench, Omega_v, w0, wa):
54  """
55  Compare growth factor calculated by pyccl with the values in the benchmark
56  file. This test only works if radiation is explicitly set to 0.
57  """
58 
59  # Set Omega_K in a consistent way
60  Omega_k = 1.0 - Omega_c - Omega_b - Omega_v
61 
62  cosmo = ccl.Cosmology(Omega_c=Omega_c, Omega_b=Omega_b, Neff=Neff, m_nu=m_nu,
63  h=h, A_s=A_s, n_s=n_s, Omega_k=Omega_k,
64  w0=w0, wa=wa, Omega_g=0)
65 
66  # Calculate distance using pyccl
67  a = 1. / (1. + z)
68  gfac = ccl.growth_factor_unnorm(cosmo, a)
69 
70  # Compare to benchmark data
71  assert_allclose(gfac, gfac_bench, atol=1e-12, rtol=GROWTH_TOLERANCE)
72 
73 
75  i = 0
76  compare_growth(z_lowz, gfac_lowz[i], Omega_v_vals[i], w0_vals[i], wa_vals[i])
77 
79  i = 1
80  compare_growth(z_lowz, gfac_lowz[i], Omega_v_vals[i], w0_vals[i], wa_vals[i])
81 
83  i = 2
84  compare_growth(z_lowz, gfac_lowz[i], Omega_v_vals[i], w0_vals[i], wa_vals[i])
85 
87  i = 3
88  compare_growth(z_lowz, gfac_lowz[i], Omega_v_vals[i], w0_vals[i], wa_vals[i])
89 
91  i = 4
92  compare_growth(z_lowz, gfac_lowz[i], Omega_v_vals[i], w0_vals[i], wa_vals[i])
93 
94 # 0.01 < z < 1000 tests
96  i = 0
97  compare_growth(z_allz, gfac_allz[i], Omega_v_vals[i], w0_vals[i], wa_vals[i])
98 
100  i = 1
101  compare_growth(z_allz, gfac_allz[i], Omega_v_vals[i], w0_vals[i], wa_vals[i])
102 
104  i = 2
105  compare_growth(z_allz, gfac_allz[i], Omega_v_vals[i], w0_vals[i], wa_vals[i])
106 
108  i = 3
109  compare_growth(z_allz, gfac_allz[i], Omega_v_vals[i], w0_vals[i], wa_vals[i])
110 
112  i = 4
113  compare_growth(z_allz, gfac_allz[i], Omega_v_vals[i], w0_vals[i], wa_vals[i])
114 
116  """
117  Compare the modified growth function computed by CCL against the exact
118  result for a particular modification of the growth rate.
119  """
120  # Define differential growth rate arrays
121  nz_mg = 128
122  z_mg = np.zeros(nz_mg)
123  df_mg = np.zeros(nz_mg)
124  for i in range(0, nz_mg):
125  z_mg[i] = 4. * (i + 0.0) / (nz_mg - 1.)
126  df_mg[i] = 0.1 / (1. + z_mg[i])
127 
128  # Define two test cosmologies, without and with modified growth respectively
129  cosmo1 = ccl.Cosmology(Omega_c=0.25, Omega_b=0.05, Omega_k=0., Neff=0., m_nu=0.,
130  w0=-1., wa=0., h=0.7, A_s=2.1e-9, n_s=0.96)
131  cosmo2 = ccl.Cosmology(Omega_c=0.25, Omega_b=0.05, Omega_k=0., Neff=0., m_nu=0.,
132  w0=-1., wa=0., h=0.7, A_s=2.1e-9, n_s=0.96,
133  z_mg=z_mg, df_mg=df_mg)
134 
135  # We have included a growth modification \delta f = K*a, with K==0.1
136  # (arbitrarily). This case has an analytic solution, given by
137  # D(a) = D_0(a)*exp(K*(a-1)). Here we compare the growth computed by CCL
138  # with the analytic solution.
139  a = 1. / (1. + z_mg)
140 
141  d1 = ccl.growth_factor(cosmo1, a)
142  d2 = ccl.growth_factor(cosmo2, a)
143  f1 = ccl.growth_rate(cosmo1, a)
144  f2 = ccl.growth_rate(cosmo2, a)
145 
146  f2r = f1 + 0.1*a
147  d2r = d1 * np.exp(0.1*(a-1.))
148 
149  # Check that ratio of calculated and analytic results is within tolerance
150  assert_allclose(d2r, d2, rtol=GROWTH_TOLERANCE)
151  assert_allclose(f2r, f2, rtol=GROWTH_TOLERANCE)
152 
153 
154 if __name__ == "__main__":
155  run_module_suite()
def test_growth_allz_model_2()
def read_growth_lowz_benchmark_file()
def test_growth_lowz_model_4()
def test_growth_lowz_model_2()
def test_growth_allz_model_4()
def read_growth_allz_benchmark_file()
def test_growth_lowz_model_3()
def compare_growth(z, gfac_bench, Omega_v, w0, wa)
auto range(T const &first, T const &last) -> Generator< T >
Definition: catch.hpp:3156
def test_growth_lowz_model_1()
def test_growth_allz_model_1()
def test_growth_allz_model_0()
def test_growth_allz_model_3()
def test_growth_lowz_model_0()