Esempio n. 1
0
def test_cgeometrylist_from_geometrylist():
    """Tests `from_geometrylist` of the `CGeometryList` class."""

    x_coordinates = np.array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=np.double)
    y_coordinates = np.array([5.0, 6.0, 7.0, 8.0, 9.0], dtype=np.double)
    values = np.array([10.0, 11.0, 12.0, 13.0, 14.0], dtype=np.double)
    geometry_separator = 15.0
    inner_outer_separator = 16.0

    geometry_list = GeometryList(x_coordinates, y_coordinates)
    geometry_list.values = values
    geometry_list.geometry_separator = geometry_separator
    geometry_list.inner_outer_separator = inner_outer_separator

    c_geometry_list = CGeometryList.from_geometrylist(geometry_list)

    # Get the numpy arrays from the ctypes object
    c_geometry_list_x_coordinates = as_array(c_geometry_list.x_coordinates,
                                             (5, ))
    c_geometry_list_y_coordinates = as_array(c_geometry_list.y_coordinates,
                                             (5, ))
    c_geometry_list_values = as_array(c_geometry_list.values, (5, ))

    assert_array_equal(c_geometry_list_x_coordinates, x_coordinates)
    assert_array_equal(c_geometry_list_y_coordinates, y_coordinates)
    assert_array_equal(c_geometry_list_values, values)

    assert c_geometry_list.geometry_separator == geometry_separator
    assert c_geometry_list.inner_outer_separator == inner_outer_separator
    assert c_geometry_list.n_coordinates == x_coordinates.size
def test_mesh2d_flip_edges2_triangulate(meshkernel_with_mesh2d: MeshKernel):
    """Tests `mesh2d_flip_edges` with a simple 2x2 mesh.

    6---7---8       6---7---8
    |   |   |       | / | / |
    3---4---5  -->  3---4---5
    |   |   |       | / | / |
    0---1---2       0---1---2
    """

    mk = meshkernel_with_mesh2d(2, 2)

    mk.mesh2d_flip_edges(
        True,
        True,
        GeometryList(np.empty(0, dtype=np.double), np.empty(0,
                                                            dtype=np.double)),
        GeometryList(np.empty(0, dtype=np.double), np.empty(0,
                                                            dtype=np.double)),
    )

    mesh2d = mk.mesh2d_get()

    assert mesh2d.node_x.size == 9
    assert mesh2d.edge_x.size == 16
    assert mesh2d.face_x.size == 8

    assert np.all(mesh2d.nodes_per_face == 3)
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_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_refine_based_on_polygon(
    meshkernel_with_mesh2d: MeshKernel,
    max_iterations: int,
    exp_nodes: int,
    exp_edges: int,
    exp_faces: int,
):
    """Tests `mesh2d_refine_based_on_polygon` with a simple 2x2 mesh.

    6---7---8
    |   |   |
    3---4---5
    |   |   |
    0---1---2
    """

    mk = meshkernel_with_mesh2d(2, 2)

    x_coordinates = np.array([0.0, 0.0, 2.0, 2.0, 0.0], dtype=np.double)
    y_coordinates = np.array([0.0, 2.0, 2.0, 0.0, 0.0], dtype=np.double)
    polygon = GeometryList(x_coordinates, y_coordinates)

    refinement_params = MeshRefinementParameters(True, False, 0.5, 1, False,
                                                 False, max_iterations)

    mk.mesh2d_refine_based_on_polygon(polygon, refinement_params)

    mesdh2d = mk.mesh2d_get()

    assert mesdh2d.node_x.size == exp_nodes
    assert mesdh2d.edge_x.size == exp_edges
    assert mesdh2d.face_x.size == exp_faces
