Skip to content

Commit db55c2e

Browse files
author
Jeremy E Kozdon
committed
Remove DM and add DMDA and PetscDM types
1 parent b9c3572 commit db55c2e

3 files changed

Lines changed: 48 additions & 32 deletions

File tree

src/dm.jl

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const CDM = Ptr{Cvoid}
22

33
abstract type AbstractDM{PetscLib} end
4-
mutable struct DM{PetscLib} <: AbstractDM{PetscLib}
4+
5+
# Mainly for DM we do not know the type of, namely ones returned by PETSc
6+
# functions such as `KSPGetDM`
7+
mutable struct PetscDM{PetscLib} <: AbstractDM{PetscLib}
58
ptr::CDM
6-
opts::Options{PetscLib}
7-
DM{PetscLib}(ptr, opts = Options(PetscLib)) where {PetscLib} =
8-
new{PetscLib}(ptr, opts)
99
end
1010

11+
1112
"""
1213
DMSetUp!(da::DM, opts=da.opts)
1314
@@ -17,7 +18,7 @@ $(_doc_external("DM/DMSetUp"))
1718
"""
1819
function DMSetUp! end
1920

20-
@for_petsc function DMSetUp!(da::DM{$PetscLib}, opts::Options=da.opts)
21+
@for_petsc function DMSetUp!(da::AbstractDM{$PetscLib}, opts::Options=da.opts)
2122
with(opts) do
2223
@chk ccall(
2324
(:DMSetFromOptions, $petsc_library),
@@ -31,7 +32,7 @@ function DMSetUp! end
3132
end
3233

3334
@for_petsc begin
34-
function destroy(da::DM{$PetscLib})
35+
function destroy(da::AbstractDM{$PetscLib})
3536
finalized($PetscScalar) || @chk ccall(
3637
(:DMDestroy, $petsc_library),
3738
PetscErrorCode,
@@ -44,7 +45,7 @@ end
4445
end
4546

4647
"""
47-
DMGetDimension(dm::DM)
48+
DMGetDimension(dm::AbstractDM)
4849
4950
Return the topological dimension of the `dm`
5051
@@ -53,7 +54,7 @@ $(_doc_external("DM/DMGetDimension"))
5354
"""
5455
function DMGetDimension end
5556

56-
@for_petsc function DMGetDimension(dm::DM{$PetscLib})
57+
@for_petsc function DMGetDimension(dm::AbstractDM{$PetscLib})
5758
dim = Ref{$PetscInt}()
5859

5960
@chk ccall(
@@ -68,16 +69,16 @@ function DMGetDimension end
6869
end
6970

7071
"""
71-
gettype(dm::DM)
72+
gettype(dm::AbstractDM)
7273
7374
Gets type name of the `dm`
7475
7576
# External Links
7677
$(_doc_external("DM/DMGetType"))
7778
"""
78-
function gettype(::DM) end
79+
function gettype(::AbstractDM) end
7980

80-
@for_petsc function gettype(dm::DM{$PetscLib})
81+
@for_petsc function gettype(dm::AbstractDM{$PetscLib})
8182
t_r = Ref{Cstring}()
8283
@chk ccall(
8384
(:DMGetType, $petsc_library),
@@ -90,17 +91,17 @@ function gettype(::DM) end
9091
end
9192

9293
"""
93-
view(dm::DM, viewer::Viewer=ViewerStdout(petsclib, getcomm(dm)))
94+
view(dm::AbstractDM, viewer::Viewer=ViewerStdout(petsclib, getcomm(dm)))
9495
9596
view a `dm` with `viewer`
9697
9798
# External Links
9899
$(_doc_external("DM/DMView"))
99100
"""
100-
function view(::DM) end
101+
function view(::AbstractDM) end
101102

102103
@for_petsc function view(
103-
dm::DM{$PetscLib},
104+
dm::AbstractDM{$PetscLib},
104105
viewer::AbstractViewer{$PetscLib} = ViewerStdout($petsclib, getcomm(dm)),
105106
)
106107
@chk ccall(
@@ -114,7 +115,7 @@ function view(::DM) end
114115
end
115116

116117
"""
117-
DMCreateMatrix(dm::DM)
118+
DMCreateMatrix(dm::AbstractDM)
118119
119120
Generates a matrix from the `dm` object.
120121
@@ -123,7 +124,7 @@ $(_doc_external("DM/DMCreateMatrix"))
123124
"""
124125
function DMCreateMatrix end
125126

126-
@for_petsc function DMCreateMatrix(dm::DM{$PetscLib})
127+
@for_petsc function DMCreateMatrix(dm::AbstractDM{$PetscLib})
127128
mat = Mat{$PetscScalar}(C_NULL)
128129

129130
@chk ccall(
@@ -138,7 +139,7 @@ function DMCreateMatrix end
138139
end
139140

140141
"""
141-
DMCreateLocalVector(dm::DM)
142+
DMCreateLocalVector(dm::AbstractDM)
142143
143144
returns a local vector from the `dm` object.
144145
@@ -147,7 +148,7 @@ $(_doc_external("DM/DMCreateLocalVector"))
147148
"""
148149
function DMCreateLocalVector end
149150

150-
@for_petsc function DMCreateLocalVector(dm::DM{$PetscLib})
151+
@for_petsc function DMCreateLocalVector(dm::AbstractDM{$PetscLib})
151152
vec = Vec{$PetscScalar}(C_NULL)
152153

153154
@chk ccall(
@@ -171,7 +172,7 @@ $(_doc_external("DM/DMCreateGlobalVector"))
171172
"""
172173
function DMCreateGlobalVector end
173174

174-
@for_petsc function DMCreateGlobalVector(dm::DM{$PetscLib})
175+
@for_petsc function DMCreateGlobalVector(dm::AbstractDM{$PetscLib})
175176
vec = Vec{$PetscScalar}(C_NULL)
176177

177178
@chk ccall(
@@ -187,7 +188,7 @@ end
187188

188189
"""
189190
DMLocalToGlobal!(
190-
dm::DM
191+
dm::AbstractDM
191192
local_vec::AbstractVec,
192193
mode::InsertMode,
193194
global_vec::AbstractVec,
@@ -201,7 +202,7 @@ $(_doc_external("DM/DMLocalToGlobal"))
201202
function DMLocalToGlobal! end
202203

203204
@for_petsc function DMLocalToGlobal!(
204-
dm::DM{$PetscLib},
205+
dm::AbstractDM{$PetscLib},
205206
local_vec::AbstractVec,
206207
mode::InsertMode,
207208
global_vec::AbstractVec,
@@ -235,7 +236,7 @@ $(_doc_external("DM/DMGlobalToLocal"))
235236
function DMGlobalToLocal! end
236237

237238
@for_petsc function DMGlobalToLocal!(
238-
dm::DM{$PetscLib},
239+
dm::AbstractDM{$PetscLib},
239240
global_vec::AbstractVec,
240241
mode::InsertMode,
241242
local_vec::AbstractVec,
@@ -255,34 +256,35 @@ end
255256

256257
"""
257258
DMGetCoordinateDM(
258-
dm::DM
259+
dm::AbstractDM
259260
)
260261
261-
Create a `DM` for the coordinates of `dm`.
262+
Create a `coord_dm` for the coordinates of `dm`.
262263
263264
# External Links
264265
$(_doc_external("DM/DMGetCoordinateDM"))
265266
"""
266267
function DMGetCoordinateDM end
267268

268269
@for_petsc function DMGetCoordinateDM(
269-
dm::DM{$PetscLib},
270+
dm::AbstractDM{$PetscLib},
270271
)
271-
coord_dm = DM{$PetscLib}(C_NULL)
272+
coord_dm = empty(dm)
272273
@chk ccall(
273274
(:DMGetCoordinateDM, $petsc_library),
274275
PetscErrorCode,
275276
(CDM, Ptr{CDM}),
276277
dm,
277278
coord_dm,
278279
)
280+
@assert gettype(dm) == gettype(coord_dm)
279281

280282
return coord_dm
281283
end
282284

283285
"""
284286
DMGetCoordinatesLocal(
285-
dm::DM
287+
dm::AbstractDM
286288
)
287289
288290
Gets a local vector with the coordinates associated with `dm`.
@@ -293,7 +295,7 @@ $(_doc_external("DM/DMGetCoordinatesLocal"))
293295
function DMGetCoordinatesLocal end
294296

295297
@for_petsc function DMGetCoordinatesLocal(
296-
dm::DM{$PetscLib},
298+
dm::AbstractDM{$PetscLib},
297299
)
298300
coord_vec = Vec{$PetscScalar}(C_NULL)
299301
@chk ccall(

src/dmda.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ mutable struct DMDALocalInfo{IT}
1313
DMDALocalInfo{IT}() where {IT} = new{IT}()
1414
end
1515

16+
mutable struct DMDA{PetscLib} <: AbstractDM{PetscLib}
17+
ptr::CDM
18+
opts::Options{PetscLib}
19+
DMDA{PetscLib}(ptr, opts = Options(PetscLib)) where {PetscLib} =
20+
new{PetscLib}(ptr, opts)
21+
end
22+
23+
"""
24+
empty(da::DMDA)
25+
26+
return an uninitialized `DMDA` struct.
27+
"""
28+
Base.empty(::DMDA{PetscLib}) where PetscLib = DMDA{PetscLib}(C_NULL)
29+
1630
"""
1731
DMDACreate1d(
1832
::PetscLib
@@ -54,7 +68,7 @@ function DMDACreate1d end
5468
@assert length(points_per_proc) == MPI.Comm_size(comm)
5569
points_per_proc
5670
end
57-
da = DM{$PetscLib}(C_NULL, opts)
71+
da = DMDA{$PetscLib}(C_NULL, opts)
5872
with(da.opts) do
5973
@chk ccall(
6074
(:DMDACreate1d, $petsc_library),
@@ -142,7 +156,7 @@ function DMDACreate2d end
142156
@assert length(points_per_proc_y) == procs_y
143157
points_per_proc_y
144158
end
145-
da = DM{$PetscLib}(C_NULL, opts)
159+
da = DMDA{$PetscLib}(C_NULL, opts)
146160
with(da.opts) do
147161
@chk ccall(
148162
(:DMDACreate2d, $petsc_library),
@@ -257,7 +271,7 @@ function DMDACreate3d end
257271
@assert length(points_per_proc_z) == procs_z
258272
points_per_proc_z
259273
end
260-
da = DM{$PetscLib}(C_NULL, opts)
274+
da = DMDA{$PetscLib}(C_NULL, opts)
261275
with(da.opts) do
262276
@chk ccall(
263277
(:DMDACreate3d, $petsc_library),

src/ksp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct Fn_KSPComputeOperators{T} end
173173
ksp,
174174
t_dm,
175175
)
176-
dm = DM{$PetscLib}(t_dm[])
176+
dm = PetscDM{$PetscLib}(t_dm[])
177177
return dm
178178
end
179179

0 commit comments

Comments
 (0)