@@ -15,8 +15,7 @@ objects. This object is then used to design controllers similar to the previous
1515!!! danger "Disclaimer"
1616 This simple example is not an official interface to ` ModelingToolkit.jl ` . It is provided
1717 as a basic starting template to combine both packages. There is no guarantee that it
18- will work for all corner cases. For instance, measured disturbances are not supported in
19- the current implementation (to be added).
18+ will work for all corner cases.
2019
2120``` @codeblocks
2221line_counter = :continue
@@ -41,9 +40,11 @@ function generate_f_h_mtk(model, inputs, outputs, disturbances=nothing)
4140 model, inputs; known_disturbance_inputs, split=false, simplify=true
4241 )
4342 i_alg = ModelingToolkit.is_alg_equation.(equations(io_sys))
44- function f_mtk!(ẋ, x, u, _ , p)
43+ nd = isnothing(disturbances) ? 0 : length(disturbances)
44+ f_ip_with_d = (nd < 1) ? (ẋ, x, u, p, t, _ ) -> f_ip(ẋ, x, u, p, t) : f_ip
45+ function f_mtk!(ẋ, x, u, d, p)
4546 try
46- f_ip (ẋ, x, u, p, nothing)
47+ f_ip_with_d (ẋ, x, u, p, nothing, d )
4748 catch err
4849 if err isa MethodError
4950 error("NonLinModel does not support a time argument t in the f "*
@@ -55,15 +56,16 @@ function generate_f_h_mtk(model, inputs, outputs, disturbances=nothing)
5556 return nothing
5657 end
5758 (_, h_ip) = ModelingToolkit.build_explicit_observed_function(
58- io_sys, outputs; inputs, return_inplace = true
59+ io_sys, outputs; inputs, known_disturbance_inputs, return_inplace = true
5960 )
61+ h_ip_with_d = (nd < 1) ? (y, x, u, p, t, _ ) -> h_ip(y, x, u, p, t) : h_ip
6062 u_nothing = fill(nothing, length(inputs))
61- function h_mtk!(y, x, _ , p)
63+ function h_mtk!(y, x, d , p)
6264 try
6365 # MTK.jl supports a `u` argument in `h_ip` function but not this package. We
6466 # set `u` as a vector of nothing and `h_ip` function will presumably throw
6567 # an MethodError if this argument is used inside the function
66- h_ip (y, x, u_nothing, p, nothing)
68+ h_ip_with_d (y, x, u_nothing, p, nothing, d )
6769 catch err
6870 if err isa MethodError
6971 error("NonLinModel only support strictly proper systems (no "*
@@ -100,10 +102,11 @@ this package's interface:
100102
101103``` @example 1
102104function generate_model(
103- f_mtk!::F, h_mtk!::H, Ts, i_alg, inputs, outputs, p;
104- xs_mtk=zeros(length(i_alg)), unit="s", transcription=OrthogonalCollocation()
105+ f_mtk!::F, h_mtk!::H, Ts, i_alg, inputs, outputs, disturbances=nothing;
106+ p=nothing, xs_mtk=zeros(length(i_alg)), unit="s", transcription=OrthogonalCollocation()
105107) where {F<:Function, H<:Function}
106108 nu, ny = length(inputs), length(outputs)
109+ nd = isnothing(disturbances) ? 0 : length(disturbances)
107110 nx_mtk::Int = length(i_alg)
108111 na::Int = sum(i_alg)
109112 nx::Int = nx_mtk - na
@@ -130,11 +133,11 @@ function generate_model(
130133 end
131134 xs_0, as_0 = xs_mtk[i_x], xs_mtk[i_a]
132135 model = NonLinModelDAE(
133- fq!, h!, Ts, nu, nx, na, ny; p, unit, xs_0, as_0, transcription
136+ fq!, h!, Ts, nu, nx, na, ny, nd ; p, unit, xs_0, as_0, transcription
134137 )
135138 else
136139 f!, h! = f_mtk!, h_mtk!
137- model = NonLinModel(f!, h!, Ts, nu, nx, ny; p, unit)
140+ model = NonLinModel(f!, h!, Ts, nu, nx, ny, nd ; p, unit)
138141 end
139142 return model # deliberately type unstable function
140143end
@@ -179,7 +182,7 @@ We can now construct a [`SimModel`](@ref) with this specific state realization:
179182
180183``` @example 1
181184Ts = 0.1
182- model = generate_model(f_mtk!, h_mtk!, Ts, i_alg, inputs, outputs, p)
185+ model = generate_model(f_mtk!, h_mtk!, Ts, i_alg, inputs, outputs; p)
183186vu, vx, vy = ["\$τ\$ (Nm)"], ["\$ω\$ (rad/s)", "\$θ\$ (rad)"], ["\$θ\$ (°)"]
184187model = setname!(model; u=vu, x=vx, y=vy)
185188```
@@ -192,7 +195,7 @@ coefficient ``K``:
192195i_K = findfirst(==("K"), string.(p_sym))
193196p2 = copy(p)
194197p2[i_K] = 1.25*p[i_K]
195- plant = generate_model(f_mtk!, h_mtk!, Ts, i_alg, inputs, outputs, p2)
198+ plant = generate_model(f_mtk!, h_mtk!, Ts, i_alg, inputs, outputs; p= p2)
196199plant = setname!(plant; u=vu, x=vx, y=vy)
197200```
198201
@@ -291,11 +294,12 @@ Ts, unit = 0.5, "h"
291294xs_mtk = x
292295transcription = TrapezoidalCollocation()
293296model = generate_model(
294- f_mtk!, h_mtk!, Ts, i_alg, inputs, outputs, p; xs_mtk, unit, transcription
297+ f_mtk!, h_mtk!, Ts, i_alg, inputs, outputs, disturbances;
298+ p, xs_mtk, unit, transcription
295299)
296- vu = [raw"$q_{Bin}$ (L/min)"]
300+ vu, vd = [raw"$q_{Bin}$ (L/min)"], [raw"$q_{Ain }$ (L/min)"]
297301vx, vy = [raw"$c_B$ (mol/L)", raw"$c_A$ (mol/L)"], [raw"$\mathrm{pH}$"]
298- model = setname!(model, u=vu, y=vy, x=vx)
302+ model = setname!(model, u=vu, d=vd, y=vy, x=vx)
299303```
300304
301305The returned value is a [ ` NonLinModelDAE ` ] ( @ref ) object, as expected. The transient
@@ -304,11 +308,11 @@ response is identical to the first 10 hours of the open-loop simulations of the
304308
305309``` @example 1
306310using Plots
307- u, x_0 = [10.0], [0.0495, 0.0505]
311+ u, d, x_0 = [10.0], [10.0], [0.0495, 0.0505]
308312N = 21
309313using Logging # hide
310314res = with_logger(ConsoleLogger(stderr, Error)) do # hide
311- res = sim!(model, N, u; x_0)
315+ res = sim!(model, N, u, d ; x_0)
312316end # hide
313317p = plot(res)
314318savefig(p, "plot3_MTK.svg") # hide
0 commit comments