Repository navigation
Do not tear large linear blocks of stationary systems - #5352
Conversation
Tearing the interior of a discretized Laplace equation selects a boundary strip as iteration variables and solves the rest by a recursion whose conditioning degrades exponentially with the grid size (Newton stalls from about 8x8, error 6e23 at 31x31 in MethodOfLines). For systems without an independent variable, strongly connected components of more than max_linear_scc_tearing (default 16) equations that are all linear in their own unknowns are now left untorn and solved as one block. Related: PR SciML#5332 (separate Int64 Bareiss overflow in alias elimination). Agent-Harness: Claude Code 2.1.312 Agent-Model: claude-sonnet-5-5 Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b Agent-Review: unreviewed Co-authored-by: Claude Code <[email protected]>
One conflict in dummy_derivative's signature in src/structural_transformation/symbolics_tearing.jl: master added the non-forwarded verbose keyword, this branch added max_linear_scc_tearing. Kept both. Agent-Harness: Claude Code 2.1.312 Agent-Model: claude-sonnet-5-5 Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b Agent-Review: unreviewed Co-authored-by: Claude Code <[email protected]>
|
Independent check from the MethodOfLines side, MOL master
With registered MTK the same MOL code gives, for n = 9 / 11 / 31, 8 / 10 / 30 unknowns, with The 🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5 |
… ignore list The stationary linear tearing path calls StateSelection.get_mm, which StateSelection does not declare public, failing ExplicitImports all_qualified_accesses_are_public. No public route to the value exists (state.mm is not equivalent after pantelides!), so list it with the other StateSelection names. Agent-Harness: Claude Code 2.1.312 Agent-Model: claude-sonnet-5-5 Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b Agent-Review: unreviewed Co-authored-by: Claude Code <[email protected]>
|
Pushed 🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5 |
|
|
||
| function is_linear_in(eq::Equation, vars::Set{SymbolicT}) | ||
| expr = eq.rhs - eq.lhs | ||
| for v in Symbolics.get_variables(expr) |
There was a problem hiding this comment.
This should use the graph to figure out incidence instead of get_variables.
There was a problem hiding this comment.
Changed in c8383ec: the equation's incident variables now come from the structural graph (𝑠neighbors(graph, ieq) restricted to the component), and get_variables is no longer used for this check.
🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b
| expr = eq.rhs - eq.lhs | ||
| for v in Symbolics.get_variables(expr) | ||
| v in vars || continue | ||
| coeff, _, islinear = Symbolics.linear_expansion(expr, v) |
There was a problem hiding this comment.
This should use the get_linear_expander_for! API, which is much faster.
There was a problem hiding this comment.
Changed in c8383ec: linearity is now decided with get_linear_expander_for!(sys, var, true), expanding each incident variable in turn and carrying the remainder, the same step get_linear_scc_linsol takes. One extra check: a coefficient that still depends on another unknown of the block (for example u1*u2) means the equation is not linear in the block, so it is torn as before; linear in each variable separately would otherwise accept it. There is a test for that case.
🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b
| return big_linear_vars, big_linear_eqs | ||
| end | ||
|
|
||
| function is_linear_in(eq::Equation, vars::Set{SymbolicT}) |
There was a problem hiding this comment.
This entire function should instead just replicate the logic in the inline linear SCC code in reassembly. In fact, I'd propose that LinearBlockTearing be moved to MTKTearing.jl.
There was a problem hiding this comment.
The detection now follows the inline linear-SCC code in reassembly (graph incidence, get_linear_expander_for!, the same component definition) instead of a separate symbolic traversal (c8383ec). LinearBlockTearing is still defined in this repo: moving it into ModelingToolkitTearing needs a change and a release in that package and then a compat bump here, while the struct only uses StateSelection entry points this repo already imports. Would you rather keep it here for now?
🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b
…near block detection Incidence of the equations in a strongly connected component now comes from the structural graph instead of get_variables, and linearity is decided with get_linear_expander_for!, following the inline linear SCC code in reassembly. Agent-Harness: Claude Code 2.1.312 Agent-Model: claude-sonnet-5-5 Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b Agent-Review: unreviewed Co-authored-by: Claude Code <[email protected]>
| mm.nparentrows, mm.ncols, mm.nzrows[keep], mm.row_cols[keep], mm.row_vals[keep] | ||
| ) | ||
| end | ||
| tearing_result, extra = StateSelection.DummyDerivativeTearing()(structure, dummy_derivatives; mm) |
There was a problem hiding this comment.
Instead of assuming the default DummyDerivativeTearing, store this as a configurable option in LinearBlockTearing so users can change the underlying tearing algorithm.
There was a problem hiding this comment.
This one is still not addressed.
There was a problem hiding this comment.
The underlying tearing algorithm is now a field of LinearBlockTearing (tearing_alg, defaulting to StateSelection.DummyDerivativeTearing()). It is exposed as the tearing_alg keyword of dummy_derivative, and therefore of mtkcompile, and is called as tearing_alg(structure, dummy_derivatives; mm). A new test passes a recording wrapper algorithm and checks that it is used, both with the default max_linear_scc_tearing and with typemax(Int). The keyword applies to systems without an independent variable, since the time-dependent path does not go through LinearBlockTearing. Addressed in c71e2631d1.
🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b
| SU.isconst(coeff) && continue | ||
| # A coefficient containing another unknown of the block makes the equation nonlinear. | ||
| for w in @view vars[(i + 1):end] | ||
| dcoeff, _, coeff_linear = MTKBase.get_linear_expander_for!(state.sys, state.fullvars[w], true)(coeff) |
There was a problem hiding this comment.
This doesn't need to do a second linear expansion, it just needs to use SU.query to check if any of the variables occur in coeff. Look at how the calculate_A_b function does it.
There was a problem hiding this comment.
Replaced the second linear expansion with SU.query(in(scc_symbols), coeff) over the component's variables, following calculate_A_b. A coefficient that mentions any block unknown still makes the equation count as nonlinear (pushed in 686110b60d; the coupled-block test still passes).
🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b
| eqs = equations(state) | ||
| for scc in StateSelection.find_var_sccs(graph, matching) | ||
| length(scc) > max_size || continue | ||
| scc_vars = BitSet(scc) |
There was a problem hiding this comment.
Declare scc_vars outside the loop so every iteration doesn't allocate a fresh BitSet.
There was a problem hiding this comment.
scc_vars (and the matching symbolic set) is now created once before the loop and refilled with empty!/push! for each component.
🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b
| for scc in StateSelection.find_var_sccs(graph, matching) | ||
| length(scc) > max_size || continue | ||
| scc_vars = BitSet(scc) | ||
| all(v -> matching[v] isa Int && is_linear_in(state, graph, matching[v], scc_vars), scc) || continue |
There was a problem hiding this comment.
Check that this closure doesn't cause captured variables to be boxed. I don't know when Julia does that but it does, sometimes, randomly.
There was a problem hiding this comment.
Checked with code_warntype(optimize = true): there is no Core.Box in large_linear_sccs or is_linear_in. The captured variables are never rebound after the closure is created, so nothing is boxed.
🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b
| function is_linear_in(state::TearingState, graph, ieq::Int, scc_vars::BitSet) | ||
| eq = equations(state)[ieq] | ||
| expr = SU._iszero(eq.lhs) ? eq.rhs : eq.rhs - eq.lhs | ||
| vars = [v for v in 𝑠neighbors(graph, ieq) if v in scc_vars] |
There was a problem hiding this comment.
| vars = [v for v in 𝑠neighbors(graph, ieq) if v in scc_vars] | |
| vars = filter(in(scc_vars), 𝑠neighbors(graph, ieq)) |
Avoids a closure.
There was a problem hiding this comment.
Applied: filter(in(scc_vars), 𝑠neighbors(graph, ieq)); the intermediate closure and comprehension are gone.
🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent footer below. Agent-Harness: Claude Code 2.1.312 Agent-Model: claude-sonnet-5-5 Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b Agent-Review: unreviewed Co-authored-by: Claude Code <[email protected]>
|
MOL re-test of #5352 at head Setup: MethodOfLines Laplace, Dirichlet (unknowns =
|
| n | registered MTK 11.46.1 | de47c44565 |
c8383ecee8 (build / solve / retcode / steps / max err) |
|---|---|---|---|
| 5 | build throws OverflowError (Int64 in StateSelection Bareiss) |
not run | build throws the same OverflowError (see notes) |
| 6 | 5 unknowns, Success, 2 steps, 1.4e-16 | not run | 5 unknowns, 0.7 s / 2.6 s, Success, 3 steps, 3.9e-16 |
| 7 | 9 unknowns, Success, 3 steps, 1.3e-15 | not run | 25 unknowns, 0.6 s / 1.0 s, Success, 1 step, 5.6e-16 |
| 8 | 7 unknowns, Stalled, 34 steps, 2.2e-11 | not run | 36 unknowns, 0.1 s / 0.3 s, Success, 1 step, 4.4e-16 |
| 9 | 8 unknowns, Stalled, 34 steps, 6.6e-11 | 49 unknowns, Success, 1 step, 4.4e-16 | 49 unknowns, 0.1 s / 0.35 s, Success, 1 step, 4.4e-16 |
| 11 | 10 unknowns, Stalled, 9.5e-8 | 81 unknowns, Success, 1 step, 1.6e-15 | 81 unknowns, 0.2 s / 0.6 s, Success, 1 step, 1.5e-15 |
| 31 | 30 unknowns, MaxIters (1000 steps), 1.85e26 | 841 unknowns, Stalled, 33 steps, 4.4e-16 | 841 unknowns, 1.2 s / 4.3 s, Stalled, 33 steps, 4.4e-16 |
| 61 | not run | 3481 unknowns, Stalled, 33 steps, 7.8e-16 | 3481 unknowns, 5.1 s / 31 s, Stalled, 33 steps, 8.9e-16 |
| 101 | not run | 9801 unknowns, Stalled, 34 steps, 1.7e-15 | 9801 unknowns, 28 s / 205 s, Stalled, 34 steps, 2.0e-15 |
The n=5..9 registered column was re-run in this session; n=11 and n=31 registered and all de47c44565 entries are copied from the earlier run (MOL fcf63bed, Julia 1.12.1). The interior of an n x n grid has (n-2)^2 unknowns; the branch keeps all of them from n=7 on.
Laplace, Neumann (Dx(u(1,y)) ~ y)
| n | registered MTK | de47c44565 |
c8383ecee8 |
|---|---|---|---|
| 9 | not re-run | 56 unknowns, Success, 1 step, 4.8e-15 | 56 unknowns, 15 s (includes compilation) / 2.9 s, Success, 1 step, 2.3e-15 |
| 11 | not re-run | 90 unknowns, Success, 1 step, 8.6e-15 | 90 unknowns, 0.2 s / 1.2 s, Success, 1 step, 4.6e-15 |
| 31 | not re-run | 870 unknowns, Stalled, 33 steps, 7.8e-16 | 870 unknowns, 1.4 s / 4.8 s, Stalled, 33 steps, 3.3e-16 |
400-unknown mixed-derivative steady case (test/Mixed_Derivatives/MOL_Mixed_Deriv.jl setup, order-4 20x20)
| registered MTK | de47c44565 |
c8383ecee8 Julia 1.12.1 |
c8383ecee8 Julia 1.10.12 |
|
|---|---|---|---|---|
| unknowns | 65 | 324 | 324 | 324 |
| retcode | MaxIters | Success | Success | Success |
| Newton steps | 1000 | 2 | 3 | 5 |
| build / solve | not re-run | 21 s / 6.5 s | 15.6 s / 5.0 s | 13.8 s / 3.4 s |
SciMLBase.successful_retcode(sol) is true, so the @test_broken SciMLBase.successful_retcode(sol) at test/Mixed_Derivatives/MOL_Mixed_Deriv.jl:129 still passes unexpectedly (same as with de47c44565). The accuracy check against x*y still differs by 7.5e-2, as before (that PDE has a Dxy term, so x*y is not its solution). I ran the setup of this test as a script; I did not run the GROUP=Mixed_Derivatives test group this time.
max_linear_scc_tearing
The keyword is a keyword of ModelingToolkit.dummy_derivative (default 16), reached through mtkcompile keyword forwarding. MOL calls mtkcompile(sys) without keywords, so it is not reachable through discretize or the discretization kwargs. For the typemax(Int) rows below I added a method for StructuralTransformations.dummy_derivative(::System, ::TearingState; ...) in the test script that forwards max_linear_scc_tearing = typemax(Int); MOL itself was not changed.
| n | interior unknowns (block size) | registered MTK | c8383ecee8 default (16) |
c8383ecee8, typemax(Int) |
|---|---|---|---|---|
| 5 | 9 (below 16) | build throws OverflowError |
build throws OverflowError |
not run (throws earlier, see notes) |
| 6 | 16 (not above 16) | 5 unknowns | 5 unknowns (same as registered) | 5 unknowns, Success, 3 steps |
| 7 | 25 (above 16) | 9 unknowns | 25 unknowns | 9 unknowns, Success, 16 steps |
| 9 | 49 (above 16) | 8 unknowns, Stalled | 49 unknowns, Success | 8 unknowns, Success, 10 steps, 4.4e-16 |
| 31 | 841 | 30 unknowns, MaxIters | 841 unknowns, Stalled | 30 unknowns, MaxIters (1000 steps), 6.3e23 |
So a block at or below 16 gives the same unknown count as the registered path, and a block above 16 is left untorn. Note that the typemax(Int) run at n=9 reaches Success in 10 steps whereas the registered run stalls at 34 steps with the same 8 unknowns; the two runs differ in ModelingToolkit 11.47.0 (branch) versus 11.46.1 (registered), so I cannot attribute that difference to a single change.
abstol
| n | default tolerances | abstol = 1e-9 |
|---|---|---|
| 31 | Stalled, 33 steps, 4.4e-16 | Success, 1 step, 3.8e-15 |
| 61 | Stalled, 33 steps, 8.9e-16 | Success, 1 step, 8.7e-15 |
The solutions are exact to roundoff either way; with default tolerances NonlinearSolve reports Stalled from n=31 on, as with de47c44565.
Julia 1.10.12
| case | unknowns | retcode | steps | max err |
|---|---|---|---|---|
| Dirichlet n=9 | 49 | Success | 1 | 2.2e-16 |
| Dirichlet n=31 | 841 | Stalled | 33 | 3.3e-16 |
| mixed 20x20 order 4 | 324 | Success | 5 | (7.5e-2 vs x*y, as on 1.12) |
No Internal error: stack overflow in type inference and no hang in any 1.10.12 run (environment setup with precompilation, and all three solves, finished well within the 30-minute limit).
What changed since de47c44565 (PR diff de47c44565..c8383ecee8)
Two commits: one adds StateSelection.get_mm to the QA qualified-access ignore list, and c8383ecee8 rewrites the "is this block linear in its own unknowns" test in large_linear_sccs to use the structure graph incidence and the cached get_linear_expander_for!, and to reject coefficients that contain another unknown of the block (new coupled = true test). For the MOL cases above the results are unchanged from de47c44565 (same unknown counts, retcodes and step counts, apart from the mixed case needing 3 instead of 2 steps on 1.12).
Notes and limits
- The n=5
OverflowError(checked_muloverflow inStateSelection.CLIL.bareiss_update_virtual_colswap_mtk!, reached fromalias_elimination!) occurs identically with the registered MTK 11.46.1, so it is not introduced by the PR; it happens beforedummy_derivative, somax_linear_scc_tearingdoes not affect it. I did not investigate further. - The 127 integer-coefficient cap was not probed directly; n=6, 7, 8, 9 all behave according to block size (16 / 25 / 36 / 49 interior unknowns), not according to the stencil centre coefficient (-100, -144, -196, -256).
- Registered-MTK numbers for n=5..9 were run with MTK 11.46.1; the other registered numbers are from the earlier run and may use a slightly different MTK patch version.
- No Neumann or n=61/101 baseline was re-run with the registered MTK; the second solve of an already-solved problem was not repeated this time.
- Single runs, no repeated timings; the box was shared.
🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: 6c520c71-764b-4a7e-a484-62d5a95bcfa1
|
Spot check at the new head 🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5 |
LinearBlockTearing now stores the algorithm it delegates to (default StateSelection.DummyDerivativeTearing()) and dummy_derivative / mtkcompile accept it as the `tearing_alg` keyword, as requested in review. Agent-Harness: Claude Code 2.1.312 Agent-Model: claude-sonnet-5-5 Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b Agent-Review: unreviewed Co-authored-by: Claude Code <[email protected]>
The stationary linear block check reaches for the cached linear expander through ModelingToolkitBase, where it is not public; add it to the ModelingToolkitBase group of NONPUBLIC_QUALIFIED_ACCESSES like the other pair-internal seams. Agent-Harness: Claude Code 2.1.312 Agent-Model: claude-sonnet-5-5 Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b Agent-Review: unreviewed Co-authored-by: Claude Code <[email protected]>
|
Spot check at the approved head 🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5 |
mtkcompileof a large, purely linear steady system collapses it badly. MethodOfLines' steady 2D Laplace 5-point stencil (Dirichletu = x*y, rows scaled by1/h^2) is a good example: at n = 9 (81 unknowns) tearing leaves 8 iteration variables plus 74 solved observed, at n = 11 NewtonRaphson reportsStalled, and at n = 31 it reaches 6e23 error. The stencil interior is one big strongly connected component of the equation-unknown graph that is linear in its own unknowns; tearing turns it into a boundary strip of iteration variables plus a recursionu[i+1,j] = 4u[i,j] - ...whose roundoff grows exponentially with n. (The 127 cap on integer coefficients,manual_dispatch_is_small_intin ModelingToolkitTearing, is why the1/h^2-scaled rows never reach the exact integer linear subsystem; widening it is not the fix, since the rows that are in that subsystem go through Bareiss triangularisation, which fills in densely and overflowsInt64(#5332), and it does nothing for non-integer coefficients.)For systems with no independent variable, a strongly connected component of more than
max_linear_scc_tearing(default 16) equations that are all linear in the component's own unknowns is now left untorn: its rows are withheld from the exact-Bareiss path and its variables stay unknowns solved together by the nonlinear solver.max_linear_scc_tearingis a new keyword ofdummy_derivative, reachable frommtkcompile;max_linear_scc_tearing = typemax(Int)restores the old behaviour. Time-dependent systems already haveinline_linear_sccsand are unchanged, as are small blocks (n <= 6 grids).Hand-written stencil system (n x n scalar unknowns,
1/h^2scaling,NewtonRaphson), unknowns / observed / max error / retcode:Every case solves in one Newton step with this PR. On MethodOfLines master with this PR, the discretized steady Laplace gives n = 9 / 11 / 31 / 61 with 49 / 81 / 841 / 3481 unknowns and max error 4e-16 / 1.5e-15 / 4e-16 / 9e-16. The retcode is
Successfor n = 9 and 11 butStalledfor n = 31 and 61 even though the solutions are exact: that is the roundoff floor of the1/h^2stencil residual against NonlinearSolve's defaultabstoland is handled on the MethodOfLines side (its steady-state branch scalesabstol). The 400-unknown mixed-derivative case (order 4, 20x20) returnsSuccess.Not fixed here: all-integer-coefficient stencils (coefficient 1, n >= 7) and the
1/h^2stencil at n = 5 still hit theInt64OverflowErrorin alias elimination, which runs before tearing; that is the separate problem in #5332. The wrapper replicates a three-line preamble of StateSelection'sdummy_derivative_graph!because that function dropstearing_alg; it could drift if StateSelection changes, and an upstream change to forwardtearing_algwould let this shrink. I tested Julia 1.12.1 only. The untorn block gives a larger Jacobian than the torn one, which on Julia 1.10 can only make the inference stack overflow MethodOfLines saw withjac = trueon large Jacobians more likely, not less.Tests: new
test/structural_transformation/file (13/13) covering the collapse, solve accuracy, unchanged behaviour for small and nonlinear blocks, and the keyword. InterfaceI on the branch merged with current master: 1718 passed, 0 failed, 3 broken; Initialization passed before the merge with master and was not rerun after it. Codex review approved without findings.🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b