# Computing a batch of spectra

## Setting up cosmologies

Cosmologies are defined using is a list of DISCO-EB cosmological models (see DISCO-EB's
[cosmologies](https://cosmo-sims.github.io/DISCO-EB/usage/cosmologies.html)):

```python
from discoeb import LCDM, solve_eb
from discocmb import (
    Grid,
    LineOfSightSources,
    cmb_power_spectrum,
    error_weights,
    transfer_functions,
)

planck = LCDM(
    omega_b=0.02239,
    omega_c=0.1200,
    omega_de=0.3108,
    A_s=2.1e-9,
    n_s=0.966,
    T_cmb=2.7255,
    Y_He=0.2454,
    N_eff=3.046,
    z_re=7.67,  # reionization redshift, none by default
)
batch = [planck.replace(omega_c=c) for c in (0.118, 0.120, 0.122)]
```

## Defining the integration grid

{py:meth}`Grid.from_fiducial <discocmb.Grid.from_fiducial>` places the
solved modes (`n_k`), the save times (`n_save`) and the quadrature modes
(`k_fine_step`) where the fiducial's sources vary, and pre-computes Bessel and
Wigner tables on that grid (cached on disk for later re-use). The
projection computes multipoles to a given maximum multipole, and a spline
is used to interpolate between multipoles. The default
grid ensure unlensed and lensed spectrum match to within 0.5 percent of CLASS up to $\ell = 2500$. Further keyword arguments of `solve_cmb` are passed to the E-B solver, such as `rtol` and `atol`.

```python
grid = Grid.from_fiducial(planck)
```

## Running the E-B solve and computing spectra

{py:func}`discoeb.solve_eb` solves every wave mode of every cosmology in one
GPU launch. {py:class}`~discocmb.LineOfSightSources` evaluates the source functions
inside the E-B integration kernel, and `keep` ensures the kernel only returns the sources we need for the line-of-sight integral. {py:func}`~discocmb.error_weights` constructs appropriate error weights for the E-B integration that ensure low multipoles of EE and TE are computed accurately. {py:func}`~discocmb.transfer_functions` projects the
sources onto the multipoles, and {py:func}`~discocmb.cmb_power_spectrum`
integrates the transfer functions over the wave modes.

```python
values = solve_eb(
    batch,
    grid.k_values,
    grid.a_save,
    extra=[LineOfSightSources],
    keep=[LineOfSightSources.keep],
    error_weights=error_weights(LCDM),
)
transfers = transfer_functions(values, grid)
cl, dl = cmb_power_spectrum(transfers, batch)
```

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

```python
from discocmb import solve_cmb

ells, cl, dl = solve_cmb(batch, grid=grid)
```

`cl` and `dl` are {py:class}`~discocmb.Spectra` with fields `tt`, `te`, `ee`
and `pp`, each of shape `(n_cosmologies, grid.lmax + 1)` from $\ell = 0$.
`cl` holds $C_\ell$ in units of $T_\mathrm{cmb}^2$ and the dimensionless
$C_\ell^{\psi\psi}$, `dl` holds $\ell(\ell+1)C_\ell/2\pi$ in
$\mu\mathrm{K}^2$ and $[\ell(\ell+1)]^2 C_\ell^{\psi\psi}/2\pi$.

## Lensing the spectra

{py:func}`~discocmb.lensed_spectra` lenses the unlensed spectra of
`solve_cmb` or `cmb_power_spectrum`, using the lensing potential spectrum and the Wigner tables pre-computed in the `Grid` object:

```python
from discocmb import lensed_spectra

ells, cl, dl = solve_cmb(batch, grid=grid)
lensed = lensed_spectra(cl, grid)
lensed.bb  # (n_cosmologies, grid.lmax + 1), C_l in units of T_cmb^2
```

The result is a {py:class}`~discocmb.lensing.LensedSpectra` with fields
`tt`, `te`, `ee` and `bb`, in the units of `cl`. The full-sky
correlation-function method sums over every multipole to `grid.lmax`, so
the lensed spectra are good to a few hundred multipoles below it. The sums
run in the precision of the grid, chunked into 32 cosmologies at a time by default.
