# Design

DISCO-CMB computes the spectra of a batch of cosmologies in three steps, on
grid defined by a given fiducial cosmology:

1. DISCO-EB's {py:func}`~discoeb.solve_eb` computes and returns the line-of-sight sources via `discocmb.sources`, evaluated efficiently inside the Einstein-Boltzmann integration kernel.
2. {py:func}`~discocmb.transfer_functions` projects them onto the transfer
   functions $\Theta_\ell(k)$, $E_\ell(k)$ and the lensing potential.
3. {py:func}`~discocmb.cmb_power_spectrum` integrates these against the
   primordial power spectrum,
   $C_\ell^{XY} = 4\pi \int \mathrm{d}\ln k \, P_\mathcal{R}(k) X_\ell(k) Y_\ell(k)$.

For convenience, {py:func}`~discocmb.solve_cmb` runs all three steps together.

## Evaluating sources inside the E-B kernel

DISCO-EB solves every wave mode of every cosmology in one GPU launch, and
compiles any component passed as `extra` into its kernel. This allows us to
include additional physics in the integration, such as
{py:class}`~discocmb.LineOfSightSources`, which computes and returns the line-of-sight sources on-the-fly as the integration proceeds. This is much more memory-efficient than returning the entire Einstein-Boltzmann state through cosmic time.

## Defining the integration grid

{py:meth}`Grid.from_fiducial <discocmb.Grid.from_fiducial>` is a helper function that defines the integration grid for the E-B solve based on the fiducial cosmology. It determines the wave-modes, save times and quadrature points we solve by checking where the sources vary for a fiducial cosmology. The
grid helper also computes and caches tables of Bessel functions and their derivatives,
$j_\ell$, $j_\ell'$, $j_\ell''$ and $j_\ell/x^2$, the lensing potential
operators and the Wigner $d$-functions.

## From sources to spectra

DISCO-EB's solve keeps four sources at every save time: the monopole and ISW
term $S$, the Doppler term $S_{j1}$ and the polarization quadrupole
$S_{j2}$, each multiplied by the visibility $g = \kappa' e^{-\kappa}$. The
line-of-sight integral {py:func}`~discocmb.transfer_functions` projects them onto each multipole:

$$
\Theta_\ell(k) = \int_0^{\tau_0} \mathrm{d}\tau\,\left[S\,j_\ell(x) + S_{j1}\,j_\ell'(x) + S_{j2}\,j_\ell''(x)\right], \qquad x = k(\tau_0-\tau),
$$

$$
E_\ell(k) = \sqrt{\frac{(\ell+2)!}{(\ell-2)!}} \int_0^{\tau_0} \mathrm{d}\tau\,S_{j2}\,\frac{j_\ell(x)}{x^2}.
$$

It evaluates both by resampling each
cosmology's sources onto the fiducial's distance grid, stretched by
$\chi_*/\chi_*^\mathrm{fid}$. It then refines them in $\ln k$ with a cubic
spline and contracts them against the tables of $j_\ell$, $j_\ell'$,
$j_\ell''$ and $j_\ell/x^2$ in batched matrix products.

{py:func}`~discocmb.cmb_power_spectrum` integrates the products of these
transfer functions over the wave modes:

$$
C_\ell^{XY} = 4\pi\int \mathrm{d}\ln k\,P_\mathcal{R}(k)\,X_\ell(k)\,Y_\ell(k),
$$

by a trapezoidal integration on uniform steps in $k$ at the projected multipoles, and
a cubic spline in $\ln\ell$ fills in every other multipole. The lensing
potential $C_\ell^{\psi\psi}$ comes the same way from the saved Weyl
potential against the lensing kernel: exactly below $\ell = 30$, and by the
Limber approximation $k = (\ell + 1/2)/\chi$ above.
