"""Independent checks of the AC73 relic model.

Usage: python3 check_independent_limits.py [independent-checks.json]

None of these checks call the dimensionless Boltzmann equation of
relic_model.py for its evidence. They use:
  1. the thermally averaged cross-section from the Gondolo-Gelmini integral
     over the pair energy squared s, compared with the y-substitution in the model;
  2. a time-domain integration of dn/dt + 3Hn = -<sigma v>(n^2 - n_eq^2) in
     dimensional units, with H from the Friedmann equation and n_eq from the
     Maxwell-Boltzmann integral, solved with SciPy's Radau method;
  3. the freeze-in yield as a double integral of the collision rate over
     temperature and s;
  4. published reference values from Steigman, Dasgupta and Beacom (2012)
     and the Lee-Weinberg matching formulas (D'Eramo, Fernandez and Profumo 2017,
     eqs. C10 and C12) and their published cross-section for m = 100 GeV;
  5. the logarithmic law for relentless annihilation, built from eqs. C16 and
     C18 of the same paper, and printed-versus-used comparisons for eqs. (19)
     and (20) of that paper and a reading aid for its Fig. 1;
  6. three failure cases that show what goes wrong with common shortcuts.
"""
import json
import math
import sys
from pathlib import Path

import numpy as np
from scipy.integrate import dblquad, quad, solve_ivp
from scipy.optimize import brentq
from scipy.interpolate import CubicSpline
from scipy.special import kve

import relic_model as model

here = Path(__file__).resolve().parent
output_path = Path(sys.argv[1]) if len(sys.argv) > 1 else here / 'independent-checks.json'
M = 100.0
G, GSTAR, MPL = model.G_DARK, model.G_STAR, model.REDUCED_PLANCK_GEV
TO_GEV2 = 1.0 / model.GEV_MINUS2_IN_CM3_PER_S
checks = {}


def record(name, value, reference, tolerance, note):
    passed = abs(value / reference - 1) <= tolerance
    checks[name] = {'value': float(f'{value:.6g}'), 'reference': float(f'{reference:.6g}'), 'relative_tolerance': tolerance, 'passed': bool(passed), 'note': note}
    print(f"{'PASS' if passed else 'FAIL'} {name}: {value:.5g} vs {reference:.5g}")


# 1. <sigma v>/sigma0 from the integral over s, in dimensional form.
def shape_from_s_integral(x, m=M):
    t = m / x
    def integrand(step):
        u = 2 * m + step * t                        # u = sqrt(s)
        beta = math.sqrt(max(0.0, 1 - 4 * m * m / (u * u)))
        return beta * u * kve(1, u / t) * math.exp(-step) * 2 * u * t
    integral = quad(integrand, 0, np.inf, limit=400, epsabs=0, epsrel=1e-10)[0]
    return integral / (4 * m * m * t * kve(2, x) ** 2)


for x in (0.01, 1.0, 20.0, 300.0):
    record(f'shape_s_integral_vs_model_x{x:g}', model.cross_section_shape(x), shape_from_s_integral(x), 2e-4,
           'Model F(x) from the y-substitution against the Gondolo-Gelmini integral over s.')
record('shape_low_x_limit', shape_from_s_integral(1e-3), (1e-3) ** 2 / 4, 2e-3, 'F -> x^2/4 when the particles are relativistic.')
record('shape_high_x_limit', shape_from_s_integral(5e3), 1.0, 1e-3, 'F -> 1 when the particles are slow.')

# 2. Time-domain integration in GeV units.
log_x_table = np.linspace(math.log(1e-3), math.log(2e5), 260)
shape_table = CubicSpline(log_x_table, np.log([shape_from_s_integral(math.exp(v)) for v in log_x_table]))


def friedmann_hubble(t, n=0, t_r=None):
    rho_radiation = math.pi ** 2 / 30 * GSTAR * t ** 4
    rho_extra = 0.0 if n == 0 else rho_radiation * (t / t_r) ** n
    return math.sqrt((rho_radiation + rho_extra) / 3) / MPL


