"""Wilson lattice gauge theory, SU(2). Round 7: third seed plus the
estimator test.

Round 6 overturned the five-point torelon extrapolation: seed 2
torelon at beta 2.5 and 2.6 sits far below seed 1, so the continuum
intercept swings from +2.42 to -0.21 depending on the chain. Two
explanations compete:

  (a) statistics: the chains are independent Monte Carlo histories
      and the jackknife errors underestimate the true chain-to-chain
      uncertainty;
  (b) estimator: the torelon effective-mass plateau choice (m_eff(1)
      preferred, m_eff(0) otherwise) is unstable, and the picked mass
      moves with the plateau rule rather than with the chain.

This round tests both at once. A THIRD independent seed (base
20261122) runs at beta 2.5 and 2.6 with the identical setup (8^4,
400 thermalization + 1500 measurement sweeps, Metropolis
checkerboard, APE smearing alpha 0.5 levels 0/2/4, GEVP, jackknife).
Then, for all three seeds at both betas, the torelon effective
masses are compared tau by tau (m_eff(0), m_eff(1), m_eff(2)) on the
same chain. If the within-seed tau spread is small but the
cross-seed spread at fixed tau is large, the discrepancy is
statistics. If the tau choice moves the mass by more than the seed
spread does, it is an estimator problem.

The extrapolation is NOT re-attempted as a claim. A three-seed
inverse-variance mean series fit is computed as a diagnostic only,
and the verdict on the extrapolation stands OVERTURNED unless all
three chains agree within combined errors at both betas.

Run: python3 wilson_lattice_r7.py (from the lab root).
Output: data/yang_mills_r7.json; checkpoint
data/yang_mills_r7_checkpoint.json after each beta.
"""

import json
import math
import os
import sys
import time

import numpy as np

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from wilson_lattice_r5 import run  # noqa: E402
from wilson_lattice_r6 import (  # noqa: E402
    agree,
    fit_torelon,
    load_runs,
    pick_mass,
    torelon_point,
    weighted_mean,
)


def usable(m):
    return (m.get("value") is not None and m.get("error") is not None
            and m["error"] < 0.5 * abs(m["value"]))


def tau_masses(run_dict):
    """All usable torelon effective masses by tau for one run."""
    out = {}
    for tau in (0, 1, 2):
        m = run_dict["torelon_effective_masses"].get("m_eff(%d)" % tau, {})
        if usable(m):
            out[tau] = {"value": m["value"], "error": m["error"]}
    return out


def spread(values):
    """Max minus min of a list of numbers, or None when empty."""
    if not values:
        return None
    return max(values) - min(values)


