numeric_algs/symplectic/integration/
neri.rs

1use super::{super::State, Integrator, StepSize};
2
3pub struct NeriIntegrator {
4    default_step: f64,
5}
6
7impl NeriIntegrator {
8    pub fn new(step_size: f64) -> Self {
9        Self {
10            default_step: step_size,
11        }
12    }
13
14    pub fn set_default_step(&mut self, step: f64) {
15        self.default_step = step;
16    }
17}
18
19const CBRT2: f64 = 1.25992104989487316; // cube root of 2
20
21// Algorithm constants
22const C1: f64 = 0.5 / (2.0 - CBRT2);
23const C2: f64 = (1.0 - CBRT2) / 2.0 / (2.0 - CBRT2);
24const C3: f64 = (1.0 - CBRT2) / 2.0 / (2.0 - CBRT2);
25const C4: f64 = 0.5 / (2.0 - CBRT2);
26
27const D1: f64 = 1.0 / (2.0 - CBRT2);
28const D2: f64 = -CBRT2 / (2.0 - CBRT2);
29const D3: f64 = 1.0 / (2.0 - CBRT2);
30
31impl<S: State> Integrator<S> for NeriIntegrator {
32    fn propagate_in_place<DF1, DF2>(
33        &mut self,
34        start: &mut S,
35        pos_diff_eq: DF1,
36        momentum_diff_eq: DF2,
37        step_size: StepSize,
38    ) where
39        DF1: Fn(&S) -> S::PositionDerivative,
40        DF2: Fn(&S) -> S::MomentumDerivative,
41    {
42        let h = match step_size {
43            StepSize::UseDefault => self.default_step,
44            StepSize::Step(x) => x,
45        };
46
47        start.shift_position_in_place(&pos_diff_eq(start), h * C1);
48        start.shift_momentum_in_place(&momentum_diff_eq(start), h * D1);
49        start.shift_position_in_place(&pos_diff_eq(start), h * C2);
50        start.shift_momentum_in_place(&momentum_diff_eq(start), h * D2);
51        start.shift_position_in_place(&pos_diff_eq(start), h * C3);
52        start.shift_momentum_in_place(&momentum_diff_eq(start), h * D3);
53        start.shift_position_in_place(&pos_diff_eq(start), h * C4);
54    }
55}