ModelingToolkit.jl
ModelingToolkit.jl copied to clipboard
Failure to infer clock with variable appearing in multiple partitions
The model below throws a
julia> @mtkbuild model = MyMultirateModel()
7-element Vector{Any}:
ud2(t)
ud1(t)
Sample(Clock(t, 0.3))(y(t))
yd1(t)
Sample(ModelingToolkit.InferredDiscrete())(r(t))
Sample(Clock(t, 1.1))(y(t))
yd2(t)
ERROR: ClockInferenceException: Clocks are not consistent in connected component Any[ud2(t), ud1(t), Sample(Clock(t, 0.3))(y(t)), yd1(t), Sample(ModelingToolkit.InferredDiscrete())(r(t)), Sample(Clock(t, 1.1))(y(t)), yd2(t)]
The problem is likely that a Sample(r)
appears in two different clock partitions. If I specify the clock explicitly in one of the partitions, the other one is inferred correctly, like this
ud1 ~ kp1 * (Sample(r) - yd1)
ud2 ~ kp2 * (Sample(t, Ts2)(r) - yd2)
but both cannot be inferred at the same time.
I would guess that both calls to Sample(r)
produce two objects that are determined to be equivalent, which shouldn't be the case. If they are unique, one can infer that they belong to different clock partitions through their interactions with the variables
yd1 ~ Sample(t, Ts1)(y)
yd2 ~ Sample(t, Ts2)(y)
using ModelingToolkit, OrdinaryDiffEq, Plots
using ModelingToolkit: t_nounits as t, D_nounits as D
Ts1 = 0.3
Ts2 = 1.1
k = ShiftIndex()
@mtkmodel MyMultirateModel begin
@variables begin
x(t) = -1
y(t)
u(t)
yd1(t)
ud1(t)
yd2(t)
ud2(t)
e(t)
r(t)
end
@parameters begin
kp1 = 1
kp2 = 2
end
@equations begin
# plant 🚗 (continuous-time part)
u ~ Hold(ud1) + Hold(ud2)
D(x) ~ -x + u
y ~ x
r ~ 1
# Controllers 💻 💻 (discrete-time parts)
yd1 ~ Sample(t, Ts1)(y)
ud1 ~ kp1 * (Sample(r) - yd1)
yd2 ~ Sample(t, Ts2)(y)
ud2 ~ kp2 * (Sample(r) - yd2)
end
end
@mtkbuild model = MyMultirateModel()