"""Round 3 Yang-Mills hunt: beta 2.5 on 8^4, plus a 12^4 volume check.

Same protocol as rounds 1 and 2: Metropolis checkerboard, eps 0.35,
cold start, 400 thermalization + 1500 measurement sweeps. Seed
convention is the same as compute/wilson_lattice.py: seed = 20261120
+ int(beta*100), so beta 2.5 uses seed 20261370 and the 12^4 run at
beta 2.3 uses seed 20261350.

History of this script: the first version ran a light-load probe
(3.159 s per sweep, projected 6002 s for the full 12^4 run) and
started the full program, but five other hunt processes were running
in parallel and this process only got about 19 percent of a core
(99 s CPU over 516 s wall, measured 23:03 to 23:11 EST), which
projected the 12^4 measurement phase alone to roughly 8 hours. That
run was killed at 23:12 EST with beta 2.5 already complete but only
in memory. This version therefore writes a checkpoint JSON to disk
immediately after beta 2.5, then sizes the 12^4 run from a fresh
probe taken under the current load, with a wall-time budget of about
6000 s: measurement sweeps are capped at 1000 and floored at 300
(300 keeps the 30-block jackknife), and the chosen count plus the
measured probe numbers are recorded in the output JSON.

Writes data/yang_mills_r3.json (same schema shape as
data/yang_mills_r2.json, single beta plus a 12^4 volume check, and a
four-point torelon continuum extrapolation using betas 2.2, 2.3, 2.4
from data/yang_mills_r2.json plus the new beta 2.5 point).

Run: cd ~/workspace/millennium-lab && python3 compute/wilson_lattice_r3.py > data/wilson_r3.log 2>&1
"""
import json
import math
import os
import sys
import time

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from wilson_lattice import run


def pick_mass(masses):
    """Best effective mass: m_eff(1) when it exists with relative
    jackknife error under 50 percent, else m_eff(0). Same rule as rounds 1 and 2."""
    m1 = masses.get("m_eff(1)", {})
    if (m1.get("value") is not None and m1.get("error") is not None
            and m1["error"] < 0.5 * abs(m1["value"])):
        return m1
    return masses.get("m_eff(0)", {})


def gap_estimates_for(runs):
    """Gap estimates for a list of run dicts, same pick rule as rounds 1 and 2."""
    gap_estimates = []
    for r in runs:
        for key, label in (
            ("glueball_effective_masses",
             "0++ glueball effective mass from time-slice plaquette correlator (m_eff(1) preferred, m_eff(0) otherwise)"),
            ("tensor_effective_masses",
             "2++ tensor glueball effective mass from orientation-difference plaquette correlator"),
            ("torelon_effective_masses",
             "torelon effective mass from spatial Polyakov loop correlator"),
        ):
            g = pick_mass(r[key])
            if g.get("value") is not None:
                gap_estimates.append({
                    "beta": r["beta"],
                    "lattice": r["lattice"],
                    "gap_estimate": g["value"],
                    "error": g["error"],
                    "method": label,
                })
    return gap_estimates


def torelon_extrapolation(r25):
    """Four-point weighted linear fit of m_tor/sqrt(sigma) vs sigma a^2.
    Round 1 and 2 points are read from data/yang_mills_r2.json
    (betas 2.2, 2.3, 2.4); the beta 2.5 point comes from this run.
    Same fit as round 2, now with four points and 2 dof."""
    r2_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                           "..", "data", "yang_mills_r2.json")
    with open(r2_path) as f:
        r2 = json.load(f)
    pts = []
    for p in r2["continuum_extrapolation_torelon"]["points"]:
        pts.append(dict(p))
    tor25 = pick_mass(r25["torelon_effective_masses"])
    sig25 = r25["string_tension"]
    pts.append({
        "beta": 2.5,
        "sigma_a2": sig25["value"],
        "sigma_a2_error": sig25["error"],
        "m_tor": tor25["value"],
        "m_tor_error": tor25["error"],
        "m_tor_over_sqrt_sigma": tor25["value"] / math.sqrt(sig25["value"]),
    })
    pts.sort(key=lambda p: p["beta"])
    # Weighted linear fit y = c0 + c1 * x, y = m_tor/sqrt(sigma),
    # x = sigma a^2. Errors on y propagated from m_tor and sigma
    # errors (treated as independent); x errors ignored in the fit.
    xs = [p["sigma_a2"] for p in pts]
    ys = [p["m_tor_over_sqrt_sigma"] for p in pts]
    yerr = []
    for p in pts:
        rel = math.sqrt((p["m_tor_error"] / p["m_tor"]) ** 2
                        + (0.5 * p["sigma_a2_error"] / p["sigma_a2"]) ** 2)
        yerr.append(abs(p["m_tor_over_sqrt_sigma"]) * rel)
    w = [1.0 / (e * e) for e in yerr]
    sw = sum(w)
    sx = sum(wi * xi for wi, xi in zip(w, xs))
    sy = sum(wi * yi for wi, yi in zip(w, ys))
    sxx = sum(wi * xi * xi for wi, xi in zip(w, xs))
    sxy = sum(wi * xi * yi for wi, xi, yi in zip(w, xs, ys))
    det = sw * sxx - sx * sx
    c0 = (sxx * sy - sx * sxy) / det
    c1 = (sw * sxy - sx * sy) / det
    var_c0 = sxx / det
    var_c1 = sw / det
    chi2 = sum(wi * (yi - c0 - c1 * xi) ** 2
               for wi, xi, yi in zip(w, xs, ys))
    return {
        "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)",
        "points": pts,
        "continuum_value_c0": c0,
        "continuum_value_error": math.sqrt(var_c0),
        "slope_c1": c1,
        "slope_error": math.sqrt(var_c1),
        "chi2": chi2,
        "dof": 2,
        "note": "Four points, two fit parameters, two degrees of freedom. "
                "The fit is still thin: the beta 2.2 torelon error is large, "
                "sigma a^2 is itself an estimate from chi(2,2) that "
                "overestimates the asymptotic tension, the 8^4 volume "
                "leaves finite-volume effects uncontrolled, and a linear "
                "a^2 form is assumed, not established. This is an "
                "extrapolation attempt, not a continuum result.",
    }, c0, math.sqrt(var_c0), c1, math.sqrt(var_c1), chi2


