Skip to content

Solve a HomotopyProblem + standard nonlinear algorithm at its target λ - #1085

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:homotopy-solve-fallback
Jul 22, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:homotopy-solve-fallback

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Draft — please ignore until reviewed by @ChrisRackauckas.

Summary

Adds a fallback so a SciMLBase.HomotopyProblem handed a standard (non-continuation) nonlinear algorithm is solved by the continuation HomotopyPolyAlgorithm instead of erroring.

function CommonSolve.solve(
        prob::SciMLBase.HomotopyProblem, ::AbstractNonlinearSolveAlgorithm, args...; kwargs...)
    return CommonSolve.solve(prob, HomotopyPolyAlgorithm(), args...; kwargs...)
end

Why

A HomotopyProblem's residual is the λ-extended f(u, p, λ), so it cannot be driven by an ordinary Newton-type algorithm. But HomotopyProblem <: AbstractNonlinearProblem, so any init path that selects a default nonlinear solver for the problem type hands it one and hits a MethodError (get_concrete_problem(::HomotopyProblem)):

  • SciMLBase's OverrideInit computes nlsolve_alg = default_nlsolve(..., initializeprob, ...), which for a HomotopyProblem (an AbstractNonlinearProblem) returns FastShortcutNonlinearPolyalg and then calls solve(initprob, that_alg).
  • SteadyStateDiffEq and other callers do the same.

This is exactly the integration needed for ModelingToolkit's new homotopy-operator initialization (SciML/ModelingToolkit.jl companion PR): when the init system contains Modelica homotopy(actual, simplified) nodes, MTK builds a HomotopyProblem initializeprob, and this fallback lets solve(odeprob) initialize it by continuation with no per-integrator glue. The existing solve(prob::HomotopyProblem, ::Nothing) method already anticipated this ("SciMLBase OverrideInit with a default nlsolve"); this generalizes it to the concrete default the integrators actually pass.

Dispatch safety

