#!/usr/bin/env python3
"""
SCOPE NOTE, 2026-10-11 (comp 202).  Where this file quotes lambda_SM(M_*) ~ 0.0927
(Buttazzo et al. 2013), the 0.8 percent agreement, the one-sigma Higgs match or comp 176's
0.4 sigma, that is the anchor of its day and is kept as found.  comp 202 computes the number
at three loops on current inputs: 0.0932 +- 0.0008, 1.6 sigma from e^-1/4 on the 2024 Review,
which the book stated from v32.137, and 0.0930 +- 0.0008, 1.4 sigma on the 2026 Review, which
it states from v32.153.  Nothing here is re-run; the substrate side is unaffected.

PROVENANCE: PROOF

Computation 201 -- the gradient normalisation and the coherence length are one unknown:
under the book's canonical reading, d_0 = sqrt(2) hbar c / m_h, attometres, and the
nanometre expectation of the Casimir chapter does not follow from the equations
===================================================================================
WHAT THIS DOES.  The last structural item is the normalisation of P3's gradient term,
"assumed, not derived": a value for the emergent kinetic coefficient, which comp 177
wrote as K and comp 197 showed hangs on one free node, the coherence length d_0.  The
book defines that length in Chapter 15, twice (eq:coh_len, eq:xi_psi), as the
Landau-Ginzburg coherence length of the order parameter,

        d_0  =  xi_psi  =  sqrt( c_LG / (2 |a_LG|) ),

with |a_LG| = m_h^2 / 4 fixed by the Higgs identification (the sentence after
eq:ch-exact's cutoff discussion: "With the Higgs identification r_0 = v and
|a_LG| = m_h^2/4, the coherence length d_0 is fixed by the Landau-Ginzburg vacuum
structure ... its numerical value is not determined by the three postulates alone").
The one thing in that formula the postulates do not fix is c_LG, the gradient
coefficient of the four-dimensional functional eq:lgfull.  That coefficient IS the open
item.  So the item and the coherence length are the same unknown, and the two
sentences, eighty pages apart, were never put together.

THE READING THE BOOK ALREADY MAKES.  sec:ch-normalisation settles the normalisation
question in favour of reading A, "lambda_PST = 1/4 is the canonical quartic", which is
c = 1 for the kinetic coefficient (comps 173, 174, 176; reading B excluded at 121 sigma
and 9.5 sigma), and every number downstream of M_* = 1.285 TeV and Z^2 = e^-1 is a
reading-A number.  Reading A is canonical normalisation of the emergent field, i.e.
c_LG at its canonical value.  Put that into eq:xi_psi:

        d_0 m_h  =  sqrt(2 c_LG)  =  O(1).

RESULT 1 -- d_0 IS ATTOMETRES IN EVERY CONVENTION.  The book writes the sombrero in
three frames: P3's substrate variables (b = 1/4), the single neutral component
(r_0 = v, b = 1/4, the normalisation of Chapter 7) and the doublet (r_0 = v / sqrt 2,
b <-> lambda, eq:psi-Higgs-map).  In each, the radial curvature at the vacuum is
V''(r_0) = 4 eps independently of b, and the Ginzburg-Landau length of eq:xi_psi is
sqrt(c_LG / (2|a|)) with |a| = eps.  Part 1 carries this symbolically.  The frame fixes
only an order-one factor: d_0 m_h = sqrt 2 in the neutral frame (|a| = m_h^2/4), 1 in the
doublet frame (|a| = mu^2 = m_h^2/2), and the radial correlation length proper is a
further factor sqrt 2 below the GL length.  So, at c_LG = 1,

        d_0  =  1.6 to 2.2 attometres,     d_0 / l_*  =  M_* / m_h  x  (1 or sqrt 2)  =  10 to 15,

and the ratio to the grain is the book's own M_* / m_h = 4 pi sqrt(2/3) = 10.26
(eq:mstar-pred), not the three to six orders of magnitude the BCS analogy of
sec:grain-vs-coherence suggested: the analogue of the gap is m_h and the analogue of the
bandwidth is M_*, and the book itself derives their ratio.

RESULT 2 -- THE NANOMETRE EXPECTATION CONFLATED TWO PARAMETERS.  sec:grain-vs-coherence
argues that near a second-order transition xi diverges, a_LG ~ (T/tau - 1) -> 0, so a
substrate barely above threshold has d_0 >> l_*, "in the nanometre range".  But |a_LG|
is pinned at m_h^2/4 by the Higgs mass; how close to threshold the substrate sits is
not free once m_h is measured.  At fixed |a_LG| the only way to a large xi is a large
c_LG, which is a non-canonical normalisation.  A coherence length of 2.3 nm needs
c_LG = (d_0 m_h)^2 / 2 = 1.1 x 10^18, and comp 176 shows M_* linear in the normalisation
parameter: the seagull extraction of M_* would then be wrong by nine orders of
magnitude, which the 1.0 sigma Higgs match of comp 123 and the 121 sigma exclusion of
even c^2 = e rule out.  The nanometre range and the book's quantitative core cannot
both be had.  This file does not use the 0.4 sigma agreement to fix c (backlog C.4):
reading A is the book's standing assumption, the agreement remains its check, and d_0
follows from the assumption.

RESULT 3 -- WHAT FOLLOWS, AND WHAT IT COSTS.  With d_0 = sqrt(2) hbar c / m_h:
  * comp 197's dependency graph has no free node: K is canonical by assumption, c-tilde
    follows (c-tilde = 8 m_h^2 K in the units of comp 177), d_0 follows.  The item is
    one named premise, canonical normalisation, with nothing left that "needs d_0".
  * the Casimir bound d_0 < 2.3 nm is satisfied by nine orders of magnitude, and the
    correction eq:casimir, xi (d_0/d)^2 relative to the leading term with xi = 90/pi^2,
    is 1 x 10^-19 at 20 nm and 2 x 10^-21 at 160 nm, against a measurement precision of
    2 x 10^-3: the d^-6 exponent and the coefficient stay exact predictions, and the
    signal is not one anyone will measure.  Chapter 15's detectability claim rests on
    the nanometre expectation of Result 2.
  * comp 197's derived-unit coefficient becomes kappa = hbar c / (v^2 d_0^2) = m_h^2 / (2 v^2)
    = 0.129 in the neutral frame, order one, and numerically the Standard Model quartic
    m_h^2 / (2 v^2) itself; the elementary mass scale eq:pst-mass is 28 TeV; a_* of
    comp 196 passes through d_0 at k T ~ 40 GeV, the electroweak epoch, rather than at
    83 eV.  Every number this book computed "at the Casimir bound" is an illustration at
    a length nine orders above the one its own assumption implies.

CHECKS.  (1) Symbolic: V''(r_0) = 4 eps for V = -eps r^2 + b r^4 at r_0^2 = eps/(2b),
independent of b; the GL length sqrt(c/(2|a|)) and the radial correlation length
sqrt(c/V'') in the three frames; the frame factors.  (2) The numbers above, with their
ratios to l_*, l_P and the bound.  (3) The c_LG a nanometre coherence length needs, and
the M_* it would imply through comp 176's linearity.  (4) The BCS analogy quantified:
xi / a = E_F / (pi Delta) against d_0 / l_* = sqrt 2 M_* / m_h.  (5) Tamper: treating |a_LG|
as free and asking how close to threshold the substrate would have to sit for 2.3 nm
at c_LG = 1 gives |a_LG| = 1.1 x 10^-18 m_h^2 / 4, i.e. a radial mode of 1.2 x 10^-7 GeV in
place of the Higgs, which is what the near-threshold reading was silently assuming.
"""