def build_out(r25, r23_12, extrapolation, n_meas_12, probe_info, total,
              status):
    ratios = {}
    for r in [x for x in (r25, r23_12) if x is not None]:
        g = pick_mass(r["glueball_effective_masses"])
        s = r["string_tension"]
        if g.get("value") is not None and s["value"] > 0:
            ratios[r["lattice"]] = g["value"] / math.sqrt(s["value"])
    runs = [x for x in (r25, r23_12) if x is not None]
    out = {
        "method": "Metropolis, checkerboard even/odd updates, random SU(2) rotation near identity, eps=0.35, seed 20261120 + int(beta*100) (= 20261370 for beta 2.5), cold start",
        "lattice": "8^4",
        "gauge_group": "SU(2)",
        "beta": [2.5],
        "thermalization_sweeps": 400,
        "measurement_sweeps": 1500,
        "seed": 20261370,
        "status": status,
        "avg_plaquette": {"2.5": r25["avg_plaquette"]},
        "plaquette_error": {"2.5": r25["plaquette_error"]},
        "creutz_ratios": {"2.5": r25["creutz_ratios"]},
        "string_tension": {"2.5": r25["string_tension"]},
        "string_tension_note": "sigma a^2 from Creutz ratio chi(2,2); chi(3,3) also reported in creutz_ratios for comparison. Finite 8^4 volume, no continuum limit taken.",
        "gap_estimates": gap_estimates_for(runs),
        "gap_over_sqrt_sigma": ratios,
        "continuum_extrapolation_torelon": extrapolation,
        "runs": runs,
        "volume_check": {
            "beta": 2.3,
            "lattice_8": "8^4",
            "lattice_12": "12^4",
            "measurement_sweeps_12": n_meas_12,
            "probe": probe_info,
            "note": "Same beta and seed family on 12^4. Compare plaquette, sigma a^2 and gap estimates between the two volumes in the runs list. The 12^4 measurement sweeps were set from a short timing probe under the load at run time, with a wall-time budget of about 6000 s: 1500 unless the projection exceeded the budget, then capped at 1000 and floored at 300.",
        },
        "runtime_seconds": round(total, 1) if total is not None else None,
        "date": "2026-11-20",
    }
    return out


def print_run(r):
    print(f"beta={r['beta']}  lattice={r['lattice']}  SU(2)  "
          f"acceptance={r['acceptance_rate']:.3f}")
    print(f"  avg plaquette = {r['avg_plaquette']:.6f} "
          f"+/- {r['plaquette_error']:.6f}")
    for k, v in r["creutz_ratios"].items():
        err = f"{v['error']:.4f}" if v['error'] is not None else "n/a"
        print(f"  {k} = {v['value']:.4f} +/- {err}")
    s = r["string_tension"]
    serr = f"{s['error']:.4f}" if s['error'] is not None else "n/a"
    print(f"  string tension sigma a^2 = {s['value']:.4f} +/- {serr} (chi(2,2))")
    for label, key in (("glueball", "glueball_effective_masses"),
                       ("tensor 2++", "tensor_effective_masses"),
                       ("torelon", "torelon_effective_masses")):
        for k, v in r[key].items():
            if v["value"] is None:
                print(f"  {label} {k}: no positive correlator")
            else:
                err = f"{v['error']:.4f}" if v['error'] is not None else "n/a"
                print(f"  {label} {k} = {v['value']:.4f} +/- {err}")
    v1 = r["static_potential_V1"]
    if v1["value"] is None:
        print("  static potential V(1): C_P(1) not positive")
    else:
        verr = f"{v1['error']:.4f}" if v1['error'] is not None else "n/a"
        print(f"  static potential V(1) = {v1['value']:.4f} +/- {verr}")
    print(f"  runtime = {r['runtime_seconds']:.1f} s")


