From 3a10c401a8e51ee0f9dc655d800fbfa43fc31b4d Mon Sep 17 00:00:00 2001 From: Luca Carniato Date: Mon, 2 Sep 2024 08:22:09 +0200 Subject: [PATCH 1/5] Add mesh2d_get_filtered_face_polygons api and unit test --- meshkernel/meshkernel.py | 46 +++++++++++++++++++++++++++++++++++++ meshkernel/py_structures.py | 6 +++++ tests/test_mesh2d_basics.py | 37 +++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/meshkernel/meshkernel.py b/meshkernel/meshkernel.py index 18c144c3..c227a89e 100644 --- a/meshkernel/meshkernel.py +++ b/meshkernel/meshkernel.py @@ -459,6 +459,52 @@ def mesh2d_get_face_polygons(self, num_edges: int) -> GeometryList: return face_polygons + def mesh2d_get_filtered_face_polygons(self, + filter: Mesh2d.Metric, + min_value: float, + max_value: float) -> GeometryList: + """Gets the polygons matching the metric value within the minimum and maximum value. + + Args: + + filter (Mesh2d.Metric): The metric used to filter the locations + min_value(float): The minimum value of the metric. + max_value(float): The maximum value of the metric. + + Returns: + GeometryList: The resulting face polygons + """ + c_geometry_list_dimension = c_int() + + self._execute_function( + self.lib.mkernel_mesh2d_get_filtered_face_polygons_dimension, + self._meshkernelid, + c_int(filter), + c_double(min_value), + c_double(max_value), + byref(c_geometry_list_dimension), + ) + + n_coordinates = c_geometry_list_dimension.value + x_coordinates = np.empty(n_coordinates, dtype=np.double) + y_coordinates = np.empty(n_coordinates, dtype=np.double) + + face_polygons = GeometryList( + x_coordinates=x_coordinates, y_coordinates=y_coordinates + ) + c_face_polygons = CGeometryList.from_geometrylist(face_polygons) + + self._execute_function( + self.lib.mkernel_mesh2d_get_filtered_face_polygons, + self._meshkernelid, + c_int(filter), + c_double(min_value), + c_double(max_value), + byref(c_face_polygons), + ) + + return face_polygons + def mesh2d_get_node_index(self, x: float, y: float, search_radius: float) -> int: """Finds the node closest to a point within a given search radius. diff --git a/meshkernel/py_structures.py b/meshkernel/py_structures.py index 126a51c7..74b1c85e 100644 --- a/meshkernel/py_structures.py +++ b/meshkernel/py_structures.py @@ -118,6 +118,12 @@ class Mesh2d: """ + @unique + class Metric(IntEnum): + """Different filtering metrics on a 2D mesh.""" + + ORTHOGONALITY = 0 + def __init__( self, node_x=np.empty(0, dtype=np.double), diff --git a/tests/test_mesh2d_basics.py b/tests/test_mesh2d_basics.py index 28a2ca4c..0c88ac89 100644 --- a/tests/test_mesh2d_basics.py +++ b/tests/test_mesh2d_basics.py @@ -18,6 +18,7 @@ MeshRefinementParameters, ProjectionType, RefinementType, + Mesh2dLocation, ) cases_projection_constructor = [(ProjectionType.CARTESIAN), (ProjectionType.SPHERICAL)] @@ -2305,3 +2306,39 @@ def test_mesh2d_deletion_and_get_orthogonality( values = values_at_locations_functions(mk).values mesh2d = mk.mesh2d_get() assert len(values) == len(mesh2d.edge_x) + + +def test_mesh2d_get_filtered_face_polygons(): + + """Test mesh2d_get_filtered_face_polygons, + getting the polygons of faces with all edges having bad orthogonality values + """ + mk = MeshKernel() + + edge_nodes = np.array([0, 1, + 1, 2, + 2, 3, + 0, 3, + 1, 4, + 0, 4, + 0, 5, + 3, 5, + 3, 6, + 2, 6, + 2, 7, + 1, 7], dtype=np.int32) + + node_x = np.array([57.0, 49.1, 58.9, 66.7, 48.8, 65.9, 67.0, 49.1], dtype=np.double) + node_y = np.array([23.6, 14.0, 6.9, 16.2, 23.4, 24.0, 7.2, 6.7], dtype=np.double) + + input_mesh2d = Mesh2d(node_x, node_y, edge_nodes) + mk.mesh2d_set(input_mesh2d) + + face_polygons = mk.mesh2d_get_filtered_face_polygons(Mesh2d.Metric.ORTHOGONALITY, 0.04, 1.0) + + expected_coordinates_x = np.array([57.0, 49.1, 58.9, 66.7, 57.0], dtype=np.double) + expected_coordinates_y = np.array([23.6, 14.0, 6.9, 16.2, 23.6], dtype=np.double) + + assert face_polygons.x_coordinates == approx(expected_coordinates_x, abs=1e-6) + assert face_polygons.y_coordinates == approx(expected_coordinates_y, abs=1e-6) + From d07a26ebe2965f6fb665892706aec40690a63fff Mon Sep 17 00:00:00 2001 From: Luca Carniato Date: Mon, 2 Sep 2024 08:23:32 +0200 Subject: [PATCH 2/5] Formatting --- meshkernel/meshkernel.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/meshkernel/meshkernel.py b/meshkernel/meshkernel.py index c227a89e..77f91ac8 100644 --- a/meshkernel/meshkernel.py +++ b/meshkernel/meshkernel.py @@ -459,10 +459,9 @@ def mesh2d_get_face_polygons(self, num_edges: int) -> GeometryList: return face_polygons - def mesh2d_get_filtered_face_polygons(self, - filter: Mesh2d.Metric, - min_value: float, - max_value: float) -> GeometryList: + def mesh2d_get_filtered_face_polygons( + self, filter: Mesh2d.Metric, min_value: float, max_value: float + ) -> GeometryList: """Gets the polygons matching the metric value within the minimum and maximum value. Args: From c2f355dbacb707723c5e8fa3166a860d34af516c Mon Sep 17 00:00:00 2001 From: Luca Carniato Date: Mon, 2 Sep 2024 08:33:50 +0200 Subject: [PATCH 3/5] Rename metric to property --- meshkernel/meshkernel.py | 4 ++-- meshkernel/py_structures.py | 4 ++-- tests/test_mesh2d_basics.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/meshkernel/meshkernel.py b/meshkernel/meshkernel.py index 77f91ac8..391048fd 100644 --- a/meshkernel/meshkernel.py +++ b/meshkernel/meshkernel.py @@ -460,13 +460,13 @@ def mesh2d_get_face_polygons(self, num_edges: int) -> GeometryList: return face_polygons def mesh2d_get_filtered_face_polygons( - self, filter: Mesh2d.Metric, min_value: float, max_value: float + self, filter: Mesh2d.Property, min_value: float, max_value: float ) -> GeometryList: """Gets the polygons matching the metric value within the minimum and maximum value. Args: - filter (Mesh2d.Metric): The metric used to filter the locations + filter (Mesh2d.Property): The metric used to filter the locations min_value(float): The minimum value of the metric. max_value(float): The maximum value of the metric. diff --git a/meshkernel/py_structures.py b/meshkernel/py_structures.py index 74b1c85e..6d240502 100644 --- a/meshkernel/py_structures.py +++ b/meshkernel/py_structures.py @@ -119,8 +119,8 @@ class Mesh2d: """ @unique - class Metric(IntEnum): - """Different filtering metrics on a 2D mesh.""" + class Property(IntEnum): + """Different properties on a 2D mesh.""" ORTHOGONALITY = 0 diff --git a/tests/test_mesh2d_basics.py b/tests/test_mesh2d_basics.py index 0c88ac89..5f8ce196 100644 --- a/tests/test_mesh2d_basics.py +++ b/tests/test_mesh2d_basics.py @@ -2334,7 +2334,7 @@ def test_mesh2d_get_filtered_face_polygons(): input_mesh2d = Mesh2d(node_x, node_y, edge_nodes) mk.mesh2d_set(input_mesh2d) - face_polygons = mk.mesh2d_get_filtered_face_polygons(Mesh2d.Metric.ORTHOGONALITY, 0.04, 1.0) + face_polygons = mk.mesh2d_get_filtered_face_polygons(Mesh2d.Property.ORTHOGONALITY, 0.04, 1.0) expected_coordinates_x = np.array([57.0, 49.1, 58.9, 66.7, 57.0], dtype=np.double) expected_coordinates_y = np.array([23.6, 14.0, 6.9, 16.2, 23.6], dtype=np.double) From a46a852cd802d939937a5753433f07f2e87cec95 Mon Sep 17 00:00:00 2001 From: Luca Carniato Date: Mon, 2 Sep 2024 14:37:13 +0200 Subject: [PATCH 4/5] Fix linting --- tests/test_mesh2d_basics.py | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/tests/test_mesh2d_basics.py b/tests/test_mesh2d_basics.py index 5f8ce196..95217795 100644 --- a/tests/test_mesh2d_basics.py +++ b/tests/test_mesh2d_basics.py @@ -13,12 +13,12 @@ InterpolationValues, MakeGridParameters, Mesh2d, + Mesh2dLocation, MeshKernel, MeshKernelError, MeshRefinementParameters, ProjectionType, RefinementType, - Mesh2dLocation, ) cases_projection_constructor = [(ProjectionType.CARTESIAN), (ProjectionType.SPHERICAL)] @@ -2309,24 +2309,15 @@ def test_mesh2d_deletion_and_get_orthogonality( def test_mesh2d_get_filtered_face_polygons(): - """Test mesh2d_get_filtered_face_polygons, getting the polygons of faces with all edges having bad orthogonality values - """ + """ mk = MeshKernel() - edge_nodes = np.array([0, 1, - 1, 2, - 2, 3, - 0, 3, - 1, 4, - 0, 4, - 0, 5, - 3, 5, - 3, 6, - 2, 6, - 2, 7, - 1, 7], dtype=np.int32) + edge_nodes = np.array( + [0, 1, 1, 2, 2, 3, 0, 3, 1, 4, 0, 4, 0, 5, 3, 5, 3, 6, 2, 6, 2, 7, 1, 7], + dtype=np.int32, + ) node_x = np.array([57.0, 49.1, 58.9, 66.7, 48.8, 65.9, 67.0, 49.1], dtype=np.double) node_y = np.array([23.6, 14.0, 6.9, 16.2, 23.4, 24.0, 7.2, 6.7], dtype=np.double) @@ -2334,11 +2325,12 @@ def test_mesh2d_get_filtered_face_polygons(): input_mesh2d = Mesh2d(node_x, node_y, edge_nodes) mk.mesh2d_set(input_mesh2d) - face_polygons = mk.mesh2d_get_filtered_face_polygons(Mesh2d.Property.ORTHOGONALITY, 0.04, 1.0) + face_polygons = mk.mesh2d_get_filtered_face_polygons( + Mesh2d.Property.ORTHOGONALITY, 0.04, 1.0 + ) expected_coordinates_x = np.array([57.0, 49.1, 58.9, 66.7, 57.0], dtype=np.double) expected_coordinates_y = np.array([23.6, 14.0, 6.9, 16.2, 23.6], dtype=np.double) assert face_polygons.x_coordinates == approx(expected_coordinates_x, abs=1e-6) assert face_polygons.y_coordinates == approx(expected_coordinates_y, abs=1e-6) - From 1c518b3135cdeb86ab1279bf0364147441ac774f Mon Sep 17 00:00:00 2001 From: Luca Carniato Date: Mon, 2 Sep 2024 16:15:48 +0200 Subject: [PATCH 5/5] Parameter rename --- meshkernel/meshkernel.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/meshkernel/meshkernel.py b/meshkernel/meshkernel.py index 391048fd..439621f4 100644 --- a/meshkernel/meshkernel.py +++ b/meshkernel/meshkernel.py @@ -460,13 +460,13 @@ def mesh2d_get_face_polygons(self, num_edges: int) -> GeometryList: return face_polygons def mesh2d_get_filtered_face_polygons( - self, filter: Mesh2d.Property, min_value: float, max_value: float + self, property: Mesh2d.Property, min_value: float, max_value: float ) -> GeometryList: """Gets the polygons matching the metric value within the minimum and maximum value. Args: - filter (Mesh2d.Property): The metric used to filter the locations + property (Mesh2d.Property): The property used to filter the locations min_value(float): The minimum value of the metric. max_value(float): The maximum value of the metric. @@ -478,7 +478,7 @@ def mesh2d_get_filtered_face_polygons( self._execute_function( self.lib.mkernel_mesh2d_get_filtered_face_polygons_dimension, self._meshkernelid, - c_int(filter), + c_int(property), c_double(min_value), c_double(max_value), byref(c_geometry_list_dimension), @@ -496,7 +496,7 @@ def mesh2d_get_filtered_face_polygons( self._execute_function( self.lib.mkernel_mesh2d_get_filtered_face_polygons, self._meshkernelid, - c_int(filter), + c_int(property), c_double(min_value), c_double(max_value), byref(c_face_polygons),