Skip to content

Support nlstep_data on the DAE and multistep stage paths - #4264

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix-nlstep-data-dae-4127
Aug 16, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix-nlstep-data-dae-4127

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Aug 14, 2026 •

Copy link
Copy Markdown
Member

Please ignore this PR until it has been reviewed by @ChrisRackauckas. It is a draft and should not be merged before then.

Rebased onto 3d2933057 (#4279). That PR made NonlinearSolveAlg solve DAEProblems and independently fixed four defects this branch previously carried — the daenlf/oopdaenlf argument mismatches, the unallocated dz, and the initialize! parameter tuple. Those are dropped here. #4127 is fixed by #4279, not by this PR; an earlier revision of this description claimed the issue stayed open, which is no longer true.

What remains is the part #4279 did not touch: nlstep_data on the DAE and multistep stage paths. It adds zero lines mentioning nlstep_data, and master's branch still has only the multistep and DIRK arms.

The three defects

1. nlstep_data is read as a field off any AbstractSciMLFunction. Only ODEFunction, SplitFunction and (since SciMLBase 3.44) DAEFunction have it, so a DynamicalODEFunction threw before the first step:

FieldError: type DynamicalODEFunction has no field `nlstep_data`
  build_nlsolver(...) @ utils.jl:839

Looked up the way get_mass_matrix already does for DiscreteFunction. The check constant-folds on a concrete f, so the branch selecting prob stays resolved at compile time.

2. A DAEFunction carrying nlstep_data had no stage assignment. Both arms state the stage system in mass-matrix ODE conventions, so a fully implicit problem took the DIRK arm and solved the wrong system. _compute_rhs! states the fully implicit stage as F(du, u, p, t) at du = (tmp + α z) * invγdt and u = get_dae_uprev(integrator, uprev) + z, so the coefficient belongs on the derivative argument and the state argument carries the predictor base. There is no M z term, so γ₃ stays 1.

3. The multistep stage residual is inflated by α * invγdt. tmp + f(z) - (α * invγdt) * M * z is O(z / dt), but this branch feeds the raw residual to calculate_residuals! as its convergence measure. At small dt it reads as divergence, the step is rejected, dt shrinks, the residual grows, and the solver walks to dtmin. Scaled so the M z coefficient is 1, matching the DIRK arm — which only escaped this because its assignment omits the invγdt factor. The root is unchanged.

The bit to review hardest

Defect 2's predictor base does not announce itself. get_dae_uprev(integrator, uprev) returns cache.u₀ when the cache defines one — as BDF's does, since a multistep predictor is not uprev. Using uprev still converges: it does not error, it solves a slightly wrong stage system and reports Success.

Robertson index-1, tspan = (0, 1e2), abstol = reltol = 1e-10:

no DAE arm at all (master):  Success  [0.0, 1.0]     ← the initial condition
with uprev:                  Success  [6.153424605628343e-6, 0.6172329005992305]
with the predictor base:     Success  [6.153591276834092e-6, 0.6172348824868119]
plain DFBDF (reference):     Success  [6.153591274602223e-6, 0.6172348823018348]

Four orders of accuracy between the middle two, with the same retcode either way.

Verification

Julia 1.12.4, SciMLBase 3.47.0, on 3d2933057.

Multistep, D(y) ~ -y^3 - y from y = 1.0, tspan = (0, 1):

FBDF()                                    Success  0.2694046833972673       (reference)
FBDF(nlsolve=NonlinearSolveAlg())         Success  0.2694046833972673       (no nlstep: unaffected)
FBDF(nlsolve=NonlinearSolveAlg()) nlstep  Success  0.26940468363541614
  before this PR:                         Unstable 0.9999999999500001       ← the initial condition

End to end, all four NonlinearSolveAlg DAE/ODE configurations on this branch:

#4127's own reproducer (raw DAEFunction)  Success  [0.617234888799185, 6.153591429556399e-6, ...]
MTK DAEProblem, no nlstep                 Success  [6.153591276836758e-6, 0.6172348824869219]
MTK DAEProblem, nlstep = true             Success  [6.153591276834092e-6, 0.6172348824868119]
ODE nlstep regression                     Success  maxdiff vs plain FBDF 2.38e-10

Tests — new lib/OrdinaryDiffEqNonlinearSolve/test/nsa_nlstep_data_field_tests.jl, registered in that sublibrary's runtests.jl. The stage systems are hand-rolled so this does not add a ModelingToolkit dependency. Run as julia --project=env <file> > log 2>&1; echo "EXIT=$?" — exit 0, 10 tests, no failures:

Test Summary:                               | Pass  Total     Time
DynamicalODEFunction carries no nlstep_data |    3      3  2m29.6s
Test Summary:                                                      | Pass  Total  Time
DAEProblem with nlstep_data solves the fully implicit stage system |    4      4   7.1s
Test Summary:                                                          | Pass  Total  Time
COEFFICIENT_MULTISTEP nlstep stage residual is on the solution's scale |    3      3   4.5s

Each assertion has a demonstrated failing-before state. The DAE and multistep testsets both assert ≉ u0, because "sitting at the initial condition" was the signature of both bugs and a tolerance check alone can miss it; the DAE case is checked against the closed form u₁ = 1/2 + exp(-2t)/2 rather than a reference solve.

This changes ODE behaviour, deliberately

Fixing defect 3 is the point, and it turns an Unstable result into a correct one. NonlinearSolveAlg without nlstep_data is unaffected — verified byte-identical to plain FBDF above.

Not verified

  • The full OrdinaryDiffEqNonlinearSolve suite was not run to completion locally; only the NonlinearSolveAlg paths above.
  • No GPU or downstream jobs.
  • Only DFBDF, FBDF and ImplicitEuler were exercised. Other algorithms reach the same branch but are untested here.

🤖 Generated with Claude Code

@ChrisRackauckas-Claude ChrisRackauckas-Claude changed the title Look nlstep_data up by field, and refuse it on DAEProblems Make NonlinearSolveAlg work on DAEProblems carrying nlstep_data Aug 15, 2026
SciML#4279 made `NonlinearSolveAlg` solve `DAEProblem`s, but left the
`nlstep_data` branch untouched. Three defects remain on that branch.

**`nlstep_data` is read as a field off any `AbstractSciMLFunction`.** Only
`ODEFunction`, `SplitFunction` and (since SciMLBase 3.44) `DAEFunction` have
it, so a `DynamicalODEFunction` threw before the first step:

    FieldError: type DynamicalODEFunction has no field `nlstep_data`
      build_nlsolver(...) @ utils.jl:839

Look it up the way `get_mass_matrix` already does for `DiscreteFunction`; the
check constant-folds on a concrete `f`, so the branch selecting `prob` stays
resolved at compile time.

**A `DAEFunction` carrying `nlstep_data` had no stage assignment.** Both arms
state the stage system in mass-matrix ODE conventions, so a fully implicit
problem took the `DIRK` arm and solved the wrong system, returning `Success`
at the initial condition. `_compute_rhs!` states the fully implicit stage as
`F(du, u, p, t)` at `du = (tmp + α z) * invγdt` and
`u = get_dae_uprev(integrator, uprev) + z`, so the coefficient belongs on the
derivative argument and the state argument carries the predictor base --
`cache.u₀` for a multistep method, not `uprev`. There is no `M z` term, so
`γ₃` stays 1.

The predictor base is worth a second look on review: using `uprev` still
converges, so it does not fail, it just solves a slightly wrong stage system.

    DAEProblem(robsys, du0, (0, 1e2); nlstep = true),
    DFBDF(nlsolve = NonlinearSolveAlg()), abstol = reltol = 1e-10
      no DAE arm at all:  Success  [0.0, 1.0]   (the initial condition)
      with uprev:         Success  [6.153424605628343e-6, 0.6172329005992305]
      with the predictor: Success  [6.153591276834092e-6, 0.6172348824868119]
      plain DFBDF:        Success  [6.153591274602223e-6, 0.6172348823018348]

**The multistep stage residual is inflated by `α * invγdt`.**
`tmp + f(z) - (α * invγdt) * M * z` is `O(z / dt)`, but this branch feeds the
raw residual to `calculate_residuals!` as its convergence measure. At small
`dt` it reads as divergence, the step is rejected, `dt` shrinks, the residual
grows, and the solver walks to `dtmin`. Scale the stage system so the `M z`
coefficient is 1, matching the `DIRK` arm -- which only escaped this because
its assignment omits the `invγdt` factor. The root is unchanged.

    ODEProblem(sys; nlstep = true), FBDF(nlsolve = NonlinearSolveAlg())
    on D(y) ~ -y^3 - y from y = 1.0, tspan (0, 1), abstol = reltol = 1e-10
      before: Unstable  0.9999999999500001   (the initial condition)
      after:  Success   0.26940468363541614
      plain FBDF:       0.2694046833972673

`NonlinearSolveAlg` without `nlstep_data` is untouched: verified byte-identical
to plain `FBDF` on the case above.

Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas-Claude ChrisRackauckas-Claude changed the title Make NonlinearSolveAlg work on DAEProblems carrying nlstep_data Support nlstep_data on the DAE and multistep stage paths Aug 15, 2026
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review August 16, 2026 03:54
@ChrisRackauckas
ChrisRackauckas merged commit 523c650 into SciML:master Aug 16, 2026
128 of 172 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