Skip to content

Commit 0fdba85

Browse files
committed
Update tests
1 parent 0294f36 commit 0fdba85

11 files changed

+76
-65
lines changed

examples/electrical_components.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using ModelingToolkit, OrdinaryDiffEq
88
ODESystem(Equation[], t, [v, i], [], name=name, defaults=[v=>1.0, i=>1.0])
99
end
1010

11-
function ModelingToolkit.connect(::Type{Pin}, ps...)
11+
@namespace function ModelingToolkit.connect(::Type{Pin}, ps...)
1212
eqs = [
1313
0 ~ sum(p->p.i, ps) # KCL
1414
]
@@ -20,13 +20,13 @@ function ModelingToolkit.connect(::Type{Pin}, ps...)
2020
return eqs
2121
end
2222

23-
function Ground(;name)
23+
@namespace function Ground(;name)
2424
@named g = Pin()
2525
eqs = [g.v ~ 0]
2626
ODESystem(eqs, t, [], [], systems=[g], name=name)
2727
end
2828

29-
function ConstantVoltage(;name, V = 1.0)
29+
@namespace function ConstantVoltage(;name, V = 1.0)
3030
val = V
3131
@named p = Pin()
3232
@named n = Pin()
@@ -38,7 +38,7 @@ function ConstantVoltage(;name, V = 1.0)
3838
ODESystem(eqs, t, [], [V], systems=[p, n], defaults=Dict(V => val), name=name)
3939
end
4040

41-
function Resistor(;name, R = 1.0)
41+
@namespace function Resistor(;name, R = 1.0)
4242
val = R
4343
@named p = Pin()
4444
@named n = Pin()
@@ -52,7 +52,7 @@ function Resistor(;name, R = 1.0)
5252
ODESystem(eqs, t, [v], [R], systems=[p, n], defaults=Dict(R => val), name=name)
5353
end
5454

55-
function Capacitor(;name, C = 1.0)
55+
@namespace function Capacitor(;name, C = 1.0)
5656
val = C
5757
@named p = Pin()
5858
@named n = Pin()
@@ -67,7 +67,7 @@ function Capacitor(;name, C = 1.0)
6767
ODESystem(eqs, t, [v], [C], systems=[p, n], defaults=Dict(C => val), name=name)
6868
end
6969

70-
function Inductor(; name, L = 1.0)
70+
@namespace function Inductor(; name, L = 1.0)
7171
val = L
7272
@named p = Pin()
7373
@named n = Pin()

examples/rc_model.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ V = 1.0
88
@named source = ConstantVoltage(V=V)
99
@named ground = Ground()
1010

