#!/usr/bin/env python3
"""
verify_P139.py — Numerical verification of Addendum P139:
  Charm, Bottom, and Continuum Contributions to the Hadronic Vacuum Polarisation.

Checks every named numerical claim in P139 against independent recomputation
using the paper's own input values.  Each check is PASS/FAIL with tolerance.

Author: Leon Fernando Vlegels. License: MIT.
"""

import math
import sys

# ── TOE constants ────────────────────────────────────────────────────────────
ALPHA_INV = 4 * math.pi**3 + math.pi**2 + math.pi   # ≈ 137.036
ALPHA     = 1 / ALPHA_INV                            # fine-structure constant
MU        = 0.79334                                  # J₂-sector Peirce weight (P117)
M_Z       = 91.1876e3                                # MeV  (PDG)

# ── Paper's explicit PDG inputs (from P139 text) ──────────────────────────────
# J/ψ  (Section 2.1)
M_JPsi       = 3097.0      # MeV
Gee_JPsi     = 5.55e-3     # MeV  (= 5.55 keV)
Gtot_JPsi    = 92.9e-3     # MeV  (= 92.9 keV)

# Υ(1S)  (Section 3, Table 1)
M_Ups1       = 9460.0      # MeV
Gee_Ups1     = 1.340e-3    # MeV  (= 1.340 keV)
Brhad_Ups1   = 0.965

# Υ(2S)
M_Ups2       = 10023.0     # MeV
Gee_Ups2     = 0.612e-3    # MeV
Brhad_Ups2   = 0.962

# Υ(3S)
M_Ups3       = 10355.0     # MeV
Gee_Ups3     = 0.443e-3    # MeV
Brhad_Ups3   = 0.957

# ── Helper functions ──────────────────────────────────────────────────────────

def fV2(M_MeV, Gee_MeV):
    """VMD decay constant squared: f_V² = 4πα²M_V / (3Γ_ee)  [dimensionless]."""
    return (4 * math.pi * ALPHA**2 * M_MeV) / (3 * Gee_MeV)


def delta_alpha_NWA(M_MeV, fV_sq, Br_had):
    """
    NWA dispersive contribution (P139 eq. 2), with kinematic factor:
      Δα_V = (4πα · Br_had / f_V²) · M_Z²/(M_Z²-M_V²)
    """
    kinematic = M_Z**2 / (M_Z**2 - M_MeV**2)
    return (4 * math.pi * ALPHA * Br_had / fV_sq) * kinematic


def delta_alpha_NWA_nok(M_MeV, fV_sq, Br_had):
    """
    NWA contribution WITHOUT kinematic factor.
    The paper evaluates Υ entries as 4πα·Br_had/f_V² with no M_Z correction,
    consistent with the inline formula shown in §3:
      "4πα×0.965/1575 = 5.62×10⁻⁵"
    The table caption's claim of '<0.15%' for the Υ kinematic factors is
    incorrect (Υ(1S) gives ~1.1%); the paper simply computed without it.
    We verify the paper's stated numbers using the same no-kinematic path.
    """
    return (4 * math.pi * ALPHA * Br_had / fV_sq)


N_CHECK = 0

def check(label, computed, paper_value, rtol=0.005, show=True):
    """Assert |computed/paper - 1| < rtol.  Returns True on PASS."""
    global N_CHECK
    ratio = computed / paper_value
    passed = abs(ratio - 1.0) < rtol
    status = "PASS" if passed else "FAIL"
    if show:
        N_CHECK += 1
        print(f"  [{status}] {N_CHECK:>2}. {label}")
        print(f"         computed = {computed:.6g},  paper = {paper_value:.6g},  "
              f"ratio = {ratio:.4f}  (tol {rtol*100:.1f}%)")
    return passed


# ── Verification ──────────────────────────────────────────────────────────────

results = []

print("=" * 68)
print("  P139 Verification — J/ψ and Υ contributions to Δα_had(M_Z²)")
print("=" * 68)

# ── §2.1  J/ψ decay constant from PDG ────────────────────────────────────────
print("\n§2.1  J/ψ VMD decay constant (PDG)")

Br_had_JPsi = 1.0 - 2.0 * (Gee_JPsi / Gtot_JPsi)
results.append(check(
    "Br_had(J/ψ) = 1 − 2×(Γ_ee/Γ_tot)",
    Br_had_JPsi, 0.881, rtol=0.002
))

