faer-precond 0.2.0

Numerical preconditioners (Jacobi, block-Jacobi, ILU(0), IC(0)) for iterative solvers, built on faer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Zero-fill incomplete LU preconditioner.
//!
//! ILU(0) is the general-purpose workhorse for nonsymmetric sparse systems
//! solved with GMRES or BiCGSTAB. It approximates `A` by an LU factorisation
//! constrained to `A`'s own sparsity pattern: no fill-in is allowed, so the
//! factors take no more memory than `A` and are cheap to build. That
//! approximation is enough to cut Krylov iteration counts substantially on most
//! discretised PDE operators. (For symmetric positive-definite `A`, use
//! [`crate::Ic0`] instead — it is the cheaper symmetric specialisation.)
//!
//! Concretely, [`Ilu0::try_new`] computes the unique factors `L` (unit lower)
//! and `U` (upper) such that:
//!
//! - `pattern(L) ∪ pattern(U) = pattern(A)` (no fill-in),
//! - `L * U` agrees with `A` at every entry in `pattern(A)`.
//!
//! # Repeated factorisation
//!
//! Krylov methods inside nonlinear solvers typically refactorise the
//! preconditioner whenever the operator's values change but the sparsity
//! pattern stays the same. For that use-case, build [`SymbolicIlu0`] once,
//! allocate an [`Ilu0`] via [`Ilu0::new_with_symbolic`], and call
//! [`Ilu0::refactorize`] in the hot loop — no allocation occurs.
//!
//! # Example
//!
//! ```
//! use dyn_stack::MemStack;
//! use faer::sparse::{SparseColMat, Triplet};
//! use faer::{mat, Par};
//! use faer::matrix_free::Precond;
//! use faer_precond::Ilu0;
//!
//! // 5x5 tridiagonal: diag 4, off-diagonals -1.
//! let mut triplets = Vec::new();
//! for i in 0..5 {
//!     triplets.push(Triplet::new(i, i, 4.0_f64));
//!     if i > 0 {
//!         triplets.push(Triplet::new(i, i - 1, -1.0));
//!         triplets.push(Triplet::new(i - 1, i, -1.0));
//!     }
//! }
//! let a = SparseColMat::<usize, f64>::try_new_from_triplets(5, 5, &triplets).unwrap();
//!
//! let pc = Ilu0::try_new(a.as_ref()).expect("non-singular pattern");
//!
//! // Apply M^{-1} to a right-hand side in place.
//! let mut b = mat![[1.0_f64], [0.0], [0.0], [0.0], [0.0]];
//! pc.apply_in_place(b.as_mut(), Par::Seq, MemStack::new(&mut []));
//! ```
//!
//! # Storage
//!
//! The factors are stored in column-compressed (CSC) form following faer's
//! sparse triangular-solve conventions — `L`'s unit diagonal stored *first* in
//! each column and `U`'s diagonal stored *last*. Apply uses
//! [`faer::sparse::linalg::triangular_solve`] directly and allocates no heap
//! memory.

use core::fmt::Debug;

use dyn_stack::{MemStack, StackReq};
use faer::matrix_free::{BiLinOp, BiPrecond, LinOp, Precond};
use faer::{Conj, MatMut, MatRef, Par};
use faer_traits::{ComplexField, Index};

pub mod apply;
pub mod numeric;
pub mod symbolic;

pub use numeric::Ilu0;
pub use symbolic::SymbolicIlu0;

/// Error returned by ILU(0) construction or refactorisation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Ilu0Error {
    /// The source matrix was not square.
    NonSquareMatrix { nrows: usize, ncols: usize },
    /// Column `col` of the source matrix does not contain its diagonal entry.
    MissingDiagonal { col: usize },
    /// Row indices in column `col` are not sorted ascending.
    UnsortedRowIndices { col: usize },
    /// A refactorisation was attempted with a matrix whose pattern does not
    /// match the symbolic factor.
    PatternMismatch,
    /// A zero pivot was encountered while eliminating column `col`.
    ZeroPivot { col: usize },
}

