Skip to content

Project · Adding a solver

Adding a solver

A new integration method is a two-sided contribution: the real kernel lives in Rust (in the tsdyn-solvers crate), and a small SolverSpec mirrors it in the Python registry so users can select it with system.run(solver="..."). Registering Python metadata alone does not supply an algorithm. For installed packages and aliases of existing kernels, start with Plugins & extensions.

This is an engine change

Unlike adding a system — pure Python, no build step — a solver touches the Rust engine, so you need a Rust toolchain and the editable dev install (uv sync --group dev). The reference backend is a SciPy oracle that does not run our kernels; test a new kernel on the engine backends — backend="jit" (the default) and backend="interp", which must agree bit-for-bit.

The mental model

The engine owns the integration loop; a kernel owns a single step. Deterministic ODE kernels implement the Solver trait (crates/tsdyn-solvers/src/solver.rs):

Rust
pub trait Solver: Send {
    fn name(&self) -> &'static str;
    fn caps(&self) -> Caps;
    fn step(&mut self, ev: &dyn Evaluator, st: &mut SolverState, h: f64) -> StepOutcome;
}

Kernels self-register at link time via the register_solver! macro (inventory-based — there is no central kernel table), and both the SSA interpreter and the Cranelift JIT back the same Evaluator trait, so a kernel is shared by both evaluation backends. Verify their numerical agreement in tests; registration alone does not prove a kernel correct.

The DDE method-of-steps driver can reuse explicit ODE kernels; it does not support implicit kernels. SDE kernels have a separate SdeKernel trait and register_sde_kernel! registration in crates/tsdyn-solvers/src/sde/.

The fast inner loop

Iterate against the pure-Rust crate — no Python, no maturin — while you get the tableau right:

Bash
cargo test --manifest-path crates/Cargo.toml -p tsdyn-solvers --locked

The stale-extension gotcha

uv run pytest does not auto-rebuild the Rust extension on a pure-Rust edit — it reuses the stale src/tsdynamics/_rust.abi3.so, and your change silently has no effect. After editing any Rust file, force a rebuild:

Bash
uv sync --locked --group dev --reinstall-package tsdynamics
uv run --no-sync pytest --no-cov tests/test_solvers.py tests/test_engine_wire.py

Look for the Building tsdynamics line. --locked prevents an unintended dependency change; investigate a lock mismatch rather than discarding edits to make the command pass. Add your kernel's own accuracy tests to the focused test command, then run the normal contribution gates.

Writing the kernel

Two shared control layers do almost all the work — you supply a Butcher tableau (or a one-step formula) and a thin Solver impl.

The shared explicit/control.rs implements the step machinery; you provide the tableau constants.

  • Fixed-step: tableau C / A / B + a thin Solver calling fixed_step(...). It builds through the registry's default factory — no build_solver change.
  • Adaptive (single embedded error vector): tableau C / A / B / E
  • an err_exponent (= −1 / (est_order + 1)) + a with_tolerances constructor + adaptive_step(...). (dop853 is the exception — it blends its own error estimate in a custom step().)
  • Wire it up: add mod your_kernel; and pub use to crates/tsdyn-solvers/src/explicit/mod.rs.

The registration lives in the kernel's own always-compiled module — copy the pattern from explicit/rk45.rs:

Rust
register_solver!(
    "your_kernel",
    Caps::explicit(ProblemKinds::of(ProblemKind::Ode)).adaptive(),
    || Box::new(YourKernel::new())
);

The shared implicit/control.rs supplies adaptive error control via step-doubling + Richardson extrapolation: implement the BaseStep trait (the one-step formula + order()), and DoublingWork::doubled_step handles the rest.

  • Linear algebra is in implicit/linalg.rs (build_shifted for \(I - c\,h\,J\), plus lu_factor / lu_solve); the modified-Newton substage solver and wrms norm are in implicit/newton.rs.
  • Every implicit kernel needs the analytic Jacobian, so Caps::implicit defaults needs_jacobian = true — the engine lowers the tape with_jacobian=true automatically for it.
  • Wire it up: add mod your_kernel; and pub use to crates/tsdyn-solvers/src/implicit/mod.rs, and register_solver! with Caps::implicit(...).

