# ModelingToolkit - DiscreteUpdate

**URL:** <https://discourse.julialang.org/t/modelingtoolkit-discreteupdate/94183>\
**Category:** Modelling & Simulations\
**Created:** [February 7, 2023, 3:11am UTC](https://discourse.julialang.org/t/modelingtoolkit-discreteupdate/94183 "2023-02-07T03:11:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jondavis847](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jondavis847](https://discourse.julialang.org/u/jondavis847)\
**Post date:** [February 7, 2023, 3:11am UTC](https://discourse.julialang.org/t/modelingtoolkit-discreteupdate/94183/1 "2023-02-07T03:11:51Z")

</div>

Hello. I’m trying to implement discrete control in MTK using the DiscreteUpdate operator. It failed to update on my more complex model, seems to fail for a very basic MWE as well.

```julia
using ModelingToolkit, DifferentialEquations

@variables t x(t)=0 y(t)=0
D = Differential(t)
U = DiscreteUpdate(t; dt = 1)

eqs = [D(x) ~ 1
       U(y) ~ x]

@named de = ODESystem(eqs,t)
sys = structural_simplify(de)
prob = ODEProblem(sys,[],(0,10))
sol = solve(prob)

```

Results in

```julia
julia> sol[x]
7-element Vector{Float64}:
 0.0
 9.999999999999996e-5
 0.0010999999999999996
 0.011099999999999994
 0.11109999999999993
 1.1110999999999993
 9.999999999999996

julia> sol[y]
7-element Vector{Int64}:
 0
 0
 0
 0
 0
 0
 0

```

Am I doing something wrong?

I found [Coupling MTK with time discrete control · Issue #1180 · SciML/ModelingToolkit.jl · GitHub](https://github.com/SciML/ModelingToolkit.jl/issues/1180)

Looks like a workaround using PeriodicCallback was found back in 2021 but is this still the best way to implement discrete control in MTK? What would that look like for models that compose multiple ODESystems together, some discrete and some continuous, since the callback is set at the ODEProblem or solve() level?

I have a similar model developed with only DifferentialEquations and a PeriodicCallback, but MTK with DiscreteUpdate seems like a much better long term solution.

Thanks!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 7, 2023, 11:01am UTC](https://discourse.julialang.org/t/modelingtoolkit-discreteupdate/94183/2 "2023-02-07T11:01:39Z")

</div>

Open an issue, the mixed discrete stuff still has a few things WIP so any non-working example is useful…

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 7, 2023, 11:39am UTC](https://discourse.julialang.org/t/modelingtoolkit-discreteupdate/94183/3 "2023-02-07T11:39:13Z")

</div>

We’ve been working on and off for the last couple of months adding support for hybrid continuous/discrete systems. The functionality is still not in a usable state and is as a consequence of that not yet documented. You can get a feeling for the current capabilities by looking at this test file, there are a number of increasingly more complicated hybrid systems

> <https://github.com/SciML/ModelingToolkit.jl/blob/master/test/clock.jl#L17>

Please consider this functionality experimental at this stage, and subject to breaking changes. Here’s a [TODO list](https://github.com/SciML/ModelingToolkit.jl/issues/1939) for things that needs to fall into place before this stuff is properly usable.

---

<div class="post-metadata">

**Author:** ![jondavis847](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jondavis847](https://discourse.julialang.org/u/jondavis847)\
**Post date:** [February 7, 2023, 7:04pm UTC](https://discourse.julialang.org/t/modelingtoolkit-discreteupdate/94183/4 "2023-02-07T19:04:24Z")

</div>

Thank you gentlemen for the response! Glad to hear it’s on the radar and being worked. Looks like Sample(t,dt)(f) is the functionality I’m looking for, though it looks like it still needs some work. Perhaps I’ll just consider DiscreteUpdate OBE and update issue #1180 with any findings I have from experimenting with Sample? Or maybe just have patience and let you guys work your magic.
