Skip to content

Commit 7dc7ab4

Browse files
test: make inline linear SCC test more reliable
It does not rely on a 1x1 SCC anymore
1 parent d954c7a commit 7dc7ab4

1 file changed

Lines changed: 28 additions & 48 deletions

File tree

test/structural_transformation/tearing.jl

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -238,56 +238,35 @@ prob_complex = ODEProblem(sys, u0, (0, 1.0))
238238
sol = solve(prob_complex, Tsit5())
239239
@test all(sol[mass.v] .== 1)
240240

241-
using ModelingToolkitStandardLibrary.Electrical
242-
using ModelingToolkitStandardLibrary.Blocks: Constant
243-
244-
function RCModel(; name)
245-
pars = @parameters begin
246-
R = 1.0
247-
C = 1.0
248-
V = 1.0
249-
end
250-
systems = @named begin
251-
resistor1 = Resistor(R = R)
252-
resistor2 = Resistor(R = R)
253-
capacitor = Capacitor(C = C, v = 0.0)
254-
source = Voltage()
255-
constant = Constant(k = V)
256-
ground = Ground()
257-
end
258-
eqs = [
259-
connect(constant.output, source.V)
260-
connect(source.p, resistor1.p)
261-
connect(resistor1.n, resistor2.p)
262-
connect(resistor2.n, capacitor.p)
263-
connect(capacitor.n, source.n, ground.g)
264-
]
265-
return System(eqs, t, [], pars; systems, name)
266-
end
267-
268-
269241
@testset "Inline linear SCCs" begin
270-
reassemble_alg1 = StructuralTransformations.DefaultReassembleAlgorithm(; inline_linear_sccs = true)
271-
reassemble_alg2 = StructuralTransformations.DefaultReassembleAlgorithm(; inline_linear_sccs = true, analytical_linear_scc_limit = 0)
272-
@mtkcompile sys1 = RCModel()
273-
@mtkcompile sys2 = RCModel() reassemble_alg = reassemble_alg1
274-
@mtkcompile sys3 = RCModel() reassemble_alg = reassemble_alg2
275-
276-
@test length(equations(sys1)) == 2
277-
@test length(equations(sys2)) == 1
278-
@test isequal(only(unknowns(sys2)), sys2.capacitor.v)
279-
@test length(equations(sys3)) == 1
280-
@test isequal(only(unknowns(sys3)), sys3.capacitor.v)
242+
reassemble_alg1 = StructuralTransformations.DefaultReassembleAlgorithm(; inline_linear_sccs = true, analytical_linear_scc_limit = 1)
243+
reassemble_alg2 = StructuralTransformations.DefaultReassembleAlgorithm(; inline_linear_sccs = true, analytical_linear_scc_limit = 5)
244+
@variables x(t)[1:3] y(t)[1:3]
245+
@parameters p[1:3, 1:3]
246+
@mtkcompile sys1 = System([D(x) ~ x, p * y ~ x], t)
247+
@mtkcompile sys2 = System([D(x) ~ x, p * y ~ x], t) reassemble_alg = reassemble_alg1
248+
@mtkcompile sys3 = System([D(x) ~ x, p * y ~ x], t) reassemble_alg = reassemble_alg2
281249

282-
idx = findfirst(observed(sys3)) do eq
250+
@test length(equations(sys1)) > 3
251+
@test length(equations(sys2)) == 3
252+
@test length(equations(sys3)) == 3
253+
254+
idx = findfirst(observed(sys2)) do eq
283255
arr, isarr = ModelingToolkit.MTKBase.split_indexed_var(eq.rhs)
284256
isarr && iscall(arr) && operation(arr) === (\)
285257
end
286258
@test idx !== nothing
259+
# This one is analytically solved
260+
idx = findfirst(observed(sys3)) do eq
261+
arr, isarr = ModelingToolkit.MTKBase.split_indexed_var(eq.rhs)
262+
isarr && iscall(arr) && operation(arr) === (\)
263+
end
264+
@test idx === nothing
287265

288-
prob1 = ODEProblem(sys1, [], (0.0, 10.0); guesses = [sys1.resistor1.v => 1.0])
289-
prob2 = ODEProblem(sys2, [], (0.0, 10.0))
290-
prob3 = ODEProblem(sys3, [], (0.0, 10.0))
266+
pval = rand(3, 3)
267+
prob1 = ODEProblem(sys1, [x => ones(3), p => pval], (0.0, 10.0); guesses = [y => ones(3)])
268+
prob2 = ODEProblem(sys2, [x => ones(3), p => pval], (0.0, 10.0))
269+
prob3 = ODEProblem(sys3, [x => ones(3), p => pval], (0.0, 10.0))
291270

292271
sol1 = solve(prob1, Rodas5P(); abstol = 1.0e-8, reltol = 1.0e-8)
293272
sol2 = solve(prob2, Tsit5(), abstol = 1.0e-8, reltol = 1.0e-8)
@@ -297,15 +276,16 @@ end
297276
@test SciMLBase.successful_retcode(sol2)
298277
@test SciMLBase.successful_retcode(sol3)
299278

300-
@test sol2(sol1.t; idxs = unknowns(sys1)).u sol1.u atol = 1.0e-8
301-
@test sol3(sol1.t; idxs = unknowns(sys1)).u sol1.u atol = 1.0e-8
279+
@test sol2(sol1.t; idxs = unknowns(sys1)).u sol1.u rtol = 1.0e-6
280+
@test sol3(sol1.t; idxs = unknowns(sys1)).u sol1.u rtol = 1.0e-6
302281
end
303282

304283
@testset "`Initial` parameters are added for observed variables solved by inline linear SCCS" begin
305284
reassemble_alg = StructuralTransformations.DefaultReassembleAlgorithm(; inline_linear_sccs = true, analytical_linear_scc_limit = 1)
306-
@mtkcompile sys = RCModel() reassemble_alg = reassemble_alg
307-
@test Initial(sys.resistor1.v) in Set(ModelingToolkit.get_ps(sys))
308-
@test Initial(sys.resistor2.v) in Set(ModelingToolkit.get_ps(sys))
285+
@variables x(t)[1:3] y(t)[1:3]
286+
@parameters p[1:3, 1:3]
287+
@mtkcompile sys = System([D(x) ~ x, p * y ~ x], t) reassemble_alg = reassemble_alg
288+
@test Initial(y) in Set(ModelingToolkit.get_ps(sys))
309289
end
310290

311291
@testset "AD through inline linear SCCs works" begin

0 commit comments

Comments
 (0)