Skip to content

Commit

Permalink
Merge pull request #995 from nschloe/gh-993
Browse files Browse the repository at this point in the history
fix for remove_orphaned_nodes()
  • Loading branch information
nschloe authored Dec 21, 2020
2 parents 23f7228 + fa5f894 commit 0438cd9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
12 changes: 9 additions & 3 deletions meshio/_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def remove_orphaned_nodes(self):
"""Remove nodes which don't belong to any cell."""
all_cells_flat = numpy.concatenate([c.data.flat for c in self.cells])
orphaned_nodes = numpy.setdiff1d(numpy.arange(len(self.points)), all_cells_flat)

if len(orphaned_nodes) == 0:
return

self.points = numpy.delete(self.points, orphaned_nodes, axis=0)
# also adapt the point data
self.point_data = {
Expand All @@ -133,19 +137,21 @@ def remove_orphaned_nodes(self):

# reset GLOBAL_ID
if "GLOBAL_ID" in self.point_data:
n = len(self.point_data["GLOBAL_ID"])
assert numpy.all(self.point_data["GLOBAL_ID"] == numpy.arange(1, n + 1))
self.point_data["GLOBAL_ID"] = numpy.arange(1, len(self.points) + 1)

# We now need to adapt the cells too.
diff = numpy.zeros(len(all_cells_flat), dtype=all_cells_flat.dtype)
for orphan in orphaned_nodes:
diff[numpy.argwhere(all_cells_flat > orphan)] += 1
all_cells_flat -= diff
k = 0
i = 0
for k, c in enumerate(self.cells):
s = c.data.shape
n = numpy.prod(s)
self.cells[k] = CellBlock(c.type, all_cells_flat[k : k + n].reshape(s))
k += n
self.cells[k] = CellBlock(c.type, all_cells_flat[i : i + n].reshape(s))
i += n

def prune_z_0(self, tol: float = 1.0e-13):
"""Remove third (z) component of points if it is 0 everywhere (up to a
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = meshio
version = 4.3.6
version = 4.3.7
author = Nico Schlömer et al.
author_email = [email protected]
description = I/O for many mesh formats
Expand Down
5 changes: 3 additions & 2 deletions test/test_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def test_print_prune():
def test_remove_orphaned():
points = numpy.array(
[
[3.14, 2.71], # orphaned
[0.0, 0.0],
[1.0, 0.0],
[0.0, 1.0],
[3.14, 2.71], # orphaned
]
)
cells = numpy.array([[0, 1, 2]])
cells = numpy.array([[1, 2, 3]])
a = {"a": numpy.array([0.1, 0.2, 0.3, 0.4])}
mesh = meshio.Mesh(points, {"triangle": cells}, point_data=a)
mesh.remove_orphaned_nodes()
Expand All @@ -39,6 +39,7 @@ def test_remove_orphaned():
# make sure the dict `a` wasn't changed,
# <https://github.com/nschloe/meshio/pull/994>
assert len(a["a"]) == 4
assert numpy.all(mesh.cells[0].data == [0, 1, 2])


def test_cells_dict():
Expand Down

0 comments on commit 0438cd9

Please sign in to comment.