Skip to content

NonlinearSolveAlg: reassemble W after a rejected step, and retry a stale Jacobian - #4282

Closed
ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:nsa-cf
Closed

ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:nsa-cf

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Please ignore until reviewed by @ChrisRackauckas.

NonlinearSolveAlg's W-reuse gate was missing two of do_newJW's terms, so after an error-test rejection it kept reusing a W that the stage could not converge against.

The mechanism

do_newJW rebuilds W when wbad = … || (isfirststage && errorfail) || …. A dt cut after a rejected step moves γΔt by ~15%, which is inside new_W_dt_cutoff = 1//5, so NSA — which had no errorfail term — reused it. The result is a chord iteration with a mismatched W, converging linearly instead of quadratically and exhausting maxiters = 10:

failing   ndz: 2.56e5 6.56e4 1.68e4 4.33e3 1.11e3 285 73.3 18.8 4.84 1.25   (ratio 0.2564 every step)
converged ndz: 1.14e5 82.6 0.14 0.000278                                     (quadratic)

Every failure was :exhausted at maxiters, all on the TRBDF2 trapezoid stage, all with cache.new_W == false. No divergence test ever fired. NLNewton hits the same exhaustion but recovers in-step via TryAgain → @goto REDO; NSA was excluded from that by the isnewton(nlsolver) gate.

Confirmed from the outside using only public knobs, with no source patch: tightening new_W_dt_cutoff below the failing drift band, or raising max_iter, both drive the failures to zero.

Bruss1D  new_W_dt_cutoff 1//5 -> ncf=112 ;  1//10 -> ncf=0
Bruss1D  max_iter 10 -> ncf=112 ; 15 -> ncf=2 ; 20 -> ncf=0

And the decisive negative control on the other solver — disabling (isfirststage && errorfail) in NLNewton's own do_newJW reproduces NSA's behaviour:

Bruss1D nf / njacs ROBER nf / njacs
NLNewton 9463 / 17 426618 / 10
NLNewton, errorfail disabled 17398 / 117 558039 / 152
NSA before 16382 / 113 584833 / 136

Both terms are needed. errorfail alone cuts convergence failures but causes a rejection explosion (ROBER/TRBDF2 nreject 3763 → 39533); the retry alone leaves 128 failures. Together they land on NLNewton's count exactly.

Effect

120 configurations (5 problems × 8 integrators × rtol 1e-6/1e-9/1e-12), 111 where all three solvers succeed:

NSA / NLNewton nf median mean ≤1.10 max
before 1.036 1.247 74/111 12.36
after 1.001 1.004 105/111 1.28

Total nnonlinconvfail 85768 → 337 (NLNewton: 326). Total njacs 85879 → 5026 (NLNewton 4858).

It removes a silent wrong answer. ROBER + Kvaerno5 at rtol 1e-6, over 10 one-ULP perturbations of u0:

before: 6/10 return ReturnCode.Success with end-point error ~1.6e+07
after:  0/10  (errors 4.4e-12 … 2.7e-10)

The largest work wins are on Trapezoid, not the configurations that motivated the investigation: ROBER/Trapezoid@1e-6 was 12.4× NLNewton with 16196 convergence failures and is now 1.00× with 15; VdPstiff/Trapezoid@1e-12 was 107× and MaxIters and is now 0.97× and Success; Bruss1D/Trapezoid@1e-12 3.79× → 1.00×. Mass-matrix, sparse and matrix-free paths all drop to NLNewton's convergence-failure counts.

What this is not

It is a parity change, not an improvement over NLNewton. NSA now reproduces NLNewton's step sequence, and therefore its error — which on some configurations is larger than what NSA produced before, because master was taking a different (accidentally luckier) trajectory. On ROBER/TRBDF2, pooled over 64 samples across four perturbation axes, the error ratio to NLNewton is ≈1.00 while the ratio to master is 1.99. Every one of those errors is 2–5 orders of magnitude below the requested tolerance, so this was never an accuracy defect in either direction.

I am deliberately not quoting single-point numbers for ROBER/TRBDF2: rtol = 1e-9 exactly is a lucky point for NLNewton there (nf 426618, against ~600–675k at 13 of 14 neighbouring rtol values), so improvements measured at that point do not generalize. What survives every axis is fix/NLNewton ≈ 1.00.

Regressions, disclosed

  • NonlinearSolveNoInitCache (a SimpleNonlinearSolve inner algorithm) regresses: Bruss1D KenCarp4@1e-8 nf 2003 → 4323 (2.16×), TRBDF2@1e-9 1.82×, KenCarp4@1e-9 1.52×. That path has a reusable W, so it takes the retry, but lacks the outer machinery to benefit. Worth gating on before merge — I have not.
  • VdPstiff/FBDF@1e-9 goes Success → Unstable. NLNewton is also Unstable on that exact configuration, and over 36 perturbations the fix and NLNewton fail on precisely the same 15 samples (McNemar p = 1.0, zero discordant). Master's NSA was accidentally more robust by holding a staler W. This is the judgement call for review.
  • HIRES/FBDF@1e-12 1.72× vs master — the only row where the fix is materially above NLNewton (1.281). VdPstiff/QNDF@1e-12 convergence failures 4 → 9.

Tests

New nsa_stale_w_reuse_tests.jl (16 assertions), registered in runtests.jl. Negative control on unmodified master:

brusselator1d  ncf 112 <= 2 ;  njacs 113 <= 68 ;  nf 16382 < 10409
hires          ncf 126 <= 2 ;  njacs 127 <= 56
pollution      ncf  46 <= 2 ;  njacs  47 <= 20

