Skip to content

Sweep drivers: consume jac/jac_prototype/sparsity/colorvec from the NonlinearFunction (#1020 item 2) - #1022

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:homotopy-jac-passthrough
Jul 10, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:homotopy-jac-passthrough

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Important

This PR should be ignored until reviewed by @ChrisRackauckas.

Implements roadmap item Priority 2 of #1020 for the sweep drivers: HomotopySweep and SimpleHomotopySweep now consume the derivative fields of the problem's NonlinearFunction instead of silently dropping them.

What changed

Both drivers previously built SciMLBase.NonlinearFunction{iip}(FixLambda(prob.f, λ)), discarding jac, jac_prototype, sparsity, and colorvec — so a user's analytic Jacobian was ignored (the default polyalgorithm's must_use_jacobian never fired) and sparse systems were differentiated dense at every Newton iteration of every step.

  • lib/NonlinearSolveBase/src/homotopy_sweep.jl (HomotopySweep): new FixLambdaJac wrapper λ-fixes the user's jac(u, p, λ) / jac(J, u, p, λ) into the standard 2/3-argument form. It reads λ from the residual's own mutable FixLambda rather than carrying a copy, so advancing the sweep remains a single field write and the Jacobian can never be evaluated at a different λ than the residual (no second λ state to desynchronize). jac_prototype, sparsity, and colorvec are forwarded unchanged.
  • lib/SimpleNonlinearSolve/src/simple_homotopy_sweep.jl (SimpleHomotopySweep): the immutable SimpleFixLambda is rebuilt per step, so the jac wrapper is a second SimpleFixLambda(f.jac, λfix) constructed at the same site from the same λfix value — rebuilt together, they cannot desynchronize.
  • When no derivative field is present, both drivers construct the inner function exactly as before (NonlinearFunction{iip}(wrapper), positional only). The branch is decided by the problem's type, so the unused arm is compiled away and the StaticArray/scalar SimpleHomotopySweep path stays AllocCheck-clean.
  • The jac's iip-ness follows the residual's (NonlinearFunction{iip} governs both; the varargs wrappers serve either arity).
  • Eltype choice (documented in the docstrings): jac_prototype is forwarded unchanged — it is not eltype-promoted with λ. Users whose λ precision differs from u0's should supply a prototype whose eltype matches the promoted residual eltype.
  • Docstrings of HomotopySweep and SimpleHomotopySweep now state the fields are consumed, mirroring the SciMLBase.HomotopyProblem docstring's λ-extended-convention wording (which currently ends with "continuation solvers do not consume them yet" — that phrase can be updated in SciMLBase once this merges).

