Skip to content

Commit 08333b6

Browse files
test: test map_variables_to_equations
1 parent 235bcbf commit 08333b6

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

test/structural_transformation/utils.jl

+96-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ using ModelingToolkit
33
using Graphs
44
using SparseArrays
55
using UnPack
6-
using ModelingToolkit: t_nounits as t, D_nounits as D
6+
using ModelingToolkit: t_nounits as t, D_nounits as D, default_toterm
7+
using Symbolics: unwrap
78

89
# Define some variables
910
@parameters L g
@@ -161,3 +162,97 @@ end
161162
structural_simplify(sys; additional_passes = [pass])
162163
@test value[] == 1
163164
end
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 ~ 2x], 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) ~ 2x + y, y ~ x + z, z^3 + x^3 ~ 12], t)
181+
mapping = map_variables_to_equations(sys)
182+
@test mapping[x] == (D(x) ~ 2x + 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 ~ -2xt * x - 2yt * 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 ~ -2xt * x - 2yt * 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 ~ 2y^2 + 1, sin(z) ~ y, z^3 + 4z + 1 ~ 0])
252+
mapping = map_variables_to_equations(sys)
253+
@test mapping[x] == (0 ~ 2y^2 + 1 - x^2)
254+
@test mapping[y] == (y ~ sin(z))
255+
@test mapping[z] == (0 ~ -1 - 4z - z^3)
256+
@test length(mapping) == 3
257+
end
258+
end

0 commit comments

Comments
 (0)