Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cached element coordinates on modification #715

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mikeio/dfsu/_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
21 changes: 15 additions & 6 deletions mikeio/spatial/_FM_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
10 changes: 8 additions & 2 deletions tests/test_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading