section_properties/fea/solver/impls/
skyline_ldlt.rs1use crate::fea::SparseMatrix;
14use crate::fea::solver::{LinearSolver, SolverCapabilities, SolverError};
15
16pub struct SkylineLdltSolver {
18 inner: Option<crate::fea::SkylineLdlt>,
19 n: usize,
20}
21
22impl SkylineLdltSolver {
23 pub fn new() -> Self {
24 Self { inner: None, n: 0 }
25 }
26}
27
28impl Default for SkylineLdltSolver {
29 fn default() -> Self {
30 Self::new()
31 }
32}
33
34impl LinearSolver for SkylineLdltSolver {
35 fn name(&self) -> &'static str {
36 "skyline_ldlt"
37 }
38
39 fn capabilities(&self) -> SolverCapabilities {
40 SolverCapabilities::skyline_ldlt()
41 }
42
43 fn factor(&mut self, matrix: &SparseMatrix) -> Result<(), SolverError> {
44 self.inner = None;
48 self.n = 0;
49
50 let n = matrix.n;
51 if n == 0 {
52 return Err(SolverError::invalid_input("Empty matrix"));
53 }
54
55 if !matrix.is_symmetric(1e-12) {
57 return Err(SolverError::not_symmetric());
58 }
59
60 let ldlt = crate::fea::SkylineLdlt::factor(matrix).map_err(|e| match e {
61 crate::fea::FemError::SingularMatrix => SolverError::singular(e.to_string()),
64 _ => SolverError::factorization_failed(e.to_string()),
65 })?;
66
67 self.inner = Some(ldlt);
68 self.n = n;
69 Ok(())
70 }
71
72 fn solve(&self, rhs: &[f64]) -> Result<Vec<f64>, SolverError> {
73 let ldlt = self
74 .inner
75 .as_ref()
76 .ok_or_else(SolverError::not_factorized)?;
77
78 if rhs.len() != self.n {
79 return Err(SolverError::dimension_mismatch(self.n, rhs.len()));
80 }
81 for (i, &val) in rhs.iter().enumerate() {
82 if !val.is_finite() {
83 return Err(SolverError::non_finite(i));
84 }
85 }
86
87 ldlt.solve(rhs)
88 .map_err(|e| SolverError::solve_failed(e.to_string()))
89 }
90}