Skip to content

Commit

Permalink
Merge pull request #1218 from nschloe/copy
Browse files Browse the repository at this point in the history
mesh.copy(), mesh.deduplicate_points()
  • Loading branch information
nschloe authored Nov 17, 2021
2 parents ecda7cc + 941e96a commit 38d6679
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = meshio
version = 5.0.2
version = 5.0.3
author = Nico Schlömer et al.
author_email = [email protected]
description = I/O for many mesh formats
Expand Down Expand Up @@ -39,6 +39,7 @@ package_dir =
packages = find:
install_requires =
importlib_metadata;python_version<"3.8"
npx >= 0.0.22
numpy
python_requires = >=3.7

Expand Down
27 changes: 27 additions & 0 deletions src/meshio/_mesh.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import collections
import copy
import warnings

import npx
import numpy as np

from ._common import _topological_dimension, num_nodes_per_cell
Expand Down Expand Up @@ -132,6 +134,9 @@ def __repr__(self):

return "\n".join(lines)

def copy(self):
return copy.deepcopy(self)

def prune(self):
# nschloe, 2020-11:
warnings.warn(
Expand Down Expand Up @@ -407,3 +412,25 @@ def int_data_to_sets(self):
# remove the cell data
for key in keys:
del self.point_data[key]

def deduplicate_points(self, tol: float) -> int:
num_points_before = len(self.points)

unique_points, idx, inv = npx.unique(
self.points, tol=tol, axis=0, return_index=True, return_inverse=True
)
self.points = unique_points
num_removed_points = num_points_before - len(self.points)

# re-index cell blocks
self.cells = [CellBlock(cb.type, inv[cb.data]) for cb in self.cells]

# reset point data
for key, values in self.point_data.items():
self.point_data[key] = values[idx]

# reset point sets
for key, values in self.point_sets.items():
self.point_sets[key] = inv[values]

return num_removed_points
35 changes: 35 additions & 0 deletions tests/test_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,41 @@ def test_gh_1165():
assert_equal(mesh.cell_sets, {"test": [[], [1]], "sets": [[0, 1], [0, 2, 3]]})


def test_copy():
mesh = helpers.tri_mesh
mesh2 = mesh.copy()

assert np.all(mesh.points == mesh2.points)
assert not np.may_share_memory(mesh.points, mesh2.points)


def test_deduplicate_points():
points = np.array(
[
[0.0, 0.0],
[1.0, 0.0],
[1.0, 1.0],
[1.0, 0.0],
[1.0, 1.0],
[2.0, 0.0],
]
)
point_data = {"a": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]}
point_sets = {"b": [0, 4, 5]}
cells = [[0, 1, 2], [3, 4, 5]]
mesh = meshio.Mesh(
points, {"triangle": cells}, point_data=point_data, point_sets=point_sets
)

num_removed_points = mesh.deduplicate_points(1.0e-10)
assert num_removed_points == 2
assert np.all(mesh.points == [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [2.0, 0.0]])
assert np.all(mesh.cells[0].data == [[0, 1, 2], [1, 2, 3]])

assert np.all(mesh.point_data["a"] == [0.1, 0.2, 0.3, 0.6])
assert np.all(mesh.point_sets["b"] == [0, 2, 3])


if __name__ == "__main__":
# test_sets_to_int_data()
test_int_data_to_sets()

0 comments on commit 38d6679

Please sign in to comment.