or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

configuration.mdconstants.mdconvolution.mdcoordinates.mdcosmology.mdfits-io.mdindex.mdmodeling.mdnddata.mdsamp.mdstatistics.mdtables.mdtime.mdtimeseries.mduncertainty.mdunits-quantities.mdutils.mdvisualization.mdwcs.md

cosmology.mddocs/

0

# Cosmology

1

2

Cosmological calculations and standard cosmological models for distance measurements, age calculations, and cosmological parameters.

3

4

## Capabilities

5

6

### Cosmological Models

7

8

Base classes and specific cosmological models with dark matter, dark energy, and curvature parameters.

9

10

```python { .api }

11

class FLRW:

12

"""

13

Friedmann-Lemaître-Robertson-Walker cosmology base class.

14

15

Parameters:

16

- H0: Hubble constant at z=0

17

- Om0: Omega matter at z=0

18

- Ode0: Omega dark energy at z=0

19

- Tcmb0: CMB temperature at z=0

20

- Neff: effective number of neutrino species

21

- m_nu: neutrino mass

22

- Ob0: Omega baryon at z=0

23

- name: cosmology name

24

"""

25

def __init__(self, H0, Om0, Ode0=None, Tcmb0=2.725*u.K, Neff=3.04,

26

m_nu=0.0*u.eV, Ob0=None, name=None, meta=None): ...

27

28

def age(self, z):

29

"""

30

Age of universe at redshift z.

31

32

Parameters:

33

- z: redshift

34

35

Returns:

36

Quantity: age in time units

37

"""

38

39

def lookback_time(self, z):

40

"""Lookback time to redshift z."""

41

42

def comoving_distance(self, z):

43

"""Comoving distance to redshift z."""

44

45

def angular_diameter_distance(self, z):

46

"""Angular diameter distance to redshift z."""

47

48

def luminosity_distance(self, z):

49

"""Luminosity distance to redshift z."""

50

51

def comoving_volume(self, z):

52

"""Comoving volume out to redshift z."""

53

54

def differential_comoving_volume(self, z):

55

"""Differential comoving volume at redshift z."""

56

57

def angular_diameter_distance_z1z2(self, z1, z2):

58

"""Angular diameter distance between two redshifts."""

59

60

def absorption_distance(self, z):

61

"""Absorption distance for IGM studies."""

62

63

def distmod(self, z):

64

"""Distance modulus to redshift z."""

65

66

def kpc_comoving_per_arcmin(self, z):

67

"""Comoving kpc per arcminute at redshift z."""

68

69

def kpc_proper_per_arcmin(self, z):

70

"""Proper kpc per arcminute at redshift z."""

71

72

def arcsec_per_kpc_comoving(self, z):

73

"""Arcseconds per comoving kpc at redshift z."""

74

75

def arcsec_per_kpc_proper(self, z):

76

"""Arcseconds per proper kpc at redshift z."""

77

78

def critical_density(self, z):

79

"""Critical density at redshift z."""

80

81

def H(self, z):

82

"""Hubble parameter at redshift z."""

83

84

def scale_factor(self, z):

85

"""Scale factor at redshift z."""

86

87

def Tcmb(self, z):

88

"""CMB temperature at redshift z."""

89

90

class LambdaCDM(FLRW):

91

"""

92

Lambda-CDM cosmology with curvature.

93

94

Parameters:

95

- H0: Hubble constant

96

- Om0: matter density parameter

97

- Ode0: dark energy density parameter (default: 1 - Om0 - Ok0)

98

- Ok0: curvature density parameter (default: 0)

99

"""

100

def __init__(self, H0, Om0, Ode0=None, **kwargs): ...

101

102

class FlatLambdaCDM(FLRW):

103

"""Flat Lambda-CDM cosmology (Ok0 = 0)."""

104

def __init__(self, H0, Om0, **kwargs): ...

105

106

class wCDM(FLRW):

107

"""CDM cosmology with constant dark energy equation of state w."""

108

def __init__(self, H0, Om0, Ode0, w0, **kwargs): ...

109

110

class FlatwCDM(FLRW):

111

"""Flat wCDM cosmology."""

112

def __init__(self, H0, Om0, w0, **kwargs): ...

113

114

class w0waCDM(FLRW):

115

"""CDM with time-varying dark energy: w(z) = w0 + wa * z/(1+z)."""

116

def __init__(self, H0, Om0, Ode0, w0, wa, **kwargs): ...

117

118

class Flatw0waCDM(FLRW):

119

"""Flat w0waCDM cosmology."""

120

def __init__(self, H0, Om0, w0, wa, **kwargs): ...

121

```