def test_mesh2d_refine_based_on_samples(
    meshkernel_with_mesh2d: MeshKernel,
    min_face_size: float,
    sample_value: float,
    exp_nodes: int,
    exp_edges: int,
    exp_faces: int,
):
    """Tests `mesh2d_refine_based_on_samples` with a simple 2x2 mesh.

    6---7---8
    |   |   |
    3---4---5
    |   |   |
    0---1---2
    """
    mk = meshkernel_with_mesh2d(2, 2)

    x_coordinates = np.array([0.5, 0.5, 1.5, 1.5], dtype=np.double)
    y_coordinates = np.array([0.5, 1.5, 1.5, 0.5], dtype=np.double)
    values = np.array([sample_value, sample_value, sample_value, sample_value],
                      dtype=np.double)
    samples = GeometryList(x_coordinates, y_coordinates, values)

    refinement_params = MeshRefinementParameters(
        False, False, min_face_size, RefinementType.REFINEMENT_LEVELS, False,
        False, 1)

    mk.mesh2d_refine_based_on_samples(samples, 1.0, 1, refinement_params)

    mesdh2d = mk.mesh2d_get()

    assert mesdh2d.node_x.size == exp_nodes
    assert mesdh2d.edge_x.size == exp_edges
    assert mesdh2d.face_x.size == exp_faces
def test_mesh2d_delete_empty_polygon(
    meshkernel_with_mesh2d: MeshKernel,
    invert_deletion: bool,
    exp_nodes: int,
    exp_edges: int,
    exp_faces: int,
):
    """Test `mesh2d_delete` by deleting a an empty polygon from a 4x4 mesh2d.

    20--21--22--23--24
    |   |   |   |   |
    15--16--17--18--19
    |   |   |   |   |
    10--11--12--13--14
    |   |   |   |   |
    5---6---7---8---9
    |   |   |   |   |
    0---1---2---3---4

    """
    mk = meshkernel_with_mesh2d(4, 4)

    x_coordinates = np.empty(0, dtype=np.double)
    y_coordinates = np.empty(0, dtype=np.double)

    geometry_list = GeometryList(x_coordinates, y_coordinates)
    delete_option = DeleteMeshOption.ALL_NODES

    mk.mesh2d_delete(geometry_list, delete_option, invert_deletion)
    mesh2d = mk.mesh2d_get()

    assert mesh2d.node_x.size == exp_nodes
    assert mesh2d.edge_x.size == exp_edges
    assert mesh2d.face_x.size == exp_faces
def test_polygon_get_included_points(selecting_x: np.array,
                                     selecting_y: np.array,
                                     exp_values: np.array):
    """Tests `polygon_get_included_points` with a simple polygon and various selecting polygons."""

    selecting_polygon = GeometryList(selecting_x, selecting_y)

    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)
    selected_polygon = GeometryList(x_coordinates, y_coordinates)

    mk = MeshKernel()

    selection = mk.polygon_get_included_points(selecting_polygon,
                                               selected_polygon)

    assert_array_equal(selection.values, exp_values)
