@@ -3,8 +3,8 @@ using ModelingToolkit
33using Graphs
44using SparseArrays
55using UnPack
6- using ModelingToolkit: t_nounits as t, D_nounits as D
7- const ST = StructuralTransformations
6+ using ModelingToolkit: t_nounits as t, D_nounits as D, default_toterm
7+ using Symbolics : unwrap
88
99# Define some variables
1010@parameters L g
162162 structural_simplify (sys; additional_passes = [pass])
163163 @test value[] == 1
164164end
165+
166+ @testset " `map_variables_to_equations`" begin
167+ @testset " Not supported for systems without `.tearing_state`" begin
168+ @variables x
169+ @mtkbuild sys = OptimizationSystem (x^ 2 )
170+ @test_throws ArgumentError map_variables_to_equations (sys)
171+ end
172+ @testset " Requires simplified system" begin
173+ @variables x (t) y (t)
174+ @named sys = ODESystem ([D (x) ~ x, y ~ 2 x], t)
175+ sys = complete (sys)
176+ @test_throws ArgumentError map_variables_to_equations (sys)
177+ end
178+ @testset " `ODESystem`" begin
179+ @variables x (t) y (t) z (t)
180+ @mtkbuild sys = ODESystem ([D (x) ~ 2 x + y, y ~ x + z, z^ 3 + x^ 3 ~ 12 ], t)
181+ mapping = map_variables_to_equations (sys)
182+ @test mapping[x] == (D (x) ~ 2 x + y)
183+ @test mapping[y] == (y ~ x + z)
184+ @test mapping[z] == (0 ~ 12 - z^ 3 - x^ 3 )
185+ @test length (mapping) == 3
186+
187+ @testset " With dummy derivatives" begin
188+ @parameters g
189+ @variables x (t) y (t) [state_priority = 10 ] λ (t)
190+ eqs = [D (D (x)) ~ λ * x
191+ D (D (y)) ~ λ * y - g
192+ x^ 2 + y^ 2 ~ 1 ]
193+ @mtkbuild sys = ODESystem (eqs, t)
194+ mapping = map_variables_to_equations (sys)
195+
196+ yt = default_toterm (unwrap (D (y)))
197+ xt = default_toterm (unwrap (D (x)))
198+ xtt = default_toterm (unwrap (D (D (x))))
199+ @test mapping[x] == (0 ~ 1 - x^ 2 - y^ 2 )
200+ @test mapping[y] == (D (y) ~ yt)
201+ @test mapping[D (y)] == (D (yt) ~ - g + y * λ)
202+ @test mapping[D (x)] == (0 ~ - 2 xt * x - 2 yt * y)
203+ @test mapping[D (D (x))] == (xtt ~ x * λ)
204+ @test length (mapping) == 5
205+
206+ @testset " `rename_dummy_derivatives = false`" begin
207+ mapping = map_variables_to_equations (sys; rename_dummy_derivatives = false )
208+
209+ @test mapping[x] == (0 ~ 1 - x^ 2 - y^ 2 )
210+ @test mapping[y] == (D (y) ~ yt)
211+ @test mapping[yt] == (D (yt) ~ - g + y * λ)
212+ @test mapping[xt] == (0 ~ - 2 xt * x - 2 yt * y)
213+ @test mapping[xtt] == (xtt ~ x * λ)
214+ @test length (mapping) == 5
215+ end
216+ end
217+ @testset " DDEs" begin
218+ function oscillator (; name, k = 1.0 , τ = 0.01 )
219+ @parameters k= k τ= τ
220+ @variables x (.. )= 0.1 y (t)= 0.1 jcn (t)= 0.0 delx (t)
221+ eqs = [D (x (t)) ~ y,
222+ D (y) ~ - k * x (t - τ) + jcn,
223+ delx ~ x (t - τ)]
224+ return System (eqs, t; name = name)
225+ end
226+
227+ systems = @named begin
228+ osc1 = oscillator (k = 1.0 , τ = 0.01 )
229+ osc2 = oscillator (k = 2.0 , τ = 0.04 )
230+ end
231+ eqs = [osc1. jcn ~ osc2. delx,
232+ osc2. jcn ~ osc1. delx]
233+ @named coupledOsc = System (eqs, t)
234+ @mtkbuild sys = compose (coupledOsc, systems)
235+ mapping = map_variables_to_equations (sys)
236+ x1 = operation (unwrap (osc1. x))
237+ x2 = operation (unwrap (osc2. x))
238+ @test mapping[osc1. x] == (D (osc1. x) ~ osc1. y)
239+ @test mapping[osc1. y] == (D (osc1. y) ~ osc1. jcn - osc1. k * x1 (t - osc1. τ))
240+ @test mapping[osc1. delx] == (osc1. delx ~ x1 (t - osc1. τ))
241+ @test mapping[osc1. jcn] == (osc1. jcn ~ osc2. delx)
242+ @test mapping[osc2. x] == (D (osc2. x) ~ osc2. y)
243+ @test mapping[osc2. y] == (D (osc2. y) ~ osc2. jcn - osc2. k * x2 (t - osc2. τ))
244+ @test mapping[osc2. delx] == (osc2. delx ~ x2 (t - osc2. τ))
245+ @test mapping[osc2. jcn] == (osc2. jcn ~ osc1. delx)
246+ @test length (mapping) == 8
247+ end
248+ end
249+ @testset " `NonlinearSystem`" begin
250+ @variables x y z
251+ @mtkbuild sys = NonlinearSystem ([x^ 2 ~ 2 y^ 2 + 1 , sin (z) ~ y, z^ 3 + 4 z + 1 ~ 0 ])
252+ mapping = map_variables_to_equations (sys)
253+ @test mapping[x] == (0 ~ 2 y^ 2 + 1 - x^ 2 )
254+ @test mapping[y] == (y ~ sin (z))
255+ @test mapping[z] == (0 ~ - 1 - 4 z - z^ 3 )
256+ @test length (mapping) == 3
257+ end
258+ end
0 commit comments