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

use ndarray::{RcArray, Dimension, LinalgScalar};
use std::ops::*;

/// Equation of motion (EOM)
pub trait EOM<A, D>
    where D: Dimension
{
    /// calculate right hand side (rhs) of EOM from current state
    fn rhs(&self, RcArray<A, D>) -> RcArray<A, D>;
}

/// Stiff equation with diagonalized linear part
pub trait StiffDiag<A, D>
    where D: Dimension
{
    /// Non-Linear part of EOM
    fn nonlinear(&self, RcArray<A, D>) -> RcArray<A, D>;
    /// Linear part of EOM (assume to be diagonalized)
    fn linear_diagonal(&self) -> RcArray<A, D>;
}

impl<A, D, F> EOM<A, D> for F
    where F: StiffDiag<A, D>,
          A: LinalgScalar,
          D: Dimension
{
    fn rhs(&self, x: RcArray<A, D>) -> RcArray<A, D> {
        let nlin = self.nonlinear(x.clone());
        let a = self.linear_diagonal();
        nlin + a * x
    }
}

/// Time-evolution operator
pub trait TimeEvolution<A, D>
    where D: Dimension
{
    /// calculate next step
    fn iterate(&self, RcArray<A, D>) -> RcArray<A, D>;
    fn get_dt(&self) -> f64;
}

/// utility trait for easy implementation
pub trait OdeScalar<R: Ring>: LinalgScalar + RMod<R> {}
impl<A, R: Ring> OdeScalar<R> for A where A: LinalgScalar + RMod<R> {}

/// Ring (math)
pub trait Ring
    : Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Sized {
}
impl<A> Ring for A where A: Add<Output = A> + Sub<Output = A> + Mul<Output = A> + Sized {}

/// R-module
pub trait RMod<R: Ring>: Mul<R, Output = Self> + Sized {}
impl<A, R: Ring> RMod<R> for A where A: Mul<R, Output = A> + Sized {}