Skip to main content

conspire/math/integrate/ode/explicit/fixed_step/heun/
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::{Add, Mul};
12
13/// The Heun (explicit trapezoidal) 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]] = &[&[], &[1.0]];
21    const C: &'static [Scalar] = &[0.0, 1.0];
22    const B: &'static [Scalar] = &[0.5, 0.5];
23}
24
25#[doc = include_str!("doc.md")]
26#[derive(Debug, Default)]
27pub struct Heun {
28    /// Fixed value for the time step.
29    dt: Scalar,
30}
31
32impl<Y, U> OdeIntegrator<Y, U> for Heun
33where
34    Y: Tensor,
35    U: TensorVec<Item = Y>,
36{
37}
38
39impl<T> FixedStep<T> for Heun {
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 Heun
46where
47    Y: Differentiable<T> + Tensor,
48    Derivative<Y, T>: Mul<Quantity<T>, Output = Y>,
49    for<'a> &'a Derivative<Y, T>:
50        Add<&'a Derivative<Y, T>, Output = Derivative<Y, T>> + Mul<Quantity<T>, Output = Y>,
51    U: TensorVec<Item = Y>,
52    V: TensorVec<Item = Derivative<Y, T>>,
53{
54    const SLOPES: usize = 2;
55    fn integrate(
56        &self,
57        function: impl FnMut(Quantity<T>, &Y) -> Result<Derivative<Y, T>, String>,
58        time: &[Quantity<T>],
59        initial_condition: Y,
60    ) -> Result<(Times<T>, U, V), IntegrationError> {
61        self.integrate_fixed_step(function, time, initial_condition)
62    }
63}
64
65impl<Y, U, V, T> FixedStepExplicit<Y, U, V, T> for Heun
66where
67    Y: Differentiable<T> + Tensor,
68    Derivative<Y, T>: Mul<Quantity<T>, Output = Y>,
69    for<'a> &'a Derivative<Y, T>:
70        Add<&'a Derivative<Y, T>, Output = Derivative<Y, T>> + Mul<Quantity<T>, Output = Y>,
71    U: TensorVec<Item = Y>,
72    V: TensorVec<Item = Derivative<Y, T>>,
73{
74    type Tableau = Tableau;
75}