Skip to content

Commit 0e7fa09

Browse files
committed
changed: DimensionMismatch except. for all kalman Filters
1 parent 4015f97 commit 0e7fa09

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

src/estimator/construct.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,16 @@ Also validate initial estimate covariance `P̂_0`, if provided.
122122
function validate_kfcov(model, i_ym, nint_u, nint_ym, Q̂, R̂, P̂_0=nothing)
123123
nym = length(i_ym)
124124
nx̂ = model.nx + sum(nint_u) + sum(nint_ym)
125-
size(Q̂) (nx̂, nx̂) && error("Q̂ size $(size(Q̂)) ≠ nx̂, nx̂ $((nx̂, nx̂))")
125+
(any(<(0), nint_u) || any(<(0), nint_ym)) && return nothing # a clearer error is thrown later
126+
size(Q̂) (nx̂, nx̂) && throw(DimensionMismatch("Q̂ size $(size(Q̂)) ≠ nx̂, nx̂ $((nx̂, nx̂))"))
126127
!ishermitian(Q̂) && error("Q̂ is not Hermitian")
127-
size(R̂) (nym, nym) && error("R̂ size $(size(R̂)) ≠ nym, nym $((nym, nym))")
128+
size(R̂) (nym, nym) && throw(DimensionMismatch("R̂ size $(size(R̂)) ≠ nym, nym $((nym, nym))"))
128129
!ishermitian(R̂) && error("R̂ is not Hermitian")
129130
if ~isnothing(P̂_0)
130-
size(P̂_0) (nx̂, nx̂) && error("P̂_0 size $(size(P̂_0)) ≠ nx̂, nx̂ $((nx̂, nx̂))")
131+
size(P̂_0) (nx̂, nx̂) && throw(DimensionMismatch("P̂_0 size $(size(P̂_0)) ≠ nx̂, nx̂ $((nx̂, nx̂))"))
131132
!ishermitian(P̂_0) && error("P̂_0 is not Hermitian")
132133
end
134+
return nothing
133135
end
134136

135137
@doc raw"""
@@ -210,9 +212,10 @@ function init_integrators(nint::IntVectorOrInt, ny, varname::String)
210212
nint = fill(0, ny)
211213
end
212214
if length(nint) ny
213-
error("nint_$(varname) length ($(length(nint))) ≠ n$(varname) ($ny)")
215+
msg = "nint_$(varname) length ($(length(nint))) ≠ n$(varname) ($ny)"
216+
throw(DimensionMismatch(msg))
214217
end
215-
any(nint .< 0) && error("nint_$(varname) values should be ≥ 0")
218+
any(nint .< 0) && throw(ArgumentError("nint_$(varname) values should be ≥ 0"))
216219
nx = sum(nint)
217220
A, C = zeros(nx, nx), zeros(ny, nx)
218221
if nx 0

test/2_test_state_estim.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
@test skalmanfilter9.cov. I(4)
5050
@test skalmanfilter9.cov. I(2)
5151

52-
@test_throws ErrorException SteadyKalmanFilter(linmodel, nint_ym=[1,1,1])
53-
@test_throws ErrorException SteadyKalmanFilter(linmodel, nint_ym=[-1,0])
54-
@test_throws ErrorException SteadyKalmanFilter(linmodel, nint_ym=0, σQ=[1])
55-
@test_throws ErrorException SteadyKalmanFilter(linmodel, nint_ym=0, σR=[1,1,1])
52+
@test_throws DimensionMismatch SteadyKalmanFilter(linmodel, nint_ym=[1,1,1])
53+
@test_throws ArgumentError SteadyKalmanFilter(linmodel, nint_ym=[-1,0])
54+
@test_throws DimensionMismatch SteadyKalmanFilter(linmodel, nint_ym=0, σQ=[1])
55+
@test_throws DimensionMismatch SteadyKalmanFilter(linmodel, nint_ym=0, σR=[1,1,1])
5656
@test_throws ErrorException SteadyKalmanFilter(linmodel3, nint_ym=[1, 0, 0])
5757
model_unobs = LinModel([1 0;0 1.5], [1; 0], [1 0], zeros(2,0), zeros(1,0), 1.0)
5858
@test_throws ErrorException SteadyKalmanFilter(model_unobs, nint_ym=[1])
@@ -188,7 +188,7 @@ end
188188
kalmanfilter8 = KalmanFilter(linmodel2)
189189
@test isa(kalmanfilter8, KalmanFilter{Float32})
190190

191-
@test_throws ErrorException KalmanFilter(linmodel, nint_ym=0, σP_0=[1])
191+
@test_throws DimensionMismatch KalmanFilter(linmodel, nint_ym=0, σP_0=[1])
192192
end
193193

194194
@testitem "KalmanFilter estimator methods" setup=[SetupMPCtests] begin
@@ -302,8 +302,8 @@ end
302302
lo6 = Luenberger(linmodel2)
303303
@test isa(lo6, Luenberger{Float32})
304304

305-
@test_throws ErrorException Luenberger(linmodel, nint_ym=[1,1,1])
306-
@test_throws ErrorException Luenberger(linmodel, nint_ym=[-1,0])
305+
@test_throws DimensionMismatch Luenberger(linmodel, nint_ym=[1,1,1])
306+
@test_throws ArgumentError Luenberger(linmodel, nint_ym=[-1,0])
307307
@test_throws ErrorException Luenberger(linmodel, poles=[0.5])
308308
@test_throws ErrorException Luenberger(linmodel, poles=fill(1.5, lo1.nx̂))
309309
@test_throws ErrorException Luenberger(LinModel(tf(1,[1, 0]),0.1), poles=[0.5,0.6])
@@ -1603,8 +1603,8 @@ end
16031603
@testitem "ManualEstimator construction" setup=[SetupMPCtests] begin
16041604
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
16051605
linmodel = LinModel(sys,Ts,i_u=[1,2])
1606-
f(x,u,d,model) = model.A*x + model.Bu*u + model.Bd*d
1607-
h(x,d,model) = model.C*x + model.Du*d
1606+
f = (x,u,d,model) -> model.A*x + model.Bu*u + model.Bd*d
1607+
h = (x,d,model) -> model.C*x + model.Du*d
16081608
nonlinmodel = NonLinModel(f, h, Ts, 2, 4, 2, 1, solver=nothing, p=linmodel)
16091609

16101610
manual1 = ManualEstimator(linmodel)
@@ -1651,8 +1651,8 @@ end
16511651
@testitem "ManualEstimator estimator methods" setup=[SetupMPCtests] begin
16521652
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
16531653
linmodel = LinModel(sys,Ts,i_u=[1,2])
1654-
f(x,u,d,model) = model.A*x + model.Bu*u + model.Bd*d
1655-
h(x,d,model) = model.C*x + model.Du*d
1654+
f = (x,u,d,model) -> model.A*x + model.Bu*u + model.Bd*d
1655+
h = (x,d,model) -> model.C*x + model.Du*d
16561656
nonlinmodel = NonLinModel(f, h, Ts, 2, 2, 2, 0, solver=nothing, p=linmodel)
16571657

16581658
manual1 = ManualEstimator(linmodel)

0 commit comments

Comments
 (0)