Skip to content

Commit 9f1612e

Browse files
committed
debug: error in I/O dimensions of the buffer
1 parent 48c201f commit 9f1612e

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/model/linmodel.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct LinModel{NT<:Real} <: SimModel{NT}
4545
dname = ["\$d_{$i}\$" for i in 1:nd]
4646
xname = ["\$x_{$i}\$" for i in 1:nx]
4747
x0 = zeros(NT, nx)
48-
buffer = SimModelBuffer{NT}(nx, nu, ny, nd)
48+
buffer = SimModelBuffer{NT}(nu, nx, ny, nd)
4949
return new{NT}(
5050
A, Bu, C, Bd, Dd,
5151
x0,
@@ -246,6 +246,7 @@ disturbances ``\mathbf{d_0 = d - d_{op}}``. The Moore-Penrose pseudo-inverse com
246246
function steadystate!(model::LinModel, u0, d0)
247247
M = I - model.A
248248
rtol = sqrt(eps(real(float(oneunit(eltype(M)))))) # pinv docstring recommendation
249+
#TODO: use model.buffer.x to reduce allocations
249250
model.x0 .= pinv(M; rtol)*(model.Bu*u0 + model.Bd*d0 + model.fop - model.xop)
250251
return nothing
251252
end

src/model/nonlinmodel.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct NonLinModel{NT<:Real, F<:Function, H<:Function, DS<:DiffSolver} <: SimMod
3232
dname = ["\$d_{$i}\$" for i in 1:nd]
3333
xname = ["\$x_{$i}\$" for i in 1:nx]
3434
x0 = zeros(NT, nx)
35-
buffer = SimModelBuffer{NT}(nx, nu, ny, nd)
35+
buffer = SimModelBuffer{NT}(nu, nx, ny, nd)
3636
return new{NT, F, H, DS}(
3737
x0,
3838
f!, h!,

src/sim_model.jl

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ end
3232
SimModelBuffer(nu, nx, ny, nd) -> SimModelBuffer{NT}
3333
3434
Create a buffer for `SimModel` objects for inputs, states, outputs, and disturbances.
35+
36+
The buffer is used to store intermediate results during simulation without allocating.
3537
"""
3638
function SimModelBuffer{NT}(nu, nx, ny, nd) where NT <: Real
3739
u = Vector{NT}(undef, nu)
@@ -207,9 +209,12 @@ true
207209
"""
208210
function initstate!(model::SimModel, u, d=model.buffer.empty)
209211
validate_args(model::SimModel, d, u)
210-
u0, d0 = u - model.uop, d - model.dop
212+
u0, d0 = model.buffer.u, model.buffer.d
213+
u0 .= u .- model.uop
214+
d0 .= d .- model.dop
211215
steadystate!(model, u0, d0)
212-
x = model.x0 + model.xop
216+
x = model.buffer.x
217+
x .= model.x0 .+ model.xop
213218
return x
214219
end
215220

@@ -229,13 +234,15 @@ julia> x = updatestate!(model, [1])
229234
"""
230235
function updatestate!(model::SimModel{NT}, u, d=model.buffer.empty) where NT <: Real
231236
validate_args(model::SimModel, d, u)
232-
xnext0 = Vector{NT}(undef, model.nx)
233-
u0, d0 = u - model.uop, d - model.dop
237+
u0, d0 = model.buffer.u, model.buffer.d
238+
u0 .= u .- model.uop
239+
d0 .= d .- model.dop
240+
xnext0 = model.buffer.x
234241
f!(xnext0, model, model.x0, u0, d0)
235242
xnext0 .+= model.fop .- model.xop
236243
model.x0 .= xnext0
237-
xnext = xnext0
238-
xnext .+= model.xop
244+
xnext = model.buffer.x
245+
xnext .= xnext0 .+ model.xop
239246
return xnext
240247
end
241248

@@ -257,11 +264,12 @@ julia> y = evaloutput(model)
257264
"""
258265
function evaloutput(model::SimModel{NT}, d=model.buffer.empty) where NT <: Real
259266
validate_args(model, d)
260-
y0 = Vector{NT}(undef, model.ny)
261-
d0 = d - model.dop
267+
d0 = model.buffer.d
268+
d0 .= d .- model.dop
269+
y0 = model.buffer.y
262270
h!(y0, model, model.x0, d0)
263-
y = y0
264-
y .+= model.yop
271+
y = model.buffer.y
272+
y .= y0 .+ model.yop
265273
return y
266274
end
267275

0 commit comments

Comments
 (0)