"""
DFD-CLASS — Derived cosmological parameters for Density Field Dynamics.

Values verified at file:line against the canonical v40 corpus (see corpus_extract.md).
HONEST LEDGER per parameter: DERIVED (theorem-grade) vs FITTED/ASSERTED.

LambdaCDM FITS 6: {Omega_b h^2, Omega_c h^2, H0, A_s, n_s, tau}.
DFD status:
  H0      DERIVED zero-param (alpha^28.5 / t_P)                       <- big win (resolves tension)
  n_s     DERIVED (slow-roll 1 - 28.7 alpha/2pi = 0.96667)            <- win
  A_s     forced-power (alpha^5) / FITTED-coefficient (32pi asserted) <- partial
  w,wa    DERIVED (-1,0) but by-construction (screen built from LCDM) <- tie
  Omega_chi h^2  misalignment-DERIVED mechanism, ANGLE-FITTED (theta_i)<- 1 free angle
  Omega_b h^2    BBN input (shared with standard BBN)                 <- 1 input
  tau     astrophysical reionization input                           <- 1 input
=> DFD free params ~ {theta_i, Omega_b h^2, tau} (+ A_s coeff) ~ 3-4, vs LCDM's 6,
   AND it derives H0 (the SH0ES-side value LCDM cannot reach without tension).
"""

import numpy as np

# ---- Fundamental DFD constant -------------------------------------------------
ALPHA = 1.0 / 137.035999084          # fine-structure constant (the one DFD input)
K_MAX = 60                           # forced by chi(CP^2, E)=60 (HRR/Spin^c index)

# ---- Standard physical constants ----------------------------------------------
C_KMS = 299792.458                   # speed of light [km/s]
T_CMB = 2.7255                       # CMB temperature [K]  (FIRAS, measured)
N_EFF = 3.044                        # effective neutrino species (SM value)
Z_STAR = 1089.80                     # recombination redshift (standard, hybrid BB)
Z_DRAG = 1059.94                     # baryon drag epoch (standard)

# ---- DFD DERIVED parameters ---------------------------------------------------
# H0: DERIVED zero-param, H0 = sqrt(alpha^57 c^5/(G hbar)) = alpha^28.5 / t_P
#     (section_G_derivation.tex:109,116). Headline 72.09; resolves the H0 tension.
H0 = 72.09                           # [km/s/Mpc]  DERIVED
h = H0 / 100.0

# Baryon density: the ONE early-universe input, fixed by BBN/D-abundance (standard).
OMEGA_B_H2 = 0.02237                 # BBN-anchored input (corpus shares the standard value)

# Cold dark matter = the chi field. m_chi = sqrt(158)*Mbar_P*alpha^13 ~ 5.1 eV (DERIVED),
# f_chi = Mbar_P*alpha^3 (DERIVED). Abundance via VACUUM MISALIGNMENT:
#     Omega_chi h^2 = 1.62 * theta_i^2   (appendix_chi_matter.tex:243)
# theta_i ~ 0.272 (ONE free misalignment angle) reproduces the observed 0.12.
# NOTE: the legacy Omega_chi/Omega_b = 16/3 is RETIRED/superseded (appendix_J.tex:418,
#   "a clue, not a derivation"). Use 0.12 from misalignment; flag it angle-fitted.
THETA_I = 0.272                      # free misalignment angle (1 fitted d.o.f.)
OMEGA_CHI_H2 = 1.62 * THETA_I ** 2   # = 0.1199 ~ 0.12  (misalignment-derived, angle-fitted)

# Radiation (photons) from T_CMB; neutrinos from N_eff. Standard, measured T_CMB.
OMEGA_G_H2 = 2.4728e-5 * (T_CMB / 2.7255) ** 4
OMEGA_NU_H2 = OMEGA_G_H2 * (7.0 / 8.0) * (4.0 / 11.0) ** (4.0 / 3.0) * N_EFF
OMEGA_R_H2 = OMEGA_G_H2 + OMEGA_NU_H2        # massless-nu approximation

# Density fractions today (flat universe; optical-DE / Lambda closes it, w=-1).
OMEGA_B = OMEGA_B_H2 / h ** 2
OMEGA_CHI = OMEGA_CHI_H2 / h ** 2
OMEGA_R = OMEGA_R_H2 / h ** 2
OMEGA_M = OMEGA_B + OMEGA_CHI                 # total clustering matter today
OMEGA_LAMBDA = 1.0 - OMEGA_M - OMEGA_R        # flat closure (optical-reinterpreted DE)

