cosmology.md
334 lines · skills/astropy/references/cosmology.md at 5f2bc03
Cosmological Calculations (astropy.cosmology)
The astropy.cosmology subpackage provides tools for cosmological calculations based on various cosmological models. Requires SciPy for these workflows. Pass redshifts positionally in 8.0.1. The shorter formula fragments below assume cosmo, z, and any measured magnitudes are defined for the science case.
Import paths: Always import classes and functions directly fromRaw lines 1 to 8astropy.cosmology(as shown in all examples below). The old submodule import paths (astropy.cosmology.flrw,.core,.funcs,.connect,.parameter) were deprecated in v7.1 and removed in Astropy 8.0.
Using Built-in Cosmologies
Preloaded cosmologies based on WMAP and Planck observations:
from astropy.cosmology import Planck18, Planck15, Planck13
from astropy.cosmology import WMAP9, WMAP7, WMAP5
from astropy import units as u
# Use Planck 2018 cosmology
cosmo = Planck18
# Calculate distance to z=4
d = cosmo.luminosity_distance(4)
print(f"Luminosity distance at z=4: {d}")
# Age of universe at z=0
age = cosmo.age(0)
print(f"Current age of universe: {age.to(u.Gyr)}")Raw lines 9 to 29Creating Custom Cosmologies
Raw lines 30 to 31FlatLambdaCDM (Most Common)
Flat universe with cosmological constant:
from astropy.cosmology import FlatLambdaCDM
# Define cosmology
cosmo = FlatLambdaCDM(
H0=70 * u.km / u.s / u.Mpc, # Hubble constant at z=0
Om0=0.3, # Matter density parameter at z=0
Tcmb0=2.725 * u.K # CMB temperature (optional)
)Raw lines 32 to 46LambdaCDM (Non-Flat)
Non-flat universe with cosmological constant:
from astropy.cosmology import LambdaCDM
cosmo = LambdaCDM(
H0=70 * u.km / u.s / u.Mpc,
Om0=0.3,
Ode0=0.7 # Dark energy density parameter
)Raw lines 47 to 60wCDM and w0wzCDM
Dark energy with equation of state parameter:
from astropy.cosmology import FlatwCDM, w0wzCDM
# Constant w
cosmo_w = FlatwCDM(H0=70 * u.km/u.s/u.Mpc, Om0=0.3, w0=-0.9)
# Evolving w(z) = w0 + wz * z
cosmo_wz = w0wzCDM(H0=70 * u.km/u.s/u.Mpc, Om0=0.3, Ode0=0.7,
w0=-1.0, wz=0.1)w0wzCDM extrapolates w(z) linearly; upstream does not recommend it for z > 1. Its age integral samples to infinity and can fail numerically for parameters like wz=0.1; a finite low-redshift distance does not validate that extrapolation. Choose an appropriate physical model before interpreting ages.
Distance Calculations
Raw lines 81 to 82Luminosity Distance
Distance for calculating luminosity from observed flux:
d_L = cosmo.luminosity_distance(z)
# Calculate absolute magnitude from apparent magnitude
M = m - 5*np.log10(d_L.to(u.pc).value) + 5Raw lines 91 to 101Angular Diameter Distance
Distance for calculating physical size from angular size:
d_A = cosmo.angular_diameter_distance(z)
# Calculate physical size from angular size
theta = 10 * u.arcsec # Angular size
physical_size = d_A * theta.to(u.radian).valueRaw lines 102 to 113Comoving Transverse Distance
Transverse comoving distance (equals comoving distance in flat universe):
d_M = cosmo.comoving_transverse_distance(z)Raw lines 114 to 121Distance Modulus
dm = cosmo.distmod(z)
# Relates apparent and absolute magnitudes: m - M = dmRaw lines 122 to 128Scale Calculations
Raw lines 129 to 130kpc per Arcminute
Physical scale at a given redshift:
scale = cosmo.kpc_proper_per_arcmin(z)
# Returns proper kpc / arcmin; keep the angular unit when applying the scale.Raw lines 131 to 139Comoving Volume
Volume element for survey volume calculations:
vol = cosmo.comoving_volume(z) # Total volume to redshift z
vol_element = cosmo.differential_comoving_volume(z) # dV / (dz dOmega), Mpc3 / srRaw lines 140 to 148Time Calculations
Raw lines 149 to 150Age of Universe
Age at a given redshift:
age = cosmo.age(z)
age_now = cosmo.age(0) # Current age
age_at_z1 = cosmo.age(1) # Age at z=1Raw lines 151 to 160Lookback Time
Time since photons were emitted:
t_lookback = cosmo.lookback_time(z)
# Time between z and z=0Raw lines 161 to 169Hubble Parameter
Hubble parameter as function of redshift:
H_z = cosmo.H(z) # H(z) in km/s/Mpc
E_z = cosmo.efunc(z) # E(z) = H(z)/H0Raw lines 170 to 178Density Parameters
Evolution of density parameters with redshift:
Om_z = cosmo.Om(z) # Matter density at z
Ode_z = cosmo.Ode(z) # Dark energy density at z
Ok_z = cosmo.Ok(z) # Curvature density at z
Ogamma_z = cosmo.Ogamma(z) # Photon density at z
Onu_z = cosmo.Onu(z) # Neutrino density at zRaw lines 179 to 190Critical and Characteristic Densities
rho_c = cosmo.critical_density(z) # Critical density at z
rho_m = cosmo.critical_density(z) * cosmo.Om(z) # Matter densityRaw lines 191 to 197Inverse Calculations
Find redshift corresponding to a specific value:
from astropy.cosmology import z_at_value
# Find z at specific lookback time
z = z_at_value(cosmo.lookback_time, 10*u.Gyr)
# Find z at specific luminosity distance
z = z_at_value(cosmo.luminosity_distance, 1000*u.Mpc)
# Find z at specific age
z = z_at_value(cosmo.age, 1*u.Gyr)Angular-diameter distance is not monotonic. Select a scientifically justified zmin/zmax branch and check the forward residual; a two-point Brent bracket does not constrain the solution to that interval.
z_low = z_at_value(Planck18.angular_diameter_distance, 1500*u.Mpc, zmax=1.5)
z_high = z_at_value(Planck18.angular_diameter_distance, 1500*u.Mpc, zmin=2.5, zmax=10)Raw lines 198 to 223Array Operations
All methods accept array inputs:
import numpy as np
z_array = np.linspace(0, 5, 100)
d_L_array = cosmo.luminosity_distance(z_array)
H_array = cosmo.H(z_array)
age_array = cosmo.age(z_array)Raw lines 224 to 236Neutrino Effects
Include massive neutrinos:
from astropy.cosmology import FlatLambdaCDM
# With massive neutrinos
cosmo = FlatLambdaCDM(
H0=70 * u.km/u.s/u.Mpc,
Om0=0.3,
Tcmb0=2.725 * u.K,
Neff=3.04, # Effective number of neutrino species
m_nu=[0., 0., 0.06] * u.eV # Neutrino masses
)Massive neutrinos can make evaluations slower. Choose their masses and Neff from the intended physical model; adding them does not establish model accuracy. The default Tcmb0=0 disables photons and neutrinos, even when m_nu is given.
Cloning and Modifying Cosmologies
Cosmology objects are immutable. Clone an already named model. Astropy 8.0.1 raises TypeError when cloning an unnamed model with changes, even if a new name is passed; give custom models a name when constructing them.
# Start from a named built-in model
cosmo = Planck18
# Clone with different H0
cosmo_new = cosmo.clone(name="Modified H0", H0=72 * u.km/u.s/u.Mpc)
# Clone with modified name
cosmo_named = cosmo.clone(name="My Custom Cosmology")Raw lines 258 to 273Common Use Cases
Raw lines 274 to 275Calculating Absolute Magnitude
# From apparent magnitude and redshift
z = 1.5
m_app = 24.5 # Apparent magnitude
d_L = cosmo.luminosity_distance(z)
M_abs = m_app - cosmo.distmod(z).value # Distance-only; no extinction or K correctionRaw lines 276 to 285Survey Volume Calculations
# Full-sky comoving volume between two redshifts; multiply by survey solid angle / (4*pi*sr)
z_min, z_max = 0.5, 1.5
volume = cosmo.comoving_volume(z_max) - cosmo.comoving_volume(z_min)
# Convert to Gpc^3
volume_gpc3 = volume.to(u.Gpc**3)Raw lines 286 to 296Physical Size from Angular Size
theta = 1 * u.arcsec # Angular size
z = 2.0
d_A = cosmo.angular_diameter_distance(z)
size_kpc = (d_A * theta.to_value(u.rad)).to(u.kpc) # Small-angle approximationRaw lines 297 to 305Time Since Big Bang
# Age at specific redshift
z_formation = 6
age_at_formation = cosmo.age(z_formation)
time_since_formation = cosmo.age(0) - age_at_formationRaw lines 306 to 314Comparison of Cosmologies
# Compare different models
from astropy.cosmology import Planck18, WMAP9
z = 1.0
print(f"Planck18 d_L: {Planck18.luminosity_distance(z)}")
print(f"WMAP9 d_L: {WMAP9.luminosity_distance(z)}")Raw lines 315 to 325Performance Considerations
- Calculations are fast for most purposes
- Massive neutrinos reduce speed significantly
- Array operations are vectorized and efficient
- Numerical evaluation is not evidence of physical validity at a chosen redshift; record model parameters, radiation/neutrino assumptions and domain.
- Check distance duality
d_L = (1+z)**2 * d_Afor these standard FLRW models, and record the named cosmology or all custom parameters in output provenance.