xugrid.Ugrid1d.refine_by_vertices#

Ugrid1d.refine_by_vertices(vertices: ndarray, return_index: bool = False, tolerance: float | None = None) Ugrid1d[source]#

Refine Ugrid1d with extra vertices to be inserted and returns new grid. Vertices need to be located on existing grid edges, if not, a ValueError will be returned.

Parameters:
  • vertices (np.ndarray of floats) – Coordinates of vertices to be inserted in the grid. Must have shape (N, 2).

  • return_index (bool, optional) – If set to to True, the index of the new vertices in the grid will be returned. Defaults to False.

  • tolerance (float, optional) – The tolerance used to determine whether a point is on an edge. This accounts for the inherent inexactness of floating point calculations. If None, an appropriate tolerance is automatically estimated based on the geometry size. Consider adjusting this value if edge detection results are unsatisfactory.

Returns:

grid – Refined grid with new vertices.

Return type:

Ugrid1d

Examples

Let’s first create a simple grid with 3 nodes and 2 edges:

>>> import numpy as np
>>> import xugrid as xu
>>> node_xy = np.array([[0.0, 0.0], [5.0, 5.0], [10.0, 5.0]])
>>> edge_nodes = np.array([[0, 1],[1, 2]])
>>> grid = xu.Ugrid1d(*node_xy.T, -1, edge_nodes)

Now refine the grid by adding new vertices:

>>> vertices = np.array([[2.0, 2.0], [7.0, 5.0]])
>>> new = grid.refine_by_vertices(vertices)
>>> print(new.node_coordinates)

To return the indices of the inserted vertices:

>>> new, new_vertices_index = grid.refine_by_vertices(vertices, return_index=True)
>>> print(new_vertices_index)
>>> print(new.node_coordinates[new_vertices_index])