Skip to content

Commit a8b8c9b

Browse files
authored
Merge pull request #186 from JuliaControl/debug_linearize
debug: do not call `jacobian!` if `nd==0` in `linearize`
2 parents 3705821 + a006525 commit a8b8c9b

File tree

7 files changed

+23
-16
lines changed

7 files changed

+23
-16
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelPredictiveControl"
22
uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c"
33
authors = ["Francis Gagnon"]
4-
version = "1.5.1"
4+
version = "1.5.2"
55

66
[deps]
77
ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e"

src/controller/execute.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, R̂y, R̂
208208
F .+= mpc.B # F = F + B
209209
mul!(F, mpc.K, mpc.estim.x̂0, 1, 1) # F = F + K*x̂0
210210
mul!(F, mpc.V, mpc.estim.lastu0, 1, 1) # F = F + V*lastu0
211-
if model.nd 0
211+
if model.nd > 0
212212
mul!(F, mpc.G, mpc.d0, 1, 1) # F = F + G*d0
213213
mul!(F, mpc.J, mpc.D̂0, 1, 1) # F = F + J*D̂0
214214
end
@@ -256,7 +256,7 @@ is an [`InternalModel`](@ref). The function returns `mpc.F`.
256256
function initpred_common!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂u)
257257
mul!(mpc.Tu_lastu0, mpc.Tu, mpc.estim.lastu0)
258258
mpc.ŷ .= evaloutput(mpc.estim, d)
259-
if model.nd 0
259+
if model.nd > 0
260260
mpc.d0 .= d .- model.dop
261261
mpc.D̂0 .=.- mpc.Dop
262262
mpc.D̂e[1:model.nd] .= d

src/controller/transcription.jl

+3-3
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

+4-4
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

+4-2
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

+4-4
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 ---

test/1_test_sim_model.jl

+5
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,11 @@ end
355355
Ynl, Yl
356356
end
357357
@test all(isapprox.(Ynl, Yl, atol=1e-6))
358+
359+
f2!(xnext, x, u, _, _) = (xnext .= x .+ u)
360+
h2!(y, x, _, _) = (y .= x)
361+
nonlinmodel4 = NonLinModel(f2!,h2!,Ts,1,1,1,0,solver=nothing,jacobian=AutoFiniteDiff())
362+
@test_nowarn linearize(nonlinmodel4, x=[1], u=[2])
358363
end
359364

360365
@testitem "NonLinModel real time simulations" setup=[SetupMPCtests] begin

0 commit comments

Comments
 (0)