def main():
    t_start = time.time()
    here = os.path.dirname(os.path.abspath(__file__))
    ckpt_path = os.path.join(here, "..", "data",
                             "yang_mills_r7_checkpoint.json")
    seed_base = 20261122
    runs = []
    if os.path.exists(ckpt_path):
        with open(ckpt_path) as f:
            runs = json.load(f).get("runs", [])
        print(f"checkpoint found: reusing completed betas "
              f"{[r['beta'] for r in runs]}", flush=True)
    done_betas = {r["beta"] for r in runs}
    for beta in (2.5, 2.6):
        if beta in done_betas:
            print(f"beta {beta} already complete in checkpoint, skipping",
                  flush=True)
            continue
        print(f"Round 7: beta={beta} on 8^4 SU(2), seed base {seed_base}",
              flush=True)
        runs.append(run(beta, seed=seed_base))
        print(f"beta {beta} 8^4 done in {runs[-1]['runtime_seconds']:.1f} s",
              flush=True)
        with open(ckpt_path, "w") as f:
            json.dump({"runs": runs, "date": "2026-11-21",
                       "seed": seed_base}, f, indent=2)
        print(f"checkpoint written: {ckpt_path}", flush=True)
    total = time.time() - t_start

    # Reference runs: seed 1 beta 2.5 from round 3, beta 2.6 from
    # round 5; seed 2 both betas from round 6.
    seed1 = {}
    for r in load_runs("yang_mills_r3.json"):
        if str(r["beta"]) == "2.5" and r["lattice"] == "8^4":
            seed1[2.5] = r
    for r in load_runs("yang_mills_r5.json"):
        if str(r["beta"]) == "2.6":
            seed1[2.6] = r
    seed2 = {r["beta"]: r for r in load_runs("yang_mills_r6.json")}
    seed3 = {r["beta"]: r for r in runs}

    # Three-seed comparison per beta.
    comparison = {}
    for b in (2.5, 2.6):
        s1, s2, s3 = seed1[b], seed2[b], seed3[b]
        tor1, tor2, tor3 = (pick_mass(s1["torelon_effective_masses"]),
                            pick_mass(s2["torelon_effective_masses"]),
                            pick_mass(s3["torelon_effective_masses"]))
        vg1 = s1.get("variational_glueball", {}).get("ground_state_mass")
        gevp1 = vg1 if vg1 is not None else {"value": None, "error": None,
                                             "note": "no GEVP in round 3"}
        gevp2 = s2["variational_glueball"]["ground_state_mass"]
        gevp3 = s3["variational_glueball"]["ground_state_mass"]
        comparison[str(b)] = {
            "seed1": {
                "plaquette": s1["avg_plaquette"],
                "plaquette_error": s1["plaquette_error"],
                "string_tension": s1["string_tension"],
                "torelon": tor1,
                "gevp_ground_state_mass": gevp1,
            },
            "seed2": {
                "plaquette": s2["avg_plaquette"],
                "plaquette_error": s2["plaquette_error"],
                "string_tension": s2["string_tension"],
                "torelon": tor2,
                "gevp_ground_state_mass": gevp2,
            },
            "seed3": {
                "plaquette": s3["avg_plaquette"],
                "plaquette_error": s3["plaquette_error"],
                "string_tension": s3["string_tension"],
                "torelon": tor3,
                "gevp_ground_state_mass": gevp3,
            },
            "plaquette_agrees_1v2": agree(
                {"value": s1["avg_plaquette"],
                 "error": s1["plaquette_error"]},
                {"value": s2["avg_plaquette"],
                 "error": s2["plaquette_error"]}),
            "plaquette_agrees_1v3": agree(
                {"value": s1["avg_plaquette"],
                 "error": s1["plaquette_error"]},
                {"value": s3["avg_plaquette"],
                 "error": s3["plaquette_error"]}),
            "plaquette_agrees_2v3": agree(
                {"value": s2["avg_plaquette"],
                 "error": s2["plaquette_error"]},
                {"value": s3["avg_plaquette"],
                 "error": s3["plaquette_error"]}),
            "string_tension_agrees_1v2": agree(s1["string_tension"],
                                               s2["string_tension"]),
            "string_tension_agrees_1v3": agree(s1["string_tension"],
                                               s3["string_tension"]),
            "string_tension_agrees_2v3": agree(s2["string_tension"],
                                               s3["string_tension"]),
            "torelon_agrees_1v2": agree(tor1, tor2),
            "torelon_agrees_1v3": agree(tor1, tor3),
            "torelon_agrees_2v3": agree(tor2, tor3),
            "gevp_agrees_1v2": agree(gevp1, gevp2),
            "gevp_agrees_1v3": agree(gevp1, gevp3),
            "gevp_agrees_2v3": agree(gevp2, gevp3),
            "torelon_three_seed_spread": spread(
                [tor1["value"], tor2["value"], tor3["value"]]),
            "gevp_three_seed_spread": spread(
                [g["value"] for g in (gevp1, gevp2, gevp3)
                 if g["value"] is not None]),
        }

    # Estimator test: tau-by-tau torelon masses on the same chain.
    estimator = {}
    for b in (2.5, 2.6):
        per_seed = {}
        tau_spreads = []
        for label, s in (("seed1", seed1[b]), ("seed2", seed2[b]),
                         ("seed3", seed3[b])):
            tm = tau_masses(s)
            per_seed[label] = tm
            tau_spreads.append(spread([v["value"] for v in tm.values()]))
        # Cross-seed spread at fixed tau (the picked tau, 1).
        pick_vals = []
        for s in (seed1[b], seed2[b], seed3[b]):
            m = pick_mass(s["torelon_effective_masses"])
            pick_vals.append(m["value"])
        cross_seed_spread = spread(pick_vals)
        # Cross-seed spread at each tau, for the record.
        cross_by_tau = {}
        for tau in (0, 1, 2):
            vals = []
            for s in (seed1[b], seed2[b], seed3[b]):
                tm = tau_masses(s)
                if tau in tm:
                    vals.append(tm[tau]["value"])
            cross_by_tau[str(tau)] = spread(vals)
        within = [x for x in tau_spreads if x is not None]
        within_max = max(within) if within else None
        if within_max is not None and cross_seed_spread is not None:
            if within_max > cross_seed_spread:
                verdict = ("ESTIMATOR: within a single chain the torelon "
                           "mass moves more across tau (%.3f) than it "
                           "moves across chains at fixed tau (%.3f); the "
                           "plateau choice, not the chain, drives the "
                           "discrepancy." % (within_max, cross_seed_spread))
            else:
                verdict = ("STATISTICS: within a single chain the torelon "
                           "mass moves little across tau (%.3f) while it "
                           "moves more across chains at fixed tau (%.3f); "
                           "the discrepancy is chain-to-chain noise and "
                           "the jackknife errors underestimate it."
                           % (within_max, cross_seed_spread))
        else:
            verdict = ("INCONCLUSIVE: too few usable tau masses to "
                       "compare within-seed and cross-seed spreads.")
        estimator[str(b)] = {
            "per_seed_tau_masses": per_seed,
            "within_seed_tau_spread_max": within_max,
            "cross_seed_spread_at_picked_tau": cross_seed_spread,
            "cross_seed_spread_by_tau": cross_by_tau,
            "verdict": verdict,
        }

    # Diagnostic three-seed mean series fit (NOT a continuum claim).
    base = []
    for r in load_runs("yang_mills.json"):
        if str(r["beta"]) == "2.2" and r["lattice"] == "8^4":
            base.append(r)
    for r in load_runs("yang_mills_r4.json"):
        if r["lattice"] == "8^4":
            base.append(r)
    for r in load_runs("yang_mills_r3.json"):
        if str(r["beta"]) == "2.5" and r["lattice"] == "8^4":
            base.append(r)
    for r in load_runs("yang_mills_r5.json"):
        base.append(r)
    base.sort(key=lambda r: r["beta"])
    points_A = [torelon_point(r) for r in base]

    def mean3(p_list):
        vals = [p["m_tor"] for p in p_list]
        errs = [p["m_tor_error"] for p in p_list]
        w = [1.0 / e ** 2 for e in errs]
        v = sum(x * wi for x, wi in zip(vals, w)) / sum(w)
        e = math.sqrt(1.0 / sum(w))
        s_vals = [p["sigma_a2"] for p in p_list]
        s_errs = [p["sigma_a2_error"] for p in p_list]
        ws = [1.0 / e ** 2 for e in s_errs]
        sv = sum(x * wi for x, wi in zip(s_vals, ws)) / sum(ws)
        se = math.sqrt(1.0 / sum(ws))
        y = v / math.sqrt(sv)
        ey = y * math.sqrt((e / v) ** 2 + (0.5 * se / sv) ** 2)
        return {"m_tor": v, "m_tor_error": e, "sigma_a2": sv,
                "sigma_a2_error": se, "m_tor_over_sqrt_sigma": y,
                "m_tor_over_sqrt_sigma_error": ey}

    points_D = []
    for p in points_A:
        b = p["beta"]
        if b in seed2 and b in seed3:
            p2 = torelon_point(seed2[b])
            p3 = torelon_point(seed3[b])
            m = mean3([p, p2, p3])
            m["beta"] = b
            points_D.append(m)
        else:
            points_D.append(p)
    fit_D = fit_torelon(points_D)

    # Extrapolation verdict: stands OVERTURNED unless all three chains
    # agree within combined errors on the torelon at both betas.
    all_agree = all(
        comparison[str(b)]["torelon_agrees_1v2"] is True
        and comparison[str(b)]["torelon_agrees_1v3"] is True
        and comparison[str(b)]["torelon_agrees_2v3"] is True
        for b in (2.5, 2.6))
    if all_agree:
        verdict = ("REVISED: all three seeds agree on the torelon "
                   "within combined errors at both betas; the Round 6 "
                   "overturn is itself overturned and the extrapolation "
                   "may be re-attempted with the three-seed mean series.")
    else:
        verdict = ("OVERTURNED STANDS: the three seeds do not agree on "
                   "the torelon within combined errors; the five-point "
                   "linear extrapolation still cannot support any "
                   "continuum claim.")

    out = {
        "method": "Metropolis, checkerboard even/odd updates, random "
                  "SU(2) rotation near identity, eps=0.35, seed "
                  "20261122 + int(beta*100), cold start; APE smearing of "
                  "spatial links, alpha 0.5, levels 0, 2, 4 steps, "
                  "variational GEVP over the three levels",
        "lattice": "8^4",
        "gauge_group": "SU(2)",
        "beta": [2.5, 2.6],
        "thermalization_sweeps": 400,
        "measurement_sweeps": 1500,
        "seed": seed_base,
        "seed_references": "seed 1 base 20261120: beta 2.5 from "
                           "data/yang_mills_r3.json, beta 2.6 from "
                           "data/yang_mills_r5.json; seed 2 base 20261121 "
                           "from data/yang_mills_r6.json",
        "avg_plaquette": {str(r["beta"]): r["avg_plaquette"] for r in runs},
        "plaquette_error": {str(r["beta"]): r["plaquette_error"]
                            for r in runs},
        "string_tension": {str(r["beta"]): r["string_tension"]
                           for r in runs},
        "three_seed_comparison": comparison,
        "estimator_test": estimator,
        "continuum_extrapolation_torelon": {
            "channel": "torelon m_eff(1) preferred, m_eff(0) otherwise, "
                       "in units of sqrt(sigma)",
            "fit": "weighted linear fit of m_tor / sqrt(sigma) vs "
                   "sigma a^2 (a^2 proxy at fixed physical sigma)",
            "series_D_three_seed_mean_points": points_D,
            "series_D_three_seed_mean_fit": fit_D,
            "note": "Diagnostic only. The fit is reported so the "
                    "three-seed mean series is on the record; it is not "
                    "a continuum claim. Same standing caveats as before: "
                    "sigma a^2 from chi(2,2) overestimates the "
                    "asymptotic tension, the 8^4 volume leaves "
                    "finite-volume effects uncontrolled, and a linear a^2 "
                    "form is assumed, not established.",
            "verdict": verdict,
        },
        "runs": runs,
        "runtime_seconds": round(total, 1),
        "date": "2026-11-21",
    }

    out_path = os.path.join(here, "..", "data", "yang_mills_r7.json")
    with open(out_path, "w") as f:
        json.dump(out, f, indent=2)

    print("=" * 60)
    for b in (2.5, 2.6):
        c = comparison[str(b)]
        print(f"beta={b} seed3 torelon = "
              f"{c['seed3']['torelon']['value']:.4f} +/- "
              f"{c['seed3']['torelon']['error']:.4f} "
              f"(seed1 {c['seed1']['torelon']['value']:.4f} +/- "
              f"{c['seed1']['torelon']['error']:.4f}, "
              f"seed2 {c['seed2']['torelon']['value']:.4f} +/- "
              f"{c['seed2']['torelon']['error']:.4f}) "
              f"agree 1v2={c['torelon_agrees_1v2']} "
              f"1v3={c['torelon_agrees_1v3']} "
              f"2v3={c['torelon_agrees_2v3']}")
        print(f"  GEVP 0++ seed3 = "
              f"{c['seed3']['gevp_ground_state_mass']['value']:.4f} +/- "
              f"{c['seed3']['gevp_ground_state_mass']['error']:.4f} "
              f"(seed1 "
              f"{c['seed1']['gevp_ground_state_mass']['value'] if c['seed1']['gevp_ground_state_mass']['value'] is not None else 'none'}, "
              f"seed2 {c['seed2']['gevp_ground_state_mass']['value']:.4f})")
        print(f"  estimator: {estimator[str(b)]['verdict']}")
    print(f"fit D (three-seed mean): c0 = "
          f"{fit_D['continuum_value_c0']:.4f} +/- "
          f"{fit_D['continuum_value_error']:.4f}, chi2/dof = "
          f"{fit_D['chi2']:.2f}/{fit_D['dof']}")
    print(verdict)


if __name__ == "__main__":
    main()
