Building models#
discoeb.models defines the following built-in cosmological models:
Model |
Components added to |
|---|---|
CDM, baryons, photons, massless neutrinos, a cosmological constant, Thomson scattering |
|
|
|
both |
Constructing a new model#
New cosmological models can be built by defining a new class that inherits from existing models and/or base components that define new species or physics.
from discoeb import LCDM
from discoeb.components.species import MassiveNeutrinos
class LCDM_MNU(LCDM, MassiveNeutrinos):
"""LCDM with massive neutrinos."""
Model settings, such as the lengths of the multipole hierarchies, are defined via class keyword arguments. The following example defines a model with fewer photon and neutrino multipoles:
class Coarse(LCDM, LMAX_G=10, LMAX_NR=10):
"""LCDM with fewer photon and neutrino multipoles."""
You can also modify an existing model by adding, removing or swapping components, or changing its settings, as follows:
from discoeb.components.species import CosmologicalConstant, DarkEnergy, Fluid
LCDM.swap(CosmologicalConstant, DarkEnergy, name="w0waCDM")
LCDM.add(Fluid.named("dr"), name="LCDM+dr") # parameters dr_omega, dr_w, dr_cs2
LCDM.remove(CosmologicalConstant, name="CDM")
LCDM.with_settings(LMAX_G=20)
Writing a new component#
Components are base classes that model a particular fluid species, interaction between species, or gravitational theory. A new component is defined by creating a new class that inherits from an existing component like Species. It declares its state, cosmological parameters and settings as class attributes. Its @derivative methods define contributions to the Einstein-Boltzmann equations, and @contributes methods mark summed quantities that they contribute to.
Components consist of the following elemtents:
State(size, initial=...)is a state, scalar by default, withinitialits starting value.Parameter(default)defines a run-time parameter of the model.Setting(defult)defines a compile-time constant.@derivative("x")marks a contribution to the rate of the statex, in conformal time.@contributes("rho")marks a contribution to a summed quantity. The gravity components read the sumsrho,pressure,delta_rho,momentumandstress, and the initial conditions readrho_matter,rho_radiationandrho_free_streaming.
The methods compile to CUDA device code, so they must be written in plain Python. math functions and
plain loops with bounds fixed by settings are acceptable. A model missing a quantity that
one of its components’ methods requires as an input will error at compile time.
For example, the following component defines a second dark matter species with a density parameter omega_x and an adiabatic initial condition for its density perturbation delta_x:
from jax.typing import ArrayLike
from discoeb.components import Parameter, Species, State, contributes, derivative
class SecondDarkMatter(Species):
delta_x = State(initial=lambda F: 0.75 * F[0]) # adiabatic mode
omega_x: ArrayLike = Parameter(0.0)
def rho_x(self, omega_x, a):
return omega_x / a**3
@contributes("rho", "rho_matter")
def rho_second(self, rho_x):
return rho_x
@contributes("delta_rho")
def delta_rho_x(self, rho_x, delta_x):
return rho_x * delta_x
@derivative("delta_x")
def continuity_x(self, h_prime):
return -0.5 * h_prime
LCDM_X = LCDM.add(SecondDarkMatter, name="LCDM+x")
To swap in a theory of gravity, replace the Background or Metric components with new ones:
LCDM.swap(Background, MyBackground, name=...)
LCDM.swap(Metric, MyMetric, name=...).