Source code for discocmb.spectra._solve

"""The spectra of a batch in one call: :func:`solve_cmb`."""

from __future__ import annotations

import functools

import discoeb as eb
import numpy as np

from .._cosmology import as_cosmologies, cosmology_of, fiducial
from ..grids._grids import Grid
from ..sources._component import LineOfSightSources, error_weights
from ..transfer._transfer_functions import transfer_functions
from ._spectrum import cmb_power_spectrum


@functools.lru_cache(maxsize=1)
def _fiducial_grid(parameters, model) -> Grid:
    """Return the default :class:`~discocmb.Grid` of one fiducial, kept for the next call."""

    return Grid.from_fiducial(cosmology_of(parameters, model))


[docs] def solve_cmb(cosmologies, *, grid: Grid | None = None, **solve_kwargs): """Return ``(ells, C_l, D_l)`` of a batch: the multipoles and two :class:`~discocmb.Spectra`. ``cosmologies`` is one DISCO-EB cosmology or a list of one model's, ``[discoeb.LCDM(omega_b=..., ...), ...]``. ``ells`` is ``0..grid.lmax`` and each spectrum ``(n_cosmologies, lmax + 1)``: TT, TE, EE and the lensing potential, unlensed. :func:`~discocmb.lensed_spectra` lenses ``C_l``. The steps, each of which can run on its own: 1. :func:`discoeb.solve_eb` solves every wave mode of every cosmology in one launch and keeps the sources, which :class:`~discocmb.LineOfSightSources` evaluates in the kernel, with :func:`~discocmb.error_weights`. 2. :func:`~discocmb.transfer_functions` projects the sources onto the multipoles. 3. :func:`~discocmb.cmb_power_spectrum` integrates the transfer functions over the wave modes against the cosmologies' primordial spectra. ``grid`` (from :meth:`~discocmb.Grid.from_fiducial`) fix the wave modes, save times, multipoles and operators. Build them once and pass them to every batch. Without them the first cosmology shapes the grid, and the last of these stays cached. ``solve_kwargs`` go to the solver (``rtol``, ``atol``, ``max_steps``, ...), and ``error_weights`` there go over :func:`~discocmb.error_weights`. **Derivatives.** The spectra are differentiable in every parameter of the cosmologies, in forward and reverse mode (``jax.jacfwd``, ``jax.grad``): a cosmology is a pytree, and ``jax.jacfwd`` of a function of one returns the derivatives as a cosmology. The kernel carries forward sensitivities through the hierarchy and the recombination history, and the kept conformal times carry the distance to every source. ``cosmology.replace`` also sets a column DISCO-EB derives from the parameters, such as ``omega_g`` (:func:`discoeb.derived_parameters`), for a derivative in it. Derivatives need a grid in double precision (``dtype=jnp.float64``), and for many directions ``split_derivatives=True`` keeps a thread's memory that of one column. """ batch = as_cosmologies(cosmologies) if grid is None: grid = _fiducial_grid(*fiducial(batch[0])) solve_kwargs["error_weights"] = error_weights( type(batch[0]), solve_kwargs.get("error_weights") ) values = eb.solve_eb( batch, grid.k_values, grid.a_save, extra=(LineOfSightSources,), keep=[LineOfSightSources.keep], **solve_kwargs, ) cl, dl = cmb_power_spectrum(transfer_functions(values, grid), batch) return np.arange(grid.lmax + 1), cl, dl