Models#

Cosmological models. A model is a class whose bases are its components, and a cosmology is an instance of it.

class discoeb.Model(*args, **values)[source]#

Bases: Recombination

A cosmological model. Its instances are cosmologies.

A model lists its components as bases, in state order, after Model or after a model whose components it extends:

class LCDM(Model, Background, ..., ThomsonScattering): ...
class LCDM_MNU(LCDM, MassiveNeutrinos): ...

The parameters the components declare are the keyword arguments of its constructor. They are required if there is no default. A type checker reads them off the declarations (the dataclass_transform on Component), and help, IPython and the class docstring list them. Every model has the recombination block (RECOMBINATION), whose z_re it inherits from Recombination.

Class keywords set component settings, class Coarse(LCDM, LMAX_G=10), and name= the name errors use. A setting a model leaves out takes the component’s own value, which DISCO_<NAME> in the environment or rebinding the class attribute (Photons.LMAX_G = 20) sets. The solver caches its kernel on resolved(), the model with every setting explicit.

A cosmology is an immutable JAX pytree whose leaves are its parameters, so jax.jit traces them and jax.jacfwd returns a cosmology of derivatives, one column of the solve per parameter. Take it in chosen parameters to integrate fewer: jacfwd(lambda om: f(planck.replace(omega_c=om))).

name: ClassVar[str]#

The model’s name, name= or the class’s.

components: ClassVar[tuple[type[Component], ...]]#

The component classes, in state order.

settings: ClassVar[tuple[tuple[str, object], ...]]#

The settings the model overrides, (name, value) sorted by name.

classmethod from_dict(params) → Model[source]#

Return the cosmology of a parameter dictionary.

Unlike cls(**params), it accepts keys this model does not have while they keep their declared default (w_DE_0 = -1 for a model without dark energy), and ignores keys no component declares (validate_keys()). Use it for a dictionary written for another model. Under jit or a derivative such a key’s value is traced and can’t be checked. The solve reads none of it, so its derivative is zero.

classmethod from_density_parameters(*, Omega_m, Omega_b, H_0, Omega_k=0.0, **others) → Model[source]#

Return the cosmology with matter fraction Omega_m, baryon fraction Omega_b and Hubble constant H_0.

The model’s own parameters are physical densities. This converts, omega_b = Omega_b h^2 and omega_c = (Omega_m - Omega_b) h^2, and sets omega_de so that the model expands at H_0 (km/s/Mpc) today (physical_parameters()). others are the model’s remaining parameters (T_cmb, A_s, …). JAX values stay differentiable, so this is how to take a derivative in H_0 or Omega_m. A model with no omega_de (a scalar field) has no such constructor.

property H_0#

The Hubble constant in km/s/Mpc, which the densities sum to (hubble_constant()).

to_dict() → dict[str, Any][source]#

Return the parameters by name, every one of the model’s.

replace(**changes) → Model[source]#

Return this cosmology with changes to its parameters: planck.replace(H_0=70.0).

A change may also name a derived column of the parameter row, a quantity the model computes from its parameters such as the photon density omega_g (derived_parameters() gives them). The cosmology then holds that value in place of the computed one, the solve’s parameter rows take it, and a derivative reaches it like any parameter: jacfwd(lambda w: f(planck.replace(omega_g=w)))(planck.omega_g). planck.omega_g reads the value set, else the one the parameters imply. The value stays when a parameter it is computed from changes.

classmethod has(component: type[Component]) → bool[source]#

Whether the model includes component or a subclass of it.

classmethod setting(name: str)[source]#

Return a setting’s value: the model’s override or the component’s own.

classmethod resolved() → type[Model][source]#

Return this model with every setting explicit, at the components’ current values.

Models of one name, components and settings resolve to the same class, the first such model asked or a subclass of it, so the solver caches its kernels on it. Rebinding a component’s setting before a solve resolves to another class and compiles a new kernel.

classmethod instances() → list[Component][source]#

Return the components instantiated with the model’s settings.

classmethod build(thermal_history: Component | None = None, extra=(), time: str = 'tau', clock: str | None = None, last=()) → Equations[source]#