Verified locally (all run, outputs observed)

  • New test file test/Core/homotopy_jac_tests__item1.jl (registered in test/runtests.jl's core closure), covering: counting oop/iip analytic jacs consumed via HomotopySweep(; inner = NewtonRaphson()) and via the default polyalgorithm; analytic jac reproduces the AD solution to 1e-12; SimpleHomotopySweep(; inner = SimpleNewtonRaphson()) consumes oop and iip analytic jacs (verified empirically — SimpleNonlinearSolve's Newton honors f.jac through SciMLBase.has_jac); a 50-dim tridiagonal system with jac_prototype = Tridiagonal(...) where the inner solver's J is asserted to be <: Tridiagonal; prototype-only (no analytic jac) passthrough; unit check that field-free problems produce the exact pre-change inner-function type; and a residual-count determinism check for field-free problems.
  • Full homotopy/arclength/polyalg suite (rerun after rebasing onto Add HomotopyPolyAlgorithm: staged polyalgorithm for HomotopyProblem #1019's HomotopyPolyAlgorithm merge): new file + homotopy_sweep_tests__item1–21.jl + arclength_tests__item1–6.jl (190/190 pass) + homotopy_polyalg_tests__item1.jl (25/25 pass), all via @safetestset on this branch.
  • No-field parity vs unmodified master (measured with a counting residual closure, identical script on both): HomotopySweep u=1.2599210498948732, 86 residual calls on master and on this branch; SimpleHomotopySweep u=1.2599210498948732, 73 calls on both — byte-identical behavior.
  • NONLINEARSOLVE_TEST_GROUP=Core Pkg.test() from lib/SimpleNonlinearSolve: pass (totals in CI log below).
  • lib/SimpleNonlinearSolve/test/alloc/allocation_tests__item2.jl in its own env: 2/2 pass — the field-free SimpleHomotopySweep path is still AllocCheck-clean.
  • Runic --inplace then --check (exit 0) on all touched files.

Interface note (upstream wrinkle, not addressed here)

SciMLBase's NonlinearFunction constructor arity-checks jac with the standard nonlinear convention (isinplace(jac, 3, ...)), so a bare λ-extended jac (jac(u, p, λ) = 3 args oop, jac(J, u, p, λ) = 4 args iip) is rejected at user-side construction (NonconformingFunctionsError / TooManyArgumentsError) unless the jac also carries a λ-free method — MTK-style generated functions with dual dispatches pass naturally, and the tests use loud-error λ-free dummy methods. Relaxing that check for HomotopyProblem use is a SciMLBase follow-up.

Scope / coordination

  • arclength.jl is deliberately untouched: derivative-field support for ArcLengthContinuation (which needs the bordered prototype [J_proto col; rowᵀ 1]) is a follow-up, and a sibling PR is working in that file.
  • Sibling PRs are editing the controller regions of these same two sweep files; this diff is confined to the function-construction sites and docstrings, but sequential rebases may still be needed depending on merge order. (Already rebased once onto Add HomotopyPolyAlgorithm: staged polyalgorithm for HomotopyProblem #1019; the full suite was rerun after the rebase.)

🤖 Generated with Claude Code

…onlinearFunction

HomotopySweep and SimpleHomotopySweep previously built
NonlinearFunction{iip}(FixLambda(prob.f, λ)), dropping every derivative
field of the user's NonlinearFunction: an analytic Jacobian was silently
ignored (the default polyalgorithm's must_use_jacobian never fired) and
sparse systems were differentiated dense at every Newton iteration of
every continuation step.

The drivers now forward these fields into the inner NonlinearFunction. A
user jac following HomotopyProblem's λ-extended convention jac(u, p, λ) /
jac(J, u, p, λ) is λ-fixed into the standard 2/3-argument form: in
HomotopySweep via FixLambdaJac, which reads λ from the residual's own
mutable FixLambda (one λ state, so residual and Jacobian cannot
desynchronize); in SimpleHomotopySweep via a second immutable
SimpleFixLambda built at the same site from the same λ value each step.
jac_prototype, sparsity, and colorvec pass through unchanged (the
prototype is not eltype-promoted with λ; documented in the docstrings).

Problems without derivative fields construct the inner function exactly
as before — the branch is decided by the problem's type, so the
StaticArray/scalar SimpleHomotopySweep path stays AllocCheck-clean, and
result plus residual-call count are byte-identical to master.

ArcLengthContinuation is intentionally untouched (needs the bordered
prototype; follow-up).

Part of SciML#1020 (Priority 2).

Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 10, 2026 07:25
@ChrisRackauckas
ChrisRackauckas merged commit 44a5232 into SciML:master Jul 10, 2026
12 of 14 checks passed
ChrisRackauckas added a commit to SciML/SciMLBase.jl that referenced this pull request Jul 10, 2026
…yProblem (#1430)

HomotopyProblem documents that all functions of the wrapped
NonlinearFunction follow the λ-extended argument convention (residual
f(u, p, λ) / f(du, u, p, λ), jac jac(u, p, λ) / jac(J, u, p, λ)), and
NonlinearSolve.jl's continuation solvers consume the derivative fields
as of SciML/NonlinearSolve.jl#1022. But the NonlinearFunction
constructor validated jac/jvp/vjp against the standard nonlinear
arities, so a bare λ-extended jac was rejected at construction:
NonconformingFunctionsError for a 3-argument out-of-place jac and
TooManyArgumentsError for a 4-argument in-place jac. Users had to
define a dummy λ-free method on their jac just to get past the check.

Add a `lambda_extended` keyword (default `false`, not stored as a
field) to the NonlinearFunction constructor that shifts the jac/jvp/vjp
conformance arities by the one trailing λ argument, and shifts the
residual in-place detection in the type-parameter-free constructor the
same way. The standard checks are unchanged when the keyword is not
passed, and nonconforming λ-extended functions are still rejected with
the same errors.

Also update the HomotopyProblem docstring: the derivative fields are
now consumed by NonlinearSolve.jl's continuation solvers rather than
"not consumed yet".

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Jul 10, 2026
The typos spell-checker reads the "soop" (scalar/short oop) suffix in
test/Core/homotopy_jac_tests__item1.jl (merged in #1022) as a misspelling,
failing Spell Check on master and therefore on every open PR. Rename to
short_oop. No behavior change; the file passes 30/30 locally.

Co-authored-by: Claude <[email protected]>
Co-authored-by: Chris Rackauckas <[email protected]>
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/NonlinearSolve.jl that referenced this pull request Jul 10, 2026
…ciML#1020 item 2 follow-up)

The user's λ-extended `jac(u, p, λ)` / `jac(J, u, p, λ)` supplies only the n×n
∂H/∂u block, but the arclength driver differentiates two larger systems: the
packed path system behind the `:tangent` predictor (the n×(n+1) augmented
Jacobian [∂H/∂u | ∂H/∂λ]) and the (n+1)×(n+1) bordered corrector system with
the θ-weighted Keller constraint row.

∂H/∂λ-column design: the missing column is a scalar-parameter derivative, so
it is taken as ONE forward-mode derivative of the residual in λ at fixed u —
a DifferentiationInterface `derivative` of the λ-shifted residual with (u, p)
as `Constant` contexts, prepared once at solve start and reused every call —
rather than differentiating the packed system wholesale. `AugmentedPathJac`
assembles [user ∂H/∂u | ∂H/∂λ] under the standard 2/3-argument Jacobian
convention, so the tangent predictor consumes it through
`construct_jacobian_cache`'s ordinary `has_jac` path unchanged. The
corrector's `AugmentedHomotopyJac` embeds the same path Jacobian as its top
block and writes the constraint row analytically — it is the θ-weighted τ,
read from the same in-place-updated τ buffer the per-step residual reads, so
the Jacobian and the residual cannot desynchronize (`ds`/`xcur` do not enter:
the constraint is affine in x, its gradient independent of both).

Bordered prototype: `jac_prototype` / matrix `sparsity` are extended to the
augmented shapes — one structurally dense ∂H/∂λ column for the predictor's
n×(n+1) system, plus the structurally dense constraint row for the
corrector's bordered (n+1)×(n+1) system. Sparse/structured prototypes are
promoted to SparseMatrixCSC via the STRUCTURAL pattern
(`Utils.structural_sparse`, new in the SparseArrays extension — `sparse(x)`
would drop zero-valued band entries): a bordered Tridiagonal is no longer
tridiagonal, and CSC is what the coloring and sparse-LU machinery handle;
without SparseArrays loaded, structured prototypes fall back to a dense
bordered prototype. A sparsity detector is forwarded unchanged (it detects
the augmented pattern from the augmented residual itself). The user
`colorvec` is extended by one fresh color for the predictor's dense λ
column; it is not forwarded to the corrector, whose dense constraint row
admits no nontrivial column coloring — the bordered pattern's value there is
chiefly the sparse linear solve. The λ-fixed anchor/landing solves reuse the
sweep's `_sweep_nonlinear_function` forwarding from SciML#1022. When no derivative
fields are present, every construction is exactly the previous one (branches
decided by the problem's type), verified by unchanged residual-eval counts
against the base branch.

Tests: test/Core/arclength_jac_tests__item1.jl (registered in the core
group) covers analytic-jac consumption through the augmented corrector and
through the tangent predictor on the S-fold problem (root to 1e-4, counters
> 0, jac evaluations at interior λ prove the corrector consumed it),
analytic-vs-AD solution agreement, the augmented/bordered prototype
structure (zero-valued band survival, dense borders, CSC promotion), an
n = 50 tridiagonal fold-free system solved with `:tangent` both with
jac+prototype (asserting the corrector's Jacobian buffer is the bordered
(n+1)×(n+1) SparseMatrixCSC) and with prototype+colorvec without an analytic
jac, no-field construction type-identity, and no-field root/residual-count
determinism. SparseArrays and SparseMatrixColorings are added to the root
test target for the sparse assertions.

Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Chris Rackauckas <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Jul 11, 2026
…1020 item 2 follow-up, stacked on #1021) (#1025)

* ArcLengthContinuation: consume jac/jac_prototype/sparsity/colorvec (#1020 item 2 follow-up)

The user's λ-extended `jac(u, p, λ)` / `jac(J, u, p, λ)` supplies only the n×n
∂H/∂u block, but the arclength driver differentiates two larger systems: the
packed path system behind the `:tangent` predictor (the n×(n+1) augmented
Jacobian [∂H/∂u | ∂H/∂λ]) and the (n+1)×(n+1) bordered corrector system with
the θ-weighted Keller constraint row.

∂H/∂λ-column design: the missing column is a scalar-parameter derivative, so
it is taken as ONE forward-mode derivative of the residual in λ at fixed u —
a DifferentiationInterface `derivative` of the λ-shifted residual with (u, p)
as `Constant` contexts, prepared once at solve start and reused every call —
rather than differentiating the packed system wholesale. `AugmentedPathJac`
assembles [user ∂H/∂u | ∂H/∂λ] under the standard 2/3-argument Jacobian
convention, so the tangent predictor consumes it through
`construct_jacobian_cache`'s ordinary `has_jac` path unchanged. The
corrector's `AugmentedHomotopyJac` embeds the same path Jacobian as its top
block and writes the constraint row analytically — it is the θ-weighted τ,
read from the same in-place-updated τ buffer the per-step residual reads, so
the Jacobian and the residual cannot desynchronize (`ds`/`xcur` do not enter:
the constraint is affine in x, its gradient independent of both).

Bordered prototype: `jac_prototype` / matrix `sparsity` are extended to the
augmented shapes — one structurally dense ∂H/∂λ column for the predictor's
n×(n+1) system, plus the structurally dense constraint row for the
corrector's bordered (n+1)×(n+1) system. Sparse/structured prototypes are
promoted to SparseMatrixCSC via the STRUCTURAL pattern
(`Utils.structural_sparse`, new in the SparseArrays extension — `sparse(x)`
would drop zero-valued band entries): a bordered Tridiagonal is no longer
tridiagonal, and CSC is what the coloring and sparse-LU machinery handle;
without SparseArrays loaded, structured prototypes fall back to a dense
bordered prototype. A sparsity detector is forwarded unchanged (it detects
the augmented pattern from the augmented residual itself). The user
`colorvec` is extended by one fresh color for the predictor's dense λ
column; it is not forwarded to the corrector, whose dense constraint row
admits no nontrivial column coloring — the bordered pattern's value there is
chiefly the sparse linear solve. The λ-fixed anchor/landing solves reuse the
sweep's `_sweep_nonlinear_function` forwarding from #1022. When no derivative
fields are present, every construction is exactly the previous one (branches
decided by the problem's type), verified by unchanged residual-eval counts
against the base branch.

Tests: test/Core/arclength_jac_tests__item1.jl (registered in the core
group) covers analytic-jac consumption through the augmented corrector and
through the tangent predictor on the S-fold problem (root to 1e-4, counters
> 0, jac evaluations at interior λ prove the corrector consumed it),
analytic-vs-AD solution agreement, the augmented/bordered prototype
structure (zero-valued band survival, dense borders, CSC promotion), an
n = 50 tridiagonal fold-free system solved with `:tangent` both with
jac+prototype (asserting the corrector's Jacobian buffer is the bordered
(n+1)×(n+1) SparseMatrixCSC) and with prototype+colorvec without an analytic
jac, no-field construction type-identity, and no-field root/residual-count
determinism. SparseArrays and SparseMatrixColorings are added to the root
test target for the sparse assertions.

Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Chris Rackauckas <[email protected]>

* structural_sparse: correct the docstring premise, add the Diagonal regression it actually guards

Empirically, sparse() on band-copying structured types (Tridiagonal,
SymTridiagonal, Bidiagonal) preserves zero-valued structural entries —
the docstring's Tridiagonal justification was wrong. The type where the
plain conversion is value-based is Diagonal: sparse(Diagonal(zeros(n)))
has zero stored entries, which would silently drop the pattern sparse AD
coloring needs. Test the case that actually requires the helper.

Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Chris Rackauckas <[email protected]>

---------

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Jul 11, 2026
… NonlinearSolve 4.20.4 (#1034)

Registered versions (NLSB 2.33.0 on 2026-07-06) predate the merged
homotopy work: #1021, #1022, #1024, #1025, #1028, #1029 (NLSB features
=> minor), #1028's tracking_abstol on SimpleHomotopySweep (=> minor),
and #1027's SciMLBase 3.34 compat floor (top-level => patch).

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
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