Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_test_core Namespace Reference

Functions

def test_parameters_valid_input ()
 
def test_parameters_missing ()
 
def test_parameters_set ()
 
def test_parameters_mgrowth ()
 
def test_parameters_read_write ()
 
def test_cosmology_init ()
 
def test_cosmology_output ()
 
def test_cosmology_pickles ()
 
def test_cosmology_repr ()
 

Function Documentation

def ccl_test_core.test_cosmology_init ( )
Check that Cosmology objects can only be constructed in a valid way.

Definition at line 197 of file ccl_test_core.py.

198  """
199  Check that Cosmology objects can only be constructed in a valid way.
200  """
201  # Make sure error raised if invalid transfer/power spectrum etc. passed
202  assert_raises(
203  ValueError, ccl.Cosmology,
204  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
205  matter_power_spectrum='x')
206  assert_raises(
207  ValueError, ccl.Cosmology,
208  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
209  transfer_function='x')
210  assert_raises(
211  ValueError, ccl.Cosmology,
212  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
213  baryons_power_spectrum='x')
214  assert_raises(
215  ValueError, ccl.Cosmology,
216  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
217  mass_function='x')
218  assert_raises(
219  ValueError, ccl.Cosmology,
220  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
221  halo_concentration='x')
222 
223 
def test_cosmology_init()
def ccl_test_core.test_cosmology_output ( )
Check that status messages and other output from Cosmology() object works
correctly.

Definition at line 224 of file ccl_test_core.py.

225  """
226  Check that status messages and other output from Cosmology() object works
227  correctly.
228  """
229  # Create test cosmology object
230  cosmo = ccl.Cosmology(Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9,
231  n_s=0.96)
232 
233  # Return and print status messages
234  assert_no_warnings(cosmo.status)
235  assert_no_warnings(print, cosmo)
236 
237  # Test status methods for different precomputable quantities
238  assert_(cosmo.has_distances() is False)
239  assert_(cosmo.has_growth() is False)
240  assert_(cosmo.has_power() is False)
241  assert_(cosmo.has_sigma() is False)
242 
243  # Check that quantities can be precomputed
244  assert_no_warnings(cosmo.compute_distances)
245  assert_no_warnings(cosmo.compute_growth)
246  assert_no_warnings(cosmo.compute_power)
247  assert_(cosmo.has_distances() is True)
248  assert_(cosmo.has_growth() is True)
249  assert_(cosmo.has_power() is True)
250 
251 
def test_cosmology_output()
def ccl_test_core.test_cosmology_pickles ( )
Check that a Cosmology object pickles.

Definition at line 252 of file ccl_test_core.py.

253  """Check that a Cosmology object pickles."""
254  cosmo = ccl.Cosmology(
255  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
256  m_nu=[0.02, 0.1, 0.05], mnu_type='list',
257  z_mg=[0.0, 1.0], df_mg=[0.01, 0.0])
258 
259  with tempfile.TemporaryFile() as fp:
260  pickle.dump(cosmo, fp)
261 
262  fp.seek(0)
263  cosmo2 = pickle.load(fp)
264 
265  assert_(
266  ccl.comoving_radial_distance(cosmo, 0.5) ==
267  ccl.comoving_radial_distance(cosmo2, 0.5))
268 
269 
def test_cosmology_pickles()
def ccl_test_core.test_cosmology_repr ( )
Check that we can make a Cosmology object from its repr.

Definition at line 270 of file ccl_test_core.py.

271  """Check that we can make a Cosmology object from its repr."""
272  import pyccl # noqa: F401
273 
274  cosmo = ccl.Cosmology(
275  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
276  m_nu=[0.02, 0.1, 0.05], mnu_type='list',
277  z_mg=[0.0, 1.0], df_mg=[0.01, 0.0])
278 
279  cosmo2 = eval(str(cosmo))
280  assert_(
281  ccl.comoving_radial_distance(cosmo, 0.5) ==
282  ccl.comoving_radial_distance(cosmo2, 0.5))
283 
284  cosmo3 = eval(repr(cosmo))
285  assert_(
286  ccl.comoving_radial_distance(cosmo, 0.5) ==
287  ccl.comoving_radial_distance(cosmo3, 0.5))
288 
289  # same test with arrays to be sure
290  cosmo = ccl.Cosmology(
291  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
292  m_nu=np.array([0.02, 0.1, 0.05]), mnu_type='list',
293  z_mg=np.array([0.0, 1.0]), df_mg=np.array([0.01, 0.0]))
294 
295  cosmo2 = eval(str(cosmo))
296  assert_(
297  ccl.comoving_radial_distance(cosmo, 0.5) ==
298  ccl.comoving_radial_distance(cosmo2, 0.5))
299 
300  cosmo3 = eval(repr(cosmo))
301  assert_(
302  ccl.comoving_radial_distance(cosmo, 0.5) ==
303  ccl.comoving_radial_distance(cosmo3, 0.5))
304 
305 
def test_cosmology_repr()
def ccl_test_core.test_parameters_mgrowth ( )
Check that valid modified growth inputs are allowed, and invalid ones are
rejected.

