3232 SimModelBuffer(nu, nx, ny, nd) -> SimModelBuffer{NT}
3333
3434Create 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"""
3638function SimModelBuffer {NT} (nu, nx, ny, nd) where NT <: Real
3739 u = Vector {NT} (undef, nu)
@@ -207,9 +209,12 @@ true
207209"""
208210function 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
214219end
215220
@@ -229,13 +234,15 @@ julia> x = updatestate!(model, [1])
229234"""
230235function 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
240247end
241248
@@ -257,11 +264,12 @@ julia> y = evaloutput(model)
257264"""
258265function 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
266274end
267275
0 commit comments