11"""
2- get_linearization_func(NT, f!, h!, nu, nx, ny, nd, p, backend) -> linfunc!
2+ get_linearization_func(
3+ NT, solver_f!, solver_h!, nu, nx, ny, nd, ns, p, solver, backend
4+ ) -> linfunc!
35
4- Return the `linfunc!` function that computes the Jacobians of `f !` and `h !` functions.
6+ Return `linfunc!` function that computes Jacobians of `solver_f !` and `solver_h !` functions.
57
68The function has the following signature:
79```
@@ -11,31 +13,33 @@ and it should modifies in-place all the arguments before `backend`. The `backend
1113is an `AbstractADType` object from `DifferentiationInterface`. The `cst_x`, `cst_u` and
1214`cst_d` are `DifferentiationInterface.Constant` objects with the linearization points.
1315"""
14- function get_linearization_func (NT, f !, h !, nu, nx, ny, nd, p, backend)
15- f_x! (xnext, x, u, d) = f ! (xnext, x, u, d, p)
16- f_u! (xnext, u, x, d) = f ! (xnext, x, u, d, p)
17- f_d! (xnext, d, x, u) = f ! (xnext, x, u, d, p)
18- h_x! (y, x, d) = h ! (y, x, d, p)
19- h_d! (y, d, x) = h ! (y, x, d, p)
16+ function get_linearization_func (NT, solver_f !, solver_h !, nu, nx, ny, nd, p, solver , backend)
17+ f_x! (xnext, x, xi, u, d) = solver_f ! (xnext, xi , x, u, d, p)
18+ f_u! (xnext, u, xi, x, d) = solver_f ! (xnext, xi , x, u, d, p)
19+ f_d! (xnext, d, xi, x, u) = solver_f ! (xnext, xi , x, u, d, p)
20+ h_x! (y, x, d) = solver_h ! (y, x, d, p)
21+ h_d! (y, d, x) = solver_h ! (y, x, d, p)
2022 strict = Val (true )
2123 xnext = zeros (NT, nx)
2224 y = zeros (NT, ny)
2325 x = zeros (NT, nx)
2426 u = zeros (NT, nu)
2527 d = zeros (NT, nd)
28+ xi = zeros (NT, nx* (solver. ni+ 1 ))
29+ cache_xi = Cache (xi)
2630 cst_x = Constant (x)
2731 cst_u = Constant (u)
2832 cst_d = Constant (d)
29- A_prep = prepare_jacobian (f_x!, xnext, backend, x, cst_u, cst_d; strict)
30- Bu_prep = prepare_jacobian (f_u!, xnext, backend, u, cst_x, cst_d; strict)
31- Bd_prep = prepare_jacobian (f_d!, xnext, backend, d, cst_x, cst_u; strict)
32- C_prep = prepare_jacobian (h_x!, y, backend, x, cst_d ; strict)
33- Dd_prep = prepare_jacobian (h_d!, y, backend, d, cst_x ; strict)
33+ A_prep = prepare_jacobian (f_x!, xnext, backend, x, cache_xi, cst_u, cst_d; strict)
34+ Bu_prep = prepare_jacobian (f_u!, xnext, backend, u, cache_xi, cst_x, cst_d; strict)
35+ Bd_prep = prepare_jacobian (f_d!, xnext, backend, d, cache_xi, cst_x, cst_u; strict)
36+ C_prep = prepare_jacobian (h_x!, y, backend, x, cst_d ; strict)
37+ Dd_prep = prepare_jacobian (h_d!, y, backend, d, cst_x ; strict)
3438 function linfunc! (xnext, y, A, Bu, C, Bd, Dd, backend, x, u, d, cst_x, cst_u, cst_d)
3539 # all the arguments before `backend` are mutated in this function
36- jacobian! (f_x!, xnext, A, A_prep, backend, x, cst_u, cst_d)
37- jacobian! (f_u!, xnext, Bu, Bu_prep, backend, u, cst_x, cst_d)
38- jacobian! (f_d!, xnext, Bd, Bd_prep, backend, d, cst_x, cst_u)
40+ jacobian! (f_x!, xnext, A, A_prep, backend, x, cache_xi, cst_u, cst_d)
41+ jacobian! (f_u!, xnext, Bu, Bu_prep, backend, u, cache_xi, cst_x, cst_d)
42+ jacobian! (f_d!, xnext, Bd, Bd_prep, backend, d, cache_xi, cst_x, cst_u)
3943 jacobian! (h_x!, y, C, C_prep, backend, x, cst_d)
4044 jacobian! (h_d!, y, Dd, Dd_prep, backend, d, cst_x)
4145 return nothing
@@ -154,21 +158,21 @@ function linearize!(
154158 nonlinmodel = model
155159 buffer = nonlinmodel. buffer
156160 # --- remove the operating points of the nonlinear model (typically zeros) ---
157- x0, u0, d0 = buffer. x, buffer. u, buffer. d
161+ x0, u0, d0, x0i = buffer. x, buffer. u, buffer. d, buffer . xi
158162 x0 .= x .- nonlinmodel. xop
159163 u0 .= u .- nonlinmodel. uop
160164 d0 .= d .- nonlinmodel. dop
161165 # --- compute the Jacobians at linearization points ---
162166 linearize_core! (linmodel, nonlinmodel, x0, u0, d0)
163167 # --- compute the nonlinear model output at operating points ---
164- xnext0 , y0 = linmodel. buffer. x, linmodel. buffer. y
168+ x0next , y0 = linmodel. buffer. x, linmodel. buffer. y
165169 h! (y0, nonlinmodel, x0, d0, model. p)
166170 y = y0
167171 y .= y0 .+ nonlinmodel. yop
168172 # --- compute the nonlinear model next state at operating points ---
169- f! (xnext0 , nonlinmodel, x0, u0, d0, model. p)
170- xnext = xnext0
171- xnext .= xnext0 .+ nonlinmodel. fop .- nonlinmodel. xop
173+ f! (x0next, x0i , nonlinmodel, x0, u0, d0, model. p)
174+ xnext = x0next
175+ xnext .= x0next .+ nonlinmodel. fop .- nonlinmodel. xop
172176 # --- modify the linear model operating points ---
173177 linmodel. uop .= u
174178 linmodel. yop .= y
@@ -182,13 +186,13 @@ end
182186
183187" Call `linfunc!` function to compute the Jacobians of `model` at the linearization point."
184188function linearize_core! (linmodel:: LinModel , model:: SimModel , x0, u0, d0)
185- xnext0 , y0 = linmodel. buffer. x, linmodel. buffer. y
189+ x0next , y0 = linmodel. buffer. x, linmodel. buffer. y
186190 A, Bu, C, Bd, Dd = linmodel. A, linmodel. Bu, linmodel. C, linmodel. Bd, linmodel. Dd
187191 cst_x = Constant (x0)
188192 cst_u = Constant (u0)
189193 cst_d = Constant (d0)
190194 backend = model. jacobian
191- model. linfunc! (xnext0 , y0, A, Bu, C, Bd, Dd, backend, x0, u0, d0, cst_x, cst_u, cst_d)
195+ model. linfunc! (x0next , y0, A, Bu, C, Bd, Dd, backend, x0, u0, d0, cst_x, cst_u, cst_d)
192196 return nothing
193197end
194198
0 commit comments