Skip to content

Commit 648956d

Browse files
test: fix inversemodel testset
1 parent f0f1da9 commit 648956d

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

test/downstream/inversemodel.jl

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,10 @@ begin
6868
Ftf = tf(1, [(100), 1])^3
6969
Fss = ss(Ftf)
7070

71-
"Compute initial state that yields y0 as output"
72-
function init_filter(y0)
73-
(; A, B, C, D) = Fss
74-
Fx0 = -A \ B * y0
75-
@assert C * Fx0[y0] "C*Fx0*y0 ≈ y0 failed, got $(C*Fx0*y0)$(y0)]"
76-
Fx0
77-
end
78-
7971
# Create an MTK-compatible constructor
8072
function RefFilter(; name)
8173
sys = ODESystem(Fss; name)
82-
delete!(defaults(sys), @nonamespace(sys.x)[1])
74+
empty!(ModelingToolkit.get_defaults(sys))
8375
return sys
8476
end
8577
end
@@ -140,7 +132,7 @@ op = Dict(D(cm.inverse_tank.xT) => 1,
140132
cm.tank.xc => 0.65)
141133
tspan = (0.0, 1000.0)
142134
# https://github.com/SciML/ModelingToolkit.jl/issues/2786
143-
prob = ODEProblem(ssys, op, tspan; build_initializeprob = false)
135+
prob = ODEProblem(ssys, op, tspan)
144136
sol = solve(prob, Rodas5P())
145137

146138
@test SciMLBase.successful_retcode(sol)
@@ -150,27 +142,29 @@ sol = solve(prob, Rodas5P())
150142

151143
@test sol(tspan[2], idxs = cm.tank.xc)getp(prob, cm.ref.k)(prob) atol=1e-2 # Test that the inverse model led to the correct reference
152144

153-
Sf, simplified_sys = Blocks.get_sensitivity_function(model, :y) # This should work without providing an operating opint containing a dummy derivative
154-
x, _ = ModelingToolkit.get_u0_p(simplified_sys, op)
155-
p = ModelingToolkit.MTKParameters(simplified_sys, op)
145+
# we need to provide `op` so the initialization system knows what to hold constant
146+
# the values don't matter
147+
Sf, simplified_sys = Blocks.get_sensitivity_function(model, :y; op); # This should work without providing an operating opint containing a dummy derivative
148+
x = state_values(Sf)
149+
p = parameter_values(Sf)
156150
# If this somehow passes, mention it on
157151
# https://github.com/SciML/ModelingToolkit.jl/issues/2786
158152
matrices1 = Sf(x, p, 0)
159-
matrices2, _ = Blocks.get_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API
160-
@test_broken matrices1.f_x matrices2.A[1:7, 1:7]
153+
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]
161155
nsys = get_named_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API
162156
@test matrices2.A nsys.A
163157

164158
# Test the same thing for comp sensitivities
165159

166-
Sf, simplified_sys = Blocks.get_comp_sensitivity_function(model, :y) # This should work without providing an operating opint containing a dummy derivative
167-
x, _ = ModelingToolkit.get_u0_p(simplified_sys, op)
168-
p = ModelingToolkit.MTKParameters(simplified_sys, op)
160+
Sf, simplified_sys = Blocks.get_comp_sensitivity_function(model, :y; op); # This should work without providing an operating opint containing a dummy derivative
161+
x = state_values(Sf)
162+
p = parameter_values(Sf)
169163
# If this somehow passes, mention it on
170164
# https://github.com/SciML/ModelingToolkit.jl/issues/2786
171165
matrices1 = Sf(x, p, 0)
172166
matrices2, _ = Blocks.get_comp_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API
173-
@test_broken matrices1.f_x matrices2.A[1:7, 1:7]
167+
@test matrices1.f_x matrices2.A[1:7, 1:7]
174168
nsys = get_named_comp_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API
175169
@test matrices2.A nsys.A
176170

@@ -186,7 +180,9 @@ nsys = get_named_comp_sensitivity(model, :y; op) # Test that we get the same res
186180
)
187181

188182
output = :y
189-
lin_fun, ssys = Blocks.get_sensitivity_function(model, output)
183+
# we need to provide `op` so the initialization system knows which
184+
# values to hold constant
185+
lin_fun, ssys = Blocks.get_sensitivity_function(model, output; op = op1)
190186
matrices1 = linearize(ssys, lin_fun, op = op1)
191187
matrices2 = linearize(ssys, lin_fun, op = op2)
192188
S1f = ss(matrices1...)

0 commit comments

Comments
 (0)