In this notebook, we show a few usage examples for how to read and write Cosmology
objects to disk.
from pyccl import Cosmology
cosmo = Cosmology(Omega_c=0.25, Omega_b=0.05, sigma8=0.8, h=0.7, n_s=0.96)
print(cosmo.__doc__)
A cosmology including parameters and associated data. .. note:: Although some arguments default to `None`, they will raise a ValueError inside this function if not specified, so they are not optional. .. note:: The parameter Omega_g can be used to set the radiation density (not including relativistic neutrinos) to zero. Doing this will give you a model that is physically inconsistent since the temperature of the CMB will still be non-zero. Note however that this approximation is common for late-time LSS computations. .. note:: BCM stands for the "baryonic correction model" of Schneider & Teyssier (2015; https://arxiv.org/abs/1510.06034). See the `DESC Note <https://github.com/LSSTDESC/CCL/blob/master/doc/0000-ccl_note/main.pdf>`_ for details. Args: Omega_c (:obj:`float`): Cold dark matter density fraction. Omega_b (:obj:`float`): Baryonic matter density fraction. h (:obj:`float`): Hubble constant divided by 100 km/s/Mpc; unitless. A_s (:obj:`float`): Power spectrum normalization. Exactly one of A_s and sigma_8 is required. sigma8 (:obj:`float`): Variance of matter density perturbations at an 8 Mpc/h scale. Exactly one of A_s and sigma_8 is required. n_s (:obj:`float`): Primordial scalar perturbation spectral index. Omega_k (:obj:`float`, optional): Curvature density fraction. Defaults to 0. Omega_g (:obj:`float`, optional): Density in relativistic species except massless neutrinos. The default of `None` corresponds to setting this from the CMB temperature. Note that if a non-`None` value is given, this may result in a physically inconsistent model because the CMB temperature will still be non-zero in the parameters. Neff (:obj:`float`, optional): Effective number of massless neutrinos present. Defaults to 3.046. m_nu (:obj:`float`, optional): Total mass in eV of the massive neutrinos present. Defaults to 0. mnu_type (:obj:`str`, optional): The type of massive neutrinos. w0 (:obj:`float`, optional): First order term of dark energy equation of state. Defaults to -1. wa (:obj:`float`, optional): Second order term of dark energy equation of state. Defaults to 0. bcm_log10Mc (:obj:`float`, optional): One of the parameters of the BCM model. Defaults to `np.log10(1.2e14)`. bcm_etab (:obj:`float`, optional): One of the parameters of the BCM model. Defaults to 0.5. bcm_ks (:obj:`float`, optional): One of the parameters of the BCM model. Defaults to 55.0. df_mg (array_like, optional): Perturbations to the GR growth rate as a function of redshift :math:`\Delta f`. Used to implement simple modified growth scenarios. z_mg (array_like, optional): Array of redshifts corresponding to df_mg. transfer_function (:obj:`str`, optional): The transfer function to use. Defaults to 'boltzmann_class'. matter_power_spectrum (:obj:`str`, optional): The matter power spectrum to use. Defaults to 'halofit'. baryons_power_spectrum (:obj:`str`, optional): The correction from baryonic effects to be implemented. Defaults to 'nobaryons'. mass_function (:obj:`str`, optional): The mass function to use. Defaults to 'tinker10' (2010). halo_concentration (:obj:`str`, optional): The halo concentration relation to use. Defaults to Duffy et al. (2008) 'duffy2008'. emulator_neutrinos: `str`, optional): If using the emulator for the power spectrum, specified treatment of unequal neutrinos. Options are 'strict', which will raise an error and quit if the user fails to pass either a set of three equal masses or a sum with mnu_type = 'sum_equal', and 'equalize', which will redistribute masses to be equal right before calling the emualtor but results in internal inconsistencies. Defaults to 'strict'.
Cosmology objects can be saved to a YAML format using the write_yaml
method. This format is not currently very robust -- the exact order of the parameters must be maintained or the object cannot be read back in.
cosmo.write_yaml('example_params.yaml')
!cat example_params.yaml
Omega_c: 2.500000e-01 Omega_b: 5.000000e-02 Omega_m: 3.000000e-01 Omega_k: 0.000000e+00 k_sign: 0 w0: -1.000000e+00 wa: 0.000000e+00 H0: 7.000000e+01 h: 7.000000e-01 Neff: 3.046000e+00 N_nu_mass: 0 N_nu_rel: 3.046000e+00 sum_nu_masses: 0.000000e+00 Omega_n_mass: 0.000000e+00 Omega_n_rel: 3.488600e-05 A_s: nan n_s: 9.600000e-01 Omega_g: 5.043013e-05 T_CMB: 2.725000e+00 bcm_log10Mc: 1.407918e+01 bcm_etab: 5.000000e-01 bcm_ks: 5.500000e+01 sigma8: 8.000000e-01 Omega_l: 6.999147e-01 z_star: nan has_mgrowth: 0 nz_mgrowth: 0
The parameters can be read back in using the read_yaml
class method. Note that this must be called on the Cosmology
class itself, as shown below, and not an instance of the class.
cosmo2 = Cosmology.read_yaml("example_params.yaml")
print(cosmo2)
pyccl.Cosmology(wa=0.0, n_s=0.96, Neff=3.046, sigma8=0.8, w0=-1.0, bcm_etab=0.5, bcm_log10Mc=14.07918, A_s=None, h=0.7, Omega_k=0.0, bcm_ks=55.0, Omega_c=0.25, Omega_b=0.05, Omega_g=None, m_nu=0.0, mnu_type=None, z_mg=None, df_mg=None, matter_power_spectrum='halofit', baryons_power_spectrum='nobaryons', halo_concentration='duffy2008', transfer_function='boltzmann_class', mass_function='tinker10', emulator_neutrinos='strict')
This Cosmology
object can then be used to obtain cosmological predictions. See the other examples in this directory, for example Distance Calculations Example.ipynb or the more comprehensive demo SLAC Feb2018 Demo.ipynb.
Cosmology
objects are also pickle-able, to make them easy to store on disk and to pass around in MPI environments.
import pickle
with open('cosmo.pkl', 'wb') as fp:
pickle.dump(cosmo2, fp)
with open('cosmo.pkl', 'rb') as fp:
cosmo3 = pickle.load(fp)
print(cosmo3)
pyccl.Cosmology(Neff=3.046, A_s=None, wa=0.0, h=0.7, Omega_b=0.05, n_s=0.96, Omega_k=0.0, sigma8=0.8, w0=-1.0, Omega_c=0.25, bcm_etab=0.5, Omega_g=None, bcm_log10Mc=14.07918, bcm_ks=55.0, m_nu=0.0, mnu_type=None, z_mg=None, df_mg=None, matter_power_spectrum='halofit', halo_concentration='duffy2008', mass_function='tinker10', emulator_neutrinos='strict', baryons_power_spectrum='nobaryons', transfer_function='boltzmann_class')