# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Print reproducible BDF2 fixtures."""

import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "reference"))
from bdf2 import start, step
from check_port import advance


def build():
    inputs = [
        ("implicit startup only", "manufactured", 0.4, (0.6,), 0.31, 1, None),
        ("history and endpoint time", "manufactured", 0.5, (0.6,), 0.17, 1, (0.48,)),
        ("coupled Jacobian", "coupled", 0.3, (0.4, -0.7), 0.12, 6, None),
        ("nonlinear implicit residual", "nonlinear", 0.3, (0.4,), 0.17, 4, None),
        ("stiff damping with startup", "fast", 0, (1,), 0.1, 3, None),
    ]
    return {
        "format": 1, "method": "bdf2", "version": "0.1.0",
        "interface": ("start(f,J,t,y,h) returns one backward Euler step. "
                      "step(f,J,t,previous,current,h) advances from t to t+h, with previous at t-h. "
                      "When previous is absent, start on the first step; otherwise use BDF2 from the first step."),
        "problems": {
            "manufactured": "f(t,y)=[cos(t)-y[0]+sin(t)], J=[[-1]].",
            "coupled": "f(t,y)=[-4*y[0]+2*y[1]+t,-y[0]-3*y[1]+t^2], J=[[-4,2],[-1,-3]].",
            "nonlinear": "f(t,y)=[t-y[0]^2], J=[[-2*y[0]]].",
            "fast": "f(t,y)=[-1e6*y[0]], J=[[-1e6]].",
        },
        "cases": [
            {"name": name, "problem": problem, "t": t, "y": y, "h": h, "steps": n,
             **({"previous": prev} if prev is not None else {}),
             "expected": advance(start, step, problem, t, y, h, n, prev), "tol": 2e-12}
            for name, problem, t, y, h, n, prev in inputs
        ],
    }


if __name__ == "__main__":
    print(json.dumps(build(), indent=2))