122

123

### Standard Cosmological Realizations

124

125

Pre-defined cosmological models from major observational campaigns.

126

127

```python { .api }

128

# Planck mission results

129

Planck18: FlatLambdaCDM # Planck 2018 results

130

Planck15: FlatLambdaCDM # Planck 2015 results

131

Planck13: FlatLambdaCDM # Planck 2013 results

132

133

# WMAP mission results

134

WMAP9: FlatLambdaCDM # WMAP 9-year results

135

WMAP7: FlatLambdaCDM # WMAP 7-year results

136

WMAP5: FlatLambdaCDM # WMAP 5-year results

137

138

# Example parameter values (Planck 2018)

139

# H0 = 67.36 km/s/Mpc

140

# Om0 = 0.3153

141

# Ode0 = 0.6847

142

# Tcmb0 = 2.7255 K

143

# Ob0 = 0.04930

144

```

145

146

### Cosmological Utility Functions

147

148

Helper functions for cosmological calculations and model comparisons.

149

150

```python { .api }

151

def z_at_value(func, fval, zmin=1e-8, zmax=1000, ztol=1e-8, maxfun=500):

152

"""

153

Find redshift at which cosmological function equals specified value.

154

155

Parameters:

156

- func: cosmological function (e.g., cosmo.age)

157

- fval: target function value

158

- zmin, zmax: redshift search range

159

- ztol: redshift tolerance

160

- maxfun: maximum function evaluations

161

162

Returns:

163

float: redshift at which func(z) = fval

164

"""

165

166

def cosmology_equal(cosmo1, cosmo2, format=False):

167

"""

168

Test equality of two cosmologies.

169

170

Parameters:

171

- cosmo1, cosmo2: cosmology instances

172

- format: return detailed comparison

173

174

Returns:

175

bool or dict: equality result

176

"""

177

178

# Default cosmology management

179

from astropy.cosmology import default_cosmology

180

181

@default_cosmology.getter

182

def get_default_cosmology():

183

"""Get current default cosmology."""

184

185

@default_cosmology.setter

186

def set_default_cosmology(cosmo):

187

"""Set default cosmology."""

188

189

# Available cosmologies

190

from astropy.cosmology import available

191

available: list # List of available standard cosmologies

192

```

193

194

### Parameter Classes

195

196

Classes for handling cosmological parameters with units and metadata.

197

198

```python { .api }

199

class Parameter:

200

"""

201

Cosmological parameter with units and metadata.

202

203

Parameters:

204

- value: parameter value

205

- unit: astropy unit

206

- equivalencies: unit equivalencies

207

- fvalidate: validation function

208

- doc: parameter documentation

209

"""

210

def __init__(self, doc='', unit=None, equivalencies=[], fvalidate='default'): ...

211

212

@property

213

def value(self):

214

"""Parameter value."""

215

216

@property

217

def unit(self):

218

"""Parameter unit."""

219

```

220

221

## Usage Examples

222

223

### Basic Cosmological Calculations

224

225

```python

226

from astropy.cosmology import Planck18

227

import astropy.units as u

228

import numpy as np

229

230

# Use standard Planck 2018 cosmology

231

cosmo = Planck18

232

233

# Basic distance calculations

234

z = 1.0 # redshift

235

d_L = cosmo.luminosity_distance(z)

236

d_A = cosmo.angular_diameter_distance(z)

237

d_C = cosmo.comoving_distance(z)

238

239

print(f"At z = {z}:")

240

print(f" Luminosity distance: {d_L}")

241

print(f" Angular diameter distance: {d_A}")

242

print(f" Comoving distance: {d_C}")

243

244

# Time calculations

245

age_z1 = cosmo.age(z)

246

age_now = cosmo.age(0)

247

lookback = cosmo.lookback_time(z)

248

249

print(f"Age at z=1: {age_z1.to(u.Gyr)}")

250

print(f"Age now: {age_now.to(u.Gyr)}")

251

print(f"Lookback time: {lookback.to(u.Gyr)}")

252

```

253

254

### Custom Cosmological Models

255

256