Definition at line 106 of file ccl_test_core.py.

107  """
108  Check that valid modified growth inputs are allowed, and invalid ones are
109  rejected.
110  """
111  zarr = np.linspace(0., 1., 15)
112  dfarr = 0.1 * np.ones(15)
113  f_func = lambda z: 0.1 * z
114 
115  # Valid constructions
116  for omega_g in [None, 0.0, 0.1]:
117  assert_no_warnings(
118  ccl.Cosmology,
119  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
120  z_mg=zarr, df_mg=dfarr, Omega_g=omega_g)
121  assert_no_warnings(
122  ccl.Cosmology,
123  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
124  z_mg=[0., 0.1, 0.2],
125  df_mg=[0.1, 0.1, 0.1], Omega_g=omega_g)
126 
127  # Invalid constructions
128  assert_raises(
129  ValueError, ccl.Cosmology,
130  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
131  z_mg=zarr, Omega_g=omega_g)
132  assert_raises(
133  ValueError, ccl.Cosmology,
134  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
135  df_mg=dfarr, Omega_g=omega_g)
136  assert_raises(
137  ValueError, ccl.Cosmology,
138  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
139  z_mg=None,
140  df_mg=dfarr, Omega_g=omega_g)
141  assert_raises(
142  ValueError, ccl.Cosmology,
143  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
144  z_mg=zarr,
145  df_mg=0.1, Omega_g=omega_g)
146  assert_raises(
147  ValueError, ccl.Cosmology,
148  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
149  z_mg=zarr,
150  df_mg=f_func, Omega_g=omega_g)
151 
152  # Mis-matched array sizes and dimensionality
153  assert_raises(
154  ValueError, ccl.Cosmology,
155  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
156  z_mg=zarr,
157  df_mg=dfarr[1:], Omega_g=omega_g)
158  assert_raises(
159  ValueError, ccl.Cosmology,
160  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
161  z_mg=zarr,
162  df_mg=np.column_stack((dfarr, dfarr)), Omega_g=omega_g)
163 
164 
def test_parameters_mgrowth()
def ccl_test_core.test_parameters_missing ( )
Check that errors are raised when compulsory parameters are missing, but
not when non-compulsory ones are.

Definition at line 32 of file ccl_test_core.py.

33  """
34  Check that errors are raised when compulsory parameters are missing, but
35  not when non-compulsory ones are.
36  """
37  assert_raises(ValueError, ccl.Cosmology, Omega_c=0.25)
38 
39  # Check that a single missing compulsory parameter is noticed
40  assert_raises(ValueError, ccl.Cosmology, Omega_c=0.25, Omega_b=0.05,
41  h=0.7, A_s=2.1e-9)
42  assert_raises(ValueError, ccl.Cosmology, Omega_c=0.25, Omega_b=0.05,
43  h=0.7, n_s=0.96)
44  assert_raises(ValueError, ccl.Cosmology, Omega_c=0.25, Omega_b=0.05,
45  A_s=2.1e-9, n_s=0.96)
46  assert_raises(ValueError, ccl.Cosmology, Omega_c=0.25,
47  h=0.7, A_s=2.1e-9, n_s=0.96)
48  assert_raises(ValueError, ccl.Cosmology, Omega_b=0.05,
49  h=0.7, A_s=2.1e-9, n_s=0.96)
50 
51  # Make sure that compulsory parameters are compulsory
52  assert_raises(
53  ValueError, ccl.Cosmology,
54  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
55  Omega_k=None)
56  assert_raises(
57  ValueError, ccl.Cosmology,
58  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
59  w0=None)
60  assert_raises(
61  ValueError, ccl.Cosmology,
62  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
63  wa=None)
64 
65  # Check that sigma8 vs A_s is handled ok.
66  assert_raises(
67  ValueError, ccl.Cosmology,
68  Omega_c=0.25, Omega_b=0.05, h=0.7, n_s=0.8,
69  A_s=2.1e-9, sigma8=0.7)
70  assert_raises(
71  ValueError, ccl.Cosmology,
72  Omega_c=0.25, Omega_b=0.05, h=0.7, n_s=0.8)
73 
74  # Make sure that optional parameters are optional
75  assert_no_warnings(
76  ccl.Cosmology,
77  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
78  z_mg=None, df_mg=None)
79  assert_no_warnings(
80  ccl.Cosmology,
81  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
82  z_mg=None)
83  assert_no_warnings(
84  ccl.Cosmology,
85  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
86  df_mg=None)
87 
88 
def test_parameters_missing()
def ccl_test_core.test_parameters_read_write ( )
Check that Cosmology objects can be read and written

