Skip to content

Commit 9e00982

Browse files
authored
Merge pull request #516 from JuliaControl/bench_mhe_ph
bench: adding pH neutralization reactor to `SimModel` and `MovingHorizonEstimator` case studies
2 parents e6c3d98 + 5748fb8 commit 9e00982

4 files changed

Lines changed: 108 additions & 2 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ModelPredictiveControl"
22
uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c"
3-
version = "2.16.2"
3+
version = "2.16.3"
44
authors = ["Francis Gagnon"]
55

66
[deps]

‎benchmark/0_bench_setup.jl‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## -------------------------- linear CSTR model ----------------------------------
2+
13
Ts = 400.0
24
sys = [ tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]);
35
tf(-0.74,[800.0,1]) tf(0.74,[800.0,1]) tf(-0.74,[800.0,1]) ]
@@ -28,6 +30,7 @@ uop, yop, dop = [20, 20], [50, 30], [20]
2830
CSTR_model = setop!(LinModel(G, 2.0); uop, yop)
2931
CSTR_model_d = setop!(LinModel([G G[1:2, 2]], 2.0; i_d=[3]); uop, yop, dop)
3032

33+
## ------------------------ pendulum model -------------------------------------
3134
function f!(ẋ, x, u, _ , p)
3235
g, L, K, m = p
3336
θ, ω = x[1], x[2]
@@ -44,4 +47,33 @@ pendulum_p = p
4447
h2!(y, x, _ , _ ) = (y[1] = 180/π*x[1]; y[2]=x[2])
4548
nu, nx, ny = 1, 2, 2
4649
pendulum_model2 = NonLinModel(f!, h2!, Ts, nu, nx, ny; p)
47-
pendulum_p2 = p
50+
pendulum_p2 = p
51+
52+
# --------------------------- pH neutralization reactor model ----------------------------
53+
function fq_ph!(dx̂, res, x̂, a, u, d, p̂)
54+
c_Bin, Kw, Ka, V = p̂
55+
q_Ain, q_Bin = d[1], u[1]
56+
c_A, c_B = x̂[1], x̂[2]
57+
c_Ain = x̂[3]
58+
pH = a[1]
59+
q_out = q_Ain + q_Bin
60+
c_Aout = c_A
61+
c_Bout = c_B
62+
a_H = 10.0^(-pH)
63+
dx̂[1] = (60/V)*(q_Ain * c_Ain - q_out * c_Aout)
64+
dx̂[2] = (60/V)*(q_Bin * c_Bin - q_out * c_Bout)
65+
dx̂[3] = 0
66+
res[1] = a_H + c_B - (Kw / a_H) - (Ka * c_A / (Ka + a_H))
67+
return nothing
68+
end
69+
h_ph!(y, _ , a , _ , _ ) = (y .= a; nothing)
70+
p = [0.1, 1.0e-14, 1.75e-5, 1000.0]
71+
Ts = 0.5
72+
nu, nx, na, ny, nd = 1, 2+1, 1, 1, 1
73+
xs_0, as_0 = [0.025, 0.025, 0.1], [7]
74+
transcription = TrapezoidalCollocation()
75+
ph_model = NonLinModelDAE(
76+
fq_ph!, h_ph!, Ts, nu, nx, na, ny, nd; p, xs_0, as_0, transcription, unit="h"
77+
)
78+
ph_p = p
79+
ph_x_0 = [0.0, 0.0, 0.1]

‎benchmark/1_bench_sim_model.jl‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ u_pendulum = [0.5]
3333
CASE_MODEL["NonLinModel"]["pendulum"]["updatestate!"] =
3434
@benchmarkable(
3535
updatestate!($pendulum_model, $u_pendulum);
36+
)
37+
u_ph, d_ph = [10.0], [10.0]
38+
CASE_MODEL["NonLinModelDAE"]["pH neutral."]["updatestate!"] =
39+
@benchmarkable(
40+
updatestate!($ph_model, $u_ph, $d_ph);
3641
)

