From cfeb30fac02b7e8778e68ee44215ef1152c5571e Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Fri, 25 Feb 2022 20:43:57 +0100 Subject: [PATCH] Small clippy lint fixes --- euler/src/lib.rs | 2 +- multigrid/src/system.rs | 4 ++-- sbp/src/utils.rs | 13 +++++++------ shallow_water/src/lib.rs | 24 ++++++++++-------------- utils/constmatrix/src/lib.rs | 19 ++++++++++++------- utils/integrate/src/lib.rs | 15 ++++++++++----- webfront/src/euler.rs | 14 +++++++------- webfront/src/maxwell.rs | 12 ++++++------ webfront/src/shallow_water.rs | 8 ++++---- 9 files changed, 59 insertions(+), 52 deletions(-) diff --git a/euler/src/lib.rs b/euler/src/lib.rs index 9489889..567dc87 100644 --- a/euler/src/lib.rs +++ b/euler/src/lib.rs @@ -203,7 +203,7 @@ impl Clone for Field { pub struct Diff(pub(crate) Array3); impl integrate::Integrable for Field { - type State = Field; + type State = Self; type Diff = Diff; fn scaled_add(s: &mut Self::State, o: &Self::Diff, scale: Float) { diff --git a/multigrid/src/system.rs b/multigrid/src/system.rs index 166e6cc..4779fd1 100644 --- a/multigrid/src/system.rs +++ b/multigrid/src/system.rs @@ -621,7 +621,7 @@ impl SingleThreadedSystem { let mut fvort = fmod.clone(); match &self.initial_conditions { parsing::InitialConditions::Vortex(vortexparams) => { - fvort.vortex(grid.x(), grid.y(), self.time, &vortexparams); + fvort.vortex(grid.x(), grid.y(), self.time, vortexparams); } parsing::InitialConditions::Expressions(expr) => { let (rho, rhou, rhov, e) = fvort.components_mut(); @@ -1179,7 +1179,7 @@ impl DistributedSystemPart { let mut fvort = self.current.clone(); match &self.initial_conditions { parsing::InitialConditions::Vortex(vortexparams) => { - fvort.vortex(self.grid.0.x(), self.grid.0.y(), self.t, &vortexparams); + fvort.vortex(self.grid.0.x(), self.grid.0.y(), self.t, vortexparams); } parsing::InitialConditions::Expressions(expr) => { let (rho, rhou, rhov, e) = fvort.components_mut(); diff --git a/sbp/src/utils.rs b/sbp/src/utils.rs index bd87a7b..09fc34a 100644 --- a/sbp/src/utils.rs +++ b/sbp/src/utils.rs @@ -55,6 +55,7 @@ impl Direction { } /// Flips all direction through origo + #[must_use] pub fn opposite(self) -> Self { Self { north: self.south, @@ -92,25 +93,25 @@ impl Direction> { /// Methods for borrowing impl Direction { - pub fn north(&self) -> &T { + pub const fn north(&self) -> &T { &self.north } pub fn north_mut(&mut self) -> &mut T { &mut self.north } - pub fn south(&self) -> &T { + pub const fn south(&self) -> &T { &self.south } pub fn south_mut(&mut self) -> &mut T { &mut self.south } - pub fn east(&self) -> &T { + pub const fn east(&self) -> &T { &self.east } pub fn east_mut(&mut self) -> &mut T { &mut self.east } - pub fn west(&self) -> &T { + pub const fn west(&self) -> &T { &self.west } pub fn west_mut(&mut self) -> &mut T { @@ -119,10 +120,10 @@ impl Direction { } impl Direction { - pub fn all(&self) -> bool { + pub const fn all(&self) -> bool { self.north && self.south && self.east && self.west } - pub fn any(&self) -> bool { + pub const fn any(&self) -> bool { self.north || self.south || self.east || self.west } } diff --git a/shallow_water/src/lib.rs b/shallow_water/src/lib.rs index 32bad68..13abfc5 100644 --- a/shallow_water/src/lib.rs +++ b/shallow_water/src/lib.rs @@ -14,7 +14,7 @@ impl Clone for Field { Self(self.0.clone()) } fn clone_from(&mut self, source: &Self) { - self.0.clone_from(&source.0) + self.0.clone_from(&source.0); } } @@ -130,9 +130,7 @@ impl System { let eta = now.eta(); for j in 0..ny { for i in 0..nx { - if eta[(j, i)] <= 0.0 { - panic!("{} {}", j, i); - } + assert!(eta[(j, i)] > 0.0, "eta: {} at ({},{})", eta[(j, i)], i, j); } } } @@ -148,13 +146,13 @@ impl System { next_eta.scaled_add(-1.0, &temp_dx); azip!((dest in &mut temp, eta in now.eta(), etau in now.etau()) { - *dest = etau.powi(2)/eta + G*eta.powi(2)/2.0 + *dest = etau.powi(2)/eta + G*eta.powi(2)/2.0; }); op.diffxi(temp.view(), temp_dx.view_mut()); next_etau.scaled_add(-1.0, &temp_dx); azip!((dest in &mut temp, eta in now.eta(), etau in now.etau(), etav in now.etav()) { - *dest = etau*etav/eta + *dest = etau*etav/eta; }); op.diffxi(temp.view(), temp_dx.view_mut()); next_etav.scaled_add(-1.0, &temp_dx); @@ -174,7 +172,7 @@ impl System { next_etau.scaled_add(-1.0, &temp_dy); azip!((dest in &mut temp, eta in now.eta(), etav in now.etav()) { - *dest = etav.powi(2)/eta + G*eta.powi(2)/2.0 + *dest = etav.powi(2)/eta + G*eta.powi(2)/2.0; }); op.diffeta(temp.view(), temp_dy.view_mut()); next_etav.scaled_add(-1.0, &temp_dy); @@ -184,7 +182,7 @@ impl System { if let Some(op) = op.upwind() { let mut temp_dx = temp_dy; azip!((dest in &mut temp, eta in now.eta(), etau in now.etau()) { - *dest = -(eta.powf(3.0/2.0)*G.sqrt() + etau.abs())/eta + *dest = -(eta.powf(3.0/2.0)*G.sqrt() + etau.abs())/eta; }); op.dissxi(temp.view(), temp_dx.view_mut()); azip!((dest in &mut next_eta, eta in now.eta(), diss in &temp_dx) { @@ -199,7 +197,7 @@ impl System { let mut temp_dy = temp_dx; azip!((dest in &mut temp, eta in now.eta(), etav in now.etav()) { - *dest = -(eta.powf(3.0/2.0)*G.sqrt() + etav.abs())/eta + *dest = -(eta.powf(3.0/2.0)*G.sqrt() + etav.abs())/eta; }); op.disseta(temp.view(), temp_dy.view_mut()); azip!((dest in &mut next_eta, eta in now.eta(), diss in &temp_dy) { @@ -259,10 +257,8 @@ impl System { .zip(other.axis_iter(Axis(1))) { let tau = match dir { - Direction::North => 1.0, - Direction::South => -1.0, - Direction::East => 1.0, - Direction::West => -1.0, + Direction::North | Direction::East => 1.0, + Direction::South | Direction::West => -1.0, }; let hinv = match dir { Direction::North | Direction::South => { @@ -316,7 +312,7 @@ impl System { &mut self.k[..], ); - std::mem::swap(&mut self.fnow, &mut self.fnext) + std::mem::swap(&mut self.fnow, &mut self.fnext); } } diff --git a/utils/constmatrix/src/lib.rs b/utils/constmatrix/src/lib.rs index 950d01f..91c5526 100644 --- a/utils/constmatrix/src/lib.rs +++ b/utils/constmatrix/src/lib.rs @@ -1,5 +1,6 @@ #![no_std] #![feature(const_fn_floating_point_arithmetic)] +#![allow(clippy::inline_always)] use float::Float; use num_traits::identities::Zero; @@ -28,7 +29,7 @@ impl Zero for Matrix } } fn is_zero(&self) -> bool { - self.iter().all(|x| x.is_zero()) + self.iter().all(Zero::is_zero) } } @@ -96,17 +97,17 @@ impl Matrix { impl ColVector { #[inline(always)] - pub fn map_to_col(slice: &[T; N]) -> &ColVector { + pub const fn map_to_col(slice: &[T; N]) -> &Self { unsafe { &*(slice.as_ptr().cast()) } } #[inline(always)] - pub fn map_to_col_mut(slice: &mut [T; N]) -> &mut ColVector { + pub fn map_to_col_mut(slice: &mut [T; N]) -> &mut Self { unsafe { &mut *(slice.as_mut_ptr().cast()) } } } impl RowVector { - pub fn map_to_row(slice: &[T; N]) -> &Self { + pub const fn map_to_row(slice: &[T; N]) -> &Self { unsafe { &*(slice.as_ptr().cast()) } } pub fn map_to_row_mut(slice: &mut [T; N]) -> &mut Self { @@ -191,17 +192,17 @@ where { #[inline(always)] fn mul_assign(&mut self, other: T) { - self.iter_mut().for_each(|x| *x *= other) + self.iter_mut().for_each(|x| *x *= other); } } -impl core::ops::Add> for Matrix +impl core::ops::Add for Matrix where T: Copy + Zero + core::ops::Add + PartialEq, { type Output = Self; fn add(self, lhs: Self) -> Self::Output { - let mut out = Matrix::zero(); + let mut out = Self::zero(); for i in 0..M { for j in 0..N { out[(i, j)] = self[(i, j)] + lhs[(i, j)]; @@ -299,6 +300,7 @@ mod approx { } impl Matrix { + #[must_use] pub const fn flip_ud(&self) -> Self { let mut m = Self::new([[0.0; N]; M]); let mut i = 0; @@ -309,6 +311,7 @@ impl Matrix { m } + #[must_use] pub const fn flip_lr(&self) -> Self { let mut m = Self::new([[0.0; N]; M]); let mut i = 0; @@ -324,6 +327,7 @@ impl Matrix { } /// Flip all sign bits + #[must_use] pub const fn flip_sign(&self) -> Self { let mut m = Self::new([[0.0; N]; M]); let mut i = 0; @@ -339,6 +343,7 @@ impl Matrix { } /// Zero extends if larger than self + #[must_use] pub const fn resize(&self) -> Matrix { let mut m = Matrix::new([[0.0; N2]; M2]); diff --git a/utils/integrate/src/lib.rs b/utils/integrate/src/lib.rs index d91afce..d84562f 100644 --- a/utils/integrate/src/lib.rs +++ b/utils/integrate/src/lib.rs @@ -161,7 +161,7 @@ pub trait Integrable { #[allow(clippy::too_many_arguments)] /// Integrates using the [`ButcherTableau`] specified. `rhs` should be the result -/// of the right hand side of $u_t = rhs$ +/// of the right hand side of `$u_t = rhs$` /// /// rhs takes the old state and the current time, and outputs the state difference /// in the first parameter @@ -170,6 +170,9 @@ pub trait Integrable { /// ```rust,ignore /// integrate::(...) /// ``` +/// +/// # Panics +/// `k` must have enough elements for the given tableau pub fn integrate( mut rhs: RHS, prev: &F::State, @@ -183,11 +186,10 @@ pub fn integrate( assert!(k.len() >= BTableau::S); for i in 0.. { - let simtime; - match i { + let simtime = match i { 0 => { fut.clone_from(prev); - simtime = *time; + *time } i if i < BTableau::S => { fut.clone_from(prev); @@ -197,7 +199,7 @@ pub fn integrate( } F::scaled_add(fut, k, a * dt); } - simtime = *time + dt * BTableau::C[i - 1]; + *time + dt * BTableau::C[i - 1] } _ if i == BTableau::S => { fut.clone_from(prev); @@ -224,6 +226,9 @@ pub fn integrate( /// /// This produces two results, the most accurate result in `fut`, and the less accurate /// result in `fut2`. This can be used for convergence testing and adaptive timesteps. +/// +/// # Panics +/// `k` must have enough elements for the given tableau pub fn integrate_embedded_rk( rhs: RHS, prev: &F::State, diff --git a/webfront/src/euler.rs b/webfront/src/euler.rs index 7d00db2..103cebf 100644 --- a/webfront/src/euler.rs +++ b/webfront/src/euler.rs @@ -21,28 +21,28 @@ impl EulerUniverse { } pub fn init(&mut self, x0: f32, y0: f32) { - self.0.init_with_vortex(x0, y0) + self.0.init_with_vortex(x0, y0); } pub fn advance(&mut self, dt: f32) { - self.0.advance(dt) + self.0.advance(dt); } pub fn advance_upwind(&mut self, dt: f32) { - self.0.advance_upwind(dt) + self.0.advance_upwind(dt); } pub fn get_rho_ptr(&self) -> *const u8 { - self.0.field().rho().as_ptr() as *const u8 + self.0.field().rho().as_ptr().cast() } pub fn get_rhou_ptr(&self) -> *const u8 { - self.0.field().rhou().as_ptr() as *const u8 + self.0.field().rhou().as_ptr().cast() } pub fn get_rhov_ptr(&self) -> *const u8 { - self.0.field().rhov().as_ptr() as *const u8 + self.0.field().rhov().as_ptr().cast() } pub fn get_e_ptr(&self) -> *const u8 { - self.0.field().e().as_ptr() as *const u8 + self.0.field().e().as_ptr().cast() } } diff --git a/webfront/src/maxwell.rs b/webfront/src/maxwell.rs index 4916c56..392bb5d 100644 --- a/webfront/src/maxwell.rs +++ b/webfront/src/maxwell.rs @@ -18,27 +18,27 @@ impl MaxwellUniverse { } pub fn advance(&mut self, dt: f32) { - self.0.advance(dt) + self.0.advance(dt); } pub fn advance_upwind(&mut self, dt: f32) { - self.0.advance_upwind(dt) + self.0.advance_upwind(dt); } #[cfg(feature = "sparse")] pub fn advance_with_matrix(&mut self, dt: f32) { - self.0.advance_sparse(dt) + self.0.advance_sparse(dt); } pub fn get_ex_ptr(&self) -> *const u8 { - self.0.field().ex().as_ptr() as *const u8 + self.0.field().ex().as_ptr().cast() } pub fn get_ey_ptr(&self) -> *const u8 { - self.0.field().ey().as_ptr() as *const u8 + self.0.field().ey().as_ptr().cast() } pub fn get_hz_ptr(&self) -> *const u8 { - self.0.field().hz().as_ptr() as *const u8 + self.0.field().hz().as_ptr().cast() } } diff --git a/webfront/src/shallow_water.rs b/webfront/src/shallow_water.rs index f3eafcc..3898b70 100644 --- a/webfront/src/shallow_water.rs +++ b/webfront/src/shallow_water.rs @@ -38,18 +38,18 @@ impl ShallowWaterUniverse { } pub fn advance(&mut self) { - self.0.advance() + self.0.advance(); } pub fn get_eta_ptr(&self) -> *const u8 { - self.0.eta().as_ptr() as *const u8 + self.0.eta().as_ptr().cast() } pub fn get_etau_ptr(&self) -> *const u8 { - self.0.etau().as_ptr() as *const u8 + self.0.etau().as_ptr().cast() } pub fn get_etav_ptr(&self) -> *const u8 { - self.0.etav().as_ptr() as *const u8 + self.0.etav().as_ptr().cast() } }