phytorch.cosmology

⚠️ Warning: under construction!

This notebook demonstrates the various functionalities of phytorch.cosmology, which are mainly related to cosmographic distance calculations.

We first import the various external modules we’ll need. Setting the default datatype to double will prevent some numerical issues which otherwise might look like bugs :)

[1]:
import torch
import numpy as np

from matplotlib import pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
%config InlineBackend.rc = {'figure.dpi': 96}


torch.set_default_dtype(torch.double)

Instantiation and basics

To initialise a cosmology class, one needs to first select the concrete cosmological model (ΛCDM, wCDM, etc.) and a driver (analytic, numerical integration, etc.) which performs the cosmographic calculaitons. If cosmography is not your thing, there is an “abstract” driver under phytorch.cosmology.drivers.abstract, which we’ll use to demonstrate basic operations.

Cosmologies are initialised by giving values for the required parameters as keyword-only arguments:

[2]:
from phytorch.cosmology.drivers import abstract

c = abstract.LambdaCDMR(Om0=0.31, Ode0=0.68, Or0=1e-4)

We can now visualise how the various relative densities evolve:

[3]:
z = torch.logspace(-2, 5, 71)

Om = c.Om(z)
Ode = c.Ode(z)
Or = c.Or(z)
Ok = c.Ok(z)

The returned values are tensors, which can be plotted (behind the scenes):

[5]:
fig
[5]:
../_images/examples_cosmology_8_0.svg

The parameters can be tensors as well and can be safely modified after instantiation:

[6]:
c.Om0 = torch.tensor([0, 0.3, 1])

All calculations broadcast as you’d expect. For example, we can calculate the dimensionless Hubble parameter \(E(z, \Omega_{\mathrm{m} 0}, \Omega_{\lambda 0}, \Omega_{\mathrm{r} 0})\) for the three different matter densities over a range of redshifts, one can do

[7]:
E = c.efunc(z.unsqueeze(-1))
E.shape
[7]:
torch.Size([71, 3])
[9]:
fig
[9]:
../_images/examples_cosmology_14_0.svg

Unitful quantities

One can also calculate unitful quantities. In cosmology, units are set by the Hubble parameter \(H_0\), which by default is taken to be \(100 \, \mathrm{km}/\mathrm{s}/\mathrm{Mpc}\).

Distance calculations

[10]:
from phytorch.cosmology.drivers.analytic import LambdaCDM

c = LambdaCDM(Om0=0.31, Ode0=0.68)

z = torch.logspace(-3, 5, 81)

dc = c.comoving_distance_dimless(z)
dl = c.luminosity_distance_dimless(z)
da = c.angular_diameter_distance_dimless(z)

Plot behind the scenes:

[12]:
fig
[12]:
../_images/examples_cosmology_20_0.svg

More cosmological models

Gradients

Module