# Cosmology¶

This module is an implementation of the standard FLRW cosmology with a number of dark energy models including $$\Lambda CDM$$, wCDM, and varying dark energy equations of state. The cosmology object models the contributions from dark matter, baryons, curvature, photons, neutrinos, and dark energy.

## Basics¶

In Colossus, the cosmology is set globally, and all functions respect that global cosmology. Colossus does not set a default cosmology, meaning that the user must set a cosmology before using any cosmological functions or any other functions that rely on the Cosmology module. This documentation contains coding examples of the most common operations. Much more extensive code samples can be found in the Tutorials.

### Setting and getting cosmologies¶

First, we import the cosmology module:

from colossus.cosmology import cosmology


Setting a cosmology is almost always achieved with the setCosmology() function, which can be used in multiple ways:

• Set one of the pre-defined cosmologies:

cosmology.setCosmology('planck18')

• Set one of the pre-defined cosmologies, but overwrite certain parameters:

cosmology.setCosmology('planck18', {'print_warnings': False})

• Add a new cosmology to the global list of available cosmologies. This has the advantage that the new cosmology can be set from anywhere in the code. Only the main cosmological parameters are mandatory, all other parameters can be left to their default values:

params = {'flat': True, 'H0': 67.2, 'Om0': 0.31, 'Ob0': 0.049, 'sigma8': 0.81, 'ns': 0.95}
cosmo = cosmology.setCosmology('myCosmo')

• Set a new cosmology without adding it to the global list of available cosmologies:

params = {'flat': True, 'H0': 67.2, 'Om0': 0.31, 'Ob0': 0.049, 'sigma8': 0.81, 'ns': 0.95}
cosmo = cosmology.setCosmology('myCosmo', params)

• Set a self-similar cosmology with a power-law power spectrum of a certain slope, and the default settings set in the powerlaw cosmology:

cosmo = cosmology.setCosmology('powerlaw_-2.60')


Whichever way a cosmology is set, the current cosmology is stored in a global variable and can be obtained at any time:

cosmo = cosmology.getCurrent()


For more extensive examples, please see the Tutorials.

### Changing and switching cosmologies¶

The current cosmology can also be set to an already existing cosmology object, for example when switching between cosmologies:

cosmo1 = cosmology.setCosmology('WMAP9')
cosmo2 = cosmology.setCosmology('planck18')
cosmology.setCurrent(cosmo1)


The user can change the cosmological parameters of an existing cosmology object at run-time, but MUST call the update function checkForChangedCosmology() directly after the changes. This function ensures that the parameters are consistent (e.g., flatness) and that no outdated cached quantities are used:

cosmo = cosmology.setCosmology('WMAP9')
cosmo.Om0 = 0.31
cosmo.checkForChangedCosmology()


### Summary of getter and setter functions¶

 setCosmology(cosmo_name[, params]) Set a cosmology. addCosmology(cosmo_name, params) Add a set of cosmological parameters to the global list. setCurrent(cosmo) Set the current global cosmology to a cosmology object. getCurrent() Get the current global cosmology.

## Standard cosmologies¶

The following sets of cosmological parameters can be chosen using the setCosmology() function:

ID Paper Location Explanation
planck18-only Planck Collab. 2018 Table 2 Best-fit, Planck only (column 5)
planck18 Planck Collab. 2018 Table 2 Best-fit with BAO (column 6)
planck15-only Planck Collab. 2015 Table 4 Best-fit, Planck only (column 2)
planck15 Planck Collab. 2015 Table 4 Best-fit with ext (column 6)
planck13-only Planck Collab. 2013 Table 2 Best-fit, Planck only
planck13 Planck Collab. 2013 Table 5 Best-fit with BAO etc.
WMAP9-only Hinshaw et al. 2013 Table 2 Max. likelihood, WMAP only
WMAP9-ML Hinshaw et al. 2013 Table 2 Max. likelihood, with eCMB, BAO and H0
WMAP9 Hinshaw et al. 2013 Table 4 Best-fit, with eCMB, BAO and H0
WMAP7-only Komatsu et al. 2011 Table 1 Max. likelihood, WMAP only
WMAP7-ML Komatsu et al. 2011 Table 1 Max. likelihood, with BAO and H0
WMAP7 Komatsu et al. 2011 Table 1 Best-fit, with BAO and H0
WMAP5-only Komatsu et al. 2009 Table 1 Max. likelihood, WMAP only
WMAP5-ML Komatsu et al. 2009 Table 1 Max. likelihood, with BAO and SN
WMAP5 Komatsu et al. 2009 Table 1 Best-fit, with BAO and SN
WMAP3-ML Spergel et al. 2007 Table 2 Max.likelihood, WMAP only
WMAP3 Spergel et al. 2007 Table 5 Best fit, WMAP only
WMAP1-ML Spergel et al. 2003 Table 1/4 Max.likelihood, WMAP only
WMAP1 Spergel et al. 2003 Table 7/4 Best fit, WMAP only
illustris Vogelsberger et al. 2014 Cosmology of the Illustris simulation
bolshoi Klypin et al. 2011 Cosmology of the Bolshoi simulation
multidark-planck Klypin et al. 2016 Table 1 Cosmology of the Multidark-Planck simulations
millennium Springel et al. 2005 Cosmology of the Millennium simulation
EdS Einstein-de Sitter cosmology
powerlaw Default settings for power-law cosms.

