feat: DAEProblem path that keeps the array (slice-form) discretization - #639
feat: DAEProblem path that keeps the array (slice-form) discretization#639ChrisRackauckas-Claude wants to merge 5 commits into
Conversation
`discretize` runs `mtkcompile`, which scalarizes the array equations `ArrayDiscretization` emits: an `ODEProblem` needs `D(x) = f(x)`, and isolating the derivative is structural simplification. The residuals MethodOfLines already emits, `D(u) - f ~ 0`, are the implicit-DAE form, so `DAEProblem(pdesys, disc)` builds a problem without `mtkcompile` and the array equations reach codegen. `initializealg` defaults to `BrownFullBasicInit()`, the only algorithm that reproduces the `discretize` result here. Because it takes the differential variables' values as given and solves for everything else, it is chosen only when every initialization equation fixes a single differential unknown to a value involving no other unknown; otherwise construction raises an error naming the offending equations and pointing at `discretize`. A user-supplied `initializealg` overrides both. `ScalarizedDiscretization` and systems that are second order in time are rejected with their own messages. Requires the ModelingToolkit array-equation DAE fixes; see the PR body. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
PDEBase only emits an initialization equation for a variable whose time-derivative order in the system exceeds the order of its initial condition, which makes that variable differential by construction. The algebraic-variable and coupled-unknown branches of the guard are therefore never exercised by a MethodOfLines-discretized system; check them on hand-built systems so the guard is known to discriminate. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
With SciMLBase's `problem_type` field on `DAEProblem` and ModelingToolkit forwarding the system's `ProblemTypeCtx` metadata into it, `wrap_sol` now reaches the DAE path, so its solutions are indexed and interpolated by the `PDESystem`'s variables like the `discretize` path. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
The wrapping commit updated the manual but left the docstring claiming the result is a plain `DAEProblem` indexed by discretized variables. Wrapping needs `problem_type` on `DAEProblem`, so bump the SciMLBase compat to match. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
The array-equation implicit-DAE codegen landed in ModelingToolkitBase 1.67.0, released as ModelingToolkit 11.39.1. The `DAEProblem` path here does not work against earlier versions, which reject array equations outright. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
|
Continuing this work in a combined v1 branch that also subsumes #618. Plan: rebase onto current |
|
Superseded by the combined v1 draft at #650. That branch is rebased through 88a568e, removes the strategy selector and |
What and why
discretizerunsmtkcompile, which scalarizes array equations: anODEProblemneedsD(x) = f(x), and isolating the derivative is structural simplification. So the array (slice-form) discretization from #619 is flattened before it ever reaches codegen, and the scaling win is lost at exactly the point it would pay off.The residuals MethodOfLines already emits,
D(u) - f ~ 0, are in implicit-DAE form. This adds aDAEProblem(pdesys, disc)path that skipsmtkcompileentirely, so the array equations survive into the generated code:Part of #428.
Consistent initialization
A fully implicit DAE needs consistent
(u0, du0).initializealgdefaults toBrownFullBasicInit(), but only when it is safe to do so: MethodOfLines always emits initialization equations (9 for the heat equation, 18 for the wave equation, 9 for the PDAE), so anisemptyguard would be useless. Insteadbrown_init_offendersinspects what those equations actually constrain, and construction raisesBrownFullBasicInitUnsafeErrornaming the offenders and pointing back atdiscretizewhen the algorithm would not honor them. Passinginitializealgexplicitly overrides both the default and the check. Systems second order in time need the order reductionmtkcompileperforms and are rejected outright.Solution wrapping
The solution is a
PDETimeSeriesSolution, the same wrapperdiscretizeproduces, so it is indexed and interpolated by thePDESystem's own variables (sol[u(t, x)],sol(t, x)) rather than by discretized variables. This neededproblem_typeonDAEProblem, which did not exist —ODEProblem,DDEProblem,BVProblemandNonlinearProblemall had one andDAEProblemwas the odd one out.Dependencies — one blocker left
problem_typefield onDAEProblemProblemTypeCtxintoDAEProblemThe ModelingToolkit compat is bumped to
11.39.1, the release that carries the codegen.The remaining blocker is not a MethodOfLines issue: since ModelingToolkit made
AutoDespecializethe default, everyDAEProblemthat needs a consistent-initialization solve fails witha parameter-despecialization barrier requires exactly one parameter wrapper. It reproduces with no ModelingToolkit and no MethodOfLines at all — see SciML/OrdinaryDiffEq.jl#4297 for the standalone reproducer. Until that merges and releases, the DAE testset here cannot pass in CI.Verification
New test group
test/Array_Discretization/dae_problem.jl(in the existingArray_Discretizationgroup).Run against fully released ModelingToolkit 11.39.1 / ModelingToolkitBase 1.67.0 / SciMLBase 3.49.1, with only the one-line SciML/OrdinaryDiffEq.jl#4297 patch
deved in:Without that patch, on stock released packages, the discretization and problem construction still succeed and only the
solvefails — which isolates the blocker precisely:42 assertions, 0 failures. Every solving case is checked against the
discretize(ODEProblem+mtkcompile) path it must reproduce, not just against an analytic solution —dae_vals ≈ ode_vals rtol = 1e-6— and the 1D heat case additionally asserts the array equations actually survived (any(isarrayeq, get_eqs(prob.f.sys))), which is the whole point of the path.End-to-end sanity on the heat equation, array form, no
mtkcompile:runic --checkandtyposare clean on the diff. Comment lines are 6.3% of added lines (36/567).Not verified
deved; a normalPkg.test()cannot resolve today. Only theArray_DiscretizationDAE group above was run. Once #4983/#4984 release, the full suite should be run before this merges.Worth pushing back on
test/qa/qa.jlgains aninitialization_equationsentry in the explicit-imports ignore list, following the existing pattern for names owned byModelingToolkitBaseand re-exported byModelingToolkit.brown_init_offendersis a heuristic over initialization equations. It is deliberately conservative — it errors rather than silently producing a wrong initialization — but it is the part of this PR most likely to need adjusting as more PDE shapes hit it.Please ignore until reviewed by @ChrisRackauckas.