def test_mesh2d_triangulation_interpolation_on_edges(
    meshkernel_with_mesh2d: MeshKernel, ):
    """Tests `mesh2d_triangulation_interpolation` on the edges of a 2x2 Mesh2d."""

    mk = meshkernel_with_mesh2d(2, 2)

    samples_x = np.array(
        [0.0, 1.1, 2.2, 0.0, 0.9, 2.0, 0.4, 1.6, 0.6, 1.3, 0.2, 1.5],
        dtype=np.double)
    samples_y = np.array(
        [0.5, 0.5, 0.5, 1.5, 1.5, 1.5, 0.0, 0.0, 1.0, 1.0, 2.0, 2.0],
        dtype=np.double)
    samples_values = np.array(
        [1, 2.1, 3.2, 1.0, 1.9, 3.0, 1.4, 2.6, 1.6, 2.3, 1.2, 2.5],
        dtype=np.double)
    samples = GeometryList(samples_x, samples_y, samples_values)

    interpolation = mk.mesh2d_triangulation_interpolation(
        samples, Mesh2dLocation.EDGES)

    assert interpolation.x_coordinates[0] == 0.0
    assert interpolation.x_coordinates[1] == 1.0
    assert interpolation.x_coordinates[2] == 2.0
    assert interpolation.x_coordinates[3] == 0.0
    assert interpolation.x_coordinates[4] == 1.0
    assert interpolation.x_coordinates[5] == 2.0
    assert interpolation.x_coordinates[6] == 0.5
    assert interpolation.x_coordinates[7] == 1.5
    assert interpolation.x_coordinates[8] == 0.5
    assert interpolation.x_coordinates[9] == 1.5
    assert interpolation.x_coordinates[10] == 0.5
    assert interpolation.x_coordinates[11] == 1.5

    assert interpolation.y_coordinates[0] == 0.5
    assert interpolation.y_coordinates[1] == 0.5
    assert interpolation.y_coordinates[2] == 0.5
    assert interpolation.y_coordinates[3] == 1.5
    assert interpolation.y_coordinates[4] == 1.5
    assert interpolation.y_coordinates[5] == 1.5
    assert interpolation.y_coordinates[6] == 0.0
    assert interpolation.y_coordinates[7] == 0.0
    assert interpolation.y_coordinates[8] == 1.0
    assert interpolation.y_coordinates[9] == 1.0
    assert interpolation.y_coordinates[10] == 2.0
    assert interpolation.y_coordinates[11] == 2.0

    assert interpolation.values[0] == approx(1, abs=0.00000001)
    assert interpolation.values[1] == approx(2, abs=0.00000001)
    assert interpolation.values[2] == approx(3, abs=0.00000001)
    assert interpolation.values[3] == approx(1, abs=0.00000001)
    assert interpolation.values[4] == approx(2, abs=0.00000001)
    assert interpolation.values[5] == approx(3, abs=0.00000001)
    assert interpolation.values[6] == approx(1.5, abs=0.00000001)
    assert interpolation.values[7] == approx(2.5, abs=0.00000001)
    assert interpolation.values[8] == approx(1.5, abs=0.00000001)
    assert interpolation.values[9] == approx(2.5, abs=0.00000001)
    assert interpolation.values[10] == approx(1.5, abs=0.00000001)
    assert interpolation.values[11] == approx(2.5, abs=0.00000001)
Esempio n. 10
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
Esempio n. 11
0
def test_contacts_compute_with_polygons():
    """Tests `contacts_compute_with_polygons` with a 5x5 Mesh2d and a Mesh1d with 5 nodes.

    30--31--32--33--34--35
    |   |   |   |   | / |
    24--25--26--27--28--29
    |   |   |   | / |   |
    18--19--20--21--22--23
    |   |   | / |   |   |
    12--13--14--15--16--17
    |   | / |   |   |   |
    6---7---8---9---10--11
    | / |   |   |   |   |
    0---1---2---3---4---5
    """

    mk = MeshKernel()

    mesh2d = Mesh2dFactory.create_rectilinear_mesh(5, 5)

    node_x = np.array([0.5, 1.5, 2.5, 3.5, 4.5], dtype=np.double)
    node_y = np.array([0.5, 1.5, 2.5, 3.5, 4.5], dtype=np.double)
    edge_nodes = np.array([0, 1, 1, 2, 2, 3, 3, 4], dtype=np.int32)
    mesh1d = Mesh1d(node_x, node_y, edge_nodes)

    mk.mesh2d_set(mesh2d)
    mk.mesh1d_set(mesh1d)

    node_mask = np.full(node_x.size, True)

    # Two polygons around Mesh2d nodes 4, 5, 23, 22 and 12, 13, 31, 30
    separator = -999.0
    polygon_x = np.array(
        [-0.1, 1.1, 1.1, -0.1, -0.1, separator, 3.9, 5.1, 5.1, 3.9, 3.9],
        dtype=np.double,
    )
    polygon_y = np.array(
        [1.9, 1.9, 5.1, 5.1, 1.9, separator, -0.1, -0.1, 3.1, 3.1, -0.1],
        dtype=np.double,
    )
    polygon = GeometryList(polygon_x, polygon_y)

    mk.contacts_compute_with_polygons(node_mask, polygon)

    contacts = mk.contacts_get()

    contacts = sort_contacts_by_mesh2d_indices(contacts)

    assert contacts.mesh1d_indices.size == 2
    assert contacts.mesh2d_indices.size == 2

    assert contacts.mesh1d_indices[0] == 1
    assert contacts.mesh1d_indices[1] == 3

    assert contacts.mesh2d_indices[0] == 10
    assert contacts.mesh2d_indices[1] == 14
