Skip to main content

conspire/math/integrate/ode/explicit/fixed_step/midpoint/
mod.rs

1#[cfg(test)]
2mod test;
3
4use crate::math::{
5    Derivative, Differentiable, Quantity, Scalar, Tensor, TensorVec,
6    integrate::{
7        ButcherTableau, Explicit, FixedStep, FixedStepExplicit, IntegrationError, OdeIntegrator,
8        Times,
9    },
10};
11use std::ops::Mul;
12
13/// The explicit midpoint tableau.
14#[derive(Debug)]
15pub struct Tableau;
16
17impl ButcherTableau for Tableau {
18    const STAGES: usize = 2;
19    const ORDER: Scalar = 2.0;
20    const A: &'static [&'static [Scalar]] = &[&[], &[0.5]];
21    const C: &'static [Scalar] = &[0.0, 0.5];
22    const B: &'static [Scalar] = &[0.0, 1.0];
23}
24
25#[doc = include_str!("doc.md")]
26#[derive(Debug, Default)]
27pub struct Midpoint {
28    /// Fixed value for the time step.
29    dt: Scalar,
30}
31
32impl<Y, U> OdeIntegrator<Y, U> for Midpoint
33where
34    Y: Tensor,
35    U: TensorVec<Item = Y>,
36{
37}
38
39impl<T> FixedStep<T> for Midpoint {
40    fn dt(&self) -> Quantity<T> {
41        Quantity::new(self.dt)
42    }
43}
44
45impl<Y, U, V, T> Explicit<Y, U, V, T> for Midpoint
46where
47    Y: Differentiable<T> + Tensor,
48    for<'a> &'a Derivative<Y, T>: Mul<Quantity<T>, Output = Y>,
49    U: TensorVec<Item = Y>,
50    V: TensorVec<Item = Derivative<Y, T>>,
51{
52    const SLOPES: usize = 2;
53    fn integrate(
54        &self,
55        function: impl FnMut(Quantity<T>, &Y) -> Result<Derivative<Y, T>, String>,
56        time: &[Quantity<T>],
57        initial_condition: Y,
58    ) -> Result<(Times<T>, U, V), IntegrationError> {
59        self.integrate_fixed_step(function, time, initial_condition)
60    }
61}
62
63impl<Y, U, V, T> FixedStepExplicit<Y, U, V, T> for Midpoint
64where
65    Y: Differentiable<T> + Tensor,
66    for<'a> &'a Derivative<Y, T>: Mul<Quantity<T>, Output = Y>,
67    U: TensorVec<Item = Y>,
68    V: TensorVec<Item = Derivative<Y, T>>,
69{
70    type Tableau = Tableau;
71}