diff --git a/mikeio/dfsu/_mesh.py b/mikeio/dfsu/_mesh.py index 463162354..bc9897a16 100644 --- a/mikeio/dfsu/_mesh.py +++ b/mikeio/dfsu/_mesh.py @@ -89,6 +89,10 @@ def node_coordinates(self) -> np.ndarray: """Coordinates of nodes""" return self.geometry.node_coordinates + @node_coordinates.setter + def node_coordinates(self, v: np.ndarray) -> None: + self.geometry.node_coordinates = v + @property def n_nodes(self) -> int: """Number of nodes""" diff --git a/mikeio/spatial/_FM_geometry.py b/mikeio/spatial/_FM_geometry.py index cc56e9057..264c9592b 100644 --- a/mikeio/spatial/_FM_geometry.py +++ b/mikeio/spatial/_FM_geometry.py @@ -238,7 +238,7 @@ def __init__( reindex: bool = False, ) -> None: super().__init__(projection=projection) - self.node_coordinates = np.asarray(node_coordinates) + self._node_coordinates = np.asarray(node_coordinates) n_nodes = len(node_coordinates) self._codes = ( @@ -355,6 +355,20 @@ def n_elements(self) -> int: """Number of elements""" return len(self._element_ids) + @property + def node_coordinates(self) -> np.ndarray: + return self._node_coordinates + + @node_coordinates.setter + def node_coordinates(self, v: np.ndarray) -> None: + self._node_coordinates = v + del self.element_coordinates + + @cached_property + def element_coordinates(self) -> np.ndarray: + """Center coordinates of each element""" + return self._calc_element_coordinates() + @property def element_ids(self) -> np.ndarray: return self._element_ids @@ -505,11 +519,6 @@ def is_tri_only(self) -> bool: """Does the mesh consist of triangles only?""" return self.max_nodes_per_element == 3 or self.max_nodes_per_element == 6 - @cached_property - def element_coordinates(self) -> np.ndarray: - """Center coordinates of each element""" - return self._calc_element_coordinates() - @cached_property def _tree2d(self) -> cKDTree: xy = self.element_coordinates[:, :2] diff --git a/tests/test_mesh.py b/tests/test_mesh.py index 8944b68ea..4a97c7e07 100644 --- a/tests/test_mesh.py +++ b/tests/test_mesh.py @@ -86,10 +86,16 @@ def test_get_bad_node_coordinates(tri_mesh): def test_set_z(tri_mesh): msh = tri_mesh + nc = msh.node_coordinates.copy() + assert msh.element_coordinates[:, 2].min() == pytest.approx(-10.938001) zn = msh.node_coordinates[:, 2] - zn[zn < -3] = -3 + nc[zn < -3, 2] = -3 + + # setting the property, triggers update of element coordinates + msh.node_coordinates = nc + + assert msh.element_coordinates[:, 2].min() == pytest.approx(-3) - msh.node_coordinates[:, 2] = zn zn = msh.node_coordinates[:, 2] assert zn.min() == -3