def main():
    t_start = time.time()
    here = os.path.dirname(os.path.abspath(__file__))
    out_path = os.path.join(here, "..", "data", "yang_mills_r3.json")

    # Step 1: fourth beta point, beta 2.5 on 8^4, same protocol.
    print("Round 3: beta=2.5 on 8^4 SU(2), same protocol as rounds 1 and 2", flush=True)
    print("Seed convention: 20261120 + int(beta*100) = 20261370 for beta 2.5", flush=True)
    r25 = run(2.5)
    print(f"beta 2.5 8^4 done in {r25['runtime_seconds']:.1f} s", flush=True)

    # Checkpoint to disk immediately: the first attempt lost this run
    # because it lived only in memory when the process was killed.
    extrapolation, c0, c0err, c1, c1err, chi2 = torelon_extrapolation(r25)
    print(f"Torelon extrapolation (4 points): continuum m_tor/sqrt(sigma) = {c0:.3f} "
          f"+/- {c0err:.3f}, slope = {c1:.3f} "
          f"+/- {c1err:.3f}, chi2/dof = {chi2:.2f}/2", flush=True)
    checkpoint = build_out(r25, None, extrapolation, None, None, None,
                           "checkpoint: beta 2.5 complete, 12^4 pending")
    with open(out_path, "w") as f:
        json.dump(checkpoint, f, indent=2)
    print(f"checkpoint wrote {out_path}", flush=True)

    # Step 2: fresh 12^4 probe under the current load. The earlier
    # light-load probe projected 6002 s, but measured CPU share was
    # only about 19 percent, so size from this probe instead.
    print("Round 3 probe: beta=2.3 on 12^4, 10 therm + 60 meas sweeps, under current load", flush=True)
    probe = run(2.3, size=12, n_therm=10, n_meas=60)
    therm_per_sweep = probe["thermalization_seconds"] / 10.0
    meas_per_sweep = probe["measurement_seconds"] / 60.0
    projected_1500 = 400 * therm_per_sweep + 1500 * meas_per_sweep
    print(f"Probe: therm {therm_per_sweep:.3f} s/sweep, meas {meas_per_sweep:.3f} s/sweep", flush=True)
    print(f"Projected 12^4 full run (400 therm + 1500 meas) under current load: "
          f"{projected_1500:.0f} s", flush=True)
    budget = 6000.0
    if projected_1500 <= budget:
        n_meas_12 = 1500
    else:
        n_meas_12 = int((budget - 400 * therm_per_sweep) / meas_per_sweep)
        n_meas_12 = max(300, min(1000, n_meas_12))
    print(f"12^4 measurement sweeps set to {n_meas_12} for a ~{budget:.0f} s wall budget", flush=True)
    probe_info = {
        "therm_seconds_per_sweep": therm_per_sweep,
        "measurement_seconds_per_sweep": meas_per_sweep,
        "projected_full_1500_seconds": projected_1500,
        "wall_budget_seconds": budget,
        "first_attempt_note": "First attempt probe (light load) gave 3.159 s per sweep and projected 6002 s, but measured CPU share was only about 19 percent (99 s CPU over 516 s wall, 23:03 to 23:11 EST) with five other hunt processes running, projecting the 12^4 measurement phase alone to roughly 8 hours; that run was killed 23:12 EST and restarted with this load-aware sizing.",
    }

    # Step 3: finite-volume control, beta 2.3 on 12^4.
    print(f"Volume check: beta=2.3 on 12^4, {n_meas_12} meas sweeps, same seed family", flush=True)
    r23_12 = run(2.3, size=12, n_meas=n_meas_12)
    print(f"beta 2.3 12^4 done in {r23_12['runtime_seconds']:.1f} s", flush=True)
    total = time.time() - t_start

    out = build_out(r25, r23_12, extrapolation, n_meas_12, probe_info,
                    total, "complete")
    with open(out_path, "w") as f:
        json.dump(out, f, indent=2)

    print("=" * 60)
    for r in (r25, r23_12):
        print_run(r)
    print(f"Torelon extrapolation: continuum m_tor/sqrt(sigma) = {c0:.3f} "
          f"+/- {c0err:.3f}, slope = {c1:.3f} "
          f"+/- {c1err:.3f}, chi2/dof = {chi2:.2f}/2")
    print(f"total runtime = {total:.1f} s")
    print(f"wrote {out_path}")


if __name__ == "__main__":
    main()
