# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Print checks/cases.json, computed with the reference implementation.

Maintainers: uv run checks/make_cases.py > checks/cases.json
verify/check_cases.py fails if the stored file no longer matches this output.
"""

import json
import math
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "reference"))
from stormer_verlet import integrate  # noqa: E402

PROBLEMS = {
    "harmonic": {
        "description": "One coordinate, F(q) = -k*q",
        "force": "F_i(q) = -k_i * q_i",
        "k": [1.0],
        "mass": [1.0],
    },
    "two-mass": {
        "description": "Two uncoupled springs with different masses (catches mixing up momentum and velocity)",
        "force": "F_i(q) = -k_i * q_i",
        "k": [2.0, 8.0],
        "mass": [2.0, 0.5],
    },
    "kepler": {
        "description": "Planar Kepler problem, nonlinear force",
        "force": "F(q) = -q / |q|^3",
        "mass": [1.0, 1.0],
    },
}


def force_for(name):
    if name == "kepler":
        def f(q):
            r3 = (q[0] * q[0] + q[1] * q[1]) ** 1.5
            return (-q[0] / r3, -q[1] / r3)
        return f
    k = PROBLEMS[name]["k"]
    return lambda q: tuple(-ki * qi for ki, qi in zip(k, q))


META = (Path(__file__).resolve().parent.parent / "meta.yaml").read_text()
VERSION = next(line.split(":", 1)[1].strip() for line in META.splitlines() if line.startswith("version:"))

E = 0.5
KEPLER_Q0, KEPLER_P0 = [1.0 - E, 0.0], [0.0, math.sqrt((1.0 + E) / (1.0 - E))]
SPECS = [
    ("harmonic", [1.0], [0.0], 0.1, 20),
    ("two-mass", [1.0, -0.5], [0.3, 0.2], 0.05, 40),
    ("kepler", KEPLER_Q0, KEPLER_P0, 2 * math.pi / 200, 50),
    ("kepler", KEPLER_Q0, KEPLER_P0, -2 * math.pi / 200, 50),
]


def build():
    cases = []
    for scheme in ("kdk", "dkd"):
        for problem, q0, p0, h, n in SPECS:
            _, q, p = integrate(q0, p0, h, n, PROBLEMS[problem]["mass"], force_for(problem), scheme=scheme)[-1]
            cases.append({
                "problem": problem, "scheme": scheme, "q0": q0, "p0": p0, "h": h, "n_steps": n,
                "expected": {"q": list(q), "p": list(p)}, "tol": 1e-12,
            })
    return {
        "format": 1,
        "method": "stormer-verlet",
        "version": VERSION,
        "interface": (
            "integrate(q0, p0, h, n_steps, mass, force, scheme) -> (q, p): advance n_steps fixed steps of "
            "size h (h may be negative) with scheme 'kdk' (velocity Verlet) or 'dkd' (position Verlet) and "
            "return the final state. p is canonical momentum, p_i = mass_i * v_i; force(q) returns F(q)."
        ),
        "comparison": "Pass when every component of q and p differs from expected by at most tol (absolute).",
        "problems": PROBLEMS,
        "cases": cases,
    }


if __name__ == "__main__":
    print(json.dumps(build(), indent=1, ensure_ascii=False))
