feat: DAEProblem path that keeps the array (slice-form) discretization - #639
Draft
ChrisRackauckas-Claude wants to merge 4 commits into
Draft
feat: DAEProblem path that keeps the array (slice-form) discretization#639ChrisRackauckas-Claude wants to merge 4 commits into
ChrisRackauckas-Claude wants to merge 4 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 — this cannot go green yet
problem_typefield onDAEProblemProblemTypeCtxintoDAEProblemSciMLBasecompat is bumped to3.48. The ModelingToolkit compat still needs a bump once #4983 and #4984 land in a release; until then CI here will fail, and I have deliberately not guessed a version number. Please merge those two first.Verification
New test group
test/Array_Discretization/dae_problem.jl(in the existingArray_Discretizationgroup), run against a local environment with all three upstream changes applied: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.