Esempio n. 12
0
def test_geometrylist_constructor():
    """Tests the default values after constructing a `GeometryList`."""

    x_coordinates = np.array([0.0], dtype=np.double)
    y_coordinates = np.array([1.0], dtype=np.double)
    geometry_list = GeometryList(x_coordinates, y_coordinates)

    assert geometry_list.x_coordinates.size == 1
    assert geometry_list.y_coordinates.size == 1
    assert geometry_list.values.size == 0
    assert geometry_list.geometry_separator == -999.0
    assert geometry_list.inner_outer_separator == -998.0
Esempio n. 13
0
def test_contacts_compute_with_points():
    """Tests `contacts_compute_with_points` with a 5x5 Mesh2d and a Mesh1d with 5 nodes.

    30--31--32--33--34--35
    |   |   |   |   | / |
    24--25--26--27--28--29
    |   |   |   | / |   |
    18--19--20--21--22--23
    |   |   | / |   |   |
    12--13--14--15--16--17
    |   | / |   |   |   |
    6---7---8---9---10--11
    | / |   |   |   |   |
    0---1---2---3---4---5
    """

    mk = MeshKernel()

    mesh2d = Mesh2dFactory.create_rectilinear_mesh(5, 5)

    node_x = np.array([0.5, 1.5, 2.5, 3.5, 4.5], dtype=np.double)
    node_y = np.array([0.5, 1.5, 2.5, 3.5, 4.5], dtype=np.double)
    edge_nodes = np.array([0, 1, 1, 2, 2, 3, 3, 4], dtype=np.int32)
    mesh1d = Mesh1d(node_x, node_y, edge_nodes)

    mk.mesh2d_set(mesh2d)
    mk.mesh1d_set(mesh1d)

    node_mask = np.full(node_x.size, True)

    # Three points in Mesh2d faces 10, 8, 14
    points_x = np.array([0.5, 3.5, 4.5], dtype=np.double)
    points_y = np.array([2.5, 1.5, 2.5], dtype=np.double)
    points = GeometryList(points_x, points_y)

    mk.contacts_compute_with_points(node_mask, points)

    contacts = mk.contacts_get()

    contacts = sort_contacts_by_mesh2d_indices(contacts)

    assert contacts.mesh1d_indices.size == 3
    assert contacts.mesh2d_indices.size == 3

    assert contacts.mesh1d_indices[0] == 2
    assert contacts.mesh1d_indices[1] == 1
    assert contacts.mesh1d_indices[2] == 3

    assert contacts.mesh2d_indices[0] == 8
    assert contacts.mesh2d_indices[1] == 10
    assert contacts.mesh2d_indices[2] == 14
Esempio n. 14
0
def test_contacts_compute_single():
    """Tests `contacts_compute_single` with a 5x5 Mesh2d and a Mesh1d with 5 nodes.

    30--31--32--33--34--35
    |   |   |   |   |  /|
    24--25--26--27--28--29
    |   |   |   |  /|   |
    18--19--20--21--22--23
    |   |   |  /|   |   |
    12--13--14--15--16--17
    |   |  /|   |   |   |
    6---7---8---9---10--11
    |  /|   |   |   |   |
    0---1---2---3---4---5
    """

    mk = MeshKernel()

    mesh2d = Mesh2dFactory.create_rectilinear_mesh(5, 5)

    node_x = np.array([0.75, 1.75, 2.75, 3.75, 4.75], dtype=np.double)
    node_y = np.array([0.25, 1.25, 2.25, 3.25, 4.25], dtype=np.double)
    edge_nodes = np.array([0, 1, 1, 2, 2, 3, 3, 4], dtype=np.int32)
    mesh1d = Mesh1d(node_x, node_y, edge_nodes)

    mk.mesh2d_set(mesh2d)
    mk.mesh1d_set(mesh1d)

    node_mask = np.full(node_x.size, True)

    polygon_x = np.array([-1.0, 6.0, 6.0, -1.0, -1.0], dtype=np.double)
    polygon_y = np.array([-1.0, -1.0, 6.0, 6.0, -1.0], dtype=np.double)
    polygon = GeometryList(polygon_x, polygon_y)

    mk.contacts_compute_single(node_mask, polygon)

    contacts = mk.contacts_get()
    contacts = sort_contacts_by_mesh2d_indices(contacts)

    assert contacts.mesh1d_indices.size == 3
    assert contacts.mesh2d_indices.size == 3

    assert contacts.mesh1d_indices[0] == 1
    assert contacts.mesh1d_indices[1] == 2
    assert contacts.mesh1d_indices[2] == 3

    assert contacts.mesh2d_indices[0] == 6
    assert contacts.mesh2d_indices[1] == 12
    assert contacts.mesh2d_indices[2] == 18
