Skip to content

Commit 2988f25

Browse files
committed
debug: do not call jacobian! if nd==0 in linearize
some backend works with empty matrices (e.g. `AutoForwardDiff`) others not (e.g. : `AutoFiniteDiff`)
1 parent 3705821 commit 2988f25

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

src/controller/transcription.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ function init_predmat(
302302
G = Matrix{NT}(undef, Hp*ny, nd)
303303
jx̂ = Matrix{NT}(undef, nx̂, Hp*nd)
304304
J = repeatdiag(D̂d, Hp)
305-
if nd 0
305+
if nd > 0
306306
for j=1:Hp
307307
iRow = (1:ny) .+ ny*(j-1)
308308
G[iRow,:] =*getpower(Âpow, j-1)*B̂d
@@ -853,7 +853,7 @@ function linconstraint!(mpc::PredictiveController, model::LinModel, ::Transcript
853853
fx̂ .= mpc.con.bx̂
854854
mul!(fx̂, mpc.con.kx̂, mpc.estim.x̂0, 1, 1)
855855
mul!(fx̂, mpc.con.vx̂, mpc.estim.lastu0, 1, 1)
856-
if model.nd 0
856+
if model.nd > 0
857857
mul!(fx̂, mpc.con.gx̂, mpc.d0, 1, 1)
858858
mul!(fx̂, mpc.con.jx̂, mpc.D̂0, 1, 1)
859859
end
@@ -939,7 +939,7 @@ function linconstrainteq!(mpc::PredictiveController, model::LinModel, ::Multiple
939939
Fŝ .= mpc.con.Bŝ
940940
mul!(Fŝ, mpc.con.Kŝ, mpc.estim.x̂0, 1, 1)
941941
mul!(Fŝ, mpc.con.Vŝ, mpc.estim.lastu0, 1, 1)
942-
if model.nd 0
942+
if model.nd > 0
943943
mul!(Fŝ, mpc.con.Gŝ, mpc.d0, 1, 1)
944944
mul!(Fŝ, mpc.con.Jŝ, mpc.D̂0, 1, 1)
945945
end

src/estimator/mhe/execute.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ function add_data_windows!(estim::MovingHorizonEstimator, y0m, d0, u0=estim.last
191191
if ismoving
192192
estim.Y0m[1:end-nym] .= @views estim.Y0m[nym+1:end]
193193
estim.Y0m[end-nym+1:end] .= y0m
194-
if nd 0
194+
if nd > 0
195195
estim.D0[1:end-nd] .= @views estim.D0[nd+1:end]
196196
estim.D0[end-nd+1:end] .= d0
197197
end
@@ -204,7 +204,7 @@ function add_data_windows!(estim::MovingHorizonEstimator, y0m, d0, u0=estim.last
204204
estim.Nk .= estim.He
205205
else
206206
estim.Y0m[(1 + nym*(Nk-1)):(nym*Nk)] .= y0m
207-
if nd 0
207+
if nd > 0
208208
# D0 include 1 additional measured disturbance if direct==true (p==0):
209209
estim.D0[(1 + nd*(Nk-p)):(nd*Nk+1-p)] .= d0
210210
end
@@ -252,7 +252,7 @@ function initpred!(estim::MovingHorizonEstimator, model::LinModel)
252252
# --- update F and fx̄ vectors for MHE predictions ---
253253
F .= estim.Y0m .+ estim.B
254254
mul!(F, estim.G, estim.U0, 1, 1)
255-
if model.nd 0
255+
if model.nd > 0
256256
mul!(F, estim.J, estim.D0, 1, 1)
257257
end
258258
estim.fx̄ .= estim.x̂0arr_old
@@ -290,7 +290,7 @@ function linconstraint!(estim::MovingHorizonEstimator, model::LinModel)
290290
Fx̂ = estim.con.Fx̂
291291
Fx̂ .= estim.con.Bx̂
292292
mul!(Fx̂, estim.con.Gx̂, estim.U0, 1, 1)
293-
if model.nd 0
293+
if model.nd > 0
294294
mul!(Fx̂, estim.con.Jx̂, estim.D0, 1, 1)
295295
end
296296
X̂0min, X̂0max = trunc_bounds(estim, estim.con.X̂0min, estim.con.X̂0max, estim.nx̂)

src/model/linearization.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ function get_linearization_func(NT, solver_f!, solver_h!, nu, nx, ny, nd, p, sol
3939
# all the arguments before `backend` are mutated in this function
4040
jacobian!(f_x!, xnext, A, A_prep, backend, x, cache_k, cst_u, cst_d)
4141
jacobian!(f_u!, xnext, Bu, Bu_prep, backend, u, cache_k, cst_x, cst_d)
42-
jacobian!(f_d!, xnext, Bd, Bd_prep, backend, d, cache_k, cst_x, cst_u)
4342
jacobian!(h_x!, y, C, C_prep, backend, x, cst_d)
44-
jacobian!(h_d!, y, Dd, Dd_prep, backend, d, cst_x)
43+
if nd > 0
44+
jacobian!(f_d!, xnext, Bd, Bd_prep, backend, d, cache_k, cst_x, cst_u)
45+
jacobian!(h_d!, y, Dd, Dd_prep, backend, d, cst_x)
46+
end
4547
return nothing
4648
end
4749
return linfunc!

src/plot_sim.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,10 @@ plot_recipe(::Nothing, ::SimResult{<:Real, <:SimModel}) = nothing
379379
nx = length(indices_x)
380380

381381
layout_mat = Matrix{Tuple{Int64, Int64}}(undef, 1, 0)
382-
ny 0 && (layout_mat = [layout_mat (ny, 1)])
383-
nu 0 && (layout_mat = [layout_mat (nu, 1)])
384-
nd 0 && (layout_mat = [layout_mat (nd, 1)])
385-
nx 0 && (layout_mat = [layout_mat (nx, 1)])
382+
ny > 0 && (layout_mat = [layout_mat (ny, 1)])
383+
nu > 0 && (layout_mat = [layout_mat (nu, 1)])
384+
nd > 0 && (layout_mat = [layout_mat (nd, 1)])
385+
nx > 0 && (layout_mat = [layout_mat (nx, 1)])
386386
layout := layout_mat
387387

388388
# --- outputs y ---

0 commit comments

Comments
 (0)