def equilibrium_density(t):
    x = M / t
    return G * M * M * t * kve(2, x) * math.exp(-x) / (2 * math.pi ** 2)


def entropy_density(t):
    return 2 * math.pi ** 2 / 45 * GSTAR * t ** 3


def time_domain_yield(cross_section, n=0, t_r=None, empty=False, x_start=0.01, x_end=None, constant_cross_section=False):
    """Integrate q = n/T^3 in e-folds of the scale factor; T = T_start/a for constant g*s."""
    sigma0 = cross_section * TO_GEV2
    x_end = x_end or (1e3 if n == 0 else max(1e3, 50 * M / t_r))
    t_start = M / x_start
    def rhs(efolds, state):
        t = t_start * math.exp(-efolds)
        q, q_eq = math.exp(state[0]), equilibrium_density(t) / t ** 3
        sigma_v = sigma0 if constant_cross_section else sigma0 * math.exp(shape_table(math.log(M / t)))
        return [-(sigma_v * t ** 3 / friedmann_hubble(t, n, t_r)) * (q - q_eq * q_eq / q)]
    q0 = equilibrium_density(t_start) / t_start ** 3 * (1e-20 if empty else 1.0)
    span = (0.0, math.log(x_end / x_start))
    solution = solve_ivp(rhs, span, [math.log(q0)], method='Radau', rtol=1e-9, atol=1e-12, first_step=1e-6, max_step=0.05, dense_output=True)
    def yield_at(x):
        t = M / x
        return math.exp(solution.sol(math.log(x / x_start))[0]) * t ** 3 / entropy_density(t)
    return yield_at


presets = json.loads((here / 'inputs.json').read_text())['presets']
benchmarks = json.loads((here / 'relic-benchmarks.json').read_text())['presets']
for name in ('wimpFreezeOut', 'strongerAnnihilation', 'relentlessKination'):
    preset = presets[name]
    y_of_x = time_domain_yield(preset['cross_section_cm3_s'], preset['n'], preset.get('equality_temperature_gev'))
    x_end = 1e3 if preset['n'] == 0 else max(1e3, 50 * M / preset['equality_temperature_gev'])
    record(f'time_domain_{name}', y_of_x(x_end), benchmarks[name]['final_yield'], 5e-3,
           'Radau in time with Friedmann H and dimensional n_eq, against the BDF2 solution in Y.')
empty_strong = time_domain_yield(presets['wimpFreezeOut']['cross_section_cm3_s'], empty=True)(1e3)
record('memory_loss_empty_start', empty_strong, benchmarks['wimpFreezeOut']['final_yield'], 5e-3,
       'Starting with 1e-20 of the equilibrium number gives the same relic once reactions are fast.')

# 3. Freeze-in from the collision rate as a double integral over T and s.
sigma_freeze_in = presets['freezeIn']['cross_section_cm3_s'] * TO_GEV2
def collision_integrand(step, log_t):
    t = math.exp(log_t)
    u = 2 * M + step * t
    beta = math.sqrt(max(0.0, 1 - 4 * M * M / (u * u)))
    # rate = g^2 T/(32 pi^4) * int 2 m^2 sigma0 beta sqrt(s) K1(sqrt(s)/T) ds
    rate = G * G * t / (32 * math.pi ** 4) * 2 * M * M * sigma_freeze_in * beta * u * kve(1, u / t) * math.exp(-u / t) * 2 * u * t
    return rate / (entropy_density(t) * friedmann_hubble(t)) # dY/dlnT
freeze_in_double = dblquad(collision_integrand, math.log(M / 400), math.log(M / 1e-3), 0, np.inf, epsrel=1e-8)[0]
closed_form = model.freeze_in_linear_yield(presets['freezeIn']['cross_section_cm3_s'])
record('freeze_in_double_integral_vs_closed_form', freeze_in_double, closed_form, 2e-3, 'Collision-rate double integral against (3 pi^2/32) a^2 A sigma0.')
record('freeze_in_solver_vs_double_integral', benchmarks['freezeIn']['final_yield'], freeze_in_double, 3e-3, 'BDF2 empty-start solution against the double integral.')