Those cosmologies that refer to particular simulations (such as bolshoi and millennium) are generally set to ignore relativistic species, i.e. photons and neutrinos, because they are not modeled in the simulations. The EdS cosmology refers to an Einstein-de Sitter model, i.e. a flat cosmology with only dark matter and $$\Omega_{\rm m} = 1$$.

## Dark energy and curvature¶

All the default parameter sets above represent flat $$\Lambda CDM$$ cosmologies, i.e. model dark energy as a cosmological constant and contain no curvature. To add curvature, the default for flatness must be overwritten, and the dark energy content of the universe must be set (which is otherwise computed from the matter and relativistic contributions):

params = cosmology.cosmologies['planck18']
params['flat'] = False
params['Ode0'] = 0.75
cosmo = cosmology.setCosmology('planck_curvature', params)


Multiple models for the dark energy equation of state parameter $$w(z)$$ are implemented, namely a cosmological constant ($$w=-1$$), a constant $$w$$, a linearly varying $$w(z) = w_0 + w_a (1 - a)$$, and arbitrary user-supplied functions for $$w(z)$$. To set, for example, a linearly varying EOS, we change the de_model parameter:

params = cosmology.cosmologies['planck18']
params['de_model'] = 'w0wa'
params['w0'] = -0.8
params['wa'] = 0.1
cosmo = cosmology.setCosmology('planck_w0wa', params)


We can implement more exotic models by supplying an arbitrary function:

def wz_func(z):
return -1.0 + 0.1 * z

params = cosmology.cosmologies['planck18']
params['de_model'] = 'user'
params['wz_function'] = wz_func
cosmo = cosmology.setCosmology('planck_wz', params)


## Power spectrum models¶

By default, Colossus relies on fitting functions for the matter power spectrum which, in turn, is the basis for the variance and correlation function. These models are implemented in the power_spectrum module, documented at the bottom of this file.

## Derivatives and inverses¶

Almost all cosmology functions that are interpolated (e.g., age(), luminosityDistance() or sigma()) can be evaluated as an nth derivative. Please note that some functions are interpolated in log space, resulting in a logarithmic derivative, while others are interpolated and differentiated in linear space. Please see the function documentations below for details.

The derivative functions were not systematically tested for accuracy. Their accuracy will depend on how well the function in question is represented by the interpolating spline approximation. In general, the accuracy of the derivatives will be worse that the error quoted on the function itself, and get worse with the order of the derivative.

Furthermore, the inverse of interpolated functions can be evaluated by passing inverse = True. In this case, for a function y(x), x(y) is returned instead. Those functions raise an Exception if the requested value lies outside the range of the interpolating spline.

The inverse and derivative flags can be combined to give the derivative of the inverse, i.e. dx/dy. Once again, please check the function documentation whether that derivative is in linear or logarithmic units.

## Performance optimization and accuracy¶

This module is optimized for fast performance, particularly in computationally intensive functions such as the correlation function. Almost all quantities are, by default, tabulated, stored in files, and re-loaded when the same cosmology is set again (see the storage module for details). For some rare applications (for example, MCMC chains where functions are evaluated few times, but for a large number of cosmologies), the user can turn this behavior off:

cosmo = cosmology.setCosmology('planck18', {'interpolation': False, 'persistence': ''})


For more details, please see the documentation of the interpolation and persistence parameters. In order to turn off the interpolation temporarily, the user can simply switch the interpolation parameter off:

cosmo.interpolation = False
Pk = cosmo.matterPowerSpectrum(k)
cosmo.interpolation = True


In this example, the power spectrum is evaluated directly without interpolation. The interpolation is fairly accurate (see specific notes in the function documentation), meaning that it is very rarely necessary to use the exact routines.

## Module reference¶

class cosmology.cosmology.Cosmology(name=None, flat=True, Om0=None, Ode0=None, Ob0=None, H0=None, sigma8=None, ns=None, de_model='lambda', w0=None, wa=None, wz_function=None, relspecies=True, Tcmb0=2.7255, Neff=3.046, power_law=False, power_law_n=0.0, interpolation=True, persistence='rw', print_info=False, print_warnings=True)

A cosmology is set via the parameters passed to the constructor. Any parameter whose default value is None must be set by the user. The easiest way to set these parameters is to use the setCosmology() function with one of the pre-defined sets of cosmological parameters listed above.

In addition, the user can choose between different equations of state for dark energy, including an arbitrary $$w(z)$$ function.

A few cosmological parameters are free in principle, but are well constrained and have a sub-dominant impact on the computations. For such parameters, default values are pre-set so that the user does not have to choose them manually. This includes the CMB temperature today (Tcmb0 = 2.7255 K, Fixsen 2009) and the effective number of neutrino species (Neff = 3.046, Planck Collaboration 2018). These values are compatible with the most recent observational measurements and can be changed by the user if necessary.

