"""The spectra of the transfer functions and a primordial spectrum: :func:`cmb_power_spectrum`."""
from __future__ import annotations
from typing import NamedTuple
import jax
import jax.numpy as jnp
from .._cosmology import as_cosmologies
from .._numerics import trapezoid_weights
[docs]
class Spectra(NamedTuple):
"""The temperature, cross, E-mode and lensing potential spectra.
Each field is ``(n_cosmologies, lmax + 1)``, indexed by the multipole
from ``l = 0``. As ``C_l`` the first three are in units of
``T_cmb^2`` and ``pp``, ``C_l^{psi psi}``, is dimensionless. As ``D_l``
the first three are ``l(l+1) C_l / 2pi`` in ``muK^2`` and ``pp`` is
``[l(l+1)]^2 C_l^{psi psi} / 2pi``.
"""
#: The temperature spectrum.
tt: jax.Array
#: The temperature and E-mode cross spectrum.
te: jax.Array
#: The E-mode spectrum.
ee: jax.Array
#: The lensing potential spectrum.
pp: jax.Array
[docs]
def power_law(A_s, n_s, k_pivot):
"""Return ``P_R(k) = A_s (k / k_pivot)^(n_s - 1)`` as a function of ``k`` ``(n, ...)``.
``A_s``, ``n_s`` and ``k_pivot`` are numbers or ``(n,)``, one per
cosmology. :func:`cmb_power_spectrum` takes the function as
``primordial``. The tilt takes the precision of ``k``.
"""
A_s, n_s, k_pivot = (jnp.asarray(v) for v in (A_s, n_s, k_pivot))
def primordial(k):
shape = (-1,) + (1,) * (k.ndim - 1)
row = [jnp.reshape(v, shape) for v in (A_s, n_s, k_pivot)]
# The tilt in the precision of k: a double pow per cosmology and point
# runs at a 64th of the rate on consumer GPUs.
tilt = ((row[1] - 1.0) * jnp.log(k / row[2])).astype(k.dtype)
return row[0] * jnp.exp(tilt)
return primordial
def _parameter(batch, name):
"""Return the ``(n,)`` values of ``name`` across the cosmologies, traced or not."""
return jnp.stack([jnp.asarray(c.to_dict()[name], jnp.float64) for c in batch])
[docs]
def cmb_power_spectrum(transfers, cosmologies, *, primordial=None):
"""Return ``(C_l, D_l)``, each a :class:`Spectra` at every multipole to ``lmax``.
The integral over the wave modes,
``C_l^{XY} = 4 pi int dln k P_R(k) X_l(k) Y_l(k)``,
of the :class:`~discocmb.transfer.Transfers` ``transfers``, where ``X``
and ``Y`` stand for ``Theta_l`` or ``E_l``. The lensing potential's
``C_l^{psi psi}`` is the same integral, exact at low ``l`` and Limber
above (:func:`lensing_potential_spectrum`). The projection computes the
multipoles ``grid.ells``, and a cubic spline in ``ln l`` gives every
other one. At ``grid.ells`` the values are the projection's own.
``cosmologies`` are the batch's, whose ``T_cmb`` converts ``D_l`` to
``muK^2``. ``primordial`` maps wave numbers ``(n, ...)`` to ``P_R``, one
row per cosmology (:func:`power_law`). By default it is the power law of
the cosmologies' ``A_s``, ``n_s`` and ``k_p``. Traceable.
"""
batch = as_cosmologies(cosmologies)
if primordial is None:
primordial = power_law(*(_parameter(batch, n) for n in ("A_s", "n_s", "k_p")))
layout, ops = transfers.grid.layout, transfers.grid.operators
n = transfers.scale.shape[0]
# The stretched quadrature mode k_fine stands for the wave number k_fine / scale.
k = jnp.asarray(layout.k_fine)[None, :] / transfers.scale[:, None] # (n, f)
nodes = weighted_spectra(
transfers.theta,
transfers.theta_e,
ops.band,
transfers.scale,
primordial(k) * ops.lnk_weights,
)
k_lensing = jnp.asarray(layout.k_lensing)
# The Limber wave numbers in the grid's precision: 1 ms of a batch of 128
# in single, where C_l^pp moves by 1e-7.
k_chi = ops.k_chi.astype(ops.limber_weights.weights.dtype)
pp = lensing_potential_spectrum(
transfers.theta_psi,
transfers.limber,
k_lensing,
primordial(jnp.broadcast_to(k_lensing, (n,) + k_lensing.shape)),
primordial(jnp.broadcast_to(k_chi, (n,) + k_chi.shape)),
)
# The spline onto every multipole in the grid's precision, as the lensing
# reads the spectra: in double the product ran at a 64th of the rate on
# consumer GPUs, 0.6 ms of a batch of 128 (5e-8 of the peak).
every = ops.every_ell
hi = jax.lax.Precision.HIGHEST
cl = Spectra(
*(
jnp.matmul(c.astype(every.dtype), every, precision=hi).astype(jnp.float64)
for c in nodes
),
pp @ ops.every_ell_pp,
)
ell = jnp.arange(ops.every_ell.shape[1], dtype=jnp.float64)
llp1 = ell * (ell + 1.0) / (2.0 * jnp.pi)
norm = (_parameter(batch, "T_cmb") * 1.0e6) ** 2
dl = Spectra(
*(llp1 * c * norm[:, None] for c in cl[:3]),
ell * (ell + 1.0) * llp1 * cl.pp,
)
return cl, dl
@jax.jit
def lensing_potential_spectrum(theta_psi, limber, k_values, p_k, p_chi):
"""Return ``C_l^{psi psi}``, ``(n_c, n_ell_exact + n_ell_limber)``, from :func:`~discocmb.transfer.lensing_transfer`.
``p_k`` is ``P_R`` at the solved modes ``k_values``, ``(n_c, n_k)``, and
``p_chi`` at the Limber wave numbers ``k_chi``, ``(n_c, n_ell_limber,
n_chi)``: any primordial spectrum, a power law (:func:`power_law`) or
not.
"""
# Exact projection: C_l = 4 pi int dln k P_R(k) Theta_l(k)^2.
w = trapezoid_weights(jnp.log(k_values), jnp)
cl_exact = 4.0 * jnp.pi * jnp.einsum("clk,ck,k->cl", theta_psi**2, p_k, w)
# Limber: C_l = 2 pi^2 int dchi S(k_chi, chi)^2 P_R(k_chi) / (k_chi^3 chi^2), k_chi = (l + 1/2) / chi.
cl_limber = 2.0 * jnp.pi**2 * jnp.sum(limber * p_chi, axis=-1)
return jnp.concatenate([cl_exact, cl_limber], axis=1)
def weighted_spectra(theta, theta_e, band, scale, weight):
"""Return ``4 pi scale^2 sum_f weight X_l Y_l`` for ``XY = TT, TE, EE``, ``(n_c, n_ell)`` each.
``theta`` and ``theta_e`` are :func:`~discocmb.transfer.project`'s
``(n_f, n_ell, n_c)`` transfer functions and ``weight`` is ``(n_c, n_f)``.
With ``weight = P_R(k) dln k`` these are ``C_l = 4 pi int dln k P_R(k)
X_l(k) Y_l(k)`` at the nodes, over the modes of ``band`` ``(n_ell,
n_f)``.
The products and the sum over the modes run in single precision on the
``(f, l, c)`` arrays as they come: in double, after a transpose, the
three integrals were 12 ms of a batch of 128.
"""
weight = weight.astype(theta.dtype) # (c, f)
mask = jnp.asarray(band, theta.dtype).T # (f, l)
weighted = theta * mask[:, :, None]
weighted_e = theta_e * mask[:, :, None]
factor = 4.0 * jnp.pi * scale[:, None] ** 2
tt = (
jnp.einsum("flc,flc,cf->cl", weighted, theta, weight).astype(jnp.float64)
* factor
)
te = (
jnp.einsum("flc,flc,cf->cl", weighted, theta_e, weight).astype(jnp.float64)
* factor
)
ee = (
jnp.einsum("flc,flc,cf->cl", weighted_e, theta_e, weight).astype(jnp.float64)
* factor
)
return tt, te, ee