Repository navigation
Route DAE/ODE initialization through the homotopy continuation solver - #4766
Conversation
|
Companion (solver-side) PR: SciML/NonlinearSolve.jl#1085 — adds the |
|
how do benchmarks compare? |
AayushSabharwal
left a comment
There was a problem hiding this comment.
This also doesn't hit the MTK method of get_initialization_problem_type which builds SCCNonlinearProblem. Or is the intention that individual SCCs do the homotopy init?
| # the initialization path, which has already resolved `iip` from the calling problem and | ||
| # must not re-derive it (an out-of-place system's `op` is still a plain varmap, so the | ||
| # `Both` derivation would wrongly pick in-place). | ||
| function SciMLBase.HomotopyProblem(sys::System, op; kwargs...) |
There was a problem hiding this comment.
Why doesn't this use @fallback_iip_specialize?
There was a problem hiding this comment.
Switched to it in b014ca5. I had hand-written the {iip} form following the SCCNonlinearProblem{iip}(sys, op) pattern, but the macro is the better fit: its problem-default of Both reproduces the previous zero-argument behaviour exactly (resolve_iip(Both, op)), and it supplies HomotopyProblem{iip} for free — which is what let me collapse the duplicated branch you spotted below.
| if TProb === SciMLBase.HomotopyProblem | ||
| # The init system contains Modelica `homotopy` nodes: build a `HomotopyProblem` | ||
| # so the initialization is solved by continuation from the `simplified` form. | ||
| # Pass the resolved `_iip` explicitly (like the sibling `TProb{_iip}` branch): the | ||
| # bare `HomotopyProblem(sys, op)` would re-derive in-place-ness from `op`, which is | ||
| # a varmap and so always reads as in-place — wrong for an out-of-place system. | ||
| SciMLBase.HomotopyProblem{_iip}( | ||
| isys, op; kwargs..., check_length_kw..., | ||
| build_initializeprob = false, is_initializeprob = true | ||
| ) | ||
| else | ||
| TProb{_iip}( | ||
| isys, op; kwargs..., check_length_kw..., | ||
| build_initializeprob = false, is_initializeprob = true | ||
| ) | ||
| end | ||
| end |
There was a problem hiding this comment.
Both branches do the same thing?
There was a problem hiding this comment.
Yes — leftover, good catch. That branch dates from when HomotopyProblem had no {iip} constructor form, so the homotopy side had to be built unparametrized. Once I added one (and now that it comes from @fallback_iip_specialize) both sides were character-for-character the same. Collapsed back to the single TProb{_iip}(...) call in b014ca5.
| # the `LinearInitializationProblem` branch above instead — there is nothing to | ||
| # continue. `get_nonlinear_problem_type` is only defined for `System`; other | ||
| # `AbstractSystem`s keep the previous `NonlinearProblem` behavior.) | ||
| isys isa System ? get_nonlinear_problem_type(isys) : NonlinearProblem |
There was a problem hiding this comment.
When is isys not a System?
There was a problem hiding this comment.
Never, as far as I can tell — generate_initializesystem + mtkcompile return a System, and your override in src/initialization.jl types it isys::System outright. That guard was unnecessary defensiveness on my part; removed in b014ca5, so it just calls get_nonlinear_problem_type(isys).
That's not a great way to look at it. Homotopy is like 5x slower than Newton methods (the gap may be closer to 2x-3x now, I've been optimizing), but if you're adding homotopy operators then Newton methods probably don't converge anyways. So the general benchmark is, yes there are lots of problems where no nonlinear solver except the homotopy solver converges to the solution, so if the user opts into homotopy methods by using a homotopy operator then we should use the homotopy 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
- `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
3769241 to
b014ca5
Compare
|
Good catch — you were right, and it was a real gap: the full-MTK path never reached the routing at all. There are two On the design question — I went with the whole init system as one That said, per-SCC is very doable and would be faster when only one block is actually hard — continuation on the whole system costs the 2-5x Chris mentions above, and here it drags the easy blocks along with it. With the companion NonlinearSolve PR (SciML/NonlinearSolve.jl#1085) a One thing worth flagging that I hit while testing: a single-equation init system degrades to a plain |
|
@oscardssmith — Chris's framing above is the right one, but here is the concrete data I collected while building this, since it makes the "Newton probably doesn't converge anyway" claim measurable. On canonical Modelica
So 3 of 4 are cases where Newton either cannot move or silently lands on the wrong branch — the wrong-root one is the nastiest, since it succeeds with a clean retcode. The I did not measure a head-to-head wall-clock ratio against Newton on a problem both solve, so I can't add a number to Chris's 2-5x. Within the continuation solvers themselves (which is what this PR selects), on a fold-free scalar problem: |
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
|
Switched to per-SCC in 8ab5274, as you suggested. An init system that tears into SCC blocks now keeps its It needs no Two things I checked before writing it, since both could have sunk the approach:
The changes: One semantics note worth flagging. This does change what the Tests (Julia 1.12): the new |
|
Given that homotopy is slower but more robust, should it be integrated into the polyalg rather than a pure opt in? |
|
I don't think that matters on the MTK side. You can just solve a HomotopyNonlinearProblem with a non-homotopy nonlinear solver, that would be the nice interface for that. I don't think the user has to care what internal structures are used. |
|
Follow-ups after reworking the NonlinearSolve companion per your note there:
Companion PRs (the init now opts into continuation via the default path, not by hijacking a concrete algorithm):
|
Summary
Makes DAE/ODE initialization automatically use the Modelica
homotopy(actual, simplified)operator. When the (square) initialization system containshomotopynodes,InitializationProblemnow builds aSciMLBase.HomotopyProblem(solved by NonlinearSolve continuation from thesimplifiedform) instead of a plainNonlinearProblem, selected with the same dispatch asAbstractNonlinearProblem(isys, op)(get_nonlinear_problem_type). This robustly initializes systems whose target form is hard to solve from a cold start — singular Jacobian at the natural guess, multiple roots, folds — which is exactly what the operator exists for (Modelica spec 3.7.4.2; Sielemann, Casella & Otter 2011).Systems without
homotopynodes take a byte-identical path (get_nonlinear_problem_typereturnsNonlinearProblem).What changed
initializationproblem.jl—get_initialization_problem_type's square-system branch now returnsisys isa System ? get_nonlinear_problem_type(isys) : NonlinearProblem(was alwaysNonlinearProblem). TheInitializationProblemconstructor builds aHomotopyProblemon that selection, threading the caller's resolved_iip.homotopyproblem.jl— adds aSciMLBase.HomotopyProblem{iip}(sys::System, op)constructor form. The bareHomotopyProblem(sys, op)derives in-place-ness fromop, but an initopis a varmap (never aStaticArray), so it always reads as in-place; the init path must pass the resolvediipexplicitly, exactly like the siblingNonlinearProblem{iip}path. Without this an out-of-place / StaticArray initialization is silently built in-place and hard-errors at solve.Companion change (separate PR)
End-to-end
solve(odeprob)also needs a one-method solver-side fallback so the OverrideInit path routes aHomotopyProbleminit to the continuation polyalgorithm. SinceHomotopyProblem <: AbstractNonlinearProblem,default_nlsolvecurrently hands it a Newton polyalg that has noHomotopyProblemmethod. The companion PR (SciML/NonlinearSolve.jl) addssolve(::HomotopyProblem, ::AbstractNonlinearSolveAlgorithm)in NonlinearSolveBase, which fixes every init caller at once without any per-integrator glue.Tests
Adds
test/homotopy_initialization.jl(registered inruntests.jl): routing selection, in-place-ness is honored both ways (regression guard for the{iip}fix), non-homotopy systems unaffected, and end-to-end solving of the stored init problem. Local run (Julia 1.12):homotopy_initialization17/17, and the existinghomotopy_lowering(43),homotopy_problem(29),homotopy_omc_parity(13) all still pass.Notes / known limitations
NonlinearLeastSquaresProblem, which loses continuation (HomotopyProblem requires a square system). Currently silent; a warning could be added.boundsmetadata (the continuation solvers don't consumelb/ub).Process
Implemented and demonstrated against canonical Modelica homotopy examples (turbulent PressureDrop resistor and coupled flow network at the singular zero-flow guess,
atansaturation, squared-flow branch selection, an S-fold) where a plain Newton init stalls / picks the wrong branch but the auto-homotopy init succeeds. An adversarial multi-agent review of the diff surfaced the{iip}bug (fixed here) and thedefault_nlsolveambiguity (addressed by choosing the NonlinearSolveBase-side companion). Runic-formatted.🤖 Generated with Claude Code