Skip to content

Commit

Permalink
fixed testcases but might fail other things
Browse files Browse the repository at this point in the history
  • Loading branch information
veenstrajelmer committed Oct 28, 2023
1 parent 8ddbf54 commit 2f4f8f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
16 changes: 9 additions & 7 deletions hydrolib/core/dflowfm/net/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def split_by(gl: mk.GeometryList, by: float) -> list:
return lists


class Mesh2d(BaseModel):
class Mesh2d(BaseModel): #TODO: this is an inconvenient name since meshkernel also has a Mesh2d class
"""Mesh2d defines a single two dimensional grid.
Attributes:
Expand Down Expand Up @@ -181,10 +181,12 @@ def create_rectilinear(self, extent: tuple, dx: float, dy: float) -> None:
mesh2d_input = mk.MeshKernel()
mesh2d_input.curvilinear_compute_rectangular_grid(params)
mesh2d_input.curvilinear_convert_to_mesh2d() #convert to ugrid/mesh2d
mesh2d_input = mesh2d_input.mesh2d_get() #get Mesh2d object
mesh2d_input_m2d = mesh2d_input.mesh2d_get() #get Mesh2d object

# mesh2d_input_raw = mk.Mesh2d(mesh2d_input_m2d.node_x, mesh2d_input_m2d.node_y, mesh2d_input_m2d.edge_nodes)

# Process
self._process(mesh2d_input)
self._process(mesh2d_input_m2d)

def create_triangular(self, geometry_list: mk.GeometryList) -> None:
"""Create triangular grid within GeometryList object
Expand All @@ -201,9 +203,9 @@ def create_triangular(self, geometry_list: mk.GeometryList) -> None:
def _process(self, mesh2d_input) -> None:

# Add input
self.meshkernel.mesh2d_set(mesh2d_input) #TODO: in this meshkernel function duplicates the amount of nodes. Seems not desireable, but more testbanks fail if commented.
# self.meshkernel.mesh2d_set(mesh2d_input) #TODO: in this meshkernel function duplicates the amount of nodes. Seems not desireable, but more testbanks fail if commented.
# Get output
mesh2d_output = self.meshkernel.mesh2d_get() #better results for some testcases, comment above and: mesh2d_output = mesh2d_input
mesh2d_output = mesh2d_input#self.meshkernel.mesh2d_get() #better results for some testcases, comment above and: mesh2d_output = mesh2d_input

# Add to mesh2d variables
self.mesh2d_node_x = mesh2d_output.node_x
Expand Down Expand Up @@ -299,7 +301,7 @@ def clip(
# Process
self._process(self.meshkernel.mesh2d_get())

def refine(self, polygon: mk.GeometryList, level: int):
def refine(self, polygon: mk.GeometryList, level: int, min_edge_size=10.0):
"""Refine the mesh within a polygon, by a number of steps (level)
Args:
Expand All @@ -324,7 +326,7 @@ def refine(self, polygon: mk.GeometryList, level: int):
parameters = mk.MeshRefinementParameters(
refine_intersected=True,
use_mass_center_when_refining=False,
min_edge_size=10.0, # Does nothing?
min_edge_size=min_edge_size, # Does nothing?
refinement_type=1, # No effect?
connect_hanging_nodes=True,
account_for_samples_outside_face=False,
Expand Down
42 changes: 15 additions & 27 deletions tests/dflowfm/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,16 @@ def test_create_2d():


@pytest.mark.parametrize(
"deletemeshoption,inside,nnodes,nedgenodes",
"deletemeshoption,inside,nnodes,nedgenodes,nfaces",
[
#TODO: update expected values for nnodes/nedgenodes
(DeleteMeshOption.INSIDE_NOT_INTERSECTED, False, 28, 90),
(DeleteMeshOption.INSIDE_AND_INTERSECTED, False, 23, 72),
(DeleteMeshOption.INSIDE_NOT_INTERSECTED, True, 23, 72),
(DeleteMeshOption.INSIDE_AND_INTERSECTED, True, 31, 94),
(DeleteMeshOption.INSIDE_NOT_INTERSECTED, False, 22, 68, 13),
(DeleteMeshOption.INSIDE_AND_INTERSECTED, False, 30, 98, 20),
(DeleteMeshOption.INSIDE_NOT_INTERSECTED, True, 33, 100, 19),
(DeleteMeshOption.INSIDE_AND_INTERSECTED, True, 20, 62, 12),
],
)
def test_create_clip_2d(deletemeshoption, inside, nnodes, nedgenodes):

# TODO: "All complete faces, outside" does not have the expected behaviour, it is similar to "All nodes, outside"

def test_create_clip_2d(deletemeshoption, inside, nnodes, nedgenodes, nfaces):

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]),
Expand All @@ -201,7 +198,8 @@ def test_create_clip_2d(deletemeshoption, inside, nnodes, nedgenodes):
mesh2d.clip(polygon, deletemeshoption=deletemeshoption, inside=inside)
mesh2d_output = mesh2d.get_mesh2d()
assert mesh2d_output.node_x.size == nnodes
assert mesh2d_output.edge_nodes.size == nedgenodes
assert mesh2d_output.edge_nodes.size == nedgenodes # 2x nedges
assert mesh2d_output.face_x.size == nfaces


def test_create_refine_2d():
Expand All @@ -218,7 +216,7 @@ def test_create_refine_2d():
# Create within bounding box
mesh2d.create_rectilinear(extent=bbox, dx=0.5, dy=0.75)
# Refine
mesh2d.refine(polygon, 1)
mesh2d.refine(polygon, 1, min_edge_size=0.1)

mesh2d_output = mesh2d.get_mesh2d()

Expand Down Expand Up @@ -575,27 +573,17 @@ def test_create_triangular():
network.mesh2d_create_triangular_within_polygon(polygon)

assert np.array_equiv(
network._mesh2d.mesh2d_node_x, # TODO: array([6., 4., 2., 0., 6., 4., 2., 0.])
np.array([0.0, 6.0, 4.0, 2.0]),
network._mesh2d.mesh2d_node_x,
np.array([6.0, 4.0, 2.0, 0.0]),
)
assert np.array_equiv(
network._mesh2d.mesh2d_node_y, #TODO: array([2., 7., 6., 0., 2., 7., 6., 0.])
np.array([0.0, 2.0, 7.0, 6.0]),
network._mesh2d.mesh2d_node_y,
np.array([2.0, 7.0, 6.0, 0.0]),
)
assert np.array_equiv(
network._mesh2d.mesh2d_edge_nodes,
np.array([[3, 0], [0, 1], [1, 3], [1, 2], [2, 3]]),
np.array([[2, 3], [3, 0], [0, 2], [0, 1], [1, 2]]),
)

#TODO: we end up with more x nodes than before in the network instance (see above)
#when doing this with meshkernel, we get the expected amount of nodes (see below)
#so something is failing in the network class of hydrolib-core
import meshkernel
mk2 = meshkernel.MeshKernel()
mk2.mesh2d_make_triangular_mesh_from_polygon(polygon)
mesh2d_obj = mk2.mesh2d_get()
print(mesh2d_obj.node_x) # [6. 4. 2. 0.]
print(mesh2d_obj.node_y) # [2. 7. 6. 0.]


def test_add_1d2d_links():
Expand Down

0 comments on commit 2f4f8f2

Please sign in to comment.