‎benchmark/2_bench_state_estim.jl‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,72 @@ CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Uno"]["Current form (Hessian,
438438
sim!($mhe_pendulum_uno_currhms, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
439439
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
440440
)
441+
442+
## ----------------- Case study: pH neutralization reactor --------------------------------
443+
model, p = ph_model, ph_p
444+
nint_ym=0; nint_u=0;
445+
He = 8;
446+
σQ = [0.5e-3, 0.5e-3, 0.9e-3]; σR=[0.05]; σP_0 = [0.05, 0.05, 1e-3]
447+
448+
x_0, x̂_0 = ph_x_0, [ph_x_0[1:2]; ph_x_0[3]+0.1]
449+
u, d = [10.0], [10.0]
450+
N = 10;
451+
452+
optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
453+
transcription = TrapezoidalCollocation(0)
454+
hessian = true
455+
mhe_ph_ipopt_tch = MovingHorizonEstimator(
456+
model; nint_ym, nint_u, He, hessian, σQ, σR, σP_0, optim, transcription
457+
)
458+
mhe_ph_ipopt_tch = setconstraint!(mhe_ph_ipopt_tch, x̂min=[0, 0, 0])
459+
JuMP.unset_time_limit_sec(mhe_ph_ipopt_tch.optim)
460+
461+
optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
462+
transcription = OrthogonalCollocation(0, 2, roots=:gaussradau)
463+
hessian = true
464+
mhe_ph_ipopt_och = MovingHorizonEstimator(
465+
model; nint_ym, nint_u, He, hessian, σQ, σR, σP_0, optim, transcription
466+
)
467+
mhe_ph_ipopt_och = setconstraint!(mhe_ph_ipopt_och, x̂min=[0, 0, 0])
468+
JuMP.unset_time_limit_sec(mhe_ph_ipopt_och.optim)
469+
470+
optim = JuMP.Model(()->UnoSolver.Optimizer(preset="funnelsqp"), add_bridges=false)
471+
transcription = TrapezoidalCollocation(0)
472+
hessian = true
473+
mhe_ph_uno_tch = MovingHorizonEstimator(
474+
model; nint_ym, nint_u, He, hessian, σQ, σR, σP_0, optim, transcription
475+
)
476+
mhe_ph_uno_tch = setconstraint!(mhe_ph_uno_tch, x̂min=[0, 0, 0])
477+
JuMP.unset_time_limit_sec(mhe_ph_uno_tch.optim)
478+
479+
optim = JuMP.Model(()->UnoSolver.Optimizer(preset="funnelsqp"), add_bridges=false)
480+
transcription = OrthogonalCollocation(0, 2, roots=:gaussradau)
481+
hessian = true
482+
mhe_ph_uno_och = MovingHorizonEstimator(
483+
model; nint_ym, nint_u, He, hessian, σQ, σR, σP_0, optim, transcription
484+
)
485+
mhe_ph_uno_och = setconstraint!(mhe_ph_uno_och, x̂min=[0, 0, 0])
486+
JuMP.unset_time_limit_sec(mhe_ph_uno_och.optim)
487+
488+
samples, evals, seconds = 25, 1, 15*60
489+
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Ipopt"]["TrapezoidalCollocation (Hessian)"] =
490+
@benchmarkable(
491+
sim!($mhe_ph_ipopt_tch, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
492+
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
493+
)
494+
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Ipopt"]["OrthogonalCollocation (Hessian)"] =
495+
@benchmarkable(
496+
sim!($mhe_ph_ipopt_och, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
497+
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
498+
)
499+
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Uno"]["TrapezoidalCollocation (Hessian)"] =
500+
@benchmarkable(
501+
sim!($mhe_ph_uno_tch, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
502+
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
503+
)
504+
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Uno"]["OrthogonalCollocation (Hessian)"] =
505+
@benchmarkable(
506+
sim!($mhe_ph_uno_och, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
507+
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
508+
)
509+

0 commit comments

Comments
 (0)