SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.38k stars 196 forks source link

No documented way to set rootfind=RightRootFind in continuous Event. #2351

Open HKruenaegel opened 7 months ago

HKruenaegel commented 7 months ago

In DifferentialEquations.jl one can specify the rootfind option:

ContinuousCallback(condition,affect!,affect_neg!;
                   initialize = INITIALIZE_DEFAULT,
                   finalize = FINALIZE_DEFAULT,
                   idxs = nothing,
                   rootfind=LeftRootFind,
                   save_positions=(true,true),
                   interp_points=10,
                   abstol=10eps(),reltol=0,repeat_nudge=1//100)

In ModelingToolkit.jl it is not obvious how to do the same. Now I'm using the workaround, that I use the differentialequations.jl callback interface for setting rootfind=RightRootFind. But it would be nice to use the symbolic interface from ModelingToolkit.jl.

ChrisRackauckas commented 7 months ago

Good point, we should add that. It wouldn't be hard.

isaacsas commented 7 months ago

Generally the symbolic events are missing a lot of the normal event features. There is another issue about directionality for continuous events that @paulflang wanted for SBML model support, and for discrete events it would be nice to have a way to include symbolic tstops involving parameter expressions with the event (i.e. that get forwarded along and handled appropriately in the solvers, so one doesn't have to manually specify them).

paulflang commented 7 months ago

Thanks @isaacsas . Linking the issue here: #1715

HKruenaegel commented 7 months ago

Inspired of your "good first issue" tag, I have a first working example for this issue. In this I only give the to parameters I need to the continous event:

continuous_events = [   (([ϕ1-ϕ2~0]   => (affect!, [ϕ1,ϕ2,ϕ3,ϕ4],[Rd1,Rd2,Rd3,Rd4], 1))    =>[SciMLBase.RightRootFind,20])
                        (([ϕ4-ϕ2~0]   => (affect!, [ϕ1,ϕ2,ϕ3,ϕ4],[Rd1,Rd2,Rd3,Rd4], 2))    =>[SciMLBase.RightRootFind,20])
                        (([-ϕ1~0]     => (affect!, [ϕ1,ϕ2,ϕ3,ϕ4],[Rd1,Rd2,Rd3,Rd4], 3))    =>[SciMLBase.RightRootFind,20])
                        (([-ϕ4~0]     => (affect!, [ϕ1,ϕ2,ϕ3,ϕ4],[Rd1,Rd2,Rd3,Rd4], 4))    =>[SciMLBase.RightRootFind,20])]

Which is the rootfind-option and the interp_points-option.

Since the options are only given once to the VectorContinuousCallback function, it makes of course no sense to give the options for every single callback. So this in mind and to have a more general solution, the question is now, what to do. Maybe something like this:

continuous_events = [  ([ϕ1-ϕ2~0]   => (affect!, [ϕ1,ϕ2,ϕ3,ϕ4],[Rd1,Rd2,Rd3,Rd4], 1))
                        ([ϕ4-ϕ2~0]   => (affect!, [ϕ1,ϕ2,ϕ3,ϕ4],[Rd1,Rd2,Rd3,Rd4], 2))    
                        ([-ϕ1~0]     => (affect!, [ϕ1,ϕ2,ϕ3,ϕ4],[Rd1,Rd2,Rd3,Rd4], 3))    
                        ([-ϕ4~0]     => (affect!, [ϕ1,ϕ2,ϕ3,ϕ4],[Rd1,Rd2,Rd3,Rd4], 4))]    =>["rootfind"=>SciMLBase.RightRootFind,"interp_points"=>20]

Or are there better possibilities?

PS.: Another question rised on the way. I use want to use a discrete event that fires at a time which is set by the continous events resp. the affect! function. In the differentialequations.jl implementation I used a global variable to handover the time and then call the same affect! function. But since I can't define a condition function manually for the discrete event, I can't do that anymore. And I can also not mix a differentialequations.jl discrete callback with continous events since the affect! wont work anymore because of the missing symbolic definitions.

PPS.: What I described last doesn't really matter because I can do want I want with another way. But maybe it is interesting despite that to have a alternative way to define the condition function manually.

ChrisRackauckas commented 6 months ago

This seems like it ended up being a more difficult issue than I imagined.

ChrisRackauckas commented 4 months ago

https://github.com/SciML/ModelingToolkit.jl/issues/2225 is related.