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..1799205 100644 --- a/lib/ModelingToolkitTearing/test/runtests.jl +++ b/lib/ModelingToolkitTearing/test/runtests.jl @@ -14,6 +14,36 @@ 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 + defaults = [x => 0.0, y => 0.0, z => 1.5] + alg = ModelingToolkit.SimpleNonlinearSolve.SimpleNewtonRaphson() + # 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 + @testset "`InferredDiscrete` validation" begin k = ShiftIndex() @variables x(t) y(t)