@@ -20,12 +20,73 @@ julia> y = model()
2020"""
2121abstract type SimModel{NT<: Real } end
2222
23- struct SimModelBuffer{NT<: Real }
23+ struct JacobianBuffer{
24+ NT<: Real ,
25+ FX<: Function ,
26+ FU<: Function ,
27+ FD<: Function ,
28+ HX<: Function ,
29+ HU<: Function ,
30+ CFX<: ForwardDiff.JacobianConfig ,
31+ CFU<: ForwardDiff.JacobianConfig ,
32+ CFD<: ForwardDiff.JacobianConfig ,
33+ CHU<: ForwardDiff.JacobianConfig ,
34+ CHX<: ForwardDiff.JacobianConfig
35+ }
36+ xnext:: Vector{NT}
37+ x:: Vector{NT}
38+ y:: Vector{NT}
39+ u:: Vector{NT}
40+ d:: Vector{NT}
41+ f_x!:: FX
42+ f_u!:: FU
43+ f_d!:: FD
44+ h_x!:: HX
45+ h_d!:: HU
46+ f_x_cfg:: CFX
47+ f_u_cfg:: CFU
48+ f_d_cfg:: CFD
49+ h_x_cfg:: CHX
50+ h_d_cfg:: CHU
51+ end
52+
53+ """
54+ JacobianBuffer(NT::DataType, f!::Function, h!::Function, nx, nu, ny, nd)
55+
56+ Buffer object for calling `ForwardDiff.jacobian!` on `SimModel` without any allocation.
57+ """
58+ function JacobianBuffer {NT} (
59+ f!:: Function , h!:: Function , nx:: Int , nu:: Int , ny:: Int , nd:: Int
60+ ) where NT <: Real
61+ xnext = Vector {NT} (undef, nx)
62+ x = Vector {NT} (undef, nx)
63+ y = Vector {NT} (undef, ny)
64+ u = Vector {NT} (undef, nu)
65+ d = Vector {NT} (undef, nd)
66+ f_x! (y, x) = f! (y, x, u, d)
67+ f_u! (y, u) = f! (y, x, u, d)
68+ f_d! (y, d) = f! (y, x, u, d)
69+ h_x! (y, x) = h! (y, x, d)
70+ h_d! (y, d) = h! (y, x, d)
71+ f_x_cfg = ForwardDiff. JacobianConfig (f_x!, xnext, x)
72+ f_u_cfg = ForwardDiff. JacobianConfig (f_u!, xnext, u)
73+ f_d_cfg = ForwardDiff. JacobianConfig (f_d!, xnext, d)
74+ h_x_cfg = ForwardDiff. JacobianConfig (h_x!, y, x)
75+ h_d_cfg = ForwardDiff. JacobianConfig (h_d!, y, d)
76+ return JacobianBuffer (
77+ xnext, x, y, u, d,
78+ f_x!, f_u!, f_d!, h_x!, h_d!,
79+ f_x_cfg, f_u_cfg, f_d_cfg, h_x_cfg, h_d_cfg
80+ )
81+ end
82+
83+ struct SimModelBuffer{NT<: Real , JB<: Union{JacobianBuffer, Nothing} }
2484 u:: Vector{NT}
2585 x:: Vector{NT}
2686 y:: Vector{NT}
2787 d:: Vector{NT}
2888 empty:: Vector{NT}
89+ jacobian:: JB
2990end
3091
3192@doc raw """
@@ -35,13 +96,15 @@ Create a buffer for `SimModel` objects for inputs, states, outputs, and disturba
3596
3697The buffer is used to store intermediate results during simulation without allocating.
3798"""
38- function SimModelBuffer {NT} (nu:: Int , nx:: Int , ny:: Int , nd:: Int ) where NT <: Real
99+ function SimModelBuffer {NT} (
100+ nu:: Int , nx:: Int , ny:: Int , nd:: Int , jacobian:: JB = nothing
101+ ) where {NT <: Real , JB <: Union{JacobianBuffer, Nothing} }
39102 u = Vector {NT} (undef, nu)
40103 x = Vector {NT} (undef, nx)
41104 y = Vector {NT} (undef, ny)
42105 d = Vector {NT} (undef, nd)
43106 empty = Vector {NT} (undef, 0 )
44- return SimModelBuffer {NT} (u, x, y, d, empty)
107+ return SimModelBuffer {NT, JB } (u, x, y, d, empty, jacobian )
45108end
46109
47110
0 commit comments