Repository navigation
Solve a HomotopyProblem + standard nonlinear algorithm at its target λ - #1085
ChrisRackauckas merged 2 commits into
Conversation
…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
|
Companion (MTK-side) PR: SciML/ModelingToolkit.jl#4766 — routes DAE/ODE initialization to a |
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
…#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
|
Reworked per @ChrisRackauckas's note that the previous approach was wrong: instead of routing every algorithm to |
|
Init companion: SciML/OrdinaryDiffEq.jl#3989 ( |
Summary
Adds a fallback so a
SciMLBase.HomotopyProblemhanded a standard (non-continuation) nonlinear algorithm is solved by the continuationHomotopyPolyAlgorithminstead of erroring.Why
A
HomotopyProblem's residual is the λ-extendedf(u, p, λ), so it cannot be driven by an ordinary Newton-type algorithm. ButHomotopyProblem <: AbstractNonlinearProblem, so any init path that selects a default nonlinear solver for the problem type hands it one and hits aMethodError(get_concrete_problem(::HomotopyProblem)):OverrideInitcomputesnlsolve_alg = default_nlsolve(..., initializeprob, ...), which for aHomotopyProblem(anAbstractNonlinearProblem) returnsFastShortcutNonlinearPolyalgand then callssolve(initprob, that_alg).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 aHomotopyProbleminitializeprob, and this fallback letssolve(odeprob)initialize it by continuation with no per-integrator glue. The existingsolve(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, andHomotopyPolyAlgorithmeach have a strictly more-specificsolve(::HomotopyProblem, ::T)method, so this fallback fires only for non-continuation algorithms, and thesolve(prob, HomotopyPolyAlgorithm())call dispatches to the specific polyalgorithm method — no recursion. (Routing to the defaultHomotopyPolyAlgorithm()rather than threading the passed algorithm as an inner corrector avoids feeding aSimple*solver — e.g. theSimpleTrustRegionthe integrators pick forStaticArrayu0— into the sweep's cache interface.)Tests
Adds
test/Core/homotopy_polyalg_tests__item3.jl(registered inruntests.jl): standard algorithms (NewtonRaphson,TrustRegion,FastShortcutNonlinearPolyalg) on aHomotopyProblemare rerouted to continuation and rescue an out-of-basinatanguess to the true root; the fallback produces the same answer asHomotopyPolyAlgorithm(); 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 asolvedispatch).Process
Companion to SciML/ModelingToolkit.jl (init → homotopy routing). Design chosen after an adversarial review showed the alternative — a
default_nlsolve(::HomotopyProblem) = nothingin OrdinaryDiffEqNonlinearSolve — has adefault_nlsolvemethod ambiguity forStaticArrayu0and only fixes the OrdinaryDiffEq caller; this single method fixes every caller. Runic-formatted.🤖 Generated with Claude Code