Prefer clone_from over assign

This commit is contained in:
Magnus Ulimoen 2021-07-15 17:25:30 +02:00
parent e4ea5b081b
commit e25ee9c74a
6 changed files with 38 additions and 35 deletions

View File

@ -185,10 +185,19 @@ impl<UO: UpwindOperator2d + SbpOperator2d> System<UO> {
}
}
#[derive(Clone, Debug)]
#[derive(Debug)]
/// A 4 x ny x nx array
pub struct Field(pub(crate) Array3<Float>);
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<Float>);
@ -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);
}

View File

@ -7,16 +7,22 @@ use sbp::Float;
#[cfg(feature = "sparse")]
pub mod sparse;
#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct Field(pub(crate) Array3<Float>);
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);
}

View File

@ -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()

View File

@ -30,17 +30,7 @@ pub(crate) static MULTITHREAD: AtomicBool = AtomicBool::new(false);
impl integrate::Integrable for System {
type State = Vec<euler::Field>;
type Diff = Vec<euler::Diff>;
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()

View File

@ -6,16 +6,22 @@ use characteristics::{Aminus, Aplus, Bminus, Bplus};
const G: Float = 1.0;
#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct Field(Array3<Float>);
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);
}

View File

@ -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<BTableau: ButcherTableau, F: Integrable, RHS>(
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<BTableau: ButcherTableau, F: Integrable, RHS>(
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<BTableau: EmbeddedButcherTableau, F: Integrable, RH
RHS: FnMut(&mut F::Diff, &F::State, Float),
{
integrate::<BTableau, F, RHS>(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;