Skip to content

Commit 438c8e2

Browse files
authored
Merge pull request #260 from JuliaControl/no_progress
add: `progress` keyword argument in `sim!` to disable progress logs
2 parents 5ac89b4 + c9d662a commit 438c8e2

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

src/ModelPredictiveControl.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ using LinearAlgebra, SparseArrays
55
using Random: randn
66

77
using RecipesBase
8-
using ProgressLogging
98

109
using DifferentiationInterface: ADTypes.AbstractADType, AutoForwardDiff, AutoSparse
1110
using DifferentiationInterface: gradient!, jacobian!, prepare_gradient, prepare_jacobian
@@ -14,6 +13,8 @@ using DifferentiationInterface: Constant, Cache
1413
using SparseConnectivityTracer: TracerSparsityDetector
1514
using SparseMatrixColorings: GreedyColoringAlgorithm, sparsity_pattern
1615

16+
import ProgressLogging
17+
1718
import ForwardDiff
1819

1920
import ControlSystemsBase

src/controller/transcription.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,8 +1169,8 @@ The method mutates `Ŷ0`, `x̂0end`, `X̂0`, `Û0` and `K0` arguments. The aug
11691169
[`f̂!`](@ref) and [`ĥ!`](@ref) functions is called recursively in a `for` loop:
11701170
```math
11711171
\begin{aligned}
1172-
\mathbf{x̂_0}(k+1) &= \mathbf{f̂}\Big(\mathbf{x̂_0}(k), \mathbf{u_0}(k), \mathbf{d_0}(k) \Big) \\
1173-
\mathbf{ŷ_0}(k) &= \mathbf{ĥ}\Big(\mathbf{x̂_0}(k), \mathbf{d_0}(k) \Big)
1172+
\mathbf{x̂_0}(k+1) &= \mathbf{f̂}\Big(\mathbf{x̂_0}(k), \mathbf{u_0}(k), \mathbf{d̂_0}(k) \Big) \\
1173+
\mathbf{ŷ_0}(k) &= \mathbf{ĥ}\Big(\mathbf{x̂_0}(k), \mathbf{d̂_0}(k) \Big)
11741174
\end{aligned}
11751175
```
11761176
"""
@@ -1211,7 +1211,7 @@ Compute vectors if `model` is a [`NonLinModel`](@ref) and other [`TranscriptionM
12111211
The method mutates `Ŷ0` and `x̂0end` arguments. The augmented output function [`ĥ!`](@ref)
12121212
is called multiple times in a `for` loop:
12131213
```math
1214-
\mathbf{ŷ_0}(k) = \mathbf{ĥ}\Big(\mathbf{x̂_0}(k), \mathbf{d_0}(k) \Big)
1214+
\mathbf{ŷ_0}(k) = \mathbf{ĥ}\Big(\mathbf{x̂_0}(k), \mathbf{d̂_0}(k) \Big)
12151215
```
12161216
in which ``\mathbf{x̂_0}`` is the augmented state extracted from the decision variable `Z̃`.
12171217
"""
@@ -1332,7 +1332,7 @@ Nonlinear equality constrains for [`NonLinModel`](@ref) and [`MultipleShooting`]
13321332
The method mutates the `geq`, `X̂0`, `Û0` and `K0` vectors in argument. The nonlinear
13331333
equality constraints `geq` only includes the augmented state defects, computed with:
13341334
```math
1335-
\mathbf{ŝ}(k+1) = \mathbf{f̂}\Big(\mathbf{x̂_0}(k), \mathbf{u_0}(k), \mathbf{d_0}(k)\Big)
1335+
\mathbf{ŝ}(k+1) = \mathbf{f̂}\Big(\mathbf{x̂_0}(k), \mathbf{u_0}(k), \mathbf{d̂_0}(k)\Big)
13361336
- \mathbf{x̂_0}(k+1)
13371337
```
13381338
in which the augmented state ``\mathbf{x̂_0}`` are extracted from the decision variables
@@ -1395,8 +1395,8 @@ extracted from the decision variables `Z̃`. The ``\mathbf{k}`` coefficients are
13951395
evaluated from the continuous-time function `model.f!` and:
13961396
```math
13971397
\begin{aligned}
1398-
\mathbf{k}_1 &= \mathbf{f}\Big(\mathbf{x_0}(k), \mathbf{û_0}(k), \mathbf{d_0}(k) \Big) \\
1399-
\mathbf{k}_2 &= \mathbf{f}\Big(\mathbf{x_0}(k+1), \mathbf{û_0}(k+h), \mathbf{d_0}(k+1)\Big)
1398+
\mathbf{k}_1 &= \mathbf{f}\Big(\mathbf{x_0}(k), \mathbf{û_0}(k), \mathbf{d̂_0}(k) \Big) \\
1399+
\mathbf{k}_2 &= \mathbf{f}\Big(\mathbf{x_0}(k+1), \mathbf{û_0}(k+h), \mathbf{d̂_0}(k+1)\Big)
14001400
\end{aligned}
14011401
```
14021402
in which ``h`` is the hold order `transcription.h` and the disturbed input is:
@@ -1419,7 +1419,6 @@ function con_nonlinprogeq!(
14191419
nk = get_nk(model, transcription)
14201420
D̂0 = mpc.D̂0
14211421
X̂0_Z̃ = @views Z̃[(nΔU+1):(nΔU+nX̂)]
1422-
#TODO: allow parallel for loop or threads?
14231422
@threadsif f_threads for j=1:Hp
14241423
if j < 2
14251424
x̂0 = @views mpc.estim.x̂0[1:nx̂]

src/general.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,15 @@ macro threadsif(flag, expr)
133133
$expr
134134
end
135135
end |> esc
136+
end
137+
138+
"Add `ProgressLogging.@progress` with the name `name` to a `for` loop if `flag==true`"
139+
macro progressif(flag, name, expr)
140+
quote
141+
if $(flag)
142+
ProgressLogging.@progress $name $expr
143+
else
144+
$expr
145+
end
146+
end |> esc
136147
end

src/plot_sim.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,16 @@ end
106106

107107

108108
@doc raw"""
109-
sim!(plant::SimModel, N::Int, u=plant.uop.+1, d=plant.dop; x_0=plant.xop) -> res
109+
sim!(
110+
plant::SimModel, N::Int, u=plant.uop.+1, d=plant.dop; x_0=plant.xop, progress=true
111+
) -> res
110112
111113
Open-loop simulation of `plant` for `N` time steps, default to unit bump test on all inputs.
112114
113115
The manipulated inputs ``\mathbf{u}`` and measured disturbances ``\mathbf{d}`` are held
114116
constant at `u` and `d` values, respectively. The plant initial state ``\mathbf{x}(0)`` is
115-
specified by `x_0` keyword arguments. The function returns [`SimResult`](@ref) instances
117+
specified by `x_0` keyword arguments. If `progress` is `true`, VS Code will display a
118+
progress percentage of the simulation. The function returns [`SimResult`](@ref) instances
116119
that can be visualized by calling `plot` on them. Note that the method mutates `plant`
117120
internal states.
118121
@@ -129,15 +132,16 @@ function sim!(
129132
N::Int,
130133
u::Vector = plant.uop.+1,
131134
d::Vector = plant.dop;
132-
x_0 = plant.xop
135+
x_0 = plant.xop,
136+
progress = true
133137
) where {NT<:Real}
134138
T_data = collect(plant.Ts*(0:(N-1)))
135139
Y_data = Matrix{NT}(undef, plant.ny, N)
136140
U_data = Matrix{NT}(undef, plant.nu, N)
137141
D_data = Matrix{NT}(undef, plant.nd, N)
138142
X_data = Matrix{NT}(undef, plant.nx, N)
139143
setstate!(plant, x_0)
140-
@progress name="$(nameof(typeof(plant))) simulation" for i=1:N
144+
@progressif progress name="$(nameof(typeof(plant))) simulation" for i=1:N
141145
y = evaloutput(plant, d)
142146
Y_data[:, i] .= y
143147
U_data[:, i] .= u
@@ -187,6 +191,7 @@ vectors. The simulated sensor and process noises of `plant` are specified by `y_
187191
- `x̂_0 = nothing` or *`xhat_0`* : initial estimate ``\mathbf{x̂}(0)``, [`initstate!`](@ref)
188192
is used if `nothing`
189193
- `lastu = plant.uop` : last plant input ``\mathbf{u}`` for ``\mathbf{x̂}`` initialization
194+
- `progress = true` : display a progress percentage in VS Code if `true`
190195
191196
# Examples
192197
```jldoctest
@@ -263,6 +268,7 @@ function sim_closedloop!(
263268
x_0 = plant.xop,
264269
xhat_0 = nothing,
265270
lastu = plant.uop,
271+
progress = true,
266272
x̂_0 = xhat_0
267273
) where {NT<:Real}
268274
model = estim.model
@@ -282,7 +288,7 @@ function sim_closedloop!(
282288
lastd, lasty = d, evaloutput(plant, d)
283289
initstate!(est_mpc, lastu, lasty[estim.i_ym], lastd)
284290
isnothing(x̂_0) || setstate!(est_mpc, x̂_0)
285-
@progress name="$(nameof(typeof(est_mpc))) simulation" for i=1:N
291+
@progressif progress name="$(nameof(typeof(est_mpc))) simulation" for i=1:N
286292
d = lastd + d_step + d_noise.*randn(plant.nd)
287293
y = evaloutput(plant, d) + y_step + y_noise.*randn(plant.ny)
288294
ym = y[estim.i_ym]

test/4_test_plot_sim.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
@test res.D_data[:, 1] model.dop
1111
@test res.X_data[:, 1] zeros(model.nx)
1212

13+
@test_nowarn sim!(model, 15, progress=false)
14+
1315
res_man = SimResult(model, res.U_data, res.Y_data, res.D_data; X_data=res.X_data)
1416
@test res_man.U_data res.U_data
1517
@test res_man.Y_data res.Y_data
@@ -56,6 +58,8 @@ end
5658
@test res.X_data[:, 1] zeros(estim.model.nx)
5759
@test res.X̂_data[:, 1] zeros(estim.nx̂)
5860

61+
@test_nowarn sim!(estim, 15, progress=false)
62+
5963
res_man = SimResult(
6064
estim, res.U_data, res.Y_data, res.D_data;
6165
X_data=res.X_data, X̂_data=res.X̂_data
@@ -140,6 +144,8 @@ end
140144
@test res.X_data[:, 1] zeros(mpc1.estim.model.nx)
141145
@test res.X̂_data[:, 1] zeros(mpc1.estim.nx̂)
142146

147+
@test_nowarn sim!(mpc1, 15, progress=false)
148+
143149
mpc2 = ExplicitMPC(LinModel(sys, Ts, i_d=[3]))
144150
res = sim!(mpc2, 15)
145151
@test isa(res.obj, ExplicitMPC)

0 commit comments

Comments
 (0)