Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ModelPredictiveControl"
uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c"
version = "2.16.2"
version = "2.16.3"
authors = ["Francis Gagnon"]

[deps]
Expand Down
34 changes: 33 additions & 1 deletion benchmark/0_bench_setup.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## -------------------------- linear CSTR model ----------------------------------

Ts = 400.0
sys = [ tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]);
tf(-0.74,[800.0,1]) tf(0.74,[800.0,1]) tf(-0.74,[800.0,1]) ]
Expand Down Expand Up @@ -28,6 +30,7 @@ uop, yop, dop = [20, 20], [50, 30], [20]
CSTR_model = setop!(LinModel(G, 2.0); uop, yop)
CSTR_model_d = setop!(LinModel([G G[1:2, 2]], 2.0; i_d=[3]); uop, yop, dop)

## ------------------------ pendulum model -------------------------------------
function f!(ẋ, x, u, _ , p)
g, L, K, m = p
θ, ω = x[1], x[2]
Expand All @@ -44,4 +47,33 @@ pendulum_p = p
h2!(y, x, _ , _ ) = (y[1] = 180/π*x[1]; y[2]=x[2])
nu, nx, ny = 1, 2, 2
pendulum_model2 = NonLinModel(f!, h2!, Ts, nu, nx, ny; p)
pendulum_p2 = p
pendulum_p2 = p

# --------------------------- pH neutralization reactor model ----------------------------
function fq_ph!(dx̂, res, x̂, a, u, d, p̂)
c_Bin, Kw, Ka, V = p̂
q_Ain, q_Bin = d[1], u[1]
c_A, c_B = x̂[1], x̂[2]
c_Ain = x̂[3]
pH = a[1]
q_out = q_Ain + q_Bin
c_Aout = c_A
c_Bout = c_B
a_H = 10.0^(-pH)
dx̂[1] = (60/V)*(q_Ain * c_Ain - q_out * c_Aout)
dx̂[2] = (60/V)*(q_Bin * c_Bin - q_out * c_Bout)
dx̂[3] = 0
res[1] = a_H + c_B - (Kw / a_H) - (Ka * c_A / (Ka + a_H))
return nothing
end
h_ph!(y, _ , a , _ , _ ) = (y .= a; nothing)
p = [0.1, 1.0e-14, 1.75e-5, 1000.0]
Ts = 0.5
nu, nx, na, ny, nd = 1, 2+1, 1, 1, 1
xs_0, as_0 = [0.025, 0.025, 0.1], [7]
transcription = TrapezoidalCollocation()
ph_model = NonLinModelDAE(
fq_ph!, h_ph!, Ts, nu, nx, na, ny, nd; p, xs_0, as_0, transcription, unit="h"
)
ph_p = p
ph_x_0 = [0.0, 0.0, 0.1]
5 changes: 5 additions & 0 deletions benchmark/1_bench_sim_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ u_pendulum = [0.5]
CASE_MODEL["NonLinModel"]["pendulum"]["updatestate!"] =
@benchmarkable(
updatestate!($pendulum_model, $u_pendulum);
)
u_ph, d_ph = [10.0], [10.0]
CASE_MODEL["NonLinModelDAE"]["pH neutral."]["updatestate!"] =
@benchmarkable(
updatestate!($ph_model, $u_ph, $d_ph);
)
69 changes: 69 additions & 0 deletions benchmark/2_bench_state_estim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,72 @@ CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Uno"]["Current form (Hessian,
sim!($mhe_pendulum_uno_currhms, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)

## ----------------- Case study: pH neutralization reactor --------------------------------
model, p = ph_model, ph_p
nint_ym=0; nint_u=0;
He = 8;
σQ = [0.5e-3, 0.5e-3, 0.9e-3]; σR=[0.05]; σP_0 = [0.05, 0.05, 1e-3]

x_0, x̂_0 = ph_x_0, [ph_x_0[1:2]; ph_x_0[3]+0.1]
u, d = [10.0], [10.0]
N = 10;

optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
transcription = TrapezoidalCollocation(0)
hessian = true
mhe_ph_ipopt_tch = MovingHorizonEstimator(
model; nint_ym, nint_u, He, hessian, σQ, σR, σP_0, optim, transcription
)
mhe_ph_ipopt_tch = setconstraint!(mhe_ph_ipopt_tch, x̂min=[0, 0, 0])
JuMP.unset_time_limit_sec(mhe_ph_ipopt_tch.optim)

optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
transcription = OrthogonalCollocation(0, 2, roots=:gaussradau)
hessian = true
mhe_ph_ipopt_och = MovingHorizonEstimator(
model; nint_ym, nint_u, He, hessian, σQ, σR, σP_0, optim, transcription
)
mhe_ph_ipopt_och = setconstraint!(mhe_ph_ipopt_och, x̂min=[0, 0, 0])
JuMP.unset_time_limit_sec(mhe_ph_ipopt_och.optim)

optim = JuMP.Model(()->UnoSolver.Optimizer(preset="funnelsqp"), add_bridges=false)
transcription = TrapezoidalCollocation(0)
hessian = true
mhe_ph_uno_tch = MovingHorizonEstimator(
model; nint_ym, nint_u, He, hessian, σQ, σR, σP_0, optim, transcription
)
mhe_ph_uno_tch = setconstraint!(mhe_ph_uno_tch, x̂min=[0, 0, 0])
JuMP.unset_time_limit_sec(mhe_ph_uno_tch.optim)

optim = JuMP.Model(()->UnoSolver.Optimizer(preset="funnelsqp"), add_bridges=false)
transcription = OrthogonalCollocation(0, 2, roots=:gaussradau)
hessian = true
mhe_ph_uno_och = MovingHorizonEstimator(
model; nint_ym, nint_u, He, hessian, σQ, σR, σP_0, optim, transcription
)
mhe_ph_uno_och = setconstraint!(mhe_ph_uno_och, x̂min=[0, 0, 0])
JuMP.unset_time_limit_sec(mhe_ph_uno_och.optim)

samples, evals, seconds = 25, 1, 15*60
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Ipopt"]["TrapezoidalCollocation (Hessian)"] =
@benchmarkable(
sim!($mhe_ph_ipopt_tch, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Ipopt"]["OrthogonalCollocation (Hessian)"] =
@benchmarkable(
sim!($mhe_ph_ipopt_och, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Uno"]["TrapezoidalCollocation (Hessian)"] =
@benchmarkable(
sim!($mhe_ph_uno_tch, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Uno"]["OrthogonalCollocation (Hessian)"] =
@benchmarkable(
sim!($mhe_ph_uno_och, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)

Loading