Skip to main content

section_properties/fea/solver/impls/
skyline_ldlt.rs

1//! Skyline (profile) LDL^T factorization solver — unified `LinearSolver`
2//! backend.
3//!
4//! This backend is a thin adapter over the mature, tested
5//! [`crate::fea::SkylineLdlt`] implementation (RCM-ordered profile LDL^T).
6//! Delegating keeps a **single** skyline factorization in the codebase: the
7//! registry backend and the direct `SkylineLdlt` API now share the same
8//! numerical implementation.
9//!
10//! SPD only: the skyline LDL^T requires a symmetric positive-definite matrix.
11//! Symmetry is checked here; a non-positive pivot is reported by the
12//! underlying factorization as a singular/near-singular error.
13use crate::fea::SparseMatrix;
14use crate::fea::solver::{LinearSolver, SolverCapabilities, SolverError};
15
16/// Skyline LDL^T factorization solver
17pub 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        // A new factorization invalidates any previous one, even if this call
45        // fails: a failed factor() must never leave a stale factorization that
46        // a subsequent solve() could use.
47        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        // Skyline LDL^T requires a symmetric matrix.
56        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            // Preserve the "singular / not SPD" contract for callers that
62            // distinguish it from a generic factorisation failure.
63            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}