import math
import sympy as sp

HBARC = 1.973269804e-16     # GeV m
MH = 125.25                 # GeV
V = 246.22                  # GeV
MSTAR = 4 * math.pi * MH * math.sqrt(2.0 / 3.0)
LSTAR = HBARC / MSTAR
LPL = 1.616255e-35          # m
D0_BOUND = 2.3e-9           # m, Chapter 15's diagnostic bound
XI_CAS = 90 / math.pi ** 2


def rule(ch="="):
    print(ch * 92)


def part1():
    rule()
    print("  1.  THE COHERENCE LENGTH IN EVERY FRAME, SYMBOLICALLY")
    rule()
    eps, b, c, r = sp.symbols("epsilon b c r", positive=True)
    Vpot = -eps * r ** 2 + b * r ** 4
    r0 = sp.sqrt(eps / (2 * b))
    curv = sp.simplify(sp.diff(Vpot, r, 2).subs(r, r0))
    ok = curv == 4 * eps
    print(f"      V = -eps r^2 + b r^4, r_0^2 = eps/(2b): V''(r_0) = {curv}, independent of b           : {ok}")
    xi_gl = sp.sqrt(c / (2 * eps))                 # eq:xi_psi with |a_LG| = eps
    xi_rad = sp.sqrt(c / curv)                     # radial correlation length, kinetic (c/2)(grad r)^2 against (1/2) V'' delta^2
    ratio = sp.simplify(xi_gl / xi_rad)
    print(f"      GL length sqrt(c/(2|a|)) over the radial correlation length sqrt(c/V'') = {ratio}")
    ok &= ratio == sp.sqrt(2)
    mh = sp.symbols("m_h", positive=True)
    frames = {
        "neutral component, |a| = m_h^2/4 (eq:radial-mass)": mh ** 2 / 4,
        "doublet, |a| = mu^2 = m_h^2/2 (eq:psi-Higgs-map)": mh ** 2 / 2,
    }
    print("      d_0 m_h at c_LG = 1:")
    for name, a_abs in frames.items():
        val = sp.simplify(xi_gl.subs({eps: a_abs, c: 1}) * mh)
        print(f"        {name:52s} d_0 m_h = {val}")
        ok &= val in (sp.sqrt(2), 1)
    print("      so the frame fixes an order-one factor and nothing else: d_0 m_h = O(1) under canonical normalisation")
    print()
    return bool(ok)


