Skip to content

Commit 0eafd3d

Browse files
committed
removed validate_obsv function (move content in augment_model)
1 parent 3d61cbf commit 0eafd3d

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

src/estimator/kalman.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ struct SteadyKalmanFilter <: StateEstimator
2828
nx̂ = nx + nxs
2929
validate_kfcov(nym, nx̂, Q̂, R̂)
3030
As, _ , Cs = stoch_ym2y(model, i_ym, Asm, [], Csm, [])
31-
validate_obsv(model, As, Cs)
3231
 , B̂u, Ĉ, B̂d, D̂d = augment_model(model, As, Cs)
3332
K = try
3433
Q̂_kalman = Matrix(Q̂) # Matrix() required for Julia 1.6
@@ -207,7 +206,6 @@ struct KalmanFilter <: StateEstimator
207206
validate_kfcov(nym, nx̂, Q̂, R̂, P̂0)
208207
As, _ , Cs = stoch_ym2y(model, i_ym, Asm, [], Csm, [])
209208
 , B̂u, Ĉ, B̂d, D̂d = augment_model(model, As, Cs)
210-
validate_obsv(model, As, Cs)
211209
Ĉm, D̂dm = Ĉ[i_ym, :], D̂d[i_ym, :] # measured outputs ym only
212210
i_ym = collect(i_ym)
213211
lastu0 = zeros(nu)
@@ -344,7 +342,7 @@ struct UnscentedKalmanFilter{M<:SimModel} <: StateEstimator
344342
nx̂ = nx + nxs
345343
validate_kfcov(nym, nx̂, Q̂, R̂, P̂0)
346344
As, _ , Cs = stoch_ym2y(model, i_ym, Asm, [], Csm, [])
347-
validate_obsv(model, As, Cs)
345+
augment_model(model, As, Cs) # verify observability for LinModel
348346
nσ, γ, m̂, Ŝ = init_ukf(nx̂, α, β, κ)
349347
i_ym = collect(i_ym)
350348
lastu0 = zeros(nu)
@@ -564,7 +562,7 @@ struct ExtendedKalmanFilter{M<:SimModel} <: StateEstimator
564562
nx̂ = nx + nxs
565563
validate_kfcov(nym, nx̂, Q̂, R̂, P̂0)
566564
As, _ , Cs, _ = stoch_ym2y(model, i_ym, Asm, [], Csm, [])
567-
validate_obsv(model, As, Cs)
565+
augment_model(model, As, Cs) # verify observability for LinModel
568566
i_ym = collect(i_ym)
569567
lastu0 = zeros(nu)
570568
= [zeros(model.nx); zeros(nxs)]

src/estimator/luenberger.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ struct Luenberger <: StateEstimator
2626
nx̂ = nx + nxs
2727
As, _ , Cs = stoch_ym2y(model, i_ym, Asm, [], Csm, [])
2828
 , B̂u, Ĉ, B̂d, D̂d = augment_model(model, As, Cs)
29-
validate_obsv(model, As, Cs)
3029
K = try
3130
place(Â, Ĉ, p̂, :o)[:, i_ym]
3231
catch

