-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#9 more refactoring, working on diffsl now
- Loading branch information
1 parent
ec880a7
commit 9e99ee8
Showing
19 changed files
with
370 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,58 @@ | ||
use crate::{linear_solver::LinearSolver, op::LinearOp, solver::SolverProblem, Scalar}; | ||
use crate::{ | ||
linear_solver::LinearSolver, op::linearise::LinearisedOp, solver::SolverProblem, NonLinearOp, | ||
Op, Scalar, | ||
}; | ||
use anyhow::Result; | ||
use faer::{linalg::solvers::FullPivLu, solvers::SpSolver, Col, Mat}; | ||
/// A [LinearSolver] that uses the LU decomposition in the [`faer`](https://github.com/sarah-ek/faer-rs) library to solve the linear system. | ||
pub struct LU<T, C> | ||
where | ||
T: Scalar, | ||
C: LinearOp<M = Mat<T>, V = Col<T>, T = T>, | ||
C: NonLinearOp<M = Mat<T>, V = Col<T>, T = T>, | ||
{ | ||
lu: Option<FullPivLu<T>>, | ||
problem: Option<SolverProblem<C>>, | ||
problem: Option<SolverProblem<LinearisedOp<C>>>, | ||
matrix: Option<Mat<T>>, | ||
} | ||
|
||
impl<T, C> Default for LU<T, C> | ||
where | ||
T: Scalar, | ||
C: LinearOp<M = Mat<T>, V = Col<T>, T = T>, | ||
C: NonLinearOp<M = Mat<T>, V = Col<T>, T = T>, | ||
{ | ||
fn default() -> Self { | ||
Self { | ||
lu: None, | ||
problem: None, | ||
matrix: None, | ||
} | ||
} | ||
} | ||
|
||
impl<T: Scalar, C: LinearOp<M = Mat<T>, V = Col<T>, T = T>> LinearSolver<C> for LU<T, C> { | ||
fn problem(&self) -> Option<&SolverProblem<C>> { | ||
self.problem.as_ref() | ||
} | ||
fn problem_mut(&mut self) -> Option<&mut SolverProblem<C>> { | ||
self.problem.as_mut() | ||
} | ||
fn take_problem(&mut self) -> Option<SolverProblem<C>> { | ||
self.lu = None; | ||
Option::take(&mut self.problem) | ||
impl<T: Scalar, C: NonLinearOp<M = Mat<T>, V = Col<T>, T = T>> LinearSolver<C> for LU<T, C> { | ||
fn set_linearisation(&mut self, x: &C::V, t: C::T) { | ||
let matrix = self.matrix.as_mut().expect("Matrix not set"); | ||
let problem = self.problem.as_ref().expect("Problem not set"); | ||
problem.f.jacobian_inplace(x, t, matrix); | ||
self.lu = Some(matrix.full_piv_lu()); | ||
} | ||
|
||
fn solve_in_place(&self, state: &mut C::V) -> Result<()> { | ||
fn solve_in_place(&self, x: &mut C::V) -> Result<()> { | ||
if self.lu.is_none() { | ||
return Err(anyhow::anyhow!("LU not initialized")); | ||
} | ||
let lu = self.lu.as_ref().unwrap(); | ||
lu.solve_in_place(state); | ||
lu.solve_in_place(x); | ||
Ok(()) | ||
} | ||
|
||
fn set_problem(&mut self, problem: SolverProblem<C>) { | ||
self.lu = Some(problem.f.jacobian(problem.t).full_piv_lu()); | ||
self.problem = Some(problem); | ||
fn set_problem(&mut self, problem: &SolverProblem<C>) { | ||
let linearised_problem = problem.linearise(); | ||
let matrix = Mat::zeros( | ||
linearised_problem.f.nstates(), | ||
linearised_problem.f.nstates(), | ||
); | ||
self.problem = Some(linearised_problem); | ||
self.matrix = Some(matrix); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.