Parameters: name: str A name for the cosmology, e.g. WMAP9 or a user-defined name. If a user-defined set of cosmological parameters is used, it is advisable to use a name that does not represent any of the pre-set cosmologies. flat: bool If flat, there is no curvature, $$\Omega_{\rm k} = 0$$, and the dark energy content of the universe is computed as $$\Omega_{\rm de} = 1 - \Omega_{\rm m} - \Omega_{\gamma} - \Omega_{\nu}$$ where $$\Omega_{\rm m}$$ is the density of matter (dark matter and baryons) in units of the critical density, $$\Omega_{\gamma}$$ is the density of photons, and $$\Omega_{\nu}$$ the density of neutrinos. If flat == False, the Ode0 parameter must be passed. Om0: float $$\Omega_{\rm m}$$, the matter density in units of the critical density at z = 0 (includes all non-relativistic matter, i.e., dark matter and baryons but not neutrinos). Ode0: float $$\Omega_{\rm de}$$, the dark energy density in units of the critical density at z = 0. This parameter is ignored if flat == True. Ob0: float $$\Omega_{\rm b}$$, the baryon density in units of the critical density at z = 0. H0: float The Hubble constant in km/s/Mpc. sigma8: float The normalization of the power spectrum, i.e. the variance when the field is filtered with a top hat filter of radius 8 Mpc/h. See the sigma() function for details on the variance. ns: float The tilt of the primordial power spectrum. de_model: str An identifier indicating which dark energy equation of state is to be used. The DE equation of state can either be a cosmological constant (de_model = lambda), a constant w (de_model = w0, the w0 parameter must be set), a linear function of the scale factor according to the parameterization of Linder 2003 where $$w(z) = w_0 + w_a (1 - a)$$ (de_model = w0wa, the w0 and wa parameters must be set), or a function supplied by the user (de_model = user). In the latter case, the w(z) function must be passed using the wz_function parameter. w0: float If de_model == w0, this variable gives the constant dark energy equation of state parameter w. If de_model == w0wa, this variable gives the constant component w (see de_model parameter). wa: float If de_model == w0wa, this variable gives the varying component of w, otherwise it is ignored (see de_model parameter). wz_function: function If de_model == user, this field must give a function that represents the dark energy equation of state. This function must take z as its only input variable and return w(z). relspecies: bool If relspecies == False, all relativistic contributions to the energy density of the universe (such as photons and neutrinos) are ignored. If relspecies == True, their energy densities are computed based on the Tcmb0 and Neff parameters. Tcmb0: float The temperature of the CMB at z = 0 in Kelvin. Neff: float The effective number of neutrino species. power_law: bool Create a self-similar cosmology with a power-law matter power spectrum, $$P(k) = k^{\rm power\_law\_n}$$. power_law_n: float See power_law. interpolation: bool By default, lookup tables are created for certain computationally intensive quantities, cutting down the computation times for future calculations. If interpolation == False, all interpolation is switched off. This can be useful when evaluating quantities for many different cosmologies (where computing the tables takes a prohibitively long time). However, many functions will be much slower if this setting is False, and the derivatives and inverses will not work. Thus, please use interpolation == False only if absolutely necessary. persistence: str By default, interpolation tables and other data are stored in a permanent file for each cosmology. This avoids re-computing the tables when the same cosmology is set again. However, if either read or write file access is to be avoided (for example in MCMC chains), the user can set this parameter to any combination of read ('r') and write ('w'), such as 'rw' (read and write, the default), 'r' (read only), 'w' (write only), or '' (no persistence). print_info: bool Output information to the console. print_warnings: bool Output warnings to the console.

Methods

 Ez(z) The Hubble parameter as a function of redshift, in units of $$H_0$$. Hz(z) The Hubble parameter as a function of redshift. Ob(z) The baryon density of the universe, in units of the critical density. Ode(z) The dark energy density of the universe, in units of the critical density. Ogamma(z) The density of photons in the universe, in units of the critical density. Ok(z) The curvature density of the universe in units of the critical density. Om(z) The matter density of the universe, in units of the critical density. Onu(z) The density of neutrinos in the universe, in units of the critical density. Or(z) The density of relativistic species, in units of the critical density. age(z[, derivative, inverse]) The age of the universe at redshift z. angularDiameterDistance(z[, derivative]) The angular diameter distance to redshift z. checkForChangedCosmology() Check whether the cosmological parameters have been changed by the user. comovingDistance([z_min, z_max, transverse]) The transverse or line-of-sight comoving distance. correlationFunction(R[, z, derivative, ps_args]) The linear matter-matter correlation function at radius R. distanceModulus(z) The distance modulus to redshift z in magnitudes. filterFunction(filt, k, R) The Fourier transform of certain filter functions. getName() Return the name of this cosmology. growthFactor(z[, derivative, inverse]) The linear growth factor normalized to z = 0, $$D_+(z) / D_+(0)$$. growthFactorUnnormalized(z) The linear growth factor, $$D_+(z)$$. hubbleTime(z) The Hubble time, $$1/H(z)$$. lookbackTime(z[, derivative, inverse]) The lookback time since redshift z. luminosityDistance(z[, derivative, inverse]) The luminosity distance to redshift z. matterPowerSpectrum(k[, z, model, path, …]) The matter power spectrum at a scale k. rho_b(z) The baryon density of the universe at redshift z. rho_c(z) The critical density of the universe at redshift z. rho_de(z) The dark energy density of the universe at redshift z. rho_gamma(z) The photon density of the universe at redshift z. rho_m(z) The matter density of the universe at redshift z. rho_nu(z) The neutrino density of the universe at redshift z. rho_r(z) The density of relativistic species in the universe at redshift z. sigma(R, z[, j, filt, inverse, derivative, …]) The rms variance of the linear density field on a scale R, $$\sigma(R)$$. soundHorizon() The sound horizon at recombination. wz(z) The dark energy equation of state parameter.
getName()

Return the name of this cosmology.

checkForChangedCosmology()

Check whether the cosmological parameters have been changed by the user. If there are changes, all pre-computed quantities (e.g., interpolation tables) are discarded and re-computed if necessary.

Ez(z)

