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 (e.g. distances, power spectra). .. 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:: After instantiation, you can set parameters related to the internal splines and numerical integration accuracy by setting the values of the attributes of :obj:`Cosmology.cosmo.spline_params` and :obj:`Cosmology.cosmo.gsl_params`. For example, you can set the generic relative accuracy for integration by executing ``c = Cosmology(...); c.cosmo.gsl_params.INTEGRATION_EPSREL = 1e-5``. See the module level documentation of `pyccl.core` 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`): Curvature density fraction. Defaults to 0. Omega_g (:obj:`float`): 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`): Effective number of massless neutrinos present. Defaults to 3.044. m_nu (:obj:`float` or `array`): Mass in eV of the massive neutrinos present. Defaults to 0. If a sequence is passed, it is assumed that the elements of the sequence represent the individual neutrino masses. mass_split (:obj:`str`): Type of massive neutrinos. Should be one of 'single', 'equal', 'normal', 'inverted'. 'single' treats the mass as being held by one massive neutrino. The other options split the mass into 3 massive neutrinos. Ignored if a sequence is passed in m_nu. Default is 'normal'. w0 (:obj:`float`): First order term of dark energy equation of state. Defaults to -1. wa (:obj:`float`): Second order term of dark energy equation of state. Defaults to 0. T_CMB (:obj:`float`): The CMB temperature today. The default of is 2.725. transfer_function (:obj:`str` or :class:`~pyccl.emulators.emu_base.EmulatorPk`): The transfer function to use. Defaults to 'boltzmann_camb'. matter_power_spectrum (:obj:`str` or :class:`~pyccl.emulators.emu_base.EmulatorPk`): The matter power spectrum to use. Defaults to 'halofit'. baryonic_effects (:class:`~pyccl.baryons.baryons_base.Baryons` or `None`): The baryonic effects model to use. Options are `None` (no baryonic effects), or a :class:`~pyccl.baryons.baryons_base.Baryons` object. mg_parametrization (:class:`~pyccl.modified_gravity.modified_gravity_base.ModifiedGravity` or `None`): The modified gravity parametrization to use. Options are `None` (no MG), or a :class:`~pyccl.modified_gravity.modified_gravity_base.ModifiedGravity` object. Currently, only :class:`~pyccl.modified_gravity.MuSigmaMG` is supported. extra_parameters (:obj:`dict`): Dictionary holding extra parameters. Currently supports extra parameters for CAMB. Details described below. Defaults to None. T_ncdm (:obj:`float`): Non-CDM temperature in units of photon temperature. The default is 0.71611. Currently supported extra parameters for CAMB are: * `halofit_version` * `HMCode_A_baryon` * `HMCode_eta_baryon` * `HMCode_logT_AGN` * `kmax` * `lmax` * `dark_energy_model` Consult the CAMB documentation for their usage. These parameters are passed in a :obj:`dict` to `extra_parameters` as:: extra_parameters = {"camb": {"halofit_version": "mead2020_feedback", "HMCode_logT_AGN": 7.8}} .. note :: If using camb to compute the non-linear power spectrum with HMCode to include baryonic effects, you should not include any extra baryonic effects (i.e. set `baryonic_effects=None`).
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: 0.25 Omega_b: 0.05 h: 0.7 n_s: 0.96 sigma8: 0.8 A_s: null Omega_k: 0.0 Omega_g: null Neff: 3.044 m_nu: 0.0 mass_split: normal w0: -1.0 wa: 0.0 T_CMB: 2.7255 T_ncdm: 0.71611 extra_parameters: {} transfer_function: boltzmann_camb matter_power_spectrum: halofit
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.Cosmology> Neff = 3.044 Omega_b = 0.05 Omega_c = 0.25 h = 0.7 n_s = 0.96 sigma8 = 0.8 extra_parameters = HASH_ACCURACY_PARAMS = 0xb6e46c24158c0e30
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.Cosmology> Neff = 3.044 Omega_b = 0.05 Omega_c = 0.25 h = 0.7 n_s = 0.96 sigma8 = 0.8 extra_parameters = HASH_ACCURACY_PARAMS = 0xb6e46c24158c0e30