Skip to content

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

Merged
ChrisRackauckas merged 9 commits into
SciML:masterfrom
ChrisRackauckas-Claude:sdirk-inline-stages
May 25, 2026
Merged

ChrisRackauckas merged 9 commits into
SciML:masterfrom
ChrisRackauckas-Claude:sdirk-inline-stages

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Summary

  • Rewrites the @generated _perform_step_iip! / _perform_step_oop! / calculate_error_estimate! for the generic ESDIRK/IMEX :standard / :ie_dd2 flavors into hand-written non-generated functions in OrdinaryDiffEqSDIRK/src/generic_imex_perform_step.jl. Drops the S (stage count) type parameter from ESDIRKIMEXTableau and replaces the per-stage logic with an if s >= i ... end ladder for i ∈ 2..MAX_ESDIRKIMEX_STAGES (=12), with each stage's fused-coefficient broadcast hand-written. Tableaus with s > MAX_ESDIRKIMEX_STAGES throw at construction.
  • The @generated approach introduced in Use @generated perform_step! to eliminate per-step regression #3643 has known issues with compilation caching across Julia upgrades — the generated body has to be re-built whenever the world view changes, which interacts poorly with precompile caches. The new shape compiles once per (alg type, T, T2, E) tuple — stage count is now purely runtime, so a single compiled body services every tableau of a given flavor.
  • :trap_dd3 keeps its hand-written specialization unchanged (just drops S from the type signature); :standard and :ie_dd2 share a body that branches on E === :ie_dd2 (constant-folded at the compiler since E is a type parameter).

Please ignore until reviewed by @ChrisRackauckas.

Test plan

  • Local Pkg.test() on OrdinaryDiffEqSDIRK passes (tableau consistency, stage predictors, convergence, DAE)
  • Full CI is green

🤖 Generated with Claude Code

ChrisRackauckas and others added 9 commits May 25, 2026 09:32
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]>
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]>
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]>
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]>
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]>
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]>
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]>
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]>
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]>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review May 25, 2026 20:10
@ChrisRackauckas
ChrisRackauckas merged commit 7773f64 into SciML:master May 25, 2026
92 of 100 checks passed
ChrisRackauckas added a commit that referenced this pull request May 25, 2026
- OrdinaryDiffEqSDIRK 2.6.0 -> 2.7.0: inline ESDIRK/IMEX perform_step
  stages, drop @generated and S type-param (#3676)
- OrdinaryDiffEqRosenbrock 2.2.0 -> 2.3.0: new Rodas4PW W-method,
  derivative_utils fix for issue #3633 (#3672); pin
  OrdinaryDiffEqRosenbrockTableaus compat to "2.1"
- OrdinaryDiffEqRosenbrockTableaus 2.0.0 -> 2.1.0: Rodas4PWTableau (#3672)
- OrdinaryDiffEqDifferentiation 3.1.1 -> 3.2.0: derivative_utils changes
  per issue #3633 (#3672)

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
ChrisRackauckas added a commit that referenced this pull request May 26, 2026
`test/regression/ode_adaptive_tests.jl` ran in the `Regression_I` CI
group but the group is only triggered on PRs, not on master pushes —
so the failing assertions in #3669 sat latent for a long time.

Two distinct causes:

* **ESDIRK659L2SA failures (linear `@test_broken < 10`, lorenz
  `< 1000`)**: caused by #3662, which fixed the embedded `btilde`
  coefficients so adaptive solves actually work. Before #3662, the
  estimator was wrong: `prob_ode_linear` took 3554 steps and
  `prob_lorenz` MaxIters'd at 999386 steps. After #3662, both succeed
  but at counts that breach the old guards (5 steps and 1146 steps
  respectively). #3676 already flipped the linear `@test_broken` to
  `@test`; this PR bumps the lorenz guard to `< 1200`.

* **ESDIRK547L2SA2 lorenz `< 1000` failure**: NOT from #3662. Checked
  against commits before #3662 (`c4a8a493e~1`: 1010 steps) and before
  the March 2026 IMEX-tableau migration (`626639322~1`: 1007 steps) —
  the bound has been breached for months. ESDIRK547L2SA2's `< 1000`
  was set when the method was added in Feb 2023 (`4b56c4ef3`); the
  algorithm has since drifted to ~1010 across the 2026 SDIRK/nlsolver
  refactors. Bumping to `< 1100` leaves comfortable headroom.

Verified locally — both solves return `Success`, step counts are 1010
and 1146 on the current master.

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
ChrisRackauckas added a commit that referenced this pull request May 27, 2026
…3690)

The OOP path of `_perform_step` in `generic_imex_perform_step.jl` sets
`nlsolver.c = c[1]` before the stage-1 `nlsolve!` (line 1404); the IIP
path was missing this assignment. `nlsolver.c` determines
`cache.tstep = integrator.t + nlsolver.c * dt` inside the Newton solver,
which is the time argument passed to `f` during the implicit solve.

Without this assignment, the IIP path's stage-1 implicit solve carried
over the stale `c` value left behind by the previous outer step's
stage 2 (= `c[2]`). For `SDIRK2` (`c = [1, 0]`), this means stage 1 ran
at `t` instead of `t + dt`, and the cancellation in the SDIRK2
stage-2 algebra reduces both stages to the same fixed point
`zs[1] == zs[2] == dt*f(t, uprev)`. The embedded error estimate
`btilde[1]*zs[1] + btilde[2]*zs[2] = (zs[1] - zs[2])/2` then collapses
to 0, letting the PI controller grow `dt` unboundedly.

Reproduces as `DimensionMismatch` in
`lib/DelayDiffEq/test/interface/multi_algorithm.jl`'s `Constant delays`
testset: iip solves of `prob_dde_constant_2delays_ip` produce 11
timesteps while scalar solves produce 36 (and the iip trajectory is
visibly wrong — `u` jumps linearly between discontinuity tstops).

Regression bisected to PR #3641 (commit 99187d0, 2026-05-17). That
commit dropped `nlsolver.c = c[1]` from both IIP and OOP. A follow-up
restored it in OOP only; the @generated rewrite in #3643 and the
inline in #3676 preserved the asymmetry.

After this patch the `Constant delays` testset goes 27/27 green for
all nine algorithms (BS3, Tsit5, RK4, Vern6, SDIRK2, TRBDF2, KenCarp4,
Rosenbrock23, Rodas4). SDIRK2 sol.errors[:l∞] = 0.0035, well under
the 3.8e-1 threshold. No other algorithm's iip-vs-scalar step counts
or l∞ errors changed.

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.

2 participants