Skip to content

NonlinearSolveAlg: drop duplicate nnonliniter and guard resize firstcall write - #3794

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
singhharsh1708:fix/nonlinearsolvealg-stats-resize
Jun 29, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
singhharsh1708:fix/nonlinearsolvealg-stats-resize

Conversation

@singhharsh1708

Copy link
Copy Markdown
Member

Two small correctness fixes in the NonlinearSolveAlg path.

1. nnonliniter stats double-count

initialize! for NLSolver{<:NonlinearSolveAlg} (both OOP and IIP) accumulates the inner NonlinearSolve cache's stats into integrator.stats:

integrator.stats.nf += cache.cache.stats.nf
integrator.stats.nnonliniter += cache.cache.stats.nsteps
integrator.stats.njacs += cache.cache.stats.njacs

But the universal postamble! also does integrator.stats.nnonliniter += nlsolver.iter. Each outer loop iter triggers exactly one inner step!(nlcache), so nlsolver.iter == cache.cache.stats.nsteps — both lines increment by the same N every step, reporting nnonliniter ~2× actual. NLNewton doesn't have this since only postamble! accounts for it.

Drop the nnonliniter line in both initialize! sites; keep nf and njacs since the postamble! doesn't cover those for the NonlinearSolveAlg path.

2. resize_nlsolver! unguarded firstcall write

nlsolver.cache.firstcall = true is written unconditionally in resize_nlsolver!, but only NLNewtonCache/NLNewtonConstantCache have a firstcall field — NonlinearSolveCache, NLFunctionalCache, NLAndersonCache don't. The same write at postamble! line 197 is correctly guarded with isnewton(nlsolver) && (...). Match that pattern so resize doesn't blow up on those alg types.

@ChrisRackauckas
ChrisRackauckas merged commit 232a868 into SciML:master Jun 29, 2026
152 of 166 checks passed
@singhharsh1708
singhharsh1708 deleted the fix/nonlinearsolvealg-stats-resize branch June 29, 2026 15:20
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