Skip to content

Commit

Permalink
Add mesh2d_refine_ridges_based_on_gridded_samples API function (#138
Browse files Browse the repository at this point in the history
…| GRIDEDIT-873)
  • Loading branch information
lucacarniato authored Jan 25, 2024
1 parent 3514a16 commit 3b81559
Show file tree
Hide file tree
Showing 8 changed files with 520 additions and 59 deletions.
75 changes: 41 additions & 34 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: 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

0 comments on commit 3b81559

Please sign in to comment.