Skip to content

Commit babec0e

Browse files
Compute illegal cells polygon when constructing mesh2d with face-node connectivity (#248 | Gridedit 2042)
* GRIDEDIT-2042 Updated comment * GRIDEDIT-2042 Use mesh.nodes_per_face instead of mesh.face_x when setting mesh.num_faces. This is then consistent with mesh.face_nodes array (both will be set or both will be null) * add testcase for mesh2d_set including holes/face_node_connectivity * black --------- Co-authored-by: veenstrajelmer <60435591+veenstrajelmer@users.noreply.github.com> Co-authored-by: veenstrajelmer <veenstrajelmer@gmail.com>
1 parent 82a28b8 commit babec0e

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

meshkernel/c_structures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def from_mesh2d(mesh2d: Mesh2d) -> CMesh2d:
111111
# Set the sizes
112112
c_mesh2d.num_nodes = mesh2d.node_x.size
113113
c_mesh2d.num_edges = mesh2d.edge_nodes.size // 2
114-
c_mesh2d.num_faces = mesh2d.face_x.size
114+
c_mesh2d.num_faces = mesh2d.nodes_per_face.size
115115
c_mesh2d.num_face_nodes = mesh2d.face_nodes.size
116116

117117
return c_mesh2d

meshkernel/meshkernel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def mesh2d_delete_faces_in_polygons(
286286
self,
287287
geometry_list: GeometryList,
288288
) -> None:
289-
"""Deletes a faces of the mesh that lie inside a polygon or polygons.
289+
"""Deletes the faces of the mesh that lie inside a polygon or polygons.
290290
291291
Args:
292292
geometry_list (GeometryList): The GeometryList describing the polygon where to perform the deletion.

tests/test_mesh2d_basics.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,76 @@ def test_mesh2d_set_and_mesh2d_get():
7373
assert_array_equal(output_mesh2d.edge_y, np.array([0.0, 0.5, 1.0, 0.5]))
7474

7575

76+
def test_mesh2d_set_and_mesh2d_get_with_holes():
77+
"""
78+
mesh2d_set has to preserve any existing holes/illegalcells in the grid
79+
Was not the case before resolving issue
80+
https://github.com/Deltares/MeshKernelPy/issues/247
81+
"""
82+
83+
# create base grid
84+
projection = ProjectionType.SPHERICAL
85+
make_grid_parameters = MakeGridParameters(
86+
angle=0,
87+
origin_x=147.75,
88+
origin_y=-40.4,
89+
upper_right_x=147.9,
90+
upper_right_y=-40.25,
91+
block_size_x=0.01,
92+
block_size_y=0.01,
93+
)
94+
95+
mk = MeshKernel(projection=projection)
96+
mk.curvilinear_compute_rectangular_grid_on_extension(make_grid_parameters)
97+
mk.curvilinear_convert_to_mesh2d()
98+
99+
# this grid has 320 faces before deletion
100+
assert len(mk.mesh2d_get().face_x) == 320
101+
102+
# cut a hole in the grid
103+
xx, yy = np.array(
104+
[
105+
[147.815, -40.282],
106+
[147.835, -40.282],
107+
[147.835, -40.305],
108+
[147.815, -40.305],
109+
[147.815, -40.282],
110+
]
111+
).T
112+
113+
delete_pol_geom = GeometryList(
114+
x_coordinates=xx,
115+
y_coordinates=yy,
116+
geometry_separator=-999,
117+
)
118+
mk.mesh2d_delete(
119+
geometry_list=delete_pol_geom,
120+
delete_option=DeleteMeshOption.INSIDE_NOT_INTERSECTED,
121+
invert_deletion=False,
122+
)
123+
124+
mk_grid = mk.mesh2d_get()
125+
126+
# recreate the grid with minimal information, specifically by leaving out
127+
# face_x/face_y but including face_nodes and nodes_per_face since this is
128+
# the face_node_connectivity
129+
mk2_grid = Mesh2d(
130+
node_x=mk_grid.node_x,
131+
node_y=mk_grid.node_y,
132+
edge_nodes=mk_grid.edge_nodes,
133+
face_nodes=mk_grid.face_nodes,
134+
nodes_per_face=mk_grid.nodes_per_face,
135+
)
136+
137+
mk2 = MeshKernel(projection=projection)
138+
mk2.mesh2d_set(mk2_grid)
139+
140+
# assert the number of faces, if the face_node_connectivity includes a face
141+
# at the location of the deleted cells, there will be 319 faces
142+
assert len(mk.mesh2d_get().face_x) == 318
143+
assert len(mk2.mesh2d_get().face_x) == 318
144+
145+
76146
def test_mesh2d_add():
77147
"""Test adding a 2d mesh"""
78148
mk = MeshKernel()

0 commit comments

Comments
 (0)