def part2():
    rule()
    print("  2.  THE NUMBERS")
    rule()
    ok = True
    d0_n = math.sqrt(2) * HBARC / MH      # neutral frame
    d0_d = HBARC / MH                     # doublet frame
    print(f"      d_0 = sqrt(2) hbar c / m_h = {d0_n:.3e} m = {d0_n * 1e18:.2f} am (neutral frame);  hbar c / m_h = {d0_d * 1e18:.2f} am (doublet frame)")
    print(f"      l_* = hbar c / M_* = {LSTAR:.3e} m;  d_0 / l_* = {d0_n / LSTAR:.2f} (neutral) = sqrt(2) x M_*/m_h = sqrt(2) x {MSTAR / MH:.2f};  {d0_d / LSTAR:.2f} (doublet)")
    print(f"      d_0 / l_P = {d0_n / LPL:.1e};  Casimir bound / d_0 = {D0_BOUND / d0_n:.1e}")
    ok &= 1.5e-18 < d0_d < d0_n < 2.5e-18 and abs(d0_d / LSTAR - MSTAR / MH) < 1e-9
    for d in (20e-9, 50e-9, 160e-9):
        rel = XI_CAS * (d0_n / d) ** 2
        print(f"      Casimir correction at d = {d * 1e9:4.0f} nm: xi (d_0/d)^2 = {rel:.1e} of the leading term (at the bound it would be {XI_CAS * (D0_BOUND / d) ** 2:.1e}); best precision 1.9e-3")
    ok &= XI_CAS * (d0_n / 20e-9) ** 2 < 1e-18
    kappa = HBARC ** 2 / (V ** 2 * d0_n ** 2) / HBARC ** 2 * HBARC ** 2   # dimensionless: hbar c / (v^2 d_0^2) in natural units
    kappa = (HBARC / d0_n) ** 2 / V ** 2
    print(f"      derived-unit coefficient of comp 197: kappa = hbar c/(v^2 d_0^2) = {kappa:.4f} = m_h^2/(2 v^2) = {MH ** 2 / (2 * V ** 2):.4f}, the Standard Model quartic's value; at the bound it was 1.2e-19")
    ok &= abs(kappa - MH ** 2 / (2 * V ** 2)) < 1e-9
    m0 = (4 * math.pi ** 3 / 3) * V ** 2 * d0_n / HBARC
    print(f"      eq:pst-mass at r_0 = v: m_0 = (4 pi^3/3) v^2 d_0 / (hbar c) = {m0 / 1e3:.1f} TeV (was 2.9e13 GeV at the bound)")
    gstar = 106.75
    kT = (30 / (math.pi ** 2 * gstar)) ** 0.25 * HBARC / d0_n
    print(f"      comp 196's a_* passes through d_0 at k T = {kT:.0f} GeV with g_* = 106.75: the electroweak epoch, not 83 eV")
    ok &= 10 < kT < 100
    print()
    return bool(ok)


