From 7391d99e9823e1615159ed08a458339218282041 Mon Sep 17 00:00:00 2001 From: Markus <66058642+mhovd@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:36:42 +0100 Subject: [PATCH 1/4] Use a reference of problem for set_problem During simulation it may be necessary to update the state, which also requires the problem. Previously `set_problem()` consumed `problem`, but this change instead calls for a reference. The problem itself is small, and as such the clone should not be especially expensive. --- src/lib.rs | 2 +- src/ode_solver/bdf.rs | 5 +++-- src/ode_solver/mod.rs | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 983a42eb..6b33e22e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,7 +99,7 @@ mod tests { let y = solver.solve(&problem, t).unwrap(); let mut state = OdeSolverState::new(&problem); - solver.set_problem(&mut state, problem); + solver.set_problem(&mut state, &problem); while state.t <= t { solver.step(&mut state).unwrap(); } diff --git a/src/ode_solver/bdf.rs b/src/ode_solver/bdf.rs index 41ec94be..b0c3d699 100644 --- a/src/ode_solver/bdf.rs +++ b/src/ode_solver/bdf.rs @@ -250,8 +250,9 @@ where - fn set_problem(&mut self, state: &mut OdeSolverState, problem: OdeSolverProblem) { - self.ode_problem = Some(problem); + fn set_problem(&mut self, state: &mut OdeSolverState, problem: &OdeSolverProblem) { + let problem_clone = problem.clone(); + self.ode_problem = Some(problem_clone); let problem = self.ode_problem.as_ref().unwrap(); let nstates = problem.eqn.nstates(); self.order = 1usize; diff --git a/src/ode_solver/mod.rs b/src/ode_solver/mod.rs index 56f81186..958b8848 100644 --- a/src/ode_solver/mod.rs +++ b/src/ode_solver/mod.rs @@ -26,13 +26,13 @@ pub mod diffsl; pub trait OdeSolverMethod { fn problem(&self) -> Option<&OdeSolverProblem>; - fn set_problem(&mut self, state: &mut OdeSolverState, problem: OdeSolverProblem); + fn set_problem(&mut self, state: &mut OdeSolverState, problem: &OdeSolverProblem); fn step(&mut self, state: &mut OdeSolverState) -> Result<()>; fn interpolate(&self, state: &OdeSolverState, t: Eqn::T) -> Eqn::V; fn solve(&mut self, problem: &OdeSolverProblem, t: Eqn::T) -> Result { let problem = problem.clone(); let mut state = OdeSolverState::new(&problem); - self.set_problem(&mut state, problem); + self.set_problem(&mut state, &problem); while state.t <= t { self.step(&mut state)?; } @@ -41,7 +41,7 @@ pub trait OdeSolverMethod { fn make_consistent_and_solve>>>(&mut self, problem: &OdeSolverProblem, t: Eqn::T, root_solver: &mut RS) -> Result { let problem = problem.clone(); let mut state = OdeSolverState::new_consistent(&problem, root_solver)?; - self.set_problem(&mut state, problem); + self.set_problem(&mut state, &problem); while state.t <= t { self.step(&mut state)?; } @@ -260,7 +260,7 @@ mod tests { Eqn: OdeEquations, { let mut state = OdeSolverState::new_consistent(&problem, &mut root_solver).unwrap(); - method.set_problem(&mut state, problem); + method.set_problem(&mut state, &problem); for point in solution.solution_points.iter() { while state.t < point.t { method.step(&mut state).unwrap(); From 0a615fb7a3aea57ab044be8b8dacda79fe30be47 Mon Sep 17 00:00:00 2001 From: Markus Hovd <66058642+mhovd@users.noreply.github.com> Date: Sat, 23 Mar 2024 17:27:10 +0100 Subject: [PATCH 2/4] Update bdf.rs --- src/ode_solver/bdf.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ode_solver/bdf.rs b/src/ode_solver/bdf.rs index 4ff820b8..9bb5cc0c 100644 --- a/src/ode_solver/bdf.rs +++ b/src/ode_solver/bdf.rs @@ -261,9 +261,7 @@ where } fn set_problem(&mut self, state: &mut OdeSolverState, problem: &OdeSolverProblem) { - let problem_clone = problem.clone(); - self.ode_problem = Some(problem_clone); - let problem = self.ode_problem.as_ref().unwrap(); + self.ode_problem = Some(problem.clone()); let nstates = problem.eqn.nstates(); self.order = 1usize; self.n_equal_steps = 0; From 1a7b82f7fac1ceb16f57b7a01e8b14b81d8d8c45 Mon Sep 17 00:00:00 2001 From: Markus Hovd <66058642+mhovd@users.noreply.github.com> Date: Sat, 23 Mar 2024 17:34:26 +0100 Subject: [PATCH 3/4] Update src/ode_solver/mod.rs Co-authored-by: Martin Robinson --- src/ode_solver/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ode_solver/mod.rs b/src/ode_solver/mod.rs index e0f892f6..7c132f2e 100644 --- a/src/ode_solver/mod.rs +++ b/src/ode_solver/mod.rs @@ -44,7 +44,6 @@ pub trait OdeSolverMethod { t: Eqn::T, root_solver: &mut RS, ) -> Result { - let problem = problem.clone(); let mut state = OdeSolverState::new_consistent(&problem, root_solver)?; self.set_problem(&mut state, &problem); while state.t <= t { From 490b815b4fd593b3b4b618d3d1278bfdccf56ee5 Mon Sep 17 00:00:00 2001 From: Markus Hovd <66058642+mhovd@users.noreply.github.com> Date: Sat, 23 Mar 2024 17:34:31 +0100 Subject: [PATCH 4/4] Update src/ode_solver/mod.rs Co-authored-by: Martin Robinson --- src/ode_solver/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ode_solver/mod.rs b/src/ode_solver/mod.rs index 7c132f2e..5c5b66bf 100644 --- a/src/ode_solver/mod.rs +++ b/src/ode_solver/mod.rs @@ -30,7 +30,6 @@ pub trait OdeSolverMethod { fn step(&mut self, state: &mut OdeSolverState) -> Result<()>; fn interpolate(&self, state: &OdeSolverState, t: Eqn::T) -> Eqn::V; fn solve(&mut self, problem: &OdeSolverProblem, t: Eqn::T) -> Result { - let problem = problem.clone(); let mut state = OdeSolverState::new(&problem); self.set_problem(&mut state, &problem); while state.t <= t {