# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Print deterministic language-neutral fixtures for the RATTLE step."""

import json
import sys
from pathlib import Path

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

PROBLEMS = {
    "rotor": {"mass": [1, 1], "force": [0, 0], "constraints": "sphere"},
    "pendulum": {"mass": [1, 1], "force": [0, -1], "constraints": "sphere"},
    "intersection": {"mass": [2, 1, 3], "force": [0.2, -0.35, 0.4], "constraints": "sphere-plane"},
}


def build():
    tests = [
        ("free rotor", "rotor", (1, 0), (0, 0.8), 0.3, 1),
        ("pendulum with a normal force component", "pendulum", (0, -1), (0.9, 0), 0.16, 7),
        ("coupled constraints and unequal masses", "intersection", (1, 0, 0), (0, 0.7, 0), 0.17, 6),
        ("signed reverse step", "intersection", (1, 0, 0), (0, 0.7, 0), -0.13, 1),
    ]
    return {
        "format": 1, "method": "rattle", "version": "0.1.0",
        "interface": ("step(q, v, h, mass, force, constraints, gradients) returns [q_next, v_next]. "
                      "Use velocities v, positive diagonal masses and signed h. "
                      "Each test repeats the step the indicated number of times. Compare components in absolute tolerance."),
        "constraint_formulas": {
            "sphere": "g(q) = [sum(q_i^2)-1], with row [2*q_i].",
            "sphere-plane": "g(q) = [x^2+y^2+z^2-1, x+z-1], with rows [2*x,2*y,2*z] and [1,0,1].",
        },
        "problems": PROBLEMS,
        "cases": [
            {"name": name, "problem": problem, "q": q, "v": v, "h": h, "steps": n,
             "expected": advance(step, q, v, h, n, PROBLEMS[problem]), "tol": 4e-12}
            for name, problem, q, v, h, n in tests
        ],
    }


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