# Dark energy equation of state: DERIVED w=-1, wa=0 (thm:weff, appendix_AH2a:487-508),
# but by-construction (the optical screen Delta-psi is built from the LCDM distance ratio).
W0 = -1.0
WA = 0.0

# Primordial spectrum:
# n_s slow-roll on the Wheeler-DeWitt vacuum: n_s = 1 - 28.7*alpha/(2pi) (appendix_AH3a:42).
#   28.7*alpha/2pi = 2/Omega = 2/60 EXACTLY (corpus :44) => n_s = 0.96667.
#   The corpus MISPRINTS "0.964" (:42 + falsifier tuple); 1-28.7*alpha/2pi = 0.96667, not 0.964.
#   "1 - 2/k_max" is the numerical coincidence, not the mechanism. Value 0.96667 stands. DERIVED.
N_S = 1.0 - 28.7 * ALPHA / (2.0 * np.pi)      # = 0.96667
# A_s: alpha^5 exponent forced (squared alpha^3 Majorana rung); 32pi coeff ASSERTED
#   (H*-M_R normalization, not first-principles). forced-power / fitted-coefficient.
A_S = 32.0 * np.pi * ALPHA ** 5               # = 2.080e-9  (-0.82 sigma vs Planck)
K_PIVOT = 0.05                                # [1/Mpc]
R_TENSOR = 4.0 * ALPHA / np.pi                # r = 4 alpha/pi ~ 9.3e-3 (LiteBIRD target)

# Reionization optical depth (astrophysical input, not derived).
TAU_REION = 0.054

# MOND acceleration gate (perturbation sector, late-time LSS only):
#   a0(z) = 2 sqrt(alpha) c H(z); mu(x) = x/(1+x). Newtonian-screened at recombination.
A0_COEFF = 2.0 * np.sqrt(ALPHA)

# ---- LambdaCDM (Planck 2018 TT,TE,EE+lowE+lensing) reference for comparison ----
PLANCK = dict(
    H0=67.36, h=0.6736,
    omega_b_h2=0.02237, omega_c_h2=0.1200,
    Omega_m=0.3153, Omega_L=0.6847,
    sigma8=0.8111, n_s=0.9649, A_s=2.100e-9,
    r_drag=147.09, theta_star=1.04109e-2, age_Gyr=13.797,
)


def summary():
    rows = [
        ("alpha^-1", 1.0 / ALPHA, "input"),
        ("k_max", K_MAX, "forced topology"),
        ("H0 [km/s/Mpc]", H0, "DERIVED zero-param"),
        ("Omega_b h^2", OMEGA_B_H2, "BBN input"),
        ("Omega_chi h^2 (1.62 theta_i^2, theta_i=0.272)", OMEGA_CHI_H2, "DERIVED-mech/angle-fit"),
        ("Omega_r h^2", OMEGA_R_H2, "T_CMB+N_eff"),
        ("Omega_m (today)", OMEGA_M, "derived sum"),
        ("Omega_Lambda", OMEGA_LAMBDA, "flat closure, w=-1"),
        ("Omega_m h^2", OMEGA_M * h ** 2, "derived"),
        ("n_s = 1 - 28.7 alpha/2pi", N_S, "DERIVED"),
        ("A_s = 32 pi alpha^5", A_S, "forced-power/fit-coeff"),
        ("r = 4 alpha/pi", R_TENSOR, "DERIVED"),
        ("w0, wa", (W0, WA), "DERIVED by-construction"),
    ]
    print("=" * 66)
    print("DFD-CLASS derived parameters (corpus-verified)")
    print("=" * 66)
    for name, val, note in rows:
        if isinstance(val, tuple):
            print(f"  {name:44s} = {val}   [{note}]")
        else:
            print(f"  {name:44s} = {val:.6g}   [{note}]")
    print("-" * 66)
    print("LambdaCDM (Planck 2018) for contrast:")
    print(f"  Omega_c h^2 : Planck FIT {PLANCK['omega_c_h2']:.4g} | DFD misalign "
          f"{OMEGA_CHI_H2:.4g}  ({100*(OMEGA_CHI_H2/PLANCK['omega_c_h2']-1):+.2f}%)")
    print(f"  n_s         : Planck FIT {PLANCK['n_s']:.4g} | DFD derived {N_S:.5g}  "
          f"({100*(N_S/PLANCK['n_s']-1):+.2f}%)")
    print(f"  H0          : Planck FIT {PLANCK['H0']} | DFD derived {H0}  (SH0ES 73.04+/-1.04)")


if __name__ == "__main__":
    summary()
