1+ const IntRangeOrVector = Union{UnitRange{Int}, Vector{Int}}
2+
13@doc raw """
24Abstract supertype of [`LinModel`](@ref) and [`NonLinModel`](@ref) types.
35
@@ -18,7 +20,7 @@ julia> y = model()
1820"""
1921abstract type SimModel end
2022
21- struct LinModel <: SimModel
23+ struct LinModel{F <: Function , H <: Function } <: SimModel
2224 A :: Matrix{Float64}
2325 Bu :: Matrix{Float64}
2426 C :: Matrix{Float64}
@@ -35,20 +37,15 @@ struct LinModel <: SimModel
3537 uop:: Vector{Float64}
3638 yop:: Vector{Float64}
3739 dop:: Vector{Float64}
38- function LinModel (A, Bu, C, Bd, Dd, Ts, nu, nx, ny, nd)
40+ function LinModel {F, H} (
41+ A, Bu, C, Bd, Dd, f:: F , h:: H , Ts, nu, nx, ny, nd
42+ ) where {F<: Function , H<: Function }
3943 size (A) == (nx,nx) || error (" A size must be $((nx,nx)) " )
4044 size (Bu) == (nx,nu) || error (" Bu size must be $((nx,nu)) " )
4145 size (C) == (ny,nx) || error (" C size must be $((ny,nx)) " )
4246 size (Bd) == (nx,nd) || error (" Bd size must be $((nx,nd)) " )
4347 size (Dd) == (ny,nd) || error (" Dd size must be $((ny,nd)) " )
4448 Ts > 0 || error (" Sampling time Ts must be positive" )
45- # the `let` block captures and fixes A, Bu, Bd, C, Dd values (faster computations):
46- f = let A= A, Bu= Bu, Bd= Bd
47- (x,u,d) -> A* x + Bu* u + Bd* d
48- end
49- h = let C= C, Dd= Dd
50- (x,d) -> C* x + Dd* d
51- end
5249 uop = zeros (nu)
5350 yop = zeros (ny)
5451 dop = zeros (nd)
@@ -57,8 +54,12 @@ struct LinModel <: SimModel
5754 end
5855end
5956
60-
61- const IntRangeOrVector = Union{UnitRange{Int}, Vector{Int}}
57+ " Infer the type of state-space `f` and `h` functions and construct the linear model."
58+ function LinModel_ssfunc (
59+ A, Bu, C, Bd, Dd, f:: F , h:: H , Ts, nu, nx, ny, nd
60+ ) where {F<: Function , H<: Function }
61+ return LinModel {F, H} (A, Bu, C, Bd, Dd, f, h, Ts, nu, nx, ny, nd)
62+ end
6263
6364@doc raw """
6465 LinModel(sys::StateSpace[, Ts]; i_u=1:size(sys,2), i_d=Int[])
@@ -159,7 +160,14 @@ function LinModel(
159160 Bd = sys_dis. B[:,nu+ 1 : end ]
160161 C = sys_dis. C;
161162 Dd = sys_dis. D[:,nu+ 1 : end ]
162- return LinModel (A, Bu, C, Bd, Dd, Ts, nu, nx, ny, nd)
163+ # the `let` block captures and fixes A, Bu, Bd, C, Dd values (faster computations):
164+ f = let A= A, Bu= Bu, Bd= Bd
165+ (x,u,d) -> A* x + Bu* u + Bd* d
166+ end
167+ h = let C= C, Dd= Dd
168+ (x,d) -> C* x + Dd* d
169+ end
170+ return LinModel_ssfunc (A, Bu, C, Bd, Dd, f, h, Ts, nu, nx, ny, nd)
163171end
164172
165173@doc raw """
@@ -208,7 +216,6 @@ function LinModel(sys::DelayLtiSystem, Ts::Real; kwargs...)
208216 return LinModel (sys_dis, Ts; kwargs... )
209217end
210218
211-
212219@doc raw """
213220 steadystate(model::LinModel, u, d=Float64[])
214221
@@ -226,6 +233,28 @@ function steadystate(model::LinModel, u, d=Float64[])
226233 return pinv (I - model. A)* (model. Bu* (u - model. uop) + model. Bd* (d - model. dop))
227234end
228235
236+ struct NonLinModel{F<: Function , H<: Function } <: SimModel
237+ x:: Vector{Float64}
238+ f:: F
239+ h:: H
240+ Ts:: Float64
241+ nu:: Int
242+ nx:: Int
243+ ny:: Int
244+ nd:: Int
245+ uop:: Vector{Float64}
246+ yop:: Vector{Float64}
247+ dop:: Vector{Float64}
248+ function NonLinModel {F,H} (f:: F , h:: H , Ts, nu, nx, ny, nd) where {F<: Function ,H<: Function }
249+ Ts > 0 || error (" Sampling time Ts must be positive" )
250+ validate_fcts (f, h)
251+ uop = zeros (nu)
252+ yop = zeros (ny)
253+ dop = zeros (nd)
254+ x = zeros (nx)
255+ return new (x, f, h, Ts, nu, nx, ny, nd, uop, yop, dop)
256+ end
257+ end
229258
230259@doc raw """
231260 NonLinModel(f::Function, h::Function, Ts, nu, nx, ny, nd=0)
@@ -260,27 +289,10 @@ Discrete-time nonlinear model with a sample time Ts = 10.0 s and:
260289 0 measured disturbances d
261290```
262291"""
263- struct NonLinModel <: SimModel
264- x:: Vector{Float64}
265- f:: Function
266- h:: Function
267- Ts:: Float64
268- nu:: Int
269- nx:: Int
270- ny:: Int
271- nd:: Int
272- uop:: Vector{Float64}
273- yop:: Vector{Float64}
274- dop:: Vector{Float64}
275- function NonLinModel (f, h, Ts:: Real , nu:: Int , nx:: Int , ny:: Int , nd:: Int = 0 )
276- Ts > 0 || error (" Sampling time Ts must be positive" )
277- validate_fcts (f, h)
278- uop = zeros (nu)
279- yop = zeros (ny)
280- dop = zeros (nd)
281- x = zeros (nx)
282- return new (x, f, h, Ts, nu, nx, ny, nd, uop, yop, dop)
283- end
292+ function NonLinModel (
293+ f:: F , h:: H , Ts:: Real , nu:: Int , nx:: Int , ny:: Int , nd:: Int = 0
294+ ) where {F<: Function , H<: Function }
295+ return NonLinModel {F, H} (f, h, Ts, nu, nx, ny, nd)
284296end
285297
286298function validate_fcts (f, h)
0 commit comments