The Hubble parameter as a function of redshift, in units of $$H_0$$.

Parameters: z: array_like Redshift; can be a number or a numpy array. E: array_like $$H(z) / H_0$$; has the same dimensions as z.

Hz
The Hubble parameter as a function of redshift.
Hz(z)

The Hubble parameter as a function of redshift.

Parameters: z: array_like Redshift; can be a number or a numpy array. H: array_like $$H(z)$$ in units of km/s/Mpc; has the same dimensions as z.

Ez
The Hubble parameter as a function of redshift, in units of $$H_0$$.
wz(z)

The dark energy equation of state parameter.

The EOS parameter is defined as $$w(z) = P(z) / \rho(z)$$. Depending on its chosen functional form (see the de_model parameter to Cosmology()), w(z) can be -1, another constant, a linear function of a, or an arbitrary function chosen by the user.

Parameters: z: array_like Redshift; can be a number or a numpy array. w: array_like $$w(z)$$, has the same dimensions as z.
hubbleTime(z)

The Hubble time, $$1/H(z)$$.

Parameters: z: array_like Redshift; can be a number or a numpy array. tH: float $$1/H$$ in units of Gyr; has the same dimensions as z.

lookbackTime
The lookback time since z.
age
The age of the universe at redshift z.
lookbackTime(z, derivative=0, inverse=False)

The lookback time since redshift z.

The lookback time corresponds to the difference between the age of the universe at redshift z and today.

Parameters: z: array_like Redshift, where $$-0.995 < z < 200$$; can be a number or a numpy array. derivative: int If greater than 0, evaluate the nth derivative, $$d^nt/dz^n$$. inverse: bool If True, evaluate $$z(t)$$ instead of $$t(z)$$. In this case, the z field must contain the time(s) in Gyr. t: array_like The lookback time (or its derivative) since z in units of Gigayears; has the same dimensions as z.

hubbleTime
The Hubble time, $$1/H_0$$.
age
The age of the universe at redshift z.
age(z, derivative=0, inverse=False)

The age of the universe at redshift z.

Parameters: z: array_like Redshift, where $$-0.995 < z < 200$$; can be a number or a numpy array. derivative: int If greater than 0, evaluate the nth derivative, $$d^nt/dz^n$$. inverse: bool If True, evaluate $$z(t)$$ instead of $$t(z)$$. In this case, the z field must contain the time(s) in Gyr. t: array_like The age of the universe (or its derivative) at redshift z in Gigayears; has the same dimensions as z.

hubbleTime
The Hubble time, $$1/H_0$$.
lookbackTime
The lookback time since z.
comovingDistance(z_min=0.0, z_max=0.0, transverse=True)

The transverse or line-of-sight comoving distance.

This function returns the comoving distance between two points. Depending on the chosen geometry, the output can have two different meanings. If transverse = False, the line-of-sight distance is returned,

$d_{\rm com,los}(z) = \frac{c}{H_0} \int_{0}^{z} \frac{1}{E(z)} .$

However, if transverse = False, the function returns the comoving distance between two points separated by an angle of one radian at z_max (if z_min is zero). This quantity depends on the spatial curvature of the universe,

