Skip to content

Commit

Permalink
Merge tag 'v4.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-el-sayed committed Jan 25, 2024
2 parents caae4cc + 3b81559 commit 0d1ce2a
Show file tree
Hide file tree
Showing 11 changed files with 518 additions and 53 deletions.
53 changes: 30 additions & 23 deletions docs/examples/05_mesh2d_refinement_gridded_samples.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
" # Read data values\n",
" for line in file:\n",
" data_row = [float(value) for value in line.strip().split()]\n",
" data.insert(0, data_row) # Insert row at the beginning\n",
" data.append(data_row) # Insert row at the beginning\n",
"\n",
" # Flatten the data\n",
" data = np.array(data).flatten().astype(dtype)\n",
Expand Down Expand Up @@ -341,7 +341,7 @@
"mesh_refinement_parameters = meshkernel.MeshRefinementParameters(refine_intersected=False,\n",
" use_mass_center_when_refining=False,\n",
" min_edge_size=500, \n",
" refinement_type=meshkernel.RefinementType(1),\n",
" refinement_type=meshkernel.RefinementType.WAVE_COURANT,\n",
" connect_hanging_nodes=True, \n",
" account_for_samples_outside_face=False, \n",
" max_refinement_iterations=3,\n",
Expand Down
47 changes: 25 additions & 22 deletions docs/examples/07_curvilineargrid_with_defined_extension.ipynb

Large diffs are not rendered by default.

314 changes: 314 additions & 0 deletions docs/examples/11_mesh2d_refine_ridges_gridded_samples.ipynb

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions docs/examples/data_examples/gaussian_bump.asc

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions meshkernel/meshkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,40 @@ def mesh2d_refine_based_on_samples(
byref(c_refinement_params),
)

def mesh2d_refine_ridges_based_on_gridded_samples(
self,
gridded_samples: GriddedSamples,
relative_search_radius: float,
minimum_num_samples: int,
number_of_smoothing_iterations: int,
mesh_refinement_params: MeshRefinementParameters,
) -> None:
"""Refines a mesh2d based on samples with ridge refinement. This method automatically detects the ridges in a sample set.
Args:
gridded_samples (GriddedSamples): The gridded samples.
relative_search_radius (float): The relative search radius relative to the face size,
used for some interpolation algorithms.
minimum_num_samples (int): The minimum number of samples used for some averaging algorithms.
number_of_smoothing_iterations (int): The number of smoothing iterations to apply to the input sample set.
mesh_refinement_params (MeshRefinementParameters): The mesh refinement parameters.
"""

c_gridded_samples = CGriddedSamples.from_griddedSamples(gridded_samples)
c_refinement_params = CMeshRefinementParameters.from_meshrefinementparameters(
mesh_refinement_params
)

self._execute_function(
self.lib.mkernel_mesh2d_refine_ridges_based_on_gridded_samples,
self._meshkernelid,
byref(c_gridded_samples),
c_double(relative_search_radius),
c_int(minimum_num_samples),
c_int(number_of_smoothing_iterations),
byref(c_refinement_params),
)

def mesh2d_refine_based_on_gridded_samples(
self,
gridded_samples: GriddedSamples,
Expand Down
3 changes: 3 additions & 0 deletions meshkernel/py_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class RefinementType(IntEnum):
"""Refinement that refines recursively a fixed number of times."""
REFINEMENT_LEVELS = 2

"""Refinement based on ridge detection."""
RIDGE_DETECTION = 3


@unique
class Mesh2dLocation(IntEnum):
Expand Down
4 changes: 2 additions & 2 deletions meshkernel/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# MeshKernelPy version
__version__ = "4.0.0"
__version__ = "4.0.1"

# MeshKernel version
__backend_version__ = "4.0.0"
__backend_version__ = "4.0.1"
3 changes: 2 additions & 1 deletion tests/test_c_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Mesh2d,
MeshRefinementParameters,
OrthogonalizationParameters,
RefinementType,
)
from meshkernel.c_structures import (
CContacts,
Expand Down Expand Up @@ -164,7 +165,7 @@ def test_cmeshrefinementparameters_from_meshrefinementparameters():
assert c_parameters.refine_intersected == 0
assert c_parameters.use_mass_center_when_refining == 1
assert c_parameters.min_edge_size == 1.0
assert c_parameters.refinement_type == 2
assert c_parameters.refinement_type == RefinementType.REFINEMENT_LEVELS.value
assert c_parameters.connect_hanging_nodes == 0
assert c_parameters.account_for_samples_outside_face == 1

Expand Down
53 changes: 53 additions & 0 deletions tests/test_mesh2d_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,59 @@ def test_mesh2d_refine_based_on_samples(
assert mesdh2d.face_x.size == exp_faces


def test_refine_ridges_based_on_gridded_samples(meshkernel_with_mesh2d: MeshKernel):
"""Tests `mkernel_mesh2d_refine_ridges_based_on_gridded_samples` with a simple 5x4 mesh."""
num_rows = 21
num_columns = 41
mk = meshkernel_with_mesh2d(
rows=num_rows, columns=num_columns, spacing_x=100.0, spacing_y=100.0
)

refinement_params = MeshRefinementParameters(
refine_intersected=False,
use_mass_center_when_refining=False,
min_edge_size=2.0,
connect_hanging_nodes=True,
account_for_samples_outside_face=False,
refinement_type=RefinementType.RIDGE_DETECTION,
max_refinement_iterations=3,
smoothing_iterations=0,
)

num_sample_x_coordinates = (num_columns - 1) * 2 + 1
num_sample_y_coordinates = (num_rows * 1) * 2 + 1

gridded_samples = GriddedSamples(
num_x=num_sample_x_coordinates,
num_y=num_sample_y_coordinates,
x_origin=0.0,
y_origin=-0.0,
cell_size=5.0,
values=np.array(
[-0.05] * num_sample_x_coordinates * num_sample_y_coordinates,
dtype=np.float32,
),
)

relative_search_radius = 1.01
minimum_num_samples = 1
number_of_smoothing_iterations = 0

mk.mesh2d_refine_ridges_based_on_gridded_samples(
gridded_samples=gridded_samples,
relative_search_radius=relative_search_radius,
minimum_num_samples=minimum_num_samples,
number_of_smoothing_iterations=number_of_smoothing_iterations,
mesh_refinement_params=refinement_params,
)

mesdh2d = mk.mesh2d_get()

assert mesdh2d.node_x.size == 924
assert mesdh2d.edge_x.size == 1784
assert mesdh2d.face_x.size == 861


cases_mesh2d_refine_based_on_gridded_samples = [
(
GriddedSamples(
Expand Down
7 changes: 4 additions & 3 deletions tests/test_py_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ def test_projecttolandboundaryoption_values(
assert enum_val == exp_int


cases_refinementtype_values = [
cases_refinement_type_values = [
(RefinementType.WAVE_COURANT, 1),
(RefinementType.REFINEMENT_LEVELS, 2),
(RefinementType.RIDGE_DETECTION, 3),
]


@pytest.mark.parametrize("enum_val, exp_int", cases_refinementtype_values)
def test_refinementtype_values(enum_val: RefinementType, exp_int: int):
@pytest.mark.parametrize("enum_val, exp_int", cases_refinement_type_values)
def test_refinement_type_values(enum_val: RefinementType, exp_int: int):
"""Tests the integer values of the `RefinementType` enum."""

assert enum_val == exp_int
Expand Down

0 comments on commit 0d1ce2a

Please sign in to comment.