Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple event/root finding support #142

Merged
merged 19 commits into from
Nov 28, 2024
Merged

Simple event/root finding support #142

merged 19 commits into from
Nov 28, 2024

Conversation

richfitz
Copy link
Member

@richfitz richfitz commented Nov 25, 2024

This PR is fairly large, but some of that is copying in the header from lostturnip (mrc-ide/lostturnip).

The idea is most easily seen in the deSolve docs: https://tpetzoldt.github.io/deSolve-forcing/deSolve-forcing.html - example 3 there. Suppose we have some system where we want to apply a change to the state of the system when it exactly hits some state - in the included example this is where a bouncing ball hits the ground (height = 0) we want to invert and reduce its velocity (so -v becomes v * 0.9).

We do this by checking at the end of every step if a root is bounded, which occurs if the sign changes over the step. Then we interpolate within that step using the ODE interpolation to find the time where the event happens and apply some "action" function.

The actions can only change the state, they cannot change parameters, because we make the assumption that parameters are constant and shared among particles. So likely use of this for some problems will involve augmenting the state with some additional variables that have d/dt = 0. That's fine though because it keeps all mutable state within the state vector.

Other than that there's not a lot more in here:

  • the root test and action functions can use shared and internal, so they can do a bunch of calculation if required
  • this means that we hold n_groups * n_threads copies of events, mirroring the structure of internal
  • we handle the special case where a single variable is of interest by creating a lambda in events.hpp

@richfitz richfitz marked this pull request as ready for review November 26, 2024 11:27
inst/include/dust2/r/continuous/system.hpp Outdated Show resolved Hide resolved
tests/testthat/test-zzz-events.R Outdated Show resolved Hide resolved
@weshinsley weshinsley merged commit e3e1b63 into main Nov 28, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants