Repository navigation
Array-valued initial conditions and initialization - #720
Merged
Merged
Conversation
`generate_ic_defaults` built one `u[i, j] => value` pair per grid point by substituting each point into the initial condition symbolically, PDEBase then wrote one initialization equation per differential grid point, and the DAE operating point held `u[i, j]` and `D(u[i, j])` for every point. At large grids these per-point symbolic objects dominated the build's memory. Now each initial condition is a single `u => values` pair: a right-hand side of the coordinates alone is compiled once and broadcast over the (staggered) grid, anything else is still substituted point by point. MethodOfLines implements PDEBase's `generate_system` hook to build the initialization from these: one equation per slice the equations differentiate in time, `u[2:n-1, ...] ~ values[2:n-1, ...]`, fixing the same points as before. The `mtkcompile` path consumes these equations through its initialization system; the DAE path passes `build_initializeprob = false` and only checks them, so `brown_init_offenders` and the operating point work on whole unknowns and slices too. Agent-Harness: Claude Code 2.1.299 Agent-Model: claude-opus-5-5 Agent-Session: 644ed0ff-c8c9-4cc3-b4c6-f326231a4030 Agent-Review: unreviewed Co-authored-by: Claude Code <[email protected]>
Member
Author
|
keep it in mol |
xtalax
marked this pull request as ready for review
October 19, 2026 20:27
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.
Initial conditions, the DAE operating point and the initialization equations are now array valued: one entry per variable instead of one per grid point. On a 2D staggered FDTD system the problem build is about twice as fast (278 s to 149 s at 1.63M states). Retained memory drops from 2.10 to 1.74 KB per state. The problem built is unchanged, apart from one 1-ulp difference described below.
Motivation and cause
A downstream FDTD model (2D TM Maxwell on a staggered grid, 46M states) runs out of memory inside MethodOfLines before its problem is built. The equations themselves stay array valued, but three things around them were built point by point:
generate_ic_defaultsreturned oneu[i, j] => valuepair per grid point. It got each value by building a substitutionDictfor that point and substituting it into the initial condition symbolically, about 11 KB allocated per state._discrete_initializationturned those pairs into one initialization equation per differential grid point. It first scalarized every differentiated slice to find those points.u[i, j]and a freshly builtD(u[i, j])for every point, about 335 B per state retained.brown_init_offendersscalarized every unknown to check the initialization equations.Changes
src/discretization/generate_ic_defaults.jl: each initial condition becomes a singleu => valuespair, whereuis the system's array unknown, orDt^k(u)for a condition on a time derivative.build_function(...; nanmath = false)and broadcast over the grid. Edge-aligned variables are evaluated at their shifted points, computed elementwise asgridvalsdoes.src/discretization/generate_initialization.jl(new): MethodOfLines implements PDEBase'sgenerate_systemhook forMOLDiscretization.u[slice] ~ values[slice]for each slice the equations differentiate to a higher order in time than the condition's own. These cover exactly the points that previously got one equation each.src/dae_discretization.jl:brown_init_offendersworks in index space. Each array unknown gets a mask of its time-differentiated elements, and an initialization equation may fix a whole unknown, an element or a slice._dae_operating_pointis keyed by the array unknowns:u => values,D(u) => zeros(size(u)).src/discretization/discretize_vars.jl:system_unknown(s, u)is factored out ofget_system_unknowns.test/Discretization/problem_construction.jl: two new test sets.U[2:4, 2:4] ~ ..., and that an edge-aligned variable's initial condition is sampled atx + dx/2.brown_init_offenderson slices: a differential slice is accepted, an algebraic slice is rejected, and an element coupled to another is reported.Why the initialization equations are still correct
symbolic_discretizereturns oneSystemthat both problem paths start from, so the equations have to be right for each.mtkcompileandODEProblem(thediscretizefallback, explicit Runge–Kutta use, and staggereddiscretize): MTK builds its initialization system frominitialization_eqsandguesses, and that system is what fixes the differential states.ODEProblemfails with "Initialization incomplete. Not all of the state variables of the DAE system can be determined".prob.u0and the state afterinitare identical to master's.DAEProblem(the defaultdiscretizepath):_dae_problempassesbuild_initializeprob = falsewithBrownFullBasicInit(), so no solver reads the initialization equations.BrownFullBasicInitholds the differential states atu0, which comes from the operating point and so from the same initial-condition values. It solves for the algebraic states anddu0.brown_init_offenders. It confirms that each equation fixes differential points to values that involve no other unknown, which is whatBrownFullBasicInitdoes anyway.Equivalence with master
Eight systems were built on both paths (
DAEProblem, andmtkcompileplusODEProblem), on master and on this branch. The comparison coversu0,du0,p(tunable, initials and constants), the order of unknowns, the parameter names,differential_vars, and the DAE residual or ODE right-hand side at a random state andt = 0.37. On the ODE path it also covers the state afterinit. The systems are:a,bx^3 - xHx,Hyedge aligned on different axes, PEC walls, aDt(S) ~ 0coefficient fieldDtinitial conditionu0entry differs by 1.1e-16The one difference is in the last system: an algebraic wall point at
x = 1whose initial condition issin(pi * x) * y + x^2 / 3.π * 1.0back intoπ, sosin(π)evaluates to exactly 0.0.sin(3.141592653589793), which is 1.2e-16.du0andpare identical. The point is algebraic, soBrownFullBasicInitre-solves it anyway.Differences like this can appear wherever symbolic simplification changes the arithmetic. Matching master exactly would mean going back to the per-point substitution.
The downstream FDTD build was also compared at 6,875 states:
u0, the right-hand side,pand the symbols are identical.Tests
@testassertions)Measurements
The build of the 2D FDTD system was timed stage by stage at four sizes, in a fresh process per size. Retained memory is the live heap growth across each stage after a full GC, divided by the number of states. Both columns include the
evaluate_varmap!fix from SciML/ModelingToolkit.jl#5280. The FDTD model uses the array boundary slices from #717, so these measurements apply this change on top of #717 rather than on master.symbolic_discretizecomplete+brown_init_offenders_dae_operating_pointDAEProblem(sys, op)symbolic_discretizetime over the four sizes:Retained memory scales linearly with the number of states in every stage, before and after.
Known limits
DAEProblemstays quadratic without Make DAEProblem construction linear in array unknown size (evaluate_varmap! dedupe) ModelingToolkit.jl#5280. For each scalarD(u[i, j]), thedu0path ofvarmap_to_varsre-substitutes the whole array. This PR does not change that: at 183k states,DAEProblemstill takes 385 s and allocates 457 GB without the MTK fix.Per-state memory left after this PR, about 1.7 KB per state:
IndexCachekeeps per-element entries forInitialparameters, about 840 B per state, created incomplete.DAEProblemconstruction retains about 540 B per state, probably the per-element residual code generation; that is not confirmed.DiscreteSpacestill collects every array variable into per-element scalar terms (discvars), about 320 B per state.At 46M states this is still about 80 GB retained and about 145 GB peak, so the downstream case does not fit yet.
Design note
PDEBase.generate_systemis a documented hook, and implementing it here keeps the change in MethodOfLines. The cost is thatgenerate_initialization.jlrepeats about 25 lines of PDEBase'sSystemconstruction: parameter defaults and theSystem(...)call. That duplication is kept for now. A narrower PDEBase hook, such asdiscrete_initialization(disc, eqs, t, u0), could remove it later at the price of a PDEBase release.🤖 Posted by an AI agent · harness: Claude Code 2.1.299 · model: claude-opus-5-5
Review: unreviewed
Session: 644ed0ff-c8c9-4cc3-b4c6-f326231a4030