def part3():
    rule()
    print("  3.  WHAT A NANOMETRE COHERENCE LENGTH WOULD NEED")
    rule()
    c_needed = (D0_BOUND * MH / HBARC) ** 2 / 2
    print(f"      c_LG = (d_0 m_h)^2 / 2 at d_0 = 2.3 nm: {c_needed:.2e}")
    # comp 176: M_* = 4 pi m_h c sqrt(2/3) with c the normalisation parameter (phi = sqrt(c) psi), so the seagull's
    # M_* scales with the square root of the kinetic coefficient's rescaling of the quartic; state it as comp 176 does
    mstar_scaled = MSTAR * math.sqrt(c_needed)
    print(f"      comp 176: M_* is linear in the parameter c with lambda_canon = 1/(4c^2); a kinetic coefficient c_LG rescales the field by sqrt(c_LG),")
    print(f"      so the seagull's M_* would move by sqrt(c_LG) = {math.sqrt(c_needed):.1e}, to {mstar_scaled:.1e} GeV, against the 1.0 sigma Higgs match at 1285 GeV")
    print(f"      and comp 176 excludes even c^2 = e, a factor {math.sqrt(math.e):.2f}, at 121 sigma")
    ok = c_needed > 1e17
    print("      => the nanometre range and the quantitative core (M_*, Z^2, the Higgs match) cannot both be had")
    print()
    return ok


def part4():
    rule()
    print("  4.  THE BCS ANALOGY, QUANTIFIED, AND THE TAMPER")
    rule()
    # BCS: xi / a ~ hbar v_F / (pi Delta a) ~ E_F / Delta up to O(1): three to six orders because Delta/E_F ~ 1e-3 to 1e-5
    print("      BCS: xi/a ~ E_F/(pi Delta), 1e3 to 1e6 because Delta/E_F ~ 1e-3 to 1e-5")
    print(f"      PST: the gap analogue is m_h and the bandwidth analogue is M_*; d_0/l_* = sqrt(2) M_*/m_h = {math.sqrt(2) * MSTAR / MH:.1f}, with M_*/m_h = 4 pi sqrt(2/3) the book's own eq:mstar-pred")
    ok = abs(math.sqrt(2) * MSTAR / MH - math.sqrt(2) * 4 * math.pi * math.sqrt(2 / 3)) < 1e-9
    # tamper: let |a_LG| float at c_LG = 1 and ask what gives 2.3 nm
    a_needed = 1.0 / (2 * (D0_BOUND / HBARC) ** 2)          # |a| = c/(2 d_0^2), GeV^2
    m_rad = math.sqrt(4 * a_needed)                          # radial mode mass
    print(f"      tamper, |a_LG| left free at c_LG = 1: 2.3 nm needs |a_LG| = {a_needed:.2e} GeV^2 = {a_needed / (MH ** 2 / 4):.1e} x m_h^2/4, a radial mode of {m_rad:.1e} GeV in place of the Higgs")
    ok &= m_rad < 1e-6
    print("      which is what the near-threshold reading of sec:grain-vs-coherence was silently assuming")
    print()
    return bool(ok)


def main():
    rule()
    print("  COMPUTATION 201.  The gradient normalisation and the coherence length are one unknown")
    rule()
    results = [
        ("symbolic: V''(r_0) = 4 eps in every frame, d_0 m_h = O(1) at canonical normalisation", part1()),
        ("numbers: d_0 = 1.6 to 2.2 am, d_0/l_* = M_*/m_h x O(1), Casimir correction below 1e-18, kappa order one", part2()),
        ("a nanometre coherence length needs c_LG ~ 1e18, excluded by the book's own normalisation result", part3()),
        ("the BCS analogy quantified; the tamper shows what near-threshold was assuming", part4()),
    ]
    rule("-")
    for i, (name, r) in enumerate(results, 1):
        print(f"  {i}. {name}: {'PASS' if r else 'FAIL'}")
    allok = all(r for _, r in results)
    rule("-")
    print("  VERDICT:", "PASS" if allok else "FAIL")
    print("""
  Under the canonical reading the book already makes, the coherence length is
  sqrt(2) hbar c / m_h, about two attometres, fourteen grains, and the last structural
  item has no free node: it is one named premise, canonical normalisation.  The price
  is Chapter 15's nanometre expectation, which rested on treating |a_LG| as free when the
  Higgs mass pins it; the Casimir exponent and coefficient stay exact, the signal does
  not stay measurable.  The decision between the two is not this file's to make.""")
    return allok


if __name__ == "__main__":
    main()
