Skip to content

Initialize HomotopyProblem init systems by continuation (default_nlsolve → nothing) - #3989

Merged
ChrisRackauckas merged 4 commits into
SciML:masterfrom
ChrisRackauckas-Claude:homotopy-init-default-nlsolve
Jul 23, 2026
Merged

ChrisRackauckas merged 4 commits into
SciML:masterfrom
ChrisRackauckas-Claude:homotopy-init-default-nlsolve

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Draft — please ignore until reviewed by @ChrisRackauckas.

Summary

Makes the DAE-initialization OverrideInit path initialize a SciMLBase.HomotopyProblem (and an SCCNonlinearProblem that contains homotopy blocks) by continuation, by having default_nlsolve return nothing for them.

Why

default_nlsolve picks the nonlinear algorithm the OverrideInit path solves the init problem with. A HomotopyProblem — built by ModelingToolkit when the init system carries Modelica homotopy(actual, simplified) operators (SciML/ModelingToolkit.jl#4766) — has a λ-swept residual f(u, p, λ); the operator is the user declaring the system needs continuation. Returning nothing routes solve(initprob, nothing) to the problem's own default (NonlinearSolveBase.HomotopyPolyAlgorithm), which sweeps λ from the simplified form to the target.

The alternative — the previous AbstractNonlinearProblem default, FastShortcutNonlinearPolyalg — under the companion SciML/NonlinearSolve.jl#1085 solves only the target-λ system directly (solve(::HomotopyProblem, ::standard_alg) fixes λ). That is a real bug, not just slower: on branch-selection it returns a Success retcode on the wrong root. Measured end-to-end on homotopy(m² − 1, 2(m−1)) from m = −3:

BEFORE (FastShortcut / target-λ Newton):  init = -1.0   ✗ Success on the unphysical root
AFTER  (nothing / continuation):          init = +1.0   ✔ physical root

An SCCNonlinearProblem with homotopy blocks is likewise solved with nothing, so each block picks its own default (homotopy blocks continue, plain blocks use the standard nonlinear default); a pure-NonlinearProblem SCC keeps FastShortcutNonlinearPolyalg.

Tests

test/homotopy_default_nlsolve_tests.jl (registered in runtests.jl): default_nlsolve returns nothing for a HomotopyProblem and for an SCC that has a homotopy block, and the usual FastShortcutNonlinearPolyalg otherwise. Verified locally (Julia 1.12): 8/8, plus the end-to-end wrong-root/right-root result above.

Companions

Runic-formatted.

🤖 Generated with Claude Code

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Companions: SciML/NonlinearSolve.jl#1085 and SciML/ModelingToolkit.jl#4766. All drafts; ignore until reviewed by @ChrisRackauckas.

…odiff

The DAE-initialization OverrideInit path picks the nonlinear algorithm for the init
problem via `default_nlsolve`. A `SciMLBase.HomotopyProblem` (built by ModelingToolkit
when the init system carries Modelica `homotopy(actual, simplified)` operators) must be
solved by continuation, not by a Newton polyalgorithm.

`default_nlsolve` now returns a `HomotopyPolyAlgorithm` (sweep, then pseudo-arclength) for a
`HomotopyProblem`, with the `autodiff` argument threaded into the continuation's *inner
corrector* (`FastShortcutNonlinearPolyalg`) — the differentiation the sweep and the
secant-predictor arclength stage actually use, and where a finite-diff initialization must
avoid dual-numbering a residual that is not ForwardDiff-safe. (A high-level `autodiff` knob
on `HomotopyPolyAlgorithm` itself cannot do this: it lives in NonlinearSolveBase, which
cannot construct the concrete inner solver, so the AD rides on the inner algorithm here.)

Handing a `HomotopyProblem` a plain Newton polyalgorithm instead — as the previous
`AbstractNonlinearProblem` default did — solves only the target-λ system directly (see the
companion `solve(::HomotopyProblem, ::AbstractNonlinearSolveAlgorithm)` in NonlinearSolve),
which on branch selection returns a `Success` retcode on the wrong root (measured `-1.0` vs
the physical `+1.0`).

An `SCCNonlinearProblem` with `HomotopyProblem` blocks is solved with `nothing` so each
block picks its own default (homotopy blocks continue, plain blocks Newton-solve). The
`autodiff` signal cannot be threaded per block from here — `SCCNonlinearSolve` applies one
algorithm to every block — so that is left to a follow-up (routing by block type in
`SCCNonlinearSolve`); the code comment records this.

Adds test/homotopy_default_nlsolve_tests.jl (including a ForwardDiff-hostile residual that
only the finite-diff inner can solve, proving the AD is genuinely threaded).

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01GSXiS4u4BaR2RvGFWGEQeH
@ChrisRackauckas-Claude
ChrisRackauckas-Claude force-pushed the homotopy-init-default-nlsolve branch from 574892c to 5cd5e7f Compare July 22, 2026 22:49
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Good catch — the nothing return dropped the autodiff argument. Fixed in 5cd5e7f.

Whole-system HomotopyProblem: instead of nothing, default_nlsolve now returns HomotopyPolyAlgorithm((HomotopySweep(; inner), ArcLengthContinuation(; inner))) where inner = FastShortcutNonlinearPolyalg(; autodiff = autodiff ? _tagged_autodiff(u, cs) : AutoFiniteDiff()). The AD rides the inner corrector — that's the differentiation the sweep and the (default secant-predictor) arclength stage actually use, so a finite-diff init no longer dual-numbers a non-ForwardDiff-safe residual.

On the 'high level finitediff option' question: I looked at putting an autodiff knob on HomotopyPolyAlgorithm itself, but it can't work cleanly — HomotopyPolyAlgorithm/HomotopySweep live in NonlinearSolveBase, which can't construct the concrete inner (FastShortcutNonlinearPolyalg is in NonlinearSolve; the sweep just passes inner=nothing and lets init(prob, nothing) resolve the default at dispatch time). So the AD has to ride a concrete inner solver, which default_nlsolve can build. Proven genuinely threaded by a ForwardDiff-hostile residual (errors on duals): finite-diff continuation solves to the root, ForwardDiff continuation throws. Unit test 12/12.

SCC caveat: I can't thread autodiff for the SCCNonlinearProblem branch — SCCNonlinearSolve applies one algorithm to every block, so no single value both continues the homotopy blocks and Newton-solves the plain ones with a chosen AD. Doing it right wants SCCNonlinearSolve to route by block type (Newton for plain, continuation for homotopy, each with the requested AD). I left the SCC branch at nothing (falls back to ForwardDiff defaults) and recorded this in a code comment — happy to do the SCCNonlinearSolve routing as a follow-up if you'd like.

…w-up

The SCC homotopy `default_nlsolve` methods return `nothing` (each block picks
its own default) so homotopy blocks continue on the correct branch, at the
cost of the continuation using its default inner corrector instead of the
requested AD backend. SciML#1104 adds the SCCNonlinearSolve per-block routing that
makes returning `FastShortcutNonlinearPolyalg(; autodiff)` here honor autodiff
per block; the NOTE now points at it as the concrete follow-up.

Comment-only; no behavior change.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01GSXiS4u4BaR2RvGFWGEQeH
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

On "this needs a high level finitediff option?" (the _homotopy_init_alg / whole-system HomotopyProblem methods): no high-level knob is needed, and one on HomotopyPolyAlgorithm itself can't do the job. The AD choice (finite-diff included) is threaded through the continuation's inner corrector — the FastShortcutNonlinearPolyalg built here — which is the differentiation the sweep and the arclength stage actually use. HomotopyPolyAlgorithm lives in NonlinearSolveBase, which can't construct a concrete inner solver, so there's nowhere for a high-level autodiff field there to turn into an actual backend. The inner-corrector threading is the mechanism; autodiff=false → AutoFiniteDiff(), autodiff=true → tagged AutoForwardDiff. Latest push makes those methods honor autodiff this way (unit test added: a ForwardDiff-hostile residual solves under finite-diff and throws under forward-diff).

The SCC homotopy methods still return nothing on purpose — with nothing each block picks its own default, so homotopy blocks continue on the correct branch but at the continuation's default inner corrector, not the requested AD backend. Threading AD per block needs SCCNonlinearSolve to route by block type. That routing is now open as SciML/NonlinearSolve.jl#1104: it adds HomotopyPolyAlgorithm(; inner) to NonlinearSolveBase and makes solve_single_scc continue HomotopyProblem blocks with the SCC's algorithm as the inner corrector (plain blocks unchanged). Once #1104 releases, these SCC methods can return FastShortcutNonlinearPolyalg(; autodiff) (with a SCCNonlinearSolve compat bump) to honor autodiff per block. I've left a code NOTE pointing at #1104 rather than flip the return now, because returning a standard algorithm for a homotopy SCC before that routing is released would hit the target-λ fallback (#1085) and risk landing on the wrong branch.

ChrisRackauckas and others added 2 commits July 23, 2026 03:32
…lg collapse target

_homotopy_init_alg hand-rolls exactly NonlinearSolve.FastShortcutHomotopyPolyalg(; autodiff)
(SciML/NonlinearSolve.jl#1105). Once that releases, this body collapses to a call to it.

Comment-only; no behavior change.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01GSXiS4u4BaR2RvGFWGEQeH
…LinearSolve-5 migration

The SCC per-block-autodiff flip and the FastShortcutHomotopyPolyalg collapse both need
NonlinearSolve 4.23 / SCCNonlinearSolve 1.14 / NonlinearSolveBase 2.37, which require
LinearSolve 5. OrdinaryDiffEq is capped at LinearSolve "3.75.0, 4" (pinned to
NonlinearSolve <= 4.21), so the follow-ups are blocked on the LinearSolve-5 migration, not
merely on those packages being released (they are). The SCC flip additionally needs a direct
SCCNonlinearSolve dependency (it is a reverse dep of NonlinearSolve). Notes updated to say so.

Comment-only; no behavior change.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01GSXiS4u4BaR2RvGFWGEQeH
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 23, 2026 14:34
@ChrisRackauckas
ChrisRackauckas merged commit bc534d7 into SciML:master Jul 23, 2026
139 of 171 checks passed
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/OrdinaryDiffEq.jl that referenced this pull request Jul 23, 2026
Master SciML#3989 introduced scc_hom variable names that typos flags as
"hom" → "home". Allow the intentional abbreviation.

Co-Authored-By: Chris Rackauckas <[email protected]>
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/OrdinaryDiffEq.jl that referenced this pull request Jul 23, 2026
Master SciML#3989 introduced scc_hom variable names that typos flags as
"hom" → "home". Allow the intentional abbreviation.

Co-Authored-By: Chris Rackauckas <[email protected]>
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/OrdinaryDiffEq.jl that referenced this pull request Jul 23, 2026
Master SciML#3989 introduced scc_hom variable names that typos flags as
"hom" → "home". Allow the intentional abbreviation.

Co-Authored-By: Chris Rackauckas <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Jul 24, 2026
* Route OOP initdt NaN checks through DiffEqBase.NAN_CHECK

The OOP Hairer–Wanner path used nested `any(x -> any(isnan, x), f₀)`,
which custom array/field types cannot overload. Replace with
`DiffEqBase.NAN_CHECK` (and `isnan(d₁)` for AD partials), matching the
IIP intent and fixing OrdinaryDiffEq #1404.

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

* Allow hom abbreviation in typos (homotopy test vars)

Master #3989 introduced scc_hom variable names that typos flags as
"hom" → "home". Allow the intentional abbreviation.

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

---------

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Jul 24, 2026
* Fix DAE automatic initdt sign on reversed tspan

`ode_determine_initdt` for AbstractDAEProblem returned a positive dt
even when tdir < 0, causing "Automatic dt setting has the wrong sign"
on reverse integrations. Multiply by tdir (DifferentialEquations #908).

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

* Fix DAE reverse initdt test: assert dt sign and tighten tols

Default adaptive tols left DABDF2/DImplicitEuler endpoint error
outside atol=1e-2 (~1.03–1.08 vs 1.0) while still integrating
successfully. Assert integ.dt < 0 as the #908 regression check,
solve with abstol/reltol=1e-8 for endpoint accuracy, and use Runic
1.0e-2 float style.

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

* Allow hom abbreviation in typos (homotopy test vars)

Master #3989 introduced scc_hom variable names that typos flags as
"hom" → "home". Allow the intentional abbreviation.

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

---------

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Jul 24, 2026
…3998)

* Coerce initdt equality checks to scalar Bool

`f₀ == f₁` is used to detect a constant zone before callbacks, but
some array wrappers (notably PyCall PyObject of arrays) return
Vector{Bool} from `==` (JuliaPy/PyCall.jl#900). That is not valid
in a boolean context and throws TypeError (OrdinaryDiffEq.jl#1402).

Add `_bool_equal` that accepts a true Bool or `all` over array-valued
equality results, and use it in both iip and oop initdt paths.

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

* Allow hom abbreviation in typos (homotopy test vars)

Master #3989 introduced scc_hom variable names that typos flags as
"hom" → "home". Allow the intentional abbreviation.

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

---------

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Jul 24, 2026
#3989 introduced scc_hom variable names that crate-ci/typos flags as
"hom" → "home". hom is intentional (HomotopyProblem / homotopy).

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Jul 25, 2026
#4004 added `hom` under "Technical variable names" (line 72) and #4000 added it
again at the end of the file (line 149). TOML forbids duplicate keys, so typos
cannot read its own config and bails out before checking anything:

    TOML parse error at line 149, column 1
        |
    149 | hom = "hom"  # scc_hom homotopy abbreviation in NonlinearSolve tests (#3989)
        | ^^^
    duplicate key

Remove the later entry — the line 72 declaration already covers the `scc_hom`
variables — and leave a pointer in its place, matching how the file already
notes the same situation for `Numer`.

Co-authored-by: ChrisRackauckas-Claude <[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