1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use ndarray::{ArrayBase, Data, Dimension};

use crate::ode::Ode;

mod euler;
mod heun;
mod runge_kutta_4;

pub use euler::Euler;
pub use heun::Heun;
pub use runge_kutta_4::RungeKutta4;

/// A trait defining the interface of an integration method.
pub trait Stepper {
    type State: Clone;

    fn do_step<Sy>(&mut self, system: &mut Sy, state: &mut Self::State)
    where
        Sy: Ode<State = Self::State>;

    fn timestep(&self) -> f64;

    fn integrate_n_steps<Sy>(&mut self, system: &mut Sy, state: &mut Self::State, n: usize) -> f64
    where
        Sy: Ode<State = Self::State>,
    {
        let mut tacc = 0f64;;

        let dt = self.timestep();

        // Ensure t is not exceeded
        for _ in 0..n {
            self.do_step(system, state);
            tacc += dt;
        }
        tacc
    }

    fn integrate_time<Sy>(
        &mut self,
        system: &mut Sy,
        state: &mut Self::State,
        t: f64,
    ) -> (f64, usize)
    where
        Sy: Ode<State = Self::State>,
    {
        let mut tacc = 0f64;;
        let mut count = 0;

        let dt = self.timestep();

        // Ensure t is not exceeded
        while (tacc + dt) <= t {
            self.do_step(system, state);
            tacc += dt;
            count += 1;
        }
        (tacc, count)
    }
}

/// An internal marker trait to avoid trait impl conflicts.
pub trait ZipMarker {}

impl<T> ZipMarker for Vec<T> {}
impl<D: Dimension, S: Data> ZipMarker for ArrayBase<S, D> {}