Skip to content

Add generic stage-predictor menu for ESDIRK methods - #3668

Merged
ChrisRackauckas merged 5 commits into
SciML:masterfrom
singhharsh1708:sdirk-stage-predictors
May 25, 2026
Merged

ChrisRackauckas merged 5 commits into
SciML:masterfrom
singhharsh1708:sdirk-stage-predictors

Conversation

@singhharsh1708

@singhharsh1708 singhharsh1708 commented May 22, 2026 •

Copy link
Copy Markdown
Member

Closes #3658.

Adds a predictor keyword to the SDIRK/ESDIRK methods so you can pick how the per-stage Newton initial guess is formed. The SUNDIALS predictors are all there as generic options, plus the existing tableau predictors, plus a couple of stage-extrapolation ones.

Options:

  • :trivial — z=0, same as before (empty α)
  • :max_order / :variable_order / :cutoff_order — the SUNDIALS interpolant predictors (previous-step Hermite, evaluated at the stage time)
  • :tableau — the tuned α predictors KenCarp/Kvaerno/etc already carry
  • :copy_prev — reuse the last stage's derivative
  • :stage_extrap — extrapolate the last two stage derivatives

For :variable_order/:cutoff_order I needed an order-limited interpolant, which the dense output doesn't expose. Rewrote the Hermite cubic in power form (y0 + Θ·dt·k1 + Θ²·... + Θ³·...) so it can be truncated to order q; q=3 gives back the full Hermite exactly.

Are the custom ones better?

Chris asked this. Short answer: yes. I benchmarked the whole menu on vdp (μ=1, 1e3, 1e6), Robertson, pollution and oregonator against the 5 ESDIRK*L2SA methods (which had no predictor before).

The SUNDIALS interpolant predictors mostly don't help here and :max_order can actually make stiff solves worse — the trivial guess already uses the accumulated stage value, so extrapolating the old step on top of it overshoots. The stage-derivative ones win instead. :stage_extrap is the best and never blows up (I also tried quadratic and full-Lagrange versions, but those reintroduce a nasty outlier on Robertson, so I kept the 2-point form).

Default change

Switched the ESDIRK*L2SA family default from trivial to :stage_extrap. That changes their step counts vs master (fewer). Everything else still defaults to its tableau predictor. Heads up since it's a behavior change — happy to keep the default trivial if you'd rather, but the numbers are pretty convincing:

:trivial here is identical to old behavior. reltol=abstol=1e-8. Total nf 269905 → 177207, about -34%, faster in all 30 cases, accuracy the same or better.

