Skip to content

Commit 901b8d5

Browse files
committed
Add Schur Complement Preconditioner
1 parent 7743a4b commit 901b8d5

5 files changed

Lines changed: 118 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [2.1.0]
4+
5+
- Add `SchurComplementPreconditioner` and `SchurComplementPreconBuilder`
6+
37
## [2.0.0] - 2026-01-06
48

59
- Remove solver + precon API which is not based on precs or directly overloading `\`.

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ExtendableSparse"
22
uuid = "95c220a8-a1cf-11e9-0c77-dbfce5f500b3"
3-
version = "2.0.1"
3+
version = "2.1.0"
44
authors = ["Juergen Fuhrmann <juergen.fuhrmann@wias-berlin.de>", "Daniel Runge"]
55

66
[deps]

src/ExtendableSparse.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module ExtendableSparse
77

88
using DocStringExtensions: DocStringExtensions, SIGNATURES, TYPEDEF, TYPEDFIELDS, TYPEDSIGNATURES
99
using ILUZero: ILUZero
10-
using LinearAlgebra: LinearAlgebra, Diagonal, Hermitian, Symmetric, Tridiagonal, convert, mul!, ldiv!
10+
using LinearAlgebra: LinearAlgebra, Diagonal, Hermitian, Symmetric, Tridiagonal, convert, mul!, ldiv!, lu
1111
using SparseArrays: SparseArrays, AbstractSparseMatrix, AbstractSparseMatrixCSC, SparseMatrixCSC
1212
using SparseArrays: dropzeros!, findnz, nzrange, sparse, spzeros, rowvals, getcolptr, nonzeros, nnz
1313
using Sparspak: sparspaklu

src/preconbuilders.jl

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,98 @@ LinearAlgebra.ldiv!(fact::JacobiPreconditioner, v) = ldiv!(fact.factorization, v
294294

295295
allow_views(::JacobiPreconditioner) = true
296296
allow_views(::Type{JacobiPreconditioner}) = true
297+
298+
299+
300+
"""
301+
SchurComplementPreconditioner{AT, ST}
302+
303+
Preconditioner for saddle-point problems of the form:
304+
```
305+
[ A B ]
306+
[ Bᵀ 0 ]
307+
```
308+
309+
# Fields
310+
- `partitions`: Vector of two ranges defining matrix partitioning
311+
- `A_fac::AT`: Factorization of the A-block (top-left)
312+
- `S_fac::ST`: Factorization of the Schur complement (approximation of -BᵀA⁻¹B)
313+
"""
314+
struct SchurComplementPreconditioner{AT, ST}
315+
partitions
316+
A_fac::AT
317+
S_fac::ST
318+
end
319+
320+
321+
"""
322+
SchurComplementPreconBuilder(dofs_first_block, A_factorization, S_factorization = lu)
323+
324+
Factory function creating preconditioners for saddle-point problems.
325+
326+
# Arguments
327+
- `dofs_first_block`: Number of degrees of freedom in the first block (size of A matrix)
328+
- `A_factorization`: Factorization method for the A-block (e.g., `ilu0, Diagonal, lu`)
329+
- `S_factorization`: Factorization method for the Schur complement (defaults to `lu`)
330+
331+
Returns a function `prec(M, p)` that creates a `SchurComplementPreconditioner`.
332+
"""
333+
function SchurComplementPreconBuilder(dofs_first_block, A_factorization, S_factorization = lu)
334+
335+
# this is the resulting preconditioner
336+
function prec(M, p)
337+
338+
# We have
339+
# M = [ A B ] → n1 dofs
340+
# [ Bᵀ 0 ] → n2 dofs
341+
342+
n1 = dofs_first_block
343+
n2 = size(M, 1) - n1
344+
345+
# I see no other way than creating this expensive copy
346+
A = M[1:n1, 1:n1]
347+
B = M[1:n1, (n1 + 1):end]
348+
349+
# first factorization
350+
A_fac = A_factorization(A)
351+
352+
# compute the (dense!) Schur Matrix
353+
# S ≈ - Bᵀ A⁻¹ B
354+
S = zeros(n2, n2)
355+
356+
@info "Computing the Schur complement..."
357+
ldiv_buffer = zeros(n1)
358+
for col in 1:n2
359+
# TODO: the following is not thread parallel?
360+
@views ldiv!(ldiv_buffer, A_fac, B[:, col])
361+
@views mul!(S[:, col], B', ldiv_buffer)
362+
end
363+
S .*= -1.0
364+
@info "...done"
365+
366+
S_fac = S_factorization(sparse(S))
367+
368+
return SchurComplementPreconditioner([1:n1, (n1 + 1):(n1 + n2)], A_fac, S_fac)
369+
end
370+
371+
return prec
372+
end
373+
374+
375+
function LinearAlgebra.ldiv!(u, p::SchurComplementPreconditioner, v)
376+
377+
(part1, part2) = p.partitions
378+
@views ldiv!(u[part1], p.A_fac, v[part1])
379+
@views ldiv!(u[part2], p.S_fac, v[part2])
380+
381+
return u
382+
end
383+
384+
function LinearAlgebra.ldiv!(p::SchurComplementPreconditioner, v)
385+
386+
(part1, part2) = p.partitions
387+
@views ldiv!(p.A_fac, v[part1])
388+
@views ldiv!(p.S_fac, v[part2])
389+
390+
return v
391+
end

test/test_block.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module test_block
2-
using Test
2+
using AMGCLWrap
33
using ExtendableSparse
4-
using ExtendableSparse: BlockPreconditioner, jacobi
4+
using ExtendableSparse: BlockPreconditioner, jacobi, SchurComplementPreconBuilder
55
using ILUZero, AlgebraicMultigrid
66
using IterativeSolvers
77
using LinearAlgebra
8+
using SparseArrays
89
using Sparspak
9-
using AMGCLWrap
10+
using Test
1011

1112
ExtendableSparse.allow_views(::typeof(ilu0)) = true
1213

@@ -29,6 +30,19 @@ function main(; n = 100)
2930
sol = cg(A, b, Pl = BlockPreconditioner(A; partitioning, factorization = sparspaklu))
3031
@test sol sol0
3132

33+
# Schur complement: create a saddle point system
34+
let
35+
m = n ÷ 10
36+
B = I[1:n^2, 1:m^2]
37+
M = [ A B; B' spzeros(m^2, m^2)]
38+
39+
sol1 = ones(n^2 + m^2)
40+
c = M * sol1
41+
42+
sol = cg(M, c, Pl = SchurComplementPreconBuilder(n^2, lu)(M, nothing))
43+
@test sol sol1
44+
end
45+
3246
return
3347
end
3448

0 commit comments

Comments
 (0)