Skip to content

Commit 3f01145

Browse files
test: update downstream tests
1 parent 5ced95e commit 3f01145

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

test/downstream/inversemodel.jl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D
1717
connect = ModelingToolkit.connect;
1818
rc = 0.25 # Reference concentration
1919

20-
@component function MixingTank(; name, c0 = 0.8, T0 = 308.5, a1 = 0.2674, a21 = 1.815, a22 = 0.4682, b = 1.5476, k0 = 1.05e14, ϵ = 34.2894)
20+
@component function MixingTank(; name, c0 = 0.8, T0 = 308.5, a1 = 0.2674, a21 = 1.815, a22 = 0.4682, b = 1.5476, k0 = 1.05e14, ϵ = 34.2894, xc = nothing, xT = nothing)
2121
pars = @parameters begin
2222
c0 = c0, [description = "Nominal concentration"]
2323
T0 = T0, [description = "Nominal temperature"]
@@ -34,11 +34,13 @@ rc = 0.25 # Reference concentration
3434
c = RealOutput()
3535
T = RealOutput()
3636
end
37-
37+
38+
xc = @something(xc, c0)
39+
xT = @something(xT, T0)
3840
vars = @variables begin
3941
gamma(t), [description = "Reaction speed"]
40-
xc(t) = c0, [description = "Concentration"]
41-
xT(t) = T0, [description = "Temperature"]
42+
xc(t) = xc, [description = "Concentration"]
43+
xT(t) = xT, [description = "Temperature"]
4244
xT_c(t), [description = "Cooling temperature"]
4345
end
4446

@@ -73,7 +75,7 @@ begin
7375
function RefFilter(; name)
7476
sys = System(Fss; name)
7577
"Compute initial state that yields y0 as output"
76-
empty!(ModelingToolkit.get_defaults(sys))
78+
empty!(ModelingToolkit.get_initial_conditions(sys))
7779
return sys
7880
end
7981
end
@@ -132,7 +134,9 @@ cm = complete(model)
132134

133135
op = Dict(
134136
cm.filter.y.u => 0.8 * (1 - 0.42),
135-
D(cm.filter.y.u) => 0
137+
D(cm.filter.y.u) => 0,
138+
cm.noise_filter.x => nothing,
139+
cm.controller.int.x => nothing,
136140
)
137141
tspan = (0.0, 1000.0)
138142
# https://github.com/SciML/ModelingToolkit.jl/issues/2786
@@ -178,18 +182,23 @@ nsys = get_named_comp_sensitivity(model, :y; op)
178182
@testset "Issue #3319" begin
179183
op1 = Dict(
180184
cm.filter.y.u => 0.8 * (1 - 0.42),
181-
cm.tank.xc => 0.65
185+
cm.tank.xc => 0.65,
186+
cm.noise_filter.x => nothing,
182187
)
183188

184189
op2 = Dict(
185190
cm.filter.y.u => 0.8 * (1 - 0.42),
186-
cm.tank.xc => 0.45
191+
cm.tank.xc => 0.45,
192+
cm.noise_filter.x => nothing,
187193
)
188194

189195
output = :y
190196
# we need to provide `op` so the initialization system knows which
191197
# values to hold constant
192-
lin_fun, ssys = get_sensitivity_function(model, output; op = op1)
198+
#
199+
# Printing `lin_fun` in a tuple (so it doesn't hit the pretty-printing method)
200+
# will likely segfault Julia. Somehow.
201+
lin_fun, ssys = get_sensitivity_function(model, output; op = op1);
193202
matrices1, extras1 = linearize(ssys, lin_fun, op = op1)
194203
matrices2, extras2 = linearize(ssys, lin_fun, op = op2)
195204
@test extras1.x != extras2.x

test/downstream/linearization_dd.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@ using ModelingToolkitStandardLibrary.Blocks
77
using ModelingToolkitStandardLibrary.Mechanical.MultiBody2D
88
using ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition
99
using Test
10+
import Symbolics
1011
import NonlinearSolve
12+
using Setfield: @set
1113

1214
using ControlSystemsMTK
1315
using ControlSystemsMTK.ControlSystemsBase: sminreal, minreal, poles
1416
connect = ModelingToolkit.connect
1517

18+
function rm_bindings(sys)
19+
@set sys.bindings = empty(bindings(sys))
20+
end
21+
1622
@independent_variables t
1723
D = Differential(t)
1824

1925
@named link1 = Link(; m = 0.2, l = 10, I = 1, g = -9.807)
26+
link1 = rm_bindings(link1)
2027
@named cart = TranslationalPosition.Mass(; m = 1, s = 0)
2128
@named fixed = Fixed()
2229
@named force = Force(use_support = false)
@@ -64,13 +71,13 @@ lsyss,
6471

6572
dummyder = setdiff(unknowns(sysss), unknowns(model))
6673
# op2 = merge(ModelingToolkit.guesses(model), op, Dict(x => 0.0 for x in dummyder))
67-
op2 = merge(ModelingToolkit.defaults(syss), op)
74+
op2 = merge(ModelingToolkit.initial_conditions(syss), op)
6875
op2[link1.fy1] = -op2[link1.g] * op2[link1.m]
6976
op2[cart.f] = 0
7077

71-
@test substitute(lsyss.A, op2) lsys.A
78+
@test Symbolics.value.(substitute(lsyss.A, op2; fold = Val(true))) lsys.A
7279
# We cannot pivot symbolically, so the part where a linear solve is required
7380
# is not reliable.
74-
@test substitute(lsyss.B, op2)[1:6, 1] lsys.B[1:6, 1]
75-
@test substitute(lsyss.C, op2) lsys.C
76-
@test substitute(lsyss.D, op2) lsys.D
81+
@test Symbolics.value.(substitute(lsyss.B, op2; fold = Val(true)))[1:6, 1] lsys.B[1:6, 1]
82+
@test Symbolics.value.(substitute(lsyss.C, op2; fold = Val(true))) lsys.C
83+
@test Symbolics.value.(substitute(lsyss.D, op2; fold = Val(true))) lsys.D

0 commit comments

Comments
 (0)