@@ -174,22 +174,15 @@ function linearize!(
174174 linmodel:: LinModel{NT} , model:: SimModel ; x= model. x0+ model. xop, u= model. uop, d= model. dop
175175) where NT<: Real
176176 nonlinmodel = model
177- buffer, linbuffer = nonlinmodel. buffer, nonlinmodel . linbuffer
177+ buffer = nonlinmodel. buffer
178178 # --- remove the operating points of the nonlinear model (typically zeros) ---
179179 x0, u0, d0 = buffer. x, buffer. u, buffer. d
180+ x0 .= x .- nonlinmodel. xop
180181 u0 .= u .- nonlinmodel. uop
181182 d0 .= d .- nonlinmodel. dop
182- x0 .= x .- nonlinmodel. xop
183183 # --- compute the Jacobians at linearization points ---
184184 xnext0:: Vector{NT} , y0:: Vector{NT} = linmodel. buffer. x, linmodel. buffer. y
185- linbuffer. x .= x0
186- linbuffer. u .= u0
187- linbuffer. d .= d0
188- jacobian! (linmodel. A, linbuffer. buffer_f_at_u_d, xnext0, x0)
189- jacobian! (linmodel. Bu, linbuffer. buffer_f_at_x_d, xnext0, u0)
190- jacobian! (linmodel. Bd, linbuffer. buffer_f_at_x_u, xnext0, d0)
191- jacobian! (linmodel. C, linbuffer. buffer_h_at_d, y0, x0)
192- jacobian! (linmodel. Dd, linbuffer. buffer_h_at_x, y0, d0)
185+ get_jacobians! (linmodel, xnext0, y0, nonlinmodel, x0, u0, d0)
193186 # --- compute the nonlinear model output at operating points ---
194187 xnext0, y0 = linmodel. buffer. x, linmodel. buffer. y
195188 h! (y0, nonlinmodel, x0, d0, model. p)
@@ -208,4 +201,28 @@ function linearize!(
208201 # --- reset the state of the linear model ---
209202 linmodel. x0 .= 0 # state deviation vector is always x0=0 after a linearization
210203 return linmodel
204+ end
205+
206+ " Compute the 5 Jacobians of `model` at the linearization point and write them in `linmodel`."
207+ function get_jacobians! (linmodel:: LinModel , xnext0, y0, model:: SimModel , x0, u0, d0)
208+ linbuffer = model. linbuffer # a LinearizationBuffer object
209+ linbuffer. x .= x0
210+ linbuffer. u .= u0
211+ linbuffer. d .= d0
212+ jacobian! (linmodel. A, linbuffer. buffer_f_at_u_d, xnext0, x0)
213+ jacobian! (linmodel. Bu, linbuffer. buffer_f_at_x_d, xnext0, u0)
214+ jacobian! (linmodel. Bd, linbuffer. buffer_f_at_x_u, xnext0, d0)
215+ jacobian! (linmodel. C, linbuffer. buffer_h_at_d, y0, x0)
216+ jacobian! (linmodel. Dd, linbuffer. buffer_h_at_x, y0, d0)
217+ return nothing
218+ end
219+
220+ " Copy the state-space matrices of `model` to `linmodel` if `model` is already linear."
221+ function get_jacobians! (linmodel:: LinModel , _ , _ , model:: LinModel , _ , _ , _)
222+ linmodel. A .= model. A
223+ linmodel. Bu .= model. Bu
224+ linmodel. C .= model. C
225+ linmodel. Bd .= model. Bd
226+ linmodel. Dd .= model. Dd
227+ return nothing
211228end
0 commit comments