Problem nf reduction (across the 5 methods)
vdp(1) -11% to -36%
vdp(1e3) -18% to -75%
vdp(1e6) -1% to -12%
Robertson -23% to -31%
pollution -21% to -42%
oregonator -32% to -40%
full numbers
prob method before (trivial) after (stage_extrap) nf Δ
vdp(1) ESDIRK54I8L2SA nf=5991 nacc=140 err=6e-08 nf=3828 nacc=140 err=1e-07 -36%
vdp(1) ESDIRK436L2SA2 nf=6554 nacc=224 err=4e-08 nf=4328 nacc=223 err=1e-08 -34%
vdp(1) ESDIRK437L2SA nf=4482 nacc=135 err=2e-07 nf=3445 nacc=134 err=2e-07 -23%
vdp(1) ESDIRK547L2SA2 nf=4577 nacc=121 err=6e-08 nf=3386 nacc=121 err=5e-08 -26%
vdp(1) ESDIRK659L2SA nf=4693 nacc=91 err=2e-08 nf=4160 nacc=92 err=9e-09 -11%
vdp(1e3) ESDIRK54I8L2SA nf=525 nacc=32 err=2e-07 nf=411 nacc=28 err=2e-11 -22%
vdp(1e3) ESDIRK436L2SA2 nf=326 nacc=26 err=8e-11 nf=267 nacc=24 err=3e-12 -18%
vdp(1e3) ESDIRK437L2SA nf=432 nacc=26 err=3e-07 nf=264 nacc=24 err=6e-09 -39%
vdp(1e3) ESDIRK547L2SA2 nf=660 nacc=40 err=1e-07 nf=374 nacc=27 err=3e-12 -43%
vdp(1e3) ESDIRK659L2SA nf=2049 nacc=61 err=4e-09 nf=522 nacc=37 err=5e-09 -75%
vdp(1e6) ESDIRK54I8L2SA nf=93 nacc=9 err=2e-07 nf=85 nacc=8 err=8e-13 -9%
vdp(1e6) ESDIRK436L2SA2 nf=75 nacc=10 err=2e-12 nf=74 nacc=10 err=8e-13 -1%
vdp(1e6) ESDIRK437L2SA nf=273 nacc=15 err=2e-13 nf=253 nacc=12 err=8e-13 -7%
vdp(1e6) ESDIRK547L2SA2 nf=280 nacc=18 err=8e-13 nf=255 nacc=14 err=8e-13 -9%
vdp(1e6) ESDIRK659L2SA nf=598 nacc=20 err=3e-13 nf=526 nacc=14 err=8e-13 -12%
rober ESDIRK54I8L2SA nf=4477 nacc=97 err=3e-10 nf=3236 nacc=109 err=8e-09 -28%
rober ESDIRK436L2SA2 nf=6533 nacc=189 err=2e-09 nf=4536 nacc=184 err=1e-09 -31%
rober ESDIRK437L2SA nf=4908 nacc=121 err=3e-09 nf=3652 nacc=117 err=2e-09 -26%
rober ESDIRK547L2SA2 nf=4755 nacc=117 err=5e-10 nf=3646 nacc=113 err=9e-10 -23%
rober ESDIRK659L2SA nf=5810 nacc=126 err=1e-10 nf=4417 nacc=118 err=3e-10 -24%
pollu ESDIRK54I8L2SA nf=2457 nacc=61 err=9e-09 nf=1471 nacc=72 err=7e-09 -40%
pollu ESDIRK436L2SA2 nf=1691 nacc=56 err=9e-09 nf=989 nacc=55 err=4e-10 -42%
pollu ESDIRK437L2SA nf=1373 nacc=47 err=9e-09 nf=1078 nacc=48 err=2e-08 -21%
pollu ESDIRK547L2SA2 nf=1807 nacc=58 err=2e-09 nf=1361 nacc=58 err=1e-09 -25%
pollu ESDIRK659L2SA nf=2833 nacc=66 err=2e-09 nf=2177 nacc=82 err=3e-09 -23%
orego ESDIRK54I8L2SA nf=36015 nacc=626 err=6e-05 nf=21781 nacc=631 err=5e-05 -40%
orego ESDIRK436L2SA2 nf=37758 nacc=955 err=1e-04 nf=24216 nacc=955 err=5e-05 -36%
orego ESDIRK437L2SA nf=29883 nacc=632 err=8e-05 nf=20297 nacc=633 err=5e-05 -32%
orego ESDIRK547L2SA2 nf=42528 nacc=885 err=1e-04 nf=29001 nacc=887 err=7e-06 -32%
orego ESDIRK659L2SA nf=55469 nacc=866 err=1e-04 nf=33171 nacc=835 err=2e-05 -40%

Tests

Added predictor_tests.jl: checks every predictor on every method gives a finite, accurate result, and that the new default doesn't regress nf vs trivial. Convergence and tableau tests still pass.

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the contributor guidelines, in particular the SciML Style Guide and COLPRAC
  • Any new documentation only uses public API

@singhharsh1708

Copy link
Copy Markdown
Member Author

@ChrisRackauckas

@singhharsh1708

Copy link
Copy Markdown
Member Author

