ModelingToolkit.jl icon indicating copy to clipboard operation
ModelingToolkit.jl copied to clipboard

Feature Request: allow references to integrator `dt` in `ODESystem`, automatically generate callback

Open xtalax opened this issue 2 years ago • 7 comments

As in the title, I'm faced with a problem implementing flux limiters in MethodOfLines where I need to apply a callback step_limiter!(uarr, integrator, p, t) to handle situations where the timestep dt appears in a discretization scheme, and therefore in the system of equations.

I will essentially be forced to implement this feature which would need to reuse a lot of MTK code in MOL. I would need to copy much of structural_simplify to make sure that the callback is applied to the correct elements of uarr. So in the interests of reuse I request that this feature be allowed to go in MTK.

This would take allowing an additional symbolic argument div to ODESystem which will be bound to integrator.dt on generation of the callback. In the call to ODEProblem, terms in the equations which contain references to div will be removed and equations to additively update the correct variables with these terms in the callback will be generated. Of course this adds the restriction that these terms do not contain references to the time derivative, but that is ok for my use case.

Finally, the callback function will be generated, and stored in the ODEProblem.

How does this sound? Is there a much easier way of achieving the same thing?

I'm happy to implement this, but would appreciate ideas/assistance.

cc @ChrisRackauckas

xtalax avatar May 17 '22 16:05 xtalax

@YingboMa

ChrisRackauckas avatar May 20 '22 04:05 ChrisRackauckas

I thought we already have something similar for the discrete callback thing. @baggepinnen

YingboMa avatar May 20 '22 16:05 YingboMa

The symbolic callbacks are generated based on additional information provided to ODESystems and these callbacks have machinery to handle state indices etc. so you might be able to reuse some of that functionality. I can't say that I fully understand all the details of what is discussed here, would you call ODESystem(..., div=symbolic_dt) where symbolic_dt is a regular @parameter that appears in equations, and this parameter is substituted for the integrator time step during solving? The callback appears because the time step is continuously changing?

baggepinnen avatar May 20 '22 19:05 baggepinnen

I'm not sure I understand the issue either, but if it could help @shashi had previously setup the ability to use integrators as part of build_function constructed callbacks for jump affect functions, see

https://github.com/SciML/ModelingToolkit.jl/blob/490dc28b35c9cc804fc6e79fc892af4ac207d5b9/src/systems/jumps/jumpsystem.jl#L115-L128

isaacsas avatar May 20 '22 19:05 isaacsas

@baggepinnen Yes that's exactly right.

xtalax avatar May 20 '22 20:05 xtalax

I'm not sure I understand the issue either, but if it could help @shashi had previously setup the ability to use integrators as part of build_function constructed callbacks for jump affect functions, see

https://github.com/SciML/ModelingToolkit.jl/blob/490dc28b35c9cc804fc6e79fc892af4ac207d5b9/src/systems/jumps/jumpsystem.jl#L115-L128

If I can make a symbolic integrator, and pass its dt through ODESystem to ODEFunction via ODEProblem That could work, right?

xtalax avatar May 20 '22 20:05 xtalax

just add a function on top that passes dt = integrator.dt in and symbolically generates the function directly on the arguments.

ChrisRackauckas avatar Jul 06 '22 20:07 ChrisRackauckas