11using ModelingToolkit
2- using ModelingToolkit: SymScope
2+ using ModelingToolkit: SymScope, t_nounits as t, D_nounits as D
33using Symbolics: arguments, value
44using Test
55
6- @parameters t
76@variables a b (t) c d e (t)
87
98b = ParentScope (b)
5251@test renamed ([:foo :bar :baz ], c) == Symbol (" foo₊c" )
5352@test renamed ([:foo :bar :baz ], d) == :d
5453
55- @parameters t a b c d e f
54+ @parameters a b c d e f
5655p = [a
5756 ParentScope (b)
5857 ParentScope (ParentScope (c))
@@ -73,3 +72,102 @@ ps = ModelingToolkit.getname.(parameters(level3))
7372@test isequal (ps[4 ], :level2₊level0₊d )
7473@test isequal (ps[5 ], :level1₊level0₊e )
7574@test isequal (ps[6 ], :f )
75+
76+ @variables x (t) y (t)[1 : 2 ]
77+ @parameters p q[1 : 2 ]
78+
79+ @test_throws [" Symbol" , " x(t)" , " does not occur" ] ODESystem (
80+ [D (x) ~ p], t, [], [p]; name = :foo )
81+ @test_nowarn ODESystem ([D (x) ~ p], t, [x], [p]; name = :foo )
82+ @test_throws [" Symbol" , " y(t)" , " does not occur" ] ODESystem (
83+ D (y) ~ q, t, [], [q]; name = :foo )
84+ @test_nowarn ODESystem (D (y) ~ q, t, [y], [q]; name = :foo )
85+ @test_throws [" Symbol" , " y(t)" , " [1]" , " does not occur" ] ODESystem (
86+ D (y[1 ]) ~ x, t, [x], []; name = :foo )
87+ @test_nowarn ODESystem (D (y[1 ]) ~ x, t, [x, y], []; name = :foo )
88+ @test_throws [" Symbol" , " p" , " does not occur" ] ODESystem (D (x) ~ p, t, [x], []; name = :foo )
89+ @test_nowarn ODESystem (D (x) ~ p, t, [x], [p]; name = :foo )
90+ @test_throws [" Symbol" , " q" , " does not occur" ] ODESystem (D (y) ~ q, t, [y], []; name = :foo )
91+ @test_nowarn ODESystem (D (y) ~ q, t, [y], [q]; name = :foo )
92+ @test_throws [" Symbol" , " q" , " [1]" , " does not occur" ] ODESystem (
93+ D (y[1 ]) ~ q[1 ], t, [y], []; name = :foo )
94+ @test_nowarn ODESystem (D (y[1 ]) ~ q[1 ], t, [y], [q]; name = :foo )
95+ @test_throws [" Symbol" , " x(t)" , " does not occur" ] ODESystem (
96+ Equation[], t, [], [p]; name = :foo , continuous_events = [[x ~ 0.0 ] => [p ~ 1.0 ]])
97+ @test_nowarn ODESystem (
98+ Equation[], t, [x], [p]; name = :foo , continuous_events = [[x ~ 0.0 ] => [p ~ 1.0 ]])
99+
100+ @named sys1 = ODESystem (Equation[], t, [x, y], [p, q])
101+ @test_throws [" Unexpected" , " sys1₊x(t)" , " subsystem with name sys1" ] ODESystem (
102+ [D (x) ~ sys1. x], t; name = :sys2 )
103+ @test_nowarn ODESystem ([D (x) ~ sys1. x], t; name = :sys2 , systems = [sys1])
104+ @test_throws [" Unexpected" , " sys1₊y(t)" , " subsystem with name sys1" ] ODESystem (
105+ [D (x) ~ sum (sys1. y)], t; name = :sys2 )
106+ @test_nowarn ODESystem ([D (x) ~ sum (sys1. y)], t; name = :sys2 , systems = [sys1])
107+ @test_throws [" Unexpected" , " sys1₊y(t)" , " [1]" , " subsystem with name sys1" ] ODESystem (
108+ D (x) ~ sys1. y[1 ], t; name = :sys2 )
109+ @test_nowarn ODESystem (D (x) ~ sys1. y[1 ], t; name = :sys2 , systems = [sys1])
110+ @test_throws [" Unexpected" , " sys1₊p" , " subsystem with name sys1" ] ODESystem (
111+ D (x) ~ sys1. p, t; name = :sys2 )
112+ @test_nowarn ODESystem (D (x) ~ sys1. p, t; name = :sys2 , systems = [sys1])
113+ @test_throws [" Unexpected" , " sys1₊q" , " subsystem with name sys1" ] ODESystem (
114+ D (y) ~ sys1. q, t; name = :sys2 )
115+ @test_nowarn ODESystem (D (y) ~ sys1. q, t; name = :sys2 , systems = [sys1])
116+ @test_throws [" Unexpected" , " sys1₊q" , " [1]" , " subsystem with name sys1" ] ODESystem (
117+ D (x) ~ sys1. q[1 ], t; name = :sys2 )
118+ @test_nowarn ODESystem (D (x) ~ sys1. q[1 ], t; name = :sys2 , systems = [sys1])
119+ @test_throws [" Unexpected" , " sys1₊x(t)" , " subsystem with name sys1" ] ODESystem (
120+ Equation[], t, [], [p]; name = :sys2 , continuous_events = [[sys1. x ~ 0 ] => [p ~ 1.0 ]])
121+ @test_nowarn ODESystem (Equation[], t, [], [p]; name = :sys2 ,
122+ continuous_events = [[sys1. x ~ 0 ] => [p ~ 1.0 ]], systems = [sys1])
123+
124+ # Ensure SDESystem checks noise eqs as well
125+ @test_throws [" Symbol" , " x(t)" , " does not occur" ] SDESystem (
126+ Equation[], [0.1 x], t, [], []; name = :foo )
127+ @test_nowarn SDESystem (Equation[], [0.1 x], t, [x], []; name = :foo )
128+ @named sys1 = SDESystem (Equation[], [], t, [x], [])
129+ @test_throws [" Unexpected" , " sys1₊x(t)" , " subsystem with name sys1" ] SDESystem (
130+ Equation[], [0.1 sys1. x], t, [], []; name = :foo )
131+ @test_nowarn SDESystem (Equation[], [0.1 sys1. x], t, [], []; name = :foo , systems = [sys1])
132+
133+ # Ensure DiscreteSystem checks work
134+ k = ShiftIndex (t)
135+ @test_throws [" Symbol" , " x(t)" , " does not occur" ] DiscreteSystem (
136+ [x ~ x (k - 1 ) + x (k - 2 )], t, [], []; name = :foo )
137+ @test_nowarn DiscreteSystem ([x ~ x (k - 1 ) + x (k - 2 )], t; name = :foo )
138+ @named sys1 = DiscreteSystem (Equation[], t, [x], [])
139+ @test_throws [" Unexpected" , " sys1₊x(t)" , " subsystem with name sys1" ] DiscreteSystem (
140+ [x ~ x (k - 1 ) + sys1. x (k - 2 )], t, [x], []; name = :sys2 )
141+ @test_nowarn DiscreteSystem (
142+ [x ~ x (k - 1 ) + sys1. x (k - 2 )], t, [x], []; name = :sys2 , systems = [sys1])
143+
144+ # Ensure NonlinearSystem checks work
145+ @variables x
146+ @test_throws [" Symbol" , " x" , " does not occur" ] NonlinearSystem (
147+ [0 ~ 2 x + 3 ], [], []; name = :foo )
148+ @test_nowarn NonlinearSystem ([0 ~ 2 x + 3 ], [x], []; name = :foo )
149+ @named sys1 = NonlinearSystem (Equation[], [x], [])
150+ @test_throws [" Unexpected" , " sys1₊x" , " subsystem with name sys1" ] NonlinearSystem (
151+ [0 ~ sys1. x + 3 ], [], []; name = :foo )
152+ @test_nowarn NonlinearSystem ([0 ~ sys1. x + 3 ], [], []; name = :foo , systems = [sys1])
153+
154+ # Ensure ConstraintsSystem checks work
155+ @test_throws [" Symbol" , " x" , " does not occur" ] ConstraintsSystem (
156+ [0 ~ x^ 2 - 3 ], [], []; name = :foo )
157+ @test_nowarn ConstraintsSystem ([0 ~ x^ 2 - 3 ], [x], []; name = :foo )
158+ @test_throws [" Symbol" , " x" , " does not occur" ] ConstraintsSystem (
159+ [Inequality (x^ 2 , 3 , < )], [], []; name = :foo )
160+ @test_nowarn ConstraintsSystem ([Inequality (x^ 2 , 3 , < )], [x], []; name = :foo )
161+ @named sys1 = ConstraintsSystem (Equation[], [x], [])
162+ @test_throws [" Unexpected" , " sys1₊x" , " subsystem with name sys1" ] ConstraintsSystem (
163+ [0 ~ sys1. x^ 2 - 2 ], [], []; name = :sys2 )
164+ @test_nowarn ConstraintsSystem ([0 ~ sys1. x^ 2 - 2 ], [], []; name = :sys2 , systems = [sys1])
165+
166+ # Ensure OptimizationSystem checks work
167+ @test_throws [" Symbol" , " x" , " does not occur" ] OptimizationSystem (
168+ y[1 ], [y[1 ]], []; constraints = [x ~ 3 ], name = :foo )
169+ @test_nowarn OptimizationSystem (y[1 ], [y[1 ], x], []; constraints = [x ~ 3 ], name = :foo )
170+ @named sys1 = OptimizationSystem (x, [x], [])
171+ @test_throws [" Unexpected" , " sys1₊x" , " subsystem with name sys1" ] OptimizationSystem (
172+ sys1. x^ 2 - 2 , [], []; name = :sys2 )
173+ @test_nowarn OptimizationSystem (sys1. x^ 2 - 2 , [], []; name = :sys2 , systems = [sys1])
0 commit comments