Skip to content

Commit 7b8b88e

Browse files
committed
test: added no-allocations tests for StateEstimator
1 parent f8679be commit 7b8b88e

File tree

2 files changed

+75
-42
lines changed

2 files changed

+75
-42
lines changed

test/1_test_sim_model.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ end
158158
using DifferentiationInterface
159159
import FiniteDiff
160160
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
161-
f1(x,u,_,model) = model.A*x + model.Bu*u
162-
h1(x,_,model) = model.C*x
163-
nonlinmodel1 = NonLinModel(f1,h1,Ts,2,2,2,solver=nothing,p=linmodel1)
161+
f1!(x,u,_,model) = model.A*x + model.Bu*u
162+
h1!(x,_,model) = model.C*x
163+
nonlinmodel1 = NonLinModel(f1!,h1!,Ts,2,2,2,solver=nothing,p=linmodel1)
164164
@test nonlinmodel1.nx == 2
165165
@test nonlinmodel1.nu == 2
166166
@test nonlinmodel1.nd == 0
@@ -268,12 +268,12 @@ end
268268
@testitem "NonLinModel sim methods" setup=[SetupMPCtests] begin
269269
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
270270
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
271-
function f1(xnext, x, u, _ , model)
271+
function f1!(xnext, x, u, _ , model)
272272
mul!(xnext, model.A, x)
273273
mul!(xnext, model.Bu, u, 1, 1)
274274
end
275-
h1(y, x , _ , model) = mul!(y, model.C, x)
276-
nonlinmodel = NonLinModel(f1,h1,Ts,2,2,2,p=linmodel1,solver=nothing)
275+
h1!(y, x , _ , model) = mul!(y, model.C, x)
276+
nonlinmodel = NonLinModel(f1!,h1!,Ts,2,2,2,p=linmodel1,solver=nothing)
277277

278278
u, d = zeros(2), Float64[]
279279
@test updatestate!(nonlinmodel, u) zeros(2)
@@ -296,9 +296,9 @@ end
296296
using DifferentiationInterface
297297
import ForwardDiff, FiniteDiff
298298
Ts = 1.0
299-
f1(x,u,d,_) = x.^5 .+ u.^4 .+ d.^3
300-
h1(x,d,_) = x.^2 .+ d
301-
nonlinmodel1 = NonLinModel(f1,h1,Ts,1,1,1,1,solver=nothing)
299+
f1!(x,u,d,_) = x.^5 .+ u.^4 .+ d.^3
300+
h1!(x,d,_) = x.^2 .+ d
301+
nonlinmodel1 = NonLinModel(f1!,h1!,Ts,1,1,1,1,solver=nothing)
302302
x, u, d = [2.0], [3.0], [4.0]
303303
linmodel1 = linearize(nonlinmodel1; x, u, d)
304304
@test linmodel1.A 5*x.^4
@@ -313,7 +313,7 @@ end
313313
@test linmodel1.C linmodel1b.C
314314
@test linmodel1.Dd linmodel1b.Dd
315315

316-
nonlinmodel2 = NonLinModel(f1,h1,Ts,1,1,1,1,solver=nothing, jacobian=AutoFiniteDiff())
316+
nonlinmodel2 = NonLinModel(f1!,h1!,Ts,1,1,1,1,solver=nothing, jacobian=AutoFiniteDiff())
317317
linmodel2 = linearize(nonlinmodel2; x, u, d)
318318
@test linmodel2.A 5*x.^4 atol=1e-3
319319
@test linmodel2.Bu 4*u.^3 atol=1e-3
@@ -357,14 +357,15 @@ end
357357
yl = linmodel3(d)
358358
Ynl[i] = ynl[1]
359359
Yl[i] = yl[1]
360-
linmodel3 = linearize(nonlinmodel3; u, d)
360+
linearize!(linmodel3, nonlinmodel3; u, d)
361361
updatestate!(nonlinmodel3, u, d)
362362
updatestate!(linmodel3, u, d)
363363
end
364364
Ynl, Yl
365365
end
366366
@test all(isapprox.(Ynl, Yl, atol=1e-6))
367367

368+
# test nd==0 also works with AutoFiniteDiff (does not support empty matrices):
368369
f2!(xnext, x, u, _, _) = (xnext .= x .+ u)
369370
h2!(y, x, _, _) = (y .= x)
370371
nonlinmodel4 = NonLinModel(f2!,h2!,Ts,1,1,1,0,solver=nothing,jacobian=AutoFiniteDiff())
@@ -378,7 +379,7 @@ end
378379
end
379380
nonlinmodel4 = NonLinModel(f3!, h3!, Ts, 1, 1, 1, 1, solver=nothing)
380381
linmodel4 = linearize(nonlinmodel4; x, u, d)
381-
# see this bug : https://github.com/JuliaLang/julia/issues/51112
382+
# return nothing (see this issue : https://github.com/JuliaLang/julia/issues/51112):
382383
linearize2!(linmodel, model) = (linearize!(linmodel, model); nothing)
383384
linearize2!(linmodel4, nonlinmodel4)
384385
@test @allocations(linearize2!(linmodel4, nonlinmodel4)) == 0

