Skip to content

Let solve_cache! drive a polyalgorithm cache - #1189

Open
singhharsh1708 wants to merge 1 commit into
SciML:masterfrom
singhharsh1708:polyalg-termination-cache-hook
Open

Let solve_cache! drive a polyalgorithm cache#1189
singhharsh1708 wants to merge 1 commit into
SciML:masterfrom
singhharsh1708:polyalg-termination-cache-hook

Conversation

@singhharsh1708

Copy link
Copy Markdown
Contributor

NonlinearSolveBase.solve_cache! throws on a NonlinearSolvePolyAlgorithmCache:

using NonlinearSolve
import NonlinearSolveBase as NLSB

prob = NonlinearProblem((u, p) -> u .^ 2 .- p, [1.5, 1.5], [2.0, 3.0])
NLSB.solve_cache!(init(prob, RobustMultiNewton()))
# ERROR: type NonlinearSolvePolyAlgorithmCache has no field termination_cache

It passes the applicable(InternalAPI.step!, cache) guard, so it is accepted, and then
_run_cache_to_completion! reads cache.termination_cache and cache.trace as raw fields at
the end. A polyalgorithm has neither: the subsolver it is currently running owns both. It does
have nsteps, retcode and maxiters, which is why it gets all the way to the last few lines
before failing. FastShortcutNonlinearPolyalg fails identically.

This follows the hook pattern #1187 just documented for get_u, whose docstring names "a
polyalgorithm forwarding to its active subsolver" as the reason such a hook exists. Two more
accessors in the same shape, get_termination_cache and get_trace, each defaulting to the
field it reads today, with NonlinearSolvePolyAlgorithmCache forwarding both to
caches[current]. get_abstol and get_reltol read the same field and now go through the
hook, so those work on a polyalgorithm cache too.

Nothing changes for a cache that has the fields, since the defaults return exactly what the
call sites read before.

Verified

Same problem, checked against what the normal solve path returns:

solve_cache! error vs sqrt.([2, 3]) agrees with solve() get_abstol
NewtonRaphson Success 0 yes 3e-13
RobustMultiNewton Success 2.22e-16 yes 3e-13
FastShortcutNonlinearPolyalg Success 4.97e-16 yes 3e-13
TrustRegion Success 2.22e-16 yes 3e-13

All four throw on master except NewtonRaphson and TrustRegion, and get_abstol on a
polyalgorithm cache throws on master.

test/Core/cache_solve_tests__item1.jl, the file #1187 extended, passes.

Why

ImplicitDiscreteSolve drives its stage solve through solve_cache! so it can use the step
observer, and today it cannot accept a polyalgorithm as nlsolve because of this
(SciML/OrdinaryDiffEq.jl#4138). The NonlinearSolveNoInitCache branch #1187 documented covers
the SimpleNonlinearSolve half; this covers the other one.

AI Disclosure

Claude assisted with this work.

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.

1 participant