Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_test_pyccl_interface.py
Go to the documentation of this file.
1 import numpy as np,math
2 from numpy.testing import assert_raises, assert_warns, assert_no_warnings, \
3  assert_, decorators, run_module_suite, assert_allclose
4 import pyccl as ccl
5 from pyccl import CCLError
6 
8  """
9  Create a set of reference Cosmology() objects.
10  """
11  # Standard LCDM model
12  cosmo1 = ccl.Cosmology(
13  Omega_c=0.27, Omega_b=0.045, h=0.67, A_s=1e-10, n_s=0.96)
14 
15  # LCDM model with curvature
16  cosmo2 = ccl.Cosmology(
17  Omega_c=0.27, Omega_b=0.045, h=0.67, A_s=1e-10,
18  n_s=0.96, Omega_k=0.05)
19 
20  # wCDM model
21  cosmo3 = ccl.Cosmology(
22  Omega_c=0.27, Omega_b=0.045, h=0.67, A_s=1e-10,
23  n_s=0.96, w0=-0.95, wa=0.05)
24 
25  # BBKS Pk
26  cosmo4 = ccl.Cosmology(
27  Omega_c=0.27, Omega_b=0.045, h=0.67, sigma8=0.8, n_s=0.96,
28  transfer_function='bbks')
29 
30  # E&H Pk
31  cosmo5 = ccl.Cosmology(
32  Omega_c=0.27, Omega_b=0.045, h=0.67, sigma8=0.8, n_s=0.96,
33  transfer_function='eisenstein_hu')
34 
35  # Emulator Pk
36  cosmo6 = ccl.Cosmology(
37  Omega_c=0.27, Omega_b=0.022/0.67**2, h=0.67, sigma8=0.8,
38  n_s=0.96, Neff=3.04, m_nu=0.,
39  transfer_function='emulator',
40  matter_power_spectrum='emu')
41 
42  # Baryons Pk
43  cosmo8 = ccl.Cosmology(
44  Omega_c=0.27, Omega_b=0.045, h=0.67, A_s=1e-10, n_s=0.96,
45  baryons_power_spectrum='bcm')
46 
47  # Baryons Pk with choice of BCM parameters other than default
48  cosmo9 = ccl.Cosmology(
49  Omega_c=0.27, Omega_b=0.045, h=0.67, A_s=1e-10, n_s=0.96,
50  bcm_log10Mc=math.log10(1.7e14), bcm_etab=0.3, bcm_ks=75.,
51  baryons_power_spectrum='bcm')
52 
53  # Emulator Pk w/neutrinos force equalize
54  #p10 = ccl.Parameters(Omega_c=0.27, Omega_b=0.022/0.67**2, h=0.67, sigma8=0.8,
55  # n_s=0.96, Neff=3.04, m_nu=[0.02, 0.02, 0.02])
56  #cosmo10 = ccl.Cosmology(p7, transfer_function='emulator',
57  # matter_power_spectrum='emu', emulator_neutrinos='equalize')
58 
59  # Return (do a few cosmologies, for speed reasons)
60  return [cosmo1, cosmo4, cosmo5, cosmo9] # cosmo2, cosmo3, cosmo6
61 
63  """
64  Create a set of reference cosmological models with massive neutrinos.
65  This is separate because certain functionality is not yes implemented
66  for massive neutrino cosmologies so will throw errors.
67  """
68 
69  # Emulator Pk w/neutrinos list
70  cosmo1 = ccl.Cosmology(
71  Omega_c=0.27, Omega_b=0.022/0.67**2, h=0.67, sigma8=0.8,
72  n_s=0.96, Neff=3.04, m_nu=[0.02, 0.02, 0.02],
73  transfer_function='emulator',
74  matter_power_spectrum='emu')
75 
76  # Emulator Pk with neutrinos, force equalize
77  #p2 = ccl.Parameters(Omega_c=0.27, Omega_b=0.022/0.67**2, h=0.67, sigma8=0.8,
78  # n_s=0.96, Neff=3.04, m_nu=0.11)
79  #cosmo2 = ccl.Cosmology(p1, transfer_function='emulator',
80  # matter_power_spectrum='emu', emulator_neutrinos='equalize')
81 
82  return [cosmo1]
83 
84 def all_finite(vals):
85  """
86  Returns True if all elements are finite (i.e. not NaN or inf).
87  """
88  return np.all( np.isfinite(vals) )
89 
90 def check_background(cosmo):
91  """
92  Check that background and growth functions can be run.
93  """
94 
95  # Types of scale factor input (scalar, list, array)
96  a_scl = 0.5
97  is_comoving = 0
98  a_lst = [0.2, 0.4, 0.6, 0.8, 1.]
99  a_arr = np.linspace(0.2, 1., 5)
100 
101  # growth_factor
102  assert_( all_finite(ccl.growth_factor(cosmo, a_scl)) )
103  assert_( all_finite(ccl.growth_factor(cosmo, a_lst)) )
104  assert_( all_finite(ccl.growth_factor(cosmo, a_arr)) )
105 
106  # growth_factor_unnorm
107  assert_( all_finite(ccl.growth_factor_unnorm(cosmo, a_scl)) )
108  assert_( all_finite(ccl.growth_factor_unnorm(cosmo, a_lst)) )
109  assert_( all_finite(ccl.growth_factor_unnorm(cosmo, a_arr)) )
110 
111  # growth_rate
112  assert_( all_finite(ccl.growth_rate(cosmo, a_scl)) )
113  assert_( all_finite(ccl.growth_rate(cosmo, a_lst)) )
114  assert_( all_finite(ccl.growth_rate(cosmo, a_arr)) )
115 
116  # comoving_radial_distance
117  assert_( all_finite(ccl.comoving_radial_distance(cosmo, a_scl)) )
118  assert_( all_finite(ccl.comoving_radial_distance(cosmo, a_lst)) )
119  assert_( all_finite(ccl.comoving_radial_distance(cosmo, a_arr)) )
120 
121  # comoving_angular_distance
122  assert_( all_finite(ccl.comoving_angular_distance(cosmo, a_scl)) )
123  assert_( all_finite(ccl.comoving_angular_distance(cosmo, a_lst)) )
124  assert_( all_finite(ccl.comoving_angular_distance(cosmo, a_arr)) )
125 
126  # h_over_h0
127  assert_( all_finite(ccl.h_over_h0(cosmo, a_scl)) )
128  assert_( all_finite(ccl.h_over_h0(cosmo, a_lst)) )
129  assert_( all_finite(ccl.h_over_h0(cosmo, a_arr)) )
130 
131  # luminosity_distance
132  assert_( all_finite(ccl.luminosity_distance(cosmo, a_scl)) )
133  assert_( all_finite(ccl.luminosity_distance(cosmo, a_lst)) )
134  assert_( all_finite(ccl.luminosity_distance(cosmo, a_arr)) )
135 
136  # scale_factor_of_chi
137  assert_( all_finite(ccl.scale_factor_of_chi(cosmo, a_scl)) )
138  assert_( all_finite(ccl.scale_factor_of_chi(cosmo, a_lst)) )
139  assert_( all_finite(ccl.scale_factor_of_chi(cosmo, a_arr)) )
140 
141  # omega_m_a
142  assert_( all_finite(ccl.omega_x(cosmo, a_scl, 'matter')) )
143  assert_( all_finite(ccl.omega_x(cosmo, a_lst, 'matter')) )
144  assert_( all_finite(ccl.omega_x(cosmo, a_arr, 'matter')) )
145 
146  # Fractional density of different types of fluid
147  assert_( all_finite(ccl.omega_x(cosmo, a_arr, 'dark_energy')) )
148  assert_( all_finite(ccl.omega_x(cosmo, a_arr, 'radiation')) )
149  assert_( all_finite(ccl.omega_x(cosmo, a_arr, 'curvature')) )
150  assert_( all_finite(ccl.omega_x(cosmo, a_arr, 'neutrinos_rel')) )
151  assert_( all_finite(ccl.omega_x(cosmo, a_arr, 'neutrinos_massive')) )
152 
153  # Check that omega_x fails if invalid component type is passed
154  assert_raises(ValueError, ccl.omega_x, cosmo, a_scl, 'xyz')
155 
156  # rho_crit_a
157  assert_( all_finite(ccl.rho_x(cosmo, a_scl, 'critical', is_comoving)) )
158  assert_( all_finite(ccl.rho_x(cosmo, a_lst, 'critical', is_comoving)) )
159  assert_( all_finite(ccl.rho_x(cosmo, a_arr, 'critical', is_comoving)) )
160 
161  # rho_m_a
162  assert_( all_finite(ccl.rho_x(cosmo, a_scl, 'matter', is_comoving)) )
163  assert_( all_finite(ccl.rho_x(cosmo, a_lst, 'matter', is_comoving)) )
164  assert_( all_finite(ccl.rho_x(cosmo, a_arr, 'matter', is_comoving)) )
165 
166 
168  """
169  Check that background functions can be run and that the growth functions
170  exit gracefully in functions with massive neutrinos (not implemented yet).
171  """
172  # Types of scale factor input (scalar, list, array)
173  a_scl = 0.5
174  a_lst = [0.2, 0.4, 0.6, 0.8, 1.]
175  a_arr = np.linspace(0.2, 1., 5)
176 
177  # growth_factor
178  assert_raises(CCLError, ccl.growth_factor, cosmo, a_scl)
179  assert_raises(CCLError, ccl.growth_factor, cosmo, a_lst)
180  assert_raises(CCLError, ccl.growth_factor, cosmo, a_arr)
181 
182  # growth_factor_unnorm
183  assert_raises(CCLError, ccl.growth_factor_unnorm, cosmo, a_scl)
184  assert_raises(CCLError, ccl.growth_factor_unnorm, cosmo, a_lst)
185  assert_raises(CCLError, ccl.growth_factor_unnorm, cosmo, a_arr)
186 
187  # growth_rate
188  assert_raises(CCLError,ccl.growth_rate, cosmo, a_scl)
189  assert_raises(CCLError,ccl.growth_rate, cosmo, a_lst)
190  assert_raises(CCLError,ccl.growth_rate, cosmo, a_arr)
191 
192  # comoving_radial_distance
193  assert_( all_finite(ccl.comoving_radial_distance(cosmo, a_scl)) )
194  assert_( all_finite(ccl.comoving_radial_distance(cosmo, a_lst)) )
195  assert_( all_finite(ccl.comoving_radial_distance(cosmo, a_arr)) )
196 
197  # h_over_h0
198  assert_( all_finite(ccl.h_over_h0(cosmo, a_scl)) )
199  assert_( all_finite(ccl.h_over_h0(cosmo, a_lst)) )
200  assert_( all_finite(ccl.h_over_h0(cosmo, a_arr)) )
201 
202  # luminosity_distance
203  assert_( all_finite(ccl.luminosity_distance(cosmo, a_scl)) )
204  assert_( all_finite(ccl.luminosity_distance(cosmo, a_lst)) )
205  assert_( all_finite(ccl.luminosity_distance(cosmo, a_arr)) )
206 
207  # scale_factor_of_chi
208  assert_( all_finite(ccl.scale_factor_of_chi(cosmo, a_scl)) )
209  assert_( all_finite(ccl.scale_factor_of_chi(cosmo, a_lst)) )
210  assert_( all_finite(ccl.scale_factor_of_chi(cosmo, a_arr)) )
211 
212  # omega_m_a
213  assert_( all_finite(ccl.omega_x(cosmo, a_scl, 'matter')) )
214  assert_( all_finite(ccl.omega_x(cosmo, a_lst, 'matter')) )
215  assert_( all_finite(ccl.omega_x(cosmo, a_arr, 'matter')) )
216 
217 
218 def check_power(cosmo):
219  """
220  Check that power spectrum and sigma functions can be run.
221  """
222  # Types of scale factor
223  a = 0.9
224  a_arr = np.linspace(0.2, 1., 5.)
225 
226  # Types of wavenumber input (scalar, list, array)
227  k_scl = 1e-1
228  k_lst = [1e-4, 1e-3, 1e-2, 1e-1, 1e0]
229  k_arr = np.logspace(-4., 0., 5)
230 
231  # Types of smoothing scale, R
232  R_scl = 8.
233  R_lst = [1., 5., 10., 20., 50., 100.]
234  R_arr = np.array([1., 5., 10., 20., 50., 100.])
235 
236  # linear_matter_power
237  assert_( all_finite(ccl.linear_matter_power(cosmo, k_scl, a)) )
238  assert_( all_finite(ccl.linear_matter_power(cosmo, k_lst, a)) )
239  assert_( all_finite(ccl.linear_matter_power(cosmo, k_arr, a)) )
240 
241  assert_raises(TypeError, ccl.linear_matter_power, cosmo, k_scl, a_arr)
242  assert_raises(TypeError, ccl.linear_matter_power, cosmo, k_lst, a_arr)
243  assert_raises(TypeError, ccl.linear_matter_power, cosmo, k_arr, a_arr)
244 
245  # nonlin_matter_power
246  assert_( all_finite(ccl.nonlin_matter_power(cosmo, k_scl, a)) )
247  assert_( all_finite(ccl.nonlin_matter_power(cosmo, k_lst, a)) )
248  assert_( all_finite(ccl.nonlin_matter_power(cosmo, k_arr, a)) )
249 
250  assert_raises(TypeError, ccl.nonlin_matter_power, cosmo, k_scl, a_arr)
251  assert_raises(TypeError, ccl.nonlin_matter_power, cosmo, k_lst, a_arr)
252  assert_raises(TypeError, ccl.nonlin_matter_power, cosmo, k_arr, a_arr)
253 
254  # sigmaR
255  assert_( all_finite(ccl.sigmaR(cosmo, R_scl)) )
256  assert_( all_finite(ccl.sigmaR(cosmo, R_lst)) )
257  assert_( all_finite(ccl.sigmaR(cosmo, R_arr)) )
258 
259  # sigmaV
260  assert_( all_finite(ccl.sigmaV(cosmo, R_scl)) )
261  assert_( all_finite(ccl.sigmaV(cosmo, R_lst)) )
262  assert_( all_finite(ccl.sigmaV(cosmo, R_arr)) )
263 
264  # sigma8
265  assert_( all_finite(ccl.sigma8(cosmo)) )
266 
267 
268 def check_massfunc(cosmo):
269  """
270  Check that mass function and supporting functions can be run.
271  """
272 
273  z = 0.
274  z_arr = np.linspace(0., 2., 10)
275  a = 1.
276  a_arr = 1. / (1.+z_arr)
277  mhalo_scl = 1e13
278  mhalo_lst = [1e11, 1e12, 1e13, 1e14, 1e15, 1e16]
279  mhalo_arr = np.array([1e11, 1e12, 1e13, 1e14, 1e15, 1e16])
280  odelta = 200.
281 
282  # massfunc
283  assert_( all_finite(ccl.massfunc(cosmo, mhalo_scl, a, odelta)) )
284  assert_( all_finite(ccl.massfunc(cosmo, mhalo_lst, a, odelta)) )
285  assert_( all_finite(ccl.massfunc(cosmo, mhalo_arr, a, odelta)) )
286 
287  assert_raises(TypeError, ccl.massfunc, cosmo, mhalo_scl, a_arr, odelta)
288  assert_raises(TypeError, ccl.massfunc, cosmo, mhalo_lst, a_arr, odelta)
289  assert_raises(TypeError, ccl.massfunc, cosmo, mhalo_arr, a_arr, odelta)
290 
291  # Check whether odelta out of bounds
292  assert_raises(CCLError, ccl.massfunc, cosmo, mhalo_scl, a, 199.)
293  assert_raises(CCLError, ccl.massfunc, cosmo, mhalo_scl, a, 5000.)
294 
295  # massfunc_m2r
296  assert_( all_finite(ccl.massfunc_m2r(cosmo, mhalo_scl)) )
297  assert_( all_finite(ccl.massfunc_m2r(cosmo, mhalo_lst)) )
298  assert_( all_finite(ccl.massfunc_m2r(cosmo, mhalo_arr)) )
299 
300  # sigmaM
301  assert_( all_finite(ccl.sigmaM(cosmo, mhalo_scl, a)) )
302  assert_( all_finite(ccl.sigmaM(cosmo, mhalo_lst, a)) )
303  assert_( all_finite(ccl.sigmaM(cosmo, mhalo_arr, a)) )
304 
305  assert_raises(TypeError, ccl.sigmaM, cosmo, mhalo_scl, a_arr)
306  assert_raises(TypeError, ccl.sigmaM, cosmo, mhalo_lst, a_arr)
307  assert_raises(TypeError, ccl.sigmaM, cosmo, mhalo_arr, a_arr)
308 
309  # halo_bias
310  assert_( all_finite(ccl.halo_bias(cosmo, mhalo_scl, a)) )
311  assert_( all_finite(ccl.halo_bias(cosmo, mhalo_lst, a)) )
312  assert_( all_finite(ccl.halo_bias(cosmo, mhalo_arr, a)) )
313 
314 
315 def check_massfunc_nu(cosmo):
316  """
317  Check that mass function and supporting functions can be run.
318  """
319  z = 0.
320  z_arr = np.linspace(0., 2., 10)
321  a = 1.
322  a_arr = 1. / (1.+z_arr)
323  mhalo_scl = 1e13
324  mhalo_lst = [1e11, 1e12, 1e13, 1e14, 1e15, 1e16]
325  mhalo_arr = np.array([1e11, 1e12, 1e13, 1e14, 1e15, 1e16])
326  odelta = 200.
327 
328  # massfunc
329  assert_raises(CCLError, ccl.massfunc,cosmo, mhalo_scl, a, odelta)
330  assert_raises(CCLError, ccl.massfunc,cosmo, mhalo_lst, a, odelta)
331  assert_raises(CCLError, ccl.massfunc,cosmo, mhalo_arr, a, odelta)
332 
333  # halo bias
334  assert_raises(CCLError, ccl.halo_bias, cosmo, mhalo_scl, a, odelta)
335  assert_raises(CCLError, ccl.halo_bias, cosmo, mhalo_lst, a, odelta)
336  assert_raises(CCLError, ccl.halo_bias, cosmo, mhalo_arr, a, odelta)
337 
338  # massfunc_m2r
339  assert_( all_finite(ccl.massfunc_m2r(cosmo, mhalo_scl)) )
340  assert_( all_finite(ccl.massfunc_m2r(cosmo, mhalo_lst)) )
341  assert_( all_finite(ccl.massfunc_m2r(cosmo, mhalo_arr)) )
342 
343  # sigmaM
344  assert_raises(CCLError, ccl.sigmaM, cosmo, mhalo_scl, a)
345  assert_raises(CCLError, ccl.sigmaM, cosmo, mhalo_lst, a)
346  assert_raises(CCLError, ccl.sigmaM, cosmo, mhalo_arr, a)
347 
348  assert_raises(TypeError, ccl.sigmaM, cosmo, mhalo_scl, a_arr)
349  assert_raises(TypeError, ccl.sigmaM, cosmo, mhalo_lst, a_arr)
350  assert_raises(TypeError, ccl.sigmaM, cosmo, mhalo_arr, a_arr)
351 
352 
353 def check_halomod(cosmo):
354  """
355  Check that halo model functions can be run.
356  """
357 
358  # Time variables
359  z = 0.
360  z_array = np.linspace(0., 2., 10)
361  a = 1.
362  a_array = 1. / (1.+z_array)
363 
364  # Halo definition
365  odelta = 200.
366 
367  # Mass variables
368  mass_scalar = 1e13
369  mass_list = [1e11, 1e12, 1e13, 1e14, 1e15, 1e16]
370  mass_array = np.array([1e11, 1e12, 1e13, 1e14, 1e15, 1e16])
371 
372  # Wave-vector variables
373  k_scalar = 1.
374  k_list = [1e-3, 1e-2, 1e-1, 1e0, 1e1]
375  k_array = np.array([1e-3, 1e-2, 1e-1, 1e0, 1e1])
376 
377  # halo concentration
378  assert_( all_finite(ccl.halo_concentration(cosmo, mass_scalar, a, odelta)) )
379  assert_( all_finite(ccl.halo_concentration(cosmo, mass_list, a, odelta)) )
380  assert_( all_finite(ccl.halo_concentration(cosmo, mass_array, a, odelta)) )
381 
382  assert_raises(TypeError, ccl.halo_concentration, cosmo, mass_scalar, a_array, odelta)
383  assert_raises(TypeError, ccl.halo_concentration, cosmo, mass_list, a_array, odelta)
384  assert_raises(TypeError, ccl.halo_concentration, cosmo, mass_array, a_array, odelta)
385 
386  # halo model
387  assert_( all_finite(ccl.halomodel_matter_power(cosmo, k_scalar, a)) )
388  assert_( all_finite(ccl.halomodel_matter_power(cosmo, k_list, a)) )
389  assert_( all_finite(ccl.halomodel_matter_power(cosmo, k_array, a)) )
390 
391  assert_( all_finite(ccl.halomodel.twohalo_matter_power(cosmo, k_scalar, a)) )
392  assert_( all_finite(ccl.halomodel.twohalo_matter_power(cosmo, k_list, a)) )
393  assert_( all_finite(ccl.halomodel.twohalo_matter_power(cosmo, k_array, a)) )
394 
395  assert_( all_finite(ccl.halomodel.onehalo_matter_power(cosmo, k_scalar, a)) )
396  assert_( all_finite(ccl.halomodel.onehalo_matter_power(cosmo, k_list, a)) )
397  assert_( all_finite(ccl.halomodel.onehalo_matter_power(cosmo, k_array, a)) )
398 
399  assert_raises(TypeError, ccl.halomodel_matter_power, cosmo, k_scalar, a_array)
400  assert_raises(TypeError, ccl.halomodel_matter_power, cosmo, k_list, a_array)
401  assert_raises(TypeError, ccl.halomodel_matter_power, cosmo, k_array, a_array)
402 
404  """
405  Check that neutrino-related functions can be run.
406  """
407  z = 0.
408  z_arr = np.linspace(0., 2., 10)
409  a = 1.
410  a_arr = 1. / (1.+z_arr)
411  a_lst = [_a for _a in a_arr]
412 
413  TCMB = 2.725
414  N_nu_mass = 3
415  mnu = [0.02, 0.02, 0.02]
416 
417  # Omeganuh2
418  assert_( all_finite(ccl.Omeganuh2(a, mnu, TCMB)) )
419  assert_( all_finite(ccl.Omeganuh2(a_lst, mnu, TCMB)) )
420  assert_( all_finite(ccl.Omeganuh2(a_arr, mnu, TCMB)) )
421 
422  OmNuh2 = 0.01
423 
424  # Omeganuh2_to_Mnu
425  assert_( all_finite(ccl.nu_masses(OmNuh2, 'normal', TCMB)) )
426  assert_( all_finite(ccl.nu_masses(OmNuh2, 'inverted', TCMB)) )
427  assert_( all_finite(ccl.nu_masses(OmNuh2, 'equal', TCMB)) )
428  assert_( all_finite(ccl.nu_masses(OmNuh2, 'sum', TCMB)) )
429 
430  # Check that the right exceptions are raised
431  assert_raises(ValueError, ccl.Cosmology, Omega_c=0.27, Omega_b=0.045,
432  h=0.67, A_s=1e-10, n_s=0.96,
433  m_nu=[0.1, 0.2, 0.3, 0.4])
434  assert_raises(ValueError, ccl.Cosmology, Omega_c=0.27, Omega_b=0.045,
435  h=0.67, A_s=1e-10, n_s=0.96,
436  m_nu=[0.1, 0.2, 0.3],
437  mnu_type="sum")
438  assert_raises(ValueError, ccl.Cosmology, Omega_c=0.27, Omega_b=0.045,
439  h=0.67, A_s=1e-10, n_s=0.96,
440  m_nu=42)
441 
442 
443 def check_redshifts(cosmo):
444  """
445  Check that redshift functions can be run and produce finite values.
446  """
447  # Types of scale factor input (scalar, list, array)
448  a_scl = 0.5
449  a_lst = [0.2, 0.4, 0.6, 0.8, 1.]
450  a_arr = np.linspace(0.2, 1., 5)
451 
452  # Types of redshift input
453  z_scl = 0.5
454  z_lst = [0., 0.5, 1., 1.5, 2.]
455  z_arr = np.array(z_lst)
456 
457  # p(z) function for dNdz_tomog
458  def pz1(z_ph, z_s, args):
459  return np.exp(- (z_ph - z_s)**2. / 2.)
460 
461  # Lambda function p(z) function for dNdz_tomog
462  pz2 = lambda z_ph, z_s, args: np.exp(-(z_ph - z_s)**2. / 2.)
463 
464  # PhotoZFunction classes
465  PZ1 = ccl.PhotoZFunction(pz1)
466  PZ2 = ccl.PhotoZFunction(pz2)
467  PZ3 = ccl.PhotoZGaussian(sigma_z0=0.1)
468 
469  # dNdz (in terms of true redshift) function for dNdz_tomog
470  def dndz1(z, args):
471  return z**1.24 * np.exp(- (z / 0.51)**1.01)
472  # dNdzFunction classes
473  dNdZ1 = ccl.dNdzFunction(dndz1)
474  dNdZ2 = ccl.dNdzSmail(alpha = 1.24, beta = 1.01, z0 = 0.51)
475 
476  # Check that dNdz_tomog is finite with the various combinations
477  # of PhotoZ and dNdz functions
478  zmin = 0.
479  zmax = 1.
480 
481  assert_( all_finite(ccl.dNdz_tomog(z_scl, zmin, zmax, PZ1, dNdZ1)) )
482  assert_( all_finite(ccl.dNdz_tomog(z_lst, zmin, zmax, PZ1, dNdZ1)) )
483  assert_( all_finite(ccl.dNdz_tomog(z_arr, zmin, zmax, PZ1, dNdZ1)) )
484 
485  assert_( all_finite(ccl.dNdz_tomog(z_scl, zmin, zmax, PZ2, dNdZ1)) )
486  assert_( all_finite(ccl.dNdz_tomog(z_lst, zmin, zmax, PZ2, dNdZ1)) )
487  assert_( all_finite(ccl.dNdz_tomog(z_arr, zmin, zmax, PZ2, dNdZ1)) )
488 
489  assert_( all_finite(ccl.dNdz_tomog(z_scl, zmin, zmax, PZ3, dNdZ1)) )
490  assert_( all_finite(ccl.dNdz_tomog(z_lst, zmin, zmax, PZ3, dNdZ1)) )
491  assert_( all_finite(ccl.dNdz_tomog(z_arr, zmin, zmax, PZ3, dNdZ1)) )
492 
493  assert_( all_finite(ccl.dNdz_tomog(z_scl, zmin, zmax, PZ1, dNdZ2)) )
494  assert_( all_finite(ccl.dNdz_tomog(z_lst, zmin, zmax, PZ1, dNdZ2)) )
495  assert_( all_finite(ccl.dNdz_tomog(z_arr, zmin, zmax, PZ1, dNdZ2)) )
496 
497  assert_( all_finite(ccl.dNdz_tomog(z_scl, zmin, zmax, PZ2, dNdZ2)) )
498  assert_( all_finite(ccl.dNdz_tomog(z_lst, zmin, zmax, PZ2, dNdZ2)) )
499  assert_( all_finite(ccl.dNdz_tomog(z_arr, zmin, zmax, PZ2, dNdZ2)) )
500 
501  assert_( all_finite(ccl.dNdz_tomog(z_scl, zmin, zmax, PZ3, dNdZ2)) )
502  assert_( all_finite(ccl.dNdz_tomog(z_lst, zmin, zmax, PZ3, dNdZ2)) )
503  assert_( all_finite(ccl.dNdz_tomog(z_arr, zmin, zmax, PZ3, dNdZ2)) )
504 
505  # Wrong function type
506  assert_raises(TypeError, ccl.dNdz_tomog, z_scl, zmin, zmax, pz1, z_arr)
507  assert_raises(TypeError, ccl.dNdz_tomog, z_scl, zmin, zmax, z_arr, dNdZ1)
508  assert_raises(TypeError, ccl.dNdz_tomog, z_scl, zmin, zmax, None, None)
509 
510 def check_cls(cosmo):
511  """
512  Check that cls functions can be run.
513  """
514  # Number density input
515  z = np.linspace(0., 1., 200)
516  n = np.exp(-((z-0.5)/0.1)**2)
517 
518  # Bias input
519  b = np.sqrt(1. + z)
520 
521  # ell range input
522  ell_scl = 4
523  ell_lst = [2, 3, 4, 5, 6, 7, 8, 9]
524  ell_arr = np.arange(2, 10)
525 
526  # Check if power spectrum type is valid for CMB
527  cmb_ok = True
528  if cosmo._config.matter_power_spectrum_method \
529  == ccl.core.matter_power_spectrum_types['emu']: cmb_ok = False
530 
531  # ClTracer test objects
532  lens1 = ccl.WeakLensingTracer(cosmo, (z, n))
533  lens2 = ccl.WeakLensingTracer(cosmo, dndz=(z,n), ia_bias=(z, n), red_frac=(z,n))
534  nc1 = ccl.NumberCountsTracer(cosmo, False, dndz=(z,n), bias=(z,b))
535  nc2 = ccl.NumberCountsTracer(cosmo, True, dndz=(z,n), bias=(z,b))
536  nc3 = ccl.NumberCountsTracer(cosmo, True, dndz=(z,n), bias=(z,b), mag_bias=(z,b))
537  cmbl=ccl.CMBLensingTracer(cosmo, 1100.)
538 
539  assert_raises(ValueError, ccl.WeakLensingTracer, cosmo, None)
540  assert_raises(ValueError, ccl.NumberCountsTracer, cosmo, False, (z,n), None)
541 
542  # Check valid ell input is accepted
543  assert_( all_finite(ccl.angular_cl(cosmo, lens1, lens1, ell_scl)) )
544  assert_( all_finite(ccl.angular_cl(cosmo, lens1, lens1, ell_lst)) )
545  assert_( all_finite(ccl.angular_cl(cosmo, lens1, lens1, ell_arr)) )
546 
547  assert_( all_finite(ccl.angular_cl(cosmo, nc1, nc1, ell_scl)) )
548  assert_( all_finite(ccl.angular_cl(cosmo, nc1, nc1, ell_lst)) )
549  assert_( all_finite(ccl.angular_cl(cosmo, nc1, nc1, ell_arr)) )
550 
551  if cmb_ok: assert_( all_finite(ccl.angular_cl(cosmo, cmbl, cmbl, ell_arr)) )
552 
553  # Check non-limber calculations
554  assert_( all_finite(ccl.angular_cl(cosmo, nc1, nc1, ell_arr, l_limber=20)))
555 
556  # Check various cross-correlation combinations
557  assert_( all_finite(ccl.angular_cl(cosmo, lens1, lens2, ell_arr)) )
558  assert_( all_finite(ccl.angular_cl(cosmo, lens1, nc1, ell_arr)) )
559  assert_( all_finite(ccl.angular_cl(cosmo, lens1, nc2, ell_arr)) )
560  assert_( all_finite(ccl.angular_cl(cosmo, lens1, nc3, ell_arr)) )
561  if cmb_ok: assert_( all_finite(ccl.angular_cl(cosmo, lens1, cmbl, ell_arr)) )
562  assert_( all_finite(ccl.angular_cl(cosmo, lens2, nc1, ell_arr)) )
563  assert_( all_finite(ccl.angular_cl(cosmo, lens2, nc2, ell_arr)) )
564  assert_( all_finite(ccl.angular_cl(cosmo, lens2, nc3, ell_arr)) )
565  if cmb_ok: assert_( all_finite(ccl.angular_cl(cosmo, lens2, cmbl, ell_arr)) )
566  assert_( all_finite(ccl.angular_cl(cosmo, nc1, nc2, ell_arr)) )
567  assert_( all_finite(ccl.angular_cl(cosmo, nc1, nc3, ell_arr)) )
568  if cmb_ok: assert_( all_finite(ccl.angular_cl(cosmo, nc1, cmbl, ell_arr)) )
569  assert_( all_finite(ccl.angular_cl(cosmo, nc2, nc3, ell_arr)) )
570  if cmb_ok: assert_( all_finite(ccl.angular_cl(cosmo, nc2, cmbl, ell_arr)) )
571  if cmb_ok: assert_( all_finite(ccl.angular_cl(cosmo, nc3, cmbl, ell_arr)) )
572 
573  # Check that reversing order of ClTracer inputs works
574  assert_( all_finite(ccl.angular_cl(cosmo, nc1, lens1, ell_arr)) )
575  assert_( all_finite(ccl.angular_cl(cosmo, nc1, lens2, ell_arr)) )
576 
577 
578 
579 def check_cls_nu(cosmo):
580  """
581  Check that cls functions can be run.
582  """
583  # Number density input
584  z = np.linspace(0., 1., 200)
585  n = np.exp(-((z-0.5)/0.1)**2)
586 
587  # Bias input
588  b = np.sqrt(1. + z)
589 
590  # ell range input
591  ell_scl = 4
592  ell_lst = [2, 3, 4, 5, 6, 7, 8, 9]
593  ell_arr = np.arange(2, 10)
594 
595  # Check if power spectrum type is valid for CMB
596  cmb_ok = True
597  if cosmo._config.matter_power_spectrum_method \
598  == ccl.core.matter_power_spectrum_types['emu']: cmb_ok = False
599 
600  # ClTracer test objects
601  lens1 = ccl.WeakLensingTracer(cosmo, dndz=(z,n))
602  lens2 = ccl.WeakLensingTracer(cosmo, dndz=(z,n), ia_bias=(z,n), red_frac=(z,n))
603  nc1 = ccl.NumberCountsTracer(cosmo, False, dndz=(z,n), bias=(z,b))
604 
605  # Check that for massive neutrinos including rsd raises an error (not yet implemented)
606  assert_raises(CCLError, ccl.NumberCountsTracer, cosmo, True, dndz=(z,n), bias=(z,b))
607 
608  cmbl=ccl.CMBLensingTracer(cosmo,1100.)
609 
610  # Check valid ell input is accepted
611  assert_( all_finite(ccl.angular_cl(cosmo, lens1, lens1, ell_scl)) )
612  assert_( all_finite(ccl.angular_cl(cosmo, lens1, lens1, ell_lst)) )
613  assert_( all_finite(ccl.angular_cl(cosmo, lens1, lens1, ell_arr)) )
614 
615  assert_( all_finite(ccl.angular_cl(cosmo, nc1, nc1, ell_scl)) )
616  assert_( all_finite(ccl.angular_cl(cosmo, nc1, nc1, ell_lst)) )
617  assert_( all_finite(ccl.angular_cl(cosmo, nc1, nc1, ell_arr)) )
618 
619  if cmb_ok: assert_( all_finite(ccl.angular_cl(cosmo, cmbl, cmbl, ell_arr)) )
620 
621  # Check various cross-correlation combinations
622  assert_( all_finite(ccl.angular_cl(cosmo, lens1, lens2, ell_arr)) )
623  assert_( all_finite(ccl.angular_cl(cosmo, lens1, nc1, ell_arr)) )
624  if cmb_ok: assert_( all_finite(ccl.angular_cl(cosmo, lens1, cmbl, ell_arr)) )
625  assert_( all_finite(ccl.angular_cl(cosmo, lens2, nc1, ell_arr)) )
626  if cmb_ok: assert_( all_finite(ccl.angular_cl(cosmo, lens2, cmbl, ell_arr)) )
627  if cmb_ok: assert_( all_finite(ccl.angular_cl(cosmo, nc1, cmbl, ell_arr)) )
628 
629  # Check that reversing order of ClTracer inputs works
630  assert_( all_finite(ccl.angular_cl(cosmo, nc1, lens1, ell_arr)) )
631  assert_( all_finite(ccl.angular_cl(cosmo, nc1, lens2, ell_arr)) )
632 
633  # Check get_internal_function()
634  a_scl = 0.5
635  a_lst = [0.2, 0.4, 0.6, 0.8, 1.]
636  a_arr = np.linspace(0.2, 1., 5)
637  assert_( all_finite(nc1.get_internal_function(cosmo, 'dndz', a_scl)) )
638  assert_( all_finite(nc1.get_internal_function(cosmo, 'dndz', a_lst)) )
639  assert_( all_finite(nc1.get_internal_function(cosmo, 'dndz', a_arr)) )
640 
641  # Check that invalid options raise errors
642  assert_raises(ValueError, nc1.get_internal_function, cosmo, 'x', a_arr)
643  assert_raises(CCLError, ccl.NumberCountsTracer, cosmo, True,
644  dndz=(z,n), bias=(z,b))
645  assert_raises(ValueError, ccl.WeakLensingTracer, cosmo,
646  dndz=(z,n), ia_bias=(z,n))
647 
648 
649 def check_corr(cosmo):
650 
651  # Number density input
652  z = np.linspace(0., 1., 200)
653  n = np.ones(z.shape)
654 
655  # ClTracer test objects
656  lens1 = ccl.WeakLensingTracer(cosmo, dndz=(z, n))
657  lens2 = ccl.WeakLensingTracer(cosmo, dndz=(z,n), ia_bias=(z,n), red_frac=(z,n))
658 
659  ells = np.arange(3000)
660  cls = ccl.angular_cl(cosmo, lens1, lens2, ells)
661 
662  t_arr = np.logspace(-2., np.log10(5.), 20) # degrees
663  t_lst = [t for t in t_arr]
664  t_scl = 2.
665  t_int = 2
666 
667  # Make sure correlation functions work for valid inputs
668  corr1 = ccl.correlation(cosmo, ells, cls, t_arr, corr_type='L+',
669  method='FFTLog')
670  corr2 = ccl.correlation(cosmo, ells, cls, t_lst, corr_type='L+',
671  method='FFTLog')
672  corr3 = ccl.correlation(cosmo, ells, cls, t_scl, corr_type='L+',
673  method='FFTLog')
674  corr4 = ccl.correlation(cosmo, ells, cls, t_int, corr_type='L+',
675  method='FFTLog')
676  assert_( all_finite(corr1))
677  assert_( all_finite(corr2))
678  assert_( all_finite(corr3))
679  assert_( all_finite(corr4))
680 
681  # Check that exceptions are raised for invalid input
682  assert_raises(ValueError, ccl.correlation, cosmo, ells, cls, t_arr,
683  corr_type='xx', method='FFTLog')
684  assert_raises(ValueError, ccl.correlation, cosmo, ells, cls, t_arr,
685  corr_type='L+', method='xx')
686 
687 def check_corr_3d(cosmo):
688 
689  # Scale factor
690  a = 0.8
691 
692  # Distances (in Mpc)
693  r_int = 50
694  r = 50.
695  r_lst = np.linspace(50,100,10)
696 
697  # Make sure correlation functions work for valid inputs
698  corr1 = ccl.correlation_3d(cosmo, a, r_int)
699  corr2 = ccl.correlation_3d(cosmo, a, r)
700  corr3 = ccl.correlation_3d(cosmo, a, r_lst)
701  assert_( all_finite(corr1))
702  assert_( all_finite(corr2))
703  assert_( all_finite(corr3))
704 
705 
706 
708  """
709  Check that invalid transfer_function <-> matter_power_spectrum pairs raise
710  an error.
711  """
712  params = { 'Omega_c': 0.27, 'Omega_b': 0.045, 'h': 0.67,
713  'A_s': 1e-10, 'n_s': 0.96, 'w0': -1., 'wa': 0. }
714 
715  assert_raises(ValueError, ccl.Cosmology, transfer_function='emulator',
716  matter_power_spectrum='linear', **params)
717  #assert_raises(ValueError, ccl.Cosmology, transfer_function='boltzmann',
718  # matter_power_spectrum='halomodel', **params)
719  assert_raises(ValueError, ccl.Cosmology, transfer_function='bbks',
720  matter_power_spectrum='emu', **params)
721 
723  """
724  Test background and growth functions in ccl.background.
725  """
726  for cosmo in reference_models():
727  yield check_background, cosmo
728 
729  for cosmo_nu in reference_models_nu():
730  yield check_background_nu, cosmo_nu
731 
733  """
734  Test power spectrum and sigma functions in ccl.power.
735  """
736  for cosmo in reference_models():
737  yield check_power, cosmo
738 
739  for cosmo_nu in reference_models_nu():
740  yield check_power, cosmo_nu
741 
742 @decorators.slow
744  """
745  Test mass function and supporting functions.
746  """
747  for cosmo in reference_models():
748  yield check_massfunc, cosmo
749 
750  for cosmo_nu in reference_models_nu():
751  yield check_massfunc_nu, cosmo_nu
752 
754  """
755  Test halo model and supporting functions.
756  """
757  for cosmo in reference_models():
758  yield check_halomod, cosmo
759 
760 @decorators.slow
762  """
763  Test neutrino-related functions.
764  """
765  yield check_neutrinos
766 
768  """
769  Test redshifts module.
770  """
771  for cosmo in reference_models():
772  yield check_redshifts, cosmo
773 
774  for cosmo_nu in reference_models_nu():
775  yield check_redshifts, cosmo_nu
776 
777 @decorators.slow
778 def test_cls():
779  """
780  Test top-level functions in pyccl.cls module.
781  """
782  for cosmo in reference_models():
783  yield check_cls, cosmo
784 
785  for cosmo_nu in reference_models_nu():
786  yield check_cls_nu, cosmo_nu
787 
788 def test_corr():
789  """
790  Test top-level functions in pyccl.correlation module.
791  """
792  for cosmo in reference_models():
793  yield check_corr, cosmo
794 
795  for cosmo_nu in reference_models_nu():
796  yield check_corr, cosmo_nu
797 
798  for cosmo in reference_models():
799  yield check_corr_3d, cosmo
800 
801  for cosmo_nu in reference_models_nu():
802  yield check_corr_3d, cosmo_nu
803 
805  """
806  Test that debug mode can be toggled.
807  """
808  ccl.debug_mode(True)
809  ccl.debug_mode(False)
810 
811 
812 
813 if __name__ == '__main__':
814  run_module_suite()