test/2_test_state_estim.jl

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,14 @@ end
6565
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
6666
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
6767
skalmanfilter1 = SteadyKalmanFilter(linmodel1, nint_ym=[1, 1])
68-
preparestate!(skalmanfilter1, [50, 30])
69-
@test updatestate!(skalmanfilter1, [10, 50], [50, 30]) zeros(4)
70-
preparestate!(skalmanfilter1, [50, 30])
71-
@test updatestate!(skalmanfilter1, [10, 50], [50, 30], Float64[]) zeros(4)
68+
u, y, d = [10, 50], [50, 30], Float64[]
69+
preparestate!(skalmanfilter1, y)
70+
@test updatestate!(skalmanfilter1, u, y) zeros(4)
71+
preparestate!(skalmanfilter1, y)
72+
@test updatestate!(skalmanfilter1, u, y, d) zeros(4)
7273
@test skalmanfilter1.x̂0 zeros(4)
74+
@test @allocations(preparestate!(skalmanfilter1, y)) == 0
75+
@test @allocations(updatestate!(skalmanfilter1, u, y)) == 0
7376
preparestate!(skalmanfilter1, [50, 30])
7477
@test evaloutput(skalmanfilter1) skalmanfilter1() [50, 30]
7578
@test evaloutput(skalmanfilter1, Float64[]) skalmanfilter1(Float64[]) [50, 30]
@@ -194,11 +197,14 @@ end
194197
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
195198
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
196199
kalmanfilter1 = KalmanFilter(linmodel1)
197-
preparestate!(kalmanfilter1, [50, 30])
198-
@test updatestate!(kalmanfilter1, [10, 50], [50, 30]) zeros(4)
199-
preparestate!(kalmanfilter1, [50, 30])
200-
@test updatestate!(kalmanfilter1, [10, 50], [50, 30], Float64[]) zeros(4)
200+
u, y, d = [10, 50], [50, 30], Float64[]
201+
preparestate!(kalmanfilter1, y)
202+
@test updatestate!(kalmanfilter1, u, y) zeros(4)
203+
preparestate!(kalmanfilter1, y)
204+
@test updatestate!(kalmanfilter1, u, y, d) zeros(4)
201205
@test kalmanfilter1.x̂0 zeros(4)
206+
@test @allocations(preparestate!(kalmanfilter1, y)) == 0
207+
@test @allocations(updatestate!(kalmanfilter1, u, y)) == 0
202208
preparestate!(kalmanfilter1, [50, 30])
203209
@test evaloutput(kalmanfilter1) kalmanfilter1() [50, 30]
204210
@test evaloutput(kalmanfilter1, Float64[]) kalmanfilter1(Float64[]) [50, 30]
@@ -311,11 +317,14 @@ end
311317
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
312318
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
313319
lo1 = Luenberger(linmodel1, nint_ym=[1, 1])
314-
preparestate!(lo1, [50, 30])
315-
@test updatestate!(lo1, [10, 50], [50, 30]) zeros(4)
316-
preparestate!(lo1, [50, 30])
317-
@test updatestate!(lo1, [10, 50], [50, 30], Float64[]) zeros(4)
320+
u, y, d = [10, 50], [50, 30], Float64[]
321+
preparestate!(lo1, y)
322+
@test updatestate!(lo1, u, y) zeros(4)
323+
preparestate!(lo1, y)
324+
@test updatestate!(lo1, u, y, d) zeros(4)
318325
@test lo1.x̂0 zeros(4)
326+
@test @allocations(preparestate!(lo1, y)) == 0
327+
@test @allocations(updatestate!(lo1, u, y)) == 0
319328
preparestate!(lo1, [50, 30])
320329
@test evaloutput(lo1) lo1() [50, 30]
321330
@test evaloutput(lo1, Float64[]) lo1(Float64[]) [50, 30]
@@ -436,12 +445,15 @@ end
436445
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
437446
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]) , uop=[10,50], yop=[50,30])
438447
internalmodel1 = InternalModel(linmodel1)
439-
preparestate!(internalmodel1, [50, 30] .+ 1)
440-
@test updatestate!(internalmodel1, [10, 50], [50, 30] .+ 1) zeros(2)
441-
preparestate!(internalmodel1, [50, 30] .+ 1)
442-
@test updatestate!(internalmodel1, [10, 50], [50, 30] .+ 1, Float64[]) zeros(2)
448+
u, y, d = [10, 50], [50, 30] .+ 1, Float64[]
449+
preparestate!(internalmodel1, y)
450+
@test updatestate!(internalmodel1, u, y) zeros(2)
451+
preparestate!(internalmodel1, y)
452+
@test updatestate!(internalmodel1, u, y, d) zeros(2)
443453
@test internalmodel1.x̂d internalmodel1.x̂0 zeros(2)
444454
@test internalmodel1.x̂s ones(2)
455+
@test @allocations(preparestate!(internalmodel1, y)) == 0
456+
@test @allocations(updatestate!(internalmodel1, u, y)) == 0
445457
preparestate!(internalmodel1, [51, 31])
446458
@test evaloutput(internalmodel1, Float64[]) [51,31]
447459
@test initstate!(internalmodel1, [10, 50], [50, 30]) zeros(2)
@@ -557,16 +569,26 @@ end
557569
@testitem "UnscentedKalmanFilter estimator methods" setup=[SetupMPCtests] begin
558570
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
559571
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
560-
f(x,u,_,model) = model.A*x + model.Bu*u
561-
h(x,_,model) = model.C*x
562-
nonlinmodel = NonLinModel(f, h, Ts, 2, 2, 2, solver=nothing, p=linmodel1)
572+
function f!(xnext, x,u,_,model)
573+
mul!(xnext, model.A, x)
574+
mul!(xnext, model.Bu, u, 1, 1)
575+
return nothing
576+
end
577+
function h!(y, x,_,model)
578+
mul!(y, model.C, x)
579+
return nothing
580+
end
581+
nonlinmodel = NonLinModel(f!, h!, Ts, 2, 2, 2, solver=nothing, p=linmodel1)
563582
nonlinmodel = setop!(nonlinmodel, uop=[10,50], yop=[50,30])
564583
ukf1 = UnscentedKalmanFilter(nonlinmodel)
565-
preparestate!(ukf1, [50, 30])
566-
@test updatestate!(ukf1, [10, 50], [50, 30]) zeros(4) atol=1e-9
567-
preparestate!(ukf1, [50, 30])
568-
@test updatestate!(ukf1, [10, 50], [50, 30], Float64[]) zeros(4) atol=1e-9
584+
u, y, d = [10, 50], [50, 30], Float64[]
585+
preparestate!(ukf1, y)
586+
@test updatestate!(ukf1, u, y) zeros(4) atol=1e-9
587+
preparestate!(ukf1, y)
588+
@test updatestate!(ukf1, u, y, d) zeros(4) atol=1e-9
569589
@test ukf1.x̂0 zeros(4) atol=1e-9
590+
@test @allocations(preparestate!(ukf1, y)) == 0
591+
@test @allocations(updatestate!(ukf1, u, y)) == 0
570592
preparestate!(ukf1, [50, 30])
571593
@test evaloutput(ukf1) ukf1() [50, 30]
572594
@test evaloutput(ukf1, Float64[]) ukf1(Float64[]) [50, 30]
@@ -705,16 +727,26 @@ end
705727
using DifferentiationInterface
706728
import FiniteDiff
707729
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
708-
f(x,u,_,model) = model.A*x + model.Bu*u
709-
h(x,_,model) = model.C*x
710-
nonlinmodel = NonLinModel(f, h, Ts, 2, 2, 2, solver=nothing, p=linmodel1)
730+
function f!(xnext, x,u,_,model)
731+
mul!(xnext, model.A, x)
732+
mul!(xnext, model.Bu, u, 1, 1)
733+
return nothing
734+
end
735+
function h!(y, x,_,model)
736+
mul!(y, model.C, x)
737+
return nothing
738+
end
739+
nonlinmodel = NonLinModel(f!, h!, Ts, 2, 2, 2, solver=nothing, p=linmodel1)
711740
nonlinmodel = setop!(nonlinmodel, uop=[10,50], yop=[50,30])
712741
ekf1 = ExtendedKalmanFilter(nonlinmodel)
713-
preparestate!(ekf1, [50, 30])
714-
@test updatestate!(ekf1, [10, 50], [50, 30]) zeros(4) atol=1e-9
715-
preparestate!(ekf1, [50, 30])
716-
@test updatestate!(ekf1, [10, 50], [50, 30], Float64[]) zeros(4) atol=1e-9
742+
u, y, d = [10, 50], [50, 30], Float64[]
743+
preparestate!(ekf1, y)
744+
@test updatestate!(ekf1, u, y) zeros(4) atol=1e-9
745+
preparestate!(ekf1, y)
746+
@test updatestate!(ekf1, u, y, d) zeros(4) atol=1e-9
717747
@test ekf1.x̂0 zeros(4) atol=1e-9
748+
@test @allocations(preparestate!(ekf1, y)) == 0
749+
@test @allocations(updatestate!(ekf1, u, y)) == 0
718750
preparestate!(ekf1, [50, 30])
719751
@test evaloutput(ekf1) ekf1() [50, 30]
720752
@test evaloutput(ekf1, Float64[]) ekf1(Float64[]) [50, 30]

0 commit comments

Comments
 (0)