Skip to content

Commit 00d56d4

Browse files
test: update tests according to new alias elimination
1 parent 5193855 commit 00d56d4

8 files changed

Lines changed: 57 additions & 54 deletions

File tree

lib/ModelingToolkitBase/test/analysis_points.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ModelingToolkitBase, ModelingToolkitStandardLibrary.Blocks, ControlSystemsBase
22
using ModelingToolkitStandardLibrary.Mechanical.Rotational
33
using ModelingToolkitStandardLibrary.Blocks
4+
using SymbolicIndexingInterface
45
using OrdinaryDiffEq, LinearAlgebra
56
using Test
67
using ModelingToolkitBase: t_nounits as t, D_nounits as D, AnalysisPoint, AbstractSystem
@@ -618,6 +619,16 @@ if @isdefined(ModelingToolkit)
618619
@test SciMLBase.successful_retcode(solve(prob, Rodas5P()))
619620
matrices_normal, _ = get_sensitivity(sys_normal, sys_normal.normal_inner.ap)
620621

622+
function compare_matrices(reference, value)
623+
@assert size(reference.A) == (2, 2) "This testing function is only valid for `2x2` systems"
624+
@test isequal(reference.C, value.C) || isequal(reverse(reference.C), value.C)
625+
colorder = isequal(reference.C, value.C) ? [1, 2] : [2, 1]
626+
@test isequal(reference.B, value.B) || isequal(reverse(reference.B), value.B)
627+
roworder = isequal(reference.B, value.B) ? [1, 2] : [2, 1]
628+
@test isequal(reference.D, value.D)
629+
@test isequal(reference.A[roworder, colorder], value.A)
630+
end
631+
621632
@testset "Analysis point overriding part of connection - normal connect" begin
622633
@named F1 = FirstOrder(k = 1, T = 1)
623634
@named F2 = FirstOrder(k = 1, T = 1)
@@ -643,7 +654,7 @@ if @isdefined(ModelingToolkit)
643654
@test SciMLBase.successful_retcode(solve(prob, Rodas5P()))
644655

645656
matrices, _ = get_sensitivity(sys, sys.ap)
646-
@test matrices == matrices_normal
657+
compare_matrices(matrices_normal, matrices)
647658
end
648659

649660
@testset "Analysis point overriding part of connection - variable connect" begin
@@ -671,7 +682,7 @@ if @isdefined(ModelingToolkit)
671682
@test SciMLBase.successful_retcode(solve(prob, Rodas5P()))
672683

673684
matrices, _ = get_sensitivity(sys, sys.ap)
674-
@test matrices == matrices_normal
685+
compare_matrices(matrices_normal, matrices)
675686
end
676687

677688
@testset "Analysis point overriding part of connection - mixed connect" begin
@@ -699,7 +710,7 @@ if @isdefined(ModelingToolkit)
699710
@test SciMLBase.successful_retcode(solve(prob, Rodas5P()))
700711

701712
matrices, _ = get_sensitivity(sys, sys.ap)
702-
@test matrices == matrices_normal
713+
compare_matrices(matrices_normal, matrices)
703714
end
704715

705716
@testset "Ignored analysis points only affect relevant connection sets" begin

lib/ModelingToolkitBase/test/initial_values.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ end
133133
end
134134

