## Introduction¶

The astropy.cosmology subpackage contains classes for representingcosmologies, and utility functions for calculating commonly usedquantities that depend on a cosmological model. This includesdistances, ages and lookback times corresponding to a measuredredshift or the transverse separation corresponding to a measuredangular separation.

## Getting Started¶

There are many functions available to calculate cosmological quantities.They generally take a redshift as input. For example, the two casesbelow give you the value of the hubble constant at z=0 (i.e., `H0`), andthe number of transverse proper kpc corresponding to an arcminute at z=3:

>>> from astropy import cosmology>>> cosmology.H(0)70.4>>> cosmology.kpc_proper_per_arcmin(3)472.8071851564037

All the functions available are listed in the Reference/APIsection. These will use the “current” cosmology to calculate thevalues (see The Current Cosmology section below for moredetails). If you haven’t set this explicitly, they will use the 7-yearWMAP cosmological parameters and print a warning message.

There are also several standard cosmologies already defined. These areobjects with methods and attributes that calculate cosmologicalvalues. For example, the comoving distance in Mpc to redshift 4 usingthe 5-year WMAP parameters:

>>> from astropy.cosmology import WMAP5>>> WMAP5.comoving_distance(4)7329.328120760829

A full list of the pre-defined cosmologies is given by`cosmology.parameters.available`.

An important point is that the cosmological parameters of eachinstance are immutable – that is, if you want to change, say,`Om`, you need to make a new instance of the class.

## Using `cosmology`¶

Most of the functionality is enabled by theFLRW object. This represents ahom*ogenous and isotropic cosmology (a cosmology characterized by theFriedmann-Lemaitre-Robertson-Walker metric, named after the people whosolved Einstein’s field equation for this special case). However,you can’t work with this class directly, as you must specify adark energy model by using one of its subclasses instead,such as FlatLambdaCDM.

You can create a new FlatLambdaCDM object witharguments giving the hubble parameter and omega matter (both at z=0):

>>> from astropy.cosmology import FlatLambdaCDM>>> cosmo = FlatLambdaCDM(H0=70, Om0=0.3)>>> cosmoLambdaCDM(H0=70, Om0=0.3, Ode0=0.7)

A number of additional dark energy models are provided (described below).Note that photons and neutrinos are included in these models, soOm0 + Ode0 is not quite one.

The pre-defined cosmologies described in the Getting Startedsection are instances of FlatLambdaCDM, and havethe same methods. So we can find the luminosity distance in Mpc toredshift 4 by:

>>> cosmo.luminosity_distance(4)35842.35374316948

or the age of the universe at z = 0 in Gyr:

>>> cosmo.age(0)13.461701807287566

They also accept arrays of redshifts:

>>> cosmo.age([0.5, 1, 1.5])array([ 8.42128059, 5.74698062, 4.1964541 ])

See the FLRW andFlatLambdaCDM object docstring for all themethods and attributes available. In addition to flat Universes,non-flat varieties are supported such asLambdaCDM. There are also a variety ofstandard cosmologies with the parameters already defined:

>>> from astropy.cosmology import WMAP7 # WMAP 7-year cosmology>>> WMAP7.critical_density(0) # critical density at z = 0 in g/cm^39.31000313202047e-30

>>> from astropy.cosmology import WMAP5 # WMAP 5-year>>> WMAP5.H(3) # Hubble parameter at z = 3 in km/s/Mpc301.71804314602889

You can see how the density parameters evolve with redshift as well

>>> from astropy.cosmology import WMAP7 # WMAP 7-year cosmology>>> WMAP7.Om([0,1.0,2.0]), WMAP7.Ode([0.,1.0,2.0])(array([ 0.272 , 0.74898525, 0.9090524 ]), array([ 0.72791572, 0.25055062, 0.09010261]))

Note that these don’t quite add up to one even though WMAP7 assumes aflat Universe because photons and neutrinos are included.

In addition to the LambdaCDM object, thereare convenience functions that calculate some of these quantitieswithout needing to explicitly give a cosmology - but there are moremethods available if you work directly with the cosmology object.

>>> from astropy import cosmology>>> cosmology.kpc_proper_per_arcmin(3)472.8071851564037>>> cosmology.arcsec_per_kpc_proper(3)0.12690162477152736

