Skip to content

Commit e693b41

Browse files
committed
Added linear solver wrappers for lu and IterativeSolvers.cg
1 parent 11ec789 commit e693b41

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

PartitionedSolvers/src/PartitionedSolvers.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module PartitionedSolvers
33
using PartitionedArrays
44
using SparseArrays
55
using LinearAlgebra
6+
using IterativeSolvers
67

78
export setup
89
export solve!

PartitionedSolvers/src/interfaces.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ struct LinearSolver{A,B,C,D} <: AbstractLinearSolver
2828
finalize!::D
2929
end
3030

31+
function linear_solver(s::LinearSolver)
32+
s
33+
end
34+
3135
struct Preconditioner{A,B}
3236
solver::A
3337
solver_setup::B

PartitionedSolvers/src/smoothers.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,35 @@ function additive_schwarz_correction(local_solver)
179179
linear_solver(;setup,update!,solve!,finalize!)
180180
end
181181

182+
# Wrappers
183+
184+
function linear_solver(::typeof(LinearAlgebra.lu))
185+
lu_solver()
186+
end
187+
188+
function linear_solver(::typeof(IterativeSolvers.cg);Pl,kwargs...)
189+
function setup(x,A,b,options)
190+
Pl_solver = linear_solver(Pl)
191+
P = PartitionedSolvers.setup(Pl_solver,x,A,b;options...)
192+
A_ref = Ref(A)
193+
(;P,A_ref)
194+
end
195+
function update!(state,A,options)
196+
(;P,A_ref) = state
197+
A_ref[] = A
198+
P = PartitionedSolvers.update!(P,A;options...)
199+
state
200+
end
201+
function solve!(x,state,b,options)
202+
(;P,A_ref) = state
203+
A = A_ref[]
204+
IterativeSolvers.cg!(x,A,b;Pl=P,kwargs...)
205+
end
206+
function finalize!(state,A,options)
207+
(;P) = state
208+
PartitionedSolvers.finalize!(P)
209+
nothing
210+
end
211+
linear_solver(;setup,update!,solve!,finalize!)
212+
end
213+

PartitionedSolvers/test/amg_tests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using PartitionedSolvers
66
using LinearAlgebra
77
using Test
88
using IterativeSolvers: cg!
9+
import IterativeSolvers
910

1011
# First with a sequential matrix
1112
nodes_per_dir = (100,100)
@@ -65,6 +66,12 @@ Pl = setup(amg(;fine_params),y,A,b;nullspace=B)
6566
y .= 0
6667
cg!(y,A,b;Pl,verbose=true)
6768

69+
solver = linear_solver(IterativeSolvers.cg;Pl=amg(;fine_params),verbose=true)
70+
S = setup(solver,y,A,b)
71+
solve!(y,S,b)
72+
update!(S,2*A)
73+
solve!(y,S,b)
74+
6875
# Now in parallel
6976

7077
parts_per_dir = (2,2)

PartitionedSolvers/test/smoothers_tests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ solve!(y,S,b)
2626
@test norm(y-x/2)/norm(x/2) < tol
2727
finalize!(S)
2828

29+
solver = linear_solver(LinearAlgebra.lu)
30+
y = similar(x)
31+
S = setup(solver,y,A,b)
32+
solve!(y,S,b)
33+
tol = 1.e-8
34+
@test norm(y-x)/norm(x) < tol
35+
update!(S,2*A)
36+
solve!(y,S,b)
37+
@test norm(y-x/2)/norm(x/2) < tol
38+
finalize!(S)
39+
2940
solver = richardson(lu_solver(),iters=1)
3041
y = similar(x)
3142
y .= 0

0 commit comments

Comments
 (0)