Skip to content

Commit 18b2513

Browse files
ksagiyamconnorjward
andcommitted
remove mesh.init()
Co-authored-by: Connor Ward <c.ward20@imperial.ac.uk>
1 parent d85339c commit 18b2513

19 files changed

+218
-193
lines changed

firedrake/assemble.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -981,10 +981,6 @@ def wrapper(self, *args, **kwargs):
981981

982982
def __init__(self, form, bcs=None, form_compiler_parameters=None):
983983
super().__init__(form, bcs=bcs, form_compiler_parameters=form_compiler_parameters)
984-
# Ensure mesh is 'initialised' as we could have got here without building a
985-
# function space (e.g. if integrating a constant).
986-
for mesh in form.ufl_domains():
987-
mesh.init()
988984
if any(c.dat.dtype != ScalarType for c in form.coefficients()):
989985
raise ValueError("Cannot assemble a form containing coefficients where the "
990986
"dtype is not the PETSc scalar type.")

firedrake/checkpointing.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,6 @@ def save_mesh(self, mesh, distribution_name=None, permutation_name=None):
566566
:kwarg distribution_name: the name under which distribution is saved; if `None`, auto-generated name will be used.
567567
:kwarg permutation_name: the name under which permutation is saved; if `None`, auto-generated name will be used.
568568
"""
569-
mesh.init()
570569
# Handle extruded mesh
571570
tmesh = mesh.topology
572571
if mesh.extruded:
@@ -669,7 +668,6 @@ def save_mesh(self, mesh, distribution_name=None, permutation_name=None):
669668
@PETSc.Log.EventDecorator("SaveMeshTopology")
670669
def _save_mesh_topology(self, tmesh):
671670
# -- Save DMPlex --
672-
tmesh.init()
673671
topology_dm = tmesh.topology_dm
674672
tmesh_name = topology_dm.getName()
675673
distribution_name = tmesh._distribution_name
@@ -1048,7 +1046,6 @@ def load_mesh(self, name=DEFAULT_MESH_NAME, reorder=None, distribution_parameter
10481046
# -- Load mesh topology --
10491047
base_tmesh_name = self.get_attr(path, PREFIX_EXTRUDED + "_base_mesh")
10501048
base_tmesh = self._load_mesh_topology(base_tmesh_name, reorder, distribution_parameters)
1051-
base_tmesh.init()
10521049
periodic = self.get_attr(path, PREFIX_EXTRUDED + "_periodic") if self.has_attr(path, PREFIX_EXTRUDED + "_periodic") else False
10531050
variable_layers = self.get_attr(path, PREFIX_EXTRUDED + "_variable_layers")
10541051
if variable_layers:
@@ -1106,9 +1103,6 @@ def load_mesh(self, name=DEFAULT_MESH_NAME, reorder=None, distribution_parameter
11061103
# tmesh.topology_dm has already been redistributed.
11071104
path = self._path_to_mesh(tmesh_name, name)
11081105
# Load firedrake coordinates directly.
1109-
# When implementing checkpointing for MeshHierarchy in the future,
1110-
# we will need to postpone calling tmesh.init().
1111-
tmesh.init()
11121106
coord_element = self._load_ufl_element(path, PREFIX + "_coordinate_element")
11131107
coord_name = self.get_attr(path, PREFIX + "_coordinates")
11141108
coordinates = self._load_function_topology(tmesh, coord_element, coord_name)
@@ -1194,7 +1188,13 @@ def _load_mesh_topology(self, tmesh_name, reorder, distribution_parameters):
11941188
plex.distributionSetName(distribution_name)
11951189
sfXB = plex.topologyLoad(self.viewer)
11961190
plex.distributionSetName(None)
1191+
plex.labelsLoad(self.viewer, sfXB)
11971192
self.viewer.popFormat()
1193+
# These labels are distribution dependent.
1194+
# We should be able to save/load labels selectively.
1195+
plex.removeLabel("pyop2_core")
1196+
plex.removeLabel("pyop2_owned")
1197+
plex.removeLabel("pyop2_ghost")
11981198
if load_distribution_permutation:
11991199
chart_size = np.empty(1, dtype=utils.IntType)
12001200
chart_sizes_iset = PETSc.IS().createGeneral(chart_size, comm=self._comm)
@@ -1220,21 +1220,10 @@ def _load_mesh_topology(self, tmesh_name, reorder, distribution_parameters):
12201220
distribution_parameters=distribution_parameters, sfXB=sfXB, perm_is=perm_is,
12211221
distribution_name=distribution_name, permutation_name=permutation_name,
12221222
comm=self.comm)
1223-
self.viewer.pushFormat(format=format)
1224-
# tmesh.topology_dm has already been redistributed.
1225-
sfXCtemp = tmesh.sfXB.compose(tmesh.sfBC) if tmesh.sfBC is not None else tmesh.sfXB
1226-
plex.labelsLoad(self.viewer, sfXCtemp)
1227-
self.viewer.popFormat()
1228-
# These labels are distribution dependent.
1229-
# We should be able to save/load labels selectively.
1230-
plex.removeLabel("pyop2_core")
1231-
plex.removeLabel("pyop2_owned")
1232-
plex.removeLabel("pyop2_ghost")
12331223
return tmesh
12341224

12351225
@PETSc.Log.EventDecorator("LoadFunctionSpace")
12361226
def _load_function_space(self, mesh, name):
1237-
mesh.init()
12381227
mesh_key = self._generate_mesh_key_from_names(mesh.name,
12391228
mesh.topology._distribution_name,
12401229
mesh.topology._permutation_name)
@@ -1271,7 +1260,6 @@ def _load_function_space(self, mesh, name):
12711260

12721261
@PETSc.Log.EventDecorator("LoadFunctionSpaceTopology")
12731262
def _load_function_space_topology(self, tmesh, element):
1274-
tmesh.init()
12751263
if element.family() == "Real":
12761264
return impl.RealFunctionSpace(tmesh, element, "unused_name")
12771265
tmesh_key = self._generate_mesh_key_from_names(tmesh.name,

firedrake/cython/dmcommon.pyx

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,7 +3818,8 @@ def create_halo_exchange_sf(PETSc.DM dm):
38183818
def submesh_create(PETSc.DM dm,
38193819
PetscInt subdim,
38203820
label_name,
3821-
PetscInt label_value):
3821+
PetscInt label_value,
3822+
PetscBool ignore_label_halo):
38223823
"""Create submesh.
38233824
38243825
Parameters
@@ -3831,6 +3832,8 @@ def submesh_create(PETSc.DM dm,
38313832
Name of the label
38323833
label_value : int
38333834
Value in the label
3835+
ignore_label_halo : bool
3836+
If labeled points in the halo are ignored.
38343837
38353838
"""
38363839
cdef:
@@ -3860,7 +3863,7 @@ def submesh_create(PETSc.DM dm,
38603863
CHKERR(ISRestoreIndices(stratum_is, &stratum_indices))
38613864
CHKERR(ISDestroy(&stratum_is))
38623865
# Make submesh using temp_label.
3863-
CHKERR(DMPlexFilter(dm.dm, temp_label.dmlabel, label_value, PETSC_FALSE, PETSC_TRUE, &ownership_transfer_sf.sf, &subdm.dm))
3866+
CHKERR(DMPlexFilter(dm.dm, temp_label.dmlabel, label_value, ignore_label_halo, PETSC_TRUE, &ownership_transfer_sf.sf, &subdm.dm))
38643867
# Destroy temp_label.
38653868
dm.removeLabel(temp_label_name)
38663869
subdm.removeLabel(temp_label_name)
@@ -4112,3 +4115,36 @@ def submesh_create_cell_closure(
41124115
CHKERR(PetscFree(subpoint_indices_inv))
41134116
CHKERR(ISRestoreIndices(subpoint_is.iset, &subpoint_indices))
41144117
return subcell_closure
4118+
4119+
4120+
@cython.boundscheck(False)
4121+
@cython.wraparound(False)
4122+
def get_dm_cell_types(PETSc.DM dm):
4123+
"""Return all cell types in the mesh.
4124+
4125+
Parameters
4126+
----------
4127+
dm : PETSc.DM
4128+
The parent dm.
4129+
4130+
Returns
4131+
-------
4132+
tuple
4133+
Tuple of all cell types in the mesh.
4134+
4135+
"""
4136+
cdef:
4137+
PetscInt cStart, cEnd, c
4138+
np.ndarray found, found_all
4139+
PetscDMPolytopeType celltype
4140+
4141+
cStart, cEnd = dm.getHeightStratum(0)
4142+
found = np.zeros((DM_NUM_POLYTOPES, ), dtype=IntType)
4143+
found_all = np.zeros((DM_NUM_POLYTOPES, ), dtype=IntType)
4144+
for c in range(cStart, cEnd):
4145+
CHKERR(DMPlexGetCellType(dm.dm, c, &celltype))
4146+
found[celltype] = 1
4147+
dm.comm.tompi4py().Allreduce(found, found_all, op=MPI.MAX)
4148+
return tuple(
4149+
polytope_type_enum for polytope_type_enum, found in enumerate(found_all) if found
4150+
)

firedrake/cython/petschdr.pxi

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ cdef extern from "petscsys.h" nogil:
2929
int PetscFree2(void*,void*)
3030
int PetscSortIntWithArray(PetscInt,PetscInt[],PetscInt[])
3131

32+
cdef extern from "petscdmtypes.h" nogil:
33+
ctypedef enum PetscDMPolytopeType "DMPolytopeType":
34+
DM_POLYTOPE_POINT
35+
DM_POLYTOPE_SEGMENT
36+
DM_POLYTOPE_POINT_PRISM_TENSOR
37+
DM_POLYTOPE_TRIANGLE
38+
DM_POLYTOPE_QUADRILATERAL
39+
DM_POLYTOPE_SEG_PRISM_TENSOR
40+
DM_POLYTOPE_TETRAHEDRON
41+
DM_POLYTOPE_HEXAHEDRON
42+
DM_POLYTOPE_TRI_PRISM
43+
DM_POLYTOPE_TRI_PRISM_TENSOR
44+
DM_POLYTOPE_QUAD_PRISM_TENSOR
45+
DM_POLYTOPE_PYRAMID
46+
DM_POLYTOPE_FV_GHOST
47+
DM_POLYTOPE_INTERIOR_GHOST
48+
DM_POLYTOPE_UNKNOWN
49+
DM_POLYTOPE_UNKNOWN_CELL
50+
DM_POLYTOPE_UNKNOWN_FACE
51+
DM_NUM_POLYTOPES
52+
3253
cdef extern from "petscdmplex.h" nogil:
3354
int DMPlexGetHeightStratum(PETSc.PetscDM,PetscInt,PetscInt*,PetscInt*)
3455
int DMPlexGetDepthStratum(PETSc.PetscDM,PetscInt,PetscInt*,PetscInt*)
@@ -56,6 +77,9 @@ cdef extern from "petscdmplex.h" nogil:
5677
int DMPlexGetSubpointMap(PETSc.PetscDM,PETSc.PetscDMLabel*)
5778
int DMPlexSetSubpointMap(PETSc.PetscDM,PETSc.PetscDMLabel)
5879

80+
int DMPlexSetCellType(PETSc.PetscDM,PetscInt,PetscDMPolytopeType)
81+
int DMPlexGetCellType(PETSc.PetscDM,PetscInt,PetscDMPolytopeType*)
82+
5983
cdef extern from "petscdmlabel.h" nogil:
6084
struct _n_DMLabel
6185
ctypedef _n_DMLabel* DMLabel "DMLabel"

firedrake/functionspaceimpl.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ def collapse(self):
359359
@classmethod
360360
def make_function_space(cls, mesh, element, name=None):
361361
r"""Factory method for :class:`.WithGeometryBase`."""
362-
mesh.init()
363362
topology = mesh.topology
364363
# Create a new abstract (Mixed/Real)FunctionSpace, these are neither primal nor dual.
365364
if type(element) is finat.ufl.MixedElement:

0 commit comments

Comments
 (0)