Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ModelingToolkitBase/test/sdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@
sys1, [sts .=> [1.0, 0.0, 0.0]; ps .=> [10.0, 26.0]],
(0.0, 100.0)
)
solve(prob, LambaEulerHeun(), seed = 1)

Check warning on line 688 in lib/ModelingToolkitBase/test/sdesystem.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos / Spell Check with Typos

"Lamba" should be "Lambda" or "Lambs" or "Lamb".

# Test ill-formed due to more equations than states in noise equations

Expand Down Expand Up @@ -825,7 +825,7 @@
ρ => 2.33,
]

prob = SDEProblem(de, [u0map; parammap], (0.0, 100.0))
prob = SDEProblem(de, [u0map; parammap], (0.0, 100.0); seed = 42)
# SOSRI only works for diagonal and scalar noise
@test_throws ErrorException solve(prob, SOSRI()).retcode == ReturnCode.Success
# ImplicitEM does work for non-diagonal noise
Expand Down
76 changes: 28 additions & 48 deletions test/structural_transformation/tearing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,56 +238,35 @@ prob_complex = ODEProblem(sys, u0, (0, 1.0))
sol = solve(prob_complex, Tsit5())
@test all(sol[mass.v] .== 1)

using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Blocks: Constant

function RCModel(; name)
pars = @parameters begin
R = 1.0
C = 1.0
V = 1.0
end
systems = @named begin
resistor1 = Resistor(R = R)
resistor2 = Resistor(R = R)
capacitor = Capacitor(C = C, v = 0.0)
source = Voltage()
constant = Constant(k = V)
ground = Ground()
end
eqs = [
connect(constant.output, source.V)
connect(source.p, resistor1.p)
connect(resistor1.n, resistor2.p)
connect(resistor2.n, capacitor.p)
connect(capacitor.n, source.n, ground.g)
]
return System(eqs, t, [], pars; systems, name)
end


@testset "Inline linear SCCs" begin
reassemble_alg1 = StructuralTransformations.DefaultReassembleAlgorithm(; inline_linear_sccs = true)
reassemble_alg2 = StructuralTransformations.DefaultReassembleAlgorithm(; inline_linear_sccs = true, analytical_linear_scc_limit = 0)
@mtkcompile sys1 = RCModel()
@mtkcompile sys2 = RCModel() reassemble_alg = reassemble_alg1
@mtkcompile sys3 = RCModel() reassemble_alg = reassemble_alg2

@test length(equations(sys1)) == 2
@test length(equations(sys2)) == 1
@test isequal(only(unknowns(sys2)), sys2.capacitor.v)
@test length(equations(sys3)) == 1
@test isequal(only(unknowns(sys3)), sys3.capacitor.v)
reassemble_alg1 = StructuralTransformations.DefaultReassembleAlgorithm(; inline_linear_sccs = true, analytical_linear_scc_limit = 1)
reassemble_alg2 = StructuralTransformations.DefaultReassembleAlgorithm(; inline_linear_sccs = true, analytical_linear_scc_limit = 5)
@variables x(t)[1:3] y(t)[1:3]
@parameters p[1:3, 1:3]
@mtkcompile sys1 = System([D(x) ~ x, p * y ~ x], t)
@mtkcompile sys2 = System([D(x) ~ x, p * y ~ x], t) reassemble_alg = reassemble_alg1
@mtkcompile sys3 = System([D(x) ~ x, p * y ~ x], t) reassemble_alg = reassemble_alg2

idx = findfirst(observed(sys3)) do eq
@test length(equations(sys1)) > 3
@test length(equations(sys2)) == 3
@test length(equations(sys3)) == 3

idx = findfirst(observed(sys2)) do eq
arr, isarr = ModelingToolkit.MTKBase.split_indexed_var(eq.rhs)
isarr && iscall(arr) && operation(arr) === (\)
end
@test idx !== nothing
# This one is analytically solved
idx = findfirst(observed(sys3)) do eq
arr, isarr = ModelingToolkit.MTKBase.split_indexed_var(eq.rhs)
isarr && iscall(arr) && operation(arr) === (\)
end
@test idx === nothing

prob1 = ODEProblem(sys1, [], (0.0, 10.0); guesses = [sys1.resistor1.v => 1.0])
prob2 = ODEProblem(sys2, [], (0.0, 10.0))
prob3 = ODEProblem(sys3, [], (0.0, 10.0))
pval = rand(3, 3)
prob1 = ODEProblem(sys1, [x => ones(3), p => pval], (0.0, 10.0); guesses = [y => ones(3)])
prob2 = ODEProblem(sys2, [x => ones(3), p => pval], (0.0, 10.0))
prob3 = ODEProblem(sys3, [x => ones(3), p => pval], (0.0, 10.0))

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

@test sol2(sol1.t; idxs = unknowns(sys1)).u ≈ sol1.u atol = 1.0e-8
@test sol3(sol1.t; idxs = unknowns(sys1)).u ≈ sol1.u atol = 1.0e-8
@test sol2(sol1.t; idxs = unknowns(sys1)).u ≈ sol1.u rtol = 1.0e-6
@test sol3(sol1.t; idxs = unknowns(sys1)).u ≈ sol1.u rtol = 1.0e-6
end

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

@testset "AD through inline linear SCCs works" begin
Expand Down
Loading