These functions will perform calculations using the “current”cosmology. This is a specific cosmology that is currently active in`astropy` and it’s described further in the following section. Theycan also be explicitly given a cosmology using the `cosmo` keywordargument. A full list of convenience functions is included below, inthe Reference/API section.

## The Current Cosmology¶

Sometimes it’s useful for Astropy functions to assume a defaultcosmology so that the desired cosmology doesn’t have to be specifiedevery time the function is called – the convenience functionsdescribed in the previous section are one example. For these casesit’s possible to specify a “current” cosmology.

You can set the current cosmology to a pre-defined value by using the“default_cosmology” option in the `[cosmology.core]` section of theconfiguration file (see Configuration system (astropy.config)). Alternatively, you canuse the set_current function to set acosmology for the current Python session.

If you haven’t set a current cosmology using one of the methodsdescribed above, then the cosmology module will use the 7-year WMAPparameters and print a warning message letting you know this. Forexample, if you call a convenience function without setting thecurrent cosmology or using the `cosmo=` keyword you see the followingmessage:

>>> from astropy import cosmology>>> cosmology.lookback_time(1) # lookback time in Gyr at z=1WARNING: No default cosmology has been specified, using 7-year WMAP.[astropy.cosmology.core]7.787767002228743

Note

In general it’s better to use an explicit cosmology (for example`WMAP7.H(0)` instead of `cosmology.H(0)`). The motivation forthis is that when you go back to use the code at a later date orshare your scripts with someone else, the default cosmology mayhave changed. Use of the convenience functions should generally bereserved for interactive work or cases where the flexibility ofquickly changing between different cosmologies is for some reasonuseful. Alternatively, putting (for example)`cosmology.set_current(WMAP7)` at the top of your code willensure that the right cosmology is always used.

### Using `cosmology` inside Astropy¶

If you are writing code for the `astropy` core or an affiliated package,it is strongly recommended that you use the the current cosmologythrough the get_current function. It is alsorecommended that you provide an override option something like thefollowing:

def myfunc(..., cosmo=None): from astropy.cosmology import get_current if cosmo is None: cosmo = get_current() ... your code here ...

This ensures that all code consistently uses the current cosmologyunless explicitly overridden.

## Specifying a dark energy model¶

In addition to the standard FlatLambdaCDM modeldescribed above, a number of additional dark energy models areprovided. FlatLambdaCDMand FlatLambdaCDM assume that darkenergy is a cosmological constant, and should be the most commonlyused case. wCDM assumes a constant darkenergy equation of state parameterized by . Two forms of avariable dark energy equation of state are provided: the simple firstorder linear expansion byw0wzCDM, as well as the common CPL form byw0waCDM: and its generalization to include a pivotredshift by wpwaCDM: .

Users can specify their own equation of state by sub-classingFLRW. See the provided subclasses forexamples.

## Relativistic Species¶

The cosmology classes include the contribution to the energy densityfrom both photons and massless neutrinos. The two parameterscontrolling the proporties of these species are Tcmb0 (the temperatureof the CMB at z=0) and Neff, the effective number of neutrino species.Both have standard default values (2.725 and 3.04, respectively; thereason that Neff is not 3 has to do with a small bump in the neutrinoenergy spectrum due to electron-positron annihilation).

>>> from astropy.cosmology import WMAP7 # WMAP 7-year cosmology>>> z = [0,1.0,2.0]>>> WMAP7.Ogamma(z), WMAP7.Onu(z)(array([ 4.98569503e-05, 2.74574414e-04]), array([ 3.44204408e-05, 1.89561782e-04]), array([ 8.42773911e-05, 4.64136197e-04]))

If you want to exclude photons and neutrinos from your calculations,simply set the CMB Temperature to 0:

>>> from astropy.cosmology import FlatLambdaCDM>>> cos = FlatLambdaCDM(70.4, 0.272, Tcmb0 = 0.0)>>> cos.Ogamma0, cos.Onu0(0.0, 0.0)

Neutrinos can be removed (while leaving photons) by setting Neff=0:

