# ModelingToolkit for (continuous) Model ID with (discrete) input and output data

**URL:** <https://discourse.julialang.org/t/modelingtoolkit-for-continuous-model-id-with-discrete-input-and-output-data/106889>\
**Category:** Modelling & Simulations\
**Tags:** optimization, modelingtoolkit\
**Created:** [November 29, 2023, 9:24am UTC](https://discourse.julialang.org/t/modelingtoolkit-for-continuous-model-id-with-discrete-input-and-output-data/106889 "2023-11-29T09:24:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rpex/32/204786_2.png) [@rpex](https://discourse.julialang.org/u/rpex)\
**Post date:** [November 29, 2023, 9:24am UTC](https://discourse.julialang.org/t/modelingtoolkit-for-continuous-model-id-with-discrete-input-and-output-data/106889/1 "2023-11-29T09:24:37Z")

</div>

I want to model a quadrocpter with MTK. In the first step I want to identify the parameters of a simple model from real flight data. In the second step I want to learn not yet modeled effects from the data by different experiments with (Neural Networks, Universal Differential Equations, …). Finally, I want to use the model to optimize controller parameters. To do this, I want to differentiate from a trajectory error through the entire model to the controller parameters.

1. Should discrete inputs be available as variables or as parameters? (Under the aspect that I later need a gradient from the continuous model into the discrete controller.)

2. I currently use a varaible because I can have the data more conveniently in the output for logging. Is there also a solution with changing parameters?  
[Update parameters in ModelingToolkit using callbacks](https://discourse.julialang.org/t/update-parameters-in-modelingtoolkit-using-callbacks/63770)

3. Is there a better solution to connect continuous with discrete than callbacks? (I have not yet tried to differentiate the structure automatically. To optimize controller parameters later.)  
[ModelingToolkit - DiscreteUpdate](https://discourse.julialang.org/t/modelingtoolkit-discreteupdate/94183)  
[Mixing time-discrete and time-continuous systems in a simulation](https://discourse.julialang.org/t/mixing-time-discrete-and-time-continuous-systems-in-a-simulation/74277)  
[Coupling MTK with time discrete control · Issue #1180 · SciML/ModelingToolkit.jl · GitHub](https://github.com/SciML/ModelingToolkit.jl/issues/1180)

_The code is shortened to show the relevant parts._

```julia
@variables (action)(t) = 0.0;

function condition(u, t, integrator)
    # Points in time at which input data is available
    t in data.timestamp
end

function affect!(integrator)
    # searches for the next input
    i = findfirst(t -> t == integrator.t, data.timestamp)
    integrator.u[1] = data.action[i]
end

cb = DiscreteCallback(condition, affect!, save_positions = (true, true))

# for logging
eqs = vcat(
    Dt(action) ~ 0, 
);

 # Stops at the points in time at which new inputs are present.
sol = solve(prob, Tsit5(), callback = cb, tstops = data.timestamp);

```

**Optimization**  
4. I have several flight trajectories. To identify the parameters, I want to apply the real actions (discrete) to the model (continuous) and calculate the trajectory deviation at the recorded discrete time points (as loss). I don’t want to do this for just one trajectory, but for all of them at the same time. My input in the `affect!(integrator)` function is taken from a global `data` source. Do I have to create a new `ODEProblem(sys, u0, tspan, p)` every time or is there a better solution?

```julia
cost_function = build_loss_objective(prob, Tsit5(), callback = cb, tstops = data.timestamp, loss(data.timestamp, data.trajectory),
                                        Optimization.AutoForwardDiff(),
                                        maxiters = 10000, verbose = false);

```

---

<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:** [November 29, 2023, 10:08am UTC](https://discourse.julialang.org/t/modelingtoolkit-for-continuous-model-id-with-discrete-input-and-output-data/106889/2 "2023-11-29T10:08:45Z")

</div>

Hello and welcome to the community 👋

> [@rpex](#):
>
> Is there a better solution to connect continuous with discrete than callbacks?

There is a plan for a better way, but this is not yet ready. See prior discussions

- [How to compose discrete-time static and dynamic models for systems simulation similar to Simulink? - #5 by baggepinnen](https://discourse.julialang.org/t/how-to-compose-discrete-time-static-and-dynamic-models-for-systems-simulation-similar-to-simulink/74858/5)
- [Slack](https://julialang.slack.com/archives/CN04R7WKE/p1700646623212169?thread_ts=1700643472.402019&cid=CN04R7WKE)

And the relevant issue label [`discrete-time`](https://github.com/SciML/ModelingToolkit.jl/issues?q=is%3Aopen+is%3Aissue+label%3Adiscrete-time)

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [November 29, 2023, 2:41pm UTC](https://discourse.julialang.org/t/modelingtoolkit-for-continuous-model-id-with-discrete-input-and-output-data/106889/3 "2023-11-29T14:41:36Z")

</div>

I have had good results for some systems using `DataInterpolations.jl` to create an interpolated function for the data and using that function at the appropriate place in the model. PiecewiseConstant interpolation is very similar to using callbacks, although Linear is easier to build a stable model around.

---

<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:** [November 29, 2023, 3:19pm UTC](https://discourse.julialang.org/t/modelingtoolkit-for-continuous-model-id-with-discrete-input-and-output-data/106889/4 "2023-11-29T15:19:19Z")

</div>

Interpolations only work if the signal is known in advance. You cannot use them in place of callbacks if the signal is, e.g., computed as a function of the simulation state. If the discrete-time input comes from a controller that acts based on the simulation state, an interpolation will not be a viable solution.
