# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Evaluate stiff relaxation at irregular output times."""

from math import exp
from pathlib import Path
import sys

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "reference"))
from radau_iia_5 import solve

f = lambda t, y: (-y[0], -100*y[1])
J = lambda t, y: ((-1, 0), (0, -100))
_, at, accepted, rejected = solve(f, J, 0, (1, 1), 2, h0=1, rtol=1e-7, atol=1e-10)
times = (0.003, 0.019, 0.13, 0.51, 1.64, 2.0)
print("Stiff relaxation at irregular observation times")
print(f"Accepted more than one trial, rejected at least one: {accepted > 1 and rejected > 0}")
for t in times:
    y = at(t)
    err = max(abs(y[0]-exp(-t)), abs(y[1]-exp(-100*t)))
    print(f"t={t:.3f}: slow mode {y[0]:.6f}, absolute error < 1e-6: {err < 1e-6}")