11-
rc_eqs = [
11+
rc_eqs = @namespace [
1212
connect(source.p, resistor.p)
1313
connect(resistor.n, capacitor.p)
1414
connect(capacitor.n, source.n, ground.g)

examples/serial_inductor.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include("electrical_components.jl")
66
@named inductor2 = Inductor(L=2.0e-2)
77
@named ground = Ground()
88

9-
eqs = [
9+
eqs = @namespace [
1010
connect(source.p, resistor.p)
1111
connect(resistor.n, inductor1.p)
1212
connect(inductor1.n, inductor2.p)

test/components.jl

+18-14
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,40 @@ include("../examples/rc_model.jl")
55

66
sys = structural_simplify(rc_model)
77
@test !isempty(ModelingToolkit.defaults(sys))
8-
u0 = [
8+
u0 = @namespace [
99
capacitor.v => 0.0
1010
capacitor.p.i => 0.0
1111
resistor.v => 0.0
1212
]
1313
prob = ODEProblem(sys, u0, (0, 10.0))
1414
sol = solve(prob, Rodas4())
1515

16-
@test sol[resistor.p.i] == sol[capacitor.p.i]
17-
@test sol[resistor.n.i] == -sol[capacitor.p.i]
18-
@test sol[capacitor.n.i] == -sol[capacitor.p.i]
19-
@test iszero(sol[ground.g.i])
20-
@test iszero(sol[ground.g.v])
21-
@test sol[resistor.v] == sol[source.p.v] - sol[capacitor.p.v]
16+
@namespace begin
17+
@test sol[resistor.p.i] == sol[capacitor.p.i]
18+
@test sol[resistor.n.i] == -sol[capacitor.p.i]
19+
@test sol[capacitor.n.i] == -sol[capacitor.p.i]
20+
@test iszero(sol[ground.g.i])
21+
@test iszero(sol[ground.g.v])
22+
@test sol[resistor.v] == sol[source.p.v] - sol[capacitor.p.v]
23+
end
2224

2325
prob = ODAEProblem(sys, u0, (0, 10.0))
2426
sol = solve(prob, Tsit5())
2527

26-
@test sol[resistor.p.i] == sol[capacitor.p.i]
27-
@test sol[resistor.n.i] == -sol[capacitor.p.i]
28-
@test sol[capacitor.n.i] == -sol[capacitor.p.i]
29-
@test iszero(sol[ground.g.i])
30-
@test iszero(sol[ground.g.v])
31-
@test sol[resistor.v] == sol[source.p.v] - sol[capacitor.p.v]
28+
@namespace begin
29+
@test sol[resistor.p.i] == sol[capacitor.p.i]
30+
@test sol[resistor.n.i] == -sol[capacitor.p.i]
31+
@test sol[capacitor.n.i] == -sol[capacitor.p.i]
32+
@test iszero(sol[ground.g.i])
33+
@test iszero(sol[ground.g.v])
34+
@test sol[resistor.v] == sol[source.p.v] - sol[capacitor.p.v]
35+
end
3236
#using Plots
3337
#plot(sol)
3438

3539
include("../examples/serial_inductor.jl")
3640
sys = structural_simplify(ll_model)
37-
u0 = [
41+
u0 = @namespace [
3842
inductor1.i => 0.0
3943
inductor2.i => 0.0
4044
inductor2.v => 0.0

test/lowering_solving.jl

+17-15
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,26 @@ lorenz2 = ODESystem(eqs,name=:lorenz2)
5050

5151
@variables α(t)
5252
@parameters γ
53-
connections = [0 ~ lorenz1.x + lorenz2.y + α*γ]
53+
connections = @namespace [0 ~ lorenz1.x + lorenz2.y + α*γ]
5454
connected = ODESystem(connections,t,[α],[γ],systems=[lorenz1,lorenz2])
5555

56-
u0 = [lorenz1.x => 1.0,
57-
lorenz1.y => 0.0,
58-
lorenz1.z => 0.0,
59-
lorenz2.x => 0.0,
60-
lorenz2.y => 1.0,
61-
lorenz2.z => 0.0,
62-
α => 2.0]
56+
@namespace begin
57+
u0 = [lorenz1.x => 1.0,
58+
lorenz1.y => 0.0,
59+
lorenz1.z => 0.0,
60+
lorenz2.x => 0.0,
61+
lorenz2.y => 1.0,
62+
lorenz2.z => 0.0,
63+
α => 2.0]
6364

64-
p = [lorenz1.σ => 10.0,
65-
lorenz1.ρ => 28.0,
66-
lorenz1.β => 8/3,
67-
lorenz2.σ => 10.0,
68-
lorenz2.ρ => 28.0,
69-
lorenz2.β => 8/3,
70-
γ => 2.0]
65+
p = [lorenz1.σ => 10.0,
66+
lorenz1.ρ => 28.0,
67+
lorenz1.β => 8/3,
68+
lorenz2.σ => 10.0,
69+
lorenz2.ρ => 28.0,
70+
lorenz2.β => 8/3,
71+
γ => 2.0]
72+
end
7173

7274
tspan = (0.0,100.0)
7375
prob = ODEProblem(connected,u0,tspan,p)

test/nonlinearsystem.jl

+7-6
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,23 @@ lorenz = name -> NonlinearSystem(eqs1, [x,y,z,u,F], [σ,ρ,β], name=name)
9090
lorenz1 = lorenz(:lorenz1)
9191
@test_throws ArgumentError NonlinearProblem(lorenz1, zeros(5))
9292
lorenz2 = lorenz(:lorenz2)
93-
connected = NonlinearSystem([s ~ a + lorenz1.x
93+
connected = NonlinearSystem((@namespace [s ~ a + lorenz1.x
9494
lorenz2.y ~ s
9595
lorenz1.F ~ lorenz2.u
96-
lorenz2.F ~ lorenz1.u], [s, a], [], systems=[lorenz1,lorenz2])
96+
lorenz2.F ~ lorenz1.u]), [s, a], [], systems=[lorenz1,lorenz2])
9797
@test_nowarn alias_elimination(connected)
9898

9999
# system promotion
100100
using OrdinaryDiffEq
101101
@variables t
102102
D = Differential(t)
103103
@named subsys = convert_system(ODESystem, lorenz1, t)
104-
@named sys = ODESystem([D(subsys.x) ~ subsys.x + subsys.x], t, systems=[subsys])
104+
@named sys = ODESystem((@namespace [D(subsys.x) ~ subsys.x + subsys.x]), t, systems=[subsys])
105105
sys = structural_simplify(sys)
106-
prob = ODEProblem(sys, [subsys.x => 1, subsys.z => 2.0], (0, 1.0), [subsys.σ=>1,subsys.ρ=>2,subsys.β=>3])
106+
u0 = @namespace [subsys.x => 1, subsys.z => 2.0]
107+
prob = ODEProblem(sys, u0, (0, 1.0), @namespace [subsys.σ=>1,subsys.ρ=>2,subsys.β=>3])
107108
sol = solve(prob, Rodas5())
108-
@test sol[subsys.x] + sol[subsys.y] - sol[subsys.z] sol[subsys.u]
109+
@test @namespace sol[subsys.x] + sol[subsys.y] - sol[subsys.z] sol[subsys.u]
109110
@test_throws ArgumentError convert_system(ODESystem, sys, t)
110111

111112
@parameters t σ ρ β
@@ -131,7 +132,7 @@ np = NonlinearProblem(ns, [0,0,0], [1,2,3], jac=true, sparse=true)
131132
function issue819()
132133
sys1 = makesys(:sys1)
133134
sys2 = makesys(:sys1)
134-
@test_throws ArgumentError NonlinearSystem([sys2.f ~ sys1.x, sys1.f ~ 0], [], [], systems = [sys1, sys2])
135+
@test_throws ArgumentError NonlinearSystem((@namespace [sys2.f ~ sys1.x, sys1.f ~ 0]), [], [], systems = [sys1, sys2])
135136
end
136137
issue819()
137138
end

test/optimizationsystem.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ sys2 = OptimizationSystem(loss,[x,y],[a,b],name=:sys2)
88

99
@variables z
1010
@parameters β
11-
loss2 = sys1.x - sys2.y + z*β
11+
loss2 = @namespace sys1.x - sys2.y + z*β
1212
combinedsys = OptimizationSystem(loss2,[z],[β],systems=[sys1,sys2],name=:combinedsys)
1313

1414
equations(combinedsys)
@@ -22,14 +22,14 @@ generate_gradient(combinedsys)
2222
generate_hessian(combinedsys)
2323
ModelingToolkit.hessian_sparsity(combinedsys)
2424

25-
u0 = [
25+
u0 = @namespace [
2626
sys1.x=>1.0
2727
sys1.y=>2.0
2828
sys2.x=>3.0
2929
sys2.y=>4.0
3030
z=>5.0
3131
]
32-
p = [
32+
p = @namespace [
3333
sys1.a => 6.0
3434
sys1.b => 7.0
3535
sys2.a => 8.0

test/reactionsystem_components.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ pars = [α₀,α,K,n,δ,β,μ]
2020
@named os₁ = convert(ODESystem, rs; include_zero_odes=false)
2121
@named os₂ = convert(ODESystem, rs; include_zero_odes=false)
2222
@named os₃ = convert(ODESystem, rs; include_zero_odes=false)
23-
connections = [os₁.R ~ os₃.P,
23+
connections = @namespace [os₁.R ~ os₃.P,
2424
os₂.R ~ os₁.P,
2525
os₃.R ~ os₂.P]
2626
@named connected = ODESystem(connections, t, [], [], systems=[os₁,os₂,os₃])
2727
oderepressilator = structural_simplify(connected)
2828

29-
pvals = [os₁.α₀ => 5e-4,
29+
pvals = @namespace [os₁.α₀ => 5e-4,
3030
os₁.α => .5,
3131
os₁.K => 40.0,
3232
os₁.n => 2,
@@ -47,7 +47,7 @@ pvals = [os₁.α₀ => 5e-4,
4747
os₃.δ => (log(2)/120),
4848
os₃.β => (20*log(2)/120),
4949
os₃.μ => (log(2)/600)]
50-
u₀ = [os₁.m => 0.0, os₁.P => 20.0, os₂.m => 0.0, os₂.P => 0.0, os₃.m => 0.0, os₃.P => 0.0]
50+
u₀ = @namespace [os₁.m => 0.0, os₁.P => 20.0, os₂.m => 0.0, os₂.P => 0.0, os₃.m => 0.0, os₃.P => 0.0]
5151
tspan = (0.0, 100000.0)
5252
oprob = ODEProblem(oderepressilator, u₀, tspan, pvals)
5353
sol = solve(oprob, Tsit5())

test/reduction.jl

+13-11
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ ss = ModelingToolkit.get_structure(initialize_system_structure(lorenz1))
7070
@test isempty(setdiff(ss.fullvars, [D(x), F, y, x, D(y), u, z, D(z)]))
7171
lorenz2 = lorenz(:lorenz2)
7272

73-
connected = ODESystem([s ~ a + lorenz1.x
73+
connected = ODESystem((@namespace [
74+
s ~ a + lorenz1.x
7475
lorenz2.y ~ s
7576
lorenz1.F ~ lorenz2.u
76-
lorenz2.F ~ lorenz1.u],t,systems=[lorenz1,lorenz2])
77+
lorenz2.F ~ lorenz1.u]), t, systems=[lorenz1, lorenz2])
7778
@test length(Base.propertynames(connected)) == 10
7879
@test isequal((@nonamespace connected.lorenz1.x), x)
80+
@test isequal(connected.lorenz1.x, x)
7981

8082
# Reduced Flattened System
8183

@@ -85,7 +87,7 @@ reduced_system2 = structural_simplify(structural_simplify(structural_simplify(co
8587
@test isempty(setdiff(states(reduced_system), states(reduced_system2)))
8688
@test isequal(equations(reduced_system), equations(reduced_system2))
8789
@test isequal(observed(reduced_system), observed(reduced_system2))
88-
@test setdiff(states(reduced_system), [
90+
@test setdiff(states(reduced_system), @namespace [
8991
s
9092
a
9193
lorenz1.x
@@ -98,7 +100,7 @@ reduced_system2 = structural_simplify(structural_simplify(structural_simplify(co
98100
lorenz2.u
99101
]) |> isempty
100102

101-
@test setdiff(parameters(reduced_system), [
103+
@test setdiff(parameters(reduced_system), @namespace [
102104
lorenz1.σ
103105
lorenz1.ρ
104106
lorenz1.β
@@ -107,7 +109,7 @@ reduced_system2 = structural_simplify(structural_simplify(structural_simplify(co
107109
lorenz2.β
108110
]) |> isempty
109111

110-
reduced_eqs = [
112+
reduced_eqs = @namespace [
111113
D(lorenz1.x) ~ lorenz1.σ*((lorenz1.y) - (lorenz1.x)) - ((lorenz2.z) - (lorenz2.x) - (lorenz2.y))
112114
D(lorenz1.y) ~ lorenz1.z + lorenz1.x*(lorenz1.ρ - (lorenz1.z)) - (lorenz1.x) - (lorenz1.y)
113115
D(lorenz1.z) ~ lorenz1.x*lorenz1.y - (lorenz1.β*(lorenz1.z))
@@ -118,7 +120,7 @@ reduced_eqs = [
118120

119121
test_equal.(equations(reduced_system), reduced_eqs)
120122

121-
observed_eqs = [
123+
observed_eqs = @namespace [
122124
s ~ lorenz2.y
123125
a ~ lorenz2.y - lorenz1.x
124126
lorenz1.F ~ -((lorenz2.z) - (lorenz2.x) - (lorenz2.y))
@@ -128,15 +130,15 @@ observed_eqs = [
128130
]
129131
test_equal.(observed(reduced_system), observed_eqs)
130132

131-
pp = [
133+
pp = @namespace [
132134
lorenz1.σ => 10
133135
lorenz1.ρ => 28
134136
lorenz1.β => 8/3
135137
lorenz2.σ => 10
136138
lorenz2.ρ => 28
137139
lorenz2.β => 8/3
138140
]
139-
u0 = [
141+
u0 = @namespace [
140142
lorenz1.x => 1.0
141143
lorenz1.y => 0.0
142144
lorenz1.z => 0.0
@@ -148,7 +150,7 @@ prob1 = ODEProblem(reduced_system, u0, (0.0, 100.0), pp)
148150
solve(prob1, Rodas5())
149151

150152
prob2 = SteadyStateProblem(reduced_system, u0, pp)
151-
@test prob2.f.observed(lorenz2.u, prob2.u0, pp) === 1.0
153+
@test prob2.f.observed((@namespace lorenz2.u), prob2.u0, pp) === 1.0
152154

153155

154156
# issue #724 and #716
@@ -161,14 +163,14 @@ let
161163
@variables u_c(t) y_c(t)
162164
@parameters k_P
163165
pc = ODESystem(Equation[u_c ~ k_P * y_c], t, name=:pc)
164-
connections = [
166+
connections = @namespace [
165167
ol.u ~ pc.u_c
166168
pc.y_c ~ ol.y
167169
]
168170
connected = ODESystem(connections, t, systems=[ol, pc])
169171
@test equations(connected) isa Vector{Equation}
170172
reduced_sys = structural_simplify(connected)
171-
ref_eqs = [
173+
ref_eqs = @namespace [
172174
D(ol.x) ~ ol.a*ol.x + ol.b*ol.u
173175
0 ~ pc.k_P*(ol.c*ol.x + ol.d*ol.u) - ol.u
174176
]

test/runtests.jl

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using SafeTestsets, Test
22

3+
#=
34
@safetestset "Variable scope tests" begin include("variable_scope.jl") end
45
@safetestset "Symbolic parameters test" begin include("symbolic_parameters.jl") end
56
@safetestset "Parsing Test" begin include("variable_parsing.jl") end
@@ -21,6 +22,7 @@ using SafeTestsets, Test
2122
@safetestset "Modelingtoolkitize Test" begin include("modelingtoolkitize.jl") end
2223
@safetestset "Constraints Test" begin include("constraints.jl") end
2324
@safetestset "Reduction Test" begin include("reduction.jl") end
25+
=#
2426
@safetestset "Components Test" begin include("components.jl") end
2527
@safetestset "PDE Construction Test" begin include("pde.jl") end
2628
@safetestset "Lowering Integration Test" begin include("lowering_solving.jl") end

0 commit comments

Comments
 (0)