135135
@testset "Cyclic dependency checking and substitution limits" begin
136-
@variables x(t) y(t)
136+
# `irreducible` to make sure they are unknowns of the initialization system
137+
@variables x(t) [irreducible = true] y(t) [irreducible = true]
137138
@mtkcompile sys = System(
138139
[D(x) ~ x, D(y) ~ y], t; initialization_eqs = [x ~ 2y + 3, y ~ 2x],
139140
guesses = [x => 2y, y => 2x]

lib/ModelingToolkitBase/test/initializationsystem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ conditions = getfield.(equations(initprob.f.sys), :rhs)
331331
if @isdefined(ModelingToolkit)
332332
initsol = solve(initprob, reltol = 1.0e-12, abstol = 1.0e-12)
333333
@test SciMLBase.successful_retcode(initsol)
334-
@test maximum(abs.(initsol[conditions])) < 1.0e-8
334+
@test maximum(abs.(initsol[conditions])) < 2.0e-8
335335
end
336336

337337
@test_throws ERRMOD.ExtraEquationsSystemException ModelingToolkitBase.InitializationProblem(
@@ -1343,7 +1343,7 @@ if @isdefined(ModelingToolkit)
13431343
model = dc_motor()
13441344
sys = mtkcompile(model)
13451345

1346-
prob = ODEProblem(sys, [sys.L1.i => 0.0], (0, 6.0))
1346+
prob = ODEProblem(sys, [sys.L1.i => 0.0], (0, 6.0); guesses = [sys.emf.flange.phi => 0])
13471347

13481348
@test_nowarn remake(prob, p = prob.p)
13491349
end

lib/ModelingToolkitBase/test/input_output_handling.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,16 @@ if @isdefined(ModelingToolkit)
141141
model_inputs = [torque.tau.u]
142142
op = Dict(torque.tau.u => 0.0)
143143
matrices, ssys = linearize(
144-
model, model_inputs, model_outputs; op
144+
model, model_inputs, model_outputs; op,
145+
guesses = [inertia2.flange_a.phi => 0.0, inertia1.flange_b.phi => 0.0]
145146
)
146147
@test length(ModelingToolkit.outputs(ssys)) == 4
147148

148149
let # Just to have a local scope for D
149-
matrices, ssys = linearize(model, model_inputs, [y]; op)
150+
matrices, ssys = linearize(
151+
model, model_inputs, [y]; op,
152+
guesses = [inertia2.flange_a.phi => 0.0, inertia1.flange_b.phi => 0.0]
153+
)
150154
A, B, C, D = matrices
151155
obsf = ModelingToolkit.build_explicit_observed_function(
152156
ssys,

lib/ModelingToolkitBase/test/split_parameters.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ if @isdefined(ModelingToolkit)
164164
inputs = [model.torque.tau.u]
165165
op = [model.torque.tau.u => 0.0]
166166
matrices, ssys = ModelingToolkit.linearize(
167-
wr(model), inputs, model_outputs; op
167+
wr(model), inputs, model_outputs; op,
168+
guesses = [model.inertia2.flange_a.phi => 0.0, model.inertia1.flange_b.phi => 0.0]
168169
)
169170

170171
# Design state-feedback gain using LQR

test/linearize.jl

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -122,27 +122,32 @@ connections = [
122122

123123
@named cl = System(connections, t, systems = [f, c, p])
124124

125-
lsys0, ssys = linearize(cl, [f.u], [p.x])
126-
desired_order = [f.x, p.x]
127-
lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order)
128-
lsys1, ssys = linearize(cl, [f.u], [p.x]; autodiff = AutoFiniteDiff())
129-
lsys2 = ModelingToolkit.reorder_unknowns(lsys1, unknowns(ssys), desired_order)
125+
function compare_matrices(reference, value)
126+
@assert size(reference.A) == (2, 2) "This testing function is only valid for `2x2` systems"
127+
@test isapprox(reference.C, value.C) || isapprox(reverse(reference.C), value.C)
128+
colorder = isapprox(reference.C, value.C) ? [1, 2] : [2, 1]
129+
@test isapprox(reference.B, value.B) || isapprox(reverse(reference.B), value.B)
130+
roworder = isapprox(reference.B, value.B) ? [1, 2] : [2, 1]
131+
@test isapprox(reference.D, value.D)
132+
@test isapprox(reference.A[roworder, colorder], value.A)
133+
end
134+
lsys, ssys = linearize(cl, [f.u], [p.x])
135+
lsys2, ssys = linearize(cl, [f.u], [p.x]; autodiff = AutoFiniteDiff())
130136

131-
@test lsys.A == lsys2.A == [-2 0; 1 -2]
132-
@test lsys.B == lsys2.B == reshape([1, 0], 2, 1)
133-
@test lsys.C == lsys2.C == [0 1]
134-
@test lsys.D[] == lsys2.D[] == 0
137+
compare_matrices(lsys, lsys2)
138+
compare_matrices(lsys, (; A = [-2 0; 1 -2], B = reshape([1, 0], 2, 1), C = [0 1], D = [0;;]))
135139

136140
## Symbolic linearization
137141
lsyss_ns, ssys_ns = ModelingToolkit.linearize_symbolic(cl, [f.u], [p.x], split = false)
138142
lsyss, ssys = ModelingToolkit.linearize_symbolic(cl, [f.u], [p.x])
139143
@test isequal(lsyss.A, lsyss_ns.A)
140144

141-
lsyss = ModelingToolkit.reorder_unknowns(lsyss, unknowns(ssys), [f.x, p.x])
142-
@test value.(ModelingToolkit.fixpoint_sub(lsyss.A, ModelingToolkit.initial_conditions(cl))) == lsys.A
143-
@test value.(ModelingToolkit.fixpoint_sub(lsyss.B, ModelingToolkit.initial_conditions(cl))) == lsys.B
144-
@test value.(ModelingToolkit.fixpoint_sub(lsyss.C, ModelingToolkit.initial_conditions(cl))) == lsys.C
145-
@test value.(ModelingToolkit.fixpoint_sub(lsyss.D, ModelingToolkit.initial_conditions(cl))) == lsys.D
145+
_substituter(M, sys) = value.(ModelingToolkit.fixpoint_sub(M, ModelingToolkit.initial_conditions_and_guesses(sys); fold = Val(true)))
146+
lsys_m = (;
147+
A = _substituter(lsyss.A, cl), B = _substituter(lsyss.B, cl),
148+
C = _substituter(lsyss.C, cl), D = _substituter(lsyss.D, cl)
149+
)
150+
compare_matrices(lsys, lsys_m)
146151
##
147152
using ModelingToolkitStandardLibrary.Blocks: LimPID
148153
k = 400
@@ -152,14 +157,12 @@ Nd = 10
152157
@named pid = LimPID(; k, Ti, Td, Nd)
153158

154159
@unpack reference, measurement, ctr_output = pid
155-
lsys0,
160+
lsys,
156161
ssys = linearize(
157162
pid, [reference.u, measurement.u], [ctr_output.u];
158163
op = Dict(reference.u => 0.0, measurement.u => 0.0)
159164
)
160165
@unpack int, der = pid
161-
desired_order = [int.x, der.x]
162-
lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order)
163166

164167
@test lsys.A == [0 0; 0 -10]
165168
@test lsys.B == [2 -2; 10 -10]
@@ -171,31 +174,14 @@ lsyss0,
171174
pid, [reference.u, measurement.u],
172175
[ctr_output.u]
173176
)
174-
lsyss = ModelingToolkit.reorder_unknowns(lsyss0, unknowns(ssys2), desired_order)
175-
176-
@test value.(
177-
ModelingToolkit.fixpoint_sub(
178-
lsyss.A, ModelingToolkit.initial_conditions_and_guesses(pid); fold = Val(true)
179-
)
180-
) == lsys.A
181-
@test value.(
182-
ModelingToolkit.fixpoint_sub(
183-
lsyss.B, ModelingToolkit.initial_conditions_and_guesses(pid); fold = Val(true)
184-
)
185-
) == lsys.B
186-
@test value.(
187-
ModelingToolkit.fixpoint_sub(
188-
lsyss.C, ModelingToolkit.initial_conditions_and_guesses(pid); fold = Val(true)
189-
)
190-
) == lsys.C
191-
@test value.(
192-
ModelingToolkit.fixpoint_sub(
193-
lsyss.D, ModelingToolkit.initial_conditions_and_guesses(pid); fold = Val(true)
194-
)
195-
) == lsys.D
177+
lsyss = (;
178+
A = _substituter(lsyss0.A, pid), B = _substituter(lsyss0.B, pid),
179+
C = _substituter(lsyss0.C, pid), D = _substituter(lsyss0.D, pid)
180+
)
181+
compare_matrices(lsys, lsyss)
196182

197183
# Test with the reverse desired unknown order as well to verify that similarity transform and reoreder_unknowns really works
198-
lsys = ModelingToolkit.reorder_unknowns(lsys, desired_order, reverse(desired_order))
184+
lsys = ModelingToolkit.reorder_unknowns(lsys, unknowns(ssys), reverse(unknowns(ssys)))
199185

200186
@test lsys.A == [-10 0; 0 0]
201187
@test lsys.B == [10 -10; 2 -2]

test/structural_transformation/tearing.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ eqs = [
156156
]
157157
@named daesys = System(eqs, t)
158158
newdaesys = mtkcompile(daesys)
159-
@test issetequal(equations(newdaesys), [D(x) ~ h * z; 0 ~ y + sin(z) - p * t])
159+
@test issetequal(equations(newdaesys), [D(x) ~ h * z; 0 ~ x + sin(z) - p * t])
160160
@test issetequal(
161161
equations(tearing_substitution(newdaesys)), [D(x) ~ h * z; 0 ~ x + sin(z) - p * t]
162162
)
@@ -260,7 +260,7 @@ end
260260
@test length(equations(sys3)) == 1
261261
@test isequal(only(unknowns(sys3)), sys3.capacitor.v)
262262

263-
idx = findfirst(isequal(sys3.capacitor.i), observables(sys3))
263+
idx = findfirst(isequal(sys3.capacitor.p.i), observables(sys3))
264264
rhs = observed(sys3)[idx].rhs
265265
@test operation(rhs) === getindex
266266
@test operation(arguments(rhs)[1]) === (\)

test/structural_transformation/utils.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ end
194194
@testset "DDEs" begin
195195
function oscillator(; name, k = 1.0, τ = 0.01)
196196
@parameters k = k τ = τ
197-
@variables x(..) = 0.1 y(t) = 0.1 jcn(t) = 0.0 delx(t)
197+
@variables x(..) = 0.1 y(t) = 0.1 jcn(t) = 0.0 [state_priority = -1] delx(t)
198198
eqs = [
199199
D(x(t)) ~ y,
200200
D(y) ~ -k * x(t - τ) + jcn,
@@ -217,11 +217,11 @@ end
217217
x1 = operation(unwrap(osc1.x))
218218
x2 = operation(unwrap(osc2.x))
219219
@test mapping[sys.osc1.x] == (D(sys.osc1.x) ~ sys.osc1.y)
220-
@test mapping[sys.osc1.y] == (D(sys.osc1.y) ~ sys.osc1.jcn - sys.osc1.k * x1(t - sys.osc1.τ))
220+
@test mapping[sys.osc1.y] == (D(sys.osc1.y) ~ sys.osc2.delx - sys.osc1.k * x1(t - sys.osc1.τ))
221221
@test mapping[sys.osc1.delx] == (sys.osc1.delx ~ x1(t - sys.osc1.τ))
222222
@test mapping[sys.osc1.jcn] == (sys.osc1.jcn ~ sys.osc2.delx)
223223
@test mapping[sys.osc2.x] == (D(sys.osc2.x) ~ sys.osc2.y)
224-
@test mapping[sys.osc2.y] == (D(sys.osc2.y) ~ sys.osc2.jcn - sys.osc2.k * x2(t - sys.osc2.τ))
224+
@test mapping[sys.osc2.y] == (D(sys.osc2.y) ~ sys.osc1.delx - sys.osc2.k * x2(t - sys.osc2.τ))
225225
@test mapping[sys.osc2.delx] == (sys.osc2.delx ~ x2(t - sys.osc2.τ))
226226
@test mapping[sys.osc2.jcn] == (sys.osc2.jcn ~ sys.osc1.delx)
227227
@test length(mapping) == 8

0 commit comments

Comments
 (0)