Skip to content

Commit

Permalink
Rewrite TimeEvolutionBuffered
Browse files Browse the repository at this point in the history
  • Loading branch information
termoshtt committed Jul 25, 2017
1 parent 1d8c8d7 commit 43aa64e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/bin/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ fn main() {
let dt = 0.01;
let eom = model::Lorenz63::default();
let teo = explicit::rk4(eom, dt);
let mut x = arr1(&[1.0, 0.0, 0.0]);
let mut buf = explicit::RK4Buffer::new_buffer(&teo);
let mut x: Array1<f64> = arr1(&[1.0, 0.0, 0.0]);
for _ in 0..100_000_000 {
teo.iterate(&mut x);
teo.iterate_buf(&mut x, &mut buf);
}
}
29 changes: 18 additions & 11 deletions src/explicit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,34 @@ pub struct RK4Buffer<A, D> {
k3: Array<A, D>,
}

impl<A, S, D, F> TimeEvolutionBuffered<S, D> for RK4<F, F::Time>
impl<A, D> RK4Buffer<A, D>
where A: Scalar,
D: Dimension
{
pub fn new_buffer<T>(t: &T) -> RK4Buffer<A, D>
where T: ModelSize<D>
{
RK4Buffer {
x: Array::zeros(t.model_size()),
k1: Array::zeros(t.model_size()),
k2: Array::zeros(t.model_size()),
k3: Array::zeros(t.model_size()),
}
}
}

impl<A, S, D, F> TimeEvolutionBuffered<S, D, RK4Buffer<A, D>> for RK4<F, F::Time>
where A: Scalar,
S: DataMut<Elem = A>,
D: Dimension,
F: Explicit<S, D, Time = A::Real, Scalar = A>
{
type Scalar = F::Scalar;
type Buffer = RK4Buffer<A, D>;

fn get_buffer(&self) -> Self::Buffer {
RK4Buffer {
x: Array::zeros(self.model_size()),
k1: Array::zeros(self.model_size()),
k2: Array::zeros(self.model_size()),
k3: Array::zeros(self.model_size()),
}
}

fn iterate_buf<'a>(&self,
mut x: &'a mut ArrayBase<S, D>,
mut buf: &mut Self::Buffer)
mut buf: &mut RK4Buffer<A, D>)
-> &'a mut ArrayBase<S, D> {
let dt = self.dt;
let dt_2 = self.dt * into_scalar(0.5);
Expand Down
25 changes: 10 additions & 15 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,6 @@ pub trait TimeEvolutionBase<S, D>: ModelSize<D> + TimeStep
fn iterate<'a>(&self, &'a mut ArrayBase<S, D>) -> &'a mut ArrayBase<S, D>;
}

/// Time-evolution operator with buffer
pub trait TimeEvolutionBuffered<S, D>: ModelSize<D> + TimeStep
where S: DataMut,
D: Dimension
{
type Scalar: Scalar;
type Buffer;
fn get_buffer(&self) -> Self::Buffer;
/// calculate next step
fn iterate_buf<'a>(&self,
&'a mut ArrayBase<S, D>,
&mut Self::Buffer)
-> &'a mut ArrayBase<S, D>;
}

pub trait TimeEvolution<A, D>
: TimeEvolutionBase<OwnedRepr<A>, D, Scalar = A, Time = A::Real>
+ TimeEvolutionBase<OwnedRcRepr<A>, D, Scalar = A, Time = A::Real>
Expand All @@ -68,3 +53,13 @@ pub trait TimeEvolution<A, D>
D: Dimension
{
}

/// Time-evolution operator with buffer
pub trait TimeEvolutionBuffered<S, D, Buffer>: ModelSize<D> + TimeStep
where S: DataMut,
D: Dimension
{
type Scalar: Scalar;
/// calculate next step
fn iterate_buf<'a>(&self, &'a mut ArrayBase<S, D>, &mut Buffer) -> &'a mut ArrayBase<S, D>;
}

0 comments on commit 43aa64e

Please sign in to comment.