Clippy fixes

This commit is contained in:
Connor Johnstone
2024-07-23 11:20:20 -04:00
parent 0cfd4f1f5d
commit e27ef0a07c
6 changed files with 24 additions and 19 deletions

View File

@@ -11,11 +11,11 @@ pub struct Callback<'a, const D: usize, P> {
pub event: &'a dyn Fn(f64, SVector<f64, D>, &P) -> f64, pub event: &'a dyn Fn(f64, SVector<f64, D>, &P) -> f64,
/// The function to change the ODE /// The function to change the ODE
pub effect: &'a dyn Fn(&mut ODE<D, P>) -> (), pub effect: &'a dyn Fn(&mut ODE<D, P>),
} }
/// A convenience function for stopping the integration /// A convenience function for stopping the integration
pub fn stop<'a, const D: usize, P>(ode: &mut ODE<D, P>) -> () { pub fn stop<const D: usize, P>(ode: &mut ODE<D, P>) {
ode.t_end = ode.t; ode.t_end = ode.t;
} }

View File

@@ -52,17 +52,20 @@ impl PIController {
initial_h: f64, initial_h: f64,
) -> Self { ) -> Self {
Self { Self {
alpha: alpha, alpha,
beta: beta, beta,
factor_c1: 1.0 / min_factor, factor_c1: 1.0 / min_factor,
factor_c2: 1.0 / max_factor, factor_c2: 1.0 / max_factor,
factor_old: 1.0e-4, factor_old: 1.0e-4,
h_max: h_max.abs(), h_max: h_max.abs(),
safety_factor: safety_factor, safety_factor,
old_h: initial_h, 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) Self::new(0.17, 0.04, 10.0, 0.2, 100000.0, 0.9, 1e-4)
} }
} }

View File

@@ -93,7 +93,7 @@ where
h: f64, h: f64,
) -> (SVector<f64, D>, Option<f64>, Option<Vec<SVector<f64, D>>>) { ) -> (SVector<f64, D>, Option<f64>, Option<Vec<SVector<f64, D>>>) {
let mut k: Vec<SVector<f64, D>> = vec![SVector::<f64, D>::zeros(); Self::STAGES]; let mut k: Vec<SVector<f64, D>> = vec![SVector::<f64, D>::zeros(); Self::STAGES];
let mut next_y = ode.y.clone(); let mut next_y = ode.y;
let mut err = SVector::<f64, D>::zeros(); let mut err = SVector::<f64, D>::zeros();
let mut rcont5 = SVector::<f64, D>::zeros(); let mut rcont5 = SVector::<f64, D>::zeros();
// Do the first of the summations // Do the first of the summations
@@ -106,8 +106,8 @@ where
for i in 1..Self::STAGES { for i in 1..Self::STAGES {
// Compute the ks // Compute the ks
let mut y_term = SVector::<f64, D>::zeros(); let mut y_term = SVector::<f64, D>::zeros();
for j in 0..i { for (j, item) in k.iter().enumerate().take(i) {
y_term += k[j] * Self::A[(i * (i - 1)) / 2 + j]; 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); k[i] = (ode.f)(ode.t + Self::C[i] * h, ode.y + y_term * h, &ode.params);
@@ -127,7 +127,7 @@ where
&self, &self,
t_start: f64, t_start: f64,
t_end: f64, t_end: f64,
dense: &Vec<SVector<f64, D>>, dense: &[SVector<f64, D>],
t: f64, t: f64,
) -> SVector<f64, D> { ) -> SVector<f64, D> {
let s = (t - t_start) / (t_end - t_start); let s = (t - t_start) / (t_end - t_start);

View File

@@ -22,7 +22,7 @@ pub trait Integrator<const D: usize> {
&self, &self,
t_start: f64, t_start: f64,
t_end: f64, t_end: f64,
dense: &Vec<SVector<f64, D>>, dense: &[SVector<f64, D>],
t: f64, t: f64,
) -> SVector<f64, D>; ) -> SVector<f64, D>;
} }

View File

@@ -1,10 +1,12 @@
use nalgebra::SVector; use nalgebra::SVector;
type ProblemFunction<'a, const D: usize, P> = &'a dyn Fn(f64, SVector<f64, D>, &P) -> SVector<f64, D>;
/// The basic ODE object that will be passed around. The type (T) and the size (D) will be /// The basic ODE object that will be passed around. The type (T) and the size (D) will be
/// determined upon creation of the object /// determined upon creation of the object
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct ODE<'a, const D: usize, P> { pub struct ODE<'a, const D: usize, P> {
pub f: &'a dyn Fn(f64, SVector<f64, D>, &P) -> SVector<f64, D>, pub f: ProblemFunction<'a, D, P>,
pub y: SVector<f64, D>, pub y: SVector<f64, D>,
pub t: f64, pub t: f64,
pub params: P, 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> { impl<'a, const D: usize, P> ODE<'a, D, P> {
pub fn new( pub fn new(
f: &'a (dyn Fn(f64, SVector<f64, D>, &P) -> SVector<f64, D>), f: ProblemFunction<'a, D, P>,
t0: f64, t0: f64,
t_end: f64, t_end: f64,
y0: SVector<f64, D>, y0: SVector<f64, D>,

View File

@@ -23,14 +23,14 @@ where
{ {
pub fn new(ode: ODE<'a, D, P>, integrator: S, controller: PIController) -> Self { pub fn new(ode: ODE<'a, D, P>, integrator: S, controller: PIController) -> Self {
Problem { Problem {
ode: ode, ode,
integrator: integrator, integrator,
controller: controller, controller,
callbacks: Vec::new(), callbacks: Vec::new(),
} }
} }
pub fn solve(&mut self) -> Solution<S, D> { pub fn solve(&mut self) -> Solution<S, D> {
let mut convergency = SimpleConvergency { eps: 1e-10, max_iter: 1000 }; let mut convergency = SimpleConvergency { eps: 1e-12, max_iter: 1000 };
let mut times: Vec<f64> = vec![self.ode.t]; let mut times: Vec<f64> = vec![self.ode.t];
let mut states: Vec<SVector<f64, D>> = vec![self.ode.y]; let mut states: Vec<SVector<f64, D>> = vec![self.ode.y];
let mut dense_coefficients: Vec<Vec<SVector<f64, D>>> = Vec::new(); let mut dense_coefficients: Vec<Vec<SVector<f64, D>>> = Vec::new();
@@ -60,7 +60,7 @@ where
// If fixed time step just step forward one step // If fixed time step just step forward one step
(new_y, _, dense_option) = self.integrator.step(&self.ode, 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 // Check for events occurring
for callback in &self.callbacks { for callback in &self.callbacks {
if (callback.event)(self.ode.t, self.ode.y, &self.ode.params) 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 mut problem = Problem::new(ode, dp45, controller).with_callback(value_too_high);
let solution = problem.solve(); 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] #[test]