@ChrisRackauckas Addressed all review comments:

  • all SDIRK/ESDIRK methods now carry an explicit predictor field; removed the _predictor fallback
  • added a Predictor enum in OrdinaryDiffEqCore; extrapolant is now deprecated and mapped onto the enum with a depwarn
  • guarded Hermite-specific predictors with _uses_hermite_interp(alg); custom interpolants fall back to current_extrapolant!
  • removed unnecessary @generated usage from the fixed Trapezoid path; only the true stage-unrolling path remains generated
    Full SDIRK suite passes (227 pass / 2 pre-existing broken), untouched methods remain byte-identical to master, and runic is clean.
    Separately, the Regression_I CI failure is pre-existing on master (tracked in #3669).

@singhharsh1708
singhharsh1708 force-pushed the sdirk-stage-predictors branch from 471a61f to 22bebdb Compare May 23, 2026 12:48
@ChrisRackauckas
ChrisRackauckas merged commit 8abd556 into SciML:master May 25, 2026
170 of 213 checks passed
@singhharsh1708
singhharsh1708 deleted the sdirk-stage-predictors branch May 25, 2026 14:08
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/OrdinaryDiffEq.jl that referenced this pull request May 25, 2026
BDF algorithms (ABDF2, QNDF2, SBDF, MEBDF2) reuse SDIRK's
ESDIRKIMEXCache via ImplicitEulerESDIRKIMEXTableau as a starter step.
They eventually hit the SDIRK `_perform_step_iip!` body, which calls
`_predictor(alg) = alg.predictor`. BDF algorithms don't carry a
`predictor` field, so this throws `type ABDF2 has no field predictor` —
which broke `test/integrators/step_limiter_test.jl` (and the
Integrators_I/II CI groups) on master after PR SciML#3668.

Fall back to `Predictor.Trivial` when the field is absent. This matches
the default `extrapolant = :constant` mapping in `_resolve_predictor`,
so BDF algorithms now use a zero-increment guess — the behavior they
had before PR SciML#3668 introduced the predictor menu.

Confirmed locally: `ABDF2(; step_limiter!)` solves the trivial linear
ODE and reports n_accept == step_limiter call count.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/OrdinaryDiffEq.jl that referenced this pull request May 25, 2026
The test `@test_broken length(sol_linear.u) < 10` for
`solve(prob_linear, ESDIRK659L2SA())` now passes (locally and on the
PR's Regression_I CI) because PR SciML#3668's stage-predictor menu
improved the per-stage Newton seed for ESDIRK659L2SA — the linear
problem now converges in 5 steps instead of >10.

The other three ESDIRK methods immediately above
(ESDIRK436L2SA2, ESDIRK437L2SA, ESDIRK547L2SA2) already use plain
`@test length < 10`; this just brings ESDIRK659L2SA into line.

This change is surfaced only because `Regression_I` runs on PRs but
not on the master CI workflow.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
ChrisRackauckas added a commit that referenced this pull request May 25, 2026
…ram (#3676)

* Inline ESDIRK/IMEX perform_step stages; drop @generated and S type-param

Replaces the @generated `_perform_step_iip!` / `_perform_step_oop!` /
`calculate_error_estimate!` for the :standard and :ie_dd2 flavors with
hand-written non-generated functions. The per-stage logic is now a
`if s >= i ... end` ladder for i in 2..MAX_ESDIRKIMEX_STAGES (=12), each
branch carrying a fused-coefficient broadcast accumulator written out
with literal indices.

Drops `S` from `ESDIRKIMEXTableau{T, T2, E}`. Tableaus with
s > MAX_ESDIRKIMEX_STAGES now error at construction. The body compiles
once per (alg type, T, T2, E) — stage count is purely runtime — which
avoids the @generated compilation-cache invalidation issues that
motivated this rewrite. :trap_dd3 keeps its hand-written specialization;
:standard and :ie_dd2 share a body that branches on `E === :ie_dd2`
(constant-folded since E is a type parameter).

Local SDIRK test suite passes (Tableau consistency 47/47, Stage
predictors 88/88, Convergence 84/84 + 2 pre-existing broken,
DAE 8/8).

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Drop pre-allocated zero(u) for OOP z/k locals — lazy assignment

The OOP path was pre-allocating z1..z12 and k1..k12 with zero(u) at the
top of _perform_step_oop! to give Julia static knowledge of the locals.
For algorithms with s < 12 that's 24-2s wasted allocations per solve
(e.g., ImplicitEuler s=1: 22 dead allocations).

Replace the pre-allocation with `local` declarations — Julia still
treats z2..z12 / k1..k12 as function-scope locals, but no allocation
happens until something actually assigns to them. Conditional assignment
inside `if s >= i ... end` ladders covers the cases that fire; output
ladder branches reference only the slots that were filled at runtime.

Net effect on OOP solves (Lotka-Volterra, 100 steps):
- ImplicitEuler: -4% allocations, neutral time
- ESDIRK659L2SA: -8% allocations, -28% time
- SFSDIRK8:      -5% allocations, -31% time
- KenCarp5 OOP:  -5% allocations, -50% time

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Apply Runic formatting

The PR's runic check flagged `broadcast=false` vs `broadcast = false` and
some `Union{...} && ...` indentation. Auto-fixed via `Runic --inplace`.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Make _predictor(alg) tolerate algs without `predictor` field

BDF algorithms (ABDF2, QNDF2, SBDF, MEBDF2) reuse SDIRK's
ESDIRKIMEXCache via ImplicitEulerESDIRKIMEXTableau as a starter step.
They eventually hit the SDIRK `_perform_step_iip!` body, which calls
`_predictor(alg) = alg.predictor`. BDF algorithms don't carry a
`predictor` field, so this throws `type ABDF2 has no field predictor` —
which broke `test/integrators/step_limiter_test.jl` (and the
Integrators_I/II CI groups) on master after PR #3668.

Fall back to `Predictor.Trivial` when the field is absent. This matches
the default `extrapolant = :constant` mapping in `_resolve_predictor`,
so BDF algorithms now use a zero-increment guess — the behavior they
had before PR #3668 introduced the predictor menu.

Confirmed locally: `ABDF2(; step_limiter!)` solves the trivial linear
ODE and reports n_accept == step_limiter call count.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Unbreak ESDIRK659L2SA linear-step regression test

The test `@test_broken length(sol_linear.u) < 10` for
`solve(prob_linear, ESDIRK659L2SA())` now passes (locally and on the
PR's Regression_I CI) because PR #3668's stage-predictor menu
improved the per-stage Newton seed for ESDIRK659L2SA — the linear
problem now converges in 5 steps instead of >10.

The other three ESDIRK methods immediately above
(ESDIRK436L2SA2, ESDIRK437L2SA, ESDIRK547L2SA2) already use plain
`@test length < 10`; this just brings ESDIRK659L2SA into line.

This change is surfaced only because `Regression_I` runs on PRs but
not on the master CI workflow.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* OOP: pre-allocate k_i locals when f is a SplitFunction

When a non-IMEX SDIRK method (Kvaerno3/4/5) is fed a `SplitODEProblem`,
`integrator.f isa SplitFunction` is true but `issplit(alg)` is false.
The stage-1 setup then skips the k1 assignment, but stages 2..s still
enter the `tmp = tmp + Ae[i,j]*kj` accumulator (Ae is all zeros in that
case, but Julia still needs k1 defined to evaluate `Ae[2,1] * k1`).

Master's @generated body papered over this by pre-allocating all k_i =
zero(u) up front; my previous `local k1..k12` declaration dropped the
allocation and tripped `UndefVarError` in
`sdirk_convergence_tests.jl::Kvaerno SplitODEProblem`.

Restore the pre-allocation, but only when `integrator.f isa
SplitFunction` (so the common non-split path still pays nothing for
unused k slots).

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Apply Runic formatting again

The re-assembly after the k_i pre-allocation fix re-introduced
`broadcast=false` (no spaces) and the nested `Union{...}` indentation
runic dislikes. Re-runic'd via `Runic --inplace`.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Make BDF's ABDF2 declare its predictor explicitly, drop fallback

Previous commit added `_predictor(alg) = hasproperty(alg, :predictor) ? alg.predictor : Predictor.Trivial`
to tolerate ABDF2 routing through SDIRK's perform_step via its
`cache.eulercache`. Reviewer pointed out the fallback is implicit
contract: anything reaching SDIRK's perform_step should declare a
predictor.

Revert `_predictor` to plain `alg.predictor` and define a single
explicit override in BDF:

    OrdinaryDiffEqSDIRK._predictor(::ABDF2) = OrdinaryDiffEqCore.Predictor.Trivial

ABDF2 is the only BDF algorithm that reuses SDIRK's `ESDIRKIMEXCache`
(via the `eulercache` field for its first step). `Trivial` matches the
behavior ABDF2 already takes: the SDIRK Newton-seed branches all gate
on `alg isa Union{...SDIRK..., ImplicitEuler, Trapezoid}`, so the menu
falls through to the zero-seed branch regardless.

If a future algorithm adds this pattern, the missing `predictor`
field now surfaces immediately as a `FieldError` instead of being
silently filled with a default.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Restore _predictor hasproperty fallback with a docs-comment about ABDF2

Per review: keep the fallback but document *why* — it's the implicit
contract for downstream algorithms that reuse `ESDIRKIMEXCache` (e.g.
OrdinaryDiffEqBDF's `ABDF2`, which uses the Implicit Euler tableau as a
first-step starter via `cache.eulercache`) without carrying a
`predictor::Predictor.T` field of their own.

Drops the explicit `OrdinaryDiffEqSDIRK._predictor(::ABDF2)` override
from `OrdinaryDiffEqBDF/src/alg_utils.jl` and the corresponding
`import OrdinaryDiffEqSDIRK` — the fallback alone is enough now.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add/improve stage predictors for ESDIRK methods now in tableau form

2 participants