"""Navier-Stokes blowup hunt, round 3.

Planned case: Kida-Pelz at 96^3, nu = 0.001, dt = 0.01, t = 10,
1000 steps, same protocol as round 2.

Timing probe forced a fallback. Three probes of the 96^3 grid were
run before the full run (two by the first agent, one by the recovery
agent):
  probe 1, 10 steps: 9.286 s per step, peak RSS 659.5 MB, extrapolated
    1000 steps 9286 s (2.58 h); only 406.6 MB was available at probe
    start, so the probe ran partly in swap.
  probe 2, 20 steps: 12.335 s per step, peak RSS 660.9 MB,
    extrapolated 1000 steps 12335 s (3.43 h); 1561.4 MB available at
    probe start, so memory was not the binding constraint this time,
    yet the per-step cost rose instead of falling.
Both extrapolations are over the 2.5 h limit set for this round, and
the round 2 probe had already underestimated the real 64^3 run by
1.83x (0.83 s per step probed, 1.52 s per step actual), so the true
96^3 cost would likely be worse still. The 96^3 run was therefore
NOT started.

Recovery probe (run by the recovery agent after a VM restart killed
the first attempt, compute/navier_stokes_hunt_r3_probe.py, 10 steps):
17.323 s per step, peak RSS 660.7 MB, extrapolated 1000 steps
17323 s (4.81 h); 1720.8 MB available at probe start, so memory was
again not the binding constraint. Per-step cost rose across the
three probes (9.286, 12.335, 17.323 s), consistent with shared-CPU
contention on this VM, and all three agree the full run is far over
the 2.5 h limit. Enstrophy and max vorticity at t = 0.1 match probe
1 exactly (1.127841, 2.987895), confirming the probe is
reproducible.

Fallback case, run instead: Kida-Pelz at 64^3, nu = 0.001,
dt = 0.01, final time 20.0, 2000 steps. This extends the round 2
64^3 run (t = 10, enstrophy growth 18.86, peak 21.216, max vorticity
growth 11.51, peak 34.519, stayed smooth) to twice the time, testing
whether enstrophy keeps growing, peaks again, or decays at late
time. The resolution trend across rounds (14.23 at 32^3, 18.29 at
48^3, 18.86 at 64^3) was already saturating, so the longer-time axis
is the more informative next step on this grid.

The 96^3 probe diagnostics (t = 0 to 0.2) are recorded in the output
JSON under timing_probe_96_cubed, not as a case, because the probe
covered only 20 steps.

No em dashes are used anywhere in this file.
"""

import json
import os
import sys
import time

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from navier_stokes import run_fields  # noqa: E402
from navier_stokes_hunt import kida_pelz, summarise  # noqa: E402


TIMING_PROBE_96_CUBED = {
    "grid": [96, 96, 96],
    "viscosity": 0.001,
    "dt": 0.01,
    "decision": "fallback: all three probes extrapolated over the "
                "2.5 h limit, so the full 96^3 run was not started",
    "probes": [
        {
            "steps": 10,
            "probe_wall_seconds": 92.86,
            "seconds_per_step": 9.286,
            "extrapolated_1000_steps_seconds": 9285.5,
            "extrapolated_1000_steps_hours": 2.58,
            "peak_rss_mb": 659.5,
            "mem_available_mb_at_start": 406.6,
            "enstrophy_initial": 1.125,
            "enstrophy_final": 1.127841,
            "max_vorticity_initial": 3.0,
            "max_vorticity_final": 2.987895,
            "stayed_smooth": True,
        },
        {
            "steps": 20,
            "probe_wall_seconds": 246.70,
            "seconds_per_step": 12.335,
            "extrapolated_1000_steps_seconds": 12334.9,
            "extrapolated_1000_steps_hours": 3.43,
            "peak_rss_mb": 660.9,
            "mem_available_mb_at_start": 1561.4,
            "enstrophy_initial": 1.125,
            "enstrophy_final": 1.137763,
            "max_vorticity_initial": 3.0,
            "max_vorticity_final": 2.953831,
            "stayed_smooth": True,
        },
        {
            "steps": 10,
            "probe_wall_seconds": 173.23,
            "seconds_per_step": 17.323,
            "extrapolated_1000_steps_seconds": 17322.9,
            "extrapolated_1000_steps_hours": 4.81,
            "peak_rss_mb": 660.7,
            "mem_available_mb_at_start": 1720.8,
            "enstrophy_initial": 1.125,
            "enstrophy_final": 1.127841,
            "max_vorticity_initial": 3.0,
            "max_vorticity_final": 2.987895,
            "stayed_smooth": True,
            "note": "recovery agent probe, run after a VM restart "
                    "killed the first round 3 attempt; "
                    "compute/navier_stokes_hunt_r3_probe.py",
        },
    ],
    "round_2_probe_precedent": "round 2 probe measured 0.83 s per step "
        "at 64^3 but the full run averaged 1.52 s per step, so probes "
        "underestimate full-run cost on this machine",
}


def main():
    nu = 0.001
    dt = 0.01
    final_time = 20.0
    cases = []

    print("round 3 fallback case: kida-pelz-type at 64^3, "
          "final time 20.0", flush=True)
    t0 = time.time()
    u0, v0, w0 = kida_pelz(64)
    result, _series = run_fields(u0, v0, w0, n=64, nu=nu, dt=dt,
                                 final_time=final_time)
    case = summarise(
        "kida-pelz-type-64^3-t20",
        "High-symmetry field u = sin x cos y cos z, "
        "v = cos x sin y cos z, w = -2 cos x cos y sin z. "
        "Longer-time rerun of the round 2 64^3 case at final time "
        "20.0 (2000 steps) instead of 10.0, after the planned 96^3 "
        "run was ruled out by timing probes (see "
        "timing_probe_96_cubed).",
        result)
    cases.append(case)
    print(json.dumps(case, indent=2), flush=True)
    print("case wall time %.2f s" % (time.time() - t0), flush=True)

    best_all = max(cases, key=lambda c: c["max_vorticity_growth_factor"])
    hunt = {
        "date": "2026-11-20",
        "round": 3,
        "protocol": "nu = 0.001, dt = 0.01, final_time = 20.0, "
                    "2000 steps; pseudo-spectral, 2/3 dealiasing, RK4; "
                    "periodic cube [0, 2*pi]^3",
        "cases": cases,
        "timing_probe_96_cubed": TIMING_PROBE_96_CUBED,
        "strongest_signal": {
            "case": best_all["name"],
            "max_vorticity_growth_factor":
                best_all["max_vorticity_growth_factor"],
            "enstrophy_growth_factor":
                best_all["enstrophy_growth_factor"],
            "final_time": best_all["final_time"],
            "stayed_smooth": best_all["stayed_smooth"],
        },
    }
    with open("data/navier_stokes_hunt_r3.json", "w") as fh:
        json.dump(hunt, fh, indent=2)
    print("wrote data/navier_stokes_hunt_r3.json", flush=True)
    return hunt


if __name__ == "__main__":
    main()
