Skip to content

Fix reinit! resetting LinearSolveParameters to a Missing p-type - #1008

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:reinit-linearsolveparams-fix
Jul 6, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:reinit-linearsolveparams-fix

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

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 (init once, reinit! + solve! each step) instead of rebuilding the solver every step.

The bug

InternalAPI.reinit!(cache::LinearSolveJLCache; u = missing, p = missing) rebuilt the internal LinearSolveParameters from the passed p even when p was missing — i.e. the ordinary case of reinit!-ing with only a new u0, parameters unchanged:

return SciMLBase.reinit!(cache.lincache; p = LinearSolveParameters(u_fixed, p))  # p === missing here

That builds LinearSolveParameters(u_fixed, missing), whose Missing p-field type mismatches the concretely-typed p (e.g. NullParameters) the LinearSolve cache was constructed with, so reinit! throws:

setfield!: expected LinearSolveParameters{Vector{Float64}, NullParameters},
           got a value of type LinearSolveParameters{Vector{Float64}, Missing}

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 missing u/p as "unchanged" — read them from the cache's current LinearSolveParameters rather than overwriting with missing. An explicit p is still applied as before.

Verification (local, Julia 1.11)

  • NewtonRaphson, TrustRegion, and the polyalgorithm now init once and reinit!+solve! in a loop without error and with correct roots (previously all threw).
  • Passing a new p to reinit! still moves the solution to the new root (sqrt(9) = 3).
  • The reinit!-exercising suite (ForwardDiff-cache reinit cache for Forward Mode AD not working #391, NoInit caching, LinearSolve preconditioner, polyalg cache solve! MethodError: no method matching get_fu(::Vector{Float64}) #779) still passes — 106 assertions.
  • New regression test test/Core/reinit_tests__item1.jl (18 assertions) covering init → p-less reinit! → explicit-p reinit! across the three solver families.

NonlinearSolveBase bumped 2.34.0 → 2.34.1 (bug-fix).

Note: there is a separate, deeper gap where a p-less reinit! after an explicit p-change does not always retain the changed p for one solver family (the outer cache's p persistence, not this linear-cache path); left out of scope here and not asserted.

🤖 Generated with Claude Code

`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
ChrisRackauckas marked this pull request as ready for review July 6, 2026 13:24
@ChrisRackauckas
ChrisRackauckas merged commit 791a33e into SciML:master Jul 6, 2026
88 of 93 checks passed
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]>
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