From e27ef0a07cea3a101da8ad7ad7dc08d4f164ab0f Mon Sep 17 00:00:00 2001 From: Connor Johnstone Date: Tue, 23 Jul 2024 11:20:20 -0400 Subject: [PATCH] Clippy fixes --- src/callback.rs | 4 ++-- src/controller.rs | 11 +++++++---- src/integrator/dormand_prince.rs | 8 ++++---- src/integrator/mod.rs | 2 +- src/ode.rs | 6 ++++-- src/problem.rs | 12 ++++++------ 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/callback.rs b/src/callback.rs index d6080ae..c290f0a 100644 --- a/src/callback.rs +++ b/src/callback.rs @@ -11,11 +11,11 @@ pub struct Callback<'a, const D: usize, P> { pub event: &'a dyn Fn(f64, SVector, &P) -> f64, /// The function to change the ODE - pub effect: &'a dyn Fn(&mut ODE) -> (), + pub effect: &'a dyn Fn(&mut ODE), } /// A convenience function for stopping the integration -pub fn stop<'a, const D: usize, P>(ode: &mut ODE) -> () { +pub fn stop(ode: &mut ODE) { ode.t_end = ode.t; } diff --git a/src/controller.rs b/src/controller.rs index 5ce917b..6258c03 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -52,17 +52,20 @@ impl PIController { initial_h: f64, ) -> Self { Self { - alpha: alpha, - beta: beta, + alpha, + beta, factor_c1: 1.0 / min_factor, factor_c2: 1.0 / max_factor, factor_old: 1.0e-4, h_max: h_max.abs(), - safety_factor: safety_factor, + safety_factor, old_h: initial_h, } } - pub fn default() -> Self { +} + +impl Default for PIController { + fn default() -> Self { Self::new(0.17, 0.04, 10.0, 0.2, 100000.0, 0.9, 1e-4) } } diff --git a/src/integrator/dormand_prince.rs b/src/integrator/dormand_prince.rs index 06c1ddf..709fab6 100644 --- a/src/integrator/dormand_prince.rs +++ b/src/integrator/dormand_prince.rs @@ -93,7 +93,7 @@ where h: f64, ) -> (SVector, Option, Option>>) { let mut k: Vec> = vec![SVector::::zeros(); Self::STAGES]; - let mut next_y = ode.y.clone(); + let mut next_y = ode.y; let mut err = SVector::::zeros(); let mut rcont5 = SVector::::zeros(); // Do the first of the summations @@ -106,8 +106,8 @@ where for i in 1..Self::STAGES { // Compute the ks let mut y_term = SVector::::zeros(); - for j in 0..i { - y_term += k[j] * Self::A[(i * (i - 1)) / 2 + j]; + for (j, item) in k.iter().enumerate().take(i) { + y_term += item * Self::A[(i * (i - 1)) / 2 + j]; } k[i] = (ode.f)(ode.t + Self::C[i] * h, ode.y + y_term * h, &ode.params); @@ -127,7 +127,7 @@ where &self, t_start: f64, t_end: f64, - dense: &Vec>, + dense: &[SVector], t: f64, ) -> SVector { let s = (t - t_start) / (t_end - t_start); diff --git a/src/integrator/mod.rs b/src/integrator/mod.rs index c625b1b..00c74e5 100644 --- a/src/integrator/mod.rs +++ b/src/integrator/mod.rs @@ -22,7 +22,7 @@ pub trait Integrator { &self, t_start: f64, t_end: f64, - dense: &Vec>, + dense: &[SVector], t: f64, ) -> SVector; } diff --git a/src/ode.rs b/src/ode.rs index 5ae10b4..6ac6061 100644 --- a/src/ode.rs +++ b/src/ode.rs @@ -1,10 +1,12 @@ use nalgebra::SVector; +type ProblemFunction<'a, const D: usize, P> = &'a dyn Fn(f64, SVector, &P) -> SVector; + /// The basic ODE object that will be passed around. The type (T) and the size (D) will be /// determined upon creation of the object #[derive(Clone, Copy)] pub struct ODE<'a, const D: usize, P> { - pub f: &'a dyn Fn(f64, SVector, &P) -> SVector, + pub f: ProblemFunction<'a, D, P>, pub y: SVector, pub t: f64, pub params: P, @@ -16,7 +18,7 @@ pub struct ODE<'a, const D: usize, P> { impl<'a, const D: usize, P> ODE<'a, D, P> { pub fn new( - f: &'a (dyn Fn(f64, SVector, &P) -> SVector), + f: ProblemFunction<'a, D, P>, t0: f64, t_end: f64, y0: SVector, diff --git a/src/problem.rs b/src/problem.rs index 36026a2..8e72f3f 100644 --- a/src/problem.rs +++ b/src/problem.rs @@ -23,14 +23,14 @@ where { pub fn new(ode: ODE<'a, D, P>, integrator: S, controller: PIController) -> Self { Problem { - ode: ode, - integrator: integrator, - controller: controller, + ode, + integrator, + controller, callbacks: Vec::new(), } } pub fn solve(&mut self) -> Solution { - let mut convergency = SimpleConvergency { eps: 1e-10, max_iter: 1000 }; + let mut convergency = SimpleConvergency { eps: 1e-12, max_iter: 1000 }; let mut times: Vec = vec![self.ode.t]; let mut states: Vec> = vec![self.ode.y]; let mut dense_coefficients: Vec>> = Vec::new(); @@ -60,7 +60,7 @@ where // If fixed time step just step forward one step (new_y, _, dense_option) = self.integrator.step(&self.ode, step); } - if self.callbacks.len() > 0 { + if !self.callbacks.is_empty() { // Check for events occurring for callback in &self.callbacks { if (callback.event)(self.ode.t, self.ode.y, &self.ode.params) @@ -203,7 +203,7 @@ mod tests { let mut problem = Problem::new(ode, dp45, controller).with_callback(value_too_high); let solution = problem.solve(); - assert!(solution.states.last().unwrap()[0] == 10.0); + assert_relative_eq!(solution.states.last().unwrap()[0], 10.0, max_relative = 1e-11); } #[test]