Skip to content

Commit 4f907e3

Browse files
committed
doc: MTK example throw error if u arg is used inside output function
1 parent 83aca77 commit 4f907e3

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

docs/src/manual/mtk.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,45 @@ function generate_f_h(model, inputs, outputs)
6060
if any(ModelingToolkit.is_alg_equation, equations(io_sys))
6161
error("Systems with algebraic equations are not supported")
6262
end
63-
h_ = ModelingToolkit.build_explicit_observed_function(io_sys, outputs; inputs)
64-
nx = length(dvs)
63+
nu, nx, ny = length(inputs), length(dvs), length(outputs)
6564
vx = string.(dvs)
6665
p = varmap_to_vars(defaults(io_sys), psym)
6766
function f!(ẋ, x, u, _ , p)
68-
f_ip(ẋ, x, u, p, 1)
67+
try
68+
f_ip(ẋ, x, u, p, nothing)
69+
catch err
70+
if err isa MethodError
71+
error("NonLinModel does not support a time argument t in the f function, "*
72+
"see the constructor docstring for a workaround.")
73+
else
74+
rethrow()
75+
end
76+
end
6977
return nothing
7078
end
79+
h_ = ModelingToolkit.build_explicit_observed_function(io_sys, outputs; inputs)
80+
u_nothing = fill(nothing, nu)
7181
function h!(y, x, _ , p)
72-
y .= h_(x, 1, p, 1)
82+
y .= try
83+
# MTK.jl supports a `u` argument in `h_` function but not this package. We set
84+
# `u` as a vector of nothing and `h_` function will presumably throw an
85+
# MethodError it this argument is used inside the function
86+
h_(x, u_nothing, p, nothing)
87+
catch err
88+
if err isa MethodError
89+
error("NonLinModel only support strictly proper systems (no manipulated "*
90+
"input argument u in the output function h)")
91+
else
92+
rethrow()
93+
end
94+
end
7395
return nothing
7496
end
75-
return f!, h!, p, nx, vx
97+
return f!, h!, p, nu, nx, ny, vx
7698
end
7799
inputs, outputs = [mtk_model.τ], [mtk_model.y]
78-
f!, h!, p, nx, vx = generate_f_h(mtk_model, inputs, outputs)
79-
nu, ny, Ts = length(inputs), length(outputs), 0.1
100+
f!, h!, p, nu, nx, ny, vx = generate_f_h(mtk_model, inputs, outputs)
101+
Ts = 0.1
80102
vu, vy = ["\$τ\$ (Nm)"], ["\$θ\$ (°)"]
81103
nothing # hide
82104
```

0 commit comments

Comments
 (0)