The one central table

There is exactly one hardcoded table, and it is easy to miss. An adaptive kernel that owns its own rtol / atol must get an arm in build_solver (crates/tsdyn-core/src/bridge/marshal.rs), or its tolerances are silently ignored (it falls through to the default-tolerance registry factory):

Rust
// crates/tsdyn-core/src/bridge/marshal.rs
pub fn build_solver(name: &'static str, tol: Tolerances) -> Box<dyn Solver> {
    let (rtol, atol) = (tol.rtol(), tol.atol());
    match name {
        "rk45"   => Box::new(Rk45::with_tolerances(rtol, atol)),
        // ... one arm per tolerance-owning adaptive kernel ...
        "your_kernel" => Box::new(YourKernel::with_tolerances(rtol, atol)),
        // Fixed-step + Adams multistep + out-of-tree plugins build through the
        // registry factory (no arm needed):
        other    => tsdyn_solvers::make(other).expect("resolve_solver guarantees this"),
    }
}

Also add the concrete type to the use tsdyn_solvers::explicit::{...} (or implicit::{...}) import at the top of that file. Fixed-step kernels need no arm — they carry no tolerances.

Mirroring it in Python

Add a SolverSpec — the Python mirror of the Rust kernel — to the matching dict in src/tsdynamics/_solvers/ (explicit.py, implicit.py, or stochastic.py). The capability flags must match the Rust Caps:

Python
# skip-doctest — a template: registering a placeholder kernel would put
# "your_kernel" in the solver table for the rest of the session
from tsdynamics._solvers import SolverSpec, SolverCaps, register

register(
    SolverSpec(
        name="your_kernel",
        caps=SolverCaps(
            kind="explicit",        # or "implicit"
            adaptive=True,
            needs_jacobian=False,   # True for implicit kernels
            supports=frozenset({"ode"}),
        ),
        description="Your Kernel N(M) adaptive — one-line human description",
    )
)

Optional friendly aliases go in select.py::_ALIASES (keys are normalised — lowercased, whitespace / - → _ — so "RK45", "dopri5", and "rk-45" all resolve). The runtime resolves the Python name and sends SolverSpec.kernel to the compiled engine. A new method therefore becomes runnable when both its Rust implementation and Python declaration are included in the rebuilt extension and package. Use system.run(solver="your_kernel"); method= is the internal engine spelling. The reference backend delegates to SciPy and cannot exercise an arbitrary new Rust algorithm.

For an SDE method, also update the Python family's method resolution and reference/incremental step implementation in families/stochastic.py. That family currently implements Euler–Maruyama and Milstein; a SolverSpec alone does not teach it a third stochastic scheme.

Tests to update

  • tests/test_solvers.py — the _EXPECTED_BUILTINS dict and the test_available_for_filters_by_family exact-set (==) assertions break on a new solver; update them. test_python_specs_mirror_rust_register_macros parses the Rust source (a regex over register_solver! / Caps::implicit / needs_jacobian) and stays green as long as your Python spec matches the Rust Caps.
  • Rust cross-validation — crates/tsdyn-solvers/tests/explicit_accuracy.rs and implicit_stiff.rs are the strong oracle for tableau correctness: they assert cross-method agreement on Lorenz / stiff reference problems. Add analytic accuracy or convergence tests appropriate to the new method; cross-method agreement alone can miss shared errors.

The invariants your kernel must hold

Invariant Why
interp == jit bit-for-bit Both backends drive the same &mut dyn Solver through the Evaluator trait
Python Caps == Rust Caps test_python_specs_mirror_rust_register_macros gates it
Adaptive kernel → build_solver arm Otherwise rtol / atol are silently ignored
Cites the original paper Never a competing library — a hard rule in docstrings and docs

See also