# 4. Lee-Weinberg matching with a constant cross-section.
sigma0 = presets['wimpFreezeOut']['cross_section_cm3_s'] * TO_GEV2
a_const = model.entropy_to_hubble_at_mass(M)
c = math.sqrt(2) - 1
right = c * (c + 2) / (c + 1) * 3 * math.sqrt(5) * G / (2 * math.pi ** 2.5 * math.sqrt(GSTAR)) * M * MPL * sigma0
x_f = 20.0
for _ in range(60):
    x_f = math.log(right) - 0.5 * math.log(x_f)
lee_weinberg = x_f / (a_const * sigma0)   # eq. C10 with J(inf) = sigma_s / x_f
constant_solution = model.solve(presets['wimpFreezeOut']['cross_section_cm3_s'], shape=lambda xs: np.ones_like(xs))
record('lee_weinberg_c10_c12_constant_sigma', constant_solution['Y'][-1], lee_weinberg, 0.08,
       f'D\'Eramo et al. eqs. C10 and C12 with c = sqrt(2) - 1 give x_f = {x_f:.3g}. The matching formula is an estimate.')
record('published_cross_section_100_gev', presets['wimpFreezeOut']['cross_section_cm3_s'] * TO_GEV2, 1.7e-9, 0.12,
       'D\'Eramo et al. Fig. 1: 1.7e-9 GeV^-2 gives the observed density for m = 100 GeV with the full g*(T). This model uses constant g* = 86.25 and the contact shape F.')

# Steigman, Dasgupta and Beacom (2012): their eq. (12) for Gamma_eq/H with the
# leading slow-particle density, and their fit eq. (26) for <sigma v> at given Omega h^2.
x_check, sigma_cm3 = 25.0, presets['wimpFreezeOut']['cross_section_cm3_s']
t_check = M / x_check
leading_density = G * (M * t_check / (2 * math.pi)) ** 1.5 * math.exp(-x_check)
record('steigman_eq12_rate_over_hubble', leading_density * sigma_cm3 * TO_GEV2 / friedmann_hubble(t_check),
       8.00e34 * M * sigma_cm3 * math.sqrt(x_check) * math.exp(-x_check) / math.sqrt(GSTAR), 2e-3,
       'Friedmann H and the leading slow-particle density against the published coefficient 8.00e34 (m in GeV, <sigma v> in cm^3/s).')
steigman_cross_section = (2.1 - 0.3 * math.log10(0.120)) * 1e-27 / 0.120
record('steigman_eq26_cross_section_for_observed_density', sigma_cm3, steigman_cross_section, 0.12,
       'Fit to the full calculation with g*(T) for m above 10 GeV; this model holds g* = 86.25 and uses the contact shape F.')

# 5. Relentless logarithmic law, eqs. C16 + C18 for n = 2 and x_f << x << x_r.
kination = presets['relentlessKination']
x_r = M / kination['equality_temperature_gev']
y_kination = time_domain_yield(kination['cross_section_cm3_s'], 2, kination['equality_temperature_gev'])
x1, x2 = 200.0, x_r / 10
mean_shape = quad(lambda v: math.exp(shape_table(v)), math.log(x1), math.log(x2))[0]
law = 1 / y_kination(x1) + a_const * sigma0 / x_r * mean_shape
record('relentless_log_law_c16_c18', 1 / y_kination(x2), law, 0.02, 'Inverse yield grows by (A sigma0/x_r) times the integral of F dln x between x = 200 and x_r/10.')
printed_eq20 = x_r / (M * MPL * sigma0) / (2 / 19.5 + math.log(x2 / 19.5))
used_eq20 = x_r / (a_const * sigma0) / (2 / 19.5 + math.log(x2 / 19.5))
checks['deramo_eq20_printed_vs_used'] = {
    'printed_prediction': float(f'{printed_eq20:.4g}'), 'with_factor_A_over_m_Mpl': float(f'{used_eq20:.4g}'),
    'time_domain_value': float(f'{y_kination(x2):.4g}'), 'A_over_m_Mpl': float(f'{a_const / (M * MPL):.4g}'),
    'note': 'Eq. (20) as printed omits A/(m M_Pl); restoring it from eqs. C3, C16 and C18 brings the estimate within a few percent of the numerical value, while the printed form is about 12 times too large.'}