>>> from astropy.cosmology import FlatLambdaCDM>>> cos = FlatLambdaCDM(70.4, 0.272, Neff=0)>>> cos.Ogamma([0,1,2]),cos.Onu([0,1,2])(array([ 4.98569503e-05, 2.74623219e-04, 5.00051845e-04]), array([ 0., 0., 0.]))

While these examples used FlatLambdaCDM,the above examples also apply for all of the other cosmology classes.

## See Also¶

- Hogg, “Distance measures in cosmology”,http://arxiv.org/abs/astroph/9905116
- Linder, “Exploring the Expansion History of the Universe”, http://arxiv.org/abs/astro-ph/0208512
- NASA’s Legacy Archive for Microwave Background Data Analysis,http://lambda.gsfc.nasa.gov/

## Range of validity and reliability¶

The code in this sub-package is tested against several widely-usedonline cosmology calculators, and has been used to performcalculations in refereed papers. You can check the range of redshiftsover which the code is regularly tested in the module`astropy.cosmology.tests.test_cosmology`. Note that the energy densitydue to radiation is assumed to be negligible, which is valid forredshifts less than about 10. If you find any bugs, please let us knowby opening an issue at the github repository!

## Reference/API¶

### astropy.cosmology Module¶

astropy.cosmology contains classes and functions for cosmologicaldistance measures and other cosmology-related calculations.

See the Astropy documentation for moredetailed usage examples and references.

#### Functions¶

H(z[,cosmo]) | Hubble parameter (km/s/Mpc) at redshift z. |

angular_diameter_distance(z[,cosmo]) | Angular diameter distance in Mpc at a given redshift. |

arcsec_per_kpc_comoving(z[,cosmo]) | Angular separation in arcsec corresponding to a comoving kpc at redshift z. |

arcsec_per_kpc_proper(z[,cosmo]) | Angular separation in arcsec corresponding to a proper kpc at redshift z. |

comoving_distance(z[,cosmo]) | Comoving distance in Mpc at redshift z. |

critical_density(z[,cosmo]) | Critical density in grams per cubic cm at redshift z. |

distmod(z[,cosmo]) | Distance modulus at redshift z. |

get_current() | Get the current cosmology. |

kpc_comoving_per_arcmin(z[,cosmo]) | Separation in transverse comoving kpc corresponding to an arcminute at redshift z. |

kpc_proper_per_arcmin(z[,cosmo]) | Separation in transverse proper kpc corresponding to an arcminute at redshift z. |

lookback_time(z[,cosmo]) | Lookback time in Gyr to redshift z. |

luminosity_distance(z[,cosmo]) | Luminosity distance in Mpc at redshift z. |

scale_factor(z[,cosmo]) | Scale factor at redshift z. |

set_current(cosmo) | Set the current cosmology. |

#### Classes¶

FLRW(H0,Om0,Ode0[,Tcmb0,Neff,name]) | A class describing an isotropic and hom*ogeneous (Friedmann-Lemaitre-Robertson-Walker) cosmology. |

FlatLambdaCDM(H0,Om0[,Tcmb0,Neff,name]) | FLRW cosmology with a cosmological constant and no curvature. |

Flatw0waCDM(H0,Om0[,w0,wa,Tcmb0,Neff,name]) | FLRW cosmology with a CPL dark energy equation of state and no curvature. |

FlatwCDM(H0,Om0[,w0,Tcmb0,Neff,name]) | FLRW cosmology with a constant dark energy equation of state and no spatial curvature. |

LambdaCDM(H0,Om0,Ode0[,Tcmb0,Neff,name]) | FLRW cosmology with a cosmological constant and curvature. |

w0waCDM(H0,Om0,Ode0[,w0,wa,Tcmb0,...]) | FLRW cosmology with a CPL dark energy equation of state and curvature. |

w0wzCDM(H0,Om0,Ode0[,w0,wz,Tcmb0,...]) | FLRW cosmology with a variable dark energy equation of state and curvature. |

wCDM(H0,Om0,Ode0[,w0,Tcmb0,Neff,name]) | FLRW cosmology with a constant dark energy equation of state and curvature. |

wpwaCDM(H0,Om0,Ode0[,wp,wa,zp,Tcmb0,...]) | FLRW cosmology with a CPL dark energy equation of state, a pivot redshift, and curvature. |