ODE stepping with discrete input

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.

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?

Have you read:

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:

1 Like

Have you tried using ModelingToolkitInputs.jl? 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.

If you want to use something more lightweight without using DiffEq or ModelingToolkit, try SeeToDee.jl

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.

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.