diff --git a/Project.toml b/Project.toml index 1544b4abe..1572cfc85 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ModelPredictiveControl" uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c" -version = "2.16.2" +version = "2.16.3" authors = ["Francis Gagnon"] [deps] diff --git a/benchmark/0_bench_setup.jl b/benchmark/0_bench_setup.jl index 78bc7f8ea..95cdd088c 100644 --- a/benchmark/0_bench_setup.jl +++ b/benchmark/0_bench_setup.jl @@ -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]) ] @@ -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] @@ -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 \ No newline at end of file +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] \ No newline at end of file diff --git a/benchmark/1_bench_sim_model.jl b/benchmark/1_bench_sim_model.jl index 2d3e08e31..f08829114 100644 --- a/benchmark/1_bench_sim_model.jl +++ b/benchmark/1_bench_sim_model.jl @@ -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); ) \ No newline at end of file diff --git a/benchmark/2_bench_state_estim.jl b/benchmark/2_bench_state_estim.jl index fd89b0dbc..cd767f600 100644 --- a/benchmark/2_bench_state_estim.jl +++ b/benchmark/2_bench_state_estim.jl @@ -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() + ) +