Skip to content

Commit 6c0a6c0

Browse files
committed
Add, test remove_unused_vertices, use in Firedrake tests
1 parent b332cb7 commit 6c0a6c0

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

meshmode/mesh/processing.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
.. autofunction:: map_mesh
5757
.. autofunction:: affine_map
5858
.. autofunction:: rotate_mesh_around_axis
59+
60+
.. autofunction:: remove_unused_vertices
5961
"""
6062

6163

@@ -1594,4 +1596,36 @@ def make_mesh_grid(
15941596

15951597
# }}}
15961598

1599+
1600+
# {{{ remove_unused_vertices
1601+
1602+
def remove_unused_vertices(mesh: Mesh) -> Mesh:
1603+
if mesh.vertices is None:
1604+
raise ValueError("mesh must have vertices")
1605+
1606+
def not_none(vi: Optional[np.ndarray]) -> np.ndarray:
1607+
if vi is None:
1608+
raise ValueError("mesh element groups must have vertex indices")
1609+
return vi
1610+
1611+
used_vertices = np.unique(np.sort(np.concatenate([
1612+
not_none(grp.vertex_indices).reshape(-1)
1613+
for grp in mesh.groups
1614+
])))
1615+
1616+
used_flags: np.ndarray = np.zeros(mesh.nvertices, dtype=np.bool_)
1617+
used_flags[used_vertices] = 1
1618+
new_vertex_indices = np.cumsum(used_flags, dtype=mesh.vertex_id_dtype) - 1
1619+
new_vertex_indices[~used_flags] = -1
1620+
1621+
return replace(
1622+
mesh,
1623+
vertices=mesh.vertices[:, used_flags],
1624+
groups=tuple(
1625+
replace(grp, vertex_indices=new_vertex_indices[grp.vertex_indices])
1626+
for grp in mesh.groups
1627+
))
1628+
1629+
# }}}
1630+
15971631
# vim: foldmethod=marker

test/test_firedrake_interop.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def fspace_degree(request):
7878

7979
def make_mm_mesh(name: str) -> Mesh:
8080
from meshmode.mesh.io import read_gmsh
81-
return read_gmsh(name)
81+
from meshmode.mesh.processing import remove_unused_vertices
82+
return remove_unused_vertices(read_gmsh(name))
8283

8384

8485
def make_firedrake_mesh(name: str):

test/test_mesh.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,30 @@ def test_mesh_copy():
256256
mesh.copy()
257257

258258

259+
def test_remove_unused_vertices():
260+
mesh = mgen.generate_box_mesh(3*(np.linspace(0, 1, 5),))
261+
262+
assert mesh.vertices is not None
263+
264+
mesh2 = replace(
265+
mesh,
266+
vertices=np.concatenate([np.zeros((3, 1)), mesh.vertices], axis=1),
267+
groups=tuple(
268+
replace(
269+
grp,
270+
vertex_indices=grp.vertex_indices + 1
271+
)
272+
for grp in mesh.groups
273+
))
274+
275+
mesh3 = mproc.remove_unused_vertices(mesh2)
276+
277+
assert np.array_equal(mesh3.vertices, mesh.vertices)
278+
assert np.array_equal(
279+
mesh3.groups[0].vertex_indices,
280+
mesh.groups[0].vertex_indices)
281+
282+
259283
# {{{ as_python stringification
260284

261285
def test_mesh_as_python():

0 commit comments

Comments
 (0)