Repository navigation
Fix reinit! resetting LinearSolveParameters to a Missing p-type - #1008
Merged
ChrisRackauckas merged 1 commit intoJul 6, 2026
Merged
ChrisRackauckas merged 1 commit into
ChrisRackauckas merged 1 commit into
Conversation
`InternalAPI.reinit!(cache::LinearSolveJLCache; u = missing, p = missing)`
rebuilt the internal `LinearSolveParameters` from the passed `p` even when `p`
was left as `missing` (the usual case: reinit! with only a new `u0`, parameters
unchanged). That produced `LinearSolveParameters(u_fixed, missing)`, whose
`Missing` p-field type mismatches the concretely-typed `p` (e.g.
`NullParameters`) the LinearSolve cache was constructed with, throwing
setfield!: expected LinearSolveParameters{…, NullParameters},
got a value of type LinearSolveParameters{…, Missing}
for every solver that uses the LinearSolve-backed linear cache (NewtonRaphson,
TrustRegion, the default polyalgorithm). This blocked the standard
init-once / reinit!-each-step pattern — a continuation/homotopy loop could not
reuse a cache.
Treat `missing` `u`/`p` as "unchanged": preserve the cache's current values
instead of overwriting them. An explicit `p` is still applied.
Verified locally (Julia 1.11): NewtonRaphson, TrustRegion, and the
polyalgorithm now `init` once and `reinit!`+`solve!` in a loop without error and
with correct roots; a changed `p` is tracked; the reinit!-exercising suite
(ForwardDiff-cache SciML#391, NoInit caching, preconditioner, cache solve! SciML#779)
still passes (106 assertions), plus a new regression test (18 assertions).
Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Chris Rackauckas <[email protected]>
ChrisRackauckas
marked this pull request as ready for review
July 6, 2026 13:24
ChrisRackauckas-Claude
pushed a commit
to ChrisRackauckas-Claude/NonlinearSolve.jl
that referenced
this pull request
Jul 9, 2026
…ng loop Rebase and consolidation of the SciML#967 improvements onto current master (which gained the λspan[1] anchor in SciML#972 and the SciMLTesting test dispatch), plus the inner-solver cache reuse now possible after SciML#1008. Adaptive step control (the classic predictor-corrector success/failure heuristic, e.g. Timme 2021): - `expand_threshold` consecutive accepted steps grow dλ by `expand_factor`, capped at `max_step_factor` of the span; a rejected step still halves it. - Expansion is gated on a Deuflhard-style local error estimate (`expand_quality`): the secant prediction's error must be small relative to the recent solution movement, OR the corrector must have converged within 2 iterations (covers exponentially flattening paths where the relative measure stalls). This keeps the increment from ballooning right before a sharp turn, where an oversized step is rejected only after the inner solver exhausts its iterations. - `predictor = :secant` (default) warm-starts the corrector by linear extrapolation through the last two accepted points, trust-monitored with hysteresis: measured prediction error no better than half the trivial constant prediction — or any rejected step — falls back to the constant warm start until two consecutive accepted steps measure good quality again. `:constant` restores the previous warm-start behavior. Cache-reusing, non-allocating driver: - `FixLambda` is mutable: the wrapped inner function is built once and advancing λ is a field write. - The inner solver is `init`ed once and re-driven per step with `reinit!`/`solve!` (requires the SciML#1008 LinearSolveParameters fix), reusing its workspace instead of reconstructing the solver each step; the anchor solve is the cache's first `solve!`. - The sweep's own state (`u`, `u_prev`, warm-start and quality-gate scratch) lives in preallocated buffers: in-place extrapolation, `Utils.norm_op` norms without temporaries, and buffer-swap acceptance. Immutable / StaticArray / scalar `u` takes a compile-time non-mutating branch. The λspan[1] anchor, its retcode semantics, and the zero-width-span behavior from SciML#972 are preserved verbatim. `expand_factor = 1, predictor = :constant` reproduces the previous fixed-increment behavior. Verified locally (Julia 1.11): all 21 homotopy_sweep test items pass (94 assertions) — master's 1-17 (incl. the SciML#972 anchor tests and the aliasing test) plus new items 18-21 covering kwarg validation, growth + caps, secant corrector-work reduction, and regrowth after bisecting a hard region. Stats are per-solve under the reused cache (verified), so the corrector-cheap gate is unaffected. Whole-solve allocations: scalar default 68176 -> 51840 B; the few-step scalar SimpleNewtonRaphson case pays for the one-time cache init (23888 -> 29328 B), which amortizes over longer continuations — the loop is the regime this solver targets. Co-Authored-By: Claude Fable 5 <[email protected]> Co-Authored-By: Chris Rackauckas <[email protected]>
ChrisRackauckas
added a commit
that referenced
this pull request
Jul 9, 2026
…ng loop (#967) Rebase and consolidation of the #967 improvements onto current master (which gained the λspan[1] anchor in #972 and the SciMLTesting test dispatch), plus the inner-solver cache reuse now possible after #1008. Adaptive step control (the classic predictor-corrector success/failure heuristic, e.g. Timme 2021): - `expand_threshold` consecutive accepted steps grow dλ by `expand_factor`, capped at `max_step_factor` of the span; a rejected step still halves it. - Expansion is gated on a Deuflhard-style local error estimate (`expand_quality`): the secant prediction's error must be small relative to the recent solution movement, OR the corrector must have converged within 2 iterations (covers exponentially flattening paths where the relative measure stalls). This keeps the increment from ballooning right before a sharp turn, where an oversized step is rejected only after the inner solver exhausts its iterations. - `predictor = :secant` (default) warm-starts the corrector by linear extrapolation through the last two accepted points, trust-monitored with hysteresis: measured prediction error no better than half the trivial constant prediction — or any rejected step — falls back to the constant warm start until two consecutive accepted steps measure good quality again. `:constant` restores the previous warm-start behavior. Cache-reusing, non-allocating driver: - `FixLambda` is mutable: the wrapped inner function is built once and advancing λ is a field write. - The inner solver is `init`ed once and re-driven per step with `reinit!`/`solve!` (requires the #1008 LinearSolveParameters fix), reusing its workspace instead of reconstructing the solver each step; the anchor solve is the cache's first `solve!`. - The sweep's own state (`u`, `u_prev`, warm-start and quality-gate scratch) lives in preallocated buffers: in-place extrapolation, `Utils.norm_op` norms without temporaries, and buffer-swap acceptance. Immutable / StaticArray / scalar `u` takes a compile-time non-mutating branch. The λspan[1] anchor, its retcode semantics, and the zero-width-span behavior from #972 are preserved verbatim. `expand_factor = 1, predictor = :constant` reproduces the previous fixed-increment behavior. Verified locally (Julia 1.11): all 21 homotopy_sweep test items pass (94 assertions) — master's 1-17 (incl. the #972 anchor tests and the aliasing test) plus new items 18-21 covering kwarg validation, growth + caps, secant corrector-work reduction, and regrowth after bisecting a hard region. Stats are per-solve under the reused cache (verified), so the corrector-cheap gate is unaffected. Whole-solve allocations: scalar default 68176 -> 51840 B; the few-step scalar SimpleNewtonRaphson case pays for the one-time cache init (23888 -> 29328 B), which amortizes over longer continuations — the loop is the regime this solver targets. Co-authored-by: Claude <[email protected]> Co-authored-by: Chris Rackauckas <[email protected]>
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.
Note
This PR should be ignored until reviewed by @ChrisRackauckas.
Prerequisite for making the homotopy-continuation solvers reuse an inner-solver cache in their step loop (
initonce,reinit!+solve!each step) instead of rebuilding the solver every step.The bug
InternalAPI.reinit!(cache::LinearSolveJLCache; u = missing, p = missing)rebuilt the internalLinearSolveParametersfrom the passedpeven whenpwasmissing— i.e. the ordinary case of reinit!-ing with only a newu0, parameters unchanged:That builds
LinearSolveParameters(u_fixed, missing), whoseMissingp-field type mismatches the concretely-typedp(e.g.NullParameters) the LinearSolve cache was constructed with, soreinit!throws:This fires for every solver that uses the LinearSolve-backed linear cache —
NewtonRaphson,TrustRegion, and the default polyalgorithm — so the standard "init once,reinit!each step" reuse pattern was unusable with them.The fix
Treat
missingu/pas "unchanged" — read them from the cache's currentLinearSolveParametersrather than overwriting withmissing. An explicitpis still applied as before.Verification (local, Julia 1.11)
NewtonRaphson,TrustRegion, and the polyalgorithm nowinitonce andreinit!+solve!in a loop without error and with correct roots (previously all threw).ptoreinit!still moves the solution to the new root (sqrt(9) = 3).reinitcache for Forward Mode AD not working #391, NoInit caching, LinearSolve preconditioner, polyalg cachesolve!MethodError: no method matching get_fu(::Vector{Float64}) #779) still passes — 106 assertions.test/Core/reinit_tests__item1.jl(18 assertions) covering init →p-lessreinit!→ explicit-preinit!across the three solver families.NonlinearSolveBasebumped 2.34.0 → 2.34.1 (bug-fix).🤖 Generated with Claude Code