Skip to content

Commit f292664

Browse files
test: fix inversemodel tests
Changing state realization in ControlSystemsBase made it unstable, so filter order was reduced. Co-authored-by: Fredrik Bagge Carlson <[email protected]>
1 parent 267eee9 commit f292664

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

test/downstream/inversemodel.jl

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,17 @@ rc = 0.25 # Reference concentration
2727
k0 = 1.05e14
2828
ϵ = 34.2894
2929
end
30-
3130
@variables begin
3231
gamma(t), [description = "Reaction speed"]
3332
xc(t) = c0, [description = "Concentration"]
3433
xT(t) = T0, [description = "Temperature"]
3534
xT_c(t), [description = "Cooling temperature"]
3635
end
37-
3836
@components begin
3937
T_c = RealInput()
4038
c = RealOutput()
4139
T = RealOutput()
4240
end
43-
4441
begin
4542
τ0 = 60
4643
wk0 = k0 / c0
@@ -57,20 +54,18 @@ rc = 0.25 # Reference concentration
5754
gamma ~ xc * wk0 * exp(-/ xT)
5855
D(xc) ~ -wa11 * xc - wa12 * gamma + wa13
5956
D(xT) ~ -wa21 * xT + wa22 * gamma + wa23 + wb * xT_c
60-
6157
xc ~ c.u
6258
xT ~ T.u
6359
xT_c ~ T_c.u
6460
end
6561
end
66-
6762
begin
68-
Ftf = tf(1, [(100), 1])^3
63+
Ftf = tf(1, [(100), 1])^2
6964
Fss = ss(Ftf)
70-
7165
# Create an MTK-compatible constructor
7266
function RefFilter(; name)
7367
sys = ODESystem(Fss; name)
68+
"Compute initial state that yields y0 as output"
7469
empty!(ModelingToolkit.get_defaults(sys))
7570
return sys
7671
end
@@ -82,7 +77,6 @@ end
8277
x10 = 0.42
8378
x20 = 0.01
8479
u0 = -0.0224
85-
8680
c_start = c0 * (1 - x10) # Initial concentration
8781
T_start = T0 * (1 + x20) # Initial temperature
8882
c_high_start = c0 * (1 - 0.72) # Reference concentration
@@ -104,22 +98,13 @@ end
10498
@equations begin
10599
connect(ref.output, :r, filter.input)
106100
connect(filter.output, inverse_tank.c)
107-
108101
connect(inverse_tank.T_c, ff_gain.input)
109102
connect(ff_gain.output, :uff, limiter.input)
110103
connect(limiter.output, add.input1)
111-
112104
connect(controller.ctr_output, :u, add.input2)
113-
114-
#connect(add.output, :u_tot, limiter.input)
115-
#connect(limiter.output, :v, tank.T_c)
116-
117105
connect(add.output, :u_tot, tank.T_c)
118-
119106
connect(inverse_tank.T, feedback.input1)
120-
121107
connect(tank.T, :y, noise_filter.input)
122-
123108
connect(noise_filter.output, feedback.input2)
124109
connect(feedback.output, :e, controller.err_input)
125110
end
@@ -128,8 +113,10 @@ end;
128113
ssys = structural_simplify(model)
129114
cm = complete(model)
130115

131-
op = Dict(D(cm.inverse_tank.xT) => 1,
132-
cm.tank.xc => 0.65)
116+
op = Dict(
117+
cm.filter.y.u => 0.8 * (1 - 0.42),
118+
D(cm.filter.y.u) => 0
119+
)
133120
tspan = (0.0, 1000.0)
134121
# https://github.com/SciML/ModelingToolkit.jl/issues/2786
135122
prob = ODEProblem(ssys, op, tspan)
@@ -151,37 +138,34 @@ p = parameter_values(Sf)
151138
# https://github.com/SciML/ModelingToolkit.jl/issues/2786
152139
matrices1 = Sf(x, p, 0)
153140
matrices2, _ = Blocks.get_sensitivity(model, :y; op); # Test that we get the same result when calling the higher-level API
154-
@test matrices1.f_x matrices2.A[1:7, 1:7]
141+
@test matrices1.f_x matrices2.A[1:6, 1:6]
155142
nsys = get_named_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API
156143
@test matrices2.A nsys.A
157144

158145
# Test the same thing for comp sensitivities
159146

160147
# This should work without providing an operating opint containing a dummy derivative
161-
Sf, simplified_sys = Blocks.get_comp_sensitivity_function(
162-
model, :y; op, initialization_solver_alg = NewtonRaphson());
148+
Sf, simplified_sys = Blocks.get_comp_sensitivity_function(model, :y; op);
163149
x = state_values(Sf)
164150
p = parameter_values(Sf)
165151
# If this somehow passes, mention it on
166152
# https://github.com/SciML/ModelingToolkit.jl/issues/2786
167153
matrices1 = Sf(x, p, 0)
168154
# Test that we get the same result when calling the higher-level API
169-
matrices2, _ = Blocks.get_comp_sensitivity(
170-
model, :y; op, initialization_solver_alg = NewtonRaphson())
171-
@test matrices1.f_x matrices2.A[1:7, 1:7]
155+
matrices2, _ = Blocks.get_comp_sensitivity(model, :y; op)
156+
@test matrices1.f_x matrices2.A[1:6, 1:6]
172157
# Test that we get the same result when calling an even higher-level API
173-
nsys = get_named_comp_sensitivity(
174-
model, :y; op, initialization_solver_alg = NewtonRaphson())
158+
nsys = get_named_comp_sensitivity(model, :y; op)
175159
@test matrices2.A nsys.A
176160

177161
@testset "Issue #3319" begin
178162
op1 = Dict(
179-
D(cm.inverse_tank.xT) => 1,
163+
cm.filter.y.u => 0.8 * (1 - 0.42),
180164
cm.tank.xc => 0.65
181165
)
182166

183167
op2 = Dict(
184-
D(cm.inverse_tank.xT) => 1,
168+
cm.filter.y.u => 0.8 * (1 - 0.42),
185169
cm.tank.xc => 0.45
186170
)
187171

0 commit comments

Comments
 (0)