|
65 | 65 | using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
|
66 | 66 | linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
|
67 | 67 | 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) |
72 | 73 | @test skalmanfilter1.x̂0 ≈ zeros(4)
|
73 |
| - preparestate!(skalmanfilter1, [50, 30]) |
| 74 | + @test_skip @allocations(preparestate!(skalmanfilter1, y)) == 0 |
| 75 | + @test_skip @allocations(updatestate!(skalmanfilter1, u, y)) == 0 |
| 76 | + preparestate!(skalmanfilter1, y) |
74 | 77 | @test evaloutput(skalmanfilter1) ≈ skalmanfilter1() ≈ [50, 30]
|
75 |
| - @test evaloutput(skalmanfilter1, Float64[]) ≈ skalmanfilter1(Float64[]) ≈ [50, 30] |
| 78 | + @test evaloutput(skalmanfilter1, d) ≈ skalmanfilter1(d) ≈ [50, 30] |
| 79 | + @test_skip @allocations(evaloutput(skalmanfilter1, d)) == 0 |
76 | 80 | @test initstate!(skalmanfilter1, [10, 50], [50, 30+1]) ≈ [zeros(3); [1]]
|
77 | 81 | linmodel2 = LinModel(append(tf(1, [1, 0]), tf(2, [10, 1])), 1.0)
|
78 | 82 | skalmanfilter2 = SteadyKalmanFilter(linmodel2, nint_u=[1, 1], direct=false)
|
@@ -194,14 +198,18 @@ end
|
194 | 198 | using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
|
195 | 199 | linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
|
196 | 200 | 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) |
| 201 | + u, y, d = [10, 50], [50, 30], Float64[] |
| 202 | + preparestate!(kalmanfilter1, y) |
| 203 | + @test updatestate!(kalmanfilter1, u, y) ≈ zeros(4) |
| 204 | + preparestate!(kalmanfilter1, y) |
| 205 | + @test updatestate!(kalmanfilter1, u, y, d) ≈ zeros(4) |
201 | 206 | @test kalmanfilter1.x̂0 ≈ zeros(4)
|
202 |
| - preparestate!(kalmanfilter1, [50, 30]) |
| 207 | + @test_skip @allocations(preparestate!(kalmanfilter1, y)) == 0 |
| 208 | + @test_skip @allocations(updatestate!(kalmanfilter1, u, y)) == 0 |
| 209 | + preparestate!(kalmanfilter1, y) |
203 | 210 | @test evaloutput(kalmanfilter1) ≈ kalmanfilter1() ≈ [50, 30]
|
204 |
| - @test evaloutput(kalmanfilter1, Float64[]) ≈ kalmanfilter1(Float64[]) ≈ [50, 30] |
| 211 | + @test evaloutput(kalmanfilter1, d) ≈ kalmanfilter1(d) ≈ [50, 30] |
| 212 | + @test_skip @allocations(evaloutput(kalmanfilter1, d)) == 0 |
205 | 213 | @test initstate!(kalmanfilter1, [10, 50], [50, 30+1]) ≈ [zeros(3); [1]]
|
206 | 214 | setstate!(kalmanfilter1, [1,2,3,4])
|
207 | 215 | @test kalmanfilter1.x̂0 ≈ [1,2,3,4]
|
@@ -311,14 +319,18 @@ end
|
311 | 319 | using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
|
312 | 320 | linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
|
313 | 321 | 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) |
| 322 | + u, y, d = [10, 50], [50, 30], Float64[] |
| 323 | + preparestate!(lo1, y) |
| 324 | + @test updatestate!(lo1, u, y) ≈ zeros(4) |
| 325 | + preparestate!(lo1, y) |
| 326 | + @test updatestate!(lo1, u, y, d) ≈ zeros(4) |
318 | 327 | @test lo1.x̂0 ≈ zeros(4)
|
319 |
| - preparestate!(lo1, [50, 30]) |
| 328 | + @test_skip @allocations(preparestate!(lo1, y)) == 0 |
| 329 | + @test_skip @allocations(updatestate!(lo1, u, y)) == 0 |
| 330 | + preparestate!(lo1, y) |
320 | 331 | @test evaloutput(lo1) ≈ lo1() ≈ [50, 30]
|
321 |
| - @test evaloutput(lo1, Float64[]) ≈ lo1(Float64[]) ≈ [50, 30] |
| 332 | + @test evaloutput(lo1, d) ≈ lo1(d) ≈ [50, 30] |
| 333 | + @test_skip @allocations(evaloutput(lo1, d)) == 0 |
322 | 334 | @test initstate!(lo1, [10, 50], [50, 30+1]) ≈ [zeros(3); [1]]
|
323 | 335 | setstate!(lo1, [1,2,3,4])
|
324 | 336 | @test lo1.x̂0 ≈ [1,2,3,4]
|
@@ -436,14 +448,18 @@ end
|
436 | 448 | using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
|
437 | 449 | linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]) , uop=[10,50], yop=[50,30])
|
438 | 450 | 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) |
| 451 | + u, y, d = [10, 50], [50, 30] .+ 1, Float64[] |
| 452 | + preparestate!(internalmodel1, y) |
| 453 | + @test updatestate!(internalmodel1, u, y) ≈ zeros(2) |
| 454 | + preparestate!(internalmodel1, y) |
| 455 | + @test updatestate!(internalmodel1, u, y, d) ≈ zeros(2) |
443 | 456 | @test internalmodel1.x̂d ≈ internalmodel1.x̂0 ≈ zeros(2)
|
444 | 457 | @test internalmodel1.x̂s ≈ ones(2)
|
445 |
| - preparestate!(internalmodel1, [51, 31]) |
446 |
| - @test evaloutput(internalmodel1, Float64[]) ≈ [51,31] |
| 458 | + @test_skip @allocations(preparestate!(internalmodel1, y)) == 0 |
| 459 | + @test_skip @allocations(updatestate!(internalmodel1, u, y)) == 0 |
| 460 | + preparestate!(internalmodel1, y) |
| 461 | + @test evaloutput(internalmodel1, d) ≈ [51,31] |
| 462 | + @test_skip @allocations(evaloutput(internalmodel1, d)) == 0 |
447 | 463 | @test initstate!(internalmodel1, [10, 50], [50, 30]) ≈ zeros(2)
|
448 | 464 | @test internalmodel1.x̂s ≈ zeros(2)
|
449 | 465 | setstate!(internalmodel1, [1,2])
|
@@ -557,19 +573,30 @@ end
|
557 | 573 | @testitem "UnscentedKalmanFilter estimator methods" setup=[SetupMPCtests] begin
|
558 | 574 | using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
|
559 | 575 | 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) |
| 576 | + function f!(xnext, x,u,_,model) |
| 577 | + mul!(xnext, model.A, x) |
| 578 | + mul!(xnext, model.Bu, u, 1, 1) |
| 579 | + return nothing |
| 580 | + end |
| 581 | + function h!(y, x,_,model) |
| 582 | + mul!(y, model.C, x) |
| 583 | + return nothing |
| 584 | + end |
| 585 | + nonlinmodel = NonLinModel(f!, h!, Ts, 2, 2, 2, solver=nothing, p=linmodel1) |
563 | 586 | nonlinmodel = setop!(nonlinmodel, uop=[10,50], yop=[50,30])
|
564 | 587 | 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 |
| 588 | + u, y, d = [10, 50], [50, 30], Float64[] |
| 589 | + preparestate!(ukf1, y) |
| 590 | + @test updatestate!(ukf1, u, y) ≈ zeros(4) atol=1e-9 |
| 591 | + preparestate!(ukf1, y) |
| 592 | + @test updatestate!(ukf1, u, y, d) ≈ zeros(4) atol=1e-9 |
569 | 593 | @test ukf1.x̂0 ≈ zeros(4) atol=1e-9
|
570 |
| - preparestate!(ukf1, [50, 30]) |
| 594 | + @test_skip @allocations(preparestate!(ukf1, y)) == 0 |
| 595 | + @test_skip @allocations(updatestate!(ukf1, u, y)) == 0 |
| 596 | + preparestate!(ukf1, y) |
571 | 597 | @test evaloutput(ukf1) ≈ ukf1() ≈ [50, 30]
|
572 |
| - @test evaloutput(ukf1, Float64[]) ≈ ukf1(Float64[]) ≈ [50, 30] |
| 598 | + @test evaloutput(ukf1, d) ≈ ukf1(d) ≈ [50, 30] |
| 599 | + @test_skip @allocations(evaloutput(ukf1, d)) == 0 |
573 | 600 | @test initstate!(ukf1, [10, 50], [50, 30+1]) ≈ zeros(4) atol=1e-9
|
574 | 601 | setstate!(ukf1, [1,2,3,4])
|
575 | 602 | @test ukf1.x̂0 ≈ [1,2,3,4]
|
@@ -705,19 +732,30 @@ end
|
705 | 732 | using DifferentiationInterface
|
706 | 733 | import FiniteDiff
|
707 | 734 | 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) |
| 735 | + function f!(xnext, x,u,_,model) |
| 736 | + mul!(xnext, model.A, x) |
| 737 | + mul!(xnext, model.Bu, u, 1, 1) |
| 738 | + return nothing |
| 739 | + end |
| 740 | + function h!(y, x,_,model) |
| 741 | + mul!(y, model.C, x) |
| 742 | + return nothing |
| 743 | + end |
| 744 | + nonlinmodel = NonLinModel(f!, h!, Ts, 2, 2, 2, solver=nothing, p=linmodel1) |
711 | 745 | nonlinmodel = setop!(nonlinmodel, uop=[10,50], yop=[50,30])
|
712 | 746 | 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 |
| 747 | + u, y, d = [10, 50], [50, 30], Float64[] |
| 748 | + preparestate!(ekf1, y) |
| 749 | + @test updatestate!(ekf1, u, y) ≈ zeros(4) atol=1e-9 |
| 750 | + preparestate!(ekf1, y) |
| 751 | + @test updatestate!(ekf1, u, y, d) ≈ zeros(4) atol=1e-9 |
717 | 752 | @test ekf1.x̂0 ≈ zeros(4) atol=1e-9
|
718 |
| - preparestate!(ekf1, [50, 30]) |
| 753 | + @test_skip @allocations(preparestate!(ekf1, y)) == 0 |
| 754 | + @test_skip @allocations(updatestate!(ekf1, u, y)) == 0 |
| 755 | + preparestate!(ekf1, y) |
719 | 756 | @test evaloutput(ekf1) ≈ ekf1() ≈ [50, 30]
|
720 |
| - @test evaloutput(ekf1, Float64[]) ≈ ekf1(Float64[]) ≈ [50, 30] |
| 757 | + @test evaloutput(ekf1, d) ≈ ekf1(d) ≈ [50, 30] |
| 758 | + @test_skip @allocations(evaloutput(ekf1, d)) == 0 |
721 | 759 | @test initstate!(ekf1, [10, 50], [50, 30+1]) ≈ zeros(4);
|
722 | 760 | setstate!(ekf1, [1,2,3,4])
|
723 | 761 | @test ekf1.x̂0 ≈ [1,2,3,4]
|
|
0 commit comments