HomotopySweep, ArcLengthContinuation, and HomotopyPolyAlgorithm each have a strictly more-specific solve(::HomotopyProblem, ::T) method, so this fallback fires only for non-continuation algorithms, and the solve(prob, HomotopyPolyAlgorithm()) call dispatches to the specific polyalgorithm method — no recursion. (Routing to the default HomotopyPolyAlgorithm() rather than threading the passed algorithm as an inner corrector avoids feeding a Simple* solver — e.g. the SimpleTrustRegion the integrators pick for StaticArray u0 — into the sweep's cache interface.)

Tests

Adds test/Core/homotopy_polyalg_tests__item3.jl (registered in runtests.jl): standard algorithms (NewtonRaphson, TrustRegion, FastShortcutNonlinearPolyalg) on a HomotopyProblem are rerouted to continuation and rescue an out-of-basin atan guess to the true root; the fallback produces the same answer as HomotopyPolyAlgorithm(); and the continuation algorithms keep their own dispatch (no recursion). Verified locally under Julia 1.12 against NonlinearSolveBase 2.35.0 + this method (also end-to-end through the MTK companion: solve(odeprob) initializes a homotopy init system to the correct root).

Minor version bump 2.35.0 → 2.36.0 (adds a solve dispatch).

Process

Companion to SciML/ModelingToolkit.jl (init → homotopy routing). Design chosen after an adversarial review showed the alternative — a default_nlsolve(::HomotopyProblem) = nothing in OrdinaryDiffEqNonlinearSolve — has a default_nlsolve method ambiguity for StaticArray u0 and only fixes the OrdinaryDiffEq caller; this single method fixes every caller. Runic-formatted.

🤖 Generated with Claude Code

…polyalgorithm

A `SciMLBase.HomotopyProblem` has a λ-extended residual `f(u, p, λ)` and cannot be
driven by an ordinary Newton-type algorithm. But `HomotopyProblem <:
AbstractNonlinearProblem`, so any init path that picks a default nonlinear solver for
the problem type (SciMLBase OverrideInit, SteadyStateDiffEq, ...) hands it one and hits
a `MethodError`. Add a fallback that routes any `AbstractNonlinearSolveAlgorithm` on a
`HomotopyProblem` to the `HomotopyPolyAlgorithm` default.

The continuation algorithms (`HomotopySweep`, `ArcLengthContinuation`,
`HomotopyPolyAlgorithm`) each have a strictly more specific method, so this fires only
for non-continuation algorithms and does not recurse. This is the solver-side companion
to ModelingToolkit's homotopy-operator initialization: it lets `solve(odeprob)`
initialize a `homotopy`-carrying init system by continuation with no per-integrator glue.

Adds test/Core/homotopy_polyalg_tests__item3.jl and bumps NonlinearSolveBase 2.36.0.

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

Companion (MTK-side) PR: SciML/ModelingToolkit.jl#4766 — routes DAE/ODE initialization to a HomotopyProblem when the init system contains Modelica homotopy operators. Both are drafts; please ignore until reviewed by @ChrisRackauckas.

ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/ModelingToolkit.jl that referenced this pull request Jul 20, 2026
Per review: an initialization system that tears into SCC blocks now keeps its
`SCCNonlinearProblem`, and only the blocks whose equations carry Modelica
`homotopy(actual, simplified)` nodes are built as `SciMLBase.HomotopyProblem`s and
solved by continuation. The other blocks keep their plain Newton solves, so the
cost of continuation is paid only where the model asks for it.

- `SCCNonlinearFunction`: a homotopy-carrying block compiles the λ-swept residual
  `f(u, p, λ)` via `lower_homotopy` + `generate_homotopy_residual`. `λ` is a
  trailing argument, so the block's cache buffers — which `generate_rhs` appends to
  the parameter list — are unaffected.
- block construction: such a block becomes `HomotopyProblem(f, _u0, p; λspan)`.
- `src/initialization.jl`: no longer diverts homotopy systems away from SCC. The
  non-SCC fallbacks (`split = false`, and `use_scc = false` / `neqs == 0`) now
  return `get_nonlinear_problem_type(isys)`, so an init system that cannot be torn
  is still swept as a whole.

No `SCCNonlinearSolve` change is needed: its block loop calls
`SciMLBase.solve(prob, alg.nlalg)`, which routes a `HomotopyProblem` block to the
continuation polyalgorithm — via `solve(::HomotopyProblem, ::Nothing)`, or via the
`AbstractNonlinearSolveAlgorithm` fallback in SciML/NonlinearSolve.jl#1085 when a
concrete algorithm is passed.

Also updates the `homotopy` docstring: `λ` is shared within a lowered system or
block, but separate blocks sweep their own `λ` in dependency order.

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 added a commit to SciML/ModelingToolkit.jl that referenced this pull request Jul 22, 2026
…#4766)

* Route DAE/ODE initialization through the homotopy continuation solver

When the (square) initialization system contains Modelica
`homotopy(actual, simplified)` operator nodes, `InitializationProblem` now
builds a `SciMLBase.HomotopyProblem` instead of a plain `NonlinearProblem`,
selected with the same dispatch as `AbstractNonlinearProblem(isys, op)`
(`get_nonlinear_problem_type`). The initialization is then solved by
natural-parameter / pseudo-arclength continuation from the `simplified`
form, robustly initializing systems whose target form is hard to solve from
a cold start (singular Jacobian at the natural guess, multiple roots, folds).

Systems without `homotopy` nodes are unaffected — they take a byte-identical
path (`get_nonlinear_problem_type` returns `NonlinearProblem`).

The init branch threads the caller's resolved `_iip` through a new
`SciMLBase.HomotopyProblem{iip}(sys, op)` constructor form: the bare
`HomotopyProblem(sys, op)` derives in-place-ness from `op`, but the init `op`
is a varmap (never a `StaticArray`) so it would always read as in-place and
silently break out-of-place / StaticArray initialization at solve time.

Adds `test/homotopy_initialization.jl` (routing, in-place-ness, regression,
and end-to-end solve of the stored init problem) and registers it in
`runtests.jl`.

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

* Address review: route the SCC init path, and simplify the constructors

- `src/initialization.jl`: the full-MTK `get_initialization_problem_type` selects
  `SCCNonlinearProblem` for a fully determined split init system, so it bypassed the
  homotopy routing entirely. It now returns `HomotopyProblem` for a `homotopy`-carrying
  square init system, ahead of the SCC branch. Modelica's `homotopy` shares a single λ
  across the whole model (spec 3.7.4.2), so the init system is solved as one continuation
  rather than torn into blocks that would each sweep their own λ with per-block Newton.

- `homotopyproblem.jl`: use `@fallback_iip_specialize` instead of the hand-written
  `{iip}` forms. Its `Both` problem-default reproduces the previous zero-argument
  behaviour (`resolve_iip(Both, op)`) and supplies `HomotopyProblem{iip}` for free.

- `initializationproblem.jl`: with `HomotopyProblem{iip}` available, the two construction
  branches were identical — collapsed back to the single `TProb{_iip}(...)` call. Also
  dropped the `isys isa System` guard: `isys` is always a `System` here.

Adds `test/homotopy_initialization_scc.jl` (registered in `group_initialization.jl`): the
fixture is a three-block `a -> b -> c` chain, since a single-equation init system degrades
to a plain `NonlinearProblem` inside the `SCCNonlinearProblem` constructor and would not
exercise the SCC path at all.

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

* Apply homotopy per SCC block rather than to the whole init system

Per review: an initialization system that tears into SCC blocks now keeps its
`SCCNonlinearProblem`, and only the blocks whose equations carry Modelica
`homotopy(actual, simplified)` nodes are built as `SciMLBase.HomotopyProblem`s and
solved by continuation. The other blocks keep their plain Newton solves, so the
cost of continuation is paid only where the model asks for it.

- `SCCNonlinearFunction`: a homotopy-carrying block compiles the λ-swept residual
  `f(u, p, λ)` via `lower_homotopy` + `generate_homotopy_residual`. `λ` is a
  trailing argument, so the block's cache buffers — which `generate_rhs` appends to
  the parameter list — are unaffected.
- block construction: such a block becomes `HomotopyProblem(f, _u0, p; λspan)`.
- `src/initialization.jl`: no longer diverts homotopy systems away from SCC. The
  non-SCC fallbacks (`split = false`, and `use_scc = false` / `neqs == 0`) now
  return `get_nonlinear_problem_type(isys)`, so an init system that cannot be torn
  is still swept as a whole.

No `SCCNonlinearSolve` change is needed: its block loop calls
`SciMLBase.solve(prob, alg.nlalg)`, which routes a `HomotopyProblem` block to the
continuation polyalgorithm — via `solve(::HomotopyProblem, ::Nothing)`, or via the
`AbstractNonlinearSolveAlgorithm` fallback in SciML/NonlinearSolve.jl#1085 when a
concrete algorithm is passed.

Also updates the `homotopy` docstring: `λ` is shared within a lowered system or
block, but separate blocks sweep their own `λ` in dependency order.

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

---------

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
… by continuation

A `HomotopyProblem` at its target `λ` (`λspan[2]` — `λ = 1`, the `actual` system, for the
default span) is an ordinary `NonlinearProblem`: the λ-swept residual `f(u, p, λ)` reduces
to that system when `λ` is fixed. So a *standard* (non-continuation) nonlinear algorithm
handed a `HomotopyProblem` should solve exactly that system with the requested algorithm —
not silently override it with continuation. Continuation stays opt-in via `nothing`,
`HomotopySweep`, `ArcLengthContinuation`, and `HomotopyPolyAlgorithm`, each of which has a
strictly more specific `solve(::HomotopyProblem, ::T)` method.

The method fixes `λ` across the residual and every derivative field it carries
(`jac`/`jac_prototype`/`sparsity`/`colorvec`) via the existing `_sweep_nonlinear_function`
— the same λ-fixed `NonlinearFunction` the sweep builds for its inner solver — then solves
the resulting `NonlinearProblem`. It dispatches on that `NonlinearProblem`, so it never
recurses, and the result is rebuilt against the original `HomotopyProblem` so symbolic
indexing (`prob.f.sys`) is preserved.

Replaces the previous fallback, which routed every algorithm to `HomotopyPolyAlgorithm`.

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

Reworked per @ChrisRackauckas's note that the previous approach was wrong: instead of routing every algorithm to HomotopyPolyAlgorithm, the method now fixes λ at λspan[2] and solves the resulting NonlinearProblem with the requested algorithm (reusing _sweep_nonlinear_function to λ-fix the residual + jac/prototype/sparsity/colorvec). Continuation stays opt-in via nothing/HomotopySweep/ArcLengthContinuation/HomotopyPolyAlgorithm. The init integration moves to a default_nlsolve → nothing companion in OrdinaryDiffEq (linked below). 19/19 tests.

@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 22, 2026 21:27
@ChrisRackauckas
ChrisRackauckas merged commit dd99817 into SciML:master Jul 22, 2026
15 checks passed
@ChrisRackauckas-Claude ChrisRackauckas-Claude changed the title Route a HomotopyProblem + standard nonlinear algorithm to the continuation polyalgorithm Solve a HomotopyProblem + standard nonlinear algorithm at its target λ Jul 22, 2026
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Init companion: SciML/OrdinaryDiffEq.jl#3989 (default_nlsolve → nothing for homotopy init).

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