Skip to content

Commit

Permalink
added edge_node_connectivity to Ugrid2d.from_meshkernel (#250)
Browse files Browse the repository at this point in the history
* added edge_node_connectivity to Ugrid2d.from_meshkernel

* fixed formatting with black

* updated changelog
  • Loading branch information
veenstrajelmer authored Jul 3, 2024
1 parent e1beb17 commit 9283eb6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ All notable changes to this project will be documented in this file.
The format is based on `Keep a Changelog`_, and this project adheres to
`Semantic Versioning`_.

[Unreleased]
------------

Added
~~~~~

- included ``edge_node_connectivity`` in :meth:`xugrid.Ugrid2d.from_meshkernel`,
so the ordering of edges is consistent with ``meshkernel``.

[0.10.0] 2024-05-01
-------------------

Expand Down
41 changes: 41 additions & 0 deletions tests/test_ugrid2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ class Mesh2d(NamedTuple):
node_y: np.ndarray
face_nodes: np.ndarray
nodes_per_face: np.ndarray
edge_nodes: np.ndarray

mesh2d = Mesh2d(
node_x=np.array([0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]),
Expand All @@ -319,13 +320,53 @@ class Mesh2d(NamedTuple):
[0, 1, 5, 4, 1, 2, 6, 5, 2, 3, 7, 6, 4, 5, 9, 8, 5, 6, 10, 9, 6, 7, 11, 10]
),
nodes_per_face=np.array([4, 4, 4, 4, 4, 4]),
edge_nodes=np.array(
[
4,
8,
5,
6,
5,
9,
6,
7,
6,
10,
7,
11,
8,
9,
9,
10,
10,
11,
0,
1,
0,
4,
1,
2,
1,
5,
2,
3,
2,
6,
3,
7,
4,
5,
]
),
)

grid = xugrid.Ugrid2d.from_meshkernel(mesh2d)
grid.plot()
assert grid.n_face == 6
assert np.allclose(mesh2d.node_x, grid.node_x)
assert np.allclose(mesh2d.node_y, grid.node_y)
assert np.allclose(grid.face_node_connectivity, mesh2d.face_nodes.reshape((6, 4)))
assert np.allclose(grid.edge_node_connectivity, mesh2d.edge_nodes.reshape((-1, 2)))


def test_assign_node_coords():
Expand Down
6 changes: 4 additions & 2 deletions xugrid/ugrid/ugrid2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,13 @@ def from_meshkernel(
face_node_connectivity = np.full((n_face, n_max_node), fill_value)
isnode = connectivity.ragged_index(n_face, n_max_node, mesh.nodes_per_face)
face_node_connectivity[isnode] = mesh.face_nodes
edge_node_connectivity = np.reshape(mesh.edge_nodes, (-1, 2))
return cls(
mesh.node_x,
mesh.node_y,
node_x=mesh.node_x,
node_y=mesh.node_y,
fill_value=fill_value,
face_node_connectivity=face_node_connectivity,
edge_node_connectivity=edge_node_connectivity,
name=name,
projected=projected,
crs=crs,
Expand Down

0 comments on commit 9283eb6

Please sign in to comment.