impl core::fmt::Display for Ilu0Error {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::NonSquareMatrix { nrows, ncols } => {
                write!(f, "matrix must be square but is {nrows}x{ncols}")
            }
            Self::MissingDiagonal { col } => {
                write!(f, "column {col} is missing its diagonal entry")
            }
            Self::UnsortedRowIndices { col } => {
                write!(f, "column {col} has unsorted row indices")
            }
            Self::PatternMismatch => f.write_str("refactorisation pattern does not match symbolic"),
            Self::ZeroPivot { col } => write!(f, "encountered a zero pivot at column {col}"),
        }
    }
}

impl core::error::Error for Ilu0Error {}

impl<I, T> LinOp<T> for Ilu0<I, T>
where
    I: Index,
    T: ComplexField + Debug + Sync,
{
    fn apply_scratch(&self, _rhs_ncols: usize, _par: Par) -> StackReq {
        StackReq::EMPTY
    }

    fn nrows(&self) -> usize {
        self.dim()
    }

    fn ncols(&self) -> usize {
        self.dim()
    }

    fn apply(&self, mut out: MatMut<'_, T>, rhs: MatRef<'_, T>, par: Par, _stack: &mut MemStack) {
        out.copy_from(rhs);
        apply::solve_in_place(self, Conj::No, out, par);
    }

    fn conj_apply(
        &self,
        mut out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        par: Par,
        _stack: &mut MemStack,
    ) {
        out.copy_from(rhs);
        apply::solve_in_place(self, Conj::Yes, out, par);
    }
}

impl<I, T> Precond<T> for Ilu0<I, T>
where
    I: Index,
    T: ComplexField + Debug + Sync,
{
    fn apply_in_place_scratch(&self, _rhs_ncols: usize, _par: Par) -> StackReq {
        StackReq::EMPTY
    }

    fn apply_in_place(&self, rhs: MatMut<'_, T>, par: Par, _stack: &mut MemStack) {
        apply::solve_in_place(self, Conj::No, rhs, par);
    }

    fn conj_apply_in_place(&self, rhs: MatMut<'_, T>, par: Par, _stack: &mut MemStack) {
        apply::solve_in_place(self, Conj::Yes, rhs, par);
    }
}

impl<I, T> BiLinOp<T> for Ilu0<I, T>
where
    I: Index,
    T: ComplexField + Debug + Sync,
{
    fn transpose_apply_scratch(&self, _rhs_ncols: usize, _par: Par) -> StackReq {
        StackReq::EMPTY
    }

    fn transpose_apply(
        &self,
        mut out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        par: Par,
        _stack: &mut MemStack,
    ) {
        out.copy_from(rhs);
        apply::solve_transpose_in_place(self, Conj::No, out, par);
    }

    fn adjoint_apply(
        &self,
        mut out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        par: Par,
        _stack: &mut MemStack,
    ) {
        out.copy_from(rhs);
        apply::solve_transpose_in_place(self, Conj::Yes, out, par);
    }
}