Esempio n. 15
0
def test_polygon_refine(start: int, end: int, length: float, exp_nodes: int):
    """Tests `polygon_refine` by refining a simple polygon."""

    mk = MeshKernel()

    # 3---2
    # |   |
    # 0---1
    x_coordinates = np.array([0.0, 60.0, 60.0, 0.0, 0.0], dtype=np.double)
    y_coordinates = np.array([0.0, 0.0, 60.0, 60.0, 0.0], dtype=np.double)
    polygon = GeometryList(x_coordinates, y_coordinates)

    geom = mk.polygon_refine(polygon, start, end, length)

    assert geom.x_coordinates.size == exp_nodes
Esempio n. 16
0
def test_mesh2d_triangulation_interpolation_on_faces(
    meshkernel_with_mesh2d: MeshKernel, ):
    """Tests `mesh2d_triangulation_interpolation` on the faces of a 3x3 Mesh2d."""

    mk = meshkernel_with_mesh2d(3, 3)

    samples_x = np.array([0.4, 1.3, 2.6, 0.6, 1.6, 2.4, 0.4, 1.6, 2.5],
                         dtype=np.double)
    samples_y = np.array([0.5, 0.5, 0.5, 1.5, 1.5, 1.5, 2.5, 2.5, 2.5],
                         dtype=np.double)
    samples_values = np.array([0.9, 1.8, 3.1, 4.1, 5.1, 5.9, 6.9, 8.1, 9],
                              dtype=np.double)
    samples = GeometryList(samples_x, samples_y, samples_values)

    interpolation = mk.mesh2d_triangulation_interpolation(
        samples, Mesh2dLocation.FACES)

    assert interpolation.x_coordinates[0] == 0.5
    assert interpolation.x_coordinates[1] == 1.5
    assert interpolation.x_coordinates[2] == 2.5
    assert interpolation.x_coordinates[3] == 0.5
    assert interpolation.x_coordinates[4] == 1.5
    assert interpolation.x_coordinates[5] == 2.5
    assert interpolation.x_coordinates[6] == 0.5
    assert interpolation.x_coordinates[7] == 1.5
    assert interpolation.x_coordinates[8] == 2.5

    assert interpolation.y_coordinates[0] == 0.5
    assert interpolation.y_coordinates[1] == 0.5
    assert interpolation.y_coordinates[2] == 0.5
    assert interpolation.y_coordinates[3] == 1.5
    assert interpolation.y_coordinates[4] == 1.5
    assert interpolation.y_coordinates[5] == 1.5
    assert interpolation.y_coordinates[6] == 2.5
    assert interpolation.y_coordinates[7] == 2.5
    assert interpolation.y_coordinates[8] == 2.5

    assert interpolation.values[0] == approx(1, abs=0.00000001)
    assert interpolation.values[1] == approx(2, abs=0.00000001)
    assert interpolation.values[2] == approx(3, abs=0.00000001)
    assert interpolation.values[3] == approx(4, abs=0.00000001)
    assert interpolation.values[4] == approx(5, abs=0.00000001)
    assert interpolation.values[5] == approx(6, abs=0.00000001)
    assert interpolation.values[6] == approx(7, abs=0.00000001)
    assert interpolation.values[7] == approx(8, abs=0.00000001)
    assert interpolation.values[8] == approx(9, abs=0.00000001)
