Skip to content

NonlinearSolveAlg: pipe wrapprecs into inner alg and update weight per step - #3795

Merged
ChrisRackauckas merged 6 commits into
SciML:masterfrom
singhharsh1708:fix/nonlinearsolvealg-precs-weight
Jul 6, 2026
Merged

ChrisRackauckas merged 6 commits into
SciML:masterfrom
singhharsh1708:fix/nonlinearsolvealg-precs-weight

Conversation

@singhharsh1708

Copy link
Copy Markdown
Member

The NonlinearSolveAlg path builds the same weight buffer and wrapprecs(alg.linsolve, W, weight) as the NLNewton path, but then drops both:

  • _nlalg_with_linsolve(nlalg.alg, alg.linsolve) pipes the bare alg.linsolve into the inner NonlinearSolve, discarding the wrapprecs wrapper that NLNewton uses.
  • initialize! for NLSolver{<:NonlinearSolveAlg, true} never calls calculate_residuals!(weight, ...), so even if the precs were piped through they'd be acting on a buffer of zeros.

Net effect: every NonlinearSolveAlg user loses the weight-based diagonal preconditioner that NLNewton enjoys on stiff problems — quietly worse Newton convergence with no warning.

Fix

  1. Add a weight field to NonlinearSolveCache (parametrized as weightType so OOP can pass nothing).
  2. IIP build_nlsolver: pipe wrapprecs(alg.linsolve, W, weight) into _nlalg_with_linsolve, and store weight on the cache.
  3. OOP build_nlsolver: pass nothing for weight, matching the existing OOP NLNewton path which also has no weight buffer.
  4. IIP initialize! for NonlinearSolveAlg: update the weight buffer via calculate_residuals!, mirroring initialize! for NLSolver{<:NLNewton, true}.

After this, the precs closure built by wrapprecs (a Returns((Pl, Pr)) that captures _vec(weight) by reference) sees fresh weights every step, exactly as for NLNewton.

Diff: +14/−4 across newton.jl, type.jl, utils.jl. OOP path is untouched semantically (only the NonlinearSolveCache constructor call gets an extra nothing arg).

@ChrisRackauckas

Copy link
Copy Markdown
Member

Add a test for this

@singhharsh1708

Copy link
Copy Markdown
Member Author

added a test — solves with NonlinearSolveAlg and checks the weight buffer is allocated and gets filled after a step

@singhharsh1708
singhharsh1708 force-pushed the fix/nonlinearsolvealg-precs-weight branch from 6b58dc4 to 474d143 Compare June 29, 2026 18:51
prob,
TRBDF2(
autodiff = AutoFiniteDiff(),
nlsolve = NonlinearSolveAlg(NewtonRaphson())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The weights are only used with default preconditioners though? So it needs a KrylovJL_GMRES() linsolve test.

@singhharsh1708

Copy link
Copy Markdown
Member Author

Right — the weight only feeds into the default precs path via wrapprecs. Added an explicit KrylovJL_GMRES() (no user precs) + NonlinearSolveAlg test that (a) solves cleanly under the GMRES linsolve and (b) checks the weight buffer is allocated and gets filled after a step.

@singhharsh1708
singhharsh1708 force-pushed the fix/nonlinearsolvealg-precs-weight branch 3 times, most recently from 4d6f94e to d78a847 Compare July 3, 2026 19:27
@singhharsh1708
singhharsh1708 force-pushed the fix/nonlinearsolvealg-precs-weight branch from d78a847 to de67095 Compare July 5, 2026 03:42
@ChrisRackauckas
ChrisRackauckas merged commit c88f6a4 into SciML:master Jul 6, 2026
150 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