Skip to content

Array-valued initial conditions and initialization - #720

Merged
xtalax merged 1 commit into
SciML:masterfrom
xtalax:array-ics
Oct 19, 2026
Merged

xtalax merged 1 commit into
SciML:masterfrom
xtalax:array-ics

Conversation

@xtalax

@xtalax xtalax commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

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_defaults returned one u[i, j] => value pair per grid point. It got each value by building a substitution Dict for that point and substituting it into the initial condition symbolically, about 11 KB allocated per state.
  • PDEBase's _discrete_initialization turned those pairs into one initialization equation per differential grid point. It first scalarized every differentiated slice to find those points.
  • The DAE operating point held u[i, j] and a freshly built D(u[i, j]) for every point, about 335 B per state retained. brown_init_offenders scalarized every unknown to check the initialization equations.

Changes

  • src/discretization/generate_ic_defaults.jl: each initial condition becomes a single u => values pair, where u is the system's array unknown, or Dt^k(u) for a condition on a time derivative.
    • A right-hand side that depends only on the spatial coordinates is compiled once with build_function(...; nanmath = false) and broadcast over the grid. Edge-aligned variables are evaluated at their shifted points, computed elementwise as gridvals does.
    • A right-hand side that involves parameters or other variables still goes through the point-by-point substitution, but the result is stored as an array.
  • src/discretization/generate_initialization.jl (new): MethodOfLines implements PDEBase's generate_system hook for MOLDiscretization.
    • Each initial condition becomes a guess for its whole variable.
    • It also becomes one initialization equation 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.
    • Overlapping slices fall back to single points on the overlap, so no point is constrained twice.
    • Stationary systems, and systems without initial conditions, still use PDEBase's generic method.
  • src/dae_discretization.jl:
    • brown_init_offenders works 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_point is keyed by the array unknowns: u => values, D(u) => zeros(size(u)).
  • src/discretization/discretize_vars.jl: system_unknown(s, u) is factored out of get_system_unknowns.
  • test/Discretization/problem_construction.jl: two new test sets.
    • The first checks that a 2D system's initial condition stays one array, that its initialization is the single equation U[2:4, 2:4] ~ ..., and that an edge-aligned variable's initial condition is sampled at x + dx/2.
    • The second checks brown_init_offenders on 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_discretize returns one System that both problem paths start from, so the equations have to be right for each.

  • mtkcompile and ODEProblem (the discretize fallback, explicit Runge–Kutta use, and staggered discretize): MTK builds its initialization system from initialization_eqs and guesses, and that system is what fixes the differential states.
    • The equations are required. With them removed, ODEProblem fails with "Initialization incomplete. Not all of the state variables of the DAE system can be determined".
    • With the slice equations, prob.u0 and the state after init are identical to master's.
  • DAEProblem (the default discretize path): _dae_problem passes build_initializeprob = false with BrownFullBasicInit(), so no solver reads the initialization equations.
    • BrownFullBasicInit holds the differential states at u0, which comes from the operating point and so from the same initial-condition values. It solves for the algebraic states and du0.
    • On this path the equations only feed brown_init_offenders. It confirms that each equation fixes differential points to values that involve no other unknown, which is what BrownFullBasicInit does anyway.

Equivalence with master

Eight systems were built on both paths (DAEProblem, and mtkcompile plus ODEProblem), on master and on this branch. The comparison covers u0, 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 and t = 0.37. On the ODE path it also covers the state after init. The systems are:

System DAE path ODE path
1D, initial condition and equation depending on parameters a, b identical identical
1D PDAE with an algebraic variable identical identical
1D staggered, edge-aligned variable with initial condition x^3 - x identical identical
2D Yee cell: Hx, Hy edge aligned on different axes, PEC walls, a Dt(S) ~ 0 coefficient field identical identical
2D periodic identical identical
1D Neumann, nonuniform grid identical identical
1D second order in time with a Dt initial condition same error as master identical
2D, two coupled variables, time-dependent boundary conditions one u0 entry differs by 1.1e-16 identical

The one difference is in the last system: an algebraic wall point at x = 1 whose initial condition is sin(pi * x) * y + x^2 / 3.

  • Master substitutes symbolically. Its simplification turns π * 1.0 back into π, so sin(π) evaluates to exactly 0.0.
  • The compiled function computes sin(3.141592653589793), which is 1.2e-16.
  • The residual, du0 and p are identical. The point is algebraic, so BrownFullBasicInit re-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, p and the symbols are identical.

Tests

Group Result
Components pass
DAE 22 / 22
Discretization 3618 / 3618 equation discretization; 64 / 64 problem construction, including the new tests
Wave_Eq_Staggered 305 pass, 5 broken
Sol_Interface 24 / 24
Diffusion 269 / 269
MOL_Interface1 no errors (the file has no @test assertions)
MOL_Interface2 10 pass, 4 broken

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.

Stage, 1.63M states Before After
symbolic_discretize 371 B/state, 87 s, 29 GB allocated 321 B/state, 13 s, 4.8 GB allocated
complete + brown_init_offenders 844 B/state, 59 s 837 B/state, 42 s
_dae_operating_point 335 B/state, 2.3 s 8 B/state, 0.004 s
DAEProblem(sys, op) 512 B/state, 128 s 538 B/state, 93 s
Live heap after the build 2.10 KB/state 1.74 KB/state
Peak RSS 8.04 GB 6.68 GB
Total build time 278 s 149 s

symbolic_discretize time over the four sizes:

States Before After
46k 4.7 s 1.8 s
183k 12.1 s 3.1 s
727k 40.8 s 6.5 s
1.63M 87 s 13.3 s

Retained memory scales linearly with the number of states in every stage, before and after.

Known limits

  • DAEProblem stays quadratic without Make DAEProblem construction linear in array unknown size (evaluate_varmap! dedupe) ModelingToolkit.jl#5280. For each scalar D(u[i, j]), the du0 path of varmap_to_vars re-substitutes the whole array. This PR does not change that: at 183k states, DAEProblem still takes 385 s and allocates 457 GB without the MTK fix.

  • Per-state memory left after this PR, about 1.7 KB per state:

    • MTK's IndexCache keeps per-element entries for Initial parameters, about 840 B per state, created in complete.
    • DAEProblem construction retains about 540 B per state, probably the per-element residual code generation; that is not confirmed.
    • MethodOfLines' DiscreteSpace still 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_system is a documented hook, and implementing it here keeps the change in MethodOfLines. The cost is that generate_initialization.jl repeats about 25 lines of PDEBase's System construction: parameter defaults and the System(...) call. That duplication is kept for now. A narrower PDEBase hook, such as discrete_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

`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]>
@xtalax

xtalax commented Oct 19, 2026

Copy link
Copy Markdown
Member Author

keep it in mol

@xtalax
xtalax marked this pull request as ready for review October 19, 2026 20:27
@xtalax
xtalax merged commit c765919 into SciML:master Oct 19, 2026
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant