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

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

Open HKruenaegel opened this issue 1 year ago • 6 comments

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.

HKruenaegel avatar Nov 12 '23 17:11 HKruenaegel

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

ChrisRackauckas avatar Nov 12 '23 19:11 ChrisRackauckas

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).

isaacsas avatar Nov 12 '23 20:11 isaacsas

Thanks @isaacsas . Linking the issue here: #1715

paulflang avatar Nov 12 '23 20:11 paulflang

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.

HKruenaegel avatar Nov 18 '23 17:11 HKruenaegel

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

ChrisRackauckas avatar Dec 27 '23 06:12 ChrisRackauckas

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

ChrisRackauckas avatar Feb 22 '24 13:02 ChrisRackauckas

https://github.com/SciML/ModelingToolkit.jl/pull/2911 should handle this now?

isaacsas avatar Aug 05 '24 16:08 isaacsas

It should, yes.

BenChung avatar Aug 05 '24 16:08 BenChung