From 5b64bbae1063cd67f41684fb85047303d66cb513 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Mon, 23 Nov 2026 01:58:43 +0000 Subject: [PATCH 1/2] Fix timeless linear SCC inlining License: AGPL-3.0-only Copyright: JuliaHub, Inc. Co-Authored-By: Claude Opus 5.5 --- lib/ModelingToolkitTearing/src/reassemble.jl | 14 +++++++----- lib/ModelingToolkitTearing/test/runtests.jl | 24 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/ModelingToolkitTearing/src/reassemble.jl b/lib/ModelingToolkitTearing/src/reassemble.jl index 9c16768..73bff59 100644 --- a/lib/ModelingToolkitTearing/src/reassemble.jl +++ b/lib/ModelingToolkitTearing/src/reassemble.jl @@ -861,7 +861,6 @@ function get_linear_scc_linsol(state::TearingState, alg_eqs::Vector{Int}, (; fullvars, sys, structure) = state (; graph, solvable_graph, var_to_diff) = structure diff_to_var = invview(var_to_diff) - D = Differential(MTKBase.get_iv(sys)::SymbolicT) # If the SCC is fully torn, don't bother generating a linsolve all_torn = true @@ -900,6 +899,7 @@ function get_linear_scc_linsol(state::TearingState, alg_eqs::Vector{Int}, isdervar = StateSelection.isdervar(state.structure, ivar) isdervar || continue order, lv = var_order(ivar, diff_to_var) + D = Differential(MTKBase.get_iv(sys)::SymbolicT) vars[i] = D(fullvars[lv]) end @@ -1019,7 +1019,11 @@ function get_linear_scc_linsol(state::TearingState, alg_eqs::Vector{Int}, A = SU.ArrayMaker{VartypeT}(A_regions, A_values; shape = SU.ShapeVecT((1:N, 1:N))) b = SU.ArrayMaker{VartypeT}(b_regions, b_values; shape = SU.ShapeVecT((1:N,))) - reference_args = Symbolics.SArgsT((reference, MTKBase.get_iv(sys)::SymbolicT)) + reference_args = Symbolics.SArgsT((reference,)) + iv = MTKBase.get_iv(sys) + if iv !== nothing + push!(reference_args, iv) + end inps = MTKBase.inputs(sys) if !isempty(inps) push!(reference_args, first(inps)) @@ -1657,10 +1661,8 @@ function (alg::DefaultReassembleAlgorithm)(state::TearingState, else iv = D = nothing end - # `iv === nothing` implies a nonlinear system. `SCCNonlinearProblem` should be - # used instead of this pass. `D isa Shift` implies a discrete system, which currently - # doesn't support inline linear SCCs. - if iv === nothing || D isa Shift + # Discrete systems do not support inline linear SCCs. + if D isa Shift inline_linear_sccs = false end diff --git a/lib/ModelingToolkitTearing/test/runtests.jl b/lib/ModelingToolkitTearing/test/runtests.jl index e252a00..1dce3a1 100644 --- a/lib/ModelingToolkitTearing/test/runtests.jl +++ b/lib/ModelingToolkitTearing/test/runtests.jl @@ -14,6 +14,30 @@ using ForwardDiff using LinearAlgebra import ModelingToolkitBase as MTKBase +@testset "Inline linear SCCs without an independent variable" begin + @variables x y z + @parameters a = 2.0 b = 3.0 + @named sys = System([a*x + y ~ z, x + b*y ~ 1, z^2 ~ 4], nothing) + defaults = [x => 0.0, y => 0.0, z => 1.5] + alg = ModelingToolkit.SimpleNonlinearSolve.SimpleNewtonRaphson() + ref = mtkcompile(sys) + @test length(unknowns(ref)) == 2 + refsol = solve(NonlinearProblem(ref, defaults), alg; abstol = 1.0e-10) + @test ModelingToolkit.SciMLBase.successful_retcode(refsol) + @test refsol[[x, y, z]] ≈ [1.0, 0.0, 2.0] atol = 1.0e-8 + for limit in (0, 2) + reassemble_alg = MTKTearing.DefaultReassembleAlgorithm(; + inline_linear_sccs = true, analytical_linear_scc_limit = limit) + # Leave the linear block untorn to exercise both runtime and symbolic solves. + ss = mtkcompile(sys; reassemble_alg, max_linear_scc_tearing = 0) + @test length(unknowns(ss)) == 1 + @test length(MTKTearing.inline_linear_systems(ss)) == (limit == 0 ? 1 : 0) + sol = solve(NonlinearProblem(ss, defaults), alg; abstol = 1.0e-10) + @test ModelingToolkit.SciMLBase.successful_retcode(sol) + @test sol[[x, y, z]] ≈ refsol[[x, y, z]] atol = 1.0e-8 + end +end + @testset "`InferredDiscrete` validation" begin k = ShiftIndex() @variables x(t) y(t) From e6deb6c715c1e05c149c3da01cf2fd0e2337a1c8 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 25 Nov 2026 06:43:44 +0000 Subject: [PATCH 2/2] Make the timeless inline linear SCC test independent of MTK's tearing The test relied on `mtkcompile(...; max_linear_scc_tearing = 0)` to keep the 2x2 linear block untorn. That keyword only exists in unreleased ModelingToolkit 11.48; registered 11.46.1 silently ignores it, tears the block to a single variable and solves it analytically, so no runtime `A \ b` block was emitted and the test failed in the ModelingToolkitTearing CI job. Exercise the runtime path with a block whose coefficients depend on the unknown `z`, which tearing cannot solve on any version, and keep the parameter-coefficient system for the symbolic path. Co-Authored-By: Claude Opus 5.5 Co-Authored-By: Anshul Singhvi Harness: claude Model: claude-opus-5-5 Factory-Task: 0e10eeb2-800a-46f7-bc59-e7991b3a77b6 Agent-Harness: Claude 2.1.326 Agent-Model: claude-opus-5-5 Agent-Session: factory task 0e10eeb2-800a-46f7-bc59-e7991b3a77b6 on m_5343c39de33daf2a74fb039ded191f90 (no session URL) --- lib/ModelingToolkitTearing/test/runtests.jl | 38 ++++++++++++--------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/ModelingToolkitTearing/test/runtests.jl b/lib/ModelingToolkitTearing/test/runtests.jl index 1dce3a1..1799205 100644 --- a/lib/ModelingToolkitTearing/test/runtests.jl +++ b/lib/ModelingToolkitTearing/test/runtests.jl @@ -17,24 +17,30 @@ import ModelingToolkitBase as MTKBase @testset "Inline linear SCCs without an independent variable" begin @variables x y z @parameters a = 2.0 b = 3.0 - @named sys = System([a*x + y ~ z, x + b*y ~ 1, z^2 ~ 4], nothing) defaults = [x => 0.0, y => 0.0, z => 1.5] alg = ModelingToolkit.SimpleNonlinearSolve.SimpleNewtonRaphson() - ref = mtkcompile(sys) - @test length(unknowns(ref)) == 2 - refsol = solve(NonlinearProblem(ref, defaults), alg; abstol = 1.0e-10) - @test ModelingToolkit.SciMLBase.successful_retcode(refsol) - @test refsol[[x, y, z]] ≈ [1.0, 0.0, 2.0] atol = 1.0e-8 - for limit in (0, 2) - reassemble_alg = MTKTearing.DefaultReassembleAlgorithm(; - inline_linear_sccs = true, analytical_linear_scc_limit = limit) - # Leave the linear block untorn to exercise both runtime and symbolic solves. - ss = mtkcompile(sys; reassemble_alg, max_linear_scc_tearing = 0) - @test length(unknowns(ss)) == 1 - @test length(MTKTearing.inline_linear_systems(ss)) == (limit == 0 ? 1 : 0) - sol = solve(NonlinearProblem(ss, defaults), alg; abstol = 1.0e-10) - @test ModelingToolkit.SciMLBase.successful_retcode(sol) - @test sol[[x, y, z]] ≈ refsol[[x, y, z]] atol = 1.0e-8 + # Each case is `(sys, expected x/y/z, number of runtime `A \ b` blocks)`. In + # `runtime`, every coefficient of the linear block in `x, y` depends on the unknown + # `z`, so tearing cannot solve the block and it is always solved at runtime. The + # parameter coefficients of `symbolic` let the block be solved symbolically. Neither + # case depends on how a given ModelingToolkit version tears linear blocks. + @named runtime = System([z*x + (z + 1)*y ~ 1, (z - 1)*x + z*y ~ 0, z^2 ~ 4], nothing) + @named symbolic = System([a*x + y ~ z, x + b*y ~ 1, z^2 ~ 4], nothing) + for (sys, expected, nblocks) in ((runtime, [2.0, -1.0, 2.0], 1), + (symbolic, [1.0, 0.0, 2.0], 0)) + refsol = solve(NonlinearProblem(mtkcompile(sys), defaults), alg; abstol = 1.0e-10) + @test ModelingToolkit.SciMLBase.successful_retcode(refsol) + @test refsol[[x, y, z]] ≈ expected atol = 1.0e-8 + for limit in (0, 2) + reassemble_alg = MTKTearing.DefaultReassembleAlgorithm(; + inline_linear_sccs = true, analytical_linear_scc_limit = limit) + ss = mtkcompile(sys; reassemble_alg) + @test isequal(unknowns(ss), [z]) + @test length(MTKTearing.inline_linear_systems(ss)) == nblocks + sol = solve(NonlinearProblem(ss, defaults), alg; abstol = 1.0e-10) + @test ModelingToolkit.SciMLBase.successful_retcode(sol) + @test sol[[x, y, z]] ≈ expected atol = 1.0e-8 + end end end