Repository navigation
Nonlinear preconditioning hooks: precondition (G) / postcondition (H) with PCNR limiting support - #1084
Conversation
|
New commit 7b9f590:
Re-verified locally after the change (Julia 1.12.4): NonlinearSolveBase Core (28/28 in the hooks testset, incl. new arity-dispatch unit tests), NonlinearSolveFirstOrder Core (incl. a 4-arg integration test asserting the hook sees 🤖 Generated with Claude Code |
|
CI note: no workflow runs have been scheduled for this fork PR (zero check-runs on both pushed heads; SciML/SciMLBase.jl runs the same account's fork PRs fine, so this repo's Actions settings appear to gate fork workflows behind maintainer approval). @ChrisRackauckas — when you get to the review, the workflows will need the "Approve and run" click. Note CI here is expected to fail dependency resolution until SciML/SciMLBase.jl#1449 is merged and released (NonlinearSolveBase now requires SciMLBase ≥ 3.37); all seven local test suites are green as documented above. 🤖 Generated with Claude Code |
7b9f590 to
5e01573
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
…dition Implements the `precondition` (left preconditioner G) and `postcondition` (iterate corrector H) solve keywords registered in SciMLBase, per the composed-solver framework of Brune, Knepley, Smith & Tu (SIAM Review 2015), solving G(f(H(u_tilde, u_k), p), u_tilde, p) = 0. - G is applied as a problem transformation at the solve/init funnels, so the composed residual is what every consumer sees: function evaluations, AD Jacobians, line-search merit functions, and termination. It works with every solver that consumes the problem function, including SimpleNonlinearSolve. - H is read from the cache's keywords and applied at the iterate-commit points of the first-order, quasi-Newton, and spectral step! loops (and once to u0), before the residual is evaluated there. A supports_postcondition trait makes unsupported solvers throw rather than silently ignore it. Correctors may take the solver cache as an optional fourth argument, for which get_nsteps joins get_u/get_fu/get_abstol/get_reltol as public cache accessors. Both are ordinary solve keywords rather than function properties, so they can be bound late — solve(prob, alg; postcondition = H) needs no rebuild of the problem — or carried on the problem and forwarded like any other option, with the solve-time value winning as for alias. This is what makes them usable for solver-chosen preconditioning (ASPIN-style) as well as model-declared limiting. Fully encapsulates the PCNR predictor/corrector limiting method of Aadithya, Keiter & Mei: the predictor is the ordinary Newton step on the augmented system, the corrector is the postcondition, and consistency holds structurally because residuals are only evaluated at corrected iterates. Adds a tutorial with a pnjlim diode-circuit walkthrough (179 -> 11 Newton steps) and asinh residual compression (57 -> 9 steps). Requires SciMLBase >= 3.43. Fixes SciML#351. Co-Authored-By: Chris Rackauckas <[email protected]>
5e01573 to
97bce87
Compare
|
SciMLBase 3.43.0 is registered (JuliaRegistries/General#163665 merged), so this PR's dependency is now resolvable and CI can run for the first time. Verified against the released SciMLBase rather than a local checkout — a fresh environment with only this branch's sublibraries dev'd resolves
Suite status on this exact head ( @ChrisRackauckas — this repo gates fork-PR workflows, so CI here needs an "Approve and run" click; no workflow run has ever been scheduled for this PR. 🤖 Generated with Claude Code |
| post-check receiving the `SNES` object): | ||
|
|
||
| ```@example preconditioning | ||
| H_staged! = function (up, uprev, p, cache) |
There was a problem hiding this comment.
It should always have the 4th argument, it should just have a constant definition to simplify the code.
There was a problem hiding this comment.
Done in 6a4ccea. postcondition now always takes (u_proposed, u_prev, p, cache); correctors that don't need solver state just ignore the argument. That let me delete the applicable arity detection entirely — the apply path is now a direct call, and the initial-guess correction passes nothing for the cache. Propagated through the docs, all tests, and ModelingToolkit's generated LimitedPostcondition (SciML/ModelingToolkit.jl#4774).
| [bound constraints tutorial](bound_constraints.md); `postcondition` combined with | ||
| `lb`/`ub` is rejected since the bounds transform changes the iterate coordinates the | ||
| corrector would act on. |
There was a problem hiding this comment.
It shouldn't be rejected, it should compose, but the user needs a warning here that the condition is imposed on the transformed variable.
There was a problem hiding this comment.
Done in 6a4ccea — they compose now. Since the bounds transform runs after this pass, every in-loop application of the corrector sees the unconstrained coordinate, so the warning says exactly that: the condition is imposed on the transformed iterate, not the original bounded variable. It goes through a new postcondition_bounds_transform verbosity toggle (WarnLevel by default). One consequence worth flagging: I skip the initial-guess correction when bounds are active, so that every application happens in the same coordinate system rather than the first one being in user coordinates and the rest in transformed ones.
| - **Signatures.** Out-of-place problems use `Gfu = G(fu, u, p)` and | ||
| `u_new = H(u_proposed, u_prev, p)`; in-place problems mutate the first argument: | ||
| `G(fu, u, p) -> nothing` overwrites `fu`, `H(u_proposed, u_prev, p) -> nothing` | ||
| overwrites `u_proposed`. |
There was a problem hiding this comment.
This is just for the docstring.
There was a problem hiding this comment.
Done in 6a4ccea — the signature bullet is out of the tutorial and the full in-place/out-of-place signatures for both options now live in the solve docstring's keyword list (lib/NonlinearSolveBase/src/solve.jl), alongside the error-control and miscellaneous options.
| - **Precedence.** A value passed to `solve`/`init` overrides one carried on the | ||
| problem; it does not compose with it. If a model-generating package (for example a | ||
| ModelingToolkit component library with device limiting) attached a corrector to the | ||
| problem, passing your own at solve time replaces it entirely. |
There was a problem hiding this comment.
This is all kwargs, no need here
There was a problem hiding this comment.
Done in 6a4ccea — dropped. Ordinary keyword precedence applies and doesn't need restating in a tutorial.
| - **Solver support.** The first-order (`NewtonRaphson`, `TrustRegion`, | ||
| `LevenbergMarquardt`, ...), quasi-Newton (`Broyden`, `Klement`, ...), and spectral | ||
| (`DFSane`) families and poly-algorithms composed of them support `postcondition`; | ||
| unsupported solvers (e.g. SimpleNonlinearSolve or external wrappers) throw an | ||
| `ArgumentError` rather than silently ignoring it. `precondition` is a problem | ||
| transformation and works with every solver that consumes the problem function, | ||
| including SimpleNonlinearSolve. |
There was a problem hiding this comment.
This error should be able to be turned off via SciMLLogging.
There was a problem hiding this comment.
Done in 6a4ccea — it's now the unsupported_postcondition verbosity toggle instead of a bare throw. Worth noting the default behaviour is unchanged: SciMLLogging raises an ErrorException at ErrorLevel, so an unsupported solver still stops rather than silently dropping the corrector — but it's now silenceable (verbose = NonlinearVerbosity(SciMLLogging.None())), which the tests cover in both the base and SimpleNonlinearSolve suites.
…reports Addresses @ChrisRackauckas's review on SciML#1084: - postcondition correctors now always take the solver cache as a fourth argument; correctors that do not need it ignore the parameter. Dropping the arity detection simplifies the apply path to a direct call. - postcondition and lb/ub now compose instead of being rejected. Bounds are handled by transforming to an unconstrained variable, so the corrector acts on the transformed iterate; that change of coordinates is reported through the new postcondition_bounds_transform verbosity toggle, and the initial-guess correction is skipped so every application happens in the same space. - the unsupported-solver report goes through SciMLLogging as the new unsupported_postcondition toggle rather than a bare throw: it still raises by default (SciMLLogging raises at ErrorLevel) but can be turned down. - tutorial: cite the PCNR paper where the circuit example introduces it, drop the Schur-complement paragraph, and drop the signature and precedence bullets -- signatures now live in the solve docstring, and precedence is ordinary keyword behaviour. Co-Authored-By: Chris Rackauckas <[email protected]>
Follows the postcondition contract change in SciML/NonlinearSolve.jl#1084: correctors always receive the solver cache as a fourth argument. The generated LimitedPostcondition accepts and ignores it, since limiters are functions of the iterate and parameters only. Co-Authored-By: Chris Rackauckas <[email protected]>
test/Core/core_tests__item23.jl asserts that `postcondtion` is rejected by SciMLBase's common keyword whitelist, so the misspelling is the point of the test and cannot be corrected. The Spell Check workflow has failed on every PR since SciML#1084 introduced that test. Allow it via `[type.jl.extend-identifiers]`: scoped to Julia sources and to the exact, case-sensitive identifier, so the same typo is still caught in prose, in a compound name, or in any other capitalization. Co-Authored-By: Chris Rackauckas <[email protected]>
…ks (#1141) * Document NonlinearSolveBase public API added by the preconditioning hooks `run_api_docs` requires every name in NonlinearSolveBase's public API to have a docstring and to be rendered in a `@docs` block under docs/src. #1084 made nine names public without docs entries, which fails the QA group. Add docstrings for the three cache accessors (`get_u`, `get_fu`, `get_nsteps`) and render all nine in docs/src/devdocs/internal_interfaces.md. Co-Authored-By: Chris Rackauckas <[email protected]> * Tell typos the deliberate keyword misspelling in the test is intentional test/Core/core_tests__item23.jl asserts that `postcondtion` is rejected by SciMLBase's common keyword whitelist, so the misspelling is the point of the test and cannot be corrected. The Spell Check workflow has failed on every PR since #1084 introduced that test. Allow it via `[type.jl.extend-identifiers]`: scoped to Julia sources and to the exact, case-sensitive identifier, so the same typo is still caught in prose, in a compound name, or in any other capitalization. Co-Authored-By: Chris Rackauckas <[email protected]> --------- Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Important
This PR should be ignored until reviewed by @ChrisRackauckas.
Note
Depends on SciML/SciMLBase.jl#1449 (adds the
precondition/postconditionfields toNonlinearFunction, v3.37.0). CI here will fail to resolve until that PR is merged and released; all test groups below were run locally against the companion branch.Closes #351.
Summary
Implements nonlinear preconditioner support in the sense of Brune, Knepley, Smith & Tu, Composing Scalable Nonlinear Algebraic Solvers (SIAM Review 57(4), 2015): the solver works with the root-equivalent composed system
where both hooks may depend on the current iterate (frozen within each iteration).
precondition(left,G(fu, u, p)) is implemented as a problem transformation (mirroring the bounds-transform pattern): before the cache is built,fis replaced by the compositionu -> G(f(u,p), u, p)via aPreconditionWrapper. Every consumer — residual evaluations, AD Jacobians, line-search merit functions, termination criteria — sees the composed map, for every solver including SimpleNonlinearSolve and extension wrappers.postcondition(right,H(u_proposed, u_prev, p)) is applied at the iterate-commit points of the solverstep!loops (FirstOrder, QuasiNewton, Spectral families), before the residual is evaluated and convergence is tested at the new iterate, and once to the initial guess asH(u0, u0, p). Asupports_postcondition(alg)trait guards against silent ignoring: unsupported algorithms (e.g. SimpleNonlinearSolve) throw anArgumentError; poly-algorithms support it iff all sub-algorithms do.This fully encapsulates the PCNR (Predictor/Corrector Newton-Raphson) method of Aadithya, Keiter & Mei for replacing device limiting in circuit simulation: predictor = the ordinary Newton step on the (user-formulated) augmented system, corrector = the
postconditionlimiter, and PCNR's consistency property holds structurally because residual/Jacobian are only ever evaluated at corrected iterates. (postconditionis the analogue of PETSc SNES post-check.)Changes
2.35.0 → 2.36.0): newsrc/conditioning.jl(PreconditionWrapper,needs_conditioning,transform_conditioned_problem,apply_postcondition!!,supports_postconditiontrait,ComposedPreconditionsentinel); transform applied at thesolve_call/init_call/__solvefunnels (before the bounds transform); poly-algorithm trait; new names declaredpublicwith docstrings; SciMLBase compat3.37.2.2.1 → 2.3.0), NonlinearSolveQuasiNewton (1.14.1 → 1.15.0), NonlinearSolveSpectralMethods (1.7.3 → 1.8.0):apply_postcondition!!at each iterate-commit point (LineSearch/TrustRegion/None branches; TrustRegion re-evaluatesfuat the corrected iterate),supports_postcondition = true.2.13.0 → 2.14.0): the bypassCommonSolve.solveentries apply the conditioning transform, sopreconditiongenuinely composes there andpostconditionerrors instead of being silently ignored.4.21.0 → 4.22.0): new tutorialdocs/src/tutorials/nonlinear_preconditioning.md(registered inpages.jl) with a PCNR circuit-limiting walkthrough.Design notes
preconditionwith aComposedPreconditionsentinel, notnothing:remake(prob; f = new_f)merges function fields and letsnothingfields fall back to the old function's values, which would resurrect the hook and double-wrap (caught by an integration test that inspects the wrapper type).postcondition+lb/ubbounds throws: the bounds transform changes the iterate coordinates the hook would act on.precondition, user-suppliedjac/jvp/vjp/jac_prototype/sparsityare documented to describe the composed residual; the default AD path differentiates the composition automatically.sol.residfor preconditioned problems is the preconditioned residual (what the solver measured).Verification (all run locally, Julia 1.12.4, against SciMLBase#1449)
NonlinearSolveBaseCore (incl. new transform unit tests)NonlinearSolveFirstOrderCore (incl. new conditioning integration tests)NonlinearSolveQuasiNewtonCore (incl. new postcondition test)NonlinearSolveSpectralMethodsCore (incl. new DFSane postcondition test)SimpleNonlinearSolveCore (incl. new compose/throw tests)NonlinearSolveumbrella Core (incl. default-polyalg hook test)@exampleblocks executed verbatimMeasured behavior (verified by running, not estimated):
v0 = 2.0V: plainNewtonRaphson57 steps →asinh-preconditioned 9 steps.Vs=5,R=1k, augmented[v, vj],pnjlimpostcondition) from zeros: plain 179 steps → limited 11 steps; TrustRegion and LevenbergMarquardt also converge with the hook.maxiters=15: plain fails,asinh-preconditioned succeeds (proves the composition actually applies on the bypass path).Process log
NonlinearFunctionin SciMLBase (companion PR), then implemented G as an init-time problem transformation and H as commit-point application, following the existing bounds-transform architecture.remakefield-merge resurrecting the consumed hook (fixed with sentinel), (b) SimpleNonlinearSolve bypassing the funnels (fixed at its entry points, with an error guard for H).G = r/(1+|r|)on a residual linear inumakes Gauss-Newton diverge (derivative decays faster than the function) — replaced with a row re-weightingGand documented the "compress selectively" guidance in the tutorial.🤖 Generated with Claude Code
https://claude.ai/code/session_01B9GHoo4pFa3DU9yhyTkevL