Esempio n. 17
0
def test_mesh2d_triangulation_interpolation_on_nodes(
    meshkernel_with_mesh2d: MeshKernel, ):
    """Tests `mesh2d_triangulation_interpolation` on the nodes of a 2x2 Mesh2d."""

    mk = meshkernel_with_mesh2d(2, 2)

    samples_x = np.array([0.0, 0.9, 2.1, 0.1, 1.1, 2.2, 0.0, 1.2, 2.1],
                         dtype=np.double)
    samples_y = np.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0],
                         dtype=np.double)
    samples_values = np.array([1, 1.9, 3.1, 4.1, 5.1, 6.2, 7.0, 8.2, 9.1],
                              dtype=np.double)
    samples = GeometryList(samples_x, samples_y, samples_values)

    interpolation = mk.mesh2d_triangulation_interpolation(
        samples, Mesh2dLocation.NODES)

    assert interpolation.x_coordinates[0] == 0.0
    assert interpolation.x_coordinates[1] == 1.0
    assert interpolation.x_coordinates[2] == 2.0
    assert interpolation.x_coordinates[3] == 0.0
    assert interpolation.x_coordinates[4] == 1.0
    assert interpolation.x_coordinates[5] == 2.0
    assert interpolation.x_coordinates[6] == 0.0
    assert interpolation.x_coordinates[7] == 1.0
    assert interpolation.x_coordinates[8] == 2.0

    assert interpolation.y_coordinates[0] == 0.0
    assert interpolation.y_coordinates[1] == 0.0
    assert interpolation.y_coordinates[2] == 0.0
    assert interpolation.y_coordinates[3] == 1.0
    assert interpolation.y_coordinates[4] == 1.0
    assert interpolation.y_coordinates[5] == 1.0
    assert interpolation.y_coordinates[6] == 2.0
    assert interpolation.y_coordinates[7] == 2.0
    assert interpolation.y_coordinates[8] == 2.0

    assert interpolation.values[0] == approx(1, abs=0.00000001)
    assert interpolation.values[1] == approx(2, abs=0.00000001)
    assert interpolation.values[2] == approx(3, abs=0.00000001)
    assert interpolation.values[3] == approx(4, abs=0.00000001)
    assert interpolation.values[4] == approx(5, abs=0.00000001)
    assert interpolation.values[5] == approx(6, abs=0.00000001)
    assert interpolation.values[6] == approx(7, abs=0.00000001)
    assert interpolation.values[7] == approx(8, abs=0.00000001)
    assert interpolation.values[8] == approx(9, abs=0.00000001)
Esempio n. 18
0
def test_create_clip_2d():

    polygon = GeometryList(
        x_coordinates=np.array([0.0, 6.0, 4.0, 2.0, 0.0]),
        y_coordinates=np.array([0.0, 2.0, 7.0, 6.0, 0.0]),
    )

    # Define polygon
    bbox = (1.0, -2.0, 3.0, 4.0)
    mesh2d = Mesh2d(meshkernel=MeshKernel())
    mesh2d.create_rectilinear(extent=bbox, dx=0.5, dy=0.75)
    mesh2d.clip(polygon)

    mesh2d_output = mesh2d.get_mesh2d()

    assert mesh2d_output.node_x.size == 28
    assert mesh2d_output.edge_nodes.size == 90
Esempio n. 19
0
def test_get_splines(
    number_of_points_between_nodes: int,
    x_coordinates: np.ndarray,
    y_coordinates: np.ndarray,
):
    """Test `get_splines` by checking if the dimensions of the generated spline are correct"""
    mk = MeshKernel()
    geometry_list_in = GeometryList(x_coordinates, y_coordinates)

    geometry_list_out = mk.get_splines(geometry_list_in,
                                       number_of_points_between_nodes)

    original_number_of_coordinates = geometry_list_in.x_coordinates.size
    expected_new_number_of_coordinates = (
        original_number_of_coordinates * number_of_points_between_nodes -
        number_of_points_between_nodes + original_number_of_coordinates + 1)

    assert expected_new_number_of_coordinates == geometry_list_out.x_coordinates.size
Esempio n. 20
0
def test_create_refine_2d():

    polygon = GeometryList(
        x_coordinates=np.array([0.0, 6.0, 4.0, 2.0, 0.0]),
        y_coordinates=np.array([0.0, 2.0, 7.0, 6.0, 0.0]),
    )

    # Define polygon
    bbox = (1.0, -2.0, 3.0, 4.0)
    # Create instance
    mesh2d = Mesh2d(meshkernel=MeshKernel())
    # Create within bounding box
    mesh2d.create_rectilinear(extent=bbox, dx=0.5, dy=0.75)
    # Refine
    mesh2d.refine(polygon, 1)

    mesh2d_output = mesh2d.get_mesh2d()

    assert mesh2d_output.node_x.size == 114
    assert mesh2d_output.edge_nodes.size == 426
