"""
The thermal history: the :class:`Recombination` interface and reionization.
A recombination code subclasses :class:`Recombination`.
:class:`~discoeb.components.recfast.RECFAST` is the one the solve uses.
"""
from __future__ import annotations
import math
from jax.typing import ArrayLike
from ._component import Component, Parameter, State, derivative
from .gravity import H100_SQ
# Physical constants (PDG 2023 where noted).
const_mH = 1.67353284e-27 # H atom mass [kg], PDG 2023
const_mHe_mH = 3.97146570884 # Helium / Hydrogen mass ratio
const_c = 2.99792458e08 # Speed of light [m/s]
const_kB = 1.380649e-23 # Boltzmann's constant [J/K], PDG 2023
const_dens_fac = (
# 3 bigH^2 / (8 pi G m_H), bigH = 100 km/s/Mpc in 1/s, G = 6.67430e-11 m^3/kg/s^2
11.223810928601939
)
const_c_Mpc_s = 9.71561189e-15 # Mpc/s
THERMO_ERROR_WEIGHT = 1.0
"""Weight of the background states (``tau``, ``kappa`` and the recombination
code's own) in the error norm of the background block's steps, which keep
their error below ``rtol / weight``. The block takes its own steps ahead of
the perturbations, so the weight sets the thermal history's accuracy and
the number of evaluations of its rates and leaves the perturbations' steps
unchanged. ``P(k)`` does not change between 0.3 and 100. A weight of 0
leaves the block's steps uncontrolled."""
REIO_ERROR_WEIGHT = 1.0e4
"""Weight of ``kappa_reio`` (:class:`ReionizationDepth`) in the error norm.
Reionization's step is 0.5 wide in ``z``, ~150 Mpc of conformal time. At
the block's weight the steps across it resolve ``tau_reio`` (to 2e-4) but
not its derivative in ``z_re``, which the sensitivities carry without error
control: ``d tau_reio / d z_re`` comes out 6 percent high and ``dP/dz_re``
3 percent off. ``kappa_reio`` changes only there, so its weight refines
only those steps, and at 1e4 the derivative is converged. It costs a P(k)
batch with reionization 14 percent, and every launch of a model with the
state pays it. The state itself costs 4 to 7 percent of a solve, so a model
adds it as a component."""
AKTHOM_PER_OMEGAB_H0SQ = 2.3038921003709498e-9
"""``n_H sigma_T c / (H_0 units)``: ``kappa' = x_e akthom / a^2`` with
``akthom = this (1 - Y_He) Omega_b H_0^2``."""
AKTHOM_PER_OMEGAB = 1.0e4 * AKTHOM_PER_OMEGAB_H0SQ
DENS_FAC_PER_OMEGAB = const_dens_fac
"""The same and the hydrogen density's factor per unit ``omega_b = Omega_b
h^2``, the baryon density the kernel holds."""
def logistic(x):
"""``(1 + tanh x) / 2 = 1 / (1 + e^{-2x})``, a smooth step.
Goes through ``exp`` because Enzyme has no forward rule for libdevice's
``tanh``. The exponent is capped at 18, so the step reads ``1.5e-8``
instead of ``0`` where it is off. The cap keeps the exponential's
derivatives finite in single precision, which the sensitivity sweeps
multiply by tangents of order a thousand: the quotient rule applied twice
divides by ``(1 + e)^4``, ``1e52`` at a cap of 30, and at a cap of 80 the
second-order sweep overflows to infinity and turns the optical depth's
sensitivity into NaN.
"""
return 1.0 / (1.0 + math.exp(min(-2.0 * x, 18.0)))
# The interface
def _tau_initial(aH, rho_matter, a):
"""The conformal time at the start, deep in radiation domination.
With only radiation and matter, ``a'^2 = (R + M a) / 3`` integrates to
``tau = 2 sqrt(3) a / (sqrt(R + M a) + sqrt(R))``, which is ``1 / aH``
times ``2 sqrt(1 + e) / (sqrt(1 + e) + 1)`` with ``e = M a / R`` the
matter-to-radiation ratio. The model's own ``aH`` carries every
species, so the radiation is what it leaves beside the matter (massive
neutrinos included, relativistic then), and the ratio, 1e-4 at the
start, only corrects it.
"""
matter = H100_SQ * a * a * rho_matter
root = math.sqrt(1.0 + matter / (aH * aH - matter))
return 2.0 * root / (root + 1.0) / aH
[docs]
class Recombination(Component):
"""The thermal history every solve integrates once per cosmology: the interface a recombination code fills.
It holds the conformal time ``tau`` and the optical depth ``kappa`` as
driver states and provides what the perturbations and DISCO-CMB read:
``tau``, the ``opacity`` ``kappa' = x_e n_H sigma_T a`` and the baryon
sound speed ``cs2``. A subclass is one recombination code, such as
:class:`~discoeb.components.recfast.RECFAST`, and provides three
quantities from states of its own:
- ``xe_rec``, the free electrons per hydrogen nucleus from
recombination alone,
- ``Tmat``, the matter temperature in K,
- ``dTm_dz``, its rate in redshift, which ``cs2`` reads.
The class adds CAMB's tanh reionization (``z_re``) to ``xe_rec``. The
solve integrates in the scale factor, so ``a`` is the time itself and
``tau`` a state whose rate is one (per unit of itself: the solve
divides every rate by ``a'``,
:class:`~discoeb.components._component.Equations`). It starts from the
radiation era's closed form, and its value today is the conformal age.
``kappa`` integrates the opacity from ``a = 6e-4`` on, and DISCO-CMB
forms the visibility ``kappa'(tau) exp(-(kappa(tau_0) - kappa(tau)))``
from it. Nothing marks these states as the background: their rates read
no wave number, and :meth:`Equations.stages
<discoeb.components._component.Equations.stages>` finds that.
"""
tau = State(initial=_tau_initial)
kappa = State()
# The rates read Y_He, T_cmb and omega_b, which the species declare.
z_re: ArrayLike = Parameter(0.0)
"""The redshift half way through CAMB's reionization step (0: none)."""
def fHe(self, Y_He):
return Y_He / (const_mHe_mH * (1.0 - Y_He))
def xe_reio(self, a, xe_rec, fHe, z_re):
"""CAMB's reionization step, zero if ``z_re = 0``."""
inv_a = 1.0 / a
z = inv_a - 1.0
root = math.sqrt(1.0 + z)
root_re = math.sqrt(1.0 + z_re)
y = (1.0 + z) * root
y_re = (1.0 + z_re) * root_re
delta_y = 0.75 * root_re
step = logistic((y_re - y) / delta_y)
he3 = logistic((3.5 - z) / 0.5)
on = min(max(z_re, 0.0), 1.0)
return ((1.0 + fHe - xe_rec) * step + fHe * he3) * on
def xe(self, xe_rec, xe_reio):
return xe_rec + xe_reio
def akthom(self, Y_He, omega_b):
return AKTHOM_PER_OMEGAB * (1.0 - Y_He) * omega_b
def opacity(self, xe, akthom, a):
inv_a = 1.0 / a
inv_a2 = inv_a * inv_a
return max(xe * akthom * inv_a2, 1.0e-30)
def cs2(self, Tmat, xe_rec, Y_He, dTm_dz, a):
"""``c_s^2 = k_B T_m / (mu m_H c^2) (1 - (1/3) d ln T_m / d ln a)``."""
inv_a = 1.0 / a
inv_mu = 1.0 + (1.0 / const_mHe_mH - 1.0) * Y_He + (1.0 - Y_He) * xe_rec
dTm_dlna = -dTm_dz * inv_a
return max(
const_kB / (3.0 * const_mH * const_c**2) * inv_mu * (3.0 * Tmat - dTm_dlna),
0.0,
)
# What a code's rates read
def zred(self, a):
inv_a = 1.0 / a
return inv_a - 1.0
def Hz(self, aH, a):
"""``H`` in 1/s, from the model's conformal Hubble rate in 1/Mpc."""
inv_a = 1.0 / a
return aH * inv_a * const_c_Mpc_s
def Nnow(self, omega_b, Y_He):
"""The hydrogen number density today, in 1/m^3."""
return DENS_FAC_PER_OMEGAB * omega_b * (1.0 - Y_He)
def n_H(self, Nnow, a):
inv_a = 1.0 / a
return Nnow * inv_a**3
@derivative("tau")
def conformal_time(self):
return 1.0
@derivative("kappa")
def optical_depth(self, opacity, a):
# single precision: kappa starts at a = 6e-4 (z = 1666), where the optical depth to today is
# about a hundred. Integrated from the start it reaches 1e6, whose float32 spacing is 0.06,
# and the visibility needs kappa(tau_0) - kappa(tau) to 1e-4 through recombination. Before
# that exp(-(kappa(tau_0) - kappa)) is zero either way.
on = 1.0 if a > 6.0e-4 else 0.0
return opacity * on
[docs]
class ReionizationDepth(Component):
"""The optical depth of reionization as a state: ``kappa_reio``, whose value today is ``tau_reio``.
A model adds it to read ``tau_reio`` off a solve or to differentiate in
the reionization redshift, ``LCDM.add(ReionizationDepth, name=...)``.
``kappa_reio`` integrates the opacity of the electrons reionization frees,
a background state beside :class:`Recombination`'s. A launch weights
it (:data:`REIO_ERROR_WEIGHT`) so that the steps resolve the
reionization step's derivative in ``z_re``. Without the state the
derivative is inaccurate.
"""
kappa_reio = State()
@derivative("kappa_reio")
def reionization_depth(self, xe_reio, akthom, a):
"""The opacity of the electrons reionization frees, whose integral is ``tau_reio``."""
inv_a = 1.0 / a
inv_a2 = inv_a * inv_a
return xe_reio * akthom * inv_a2