print('eq. (20) printed / used / numerical:', checks['deramo_eq20_printed_vs_used'])

# Eq. (19) relates the standard and modified freeze-out temperatures. Measure
# both from time-domain solutions with a constant cross-section, defining
# freeze-out as Y - Y_eq = c Y_eq with c = sqrt(2) - 1 (the paper's App. C
# condition), and test the printed factor (T_f/T_r)^(n/2) against (T_r/T_f)^(n/2),
# which is what eqs. C12 and C22 imply.
def matched_freeze_out_x(n, t_r=None):
    y_of_x = time_domain_yield(presets['wimpFreezeOut']['cross_section_cm3_s'], n, t_r, constant_cross_section=True)
    lag = lambda x: y_of_x(x) * entropy_density(M / x) / equilibrium_density(M / x) - 1 - c
    return brentq(lag, 3.0, 60.0, xtol=1e-6)
x_standard = matched_freeze_out_x(0)
eq19 = {}
for n_index in (1, 2, 4):
    x_modified = matched_freeze_out_x(n_index, 0.02)
    t_standard, t_modified = M / x_standard, M / x_modified
    left = math.sqrt(t_standard) * math.exp(-x_standard)
    core = math.sqrt(t_modified) * math.exp(-x_modified)
    eq19[f'n{n_index}'] = {'x_f_standard': float(f'{x_standard:.4g}'), 'x_f_modified': float(f'{x_modified:.4g}'),
                           'left_over_printed_right': float(f'{left / (core * (t_modified / 0.02) ** (n_index / 2)):.4g}'),
                           'left_over_consistent_right': float(f'{left / (core * (0.02 / t_modified) ** (n_index / 2)):.4g}')}
checks['deramo_eq19_printed_vs_used'] = {**eq19, 'equality_temperature_gev': 0.02,
    'note': 'Eq. (19) as printed has (T_f/T_r)^(n/2) on the right. Eqs. C12 and C22 imply (T_r/T_f)^(n/2). With the time-domain freeze-out points the printed form misses by orders of magnitude, while the inverted factor holds to within the accuracy of the matching estimate.'}
print('eq. (19) printed vs consistent:', json.dumps(checks['deramo_eq19_printed_vs_used']))

# Fig. 1 of the same paper, read off the arXiv v1 PDF by pixel position of each
# curve at x = 1e5 (log axis calibrated on the frame from 1e-2 to 1e-13). These
# are digitized estimates, not published table values. The paper keeps the full
# g*(T) in eq. (6); with g*(T_r = 20 MeV) = 10.75 from their eq. (11) and
# g* = 86.25 near freeze-out, the extra fluid's share of H^2 is larger by
# [g*(T_r)/g*(T)] [g*s(T)/g*s(T_r)]^((4+n)/3). This comparison uses the
# model's own solver, so it is a reading aid, not an independent check.
fig1_digitized = {0: 4.5e-12, 1: 6.2e-11, 2: 3.8e-10, 4: 1.5e-9}
fig1_sigma = 1.7e-9 / TO_GEV2
fig1 = {}
standard_expansion_factor = model.expansion_factor
for n_index, digitized in fig1_digitized.items():
    plain = model.solve(fig1_sigma, M, n_index, 0.02 if n_index else None, shape=lambda xs: np.ones_like(xs))['Y'][-1]
    boost = (GSTAR / 10.75) ** ((4 + n_index) / 3 - 1)
    model.expansion_factor = lambda x, n, x_r, boost=boost: np.ones_like(np.asarray(x, dtype=float)) if n == 0 else np.sqrt(1 + boost * (x_r / np.asarray(x, dtype=float)) ** n)
    boosted = model.solve(fig1_sigma, M, n_index, 0.02 if n_index else None, shape=lambda xs: np.ones_like(xs))['Y'][-1]
    model.expansion_factor = standard_expansion_factor
    fig1[f'n{n_index}'] = {'digitized_figure': digitized, 'model_constant_g_star': float(f'{plain:.4g}'),
                           'model_with_g_star_step_in_extra_fluid': float(f'{boosted:.4g}'), 'boost_to_extra_fluid_share': float(f'{boost:.4g}')}
