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

Case 1, Kida-Pelz at 64^3: the round 1 strongest case rerun at higher
resolution, same protocol (nu = 0.001, dt = 0.01, t = 10, 1000 steps).
Round 1 gave enstrophy growth 14.23 at 32^3 and 18.29 at 48^3, so the
64^3 run tests whether the growth keeps rising with resolution or
saturates. A timing probe before the full run measured 0.83 s per step
at 64^3, projecting about 831 s for 1000 steps.

Case 2, high-amplitude anti-parallel vortex tubes at 48^3: the round 1
tube case (gamma = 2.0, core radius 0.5, displacement 0.1) decayed
monotonically with growth factor 1.0, but vortex stretching is
quadratic in vorticity while viscous diffusion is linear, so that null
may be an amplitude threshold rather than intrinsic tameness. Here the
circulation is tripled (gamma = 6.0), the core narrowed to 0.4, and the
Crow-instability seed displacement doubled to 0.2, which raises the
nonlinear stretching roughly ninefold relative to diffusion and sharpens
the collision gradients. This stresses enstrophy growth through a
localised tube collision and reconnection, a different axis from the
global single-mode coupling of Kida-Pelz. Velocity is recovered from
the vorticity by the same Fourier Biot-Savart law as round 1.

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 (  # noqa: E402
    anti_parallel_tubes,
    kida_pelz,
    summarise,
)


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

    print("round 2 case 1: kida-pelz-type at 64^3", 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",
        "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. "
        "Rerun at 64^3.",
        result)
    cases.append(case)
    print(json.dumps(case, indent=2), flush=True)
    print("case 1 wall time %.2f s" % (time.time() - t0), flush=True)

    print("round 2 case 2: anti-parallel-vortex-tubes-high-amplitude "
          "at 48^3", flush=True)
    t0 = time.time()
    u0, v0, w0 = anti_parallel_tubes(48, gamma=6.0, r0=0.4, sep=0.8,
                                     disp=0.2)
    result2, _series2 = run_fields(u0, v0, w0, n=48, nu=nu, dt=dt,
                                   final_time=final_time)
    case2 = summarise(
        "anti-parallel-vortex-tubes-high-amplitude-48^3",
        "Two Gaussian vortex tubes of opposite circulation, gamma = 6.0, "
        "core radius 0.4, separation 1.6, centre-lines displaced by "
        "0.2 sin(y) in opposite directions; velocity from Fourier "
        "Biot-Savart. High-amplitude rerun of the round 1 tube case "
        "(gamma = 2.0, core 0.5, displacement 0.1), run at 48^3.",
        result2)
    cases.append(case2)
    print(json.dumps(case2, indent=2), flush=True)
    print("case 2 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": 2,
        "protocol": "nu = 0.001, dt = 0.01, final_time = 10.0, "
                    "1000 steps; pseudo-spectral, 2/3 dealiasing, RK4; "
                    "periodic cube [0, 2*pi]^3",
        "cases": cases,
        "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_r2.json", "w") as fh:
        json.dump(hunt, fh, indent=2)
    print("wrote data/navier_stokes_hunt_r2.json", flush=True)
    return hunt


if __name__ == "__main__":
    main()
