@@ -100,11 +100,14 @@ end
100100@testitem " LinModel sim methods" setup= [SetupMPCtests] begin
101101 using . SetupMPCtests, ControlSystemsBase
102102 linmodel1 = setop! (LinModel (Gss), uop= [10 ,50 ], yop= [50 ,30 ])
103- @test updatestate! (linmodel1, [10 , 50 ]) ≈ zeros (2 )
104- @test updatestate! (linmodel1, [10 , 50 ], Float64[]) ≈ zeros (2 )
103+ u, d = [10 , 50 ], Float64[]
104+ @test updatestate! (linmodel1, u) ≈ zeros (2 )
105+ @test updatestate! (linmodel1, u, d) ≈ zeros (2 )
106+ @test @allocations (updatestate! (linmodel1, u)) == 0
105107 @test linmodel1. x0 ≈ zeros (2 )
106108 @test evaloutput (linmodel1) ≈ linmodel1 () ≈ [50 ,30 ]
107109 @test evaloutput (linmodel1, Float64[]) ≈ linmodel1 (Float64[]) ≈ [50 ,30 ]
110+ @test @allocations (evaloutput (linmodel1)) == 0
108111 x = initstate! (linmodel1, [10 , 60 ])
109112 @test evaloutput (linmodel1) ≈ [50 + 19.0 , 30 + 7.4 ]
110113 @test preparestate! (linmodel1) ≈ x # new method
@@ -265,15 +268,21 @@ end
265268@testitem " NonLinModel sim methods" setup= [SetupMPCtests] begin
266269 using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
267270 linmodel1 = LinModel (sys,Ts,i_u= [1 ,2 ])
268- f1 (x,u,_,model) = model. A* x + model. Bu* u
269- h1 (x,_,model) = model. C* x
271+ function f1 (xnext, x, u, _ , model)
272+ mul! (xnext, model. A, x)
273+ mul! (xnext, model. Bu, u, 1 , 1 )
274+ end
275+ h1 (y, x , _ , model) = mul! (y, model. C, x)
270276 nonlinmodel = NonLinModel (f1,h1,Ts,2 ,2 ,2 ,p= linmodel1,solver= nothing )
271277
272- @test updatestate! (nonlinmodel, zeros (2 ,)) ≈ zeros (2 )
273- @test updatestate! (nonlinmodel, zeros (2 ,), Float64[]) ≈ zeros (2 )
278+ u, d = zeros (2 ), Float64[]
279+ @test updatestate! (nonlinmodel, u) ≈ zeros (2 )
280+ @test updatestate! (nonlinmodel, u, d) ≈ zeros (2 )
281+ @test @allocations (updatestate! (nonlinmodel, u)) == 0
274282 @test nonlinmodel. x0 ≈ zeros (2 )
275283 @test evaloutput (nonlinmodel) ≈ nonlinmodel () ≈ zeros (2 )
276- @test evaloutput (nonlinmodel, Float64[]) ≈ nonlinmodel (Float64[]) ≈ zeros (2 )
284+ @test evaloutput (nonlinmodel, d) ≈ nonlinmodel (Float64[]) ≈ zeros (2 )
285+ @test @allocations (evaloutput (nonlinmodel)) == 0
277286
278287 x = initstate! (nonlinmodel, [0 , 10 ]) # do nothing for NonLinModel
279288 @test evaloutput (nonlinmodel) ≈ [0 , 0 ]
302311 @test linmodel1. Bu ≈ linmodel1b. Bu
303312 @test linmodel1. Bd ≈ linmodel1b. Bd
304313 @test linmodel1. C ≈ linmodel1b. C
305- @test linmodel1. Dd ≈ linmodel1b. Dd
314+ @test linmodel1. Dd ≈ linmodel1b. Dd
306315
307316 nonlinmodel2 = NonLinModel (f1,h1,Ts,1 ,1 ,1 ,1 ,solver= nothing , jacobian= AutoFiniteDiff ())
308317 linmodel2 = linearize (nonlinmodel2; x, u, d)
334343 @test linmodel3. Bd ≈ Bd
335344 @test linmodel3. C ≈ C
336345 @test linmodel3. Dd ≈ Dd
337-
346+
338347 # test `linearize` at a non-equilibrium point:
339348 Ynl, Yl = let nonlinmodel3= nonlinmodel3
340349 N = 5
355364 Ynl, Yl
356365 end
357366 @test all (isapprox .(Ynl, Yl, atol= 1e-6 ))
367+
368+ function f2! (xnext, x, u, d, _)
369+ xnext .= x.* u .+ x.* d
370+ end
371+ function h2! (y, x, d, _)
372+ y .= x.* d
373+ end
374+ nonlinmodel4 = NonLinModel (f2!, h2!, Ts, 1 , 1 , 1 , 1 , solver= nothing )
375+ linmodel4 = linearize (nonlinmodel4; x, u, d)
376+ linearize! (linmodel4, nonlinmodel4)
377+ # @test @allocations(linearize!(linmodel4, nonlinmodel4)) == 0
358378end
359379
360380@testitem " NonLinModel real time simulations" setup= [SetupMPCtests] begin
0 commit comments