Esempio n. 21
0
def test_mesh2d_averaging_interpolation(
    meshkernel_with_mesh2d: MeshKernel,
    averaging_method: AveragingMethod,
    exp_values: np.ndarray,
):
    """Tests `mesh2d_averaging_interpolation` on the faces of a 3x3 Mesh2d."""

    mk = meshkernel_with_mesh2d(3, 3)

    samples_x = np.array([0.5, 1.5, 2.5, 0.5, 1.5, 2.5, 0.5, 1.5, 2.5],
                         dtype=np.double)
    samples_y = np.array([0.5, 0.5, 0.5, 1.5, 1.5, 1.5, 2.5, 2.5, 2.5],
                         dtype=np.double)
    samples_values = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.double)
    samples = GeometryList(samples_x, samples_y, samples_values)

    interpolation = mk.mesh2d_averaging_interpolation(samples,
                                                      Mesh2dLocation.FACES,
                                                      averaging_method, 1.5, 1)

    assert interpolation.x_coordinates[0] == 0.5
    assert interpolation.x_coordinates[1] == 1.5
    assert interpolation.x_coordinates[2] == 2.5
    assert interpolation.x_coordinates[3] == 0.5
    assert interpolation.x_coordinates[4] == 1.5
    assert interpolation.x_coordinates[5] == 2.5
    assert interpolation.x_coordinates[6] == 0.5
    assert interpolation.x_coordinates[7] == 1.5
    assert interpolation.x_coordinates[8] == 2.5

    assert interpolation.y_coordinates[0] == 0.5
    assert interpolation.y_coordinates[1] == 0.5
    assert interpolation.y_coordinates[2] == 0.5
    assert interpolation.y_coordinates[3] == 1.5
    assert interpolation.y_coordinates[4] == 1.5
    assert interpolation.y_coordinates[5] == 1.5
    assert interpolation.y_coordinates[6] == 2.5
    assert interpolation.y_coordinates[7] == 2.5
    assert interpolation.y_coordinates[8] == 2.5

    assert_array_equal(interpolation.values, exp_values)
Esempio n. 22
0
def test_nodes_in_polygons_mesh2d(
    meshkernel_with_mesh2d: MeshKernel,
    x_coordinates: ndarray,
    y_coordinates: ndarray,
    inside: bool,
    exp_num_nodes: int,
):
    """Tests `nodes_in_polygons_mesh2d` by checking if it returns the correct number of nodes

    6---7---8
    |   |   |
    3---4---5
    |   |   |
    0---1---2
    """

    mk = meshkernel_with_mesh2d(2, 2)
    geometry_list = GeometryList(x_coordinates, y_coordinates)
    selected_nodes = mk.mesh2d_get_nodes_in_polygons(geometry_list, inside)

    assert selected_nodes.size == exp_num_nodes
Esempio n. 23
0
def test_mesh2d_make_mesh_from_samples():
    """Tests `mesh2d_make_mesh_from_samples` by creating a mesh2d from six sample points."""

    mk = MeshKernel()

    #  5  4
    # 0    3
    #  1  2
    x_coordinates = np.array([0.0, 0.5, 1.5, 2.0, 1.5, 0.5, 0.0],
                             dtype=np.double)
    y_coordinates = np.array([1.0, 0.0, 0.0, 1.0, 2.0, 2.0, 1.0],
                             dtype=np.double)
    polygon = GeometryList(x_coordinates, y_coordinates)

    mk.mesh2d_make_mesh_from_samples(polygon)

    mesh2d = mk.mesh2d_get()

    assert mesh2d.node_x.size == 6
    assert mesh2d.edge_x.size == 9
    assert mesh2d.face_x.size == 4
