Skip to content

Commit 3e3e0f9

Browse files
author
Jeremy E Kozdon
committed
WIP: KSP
1 parent 1bd216c commit 3e3e0f9

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

src/ksp.jl

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,28 @@ Base.@kwdef mutable struct KSP{PetscLib, PetscScalar} <:
1111
P::Union{AbstractMat, Nothing} = nothing
1212
end
1313

14+
"""
15+
KSP(A::AbstractMat, P::AbstractMat{PetscLib} = A; options...)
16+
17+
Create a `KSP` using the matrix `A` and preconditioner construction matrix `P`
18+
with the `options`.
19+
20+
The communicator is obtained from `A` and if it has size `1` then the garbage
21+
collector is set, otherwise the user is responsible for calling
22+
[`destroy`](@ref).
23+
24+
# External Links
25+
$(_doc_external("KSP/KSPCreate"))
26+
$(_doc_external("KSP/KSPSetOperators"))
27+
$(_doc_external("KSP/KSPSetFromOptions"))
28+
"""
1429
function KSP(
1530
A::AbstractMat{PetscLib},
1631
P::AbstractMat{PetscLib} = A;
17-
kwargs...,
32+
options...,
1833
) where {PetscLib}
1934
@assert initialized(PetscLib)
20-
opts = Options(PetscLib; kwargs...)
35+
opts = Options(PetscLib; options...)
2136
PetscScalar = PetscLib.PetscScalar
2237
ksp = KSP{PetscLib, PetscScalar}(opts = opts)
2338
comm = getcomm(A)
@@ -70,6 +85,34 @@ function solve!(
7085
return x
7186
end
7287

88+
"""
89+
createvecs(ksp::AbstractKSP; nright = 0, nleft = 0)
90+
91+
Create `nright` right and `nleft` left vectors compatible with the `ksp`.
92+
Returned object `V` has `Tuple` members `V.right` and `V.left` containing the
93+
vectors.
94+
95+
# External Links
96+
$(_doc_external("KSP/KSPCreateVecs"))
97+
"""
98+
function createvecs(ksp::AbstractKSP{PetscLib}; nright = 0, nleft = 0) where PetscLib
99+
r_right = [Ref{CVec}() for i = 1:nright]
100+
r_left = [Ref{CVec}() for i = 1:nleft]
101+
LibPETSc.KSPCreateVecs(PetscLib, ksp, nright, r_right, nleft, r_left)
102+
103+
for i = 1:nright
104+
@show r_right[i][]
105+
end
106+
for i = 1:nleft
107+
@show r_left[i][]
108+
end
109+
110+
right = ntuple(i -> VecPtr(PetscLib, r_right[i][], false), nright)
111+
left = ntuple(i -> VecPtr(PetscLib, r_left[i][], false), nleft)
112+
113+
(right = right, left = left)
114+
end
115+
73116
function LinearAlgebra.ldiv!(x::AbstractVec, ksp::AbstractKSP, b::AbstractVec)
74117
solve!(x, ksp, b)
75118
end

test/ksp.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ using LinearAlgebra: mul!
6666
@test x y
6767
end
6868

69+
PETSc.createvecs(ksp)
70+
PETSc.createvecs(ksp; nright = 1)
71+
PETSc.createvecs(ksp; nleft = 2)
72+
PETSc.createvecs(ksp; nright = 3, nleft = 7)
73+
6974
# PETSc.destroy(ksp)
7075
PETSc.destroy(A)
7176
PETSc.destroy(x)

0 commit comments

Comments
 (0)