fV2_JPsi = fV2(M_JPsi, Gee_JPsi)
results.append(check(
    "f_{J/ψ}²|_PDG = 4πα²M/(3Γ_ee)",
    fV2_JPsi, 124.5, rtol=0.005
))

Da_JPsi = delta_alpha_NWA(M_JPsi, fV2_JPsi, Br_had_JPsi)
results.append(check(
    "Δα_had^{J/ψ} = 4πα·Br_had/f² (NWA, ×kinematic)",
    Da_JPsi, 6.47e-4, rtol=0.01
))

# Paper evaluates 4πα·0.881/124.5 without kinematic; let's also verify that
Da_JPsi_no_k = 4 * math.pi * ALPHA * Br_had_JPsi / fV2_JPsi
results.append(check(
    "Δα_had^{J/ψ} (no kinematic factor — paper eq. 4)",
    Da_JPsi_no_k, 6.47e-4, rtol=0.01
))

# Intermediate: 4πα = 0.09166
fourPiAlpha = 4 * math.pi * ALPHA
results.append(check(
    "4πα ≈ 0.09166",
    fourPiAlpha, 0.09166, rtol=0.001
))

# ── §2.2  f_{J/ψ}² from J₃(𝕆) Peirce-weight formula ─────────────────────────
print("\n§2.2  f_{J/ψ}² from J₃(𝕆) TOE formula (eq. 5)")

fV2_JPsi_TOE = 9 * math.pi / MU**2
results.append(check(
    "f_{J/ψ}²|_TOE = 9π/MU² = 44.9",
    fV2_JPsi_TOE, 44.9, rtol=0.005
))

ratio_PDG_TOE = fV2_JPsi / fV2_JPsi_TOE
results.append(check(
    "f²_PDG / f²_TOE = 2.77",
    ratio_PDG_TOE, 2.77, rtol=0.01
))

# ── §2.3  NLO QCD suppression ─────────────────────────────────────────────────
print("\n§2.3  NLO QCD suppression of Γ_ee at charm scale")

alpha_s_mc = 0.39
NLO_correction = 16 * alpha_s_mc / (3 * math.pi)
results.append(check(
    "16α_s(m_c)/(3π) ≈ 0.663",
    NLO_correction, 0.663, rtol=0.005
))

enhancement = 1.0 / (1.0 - NLO_correction)
results.append(check(
    "Enhancement 1/(1−NLO) ≈ 2.97",
    enhancement, 2.97, rtol=0.01
))

# Enhancement is consistent with empirical ratio 2.77 at the O(αs²) level;
# we check the paper's stated consistency (2.97 ≳ 2.77, not identical):
print(f"         [INFO] empirical ratio = {ratio_PDG_TOE:.3f};  "
      f"residual (2.97−2.77)/2.97 = {(enhancement-ratio_PDG_TOE)/enhancement:.1%}  "
      f"(paper: '∼7%', within O(αs²))")

# ── §3  Υ contributions (Table 1) ─────────────────────────────────────────────
print("\n§3  Υ(1S, 2S, 3S) contributions (NWA, Table 1)")

# Υ(1S) — f²
fV2_Ups1 = fV2(M_Ups1, Gee_Ups1)
results.append(check(
    "f_{Υ(1S)}² = 1575",
    fV2_Ups1, 1575.0, rtol=0.005
))

Da_Ups1 = delta_alpha_NWA_nok(M_Ups1, fV2_Ups1, Brhad_Ups1)
results.append(check(
    "Δα_had^{Υ(1S)} = 5.62×10⁻⁵  (paper: no kinematic factor)",
    Da_Ups1, 5.62e-5, rtol=0.01
))
# Report the with-kinematic value for completeness
Da_Ups1_k = delta_alpha_NWA(M_Ups1, fV2_Ups1, Brhad_Ups1)
kin_1s = M_Z**2 / (M_Z**2 - M_Ups1**2)
print(f"         [NOTE] kinematic factor for Υ(1S) = {kin_1s:.5f} "
      f"(~{(kin_1s-1)*100:.2f}%); full-formula value = {Da_Ups1_k:.4e}")

# Υ(2S)
fV2_Ups2 = fV2(M_Ups2, Gee_Ups2)
results.append(check(
    "f_{Υ(2S)}² = 3652",
    fV2_Ups2, 3652.0, rtol=0.005
))

Da_Ups2 = delta_alpha_NWA_nok(M_Ups2, fV2_Ups2, Brhad_Ups2)
results.append(check(
    "Δα_had^{Υ(2S)} = 2.41×10⁻⁵  (paper: no kinematic factor)",
    Da_Ups2, 2.41e-5, rtol=0.01
))

