1 from __future__
import print_function
5 from numpy.testing
import assert_raises, assert_warns, assert_no_warnings, \
6 assert_, run_module_suite, assert_almost_equal
12 Check that valid parameter arguments are accepted. 14 assert_no_warnings(ccl.Cosmology, Omega_c=0.25, Omega_b=0.05, h=0.7,
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)
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)
29 assert_no_warnings(ccl.Cosmology, h=0.7, Omega_c=0.25, Omega_b=0.05,
34 Check that errors are raised when compulsory parameters are missing, but 35 not when non-compulsory ones are. 37 assert_raises(ValueError, ccl.Cosmology, Omega_c=0.25)
40 assert_raises(ValueError, ccl.Cosmology, Omega_c=0.25, Omega_b=0.05,
42 assert_raises(ValueError, ccl.Cosmology, Omega_c=0.25, Omega_b=0.05,
44 assert_raises(ValueError, ccl.Cosmology, Omega_c=0.25, Omega_b=0.05,
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)
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,
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,
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,
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)
71 ValueError, ccl.Cosmology,
72 Omega_c=0.25, Omega_b=0.05, h=0.7, n_s=0.8)
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)
81 Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
85 Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
91 Check that a Cosmology object doesn't let parameters be set. 93 params = ccl.Cosmology(Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9,
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)
103 assert_raises(KeyError,
lambda: params[
'wibble'])
108 Check that valid modified growth inputs are allowed, and invalid ones are 111 zarr = np.linspace(0., 1., 15)
112 dfarr = 0.1 * np.ones(15)
113 f_func =
lambda z: 0.1 * z
116 for omega_g
in [
None, 0.0, 0.1]:
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)
123 Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9, n_s=0.96,
125 df_mg=[0.1, 0.1, 0.1], Omega_g=omega_g)
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)
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)
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,
140 df_mg=dfarr, Omega_g=omega_g)
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,
145 df_mg=0.1, Omega_g=omega_g)
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,
150 df_mg=f_func, Omega_g=omega_g)
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,
157 df_mg=dfarr[1:], Omega_g=omega_g)
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,
162 df_mg=np.column_stack((dfarr, dfarr)), Omega_g=omega_g)
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])
173 with tempfile.NamedTemporaryFile(delete=
False)
as tmpfile:
174 temp_file_name = tmpfile.name
177 params.write_yaml(temp_file_name)
178 params2 = ccl.Cosmology.read_yaml(temp_file_name)
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'])
187 with tempfile.NamedTemporaryFile(delete=
True)
as tmpfile:
188 temp_file_name = tmpfile.name
190 assert_raises(IOError, ccl.Cosmology.read_yaml, filename=temp_file_name)
194 filename=temp_file_name+
"/nonexistent_directory/params.yml")
199 Check that Cosmology objects can only be constructed in a valid way. 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')
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')
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')
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,
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')
226 Check that status messages and other output from Cosmology() object works 230 cosmo = ccl.Cosmology(Omega_c=0.25, Omega_b=0.05, h=0.7, A_s=2.1e-9,
234 assert_no_warnings(cosmo.status)
235 assert_no_warnings(
print, cosmo)
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)
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)
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])
259 with tempfile.TemporaryFile()
as fp:
260 pickle.dump(cosmo, fp)
263 cosmo2 = pickle.load(fp)
266 ccl.comoving_radial_distance(cosmo, 0.5) ==
267 ccl.comoving_radial_distance(cosmo2, 0.5))
271 """Check that we can make a Cosmology object from its repr.""" 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])
279 cosmo2 = eval(str(cosmo))
281 ccl.comoving_radial_distance(cosmo, 0.5) ==
282 ccl.comoving_radial_distance(cosmo2, 0.5))
284 cosmo3 = eval(repr(cosmo))
286 ccl.comoving_radial_distance(cosmo, 0.5) ==
287 ccl.comoving_radial_distance(cosmo3, 0.5))
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]))
295 cosmo2 = eval(str(cosmo))
297 ccl.comoving_radial_distance(cosmo, 0.5) ==
298 ccl.comoving_radial_distance(cosmo2, 0.5))
300 cosmo3 = eval(repr(cosmo))
302 ccl.comoving_radial_distance(cosmo, 0.5) ==
303 ccl.comoving_radial_distance(cosmo3, 0.5))
306 if __name__ ==
'__main__':
def test_cosmology_pickles()
def test_parameters_valid_input()
def test_parameters_mgrowth()
def test_parameters_set()
def test_cosmology_output()
def test_parameters_read_write()
def test_cosmology_repr()
def test_parameters_missing()
def test_cosmology_init()