From e25ee9c74abb20505235f6aa6c7662743ab3bb29 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Thu, 15 Jul 2021 17:25:30 +0200 Subject: [PATCH] Prefer clone_from over assign --- euler/src/lib.rs | 14 ++++++++++---- maxwell/src/lib.rs | 14 ++++++++++---- multigrid/src/file.rs | 3 +-- multigrid/src/main.rs | 12 +----------- shallow_water/src/lib.rs | 14 ++++++++++---- utils/integrate/src/lib.rs | 16 ++++++---------- 6 files changed, 38 insertions(+), 35 deletions(-) diff --git a/euler/src/lib.rs b/euler/src/lib.rs index b1b6242..32e299c 100644 --- a/euler/src/lib.rs +++ b/euler/src/lib.rs @@ -185,10 +185,19 @@ impl System { } } -#[derive(Clone, Debug)] +#[derive(Debug)] /// A 4 x ny x nx array pub struct Field(pub(crate) Array3); +impl Clone for Field { + fn clone(&self) -> Self { + Self(self.0.clone()) + } + fn clone_from(&mut self, source: &Self) { + self.0.clone_from(&source.0) + } +} + #[derive(Clone, Debug)] /// A 4 x ny x nx array pub struct Diff(pub(crate) Array3); @@ -197,9 +206,6 @@ impl integrate::Integrable for Field { type State = Field; type Diff = Diff; - fn assign(s: &mut Self::State, o: &Self::State) { - s.0.assign(&o.0); - } fn scaled_add(s: &mut Self::State, o: &Self::Diff, scale: Float) { s.0.scaled_add(scale, &o.0); } diff --git a/maxwell/src/lib.rs b/maxwell/src/lib.rs index 323a1be..8973c93 100644 --- a/maxwell/src/lib.rs +++ b/maxwell/src/lib.rs @@ -7,16 +7,22 @@ use sbp::Float; #[cfg(feature = "sparse")] pub mod sparse; -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct Field(pub(crate) Array3); +impl Clone for Field { + fn clone(&self) -> Self { + Self(self.0.clone()) + } + fn clone_from(&mut self, source: &Self) { + self.0.clone_from(&source.0) + } +} + impl integrate::Integrable for Field { type State = Field; type Diff = Field; - fn assign(s: &mut Self::State, o: &Self::State) { - s.0.assign(&o.0); - } fn scaled_add(s: &mut Self::State, o: &Self::Diff, scale: Float) { s.0.scaled_add(scale, &o.0); } diff --git a/multigrid/src/file.rs b/multigrid/src/file.rs index 0783285..6be9569 100644 --- a/multigrid/src/file.rs +++ b/multigrid/src/file.rs @@ -38,8 +38,7 @@ impl OutputThread { match self.rx.as_ref().unwrap().try_recv() { Ok(mut copy_fields) => { for (from, to) in fields.iter().zip(copy_fields.iter_mut()) { - use integrate::Integrable; - euler::Field::assign(to, from); + to.clone_from(&from); } self.tx .as_ref() diff --git a/multigrid/src/main.rs b/multigrid/src/main.rs index a5e18c0..c3b3894 100644 --- a/multigrid/src/main.rs +++ b/multigrid/src/main.rs @@ -30,17 +30,7 @@ pub(crate) static MULTITHREAD: AtomicBool = AtomicBool::new(false); impl integrate::Integrable for System { type State = Vec; type Diff = Vec; - fn assign(s: &mut Self::State, o: &Self::State) { - if MULTITHREAD.load(Ordering::Acquire) { - s.par_iter_mut() - .zip(o.par_iter()) - .for_each(|(s, o)| euler::Field::assign(s, o)) - } else { - s.iter_mut() - .zip(o.iter()) - .for_each(|(s, o)| euler::Field::assign(s, o)) - } - } + fn scaled_add(s: &mut Self::State, o: &Self::Diff, scale: Float) { if MULTITHREAD.load(Ordering::Acquire) { s.par_iter_mut() diff --git a/shallow_water/src/lib.rs b/shallow_water/src/lib.rs index fe8bd18..32bad68 100644 --- a/shallow_water/src/lib.rs +++ b/shallow_water/src/lib.rs @@ -6,16 +6,22 @@ use characteristics::{Aminus, Aplus, Bminus, Bplus}; const G: Float = 1.0; -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct Field(Array3); +impl Clone for Field { + fn clone(&self) -> Self { + Self(self.0.clone()) + } + fn clone_from(&mut self, source: &Self) { + self.0.clone_from(&source.0) + } +} + impl integrate::Integrable for Field { type State = Field; type Diff = Field; - fn assign(s: &mut Self::State, o: &Self::State) { - s.0.assign(&o.0); - } fn scaled_add(s: &mut Self::State, o: &Self::Diff, scale: Float) { s.0.scaled_add(scale, &o.0); } diff --git a/utils/integrate/src/lib.rs b/utils/integrate/src/lib.rs index d551192..3785a77 100644 --- a/utils/integrate/src/lib.rs +++ b/utils/integrate/src/lib.rs @@ -152,10 +152,10 @@ impl EmbeddedButcherTableau for BogackiShampine { } pub trait Integrable { - type State; + /// This type should support `clone_from` + type State: Clone; type Diff; - fn assign(s: &mut Self::State, o: &Self::State); fn scaled_add(s: &mut Self::State, o: &Self::Diff, scale: Float); } @@ -186,11 +186,11 @@ pub fn integrate( let simtime; match i { 0 => { - F::assign(fut, prev); + fut.clone_from(prev); simtime = *time; } i if i < BTableau::S => { - F::assign(fut, prev); + fut.clone_from(prev); for (&a, k) in BTableau::A[i - 1].iter().zip(k.iter()) { if a == 0.0 { continue; @@ -200,7 +200,7 @@ pub fn integrate( simtime = *time + dt * BTableau::C[i - 1]; } _ if i == BTableau::S => { - F::assign(fut, prev); + fut.clone_from(prev); for (&b, k) in BTableau::B.iter().zip(k.iter()) { if b == 0.0 { continue; @@ -236,7 +236,7 @@ pub fn integrate_embedded_rk(rhs, prev, fut, time, dt, k); - F::assign(fut2, prev); + fut2.clone_from(prev); for (&b, k) in BTableau::BSTAR.iter().zip(k.iter()) { if b == 0.0 { continue; @@ -256,10 +256,6 @@ fn ballistic() { impl Integrable for Ball { type State = Ball; type Diff = (Float, Float); - fn assign(s: &mut Self::State, o: &Self::State) { - s.z = o.z; - s.v = o.v; - } fn scaled_add(s: &mut Self::State, o: &Self::Diff, sc: Float) { s.z += o.0 * sc; s.v += o.1 * sc;