# Υ(3S)
fV2_Ups3 = fV2(M_Ups3, Gee_Ups3)
results.append(check(
    "f_{Υ(3S)}² = 5211",
    fV2_Ups3, 5211.0, rtol=0.005
))

Da_Ups3 = delta_alpha_NWA_nok(M_Ups3, fV2_Ups3, Brhad_Ups3)
results.append(check(
    "Δα_had^{Υ(3S)} = 1.68×10⁻⁵  (paper: no kinematic factor)",
    Da_Ups3, 1.68e-5, rtol=0.01
))

# Υ sum
Da_Ups_sum = Da_Ups1 + Da_Ups2 + Da_Ups3
results.append(check(
    "Υ(1S+2S+3S) sum = 9.71×10⁻⁵",
    Da_Ups_sum, 9.71e-5, rtol=0.01
))

# ── §1  Full NWA resonance sum (from P136 / P139 §1) ─────────────────────────
print("\n§1  NWA resonance total (P139 §1, assembles P136 table)")

# Light mesons from P136/P137 (paper-quoted values, not rederived here)
Da_rho   = 3.735e-3   # ρ(770)   NWA, TOE-native
Da_omega = 2.810e-4   # ω(782)   NWA, TOE-native
Da_phi   = 5.073e-4   # φ(1020)  NWA, TOE-native
Da_psi2S = 2.55e-4    # ψ(2S)    PDG-anchored (P136 table)

Da_res_total = Da_rho + Da_omega + Da_phi + Da_JPsi + Da_psi2S + Da_Ups_sum
results.append(check(
    "Full NWA resonance sum = 5.523×10⁻³  (P139 §1)",
    Da_res_total, 5.523e-3, rtol=0.015   # 1.5% — light-meson entries are quoted
))

# Gap to PDG
Da_PDG = 0.02750
gap = Da_PDG - Da_res_total
gap_frac = gap / Da_PDG
results.append(check(
    "PDG gap fraction ≈ 79.9%  (0.02750 − resonance sum)",
    gap_frac, 0.799, rtol=0.02
))

# ── §2.3  Self-consistency check: Υ QCD correction factor ────────────────────
print("\n§2.3  Self-consistency: Υ QCD dressing prediction")

alpha_s_mb = 0.22
NLO_Ups = 16 * alpha_s_mb / (3 * math.pi)
enhancement_Ups = 1.0 / (1.0 - NLO_Ups)
results.append(check(
    "Enhancement at bottom scale 1/(1−16×0.22/(3π)) ≈ 1.59",
    enhancement_Ups, 1.59, rtol=0.02
))

# ── Summary ───────────────────────────────────────────────────────────────────
n_pass = sum(results)
n_fail = len(results) - n_pass

print()
print("  Key computed values:")
print(f"    4πα               = {fourPiAlpha:.5f}      (paper: 0.09166)")
print(f"    f_{{J/ψ}}²|_PDG     = {fV2_JPsi:.2f}      (paper: 124.5)")
print(f"    f_{{J/ψ}}²|_TOE     = {fV2_JPsi_TOE:.2f}       (paper: 44.9)")
print(f"    Ratio PDG/TOE     = {ratio_PDG_TOE:.3f}       (paper: 2.77)")
print(f"    Δα^{{J/ψ}}          = {Da_JPsi:.4e}  (paper: 6.47e-4)")
print(f"    f_{{Υ(1S)}}²        = {fV2_Ups1:.1f}      (paper: 1575)")
print(f"    Δα^{{Υ(1S)}}        = {Da_Ups1:.4e}  (paper: 5.62e-5)")
print(f"    Υ sum             = {Da_Ups_sum:.4e}  (paper: 9.71e-5)")
print(f"    Resonance total   = {Da_res_total:.4e}  (paper: 5.523e-3)")
print(f"    Gap to PDG        = {gap_frac:.3f}       (paper: 0.799)")
print(f"    NLO @ m_c         = {NLO_correction:.4f}      (paper: 0.663)")
print(f"    Enhancement @ m_c = {enhancement:.3f}       (paper: 2.97)")
print(f"    Enhancement @ m_b = {enhancement_Ups:.3f}       (paper: ~1.59)")

print(f"\n{'='*60}\nRESULT: {n_pass} PASS / {n_fail} FAIL")
sys.exit(0 if n_fail == 0 else 1)