impl<I, T> BiPrecond<T> for Ilu0<I, T>
where
    I: Index,
    T: ComplexField + Debug + Sync,
{
    fn transpose_apply_in_place_scratch(&self, _rhs_ncols: usize, _par: Par) -> StackReq {
        StackReq::EMPTY
    }

    fn transpose_apply_in_place(&self, rhs: MatMut<'_, T>, par: Par, _stack: &mut MemStack) {
        apply::solve_transpose_in_place(self, Conj::No, rhs, par);
    }

    fn adjoint_apply_in_place(&self, rhs: MatMut<'_, T>, par: Par, _stack: &mut MemStack) {
        apply::solve_transpose_in_place(self, Conj::Yes, rhs, par);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use faer::sparse::{SparseColMat, Triplet};
    use faer::{Mat, MatRef, mat};

    fn assert_close(lhs: MatRef<'_, f64>, rhs: MatRef<'_, f64>, tol: f64) {
        assert_eq!(lhs.nrows(), rhs.nrows());
        assert_eq!(lhs.ncols(), rhs.ncols());
        for j in 0..lhs.ncols() {
            for i in 0..lhs.nrows() {
                let diff = (*lhs.get(i, j) - *rhs.get(i, j)).abs();
                assert!(
                    diff <= tol,
                    "mismatch at ({i}, {j}): lhs={}, rhs={}, diff={diff}",
                    *lhs.get(i, j),
                    *rhs.get(i, j),
                );
            }
        }
    }

    /// 5-point Laplacian stencil on a 4x4 grid (n=16). Symmetric, diagonally
    /// dominant, banded. ILU(0) is exact-modulo-fill on this stencil.
    fn laplacian_2d(grid: usize) -> SparseColMat<usize, f64> {
        let n = grid * grid;
        let mut triplets: Vec<Triplet<usize, usize, f64>> = Vec::new();
        for gy in 0..grid {
            for gx in 0..grid {
                let idx = gy * grid + gx;
                triplets.push(Triplet::new(idx, idx, 4.0));
                if gx > 0 {
                    triplets.push(Triplet::new(idx, idx - 1, -1.0));
                }
                if gx + 1 < grid {
                    triplets.push(Triplet::new(idx, idx + 1, -1.0));
                }
                if gy > 0 {
                    triplets.push(Triplet::new(idx, idx - grid, -1.0));
                }
                if gy + 1 < grid {
                    triplets.push(Triplet::new(idx, idx + grid, -1.0));
                }
            }
        }
        SparseColMat::try_new_from_triplets(n, n, &triplets).unwrap()
    }

    /// Convert a sparse matrix to dense for verification.
    fn to_dense(a: &SparseColMat<usize, f64>) -> Mat<f64> {
        let n = a.nrows();
        let mut out = Mat::<f64>::zeros(n, a.ncols());
        let a_ref = a.as_ref();
        for j in 0..a.ncols() {
            let rows = a_ref.symbolic().row_idx_of_col_raw(j);
            let vals = a_ref.val_of_col(j);
            for (r, v) in rows.iter().zip(vals.iter()) {
                *out.as_mut().get_mut(*r, j) = *v;
            }
        }
        out
    }

    fn tridiagonal_csc(n: usize, diag: f64, off: f64) -> SparseColMat<usize, f64> {
        let mut triplets = Vec::new();
        for i in 0..n {
            triplets.push(Triplet::new(i, i, diag));
            if i > 0 {
                triplets.push(Triplet::new(i, i - 1, off));
                triplets.push(Triplet::new(i - 1, i, off));
            }
        }
        SparseColMat::try_new_from_triplets(n, n, &triplets).unwrap()
    }

    #[test]
    fn ilu0_tridiagonal_matches_exact_inverse() {
        // For a tridiagonal SPD matrix, ILU(0) coincides with the exact LU
        // factorisation (no fill is introduced anyway). Thus M^{-1} A = I.
        let a = tridiagonal_csc(5, 4.0, -1.0);
        let pc = Ilu0::try_new(a.as_ref()).unwrap();

        let a_dense = to_dense(&a);
        let x_true = mat![[1.0], [-2.0], [3.0], [-1.0], [0.5_f64]];
        let mut rhs = (&a_dense * &x_true).to_owned();

        pc.apply_in_place(rhs.as_mut(), Par::Seq, MemStack::new(&mut []));
        assert_close(rhs.as_ref(), x_true.as_ref(), 1e-12);
    }

    fn sparse_view_to_dense(a: faer::sparse::SparseColMatRef<'_, usize, f64>) -> Mat<f64> {
        let mut dense = Mat::<f64>::zeros(a.nrows(), a.ncols());
        for j in 0..a.ncols() {
            let rows = a.symbolic().row_idx_of_col_raw(j);
            let vals = a.val_of_col(j);
            for (r, v) in rows.iter().zip(vals.iter()) {
                *dense.as_mut().get_mut(*r, j) = *v;
            }
        }
        dense
    }

    #[test]
    fn ilu0_factor_satisfies_pattern_equation() {
        // For ILU(0) the relation L*U == A holds at every position in pattern(A).
        let a = laplacian_2d(4);
        let pc = Ilu0::try_new(a.as_ref()).unwrap();

        let l_dense = sparse_view_to_dense(pc.l_view());
        let u_dense = sparse_view_to_dense(pc.u_view());
        let lu_dense = &l_dense * &u_dense;
        let a_dense = to_dense(&a);

        let a_ref = a.as_ref();
        for j in 0..a.ncols() {
            for r in a_ref.symbolic().row_idx_of_col_raw(j) {
                let i = *r;
                let diff = (*lu_dense.as_ref().get(i, j) - *a_dense.as_ref().get(i, j)).abs();
                assert!(diff <= 1e-12, "L*U disagrees with A at ({i},{j}): {diff}");
            }
        }
    }

    #[test]
    fn ilu0_reduces_residual_significantly() {
        // For diagonally-dominant matrices, ILU(0) should at minimum produce a
        // very small residual ||A x - b|| for a single right-preconditioned solve.
        let a = laplacian_2d(8);
        let n = a.nrows();
        let pc = Ilu0::try_new(a.as_ref()).unwrap();
        let a_dense = to_dense(&a);

        let b = Mat::<f64>::from_fn(n, 1, |i, _| (i % 7) as f64 - 3.0);
        let mut x = b.clone();
        pc.apply_in_place(x.as_mut(), Par::Seq, MemStack::new(&mut []));

        let residual = &a_dense * &x - &b;
        let b_norm: f64 = b.as_ref().col(0).iter().map(|v| v * v).sum::<f64>().sqrt();
        let r_norm: f64 = residual
            .as_ref()
            .col(0)
            .iter()
            .map(|v| v * v)
            .sum::<f64>()
            .sqrt();
        // ILU(0) for the 5-point Laplacian gives a much smaller residual than 1.
        assert!(
            r_norm / b_norm < 0.5,
            "ILU(0) residual ratio {r_norm}/{b_norm} too large"
        );
    }

    #[test]
    fn refactorize_matches_fresh_construction() {
        let a1 = tridiagonal_csc(7, 4.0, -1.0);
        let a2 = tridiagonal_csc(7, 5.0, -2.0);

        let pc_fresh = Ilu0::try_new(a2.as_ref()).unwrap();

        let mut pc_reused = Ilu0::try_new(a1.as_ref()).unwrap();
        pc_reused.refactorize(a2.as_ref()).unwrap();

        assert_eq!(pc_fresh.l_values.len(), pc_reused.l_values.len());
        assert_eq!(pc_fresh.u_values.len(), pc_reused.u_values.len());
        for (a, b) in pc_fresh.l_values.iter().zip(pc_reused.l_values.iter()) {
            assert!((a - b).abs() < 1e-14);
        }
        for (a, b) in pc_fresh.u_values.iter().zip(pc_reused.u_values.iter()) {
            assert!((a - b).abs() < 1e-14);
        }
    }

    #[test]
    fn rejects_non_square() {
        let mut triplets = Vec::new();
        for i in 0..3 {
            triplets.push(Triplet::new(i, i, 1.0));
        }
        let a = SparseColMat::<usize, f64>::try_new_from_triplets(3, 4, &triplets).unwrap();
        let err = Ilu0::try_new(a.as_ref()).unwrap_err();
        assert_eq!(err, Ilu0Error::NonSquareMatrix { nrows: 3, ncols: 4 });
    }

    #[test]
    fn rejects_missing_diagonal() {
        // 3x3 with no diagonal in column 1.
        let triplets = vec![
            Triplet::new(0, 0, 1.0),
            Triplet::new(0, 1, 2.0),
            Triplet::new(2, 1, 3.0),
            Triplet::new(2, 2, 4.0_f64),
        ];
        let a = SparseColMat::<usize, f64>::try_new_from_triplets(3, 3, &triplets).unwrap();
        let err = Ilu0::try_new(a.as_ref()).unwrap_err();
        assert_eq!(err, Ilu0Error::MissingDiagonal { col: 1 });
    }

    #[test]
    fn rejects_pattern_mismatch_on_refactorize() {
        let a1 = tridiagonal_csc(5, 4.0, -1.0);
        let a2 = tridiagonal_csc(6, 4.0, -1.0);
        let mut pc = Ilu0::try_new(a1.as_ref()).unwrap();
        let err = pc.refactorize(a2.as_ref()).unwrap_err();
        assert_eq!(err, Ilu0Error::PatternMismatch);
    }

    #[test]
    fn transpose_apply_inverts_transposed_system() {
        let a = tridiagonal_csc(6, 4.0, -1.0);
        let pc = Ilu0::try_new(a.as_ref()).unwrap();
        let a_dense = to_dense(&a);

        let x_true = mat![[1.0], [-2.0], [3.0], [-1.0], [0.5], [2.0_f64]];
        let rhs = a_dense.transpose() * &x_true;

        let mut out = rhs.clone();
        pc.transpose_apply_in_place(out.as_mut(), Par::Seq, MemStack::new(&mut []));
        // Tridiagonal => ILU(0) exact => M^{-T} A^T x = x.
        assert_close(out.as_ref(), x_true.as_ref(), 1e-12);
    }
}