Skip to content

Commit eedfd84

Browse files
committed
added: progress kwarg for sim!
1 parent 9dd5db6 commit eedfd84

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
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/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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ 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!(plant::SimModel, N::Int, u=plant.uop.+1, d=plant.dop; x_0=plant.xop, progress=true) -> res
110110
111111
Open-loop simulation of `plant` for `N` time steps, default to unit bump test on all inputs.
112112
113113
The manipulated inputs ``\mathbf{u}`` and measured disturbances ``\mathbf{d}`` are held
114114
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
115+
specified by `x_0` keyword arguments. If `progress` is `true`, VS Code will display a
116+
progress percentage of the simulation. The function returns [`SimResult`](@ref) instances
116117
that can be visualized by calling `plot` on them. Note that the method mutates `plant`
117118
internal states.
118119
@@ -129,15 +130,16 @@ function sim!(
129130
N::Int,
130131
u::Vector = plant.uop.+1,
131132
d::Vector = plant.dop;
132-
x_0 = plant.xop
133+
x_0 = plant.xop,
134+
progress = true
133135
) where {NT<:Real}
134136
T_data = collect(plant.Ts*(0:(N-1)))
135137
Y_data = Matrix{NT}(undef, plant.ny, N)
136138
U_data = Matrix{NT}(undef, plant.nu, N)
137139
D_data = Matrix{NT}(undef, plant.nd, N)
138140
X_data = Matrix{NT}(undef, plant.nx, N)
139141
setstate!(plant, x_0)
140-
@progress name="$(nameof(typeof(plant))) simulation" for i=1:N
142+
@progressif progress name="$(nameof(typeof(plant))) simulation" for i=1:N
141143
y = evaloutput(plant, d)
142144
Y_data[:, i] .= y
143145
U_data[:, i] .= u
@@ -187,6 +189,7 @@ vectors. The simulated sensor and process noises of `plant` are specified by `y_
187189
- `x̂_0 = nothing` or *`xhat_0`* : initial estimate ``\mathbf{x̂}(0)``, [`initstate!`](@ref)
188190
is used if `nothing`
189191
- `lastu = plant.uop` : last plant input ``\mathbf{u}`` for ``\mathbf{x̂}`` initialization
192+
- `progress = true` : display a progress percentage in VS Code if `true`
190193
191194
# Examples
192195
```jldoctest
@@ -263,6 +266,7 @@ function sim_closedloop!(
263266
x_0 = plant.xop,
264267
xhat_0 = nothing,
265268
lastu = plant.uop,
269+
progress = true,
266270
x̂_0 = xhat_0
267271
) where {NT<:Real}
268272
model = estim.model
@@ -282,7 +286,7 @@ function sim_closedloop!(
282286
lastd, lasty = d, evaloutput(plant, d)
283287
initstate!(est_mpc, lastu, lasty[estim.i_ym], lastd)
284288
isnothing(x̂_0) || setstate!(est_mpc, x̂_0)
285-
@progress name="$(nameof(typeof(est_mpc))) simulation" for i=1:N
289+
@progressif progress name="$(nameof(typeof(est_mpc))) simulation" for i=1:N
286290
d = lastd + d_step + d_noise.*randn(plant.nd)
287291
y = evaloutput(plant, d) + y_step + y_noise.*randn(plant.ny)
288292
ym = y[estim.i_ym]

0 commit comments

Comments
 (0)