def test_mesh2d_delete_hanging_edges(): """Tests `mesh2d_delete_hanging_edges` by deleting 2 hanging edges in a simple Mesh2d 4* | 3---2---5* | | 0---1 """ mk = MeshKernel() node_x = np.array([0.0, 1.0, 1.0, 0.0, 0.0, 2.0], dtype=np.double) node_y = np.array([0.0, 0.0, 1.0, 1.0, 2.0, 1.0], dtype=np.double) edge_nodes = np.array([0, 1, 1, 2, 2, 3, 3, 0, 3, 4, 2, 5], dtype=np.int32) mesh2d = Mesh2d(node_x, node_y, edge_nodes) mk.mesh2d_set(mesh2d) mk.mesh2d_delete_hanging_edges() mesh2d = mk.mesh2d_get() assert mesh2d.node_x.size == 4 assert mesh2d.edge_x.size == 4 assert mesh2d.face_x.size == 1
def test_mesh2d_count_obtuse_triangles(): r"""Tests `_mesh2d_count_obtuse_triangles` on a 3x3 mesh with two obtuse triangles. 6---7---8 | / \ | 3---4---5 | \ / | 0---1---2 """ mk = MeshKernel() # Mesh with obtuse triangles (4, 5, 7 and 1, 5, 4) node_x = np.array([0.0, 1.0, 2.0, 0.0, 1.5, 2.0, 0.0, 1.0, 2.0], dtype=np.double) node_y = np.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0], dtype=np.double) edge_nodes = np.array( [ 0, 1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 0, 3, 1, 4, 2, 5, 3, 6, 4, 7, 5, 8, 1, 3, 1, 5, 3, 7, 5, 7, ], dtype=np.int32, ) mk.mesh2d_set(Mesh2d(node_x, node_y, edge_nodes)) n_obtuse_triangles = mk._mesh2d_count_obtuse_triangles() assert n_obtuse_triangles == 2
def test_mesh2d_merge_nodes(merging_distance: float, number_of_nodes: int): """Test if `mesh2d_merge_nodes` reduces the number of close nodes 4---3 | | 01--2 """ mk = MeshKernel() # Set up mesh edge_nodes = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 0], dtype=np.int32) node_x = np.array([0.0, 1e-3, 1.0, 1.0, 0.0], dtype=np.double) node_y = np.array([0.0, 0.0, 0.0, 1.0, 1.0], dtype=np.double) input_mesh2d = Mesh2d(node_x, node_y, edge_nodes) mk.mesh2d_set(input_mesh2d) # Define polygon where we want to merge x_coordinates = np.array([-1.0, 2.0, 2.0, -1.0, -1.0], dtype=np.double) y_coordinates = np.array([-1.0, -1.0, 2.0, 2.0, -1.0], dtype=np.double) geometry_list = GeometryList(x_coordinates, y_coordinates) mk.mesh2d_merge_nodes(geometry_list, merging_distance) output_mesh2d = mk.mesh2d_get() assert output_mesh2d.node_x.size == number_of_nodes
def test_mesh2d_set_and_mesh2d_get(): """Test to set a simple mesh and then get it again with new parameters 3---2 | | 0---1 """ mk = MeshKernel() edge_nodes = np.array([0, 1, 1, 2, 2, 3, 3, 0], dtype=np.int32) node_x = np.array([0.0, 1.0, 1.0, 0.0], dtype=np.double) node_y = np.array([0.0, 0.0, 1.0, 1.0], dtype=np.double) input_mesh2d = Mesh2d(node_x, node_y, edge_nodes) mk.mesh2d_set(input_mesh2d) output_mesh2d = mk.mesh2d_get() # Test if the input and output differs assert_array_equal(output_mesh2d.edge_nodes, input_mesh2d.edge_nodes) assert_array_equal(output_mesh2d.node_x, input_mesh2d.node_x) assert_array_equal(output_mesh2d.node_y, input_mesh2d.node_y) # Test if faces are correctly calculated assert_array_equal(output_mesh2d.face_nodes, np.array([0, 1, 2, 3])) assert_array_equal(output_mesh2d.nodes_per_face, np.array([4])) assert_array_equal(output_mesh2d.face_x, np.array([0.5])) assert_array_equal(output_mesh2d.face_y, np.array([0.5])) # Test if edges are correctly calculated assert_array_equal(output_mesh2d.edge_x, np.array([0.5, 1.0, 0.5, 0.0])) assert_array_equal(output_mesh2d.edge_y, np.array([0.0, 0.5, 1.0, 0.5]))
def test_mesh2d_get_smoothness_smooth_mesh2d(): r"""Tests `mesh2d_get_smoothness` with a simple triangular Mesh2d. 3---2 / \ / 0---1 """ mk = MeshKernel() node_x = np.array( [0.0, 4.0, 6.0, 2.0, 2.0], dtype=np.double, ) node_y = np.array( [0.0, 0.0, 3.0, 3.0, 1.0], dtype=np.double, ) edge_nodes = np.array( [0, 1, 1, 2, 2, 3, 3, 0, 1, 3], dtype=np.int32, ) mk.mesh2d_set(Mesh2d(node_x, node_y, edge_nodes)) smoothness = mk.mesh2d_get_smoothness() assert smoothness.values.size == 5 assert smoothness.values[0] == -999.0 assert smoothness.values[1] == -999.0 assert smoothness.values[2] == -999.0 assert smoothness.values[3] == -999.0 assert smoothness.values[4] == approx(0.0, abs=0.01)
def test_mesh2d_delete_small_flow_edges_and_small_triangles_delete_small_triangles( ): r"""Tests `mesh2d_get_small_flow_edge_centers` with a simple mesh with one small triangle. 3---4---5\ | | | 6 0---1---2/ """ mk = MeshKernel() node_x = np.array( [0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 2.1], dtype=np.double, ) node_y = np.array( [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.5], dtype=np.double, ) edge_nodes = np.array( [0, 1, 1, 2, 3, 4, 4, 5, 0, 3, 1, 4, 2, 5, 5, 6, 6, 2], dtype=np.int32, ) mk.mesh2d_set(Mesh2d(node_x, node_y, edge_nodes)) mk.mesh2d_delete_small_flow_edges_and_small_triangles(1.0, 0.01) mesh2d = mk.mesh2d_get() assert mesh2d.node_x.size == 7 assert mesh2d.edge_x.size == 8 assert mesh2d.face_x.size == 2
def test_mesh2d_flip_edges(triangulate: bool): """Tests `mesh2d_flip_edges` with a simple triangular mesh (heptagon).""" mk = MeshKernel() node_x = np.array([0, -8, -10, -4, 4, 10, 8, 0], dtype=np.double) node_y = np.array([10, 6, -2, -9, -9, -2, 6, -5], dtype=np.double) edge_nodes = np.array( [ 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 0, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, ], dtype=np.int32, ) mk.mesh2d_set(Mesh2d(node_x, node_y, edge_nodes)) polygon_x = np.array([-11, 11, 11, -11, -11], dtype=np.double) polygon_y = np.array([-11, -11, 11, 11, -11], dtype=np.double) polygon = GeometryList(polygon_x, polygon_y) land_boundaries_x = np.array([-10, -4, 4, 10], dtype=np.double) land_boundaries_y = np.array([-2, -9, -9, -2], dtype=np.double) land_boundaries = GeometryList(land_boundaries_x, land_boundaries_y) mk.mesh2d_flip_edges(triangulate, False, polygon, land_boundaries) mesh2d = mk.mesh2d_get() assert mesh2d.node_x.size == 8 assert mesh2d.edge_x.size == 14 assert mesh2d.face_x.size == 7
def test_cmesh2d_from_mesh2d(): """Tests `from_mesh2d` of the `CMesh2D` class with a simple mesh.""" # 2---3 # | | # 0---1 node_x = np.array([0.0, 1.0, 1.0, 0.0], dtype=np.double) node_y = np.array([0.0, 0.0, 1.0, 1.0], dtype=np.double) edge_nodes = np.array([0, 1, 1, 3, 3, 2, 2, 0], dtype=np.int32) face_nodes = np.array([0, 1, 2, 3], dtype=np.int32) nodes_per_face = np.array([4], dtype=np.int32) edge_x = np.array([0.5, 1.0, 0.5, 0.0], dtype=np.double) edge_y = np.array([0.0, 0.5, 1.0, 0.5], dtype=np.double) face_x = np.array([0.5], dtype=np.double) face_y = np.array([0.5], dtype=np.double) mesh2d = Mesh2d(node_x, node_y, edge_nodes) mesh2d.face_nodes = face_nodes mesh2d.nodes_per_face = nodes_per_face mesh2d.edge_x = edge_x mesh2d.edge_y = edge_y mesh2d.face_x = face_x mesh2d.face_y = face_y c_mesh2d = CMesh2d.from_mesh2d(mesh2d) # Get the numpy arrays from the ctypes object c_mesh2d_node_x = as_array(c_mesh2d.node_x, (4, )) c_mesh2d_node_y = as_array(c_mesh2d.node_y, (4, )) c_mesh2d_edge_nodes = as_array(c_mesh2d.edge_nodes, (8, )) c_mesh2d_face_nodes = as_array(c_mesh2d.face_nodes, (4, )) c_mesh2d_nodes_per_face = as_array(c_mesh2d.nodes_per_face, (1, )) c_mesh2d_edge_x = as_array(c_mesh2d.edge_x, (4, )) c_mesh2d_edge_y = as_array(c_mesh2d.edge_y, (4, )) c_mesh2d_face_x = as_array(c_mesh2d.face_x, (1, )) c_mesh2d_face_y = as_array(c_mesh2d.face_y, (1, )) # Assert data is correct assert_array_equal(c_mesh2d_node_x, node_x) assert_array_equal(c_mesh2d_node_y, node_y) assert_array_equal(c_mesh2d_edge_nodes, edge_nodes) assert_array_equal(c_mesh2d_face_nodes, face_nodes) assert_array_equal(c_mesh2d_nodes_per_face, nodes_per_face) assert_array_equal(c_mesh2d_edge_x, edge_x) assert_array_equal(c_mesh2d_edge_y, edge_y) assert_array_equal(c_mesh2d_face_x, face_x) assert_array_equal(c_mesh2d_face_y, face_y) assert c_mesh2d.num_nodes == 4 assert c_mesh2d.num_edges == 4 assert c_mesh2d.num_faces == 1 assert c_mesh2d.num_face_nodes == 4
def test_mesh2d_get_orthogonality_not_orthogonal_mesh2d(): """Tests `mesh2d_get_orthogonality` with a non-orthogonal 3x3 Mesh2d. 6---7---8 | | | 3---4*--5 | | | 0---1---2 """ mk = MeshKernel() node_x = np.array( [0.0, 1.0, 2.0, 0.0, 1.8, 2.0, 0.0, 1.0, 2.0], dtype=np.double, ) node_y = np.array( [0.0, 0.0, 0.0, 1.0, 1.8, 1.0, 2.0, 2.0, 2.0], dtype=np.double, ) edge_nodes = np.array( [ 0, 1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 0, 3, 1, 4, 2, 5, 3, 6, 4, 7, 5, 8 ], dtype=np.int32, ) mk.mesh2d_set(Mesh2d(node_x, node_y, edge_nodes)) orthogonality = mk.mesh2d_get_orthogonality() assert orthogonality.values.size == 12 assert orthogonality.values[0] == -999.0 assert orthogonality.values[1] == -999.0 assert orthogonality.values[2] > 0.0 assert orthogonality.values[3] > 0.0 assert orthogonality.values[4] == -999.0 assert orthogonality.values[5] == -999.0 assert orthogonality.values[6] == -999.0 assert orthogonality.values[7] > 0.0 assert orthogonality.values[8] == -999.0 assert orthogonality.values[9] == -999.0 assert orthogonality.values[10] > 0.0 assert orthogonality.values[11] == -999.0
def test_mesh2d_compute_orthogonalization(): """Tests `mesh2d_compute_orthogonalization` with a 3x3 Mesh2d with an uncentered middle node. 6---7---8 | | | 3---4*--5 | | | 0---1---2 """ mk = MeshKernel() node_x = np.array( [0.0, 1.0, 2.0, 0.0, 1.3, 2.0, 0.0, 1.0, 2.0], dtype=np.double, ) node_y = np.array( [0.0, 0.0, 0.0, 1.0, 1.3, 1.0, 2.0, 2.0, 2.0], dtype=np.double, ) edge_nodes = np.array( [ 0, 1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 0, 3, 1, 4, 2, 5, 3, 6, 4, 7, 5, 8 ], dtype=np.int32, ) mk.mesh2d_set(Mesh2d(node_x, node_y, edge_nodes)) polygon_x = np.array([-0.1, 2.1, 2.1, -0.1, -0.1], dtype=np.double) polygon_y = np.array([-0.1, -0.1, 2.1, 2.1, -0.1], dtype=np.double) polygon = GeometryList(polygon_x, polygon_y) land_boundary_x = np.array([0.0, 1.0, 2.0], dtype=np.double) land_boundary_y = np.array([0.0, 0.0, 0.0], dtype=np.double) land_boundary = GeometryList(land_boundary_x, land_boundary_y) mk.mesh2d_compute_orthogonalization( 0, OrthogonalizationParameters(outer_iterations=10), polygon, land_boundary) mesh2d = mk.mesh2d_get() assert 1.0 <= mesh2d.node_x[4] < 1.3 assert 1.0 <= mesh2d.node_y[4] < 1.3
def test_mesdh2d_constructor(): """Tests the default values after constructing a `Mesh2d`.""" node_x = np.array([0.0, 1.0, 1.0, 0.0], dtype=np.double) node_y = np.array([0.0, 0.0, 1.0, 1.0], dtype=np.double) edge_nodes = np.array([0, 1, 1, 2, 2, 3, 3, 0], dtype=np.int32) mesh2d = Mesh2d(node_x, node_y, edge_nodes) assert mesh2d.node_x.size == 4 assert mesh2d.node_y.size == 4 assert mesh2d.edge_nodes.size == 8 assert mesh2d.face_nodes.size == 0 assert mesh2d.nodes_per_face.size == 0 assert mesh2d.edge_x.size == 0 assert mesh2d.edge_y.size == 0 assert mesh2d.face_x.size == 0 assert mesh2d.face_y.size == 0
def test_mesh2d_get_hanging_edges(node_x: np.ndarray, node_y: np.ndarray, edge_nodes: np.ndarray, expected: int): """Tests `mesh2d_get_hanging_edges` by comparing the returned hanging edges with the expected ones 4* | 3---2---5* | | 0---1 """ mk = MeshKernel() mesh2d = Mesh2d(node_x, node_y, edge_nodes) mk.mesh2d_set(mesh2d) result = mk.mesh2d_get_hanging_edges() assert_array_equal(result, expected)
def test_mesh2d_get_obtuse_triangles_mass_centers(): r"""Tests `mesh2d_get_obtuse_triangles_mass_centers` on a 3x3 mesh with two obtuse triangles. 6---7---8 | / \ | 3---4---5 | \ / | 0---1---2 """ mk = MeshKernel() # Mesh with obtuse triangles (4, 5, 7 and 1, 5, 4) node_x = np.array([0.0, 1.0, 2.0, 0.0, 1.5, 2.0, 0.0, 1.0, 2.0], dtype=np.double) node_y = np.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0], dtype=np.double) edge_nodes = np.array( [ 0, 1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 0, 3, 1, 4, 2, 5, 3, 6, 4, 7, 5, 8, 1, 3, 1, 5, 3, 7, 5, 7, ], dtype=np.int32, ) mk.mesh2d_set(Mesh2d(node_x, node_y, edge_nodes)) obtuse_triangles = mk.mesh2d_get_obtuse_triangles_mass_centers() assert obtuse_triangles.x_coordinates.size == 2 assert obtuse_triangles.x_coordinates[0] == 1.5 assert obtuse_triangles.y_coordinates[0] == approx(0.666, 0.01) assert obtuse_triangles.x_coordinates[1] == 1.5 assert obtuse_triangles.y_coordinates[1] == approx(1.333, 0.01)
def test_mesh2d_get_small_flow_edge_centers(): """Tests `mesh2d_get_small_flow_edge_centers` with a simple 3x3 mesh with 4 small flow edges. 6---7---8 | 11|-12| 3-|-4-|-5 | 9-|-10| 0---1---2 """ mk = MeshKernel() node_x = np.array( [0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.5, 1.5, 0.5, 1.5], dtype=np.double, ) node_y = np.array( [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.5, 0.5, 1.5, 1.5], dtype=np.double, ) edge_nodes = np.array( [ 0, 1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 0, 3, 1, 4, 2, 5, 3, 6, 4, 7, 5, 8, 9, 10, 11, 12, 9, 11, 10, 12, ], dtype=np.int32, ) mk.mesh2d_set(Mesh2d(node_x, node_y, edge_nodes)) small_flow_edge_centers = mk.mesh2d_get_small_flow_edge_centers(1.1) assert small_flow_edge_centers.x_coordinates.size == 4 assert small_flow_edge_centers.x_coordinates[0] == 0.5 assert small_flow_edge_centers.y_coordinates[0] == 1.0 assert small_flow_edge_centers.x_coordinates[1] == 1.5 assert small_flow_edge_centers.y_coordinates[1] == 1.0 assert small_flow_edge_centers.x_coordinates[2] == 1.0 assert small_flow_edge_centers.y_coordinates[2] == 0.5 assert small_flow_edge_centers.x_coordinates[3] == 1.0 assert small_flow_edge_centers.y_coordinates[3] == 1.5
def test_mesh2d_count_small_flow_edge_centers(threshold: float, exp_int: int): """Tests `_mesh2d_count_small_flow_edge_centers` with a simple 3x3 mesh with 4 small flow edges. 6---7---8 | 11|-12| 3-|-4-|-5 | 9-|-10| 0---1---2 """ mk = MeshKernel() node_x = np.array( [0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.5, 1.5, 0.5, 1.5], dtype=np.double, ) node_y = np.array( [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.5, 0.5, 1.5, 1.5], dtype=np.double, ) edge_nodes = np.array( [ 0, 1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 0, 3, 1, 4, 2, 5, 3, 6, 4, 7, 5, 8, 9, 10, 11, 12, 9, 11, 10, 12, ], dtype=np.int32, ) mk.mesh2d_set(Mesh2d(node_x, node_y, edge_nodes)) n_small_flow_edges = mk._mesh2d_count_small_flow_edge_centers(threshold) assert n_small_flow_edges == exp_int