src/state_estim.jl

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,23 @@ returns the augmented matrices `Â`, `B̂u`, `Ĉ`, `B̂d` and `D̂d`:
143143
\end{aligned}
144144
```
145145
"""
146-
function augment_model(model::LinModel, As, Cs)
146+
function augment_model(model::LinModel, As, Cs; verify_obsv=true)
147147
nu, nx, nd = model.nu, model.nx, model.nd
148148
nxs = size(As, 1)
149149
= [model.A zeros(nx,nxs); zeros(nxs,nx) As]
150150
B̂u = [model.Bu; zeros(nxs,nu)]
151151
= [model.C Cs]
152152
B̂d = [model.Bd; zeros(nxs,nd)]
153153
D̂d = model.Dd
154+
# observability on Ĉ instead of Ĉm, since it would always return false when nym ≠ ny:
155+
if verify_obsv && !isobservable(Â, Ĉ)
156+
error("The augmented model is unobservable. You may try to use 0 "*
157+
"integrator on model integrating outputs with nint_ym parameter.")
158+
end
154159
return Â, B̂u, Ĉ, B̂d, D̂d
155160
end
161+
"No need to augment the model if `model` is not a [`LinModel`](@ref)."
162+
augment_model(::SimModel, _ , _ ) = nothing
156163

157164
@doc raw"""
158165
default_nint(model::LinModel, i_ym)
@@ -169,7 +176,7 @@ function default_nint(model::LinModel, i_ym)
169176
nint_ym[i] = 1
170177
Asm, Csm = init_estimstoch(i_ym, nint_ym)
171178
As , _ , Cs = stoch_ym2y(model, i_ym, Asm, [], Csm, [])
172-
 , _ , Ĉ = augment_model(model, As, Cs)
179+
 , _ , Ĉ = augment_model(model, As, Cs, verify_obsv=false)
173180
# observability on Ĉ instead of Ĉm, since it would always return false when nym ≠ ny
174181
isobservable(Â, Ĉ) || (nint_ym[i] = 0)
175182
end
@@ -178,18 +185,6 @@ end
178185
"One integrator per measured outputs by default if `model` is not a [`LinModel`](@ref)."
179186
default_nint(::SimModel, i_ym) = fill(1, length(i_ym))
180187

181-
"Validate if the augmented model is observable before augmenting it."
182-
function validate_obsv(model::LinModel, As, Cs)
183-
 , _ , Ĉ = augment_model(model, As, Cs)
184-
# observability on Ĉ instead of Ĉm, since it would always return false when nym ≠ ny:
185-
if !isobservable(Â, Ĉ)
186-
error("The augmented model is unobservable. You may try to use 0 "*
187-
"integrator on model integrating outputs with nint_ym parameter.")
188-
end
189-
end
190-
"If model is not a [`LinModel`](@ref), the observability is not verified."
191-
validate_obsv(::SimModel, _ , _ ) = nothing
192-
193188
"""
194189
isobservable(A, C)
195190

test/test_state_estim.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ sys = [ tf(1.90,[18.0,1]) tf(1.90,[18.0,1]) tf(1.90,[18.0,1]);
99
@test skalmanfilter1.nyu == 0
1010
@test skalmanfilter1.nxs == 2
1111
@test skalmanfilter1.nx̂ == 4
12+
@test skalmanfilter1.nint_ym == [1, 1]
1213

1314
linmodel2 = LinModel(sys,Ts,i_d=[3])
1415
skalmanfilter2 = SteadyKalmanFilter(linmodel2, i_ym=[2])
1516
@test skalmanfilter2.nym == 1
1617
@test skalmanfilter2.nyu == 1
1718
@test skalmanfilter2.nxs == 1
1819
@test skalmanfilter2.nx̂ == 5
20+
@test skalmanfilter2.nint_ym == [1]
1921

2022
skalmanfilter3 = SteadyKalmanFilter(linmodel1, nint_ym=0)
2123
@test skalmanfilter3.nxs == 0
2224
@test skalmanfilter3.nx̂ == 2
25+
@test skalmanfilter3.nint_ym == [0, 0]
2326

2427
skalmanfilter4 = SteadyKalmanFilter(linmodel1, nint_ym=[2,2])
2528
@test skalmanfilter4.nxs == 4
@@ -29,14 +32,21 @@ sys = [ tf(1.90,[18.0,1]) tf(1.90,[18.0,1]) tf(1.90,[18.0,1]);
2932
@test skalmanfilter5. Hermitian(diagm(Float64[1, 4, 9 ,16, 25, 36]))
3033
@test skalmanfilter5. Hermitian(diagm(Float64[49, 64]))
3134

35+
linmodel3 = LinModel(append(tf(1,[1, 0]),tf(1,[10, 1]),tf(1,[-1, 1])), 0.1)
36+
skalmanfilter6 = SteadyKalmanFilter(linmodel3)
37+
@test skalmanfilter6.nxs == 2
38+
@test skalmanfilter6.nx̂ == 5
39+
@test skalmanfilter6.nint_ym == [0, 1, 1]
40+
3241
@test_throws ErrorException SteadyKalmanFilter(linmodel1, nint_ym=[1,1,1])
3342
@test_throws ErrorException SteadyKalmanFilter(linmodel1, nint_ym=[-1,0])
3443
@test_throws ErrorException SteadyKalmanFilter(linmodel1, nint_ym=0, σQ=[1])
3544
@test_throws ErrorException SteadyKalmanFilter(linmodel1, nint_ym=0, σR=[1,1,1])
45+
@test_throws ErrorException SteadyKalmanFilter(linmodel3, nint_ym=[1, 0, 0])
3646
model_unobs = LinModel([1 0;0 1.5], [1;0][:,:], [1 0], zeros(2,0), zeros(1,0),1,1,2,1,0)
3747
@test_throws ErrorException SteadyKalmanFilter(model_unobs, nint_ym=[1])
3848
end
39-
49+
4050
@testset "SteadyKalmanFilter estimator methods" begin
4151
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
4252
skalmanfilter1 = SteadyKalmanFilter(linmodel1)

0 commit comments

Comments
 (0)