Esempio n. 24
0
def test_contacts_compute_boundary(node_mask: ndarray,
                                   exp_mesh1d_indices: ndarray,
                                   exp_mesh2d_indices: ndarray):
    """Tests `contacts_compute_boundary` with a 2x2 Mesh2d and a Mesh1d with 5 nodes.


       ---3---4
     2
    |   6---7---8
    1   |   |   |
    |   3---4---5
    0   |   |   |
        0---1---2
    """

    mk = MeshKernel()

    mesh2d = Mesh2dFactory.create_rectilinear_mesh(2, 2)

    node_x = np.array([-1.0, -1.0, -0.5, 0.5, 1.5], dtype=np.double)
    node_y = np.array([0.5, 1.5, 2.5, 3.0, 3.0], dtype=np.double)
    edge_nodes = np.array([0, 1, 1, 2, 2, 3, 3, 4], dtype=np.int32)
    mesh1d = Mesh1d(node_x, node_y, edge_nodes)

    mk.mesh2d_set(mesh2d)
    mk.mesh1d_set(mesh1d)

    polygon_x = np.array([-1.1, 3.1, 3.1, -1.1, -1.1], dtype=np.double)
    polygon_y = np.array([-0.1, -0.1, 3.1, 3.1, -0.1], dtype=np.double)
    polygon = GeometryList(polygon_x, polygon_y)

    mk.contacts_compute_boundary(node_mask, polygon, 2.0)

    contacts = mk.contacts_get()

    contacts = sort_contacts_by_mesh2d_indices(contacts)

    assert_array_equal(contacts.mesh1d_indices, exp_mesh1d_indices)
    assert_array_equal(contacts.mesh2d_indices, exp_mesh2d_indices)
Esempio n. 25
0
def test_mesh2d_make_mesh_from_polygon():
    """Tests `mesh2d_make_mesh_from_polygon` by creating a mesh2d from a simple hexagon."""

    mk = MeshKernel()

    #   5__4
    #  /    \
    # 0      3
    #  \1__2/
    x_coordinates = np.array([0.0, 0.5, 1.5, 2.0, 1.5, 0.5, 0.0],
                             dtype=np.double)
    y_coordinates = np.array([1.0, 0.0, 0.0, 1.0, 2.0, 2.0, 1.0],
                             dtype=np.double)
    polygon = GeometryList(x_coordinates, y_coordinates)

    mk.mesh2d_make_mesh_from_polygon(polygon)

    mesh2d = mk.mesh2d_get()

    assert mesh2d.node_x.size == 7
    assert mesh2d.edge_x.size == 12
    assert mesh2d.face_x.size == 6
Esempio n. 26
0
def test_mesh2d_delete_small_polygon(
    meshkernel_with_mesh2d: MeshKernel,
    invert_deletion: bool,
    delete_option: DeleteMeshOption,
    exp_nodes: int,
    exp_edges: int,
    exp_faces: int,
):
    """Test `mesh2d_delete` by deleting a polygon from a 5x5 mesh2d.

    30--31--32--33--34--35
    |   |   |   |   |   |
    24--25--26--27--28--29
    |   | * |   | * |   |
    18--19--20--21--22--23
    |   |   |   |   |   |
    12--13--14--15--16--17
    |   | * |   | * |   |
    6---7---8---9---10--11
    |   |   |   |   |   |
    0---1---2---3---4---5

    """
    mk = meshkernel_with_mesh2d(5, 5)

    # Polygon around nodes 14, 15, 21 & 20 (through the face circum centers)
    x_coordinates = np.array([1.5, 3.5, 3.5, 1.5, 1.5], dtype=np.double)
    y_coordinates = np.array([1.5, 1.5, 3.5, 3.5, 1.5], dtype=np.double)

    geometry_list = GeometryList(x_coordinates, y_coordinates)

    mk.mesh2d_delete(geometry_list, delete_option, invert_deletion)
    mesh2d = mk.mesh2d_get()

    assert mesh2d.node_x.size == exp_nodes
    assert mesh2d.edge_x.size == exp_edges
    assert mesh2d.face_x.size == exp_faces
Esempio n. 27
0
def get_circle_gl(r, detail=100):

    t = np.r_[np.linspace(0, 2 * np.pi, detail), 0]
    polygon = GeometryList(np.cos(t) * r, np.sin(t) * r)
    return polygon