Skip to content

ArcLengthContinuation: consume jac/jac_prototype/sparsity/colorvec (#1020 item 2 follow-up, stacked on #1021) - #1025

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:arclength-jac-fields
Jul 11, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:arclength-jac-fields

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Jul 10, 2026 •

Copy link
Copy Markdown
Member

Important

Please ignore this PR until it has been reviewed by @ChrisRackauckas. Opened as a draft by an agent; do not merge or act on it before that review.

Implements derivative-field consumption (jac / jac_prototype / sparsity / colorvec) for ArcLengthContinuation — item 2 of the #1020 roadmap for the arclength driver, i.e. the follow-up explicitly left out of #1022 (which did the same for the sweep drivers, HomotopySweep / SimpleHomotopySweep).

Stacking

This PR is stacked on #1021. #1021 has merged; this branch is now rebased directly onto master and carries only its own commits (the derivative-field work plus the structural_sparse docstring/test correction from review). Full arclength suite re-run locally post-rebase: 145/145 pass.

Why this needs more than the sweep's λ-fixing wrapper

The user's λ-extended jac(u, p, λ) provides only the n×n ∂H/∂u block, but the arclength driver differentiates two larger systems over the packed variable x = [u; λ]:

  1. Tangent predictor — needs the n×(n+1) augmented path Jacobian [∂H/∂u | ∂H/∂λ].
  2. Augmented corrector — needs the (n+1)×(n+1) bordered Jacobian [∂H/∂u ∂H/∂λ; wᵀ] where w is the θ-weighted Keller constraint row.

Design

  • ∂H/∂λ column: it is a scalar-parameter derivative, so it is computed as one forward-mode derivative of the residual in λ at fixed u (DifferentiationInterface derivative of a λ-shifted residual with (u, p) as Constant contexts, prepared once per solve and reused) — never a wholesale differentiation of the packed system. AugmentedPathJac assembles [user ∂H/∂u | ∂H/∂λ] under the standard 2/3-argument Jacobian convention and is wired as the analytic jac of the packed HomotopyResidual NonlinearFunction, so the :tangent predictor consumes it through construct_jacobian_cache's existing has_jac path with no changes to the Jacobian tooling.
  • Corrector Jacobian: AugmentedHomotopyJac embeds the same path Jacobian as its top n rows and writes the constraint row analytically. It reads the same in-place-updated τ buffer that the per-step AugmentedHomotopy residual reads (mirroring the base branch's τ threading), so the Jacobian and residual can never desynchronize; ds/xcur do not enter the Jacobian because the constraint is affine in x.
  • Bordered prototypes: jac_prototype / matrix sparsity are extended to the augmented shapes — a structurally dense ∂H/∂λ column for the predictor (n×(n+1)), plus the structurally dense constraint row for the corrector ((n+1)×(n+1), i.e. [proto col; rowᵀ 1]). Sparse/structured prototypes are promoted to SparseMatrixCSC — a bordered Tridiagonal is no longer tridiagonal, and CSC is what the coloring and sparse-LU machinery handle. The conversion goes through a new Utils.structural_sparse (SparseArrays extension) that preserves the structural pattern: sparse(x) would silently drop zero-valued band entries (the standard Tridiagonal(zeros, ones, zeros) prototype idiom). Without SparseArrays loaded, structured prototypes fall back to a dense bordered prototype. Dense prototypes stay dense; prototypes are not eltype-promoted with λ (same documented choice as Sweep drivers: consume jac/jac_prototype/sparsity/colorvec from the NonlinearFunction (#1020 item 2) #1022).
  • sparsity / colorvec: a sparsity detector is forwarded unchanged (it detects the augmented pattern from the augmented residual itself); a matrix sparsity is borderized, preserving the sparsity === jac_prototype object identity the NonlinearFunction constructor establishes (required by construct_concrete_adtype). The user colorvec is forwarded to the predictor's system extended by one fresh color for the dense λ column (valid for column coloring); it is not forwarded to the corrector — the structurally dense constraint row makes every pair of columns conflict, so no nontrivial column coloring exists for the bordered pattern, and its coloring (when sparse AD applies) is recomputed. The bordered prototype's value for the corrector is chiefly the sparse linear solve.
  • Anchor/landing solves: the λ-fixed solves now reuse _sweep_nonlinear_function from Sweep drivers: consume jac/jac_prototype/sparsity/colorvec from the NonlinearFunction (#1020 item 2) #1022 (λ-fixed FixLambdaJac, fields forwarded unchanged), so the whole driver is consistent.
  • No-field problems: every construction branches on the problem's type exactly like _sweep_nonlinear_function and is byte-identical to the base branch when no derivative field is present. Verified by running a residual-counting closure on the S-fold problem against the unmodified base branch: secant 203 evals / tangent 158 evals and bit-identical roots on both branches.

Same SciMLBase wrinkle as #1022: NonlinearFunction's arity-based conformance check rejects bare λ-extended jacs, so the tests supply loudly-erroring dummy λ-free methods. A SciMLBase-side fix is in flight separately; nothing here waits on it.

Tests

New test/Core/arclength_jac_tests__item1.jl, registered in the core group right after homotopy_jac_tests__item1.jl:

  • analytic oop and iip jac consumed via the augmented corrector (inner = NewtonRaphson(), default :secant): counters > 0, jac evaluations at interior λ (only the corrector evaluates there — anchor/landing sit at exactly λ = 0/1), S-fold root correct to 1e-4;
  • analytic jac consumed via the :tangent predictor: counter > 0, root correct to 1e-4;
  • analytic-jac solutions match the AD solutions (both predictors);
  • augmented/bordered prototype structure: zero-valued Tridiagonal bands survive as pattern, dense λ column + dense constraint row present, CSC promotion, CSC prototypes border without conversion;
  • n = 50 tridiagonal fold-free system with :tangent: jac + prototype (asserts the corrector's Jacobian buffer is the bordered (n+1)×(n+1) SparseMatrixCSC and the anchor kept the user's own Tridiagonal), and prototype + colorvec without an analytic jac (sparse AD end-to-end); both solve to residual < 1e-8;
  • no-field constructions are type-identical to the bare ones; no-field solves are deterministic in root and residual-eval count (and match the base branch, see above).

SparseArrays (stdlib) and SparseMatrixColorings were added to the root [extras]/test target for the sparse assertions. No version bumps.

What ran locally (Julia 1.11.9)

  • New test file standalone: 40/40 pass.
  • Full battery with absolute @safetestset include paths: homotopy_sweep_tests__item{1–21}, arclength_tests__item{1–7}, homotopy_jac_tests__item1, arclength_jac_tests__item1 (new), homotopy_effort_tests__item{1,2}, homotopy_polyalg_tests__item1 — 338/338 pass, exit 0 (33 items; the battery was run twice — 336/336 on the previous base tip, and 338/338 after rebasing onto the base's latest LinearSolve-backed bordered-solve commit, which added 2 tests).
  • Base-branch comparison run for the no-field counting closure (see above).
  • Runic --inplace then --check (exit 0) on all touched files.

Note: this branch is rebased onto the pushed arclength-bordered-theta tip (currently the LinearSolve-backed bordered-solve review commit), not the stale local copy it started from.

🤖 Generated with Claude Code

ChrisRackauckas and others added 2 commits July 10, 2026 08:04
…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]>
…gression 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]>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 11, 2026 03:58
@ChrisRackauckas
ChrisRackauckas merged commit 7ff3239 into SciML:master Jul 11, 2026
90 of 93 checks passed
ChrisRackauckas added a commit that referenced this pull request Jul 11, 2026
…l) (#1028)

Add an opt-in `tracking_abstol` kwarg to HomotopySweep and
SimpleHomotopySweep (#1020 Priority 5): interior tracking steps only need
enough accuracy to keep the predictor useful and stay inside the next
corrector's convergence basin, so they may run at a loose absolute
tolerance while the returned solution still satisfies the user's full
tolerances.

Mirroring the tracking_maxiters precedent exactly:

- The λspan[1] anchor solve and the final landing on λspan[2] are exempt.
  HomotopySweep bakes the loose abstol into its single shared inner cache
  (`abstol` cannot be changed through `reinit!` — the polyalgorithm
  cache's `InternalAPI.reinit!` accepts only `p`/`u0`), so the anchor
  runs as a standalone full-tolerance solve and the landing is
  re-polished at the full tolerance warm-started from the loose landing
  solve (~1-2 extra corrector iterations). SimpleHomotopySweep's steps
  are fresh standalone solves, so the exemption is simply not splicing
  the loose kwargs there.
- An explicit user-passed `abstol` OR `reltol` (solve kwarg or problem
  kwarg) disables the loosening entirely: splicing a loose abstol next
  to a user reltol would let the loose criterion fire first in the
  default OR-combined termination modes.
- Constructor validation consistent with tracking_maxiters
  (positive or `nothing`).

Default is `nothing` (disabled): the loose interior iterates slightly
degrade the secant/quality signals the step controller reads, so
loosening stays opt-in; 1e-4-1e-6 are good starting values (Bertini
tracks at 1e-5/1e-6 with only the endgame tight; HomotopyContinuation.jl
accepts on Newton contraction and polishes endpoints).

Measured at tracking_abstol = 1e-4 (Julia 1.12): scalar cubic with
NewtonRaphson inner 53 -> 43 residual calls; n = 50 coupled cubic
1247 -> 835 (-33%, -37% wall time; default polyalg inner 2122 -> 1442);
SimpleHomotopySweep scalar path 43 -> 27 (-37%, -35% wall time). The
SimpleHomotopySweep StaticArray/scalar paths remain allocation-free
(AllocCheck) with the loosening active.

ArcLengthContinuation intentionally untouched (churning in #1025); it
gets the same treatment in a follow-up.

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
…1020 P6, stacked on #1025) (#1029)

* ArcLengthContinuation: reuse ONE corrector cache across continuation steps

Bring the arclength corrector to parity with the HomotopySweep cache
driver (#1020 P6): the corrector's Newton cache — and with it the
Jacobian cache and linear-solve cache — is init-ed once around a single
AugmentedHomotopy residual and reused every step via reinit!/solve!,
instead of rebuilding the problem/function/cache per corrector attempt.

- AugmentedHomotopy becomes mutable-with-const-fields (like the sweep's
  FixLambda): τ and xcur alias stable driver buffers updated in place,
  ds is a field write, so advancing the continuation never re-creates
  the residual or anything compiled around it.
- The constraint row evaluates ⟨τ, x - x0⟩_θ through _theta_dot_shifted
  without materializing x .- xcur (which heap-allocated per inner
  residual call, in Dual eltype under the corrector's AD).
- Glue temporaries get preallocated buffers: xpred, the realized chord
  (reusing the secant scratch), the packed history shift, the accepted
  u-block, and the landing warm start; the packed point and pure-λ seed
  are built without the splatting pack() helper.
- When tracking_maxiters is active the cap is baked into the one cache
  (reinit! preserves cache.maxiters); the anchor and landing solves keep
  the user's full budget as before.

Measured per continuation step on the n = 50 coupled cubic system
(item7), fixed-ds slope between 50- and 250-step runs, Julia 1.12:

  iip  secant   274 KB -> 56 KB
  iip  tangent  295 KB -> 77 KB
  oop  secant   441 KB -> 160 KB
  oop  tangent  507 KB -> 226 KB

The remainder is the inner polyalgorithm's own per-iteration internals
(LU workspace, quasi-Newton updates), shared with the sweep — the sweep
itself measures ~74 KB/step on the same problem. A regression test
(arclength_tests__item8.jl) bounds the post-warmup per-step slope well
under half the uncached cost.

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

* item8: attribute the residual per-step allocations precisely (review)

Profile.Allocs attribution: the driver glue is < 1 KB/step; the bulk is
LinearSolve's alias_A-gated lu(A) copy (~21.5 KB) and Broyden's
maybe_pinv!! initial J⁻¹ (~21.5 KB), both shared Newton-stack behavior
addressed in follow-ups, not driver work.

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]>
ChrisRackauckas added a commit that referenced this pull request Jul 11, 2026
…rseArrays ext (#1035)

GROUP=QA for lib/NonlinearSolveBase errors on master (introduced by
7ff3239, PR #1025, which added Utils.structural_sparse and
Utils.is_extension_loaded to the SparseArrays extension):

- all_qualified_accesses_via_owners: `ArrayInterface` accessed via
  `NonlinearSolveBase` instead of its owner
- all_qualified_accesses_are_public: `ArrayInterface`,
  `is_extension_loaded`, `structural_sparse` non-public accesses

Fix the first by importing ArrayInterface directly (it is a hard dep of
NonlinearSolveBase; the ForwardDiff/LinearSolve extensions already do
`using ArrayInterface: ArrayInterface`), so `findstructralnz` is now
reached through its owner, where it is declared public (7.26+).

`is_extension_loaded` and `structural_sparse` are NonlinearSolveBase's
own internal extension hooks defined via qualified `Utils.` names, the
same pattern as the sibling hooks (make_sparse, condition_number,
maybe_symmetric, ...) that already sit in the qa.jl
all_qualified_accesses_are_public ignore list; add them there alongside.

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