Definition at line 165 of file ccl_test_core.py.

166  """Check that Cosmology objects can be read and written"""
167  params = ccl.Cosmology(
168  Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
169  m_nu=[0.02, 0.1, 0.05], mnu_type='list',
170  z_mg=[0.0, 1.0], df_mg=[0.01, 0.0])
171 
172  # Make a temporary file name
173  with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
174  temp_file_name = tmpfile.name
175 
176  # Write out and then eead in the parameters from that file
177  params.write_yaml(temp_file_name)
178  params2 = ccl.Cosmology.read_yaml(temp_file_name)
179 
180  # Check the read-in params are equal to the written out ones
181  assert_almost_equal(params['Omega_c'], params2['Omega_c'])
182  assert_almost_equal(params['Neff'], params2['Neff'])
183  assert_almost_equal(params['sum_nu_masses'], params2['sum_nu_masses'])
184 
185  # Now make a file that will be deleted so it does not exist
186  # and check the right error is raise
187  with tempfile.NamedTemporaryFile(delete=True) as tmpfile:
188  temp_file_name = tmpfile.name
189 
190  assert_raises(IOError, ccl.Cosmology.read_yaml, filename=temp_file_name)
191  assert_raises(
192  IOError,
193  params.read_yaml,
194  filename=temp_file_name+"/nonexistent_directory/params.yml")
195 
196 
def test_parameters_read_write()
def ccl_test_core.test_parameters_set ( )
Check that a Cosmology object doesn't let parameters be set.

Definition at line 89 of file ccl_test_core.py.

90  """
91  Check that a Cosmology object doesn't let parameters be set.
92  """
93  params = ccl.Cosmology(Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9,
94  n_s=0.96)
95 
96  # Check that values of sigma8 and A_s won't be misinterpreted by the C code
97  assert_raises(ValueError, ccl.Cosmology, Omega_c=0.25, Omega_b=0.05,
98  h=0.7, A_s=2e-5, n_s=0.96)
99  assert_raises(ValueError, ccl.Cosmology, Omega_c=0.25, Omega_b=0.05,
100  h=0.7, sigma8=9e-6, n_s=0.96)
101 
102  # Check that error is raised when unrecognized parameter requested
103  assert_raises(KeyError, lambda: params['wibble'])
104 
105 
def test_parameters_set()
def ccl_test_core.test_parameters_valid_input ( )
Check that valid parameter arguments are accepted.

Definition at line 10 of file ccl_test_core.py.

11  """
12  Check that valid parameter arguments are accepted.
13  """
14  assert_no_warnings(ccl.Cosmology, Omega_c=0.25, Omega_b=0.05, h=0.7,
15  A_s=2.1e-9, n_s=0.96)
16  assert_no_warnings(ccl.Cosmology, Omega_c=0.25, Omega_b=0.05, h=0.7,
17  A_s=2.1e-9, n_s=0.96, Omega_k=0.05)
18  assert_no_warnings(ccl.Cosmology, Omega_c=0.25, Omega_b=0.05, h=0.7,
19  A_s=2.1e-9, n_s=0.96, Neff=2.046)
20  assert_no_warnings(ccl.Cosmology, Omega_c=0.25, Omega_b=0.05, h=0.7,
21  A_s=2.1e-9, n_s=0.96, Neff=3.046, m_nu=0.06)
22 
23  assert_no_warnings(ccl.Cosmology, Omega_c=0.25, Omega_b=0.05, h=0.7,
24  A_s=2.1e-9, n_s=0.96, w0=-0.9)
25  assert_no_warnings(ccl.Cosmology, Omega_c=0.25, Omega_b=0.05, h=0.7,
26  A_s=2.1e-9, n_s=0.96, w0=-0.9, wa=0.1)
27 
28  # Check that kwarg order doesn't matter
29  assert_no_warnings(ccl.Cosmology, h=0.7, Omega_c=0.25, Omega_b=0.05,
30  A_s=2.1e-9, n_s=0.96)
31 
def test_parameters_valid_input()