All twelve nsa_*.jl files pass on the branch: 348 assertions, zero failures. Runic clean. OrdinaryDiffEqNonlinearSolve 2.10.0 → 2.11.0.

Two pre-existing master failures were confirmed identical on the base branch and are untouched: a GROUP=QA JET report about compute_step! locals, and a Linear Solver Tests MethodError on jacobian!(::NullOperator, …) with Rosenbrock23 + KLUFactorization.

🤖 Generated with Claude Code

…ale Jacobian

`NonlinearSolveAlg`'s W-reuse gate was missing two of `do_newJW`'s terms, and the
pair of them accounted for essentially every nonlinear convergence failure it
recorded where `NLNewton` recorded none.

`errorfail`: the `dt` cut that follows an error-test rejection typically moves
`γΔt` by ~15%, which is inside `new_W_dt_cutoff` (1//5), so the retry reused a `W`
whose chord iteration contracts at ~0.25 per iteration and cannot converge inside
`maxiters`. On Bruss1D/TRBDF2 at rtol 1e-9, 113 of the 118 stage-1 solves that
reused `W` had `EEst > 1` on entry and 112 of them failed; the corresponding
`NLNewton` figure is 0 of 301.

`TryAgain`: `NLNewton` hits the same `maxiters` exhaustion (15 times on that
problem) but `nlsolve!` retries it in-step with a fresh Jacobian, so it reports
none of them. `NonlinearSolveAlg` was excluded from that retry by an `isnewton`
gate; `tracks_J_age` now admits any cache that records `J_t`.

Neither change works alone: `errorfail` on its own leaves ROBER/TRBDF2 at rtol
1e-9 rejecting 39533 steps against master's 3763.

Co-Authored-By: Chris Rackauckas <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Sep 12, 2026
…(carries #4282 to current master) (#4505)

* NonlinearSolveAlg: reassemble W after a rejected step, and retry a stale Jacobian

`NonlinearSolveAlg`'s W-reuse gate was missing two of `do_newJW`'s terms, and the
pair of them accounted for essentially every nonlinear convergence failure it
recorded where `NLNewton` recorded none.

`errorfail`: the `dt` cut that follows an error-test rejection typically moves
`γΔt` by ~15%, which is inside `new_W_dt_cutoff` (1//5), so the retry reused a `W`
whose chord iteration contracts at ~0.25 per iteration and cannot converge inside
`maxiters`. On Bruss1D/TRBDF2 at rtol 1e-9, 113 of the 118 stage-1 solves that
reused `W` had `EEst > 1` on entry and 112 of them failed; the corresponding
`NLNewton` figure is 0 of 301.

`TryAgain`: `NLNewton` hits the same `maxiters` exhaustion (15 times on that
problem) but `nlsolve!` retries it in-step with a fresh Jacobian, so it reports
none of them. `NonlinearSolveAlg` was excluded from that retry by an `isnewton`
gate; `tracks_J_age` now admits any cache that records `J_t`.

Neither change works alone: `errorfail` on its own leaves ROBER/TRBDF2 at rtol
1e-9 rejecting 39533 steps against master's 3763.

Co-Authored-By: Chris Rackauckas <[email protected]>

* NSA oop: split J refresh from W reassembly in the no-init W-reuse path

The out-of-place `initialize!` for the `NonlinearSolveNoInitCache` +
`StaticWOperator` reuse path now follows the same `do_newJW` split as the
in-place branch: `calc_J` only runs on first use, divergence, `TryAgain`,
`always_new`, or non-adaptive stepping, while a `γΔt` drift or an error-test
rejection reassembles `W` from the stored `J` instead of re-evaluating the
ODE Jacobian. `TryAgain` forcing a fresh `J` also keeps the `nlsolve!`
stale-Jacobian retry from looping on an unchanged `W`.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.21
Agent-Model: SWE-2
Agent-Session: /home/crackauc/.local/share/devin/cli/summaries/history_450c9d90c1b846b9.md

* Static W-reuse test: J is now refreshed less often than W

`_update_nlsolvealg_W_oop!` reassembles `W` from the stored `J` on a `γΔt`
drift or an error-test rejection and only re-evaluates `J` on first use,
failure or retry, so `njacs == nw` no longer holds on the StaticArrays
no-init path; the reuse is exactly what `0 < njacs < nw` asserts.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Agent-Harness: Claude Code 2.1.269
Agent-Model: claude-fable-5-1
Agent-Session: https://claude.ai/code/session_01UZBsW6FiGpRStDpX3rGMEW
Claude-Session: https://claude.ai/code/session_01UZBsW6FiGpRStDpX3rGMEW

---------

Co-authored-by: Claude <[email protected]>
Co-authored-by: Chris Rackauckas <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
ChrisRackauckas added a commit that referenced this pull request Sep 13, 2026
- OrdinaryDiffEqDefault: require OrdinaryDiffEqCore 4.17.1 (#4520)
- Add adaptive step size control to SDC (#4346)
- Allow JET 0.12 in every QA test environment (#4513)
- Resolve OrdinaryDiffEqDefault against the local sublibraries (#4514)
- NonlinearSolveAlg: stale-Jacobian retry + rejected-step W reassembly (carries #4282 to current master) (#4505)
- Do not seed a retry from the previous-step interpolant (#4473)
- Say why the linear path ignores new_W_dt_cutoff (#4527)
- Carry the QNDF2 difference array across steps (#4526)
- Pass Θ = 0, not the absolute time, to the AN5 startup interpolant (#4534)
- Make the stabilized-RK step cap and eigenvalue tolerance direction-agnostic (#4536)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (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.

3 participants