```python

257

from astropy.cosmology import FlatLambdaCDM, wCDM

258

import astropy.units as u

259

260

# Create custom flat Lambda-CDM model

261

my_cosmo = FlatLambdaCDM(H0=70*u.km/u.s/u.Mpc, Om0=0.3,

262

Tcmb0=2.725*u.K, name="My Cosmology")

263

264

# Create model with dark energy equation of state

265

phantom_cosmo = wCDM(H0=70*u.km/u.s/u.Mpc, Om0=0.3,

266

Ode0=0.7, w0=-1.2, name="Phantom DE")

267

268

# Compare distances

269

z_array = np.logspace(-2, 1, 50) # z from 0.01 to 10

270

d_L_std = Planck18.luminosity_distance(z_array)

271

d_L_custom = my_cosmo.luminosity_distance(z_array)

272

d_L_phantom = phantom_cosmo.luminosity_distance(z_array)

273

274

print(f"Distance difference at z=1:")

275

print(f"Custom vs Planck18: {(my_cosmo.luminosity_distance(1) - Planck18.luminosity_distance(1)).to(u.Mpc)}")

276

```

277

278

### Redshift-Distance Relations

279

280

```python

281

from astropy.cosmology import z_at_value, Planck18

282

import astropy.units as u

283

284

# Find redshift for specific distance

285

target_distance = 1000 * u.Mpc

286

z_target = z_at_value(Planck18.comoving_distance, target_distance)

287

print(f"Redshift for comoving distance {target_distance}: z = {z_target:.3f}")

288

289

# Find redshift for specific age

290

target_age = 2 * u.Gyr # 2 billion years after Big Bang

291

z_young = z_at_value(Planck18.age, target_age)

292

print(f"Redshift when universe was {target_age}: z = {z_young:.1f}")

293

294

# Find redshift for specific lookback time

295

lookback_target = 10.5 * u.Gyr # 10.5 Gyr ago

296

z_lookback = z_at_value(Planck18.lookback_time, lookback_target)

297

print(f"Redshift for lookback time {lookback_target}: z = {z_lookback:.2f}")

298

```

299

300

### Angular Scale Calculations

301

302

```python

303

# Calculate angular scales for observations

304

z_galaxy = 2.0

305

306

# Physical scale per angular scale

307

kpc_per_arcmin = cosmo.kpc_proper_per_arcmin(z_galaxy)

308

kpc_per_arcsec = kpc_per_arcmin / 60

309

310

print(f"At z = {z_galaxy}:")

311

print(f" {kpc_per_arcsec:.1f} per arcsecond")

312

print(f" {kpc_per_arcmin:.1f} per arcminute")

313

314

# Convert observed angular size to physical size

315

observed_size = 5 * u.arcsec # 5 arcsecond diameter galaxy

316

physical_size = observed_size.to(u.kpc,

317

equivalencies=cosmo.angular_diameter_distance_z1z2(0, z_galaxy))

318

print(f"5 arcsec corresponds to {physical_size:.1f} at z={z_galaxy}")

319

```

320

321

### Cosmological Volume Calculations

322

323

```python

324

# Calculate comoving volumes

325

z_survey = 3.0

326

327

# Total comoving volume out to redshift

328

total_volume = cosmo.comoving_volume(z_survey)

329

print(f"Comoving volume out to z={z_survey}: {total_volume.to(u.Gpc**3)}")

330

331

# Differential volume element

332

dV_dz = cosmo.differential_comoving_volume(z_survey)

333

print(f"Differential volume at z={z_survey}: {dV_dz.to(u.Gpc**3)}")

334

335

# Volume of a survey

336

solid_angle = 10 * u.deg**2 # 10 square degree survey

337

survey_volume = (solid_angle / (4*np.pi*u.sr)) * total_volume

338

print(f"Survey volume ({solid_angle}): {survey_volume.to(u.Gpc**3)}")

339

```

340

341

### Comparing Cosmological Models

342

343

```python

344

from astropy.cosmology import Planck18, WMAP9, cosmology_equal

345

346

# Compare parameter values

347

print(f"Planck18 H0: {Planck18.H0}")

348

print(f"WMAP9 H0: {WMAP9.H0}")

349

print(f"Planck18 Om0: {Planck18.Om0}")

350

print(f"WMAP9 Om0: {WMAP9.Om0}")

351

352

# Test equality

353

are_equal = cosmology_equal(Planck18, WMAP9)

354

print(f"Planck18 == WMAP9: {are_equal}")

355

356

# Calculate differences in derived quantities

357

z_test = 1.0

358

diff_age = Planck18.age(z_test) - WMAP9.age(z_test)

359

diff_dL = Planck18.luminosity_distance(z_test) - WMAP9.luminosity_distance(z_test)

360

361

print(f"Age difference at z=1: {diff_age.to(u.Myr)}")

362

print(f"Luminosity distance difference: {diff_dL.to(u.Mpc)}")

363

```