Does anyone what’s currently the best way in the Julia ecosystem to mix time-continuous and time-discrete systems in one simulation? (e.g. I want to simulate a continuous system regulated by time-discrete controller). I managed to implement it using DifferentialEquations.jl and Callbacks that modify the parameter struct that is passed to the equation.
However it is not the most efficient way (in terms of implementation effort) and I was wondering if there are better ways to do that.
DiffEq+Callbacks is what it would all lower to. The easier way to do it would be ModelingToolkit.jl, but the capability is still quite fresh and not too documented right now (also still a bit buggy). That will definitely be the future, but I would probably still recommend manual writing of callbacks for now unless you’re looking for an adventure.
This idea is being used in the AOCS simulator of the Amazonia-1, a satellite built by the National Institute of Space Research (INPE). The data obtained after launch showed that the accuracy is superb, and the execution time is amazingly small given how many things are simulated.
For now I solved it by modifying the integrator parameter struct, and using it to keep track of the time-discrete control state (which is in turn modified each PeriodicCallback Interval).
Are there any (obvious or non-obvious) problems with this approach, which might lead to wrong results (e.g. like not using full_cache)? I currently see a slight discrepancy between the result I get in Simulink and the one I get in Julia (where the Julia solution has more noise/variation around the steady state).
A little bit more details about the problem: The continuous system is actuated via a PWM where the duty cycle is controlled via time-discrete controller. The PWM therefore produces a discontinuity at each iteration and to make sure, that the solver doesn’t skip it, I use a fixed step size of 1/512 of the switching period.
This kind of behavior seems more likely related to the solver configurations in Simulink. AFAIK, MATLAB uses by default some automatic algorithm for absolute or relative tolerances. Did you configure MATLAB with the same parameters and solver you used in Julia?