Skip to content

Commit 5c5f286

Browse files
author
Jeremy E Kozdon
committed
Remove DM and add DMDA and PetscDM types
1 parent ded3b0b commit 5c5f286

3 files changed

Lines changed: 48 additions & 35 deletions

File tree

src/dm.jl

Lines changed: 30 additions & 31 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
@@ -45,7 +46,7 @@ function DMSetFromOptions! end
4546
end
4647

4748
@for_petsc begin
48-
function destroy(da::DM{$PetscLib})
49+
function destroy(da::AbstractDM{$PetscLib})
4950
finalized($PetscScalar) || @chk ccall(
5051
(:DMDestroy, $petsc_library),
5152
PetscErrorCode,
@@ -58,7 +59,7 @@ end
5859
end
5960

6061
"""
61-
DMGetDimension(dm::DM)
62+
DMGetDimension(dm::AbstractDM)
6263
6364
Return the topological dimension of the `dm`
6465
@@ -67,7 +68,7 @@ $(_doc_external("DM/DMGetDimension"))
6768
"""
6869
function DMGetDimension end
6970

70-
@for_petsc function DMGetDimension(dm::DM{$PetscLib})
71+
@for_petsc function DMGetDimension(dm::AbstractDM{$PetscLib})
7172
dim = Ref{$PetscInt}()
7273

7374
@chk ccall(
@@ -82,16 +83,16 @@ function DMGetDimension end
8283
end
8384

8485
"""
85-
gettype(dm::DM)
86+
gettype(dm::AbstractDM)
8687
8788
Gets type name of the `dm`
8889
8990
# External Links
9091
$(_doc_external("DM/DMGetType"))
9192
"""
92-
function gettype(::DM) end
93+
function gettype(::AbstractDM) end
9394

94-
@for_petsc function gettype(dm::DM{$PetscLib})
95+
@for_petsc function gettype(dm::AbstractDM{$PetscLib})
9596
t_r = Ref{Cstring}()
9697
@chk ccall(
9798
(:DMGetType, $petsc_library),
@@ -104,17 +105,17 @@ function gettype(::DM) end
104105
end
105106

106107
"""
107-
view(dm::DM, viewer::Viewer=ViewerStdout(petsclib, getcomm(dm)))
108+
view(dm::AbstractDM, viewer::Viewer=ViewerStdout(petsclib, getcomm(dm)))
108109
109110
view a `dm` with `viewer`
110111
111112
# External Links
112113
$(_doc_external("DM/DMView"))
113114
"""
114-
function view(::DM) end
115+
function view(::AbstractDM) end
115116

116117
@for_petsc function view(
117-
dm::DM{$PetscLib},
118+
dm::AbstractDM{$PetscLib},
118119
viewer::AbstractViewer{$PetscLib} = ViewerStdout($petsclib, getcomm(dm)),
119120
)
120121
@chk ccall(
@@ -128,7 +129,7 @@ function view(::DM) end
128129
end
129130

130131
"""
131-
DMCreateMatrix(dm::DM)
132+
DMCreateMatrix(dm::AbstractDM)
132133
133134
Generates a matrix from the `dm` object.
134135
@@ -137,7 +138,7 @@ $(_doc_external("DM/DMCreateMatrix"))
137138
"""
138139
function DMCreateMatrix end
139140

140-
@for_petsc function DMCreateMatrix(dm::DM{$PetscLib})
141+
@for_petsc function DMCreateMatrix(dm::AbstractDM{$PetscLib})
141142
mat = Mat{$PetscScalar}(C_NULL)
142143

143144
@chk ccall(
@@ -152,7 +153,7 @@ function DMCreateMatrix end
152153
end
153154

154155
"""
155-
DMCreateLocalVector(dm::DM)
156+
DMCreateLocalVector(dm::AbstractDM)
156157
157158
returns a local vector from the `dm` object.
158159
@@ -161,7 +162,7 @@ $(_doc_external("DM/DMCreateLocalVector"))
161162
"""
162163
function DMCreateLocalVector end
163164

164-
@for_petsc function DMCreateLocalVector(dm::DM{$PetscLib})
165+
@for_petsc function DMCreateLocalVector(dm::AbstractDM{$PetscLib})
165166
vec = Vec{$PetscScalar}(C_NULL)
166167

167168
@chk ccall(
@@ -185,7 +186,7 @@ $(_doc_external("DM/DMCreateGlobalVector"))
185186
"""
186187
function DMCreateGlobalVector end
187188

188-
@for_petsc function DMCreateGlobalVector(dm::DM{$PetscLib})
189+
@for_petsc function DMCreateGlobalVector(dm::AbstractDM{$PetscLib})
189190
vec = Vec{$PetscScalar}(C_NULL)
190191

191192
@chk ccall(
@@ -201,7 +202,7 @@ end
201202

202203
"""
203204
DMLocalToGlobal!(
204-
dm::DM
205+
dm::AbstractDM
205206
local_vec::AbstractVec,
206207
mode::InsertMode,
207208
global_vec::AbstractVec,
@@ -215,7 +216,7 @@ $(_doc_external("DM/DMLocalToGlobal"))
215216
function DMLocalToGlobal! end
216217

217218
@for_petsc function DMLocalToGlobal!(
218-
dm::DM{$PetscLib},
219+
dm::AbstractDM{$PetscLib},
219220
local_vec::AbstractVec,
220221
mode::InsertMode,
221222
global_vec::AbstractVec,
@@ -249,7 +250,7 @@ $(_doc_external("DM/DMGlobalToLocal"))
249250
function DMGlobalToLocal! end
250251

251252
@for_petsc function DMGlobalToLocal!(
252-
dm::DM{$PetscLib},
253+
dm::AbstractDM{$PetscLib},
253254
global_vec::AbstractVec,
254255
mode::InsertMode,
255256
local_vec::AbstractVec,
@@ -269,34 +270,34 @@ end
269270

270271
"""
271272
DMGetCoordinateDM(
272-
dm::DM
273+
dm::AbstractDM
273274
)
274275
275-
Create a `DM` for the coordinates of `dm`.
276+
Create a `coord_dm` for the coordinates of `dm`.
276277
277278
# External Links
278279
$(_doc_external("DM/DMGetCoordinateDM"))
279280
"""
280281
function DMGetCoordinateDM end
281282

282-
@for_petsc function DMGetCoordinateDM(
283-
dm::DM{$PetscLib},
284-
)
285-
coord_dm = DM{$PetscLib}(C_NULL)
283+
@for_petsc function DMGetCoordinateDM(dm::AbstractDM{$PetscLib})
284+
coord_dm = empty(dm)
286285
@chk ccall(
287286
(:DMGetCoordinateDM, $petsc_library),
288287
PetscErrorCode,
289288
(CDM, Ptr{CDM}),
290289
dm,
291290
coord_dm,
292291
)
292+
# If this fails then the `empty` call above is probably a bad idea!
293+
@assert gettype(dm) == gettype(coord_dm)
293294

294295
return coord_dm
295296
end
296297

297298
"""
298299
DMGetCoordinatesLocal(
299-
dm::DM
300+
dm::AbstractDM
300301
)
301302
302303
Gets a local vector with the coordinates associated with `dm`.
@@ -306,9 +307,7 @@ $(_doc_external("DM/DMGetCoordinatesLocal"))
306307
"""
307308
function DMGetCoordinatesLocal end
308309

309-
@for_petsc function DMGetCoordinatesLocal(
310-
dm::DM{$PetscLib},
311-
)
310+
@for_petsc function DMGetCoordinatesLocal(dm::AbstractDM{$PetscLib})
312311
coord_vec = Vec{$PetscScalar}(C_NULL)
313312
@chk ccall(
314313
(:DMGetCoordinatesLocal, $petsc_library),

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
@@ -57,7 +71,7 @@ function DMDACreate1d end
5771
@assert length(points_per_proc) == MPI.Comm_size(comm)
5872
points_per_proc
5973
end
60-
da = DM{$PetscLib}(C_NULL, opts)
74+
da = DMDA{$PetscLib}(C_NULL, opts)
6175
with(da.opts) do
6276
@chk ccall(
6377
(:DMDACreate1d, $petsc_library),
@@ -152,7 +166,7 @@ function DMDACreate2d end
152166
@assert length(points_per_proc_y) == procs_y
153167
points_per_proc_y
154168
end
155-
da = DM{$PetscLib}(C_NULL, opts)
169+
da = DMDA{$PetscLib}(C_NULL, opts)
156170
with(da.opts) do
157171
@chk ccall(
158172
(:DMDACreate2d, $petsc_library),
@@ -274,7 +288,7 @@ function DMDACreate3d end
274288
@assert length(points_per_proc_z) == procs_z
275289
points_per_proc_z
276290
end
277-
da = DM{$PetscLib}(C_NULL, opts)
291+
da = DMDA{$PetscLib}(C_NULL, opts)
278292
with(da.opts) do
279293
@chk ccall(
280294
(: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)