Skip to main content

ordinary_diffeq/
callback.rs

1use nalgebra::SVector;
2
3use super::ode::ODE;
4
5/// A function that takes in a time and a state and outputs a single float value
6///
7/// The integration solver will check this function for zero crossings
8#[derive(Clone, Copy)]
9pub struct Callback<'a, const D: usize, P> {
10    /// The function to check for zero crossings
11    pub event: &'a dyn Fn(f64, SVector<f64, D>, &P) -> f64,
12
13    /// The function to change the ODE
14    pub effect: &'a dyn Fn(&mut ODE<D, P>),
15}
16
17/// A convenience function for stopping the integration
18pub fn stop<const D: usize, P>(ode: &mut ODE<D, P>) {
19    ode.t_end = ode.t;
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25
26    #[test]
27    fn test_basic_callbacks() {
28        type Params = ();
29        let _value_too_high = Callback {
30            event: &|_: f64, y: SVector<f64, 3>, _p: &Params| 10.0 - y[0],
31            effect: &stop,
32        };
33    }
34}