"""The sources as a DISCO-EB component, and the error weights of the solve that keeps them."""
from __future__ import annotations
import functools
import discoeb as eb
import numpy as np
from discoeb import Component
[docs]
class LineOfSightSources(Component):
"""CAMB-form temperature and polarization sources as a model component.
It provides the Weyl potential ``k^2 phi = -4 pi G a^2 (delta_rho + 3 aH
(rho + p) theta / k^2 + 3/2 (rho + p) sigma)`` and the unweighted parts
of the three sources, without the ISW term: ``monopole + 5/8 Pi``, ``k
alpha + theta_b / k`` and ``15/8 Pi``, with ``Pi`` the scattering
source. Every species the model includes contributes through the sums
``delta_rho``, ``momentum`` and ``stress``.
Pass it to :func:`discoeb.solve_eb` as ``extra=[LineOfSightSources]``
with ``keep=[LineOfSightSources.keep]``. DISCO-EB compiles it into the
kernel, which evaluates the sources at every save.
"""
keep = ("m0", "m1", "m2", "phi", "opacity", "kappa", "tau")
"""What :func:`discoeb.solve_eb` keeps at every save, in the channel order the transfer functions read.
The three unweighted sources and the Weyl potential, which this
component provides, and the opacity ``kappa'``, the optical depth
``kappa`` and the conformal time ``tau``, which the model's
recombination history has: the solve integrates in the scale factor, and
``tau`` is a state whose value today is the conformal age. The list
leaves out ``phi'`` on purpose. Late-time ``phi'`` is a small residual of
large terms that cancel, so the analytic derivative amplifies the
solver's state error tenfold at ``l = 2``. Differencing saved potentials
cancels that error, which :func:`~discocmb.sources.cmb_sources` does."""
def phi(
self, delta_rho, delta_rho_streaming, momentum, stress, four_pi_G_a2, aH, k
):
"""Return the Weyl potential, including radiation under the streaming approximation."""
inv_k2 = 1.0 / (k * k)
return (
-four_pi_G_a2
* (
delta_rho
+ delta_rho_streaming
+ 3.0 * aH * momentum * inv_k2
+ 1.5 * stress
)
* inv_k2
)
def monopole(self, eta, phi, delta_g):
return -eta + 2.0 * phi + 0.25 * delta_g
def m0(self, monopole, Pi):
return monopole + 0.625 * Pi
def m1(self, alpha, theta_b, k):
inv_k2 = 1.0 / (k * k)
return k * (alpha + theta_b * inv_k2)
def m2(self, Pi):
return 1.875 * Pi
POLARIZATION_WEIGHT_K = 1.0
"""Wave number (Mpc^-1) below which the error norm scales up the photon
quadrupole and ``E_2`` by ``POLARIZATION_WEIGHT_K / k``."""
POLARIZATION_WEIGHT_MAX = 1.0e4
"""Upper limit of that weight."""
K_MAX_CMB = 0.4
"""Wave number (Mpc^-1) of the last mode the temperature and polarization read.
The modes above it feed only the lensing potential, through the Weyl
potential long after recombination."""
LENSING_RADIATION_WEIGHT = 0.01
"""Error weight of the photon and neutrino hierarchies of the modes above :data:`K_MAX_CMB`.
The solve need not resolve their radiation as it does the CMB modes'. At
0.01 the lensing potential moves by 2e-5, and the solve, whose longest
trajectories they are, takes 58 ms in place of 73 for a batch of 128."""
[docs]
def error_weights(model, overrides: dict | None = None) -> dict:
"""Return ``{state name: weight}`` for :func:`discoeb.solve_eb`'s ``error_weights``.
**Polarization.** The E-mode source is ``g Pi`` with ``Pi = sigma_g / 5 +
3/5 E_2``. Well below the acoustic scale both moments stay near the
solve's ``atol = 1e-4`` through recombination, so step-size control
ignores them and the low multipoles of EE and TE come out 10 to 40
percent low. The weight ``clip(POLARIZATION_WEIGHT_K / k, 1,
POLARIZATION_WEIGHT_MAX)`` divides the tolerance of the photon quadrupole
``F_2`` and ``E_2`` (``E_0``). It slows only the low-``k`` trajectories,
which take the fewest steps: a batch of 128 costs 10 percent more, and EE
matches CLASS to 0.35 percent from ``l = 2``. A scalar tolerance of 1e-5
reaches the same accuracy at 1.8 times the cost.
**Massive neutrinos.** The bins ``psi`` get weight zero, which takes them
out of step-size control. They stream freely to today, and resolving them
cost a massive-neutrino batch most of its steps. With the photon
quadrupole and ``E_2`` weighted, the spectra stay as close to CLASS
without them.
**Lensing modes.** Above :data:`K_MAX_CMB` every photon and neutrino
state takes :data:`LENSING_RADIATION_WEIGHT`.
``overrides``, ``{state name: weight}``, go over the first two and the
lensing modes' weight over all. A weight is a number or a function of
the wave numbers, as DISCO-EB takes it.
"""
def polarization(k):
return np.clip(
POLARIZATION_WEIGHT_K / np.asarray(k), 1.0, POLARIZATION_WEIGHT_MAX
)
names = eb.state_names(model)
weights: dict = {"F_2": polarization, "E_0": polarization}
weights.update((n, 0.0) for n in names if n.startswith("psi_"))
weights.update(overrides or {})
def radiation(k, inner=None):
if inner is None:
base = np.ones_like(np.asarray(k, dtype=np.float64))
else:
base = inner(k) if callable(inner) else np.full(np.shape(k), float(inner))
return np.where(np.asarray(k) > K_MAX_CMB, LENSING_RADIATION_WEIGHT, base)
for name in names:
if name.split("_")[0] in ("F", "E", "N", "psi"):
weights[name] = functools.partial(radiation, inner=weights.get(name))
return weights