Skip to content

Commit f80df48

Browse files
committed
doc: simple MHE and NMPC of the pH tank with MTK
1 parent 56e12af commit f80df48

1 file changed

Lines changed: 28 additions & 30 deletions

File tree

‎docs/src/manual/mtk.md‎

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ function generate_f_h_mtk(model, inputs, outputs, disturbances=nothing)
7777
return nothing
7878
end
7979
ic = initial_conditions(io_sys)
80-
io_sys |> equations |> display
8180
p_map, x_map = try
8281
(
8382
Dict(sym => ic[sym] for sym in p_sym),
@@ -323,48 +322,47 @@ display(p) # hide
323322
![plot3_MTK](plot3_MTK.svg)
324323

325324
```@example 1
326-
using Plots
327-
u, d, x_0 = [10.0], [10.0], [0.0495, 0.0505]
328-
N = 21
325+
nint_u=[1] # integrating state at the q_Bin input
326+
He = 8; hessian = true
327+
σQ = [0.5e-3, 0.5e-3]; σR=[0.05]; σP_0 = [0.05, 0.05]; σQint_u=[0.15]; σPint_u_0=[0.075]
328+
mhe = MovingHorizonEstimator(model; nint_u, He, hessian, σQ, σR, σP_0, σQint_u, σPint_u_0)
329+
using JuMP; unset_time_limit_sec(mhe.optim) # no wall time limit during optimization
330+
mhe = setconstraint!(mhe, x̂min=[0, 0, -Inf])
331+
```
332+
333+
```@example 1
334+
x_0, x̂_0 = [0.0495, 0.0505], [0.025, 0.025, 0]
329335
using Logging # hide
330336
res = with_logger(ConsoleLogger(stderr, Error)) do # hide
331-
res = sim!(model, N, u, d; x_0)
337+
res = sim!(mhe, N, u, d; x_0, x̂_0)
332338
end # hide
333-
p = plot(res)
334-
savefig(p, "plot3_MTK.svg") # hide
339+
p = plot(res, plotd=false, plotu=false, plotxwithx̂=true, plotx̂min=false)
340+
savefig(p, "plot4_DAEpH.svg") # hide
335341
display(p) # hide
336342
```
337343

338-
```@example 1
339-
@variables c_Ain_s(t)=0.1
340-
eqs_2 = [
341-
D(c_A) ~ 60/V*(q_Ain * c_Ain_s - q_out * c_Aout)
342-
D(c_B) ~ 60/V*(q_Bin * c_Bin - q_out * c_Bout)
343-
D(c_Ain_s) ~ 0
344-
0 ~ 10^(-pH) + c_B - (Kw / 10^(-pH)) - (Ka * c_A / (Ka + 10^(-pH)))
345-
q_out ~ q_Ain + q_Bin
346-
c_Aout ~ c_A
347-
c_Bout ~ c_B
348-
]
349-
@named mtk_model_2 = System(eqs_2, t)
350-
f_mtk!, h_mtk!, p_sym, p, x_sym, x, i_alg = generate_f_h_mtk(
351-
mtk_model_2, inputs, outputs, disturbances
352-
)
353-
[x_sym x]
354-
```
344+
![plot4_MTK](plot4_MTK.svg)
355345

356346
```@example 1
347+
nmpc = NonLinMPC(mhe, Hp=8, Hc=2, Nwt=[5.0], hessian=true)
348+
using JuMP; unset_time_limit_sec(nmpc.optim) # no wall time limit during optimization
349+
nmpc = setconstraint!(nmpc, ymin=[6], ymax=[8], umin=[0], umax=[20])
357350
```
358351

359352
```@example 1
360-
model = generate_model(
361-
f_mtk!, h_mtk!, Ts, i_alg, inputs, outputs, disturbances;
362-
p, xs_mtk, unit, transcription
363-
)
364-
vx̂ = [vx; raw"$c_{Ain}$ (mol/L)"]
365-
model = setname!(model, u=vu, d=vd, y=vy, x=vx̂)
353+
ry = [7.0]
354+
x_0, x̂_0 = [0.04, 0.07], [0.025, 0.025, 0]
355+
using Logging # hide
356+
res = with_logger(ConsoleLogger(stderr, Error)) do # hide
357+
res = sim!(nmpc, N, ry, d; x_0, x̂_0)
358+
end # hide
359+
p = plot(res, plotd=false, plotu=false, plotxwithx̂=true, plotx̂min=false, plotŷ=true)
360+
savefig(p, "plot5_DAEpH.svg") # hide
361+
display(p) # hide
366362
```
367363

364+
![plot5_MTK](plot5_MTK.svg)
365+
368366
## Acknowledgement
369367

370368
Authored by `1-Bart-1` and `baggepinnen`, thanks for the contribution.

0 commit comments

Comments
 (0)