checks['deramo_fig1_digitized_vs_model'] = {**fig1, 'cross_section_gev_minus2': 1.7e-9, 'equality_temperature_gev': 0.02,
    'note': 'Constant cross-section as in the figure. Holding g* fixed leaves the faster-expansion curves 2 to 3 times below the figure; restoring the g* step in the extra fluid term of eq. (6) brings them within about 15 percent of the digitized values.'}
print('Fig. 1 digitized vs model:', json.dumps(checks['deramo_fig1_digitized_vs_model']))

# 6. Failure cases.
failures = {}
def explicit_euler_wimp():
    xs = model.grid(0, 0)
    k = model.entropy_to_hubble_at_mass(M) * sigma0 * np.array([model.cross_section_shape(x) for x in xs]) / xs
    yeq = model.equilibrium_yield(xs)
    h = math.log(xs[1] / xs[0])
    y = yeq[0]
    for i in range(1, len(xs)):
        y = y - h * k[i - 1] * (y * y - yeq[i - 1] ** 2)
        if not math.isfinite(y) or abs(y) > 1e10:
            return {'diverged_at_x': float(f'{xs[i]:.4g}'), 'step_times_rate': float(f'{h * k[i - 1] * yeq[i - 1]:.3g}')}
    return {'final_yield': y}
failures['explicit_euler_wimp'] = {**explicit_euler_wimp(), 'note': 'Forward Euler with the same 250 steps per e-fold: the step is far longer than the equilibration time, so it blows up.'}

original_equilibrium = model.equilibrium_yield
model.equilibrium_yield = lambda x: model.EQUILIBRIUM_PREFACTOR * math.sqrt(math.pi / 2) * np.asarray(x) ** 1.5 * np.exp(-np.asarray(x))
non_relativistic = model.solve(presets['freezeIn']['cross_section_cm3_s'], start='empty')['Y'][-1]
model.equilibrium_yield = original_equilibrium
failures['non_relativistic_equilibrium_freeze_in'] = {'yield_ratio_to_correct': float(f'{non_relativistic / benchmarks["freezeIn"]["final_yield"]:.4g}'),
    'note': 'Using the slow-particle formula x^(3/2) e^-x for Y_eq at x < 1 misstates freeze-in, which happens near x of 1 to 3.'}

yields = {}
for x_start in (1e-3, 1e-2):
    model.X_START = x_start
    yields[x_start] = model.solve(presets['freezeIn']['cross_section_cm3_s'], start='empty', shape=lambda xs: np.ones_like(xs))['Y'][-1]
model.X_START = 1e-3
failures['constant_sigma_v_freeze_in_start_dependence'] = {'yield_start_1e-3': float(f'{yields[1e-3]:.4g}'), 'yield_start_1e-2': float(f'{yields[1e-2]:.4g}'),
    'ratio': float(f'{yields[1e-3] / yields[1e-2]:.4g}'), 'note': 'With a constant <sigma v>, freeze-in depends on the starting temperature (about 1/x_start): the answer is set by the unknown hot past.'}
print('failures:', json.dumps(failures))

output_path.write_text(json.dumps({'kind': 'independent checks of an offline teaching model', 'checks': checks, 'failure_cases': failures}, indent=2) + '\n')
graded = [item for item in checks.values() if 'passed' in item]
print(f"{sum(item['passed'] for item in graded)}/{len(graded)} graded checks passed, {len(checks) - len(graded)} printed-versus-used comparisons and reading aids; wrote {output_path.name}")
