Fix NonlinearSolveAlg crashing/stalling with polyalgorithm inner solvers#3959
Open
singhharsh1708 wants to merge 1 commit into
Open
Fix NonlinearSolveAlg crashing/stalling with polyalgorithm inner solvers#3959singhharsh1708 wants to merge 1 commit into
singhharsh1708 wants to merge 1 commit into
Conversation
NonlinearSolvePolyAlgorithmCache (RobustMultiNewton, FastShortcutNonlinearPolyalg, etc.) has no top-level u/fu/trace field, so every raw cache.cache.u-style read in newton.jl threw FieldError. Its own reinit! also leaves force_stop/retcode set after a branch converges once, which makes CommonSolve.step!'s not_terminated guard silently no-op every later step and reuse a stale iterate. Route all reads through a cache that resolves to the active branch, and reset the two termination fields after each reinit!.
singhharsh1708
force-pushed
the
fix-nonlinearsolvealg-polyalg-3859
branch
from
July 21, 2026 14:30
d1d3bff to
7dd8e8a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3859.
Bug 1: crash
NonlinearSolvePolyAlgorithmCache(used whennlsolvewraps a polyalgorithm likeRobustMultiNewtonorFastShortcutNonlinearPolyalg) has no top-levelu/fu/tracefield — those live on whichever branch cache is currently active (cache.caches[cache.current]).newton.jlreadcache.cache.u/nlcache.u/nlcache.fu/nlcache.tracedirectly in five places, throwingFieldErrorthe moment a polyalgorithm was used:Fix: a small helper resolves to the active branch cache before reading any of these fields.
hasfieldon a concrete type is resolved at compile time, so this is a no-op branch for the non-polyalg path.Bug 2: stale iterate reused forever after the first successful step
Even after fixing the field access, a polyalgorithm cache stops making progress after its first successful nonlinear solve.
NonlinearSolvePolyAlgorithmCache's ownInternalAPI.reinit!resetscurrent/nsteps/statsbut notforce_stop/retcode. Once any branch converges once,force_stopstaystrue, soCommonSolve.step!'snot_terminated(cache) || returnguard makes every laterstep!call on that cache a silent no-op — the outerNLSolverloop reads the unchanged iterate as an instantly-converged residual (ndz == 0) and moves on with a staleu.Confirmed by instrumenting
newton.jllocally: on a plainu' = -udecay ODE withRobustMultiNewton, the solve looked fine for the firstTRBDF2stage, then froze — every later stage returned the exact sameuregardless of the actual predictor, and adaptive step sizes ballooned as the (falsely) near-zero residual fed back into the error controller.Fix: reset the two fields explicitly after every
reinit!call on a polyalgorithm cache, until this is fixed upstream in NonlinearSolveBase.jl:Verified
RobustMultiNewtonandNewtonRaphson(single-alg control) onu' = -u, both fixeddtand adaptive: results match to ~14 digits.tspan = (0, 1)) withRobustMultiNewton: matches anFBDFreference to1.4e-5relative error.OrdinaryDiffEqNonlinearSolveCoretest group (all files undertest/, 15 testsets, 500+ assertions) passes unchanged with this diff.What this does not fix
_nsa_inner_convergedin #3893 (open) readsnlsolver.cache.cache.retcodeto gate a different false-convergence shortcut (aTrustRegionstep that gets rejected leaves the iterate unmoved, which looks like convergence from a pure-displacement check). For a polyalgorithm cache that retcode is the exact field this PR resets — without this fix, #3893's gate is permanently defeated for polyalgorithms the same way the outer loop was. I checked the combination against the harder, fully-stiff Robertson case (tspan = (0, 1e5), the repro from #3859 itself): the two fixes together are still not sufficient there. That interaction needs its own investigation and is out of scope here — this PR is scoped to the crash and the reinit staleness that #3859 actually reports.AI Disclosure
Claude assisted with this work.