Skip to content

Pass the integrator and the stage buffer to stage limiters - #4356

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
singhharsh1708:fix-stage-limiter-args
Aug 26, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
singhharsh1708:fix-stage-limiter-args

Conversation

@singhharsh1708

@singhharsh1708 singhharsh1708 commented Aug 23, 2026 •

Copy link
Copy Markdown
Member

Three explicit RK methods pass the wrong object to a user-supplied stage limiter.
The limiter contract is limiter!(u, integrator, p, t)
(OrdinaryDiffEqCore.trivial_limiter!), and all four arguments are untyped, so a
wrong one type-checks and the default no-op limiter hides it.

What is wrong

Tsit5 (in-place) passes the ODEFunction where the integrator belongs, at five
of its six stages:

stage_limiter!(tmp, f, p, t + c1 * dt)      # arg 2 should be `integrator`

The sixth call in the same method uses integrator, so this is not a local
convention. Any limiter that reads the integrator throws type ODEFunction has no field ... on Tsit5 but works on RK4.

Midpoint (in-place) passes the stage buffer as the integrator and limits the
wrong array:

@.. tmp = uprev + halfdt * fsalfirst
stage_limiter!(k, tmp, p, t + halfdt)       # limits `k`, hands over `tmp`
f(k, tmp, p, t + halfdt)                    # overwrites `k`; `tmp` was never limited

The limiter's writes land in k, which the next line overwrites, so the midpoint
stage is never limited at all.

QPRK98 (in-place) hands uprev to the limiter at stage 5, where the other 14
stages pass tmp:

@.. tmp = uprev + dt * (b51 * k1 + b53 * k3 + b54 * k4)
stage_limiter!(uprev, integrator, p, t + d5 * dt)
f(k5, tmp, p, t + d5 * dt)

The limiter mutates what it is given, so this writes into the previous step's
value, which every later stage and the step update then read. Stage 16 likewise
limits u while f consumes tmp.

Effect

Limiters that only clamp u and ignore their other arguments — the common case,
and what the existing tests use — are unaffected on Tsit5. On Midpoint the
stage limiter is a no-op regardless. On QPRK98, an ordinary positivity limiter
corrupts uprev: on u' = u, u0 = [-1.0], one step of dt = 0.1, uprev
changes from -1.0 to 0.0 partway through the step.

What this changes for users

A limiter that only clamps u and ignores its other arguments -- the common case --
sees no difference on Tsit5, because the object in the wrong slot was never used.

Midpoint and QPRK98 do change. Midpoint's stage limiter was a no-op and now
runs, so a solve that passes one will give different numbers; those numbers are the
limited ones it was asking for. QPRK98 stops writing into uprev, which is a
straight correctness fix -- any limiter that mutated its argument was corrupting the
step.

Testing

test/Integrators_I/step_limiter_test.jl gains three testsets. The existing
limiters there ignore their arguments, which is why this was not caught; the new
ones assert which objects are passed:

  • the second argument is a DEIntegrator, over 27 stage-limiter-supporting methods
  • the limiter is never handed integrator.uprev, same set
  • uprev holds one value across a single QPRK98 step

Each fails on master and passes here: the first reports Midpoint, Tsit5, the
second reports QPRK98, and the third sees uprev take both -1.0 and 0.0.

Bumps OrdinaryDiffEqTsit5, OrdinaryDiffEqLowOrderRK and OrdinaryDiffEqQPRK
one patch from master.

#4035 also edits test/Integrators_I/step_limiter_test.jl. It is a different bug --
refreshing FSAL after solve-level step limiters, where this is about which
arguments the stage limiters receive -- but whichever lands second will need a
rebase in that file. No version overlap: #4035 bumps Core and ExponentialRK.

AI Disclosure

Claude assisted with this change.

@singhharsh1708
singhharsh1708 force-pushed the fix-stage-limiter-args branch from 1d8e0e7 to f2dd8c9 Compare August 24, 2026 07:43
@ChrisRackauckas
ChrisRackauckas merged commit 53cc394 into SciML:master Aug 26, 2026
120 of 154 checks passed
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