Skip to content

ArcLengthContinuation: bordered tangent solve + θ-weighted arclength metric (#1020 item 3, Priorities 3+4) - #1021

Merged
ChrisRackauckas merged 4 commits into
SciML:masterfrom
ChrisRackauckas-Claude:arclength-bordered-theta
Jul 10, 2026
Merged

ChrisRackauckas merged 4 commits into
SciML:masterfrom
ChrisRackauckas-Claude:arclength-bordered-theta

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Important

This PR should be ignored until it has been reviewed by @ChrisRackauckas. Draft, not ready for merge.

Implements roadmap item 3 of the top-3 recommendation in #1020: Priority 3 (bordered linear solve for the arclength tangent) and Priority 4 (θ-weighted arclength metric), shipped together as one "arclength at scale" change since they share the same semantic break.

Part A — bordered tangent solve replacing nullspace (Priority 3)

_arclength_tangent computed the path tangent of the :tangent predictor as LinearAlgebra.nullspace(J): a dense SVD — O(n³) with a large constant, allocating, and erroring for sparse J. It is replaced by the standard bordered solve (BifurcationKit Bordered tangent; Govaerts 1991 block elimination):

[J; wᵀ] t = e_{n+1},    w = θ-metric weighting of τ_prev

i.e. J t = 0 with the normalization ⟨τ_prev, t⟩_θ = 1, via one LU factorization, then θ-normalized and oriented by sign(⟨t, τ_prev⟩_θ). The previous tangent τ_prev was already threaded through the driver as the orientation reference (the pure-λ seed provides the first bordering row), so no new state is needed.

SVD fallback kept, never on the happy path: the bordered matrix is singular exactly when τ_prev is θ-orthogonal to the true tangent (e.g. the pure-λ first-step seed meeting a fold head-on) or at a branch point where null(J) is 2-dimensional — the case for which BifurcationKit refuses the bordered tangent. A failed (issuccess(lu(B; check = false)) == false) or non-finite bordered solve falls back to nullspace(Matrix(J)), which stays robust there.

Reproduced timing (this machine, random n×(n+1) augmented Jacobians, 5-run averages, matching the issue's benchmark protocol; issue measured 11–17×):

n= 200  nullspace(SVD):    17.56 ms   1908 KiB | bordered LU:    1.565 ms    638 KiB  (11.2x)
n= 800  nullspace(SVD):   309.66 ms  30132 KiB | bordered LU:   35.649 ms  10051 KiB  ( 8.7x)
direction agreement (θ-dot of the two unit tangents): 1 - |⟨t_svd, t_lu⟩_θ| = 0.0

The bordered path also works verbatim with sparse Jacobians / any LU-capable backend, unlocking sparse :tangent mode (nullspace throws for sparse input).

Part B — θ-weighted arclength metric (Priority 4) — semantic change

Every place the path metric appeared — the Keller constraint row in AugmentedHomotopy, tangent/secant normalization, orientation sign, realized chord length, and the bend-angle cosang — used the unweighted Euclidean dot on [u; λ], so for large n the state block swamps the single λ component. All of them now consistently use the BifurcationKit DotTheta convention:

⟨(u₁,λ₁), (u₂,λ₂)⟩_θ = (θ/n)·⟨u₁,u₂⟩ + (1−θ)·λ₁λ₂,    n = length(u)

with a new theta kwarg on ArcLengthContinuation (default 0.5, validated 0 < θ < 1). The 1/n normalization is the crucial automatic default: it makes the u-block/λ balance independent of system size.

⚠️ What this changes numerically: ds-valued options (initial_step_factor, min_ds, max_step_factor) and max_angle are now measured in the weighted metric, not the Euclidean one. For a pure-λ motion a weighted ds corresponds to a λ-distance of ds/sqrt(1−θ); for pure-u motion, a Euclidean u-distance of ds/sqrt(θ/n). Values tuned against the previous unweighted geometry may need rescaling. This is documented in a dedicated "metric" admonition in the ArcLengthContinuation docstring. Final roots are unchanged on all existing tests (see below) — only the step geometry along the way changes.

The weighted dot is written on views (no allocation, no eltype forcing), so ForwardDiff duals pass through the constraint row and Float32 problems stay Float32 (tested).

Tests

New test/Core/arclength_tests__item7.jl (registered in the core closure after item 6):

  • unit tests of _bordered_tangent on known 1×2 Jacobians: Jτ = 0, θ-unit normalization, orientation, agreement with the analytic null direction at a regular point, and the ~vertical-in-λ tangent at the fold of u³ − 3u = −3 + 6λ;
  • SVD fallback engaging without error on the exactly-singular bordered system (τ_prev θ-orthogonal to the tangent), unit-level and end-to-end (fold exactly at the start point on u² = λ with :tangent);
  • theta validation throws on 0, 1, -0.5, 1.5; default and θ = 0.8 land on the previous roots of the S-fold and monotone standard problems (values, not step counts);
  • an n = 50 fold-free coupled system solved with :tangent (exercises the 1/n-scaled metric + bordered solve at moderate n);
  • Float32 :tangent preserves eltype.

All existing arclength (items 1–6) and homotopy-sweep (items 1–21) test files pass locally unmodified — no recalibration was needed: at n = 1 (most existing tests) θ = 0.5 scales the metric uniformly by √0.5, which changes effective step sizes but no test asserted absolute step geometry; all assertions are on final roots, retcodes, and qualitative path behavior (fold rounding), which are unchanged.

Fixes part of #1020.

🤖 Generated with Claude Code

ChrisRackauckas and others added 2 commits July 10, 2026 03:33
…metric

Implements Priorities 3+4 of the homotopy roadmap (SciML#1020) as one change,
since they share the same semantic break in the arclength metric.

Part A: the :tangent predictor's path tangent is now computed by the
bordered linear solve [J; wᵀ] t = e_{n+1} (one LU factorization, w = the
θ-weighted previous tangent, i.e. J t = 0 normalized by ⟨τ_prev, t⟩_θ = 1;
BifurcationKit's Bordered tangent / Govaerts bordering) instead of the
dense SVD of nullspace(J). Measured 11.2x faster at n = 200 and 8.7x at
n = 800 on random augmented Jacobians, and works verbatim with sparse
Jacobians (nullspace throws for sparse input). The SVD null space is kept
as a fallback that engages only when the bordered matrix is
(near-)singular — τ_prev θ-orthogonal to the tangent (e.g. the pure-λ
first-step seed meeting a fold head-on) or a genuine branch point — and
never runs on the happy path.

Part B: the Keller constraint row, tangent/secant normalization,
orientation sign, chord length, and bend-angle test all now use the
BifurcationKit DotTheta metric ⟨(u₁,λ₁),(u₂,λ₂)⟩_θ = (θ/n)⟨u₁,u₂⟩ +
(1−θ)λ₁λ₂ (new `theta` kwarg, default 0.5, validated 0 < θ < 1) instead
of the unweighted Euclidean dot on [u; λ], whose u-block swamps λ at
large n. The 1/n normalization makes the balance size-independent. This
changes what ds-valued options (initial_step_factor, min_ds,
max_step_factor) and max_angle mean numerically; documented in a metric
admonition in the docstring. The weighted dot is view-based, so
ForwardDiff duals pass through the constraint row and Float32 stays
Float32.

All existing arclength (items 1-6) and homotopy-sweep (items 1-21) test
files pass unmodified; new arclength_tests__item7.jl covers the bordered
tangent on known Jacobians (including the vertical-in-λ fold tangent),
the SVD fallback unit-level and end-to-end, theta validation and root
regressions, an n = 50 system, and Float32 eltype preservation.

Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Chris Rackauckas <[email protected]>
Per review, the tangent path allocated per predictor step instead of reusing
memory: the weighted border row (`similar`), the bordered matrix (`vcat`),
the rhs (`zeros`), the LU factorization (which copies B again), the solution
(`\`), and the normalized copy (`./`) — about 2(n+1)² + several (n+1) vectors
per step. Now `B` and `t` are scratch buffers allocated once in solve and
reused: the border row is written directly into B's last row, `lu!`
factorizes in place (its internal pivot vector is the only remaining
happy-path allocation), `ldiv!` solves into `t`, and normalization and
orientation are in place; the result is `copyto!`d into the driver's stable
τ buffer (safe: τprev is only read before the copy). The secant branch's
per-step chord temporaries reuse the same scratch. J (the Jacobian cache's
buffer) is never destroyed — B holds the copy — so the SVD fallback still
reads it.

Measured (n = 200 coupled system, whole solve): tangent 75196 -> 72316 KiB,
the delta matching the eliminated per-step bordered-matrix/LU copies;
S-fold roots identical for both predictors. The remaining bulk is the
corrector-side per-step rebuild, which is roadmap SciML#1020 Priority 6, not this
PR.

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

Copy link
Copy Markdown
Member Author

Both review items addressed, and the branch is rebased for merge (0e031500, MERGEABLE on current master):

Allocations in the solve — confirmed and fixed

The bordered-tangent path did allocate per predictor step rather than reusing memory: the weighted border row (similar), the bordered matrix (vcat), the rhs (zeros), the LU factorization (which copies B again), the \ solution, and the normalized copy — ~2(n+1)² + several (n+1) vectors per step. Now (0e031500):

  • B and t are scratch buffers allocated once in solve and reused every step: the border row is written directly into B's last row, lu! factorizes in place, ldiv! solves into t, normalization/orientation are in place, and the result is copyto!d into the driver's stable τ buffer (τprev is only read before the copy, so aliasing is safe). The only remaining happy-path allocation is lu!'s internal pivot vector (O(n) Ints).
  • The secant branch's per-step chord temporaries reuse the same scratch; τ is now a stable buffer written in place on every predictor path.
  • J (the Jacobian cache's buffer) is never destroyed — B holds the copy — so the SVD fallback still reads it.

Measured (n = 200 coupled system, whole solve): tangent 75196 → 72316 KiB, the delta matching exactly the eliminated per-step matrix/LU copies; S-fold roots identical for both predictors. The remaining bulk of arclength's solve allocation is the corrector-side per-step rebuild (fresh augmented problem + inner solve each step) — that predates this PR and is roadmap #1020 Priority 6, kept separate.

Rebase

Rebased onto master with #1022 and #1024 now merged — four conflicts resolved as kwarg/registration unions (the constructor now carries both tracking_maxiters and theta; test registration keeps arclength item7 alongside the jac/effort/polyalg files). The _bordered_tangent unit tests in item7 were updated to the new in-place API.

Verified on the exact pushed code (Julia 1.11): homotopy_sweep items 1–21 + arclength items 1–7 + jac + effort 1–2 + polyalg — 268 assertions in the main battery + 34 in the updated arc7 (which unit-tests the in-place bordered solve, the SVD fallback, and the θ metric) — all pass; Runic clean.

Per review, the tangent's bordered solve now runs through
`construct_linear_solver` — the same LinearSolve.jl-backed cache the Newton
descent methods use — instead of raw lu!/ldiv!: the cache is built once per
solve and re-driven each step (`lincache(; A, b, linu)`), giving backend
selection, factorization-workspace reuse, and shared NLStats accounting
(one stats object now spans the tangent's Jacobian cache and linear cache).
ArcLengthContinuation gains the standard `linsolve` kwarg, forwarded to both
the bordered solve and construct_jacobian_cache. When the LinearSolve
extension is not loaded (NonlinearSolveBase only weak-depends on it) the
workspace falls back to the \\-backed native cache; the ext now defines
`Utils.is_extension_loaded(::Val{:LinearSolve})` so that is detectable.

Subtlety this surfaced: LinearSolve's default solver rescues a singular LU
with pivoted QR and returns a finite least-squares pseudo-solution marked
successful, which silently defeated the designed-singular -> SVD-fallback
routing (caught by the item7 fold-at-start test: the tracker got a bogus
pure-λ tangent). Acceptance is now gated on a backend-agnostic residual
check (Govaerts-style): ‖[J t; ⟨w,t⟩ − 1]‖ must be at rounding level, and it
is computed from `J` and `τprev` — not `B`, which the factorization may
destroy.

Verified (Julia 1.11): full battery (sweep 1-21, arclength 1-7 incl. the new
`linsolve = QRFactorization()` coverage and the restored SVD-fallback unit
tests, jac, effort 1-2, polyalg) 298/298; Runic clean.

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

Copy link
Copy Markdown
Member Author

Per review, pushed 9fc46e8a: the bordered tangent solve now runs through the shared Newton linear-solve stack instead of raw lu!/ldiv!:

  • The workspace holds a construct_linear_solver cache — the same LinearSolve.jl-backed machinery the descent methods use — built once per solve and re-driven each step via lincache(; A, b, linu, reuse_A_if_factorization = false). Backend selection, factorization-workspace reuse, and NLStats accounting (one shared stats object across the tangent's Jacobian cache and linear cache) all come from the stack.
  • ArcLengthContinuation gains the standard linsolve kwarg (like NewtonRaphson et al.), forwarded to both the bordered solve and construct_jacobian_cache. Covered by a new item7 test with linsolve = QRFactorization().
  • NonlinearSolveBase only weak-depends on LinearSolve, so when the extension isn't loaded the workspace falls back to the \-backed NativeJLLinearSolveCache (same abstraction); the ext now defines Utils.is_extension_loaded(::Val{:LinearSolve}) to make that detectable.
  • Subtlety this surfaced: LinearSolve's default solver rescues a singular LU with pivoted QR and returns a finite least-squares pseudo-solution with a success retcode — which silently defeated the designed-singular → SVD-fallback routing (item7's fold-at-start test caught the tracker receiving a bogus pure-λ tangent). Acceptance is now gated on a backend-agnostic Govaerts-style residual check (‖[J·t; ⟨w,t⟩−1]‖ at rounding level vs O(1) for the pseudo-solution), computed from J/τprev rather than B (which the factorization may destroy).

Verified on the pushed code (Julia 1.11): full battery — sweep items 1–21, arclength 1–7 (incl. the restored SVD-fallback unit tests + the linsolve test), jac, effort 1–2, polyalg — 298/298; Runic clean. Still MERGEABLE on current master.

Spell Check reads `bord!` as a misspelling; rename the item7 unit-test helper
to `unit_tangent!`. No behavior change; item7 passes 36/36 locally.

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 09:29
@ChrisRackauckas
ChrisRackauckas merged commit b06cb2c into SciML:master Jul 10, 2026
13 of 15 checks passed
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.

3 participants