"""The transfer functions of a batch from the sources its solve kept: :func:`transfer_functions`."""
from __future__ import annotations
from typing import Any, NamedTuple
import jax
import jax.numpy as jnp
import numpy as np
from ..sources._channels import weighted_channels
from ..sources._component import LineOfSightSources
from ._lensing_potential import lensing_transfer
from ._projection import project
from ._resample import resample_stencil
[docs]
class Transfers(NamedTuple):
"""The transfer functions of a batch, from :func:`transfer_functions`.
The spectra are quadratic forms of these, weighted by the primordial
spectrum (:func:`~discocmb.cmb_power_spectrum`), so a change of ``A_s``
or ``n_s`` alone needs no new solve. A JAX pytree, like the
:class:`~discocmb.Grid` it carries.
"""
#: The temperature transfer functions ``(n_k_fine, n_ell, n_c)`` at
#: ``k_fine / scale``, without the factor ``scale``.
theta: jax.Array
#: The E-mode transfer functions, likewise.
theta_e: jax.Array
#: Each distance to last scattering over the fiducial's, ``(n_c,)``.
scale: jax.Array
#: The lensing potential's transfer functions at its exact multipoles,
#: ``(n_c, n_ell_exact, n_k_lensing)``.
theta_psi: jax.Array
#: The Limber integrand of the lensing potential without ``P_R``,
#: ``(n_c, n_ell_limber, n_save)``.
limber: jax.Array
#: The :class:`~discocmb.Grid` the sources were projected on.
grid: Any
def _extrapolated(weyl, k_values, k_lensing):
"""Return the Weyl potential ``(n_c, n_k, n_save)`` continued to the modes ``k_lensing`` past the solved ``k_values``.
Only the Limber integral reads it there, at ``z < 1``, where ``k^2 phi =
A + B ln k`` continues the last two solved modes: the form of the
matter transfer function well inside the horizon at equality.
"""
if len(k_lensing) == len(k_values):
return weyl
k_last, k_before = k_values[-1], k_values[-2]
last, before = k_last**2 * weyl[:, -1], k_before**2 * weyl[:, -2] # (n, n_save)
slope = (last - before) / np.log(k_last / k_before)
k_more = jnp.asarray(k_lensing[len(k_values) :])[None, :, None]
more = (last[:, None] + slope[:, None] * jnp.log(k_more / k_last)) / k_more**2
return jnp.concatenate([weyl, more], axis=1)
[docs]
def transfer_functions(values, grid) -> Transfers:
"""Return the :class:`Transfers` of the sources ``values`` that a solve kept on ``grid``.
``values`` is :func:`discoeb.solve_eb`'s ``(n_c, 7, n_k, n_save)`` of
:attr:`~discocmb.LineOfSightSources.keep` at the modes ``grid.k_values`` and the
scale factors ``grid.a_save``. Traceable, and differentiable in
``values``.
Each cosmology reaches conformal times of its own at the saves, its
last the conformal age. A 5-point stencil resamples its sources onto the
fiducial's distances, stretched by its distance to last scattering over
the fiducial's (:func:`project`), and the Bessel tables of ``grid``
project them onto the multipoles. The lensing potential's transfer
reads the Weyl potential on the unstretched grid, since its kernel is
broad in ``chi``.
"""
layout, ops = grid.layout, grid.operators
tau_save = jnp.asarray(layout.tau_save)
tau_end = float(layout.tau_save[-1])
channels = weighted_channels(values) # (n_c, 5, n_k, n_save)
tau = values[:, LineOfSightSources.keep.index("tau"), 0] # (n_c, n_save)
# Each distance to the scale factor of the fiducial's visibility peak.
# Recombination lies within a part in a thousand of that scale factor in
# every cosmology, and unlike the peak of the saved visibility this
# distance is smooth in the parameters.
chi_star = tau[:, -1] - tau[:, layout.i_star]
scale = chi_star / layout.chi_star
idx, w = resample_stencil(tau / scale[:, None], tau_save, tau_end)
theta, theta_e = project(
channels,
tau,
idx,
w,
ops.band,
scale,
jnp.log(jnp.asarray(layout.k_fine)),
ops.lnk_knots,
ops.lnk_curvature,
ops.keep,
ops.bessel,
ops.bessel_e,
layout.spans,
)
shift_idx, shift_w = resample_stencil(tau, tau_save, tau_end)
weyl = _extrapolated(channels[:, 3], layout.k_values, layout.k_lensing)
theta_psi, limber = lensing_transfer(
weyl,
shift_idx,
shift_w,
tau_end - tau_save,
chi_star,
ops.lensing_exact,
ops.limber_weights,
ops.k_chi,
)
return Transfers(theta, theta_e, scale, theta_psi, limber, grid)