Return the component Equations of this cosmological model.

thermal_history, put first, stands in for the recombination block on the host by providing a, opacity and cs2 (a test’s constant history). The solve appends RECOMBINATION instead (joint_model()). Without either the model still gives the state layout, the initial conditions and the sparsity pattern. extra lists component instances to append (the line-of-sight sources of DISCO-CMB).

classmethod parameters(extra=()) → dict[str, ParameterDeclaration][source]#

Return the parameters the model’s components declare, by name.

They include those of the recombination block every solve appends (discoeb.solver._kernel.joint_model()) and of the component classes extra. They make up the cosmology dictionary (parameter_keys()). Those a method running in the kernel reads are columns of its parameter row (discoeb.solver._kernel.parameter_layout()).

classmethod parameter_keys() → dict[str, Any][source]#

Return the constructor’s parameters and their defaults, REQUIRED if none.

classmethod state_names() → tuple[str, ...][source]#

Return the perturbation state’s element names in index order.

classmethod sparsity()[source]#

Return the (nnz, 2) index array of the perturbation Jacobian’s nonzeros.

modax’s Rodas5P kernel uses the pattern twice. It colours the column intersection graph, so a Jacobian costs n_colours + 1 Enzyme sweeps instead of nvar + 1. It also orders the pattern, factorises it symbolically and compiles a sparse LU and a pair of sparse triangular solves for that exact structure. Equations.sparsity traces the pattern through the equations, so the tridiagonal free-streaming tails and the Einstein constraints that tie the metric, the fluids and the lowest multipoles together come from the code that writes them. The pattern is exact. For flat LCDM at the default truncations it has 173 entries and 11 colours (tests/test_models.py).

The kernel factorises I/(h gamma) - J. modax keeps a slot for every diagonal and resolves slot(i, i) through that table even where J has no entry (an undamped free-streaming multipole has none).

classmethod validate(cosmology) → None[source]#

Raise ValueError if cosmology sets parameters this model ignores.

cosmology is a parameter dictionary or a cosmology. A key that another component declares but none of this model’s must keep its declared default: a model without the dark-energy fluid needs w_DE_0 = -1 and w_DE_a = 0, one without massive neutrinos N_mnu = 0. Then each component’s check hook runs on the keys it names (DarkEnergy.check needs w != -1). A traced value, under jit, has nothing to compare yet and is skipped. The method is not named check because a model inherits its components’ check hooks.

classmethod validate_keys(cosmology) → None[source]#

Raise ValueError if cosmology sets a parameter of a component this model lacks.

The first half of validate(): a key that another component declares and none of this model’s must keep its declared default, or the solve would silently ignore it.

classmethod hubble_constant(cosmology)[source]#

Return H_0 in km/s/Mpc: the model’s expansion rate today, aH(a = 1).

The densities are physical and determine H_0. A model whose expansion rate reads a state of its own (a scalar field) has it only from a solve, as aH today, and raises TypeError.

classmethod physical_parameters(params) → dict[source]#

Return a dictionary in Omega_m, Omega_b, H_0 as one in the model’s own parameters.

omega_b = Omega_b h^2, omega_c = (Omega_m - Omega_b) h^2, omega_k = Omega_k h^2 (Omega_k defaults to 0), any other component’s x_omega = x_Omega h^2, and omega_de the dark-energy density that makes the model expand at H_0 today. The expansion rate squared is linear in a density, so one secant step from two evaluations finds it exactly. Every other key passes through. It is plain JAX arithmetic, so a derivative in H_0 or Omega_m goes through it.

classmethod add(*components: type[Component], name: str) → type[Model][source]#

Return a model with components appended, as class name(cls, *components).

classmethod swap(old: type[Component], new: type[Component], *, name: str) → type[Model][source]#

Return a model with new in place of old, at its position.

classmethod remove(*components: type[Component], name: str) → type[Model][source]#

Return a model without components.

classmethod with_settings(**settings) → type[Model][source]#

Return this model with the given component settings (LMAX_G=20), a subclass.