# ODE stepping with discrete input

**URL:** <https://discourse.julialang.org/t/ode-stepping-with-discrete-input/136104>\
**Category:** Modelling & Simulations\
**Tags:** question, diffeq\
**Created:** [March 9, 2026, 7:56pm UTC](https://discourse.julialang.org/t/ode-stepping-with-discrete-input/136104 "2026-03-09T19:56:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [March 9, 2026, 7:56pm UTC](https://discourse.julialang.org/t/ode-stepping-with-discrete-input/136104/1 "2026-03-09T19:56:55Z")

</div>

Hi I am using DiffEq with manual solver stepping and piecewise constant inputs.

This image shows an integrator  
\frac{dx}{dt} = u  
with a switching input 0 or 1.  
Hence the state should never decrease, but it does.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/f/af43b2a6a35539385721a8e5642a865239a0de45.png)

```julia
OrdinaryDiffEq.step!(integrator, t - current_t, true)

```

I supply `u` to the right-hand side function via an object that I change before calling `step!`.  
I know the solver cannot know that it changed, but I only do so at the step.

So now my question, how to do this properly?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [March 9, 2026, 9:39pm UTC](https://discourse.julialang.org/t/ode-stepping-with-discrete-input/136104/2 "2026-03-09T21:39:25Z")

</div>

Have you read:

> **[Note about mutating - Integrator Interface · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/basics/integrator/#Note-about-mutating)**
>
> The integrator interface gives one the ability to interactively step through the numerical solving of a differential equation. Through this interface, one can easily monitor results, modify the problem during a run, and dynamically continue solving...

It seems like you should use the `set_u!` function to update the state.

Alternatively you could use the DiscreteCallback mechanism in combination with setting `tstops` to implement your ODE I think:

> **[The Callback Types - Event Handling and Callback Functions ·...](https://docs.sciml.ai/DiffEqDocs/stable/features/callback_functions/#The-Callback-Types)**
>
> DifferentialEquations.jl allows for using callback functions to inject user code into the solver algorithms. It allows for safely and accurately applying events and discontinuities. Multiple callbacks can be chained together, and these callback types...

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [March 10, 2026, 1:01am UTC](https://discourse.julialang.org/t/ode-stepping-with-discrete-input/136104/3 "2026-03-10T01:01:41Z")

</div>

Have you tried using [ModelingToolkitInputs.jl](https://bradcarman.github.io/ModelingToolkitInputs.jl/dev/#Indeterminate-Form:-Using-ModelingToolkit-with-Streaming-Data)? This should give you the functionality your after and set the correct hooks for the integrator to make the correct steps. The demo problem in the docs is exactly this ODE.

---

<div class="post-metadata">

**Author:** ![okartal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/okartal/32/44818_2.png) [@okartal](https://discourse.julialang.org/u/okartal)\
**Post date:** [March 10, 2026, 1:19pm UTC](https://discourse.julialang.org/t/ode-stepping-with-discrete-input/136104/4 "2026-03-10T13:19:27Z")

</div>

If you want to use something more lightweight without using DiffEq or ModelingToolkit, try [SeeToDee.jl](https://github.com/baggepinnen/SeeToDee.jl)

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [March 17, 2026, 12:52pm UTC](https://discourse.julialang.org/t/ode-stepping-with-discrete-input/136104/5 "2026-03-17T12:52:25Z")

</div>

The ModelingToolkitInputs approach uses callbacks. If do that too and use a periodic callback, do I need to mark the integrator with `u_modified` ?  
The model now runs twice as slow. Maybe a discrete solver that handles the inputs directly is the better approach after all.

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [March 17, 2026, 10:32pm UTC](https://discourse.julialang.org/t/ode-stepping-with-discrete-input/136104/6 "2026-03-17T22:32:03Z")

</div>

You should be able to have additional callbacks like a periodic callback mixed with ModelingToolkitInputs.jl and this shouldn’t be causing any slowdowns. My understanding is that if a discontinuity occurred then u\_modified should be set to true and false otherwise. If it’s possible to provide a MWE I would love to investigate further.
