use ndarray::*;
use ndarray_linalg::*;
use num_traits::Float;
#[cfg(doc)]
use crate::{explicit::*, ode::*, semi_implicit::*};
#[cfg_attr(doc, katexit::katexit)]
pub trait ModelSpec: Clone {
type Scalar: Scalar;
type Dim: Dimension;
fn model_size(&self) -> <Self::Dim as Dimension>::Pattern;
}
pub trait TimeStep {
type Time: Scalar + Float;
fn get_dt(&self) -> Self::Time;
fn set_dt(&mut self, dt: Self::Time);
}
#[cfg_attr(doc, katexit::katexit)]
pub trait Explicit: ModelSpec {
fn rhs<'a, S>(&mut self, x: &'a mut ArrayBase<S, Self::Dim>) -> &'a mut ArrayBase<S, Self::Dim>
where
S: DataMut<Elem = Self::Scalar>;
}
#[cfg_attr(doc, katexit::katexit)]
pub trait SemiImplicit: ModelSpec {
fn nlin<'a, S>(
&mut self,
x: &'a mut ArrayBase<S, Self::Dim>,
) -> &'a mut ArrayBase<S, Self::Dim>
where
S: DataMut<Elem = Self::Scalar>;
fn diag(&self) -> Array<Self::Scalar, Self::Dim>;
}
pub trait TimeEvolution: ModelSpec + TimeStep {
fn iterate<'a, S>(
&mut self,
x: &'a mut ArrayBase<S, Self::Dim>,
) -> &'a mut ArrayBase<S, Self::Dim>
where
S: DataMut<Elem = Self::Scalar>;
fn iterate_n<'a, S>(
&mut self,
a: &'a mut ArrayBase<S, Self::Dim>,
n: usize,
) -> &'a mut ArrayBase<S, Self::Dim>
where
S: DataMut<Elem = Self::Scalar>,
{
for _ in 0..n {
self.iterate(a);
}
a
}
}
pub trait Scheme: TimeEvolution {
type Core: ModelSpec<Scalar = Self::Scalar, Dim = Self::Dim>;
fn new(f: Self::Core, dt: Self::Time) -> Self;
fn core(&self) -> &Self::Core;
fn core_mut(&mut self) -> &mut Self::Core;
}