Repository navigation
Register precondition/postcondition as common solver keywords - #1449
ChrisRackauckas merged 1 commit into
Conversation
|
CI triage of the first full run + new commit 542eb18:
🤖 Generated with Claude Code |
|
CI status for head 542eb18 (final): 50 pass / 12 fail / 1 pending, and every failure is accounted for as not-this-PR:
Ready for review. 🤖 Generated with Claude Code |
| precondition::PC | ||
| postcondition::POC |
There was a problem hiding this comment.
I think this is the part which I struggled with so hard when trying to integrate it myself and I am still not sure if I just overthink this or if this is a true concern. Technically the left and right preconditioner are parts of the solver, but this design proposes, if I understand correctly, that the preconditioner is part of the function. But we also need some consistent way to communicate information about f to the preconditioner (e.g. decomposition or boundary information). So, is this really the way to go down?
There was a problem hiding this comment.
@termi-official That's a fair challenge and I don't think you're overthinking it. My take: the question isn't solver vs. function, it's who knows the preconditioner, and there are two genuinely different answers that want two different homes.
Model-declared conditioning. A diode's junction-limiting rule, a discretization's Dirichlet condensation, a Modelica-style homotopy annotation — these are properties of the equations. The solver cannot invent them; only whoever wrote the model knows them. Modelica puts homotopy(actual, simplified) in the model for exactly this reason, and this is the half this PR serves. It's not hypothetical: SciML/ModelingToolkit.jl#4774 (up now) adds a limited(actual, limiter) operator whose whole point is that a component library annotates a device once and every downstream nonlinear solve gets predictor/corrector limiting automatically. That composition needs a place on the function/problem for the model to put the thing; a solver keyword can't be filled in by a component library that has no idea which solver the end user will pick.
Solver-chosen conditioning. ASPIN with a particular subdomain decomposition, a field-split, an Anderson-mixing composition — these are numerical-method choices, and I agree they belong on the algorithm. This PR does not serve that half, and I don't think it should pretend to.
On "how do we communicate information about f to the preconditioner" — that's the sharpest part of your question. What exists today:
G(fu, u, p)gets the residual, the iterate, and the parameters, so anything the model routes throughp(boundary data, coefficients, MTK bound parameters) is reachable;H(u_proposed, u_prev, p)optionally takes the solver cache as a fourth argument (added this week —H(u_prop, u_prev, p, cache), PETSc-post-check style), which covers iteration count, current residual, tolerances;- structural data known at construction time — index sets, a decomposition, compiled per-quantity rules — goes in the closure/callable struct. The MTK layer does exactly this: its generated
LimitedPostconditioncaptures the compiled limiters and their state indices.
What is genuinely not covered, and where I think your concern lands: a preconditioner that must be built from f at solve time — ASPIN partitioning derived from Jacobian sparsity that only exists once the cache is constructed, or a decomposition you want re-derived on reinit!. Closure capture is the wrong tool there.
The SciML precedent for that is LinearSolve's precs: not a preconditioner object on the operator, but a constructor passed to the solver — precs(W, p) -> (Pl, Pr) — which receives the problem data and returns the preconditioner pair. The nonlinear analogue would be an algorithm keyword along the lines of
NewtonRaphson(; nonlinear_precs = (f, u, p) -> (G, H))evaluated at cache construction (and re-evaluated on reinit!), producing the same G/H the solver already knows how to apply. The two layers then compose cleanly rather than competing: the algorithm-side constructor wraps whatever the function declares, in the same way a linsolve preconditioner composes with a Jacobian the function supplied.
I'd rather land that as a follow-up than widen this PR, since it's additive and the field-side machinery is what it would reuse. But if you'd rather see the precs-style hook designed before the function-side fields are committed to — so the two are shaped together and we don't end up with a field-side API that the algorithm-side one has to work around — say so and I'll prototype it on this branch instead. You filed the issue and you've already hit the wall once trying to integrate this, so I'd weight your read on that heavily.
🤖 Generated with Claude Code
Nonlinear preconditioning is a solver-side concern: a left preconditioner `G(fu, u, p)` on the residual and an iterate corrector `H(u_proposed, u_prev, p[, cache])` applied to accepted iterates, in the sense of Brune, Knepley, Smith & Tu (SIAM Review 57(4), 2015). Registering them in `allowedkeywords` is the whole SciMLBase surface: they are then ordinary solve options, so they can be supplied late at `solve`/`init` or carried on a problem's `kwargs` and merged in like any other keyword, and a misspelling is caught by the existing keyword validation. Semantics live in the solver packages; see SciML/NonlinearSolve.jl#351. Co-Authored-By: Chris Rackauckas <[email protected]>
542eb18 to
f07b0d0
Compare
Redesigned: these are now solve keywords, not
|
| Check | Result |
|---|---|
Late-bound postcondition on the PCNR diode circuit |
plain 179 steps → 11, problem object untouched and re-solving at 179 |
| Same corrector carried on the problem instead | 11 steps, identical |
| Solve-time corrector overriding a problem-level one | override observed |
precondition (asinh) on the scalar diode |
57 → 9 steps |
| Four-argument corrector receiving the cache | receives nothing at the initial guess, then the real cache |
| Solver coverage | NewtonRaphson, TrustRegion, LevenbergMarquardt, Broyden, Klement, DFSane, default polyalg, init/solve! |
| Guards | unsupported solver throws, lb/ub combination throws, precondition still composes on the SimpleNonlinearSolve path |
Typo postcondtion |
rejected by allowedkeywords |
| Tutorial | all 7 @example blocks execute, same numbers |
Suites: SciMLBase problem_building_test.jl (both testsets), NonlinearSolveBase Core (26/26 in the new testset, suite green), ModelingToolkitBase limited_operator.jl 29/29 including a new override test. NonlinearSolveFirstOrder Core was still running when this was written; I'll report it.
🤖 Generated with Claude Code
Important
This PR should be ignored until reviewed by @ChrisRackauckas.
Summary
Adds two optional fields to
NonlinearFunctionfor declaring a nonlinear preconditioner, in the solver-composition sense of Brune, Knepley, Smith & Tu, Composing Scalable Nonlinear Algebraic Solvers (SIAM Review 57(4), 2015), i.e. the root-equivalent composed systemprecondition— a left preconditionerG(fu, u, p)applied to the residual (in-place functions overwritefu). Solvers replace the residual with the composed map everywhere: function evaluations, AD Jacobians, line-search merit functions, and termination criteria.Gmust be root-preserving.postcondition— a right preconditioner / iterate correctorH(u_proposed, u_prev, p)(in-place functions overwriteu_proposed). Solvers apply it to every accepted iterate before evaluating the residual there, so residual/Jacobian stay consistent with corrected iterates. This is the hook for SPICE-style junction limiting — i.e. the corrector phase of the Predictor/Corrector Newton-Raphson (PCNR) method of Aadithya, Keiter & Mei — as well as projections and Dirichlet-type condensation. It corresponds to PETSc SNES post-check.The fields are purely declarative here. Solver semantics are implemented in the companion NonlinearSolve.jl PR (which depends on this being released): addresses SciML/NonlinearSolve.jl#351.
Changes
NonlinearFunctionstruct: two new trailing fields/type-parametersprecondition,postcondition(defaultnothing), threaded through both theNoSpecializeand specialized keyword-constructor branches.__has_precondition/__has_postconditionexistence helpers so constructor forwarding andremakework as for the other optional fields.G, fixed-point identity forH, Jacobian conventions).test/problem_building_test.jlcovering construction (oop/iip), defaults,remakepreservation, and constructor forwarding.Verification
Ran locally on Julia 1.12.4:
GROUP=Core Pkg.test()— passed (includes the new testset).Process notes
Design per the discussion on SciML/NonlinearSolve.jl#351: hooks take iterate arguments so limiting can decide activation from the current iterate; freeze-per-iteration semantics are the solver's responsibility. Note for reviewers:
remake(prob; f = new_f)merges function fields and letsnothingfields fall back to the old function's values, so downstream consumers cannot clear a consumed hook tonothing— NonlinearSolve.jl handles idempotence with a sentinel marker instead.🤖 Generated with Claude Code
https://claude.ai/code/session_01B9GHoo4pFa3DU9yhyTkevL