$\begin{split}d_{\rm com,trans}(z) = \left\{ \begin{array}{ll} \frac{c/H_0}{\sqrt{\Omega_{\rm k,0}}} \sinh \left(\frac{\sqrt{\Omega_{\rm k,0}}}{c/H_0} d_{\rm com,los} \right) & \forall \, \Omega_{\rm k,0} > 0 \\ d_{\rm com,los} & \forall \, \Omega_{\rm k,0} = 0 \\ \frac{c/H_0}{\sqrt{-\Omega_{\rm k,0}}} \sin \left(\frac{\sqrt{-\Omega_{\rm k,0}}}{c/H_0} d_{\rm com,los} \right) & \forall \, \Omega_{\rm k,0} < 0 \\ \end{array} \right. \end{split}$

In Colossus, this distance is referred to as the “transverse comoving distance” (e.g., Hogg 1999), but a number of other terms are used in the literature, e.g., “comoving angular diameter distance” (Dodelson 2003), “comoving coordinate distance” (Mo et al. 2010),or “angular size distance” (Peebles 1993). The latter is not to be confused with the angular diameter distance.

Either z_min or z_max can be a numpy array; in those cases, the same z_min / z_max is applied to all values of the other. If both are numpy arrays, they need to have the same dimensions, and the comoving distance returned corresponds to a series of different z_min and z_max values.

This function does not use interpolation (unlike the other distance functions) because it accepts both z_min and z_max parameters which would necessitate a 2D interpolation. Thus, for fast evaluation, the luminosityDistance() and angularDiameterDistance() functions should be used.

Parameters: zmin: array_like Redshift; can be a number or a numpy array. zmax: array_like Redshift; can be a number or a numpy array. transverse: bool Whether to return the transverse of line-of-sight comoving distance. The two are the same in flat cosmologies. d: array_like The comoving distance in Mpc/h; has the same dimensions as zmin and/or zmax.

luminosityDistance
The luminosity distance to redshift z.
angularDiameterDistance
The angular diameter distance to redshift z.
luminosityDistance(z, derivative=0, inverse=False)

The luminosity distance to redshift z.

Parameters: z: array_like Redshift, where $$-0.995 < z < 200$$; can be a number or a numpy array. derivative: int If greater than 0, evaluate the nth derivative, $$d^nD/dz^n$$. inverse: bool If True, evaluate $$z(D)$$ instead of $$D(z)$$. In this case, the z field must contain the luminosity distance in Mpc/h. d: array_like The luminosity distance (or its derivative) in Mpc/h; has the same dimensions as z.

comovingDistance
The comoving distance between redshift $$z_{\rm min}$$ and $$z_{\rm max}$$.
angularDiameterDistance
The angular diameter distance to redshift z.
angularDiameterDistance(z, derivative=0)

The angular diameter distance to redshift z.

The angular diameter distance is the transverse distance that, at redshift z, corresponds to an angle of one radian. Note that the inverse is not available for this function because it is not strictly increasing or decreasing with redshift, making its inverse multi-valued.

Parameters: z: array_like Redshift, where $$-0.995 < z < 200$$; can be a number or a numpy array. derivative: int If greater than 0, evaluate the nth derivative, $$d^nD/dz^n$$. d: array_like The angular diameter distance (or its derivative) in Mpc/h; has the same dimensions as z.

comovingDistance
The comoving distance between redshift $$z_{\rm min}$$ and $$z_{\rm max}$$.
luminosityDistance
The luminosity distance to redshift z.
distanceModulus(z)

The distance modulus to redshift z in magnitudes.

Parameters: z: array_like Redshift; can be a number or a numpy array. mu: array_like The distance modulus in magnitudes; has the same dimensions as z.
soundHorizon()

The sound horizon at recombination.

This function returns the sound horizon in Mpc/h, according to Eisenstein & Hu 1998, Equation 26. This fitting function is accurate to 2% where $$\Omega_{\rm b} h^2 > 0.0125$$ and $$0.025 < \Omega_{\rm m} h^2 < 0.5$$.

Returns: s: float The sound horizon at recombination in Mpc/h.
rho_c(z)

The critical density of the universe at redshift z.

Parameters: z: array_like Redshift; can be a number or a numpy array. rho_critical: array_like The critical density in units of physical $$M_{\odot} h^2 / {\rm kpc}^3$$; has the same dimensions as z.
rho_m(z)

The matter density of the universe at redshift z.

Parameters: z: array_like Redshift; can be a number or a numpy array. rho_matter: array_like The matter density in units of physical $$M_{\odot} h^2 / {\rm kpc}^3$$; has the same dimensions as z.

Om
The matter density of the universe, in units of the critical density.
rho_b(z)

The baryon density of the universe at redshift z.

Parameters: z: array_like Redshift; can be a number or a numpy array. rho_baryon: array_like The baryon density in units of physical $$M_{\odot} h^2 / {\rm kpc}^3$$; has the same dimensions as z.

Ob
The baryon density of the universe, in units of the critical density.
rho_de(z)

The dark energy density of the universe at redshift z.

Parameters: z: array_like Redshift; can be a number or a numpy array. rho_de: float The dark energy density in units of physical $$M_{\odot} h^2 / {\rm kpc}^3$$; has the same dimensions as z.

Ode
The dark energy density of the universe, in units of the critical density.
rho_gamma(z)

The photon density of the universe at redshift z.

If relspecies == False, this function returns 0.

Parameters: z: array_like Redshift; can be a number or a numpy array. rho_gamma: array_like The photon density in units of physical $$M_{\odot} h^2 / {\rm kpc}^3$$; has the same dimensions as z.

Ogamma
The density of photons in the universe, in units of the critical density.
rho_nu(z)

The neutrino density of the universe at redshift z.

If relspecies == False, this function returns 0.

Parameters: z: array_like Redshift; can be a number or a numpy array. rho_nu: array_like The neutrino density in units of physical $$M_{\odot} h^2 / {\rm kpc}^3$$; has the same dimensions as z.

Onu
The density of neutrinos in the universe, in units of the critical density.
rho_r(z)

The density of relativistic species in the universe at redshift z.

This density is the sum of the photon and neutrino densities. If relspecies == False, this function returns 0.

Parameters: z: array_like Redshift; can be a number or a numpy array. rho_relativistic: array_like The density of relativistic species in units of physical $$M_{\odot} h^2 / {\rm kpc}^3$$; has the same dimensions as z.

Or
The density of relativistic species in the universe, in units of the critical density.
Om(z)

The matter density of the universe, in units of the critical density.

Parameters: z: array_like Redshift; can be a number or a numpy array. Omega_matter: array_like Has the same dimensions as z.

rho_m
The matter density of the universe at redshift z.
Ob(z)

The baryon density of the universe, in units of the critical density.

Parameters: z: array_like Redshift; can be a number or a numpy array. Omega_baryon: array_like Has the same dimensions as z.

rho_b
The baryon density of the universe at redshift z.
Ode(z)

The dark energy density of the universe, in units of the critical density.

Parameters: z: array_like Redshift; can be a number or a numpy array. Omega_de: array_like Has the same dimensions as z.

rho_de
The dark energy density of the universe at redshift z.
Ogamma(z)

The density of photons in the universe, in units of the critical density.

Parameters: z: array_like Redshift; can be a number or a numpy array. Omega_gamma: array_like Has the same dimensions as z.

rho_gamma
The photon density of the universe at redshift z.
Onu(z)

The density of neutrinos in the universe, in units of the critical density.

Parameters: z: array_like Redshift; can be a number or a numpy array. Omega_nu: array_like Has the same dimensions as z.

rho_nu
The neutrino density of the universe at redshift z.
Or(z)

The density of relativistic species, in units of the critical density.

This function returns the sum of the densities of photons and neutrinos.

Parameters: z: array_like Redshift; can be a number or a numpy array. Omega_relativistic: array_like Has the same dimensions as z.

rho_r
The density of relativistic species in the universe at redshift z.
Ok(z)

The curvature density of the universe in units of the critical density.

In a flat universe, $$\Omega_{\rm k} = 0$$.

Parameters: z: array_like Redshift; can be a number or a numpy array. Omega_curvature: array_like Has the same dimensions as z.
growthFactorUnnormalized(z)

The linear growth factor, $$D_+(z)$$.

The growth factor describes the linear evolution of over- and underdensities in the dark matter density field. There are three regimes:

• In the matter-radiation regime, we use an approximate analytical formula (Equation 5 in Gnedin et al. 2011. If relativistic species are ignored, $$D_+(z) \propto a$$.
• In the matter-dominated regime, $$D_+(z) \propto a$$.
• In the matter-dark energy regime, we evaluate $$D_+(z)$$ through integration as defined in Eisenstein & Hu 1999, Equation 8 (see also Heath 1977) for LCDM cosmologies. For cosmologies where $$w(z) \neq -1$$, this expression is not valid and we instead solve the ordinary differential equation for the evolution of the growth factor (Equation 11 in Linder & Jenkins 2003).

At the transition between the integral and analytic approximation regimes, the two expressions do not quite match up, with differences of the order <1E-3. in order to avoid a discontinuity, we introduce a transition regime where the two quantities are linearly interpolated.

The normalization is such that the growth factor approaches $$D_+(a) = a$$ in the matter-dominated regime. There are other normalizations of the growth factor (e.g., Percival 2005, Equation 15), but since we almost always care about the growth factor normalized to z = 0, the normalization does not matter too much (see the growthFactor() function).

Parameters: z: array_like Redshift, where $$-0.995 < z$$; the high end of z is only limited by the validity of the analytical approximation mentioned above. Can be a number or a numpy array. D: array_like The linear growth factor; has the same dimensions as z.

Warning

This function directly evaluates the growth factor by integration or analytical approximation. In most cases, the growthFactor() function should be used since it interpolates and is thus much faster.

growthFactor
The linear growth factor normalized to z = 0, $$D_+(z) / D_+(0)$$.
growthFactor(z, derivative=0, inverse=False)

The linear growth factor normalized to z = 0, $$D_+(z) / D_+(0)$$.

The growth factor describes the linear evolution of over- and underdensities in the dark matter density field. This function is sped up through interpolation which barely degrades its accuracy, but if you wish to evaluate the exact integral or compute the growth factor for very high redshifts (z > 200), please use the growthFactorUnnormalized() function.

Parameters: z: array_like Redshift, where $$-0.995 < z < 200$$; can be a number or a numpy array. derivative: int If greater than 0, evaluate the nth derivative, $$d^nD_+/dz^n$$. inverse: bool If True, evaluate $$z(D_+)$$ instead of $$D_+(z)$$. In this case, the z field must contain the normalized growth factor. D: array_like The linear growth factor (or its derivative); has the same dimensions as z.

growthFactorUnnormalized
The linear growth factor, $$D_+(z)$$.
matterPowerSpectrum(k, z=0.0, model='eisenstein98', path=None, derivative=False)

The matter power spectrum at a scale k.

By default, the power spectrum is computed using a model for the transfer function (see the transferFunction() function). The default Eisenstein & Hu 1998 approximation is accurate to about 5%, and the interpolation introduces errors significantly smaller than that.

Alternatively, the user can supply the path to a file with a tabulated power spectrum using the path parameter. The file must contain two columns, $$\log_{10}(k)$$ and $$\log_{10}(P)$$ where k and P(k) are in the same units as in this function. This table is interpolated with a third-order spline. Note that the tabulated spectrum is normalized to the value if $$\sigma_8$$ set in the cosmology.

Parameters: k: array_like The wavenumber k (in comoving h/Mpc), where $$10^{-20} < k < 10^{20}$$; can be a number or a numpy array. If a user-supplied table is used, the limits of that table apply. z: float The redshift at which the power spectrum is evaluated, zero by default. If non-zero, the power spectrum is scaled with the linear growth factor, $$P(k, z) = P(k, 0) D_{+}^2(z)$$. model: str A model for the power spectrum (see the cosmology.power_spectrum module). If a tabulated power spectrum is used (see path parameter), this name must still be passed. Internally, the power spectrum is saved using this name, so the name must not overlap with any other models. path: str A path to a file containing the power spectrum as a table, where the two columns are $$\log_{10}(k)$$ (in comoving h/Mpc) and $$\log_{10}(P)$$ (in $$({\rm Mpc}/h)^3$$). derivative: bool If False, return P(k). If True, return $$d \log(P) / d \log(k)$$. Pk: array_like The matter power spectrum (or its logarithmic derivative if derivative == True); has the same dimensions as k and units of $$({\rm Mpc}/h)^3$$.

Warning

If a user-supplied power spectrum table is used, integrals over the power spectrum such as the variance and correlation function are integrated only within the limits of the given power spectrum. By default, Boltzmann codes return a relatively small range in wavenumber. Please increase this range if necessary, and check that the computed quantities are converged.

filterFunction(filt, k, R)

The Fourier transform of certain filter functions.

The main use of the filter function is in computing the variance, please see the documentation of the sigma() function for details. This function is dimensionless, the input units are k in comoving h/Mpc and R in comoving Mpc/h. Available filters are tophat,

$\tilde{W}_{\rm tophat} = \frac{3}{(kR)^3} \left[ \sin(kR) - kR \times \cos(kR) \right] \,,$

a gaussian filter,

$\tilde{W}_{\rm gaussian} = \exp \left[ \frac{-(kR)^2}{2} \right] \,,$

and a sharp-k filter,

$\tilde{W}_{\rm sharp-k} = \Theta(1 - kR) \,,$

where $$\Theta$$ is the Heaviside step function.

Parameters: filt: str Either tophat (a top-hat filter in real space), sharp-k (a top-hat filter in Fourier space), or gaussian (a Gaussian in both real and Fourier space). k: float A wavenumber k (in comoving h/Mpc). R: float A radius R (in comoving Mpc/h). filter: float The value of the filter function.
sigma(R, z, j=0, filt='tophat', inverse=False, derivative=False, ps_args={'model': 'eisenstein98', 'path': None})

The rms variance of the linear density field on a scale R, $$\sigma(R)$$.

The variance and its higher moments are defined as the integral

$\sigma^2(R,z) = \frac{1}{2 \pi^2} \int_0^{\infty} k^2 k^{2j} P(k,z) |\tilde{W}(kR)|^2 dk$

where $$\tilde{W}(kR)$$ is the Fourier transform of the filterFunction(), and $$P(k,z) = D_+^2(z)P(k,0)$$ is the matterPowerSpectrum(). See the documentation of filterFunction() for possible filters.

By default, the power spectrum is computed using the transfer function approximation of Eisenstein & Hu 1998 (see the cosmology.power_spectrum module). With this approximation, the variance is accurate to about 2% or better (see the Colossus code paper for details). Using a tabulated power spectrum can make this computation more accurate, but please note that the limits of the corresponding table are used for the integration.

Higher moments of the variance (such as $$\sigma_1$$, $$\sigma_2$$ etc) can be computed by setting j > 0 (see Bardeen et al. 1986). Furthermore, the logarithmic derivative $$d \log(\sigma) / d \log(R)$$ can be evaluated by setting derivative == True.

Parameters: R: array_like The radius of the filter in comoving Mpc/h, where $$10^{-12} < R < 10^3$$; can be a number or a numpy array. z: float Redshift; for z > 0, $$\sigma(R)$$ is multiplied by the linear growth factor. j: integer The order of the integral. j = 0 corresponds to the variance, j = 1 to the same integral with an extra $$k^2$$ term etc; see Bardeen et al. 1986 for mathematical details. filt: str Either tophat, sharp-k or gaussian (see filterFunction()). Higher moments (j > 0) can only be computed for the gaussian filter. inverse: bool If True, compute $$R(\sigma)$$ rather than $$\sigma(R)$$. derivative: bool If True, return the logarithmic derivative, $$d \log(\sigma) / d \log(R)$$, or its inverse, $$d \log(R) / d \log(\sigma)$$ if inverse == True. ps_args: dict Arguments passed to the matterPowerSpectrum() function. sigma: array_like The rms variance; has the same dimensions as R. If inverse and/or derivative are True, the inverse, derivative, or derivative of the inverse are returned. If j > 0, those refer to higher moments.

matterPowerSpectrum
The matter power spectrum at a scale k.
correlationFunction(R, z=0.0, derivative=False, ps_args={'model': 'eisenstein98', 'path': None})

The linear matter-matter correlation function at radius R.

The linear correlation function is defined as

$\xi(R,z) = \frac{1}{2 \pi^2} \int_0^\infty k^2 P(k,z) \frac{\sin(kR)}{kR} dk$

where P(k) is the matterPowerSpectrum(). By default, the power spectrum is computed using the transfer function approximation of Eisenstein & Hu 1998 (see the cosmology.power_spectrum module). With this approximation, the correlation function is accurate to ~5% over the range $$10^{-2} < R < 200$$ (see the Colossus code paper for details). Using a tabulated power spectrum can make this computation more accurate, but please note that the limits of the corresponding table are used for the integration.

Parameters: R: array_like The radius in comoving Mpc/h; can be a number or a numpy array. z: float Redshift; if non-zero, the correlation function is scaled with the linear growth factor, $$\xi(R, z) = \xi(R, 0) D_{+}^2(z)$$. derivative: bool If derivative == True, the linear derivative $$d \xi / d R$$ is returned. ps_args: dict Arguments passed to the matterPowerSpectrum() function. xi: array_like The correlation function, or its derivative; has the same dimensions as R.

matterPowerSpectrum
The matter power spectrum at a scale k.
cosmology.cosmology.setCosmology(cosmo_name, params=None)

Set a cosmology.

This function provides a convenient way to create a cosmology object without setting the parameters of the Cosmology class manually. See the Basic Usage section for examples. Whichever way the cosmology is set, the global variable is updated so that the getCurrent() function returns the set cosmology.

Parameters: cosmo_name: str The name of the cosmology. Can be the name of a pre-set cosmology, or another name in which case the params dictionary needs to be provided. params: dictionary The parameters of the constructor of the Cosmology class. Not necessary if cosmo_name is the name of a pre-set cosmology. cosmo: Cosmology The created cosmology object.
cosmology.cosmology.addCosmology(cosmo_name, params)

Add a set of cosmological parameters to the global list.

After this function is executed, the new cosmology can be set using setCosmology() from anywhere in the code.

Parameters: cosmo_name: str The name of the cosmology. params: dictionary A set of parameters for the constructor of the Cosmology class.
cosmology.cosmology.setCurrent(cosmo)

Set the current global cosmology to a cosmology object.

Unlike setCosmology(), this function does not create a new cosmology object, but allows the user to set a cosmology object to be the current cosmology. This can be useful when switching between cosmologies, since many routines use the getCurrent() routine to obtain the current cosmology.

Parameters: cosmo: Cosmology The cosmology object to be set as the global current cosmology.
cosmology.cosmology.getCurrent()

Get the current global cosmology.

This function should be used whenever access to the cosmology is needed. By using the globally set cosmology, there is no need to pass cosmology objects around the code. If no cosmology is set, this function raises an Exception that reminds the user to set a cosmology.

Returns: cosmo: Cosmology The current globally set cosmology.

## Power spectrum¶

This module implements models for the matter power spectrum, or more exactly, for the transfer function. Generally speaking, the transfer function should be evaluated using the matterPowerSpectrum() function. This module is automatically imported with the cosmology module.

### Power spectrum models¶

The following models are supported, and are listed in the models dictionary. Their ID can be passed as the model parameter to the transferFunction() function:

ID Reference Comment
eisenstein98 Eisenstein & Hu 1998 A semi-analytical fitting function
eisenstein98_zb Eisenstein & Hu 1998 The zero-baryon version, i.e., no BAO

### Module contents¶

 PowerSpectrumModel() Characteristics of power spectrum models. models Dictionary containing a list of models. transferFunction(k, h, Om0, Ob0, Tcmb0[, model]) The transfer function. modelEisenstein98(k, h, Om0, Ob0, Tcmb0) The transfer function according to Eisenstein & Hu 1998. modelEisenstein98ZeroBaryon(k, h, Om0, Ob0, …) The zero-baryon transfer function according to Eisenstein & Hu 1998.

### Module reference¶

class cosmology.power_spectrum.PowerSpectrumModel

Characteristics of power spectrum models.

This object is currently empty. The models dictionary contains one item of this class for each available model.

cosmology.power_spectrum.models = {'eisenstein98': <cosmology.power_spectrum.PowerSpectrumModel object at 0x10eb7a208>, 'eisenstein98_zb': <cosmology.power_spectrum.PowerSpectrumModel object at 0x10eb7acc0>}

Dictionary containing a list of models.

An ordered dictionary containing one PowerSpectrumModel entry for each model.

cosmology.power_spectrum.transferFunction(k, h, Om0, Ob0, Tcmb0, model='eisenstein98')

The transfer function.

The transfer function transforms the spectrum of primordial fluctuations into the linear power spectrum of the matter density fluctuations. The primordial power spectrum is usually described as a power law, leading to a power spectrum

$P(k) = T(k)^2 k^{n_s}$

where P(k) is the matter power spectrum, T(k) is the transfer function, and $$n_s$$ is the tilt of the primordial power spectrum. See the Cosmology class for further details on the cosmological parameters.

Parameters: k: array_like The wavenumber k (in comoving h/Mpc); can be a number or a numpy array. h: float The Hubble constant in units of 100 km/s/Mpc. Om0: float $$\Omega_{\rm m}$$, the matter density in units of the critical density at z = 0. Ob0: float $$\Omega_{\rm b}$$, the baryon density in units of the critical density at z = 0. Tcmb0: float The temperature of the CMB at z = 0 in Kelvin. Tk: array_like The transfer function; has the same dimensions as k.
cosmology.power_spectrum.modelEisenstein98(k, h, Om0, Ob0, Tcmb0)

The transfer function according to Eisenstein & Hu 1998.

This function computes the Eisenstein & Hu 1998 approximation to the transfer function at a scale k. The code was adapted from Matt Becker’s cosmocalc code.

This function was tested against numerical calculations based on the CAMB code (Lewis et al. 2000) and found to be accurate to 5% or better up to k of about 100 h/Mpc (see the Colossus code paper for details).

Parameters: k: array_like The wavenumber k (in comoving h/Mpc); can be a number or a numpy array. h: float The Hubble constant in units of 100 km/s/Mpc. Om0: float $$\Omega_{\rm m}$$, the matter density in units of the critical density at z = 0. Ob0: float $$\Omega_{\rm b}$$, the baryon density in units of the critical density at z = 0. Tcmb0: float The temperature of the CMB at z = 0 in Kelvin. Tk: array_like The transfer function; has the same dimensions as k.

modelEisenstein98ZeroBaryon
The zero-baryon transfer function according to Eisenstein & Hu 1998.
cosmology.power_spectrum.modelEisenstein98ZeroBaryon(k, h, Om0, Ob0, Tcmb0)

The zero-baryon transfer function according to Eisenstein & Hu 1998.

This fitting function is significantly simpler than the full modelEisenstein98() version, and still approximates numerical calculations from a Boltzmann code to better than 10%, and almost as accurate when computing the variance or correlation function (see the Colossus code paper for details).

Parameters: k: array_like The wavenumber k (in comoving h/Mpc); can be a number or a numpy array. h: float The Hubble constant in units of 100 km/s/Mpc. Om0: float $$\Omega_{\rm m}$$, the matter density in units of the critical density at z = 0. Ob0: float $$\Omega_{\rm b}$$, the baryon density in units of the critical density at z = 0. Tcmb0: float The temperature of the CMB at z = 0 in Kelvin. Tk: array_like The transfer function; has the same dimensions as k.

modelEisenstein98