Skip to content

NonlinearSolveCache: define Base.resize! and guard firstcall write - #3799

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
singhharsh1708:fix/nonlinearsolvecache-resize
Jun 29, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
singhharsh1708:fix/nonlinearsolvecache-resize

Conversation

@singhharsh1708

Copy link
Copy Markdown
Member

resize!(integrator, N) on an integrator using NonlinearSolveAlg blows up two ways before reaching the actual nlsolver work:

  1. resize!(nlsolver.cache, nlsolver, integrator, i) falls through to the default at utils.jl:740, which calls Base.resize!(cache::NonlinearSolveCache, i::Int) — undefined, so MethodError.
  2. Then resize_nlsolver! does nlsolver.cache.firstcall = true unconditionally, but NonlinearSolveCache has no firstcall field (only NLNewtonCache/NLNewtonConstantCache do — NLFunctionalCache and NLAndersonCache also lack it).

Fix

  1. Define Base.resize!(::NonlinearSolveCache, ::AbstractNLSolver, integrator, i::Int) mirroring the existing NLNewtonCache resize: resize the buffer fields that are populated (ustep, k, atmp, du1, jac_config, J/W via resize_J_W!), then SciMLBase.reinit!(nlcache.cache, zero(nlcache.ustep)) to bring the inner NonlinearSolve cache's u/fu/J to the new size.
  2. Guard the firstcall write in resize_nlsolver! with isnewton(nlsolver) && (...), matching the same pattern already in postamble! at nlsolve.jl:197.

The === nothing guards on each field handle both IIP (fields populated) and OOP (most fields nothing).

Note: the firstcall guard line overlaps with PR #3794 — same one-line change to the same site. Settles cleanly on merge regardless of order.

@singhharsh1708
singhharsh1708 force-pushed the fix/nonlinearsolvecache-resize branch from d63b5c4 to ab0fbd3 Compare June 29, 2026 09:28
@singhharsh1708
singhharsh1708 force-pushed the fix/nonlinearsolvecache-resize branch from ab0fbd3 to 2f65d5a Compare June 29, 2026 09:39
@ChrisRackauckas

Copy link
Copy Markdown
Member

Needs a test. Use NonlinearSolveAlg in the cache growing event tests as a good test case for this.

@ChrisRackauckas
ChrisRackauckas merged commit 32dec6c into SciML:master Jun 29, 2026
151 of 166 checks passed
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.

2 participants