Skip to content

Commit 2784084

Browse files
committed
added: allow scalars in LinModel constructor with 6 args
1 parent 9e09460 commit 2784084

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

src/model/linmodel.jl

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ struct LinModel{NT<:Real} <: SimModel{NT}
1414
yop::Vector{NT}
1515
dop::Vector{NT}
1616
function LinModel{NT}(A, Bu, C, Bd, Dd, Ts) where {NT<:Real}
17-
A, Bu, C, Bd, Dd = to_mat(A), to_mat(Bu), to_mat(C), to_mat(Bd), to_mat(Dd)
18-
nu, nx, ny, nd = size(Bu,2), size(A,2), size(C,1), size(Bd,2)
17+
A, Bu, C = to_mat(A, 1, 1), to_mat(Bu, 1, 1), to_mat(C, 1, 1)
18+
nu, nx, ny = size(Bu, 2), size(A, 2), size(C, 1)
19+
Bd = to_mat(Bd, nx, Bd 0)
20+
nd = size(Bd, 2)
21+
Dd = to_mat(Dd, ny, nd)
1922
size(A) == (nx,nx) || error("A size must be $((nx,nx))")
2023
size(Bu) == (nx,nu) || error("Bu size must be $((nx,nu))")
2124
size(C) == (ny,nx) || error("C size must be $((ny,nx))")
@@ -188,31 +191,14 @@ end
188191
189192
Construct the model from the discrete state-space matrices `A, Bu, C, Bd, Dd` directly.
190193
191-
See [`LinModel(::StateSpace)`](@ref) Extended Help for the meaning of the matrices. This
194+
See [`LinModel(::StateSpace)`](@ref) Extended Help for the meaning of the matrices. The
195+
arguments `Bd` and `Dd` can be the scalar `0` is there is no measured disturbances. This
192196
syntax do not modify the state-space representation provided in argument (`minreal` is not
193-
called). Care must be taken to ensure that the model is controllable and observable. The
194-
optional parameter `NT` explicitly specifies the number type of the matrices.
197+
called). Care must be taken to ensure that the model is controllable and observable. The
198+
optional parameter `NT` explicitly set the number type of vectors (default to `Float64`).
195199
"""
196200
LinModel{NT}(A, Bu, C, Bd, Dd, Ts) where NT<:Real
197-
198-
function LinModel(
199-
A::Array{NT}, Bu::Array{NT}, C::Array{NT}, Bd::Array{NT}, Dd::Array{NT}, Ts::Real
200-
) where {NT<:Real}
201-
return LinModel{NT}(A, Bu, C, Bd, Dd, Ts)
202-
end
203-
204-
function LinModel(
205-
A::Array{<:Real},
206-
Bu::Array{<:Real},
207-
C::Array{<:Real},
208-
Bd::Array{<:Real},
209-
Dd::Array{<:Real},
210-
Ts::Real
211-
)
212-
A, Bu, C, Bd, Dd = to_mat(A), to_mat(Bu), to_mat(C), to_mat(Bd), to_mat(Dd)
213-
A, Bu, C, Bd, Dd = promote(A, Bu, C, Bd, Dd)
214-
return LinModel(A, Bu, C, Bd, Dd, Ts)
215-
end
201+
LinModel(A, Bu, C, Bd, Dd, Ts) = LinModel{Float64}(A, Bu, C, Bd, Dd, Ts)
216202

217203
@doc raw"""
218204
steadystate!(model::LinModel, u, d)

src/sim_model.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,10 @@ function validate_args(model::SimModel, d, u=nothing)
190190
end
191191

192192
"Convert vectors to single column matrices when necessary."
193-
to_mat(A::AbstractVector) = reshape(A, length(A), 1)
194-
to_mat(A::AbstractMatrix) = A
193+
to_mat(A::AbstractVector, _ ...) = reshape(A, length(A), 1)
194+
to_mat(A::AbstractMatrix, _ ...) = A
195+
to_mat(A::Real, dims...) = fill(A, dims)
196+
195197

196198
"Functor allowing callable `SimModel` object as an alias for `evaloutput`."
197199
(model::SimModel)(d=empty(model.x)) = evaloutput(model::SimModel, d)

test/test_sim_model.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ Gss2 = c2d(sys_ss[:,1:2], 0.5Ts, :zoh)
6969
linmodel8 = LinModel(Gss.A, Gss.B, Gss.C, zeros(Float32, 2, 0), zeros(Float32, 2, 0), Ts)
7070
@test isa(linmodel8, LinModel{Float64})
7171

72-
linmodel9 = LinModel{Float32}(Gss.A, Gss.B, Gss.C, zeros(2, 0), zeros(2, 0), Ts)
73-
@test isa(linmodel9, LinModel{Float32})
72+
linmodel10 = LinModel(Gss.A, Gss.B, Gss.C, 0, 0.0, Ts)
73+
@test isa(linmodel10, LinModel{Float64})
74+
@test linmodel10.nd == 0
75+
76+
linmodel11 = LinModel{Float32}(Gss.A, Gss.B, Gss.C, zeros(2, 0), zeros(2, 0), Ts)
77+
@test isa(linmodel11, LinModel{Float32})
78+
7479

7580
@test_throws ErrorException LinModel(sys)
7681
@test_throws ErrorException LinModel(sys,-Ts)

0 commit comments

Comments
 (0)