Skip to content

Refactor mesh closures - #5719

Open
connorjward wants to merge 28 commits into
mainfrom
connorjward/extr-closure-refactor
Open

connorjward wants to merge 28 commits into
mainfrom
connorjward/extr-closure-refactor

Conversation

@connorjward

@connorjward connorjward commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

These changes unify the approaches used to determine cell closures for regular and extruded meshes. The previous approach was not very nicely designed. This is going to be useful for #5685.

The fundamental idea is to split the closure generation into two stages:

  1. Traverse the DM, load the closures, and permute the outputs from DM closure ordering to FIAT closure ordering.
  2. Permute this closure in order to have a consistent orientation of facets and thus not need to transform things during packing.

For non-extruded meshes the permutations can be collected at the same time as the closures themselves (e.g. dmcommon.cell_closures_and_perms_simplex). However, for extruded meshes this is not the case because the closures come from the extruded DM, but the permutations come from extruding the base permutation.

In addition to just being a lot more readable and unified, the approach here substantially decouples the connection between the extruded mesh and its base mesh, which will facilitate more general extruded meshes (e.g. submeshing, distribution in the extruded direction) where the base mesh needn't exist.

@connorjward
connorjward added this pull request to stack #5715 November 25, 2026 17:35
Comment thread firedrake/cython/dmcommon.pyx Resolved
Comment thread firedrake/cython/dmcommon.pyx Resolved
Base automatically changed from connorjward/minor-refactor to main November 27, 2026 12:03
@connorjward
connorjward force-pushed the connorjward/extr-closure-refactor branch from 4d3a9b0 to 357ff88 Compare November 27, 2026 12:03
The separation of closures and permutations is approximately working.
Approach appears to work for simplices, quads, extruded intervals, and
extruded prisms. Only missing pieces are extruded quads and hexes.

Substantial cleanup and performance fixes required.
…ions

Ultimately it isn't a big deal. It just affects a specific subset of
meshes that we cannot orient.

Most tests should now work I think, but substantial cleanup is
necessary.
@connorjward
connorjward force-pushed the connorjward/extr-closure-refactor branch from efd0bf3 to 0782b7b Compare December 2, 2026 23:35
Comment thread firedrake/mesh.py

map_data_renum = np.empty_like(map_data)
for i, (from_pt, to_pts) in enumerate(zip(from_dm_indices, map_data, strict=True)):
from_pt = from_dm_indices[i]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment thread firedrake/mesh.py
from_dim = self.cell_label

map_components = defaultdict(list)
for to_dim, size in self._closure_sizes[from_dim].items():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for to_dim, size in self._closure_sizes[from_dim].items():
for to_dim in self._closure_sizes[from_dim]:


CHKERR(PetscMalloc1(nverts_per_cell, &verts))
CHKERR(PetscMalloc1(nedges_per_cell, &edges))
CHKERR(PetscMalloc1(nfacets_per_cell, &facets))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code

Suggested change

PetscInt tdim, cell, v_start, v_end
PetscInt nverts_per_cell, nedges_per_cell, nfacets_per_cell
PetscInt *verts=NULL
PetscInt *facets=NULL

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code

Suggested change

Comment thread firedrake/mesh.py
# orientable submeshes. The practical implications of this are merely
# that one cannot have an oriented mesh that was first submeshed and
# then extruded.
assert not self._is_submesh_inheriting_parent_orientation, \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a NotImplementedError?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh. It's private code that a user will never touch.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then catch the error earlier if you want to disallow extruded Submesh

Comment thread firedrake/mesh.py Resolved
Comment thread firedrake/mesh.py Resolved
Comment thread firedrake/mesh.py Resolved
Comment thread firedrake/cython/dmcommon.pyx Resolved
Comment thread firedrake/cython/dmcommon.pyx Resolved
Comment thread firedrake/cython/dmcommon.pyx Resolved
orient them.

"""
cdef:

@pbrubeck pbrubeck Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to declare here vi, vj, ei, ej, fi, vert, edge and facet, and all the *_offset_* values. Without this, the cell loop runs at Python speed.

Comment on lines +881 to +882
closures = np.empty((ncells, nclosure), dtype=IntType)
closure_perms = np.empty_like(closures)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need to be declared

Comment on lines +1044 to +1045
cell_closure = np.empty((ncells, 9), dtype=IntType)
closure_perms = np.empty_like(cell_closure)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need to be declared

assert fi == 4

# The first vertex is given by the entry in cell_orientations.
cell = mesh._dm_to_entity_numbering_sec("cell").getOffset(c)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get the Section outside the loop and call PetscSectionGetOffset.

Comment thread firedrake/cython/dmcommon.pyx Resolved
Comment thread firedrake/cython/dmcommon.pyx Resolved
Comment thread firedrake/cython/dmcommon.pyx Resolved
Comment thread firedrake/cython/dmcommon.pyx Resolved
Comment thread firedrake/mesh.py
Comment on lines +1834 to +1835
# return readonly(map_data_renum)
return map_data_renum # FIXME

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXME

Comment thread firedrake/mesh.py
@@ -1572,7 +1831,8 @@
else:
to_pt_renum = to_numbering_sec.getOffset(to_pt)

@pbrubeck pbrubeck Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inside a double Python loop and is really slow. The loops can go away if you vectorize it with dmcommon.section_offsets.

Renumber the rows through the source section and the values through the target section, and keep -1 entries as they are.

Comment thread firedrake/cython/dmcommon.pyx Resolved
Comment thread firedrake/cython/dmcommon.pyx Resolved
dtype=IntType,
)

# Am I unit periodic in any direction? (only implemented for quads)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a segfault for ExtrudedMesh(PeriodicRectangleMesh(4, 1, quadrilateral=True), 3).

  • Cause: in flat_cell_closures_extruded, get_transitive_closure overwrites nclosure with the DMPlex closure size. For these meshes that size is 19, not 27. The loop for i in range(nclosure) at dmcommon.pyx:796 then fills only 19 of the 27 columns, and the other 8 stay as np.empty garbage.

  • Fix: loop over range(closure_perm.shape[0]). With that one-line change all three cases build correctly: x-periodic, y-periodic and doubly periodic.

  • The new test_one_element_advection[x/y] probably passes on CI only by luck, because np.empty happens to return clean memory there.

nclosure = closure_perm.size
closures = np.empty((ncells, nclosure), dtype=IntType)
for cell in range(ncells):
get_transitive_closure(dm.dm, cell, PETSC_TRUE, &nclosure, &closure)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This overwrites nclosure

closures[cell, i] = closure_tmp[closure_perm[i]]

else:
for i in range(nclosure):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for i in range(nclosure):
for i in range(closure_perm.size):

Comment thread firedrake/mesh.py Resolved
connorjward and others added 2 commits December 4, 2026 17:21
Co-authored-by: Pablo Brubeck <brubeck@protonmail.com>
Co-authored-by: Connor Ward <c.ward20@imperial.ac.uk>
Co-authored-by: Connor Ward <c.ward20@imperial.ac.uk>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants