Skip to content

Refactor mesh2d_get_property and related methods #260

Closed

Description

@veenstrajelmer

The meshkernelpy code related to getting properties is slightly inconsistent, some example code to illustrate this:

import numpy as np
import matplotlib.pyplot as plt
plt.close('all')
from meshkernel import (
    MeshKernel,
    ProjectionType,
    GeometryList,
    MakeGridParameters,
    MeshRefinementParameters,
    Mesh2d,
    Mesh2dLocation,
    )

# create basegrid
lon_min, lon_max, lat_min, lat_max = -6, 2, 48.5, 51.2
dxy = 0.7
make_grid_parameters = MakeGridParameters(
    origin_x=lon_min,
    origin_y=lat_min,
    upper_right_x=lon_max,
    upper_right_y=lat_max,
    block_size_x=dxy,
    block_size_y=dxy,
    )
mk = MeshKernel(projection=ProjectionType.SPHERICAL)
mk.curvilinear_compute_rectangular_grid_on_extension(make_grid_parameters)
mk.curvilinear_convert_to_mesh2d() #convert to ugrid/mesh2d

# refine with polygon
pol_x = np.array([-5,-4,0,-5], dtype=np.double)
pol_y = np.array([49,51,49.5,49], dtype=np.double)
geometry_list = GeometryList(pol_x, pol_y)
mrp = MeshRefinementParameters()
mk.mesh2d_refine_based_on_polygon(polygon=geometry_list, mesh_refinement_params=mrp)

# TODO: instead of providing property and location, just get properties FACE_CIRCUMCENTER, EDGE_ORTHOGONALITY, etc
# since getting EDGE_LENGTHS at FACES results in error anyway: "MeshKernelError: Exception of type 'MeshKernelError': Property not supported at this location"
face_circumcenters = mk.mesh2d_get_property(mesh2d_location=Mesh2dLocation.FACES, property=Mesh2d.Property.FACE_CIRCUMCENTER)
edge_orthogonality = mk.mesh2d_get_property(mesh2d_location=Mesh2dLocation.EDGES, property=Mesh2d.Property.ORTHOGONALITY)
edge_length = mk.mesh2d_get_property(mesh2d_location=Mesh2dLocation.EDGES, property=Mesh2d.Property.EDGE_LENGTHS)

# TODO: remove mesh2d_get_orthogonality() since it can be retrieved via mesh2d_get_property()
edge_orthogonality2 = mk.mesh2d_get_orthogonality()
# TODO: deprecate mesh2d_get_smoothness in favor of a EDGE_SMOOTHNESS property
edge_smoothness = mk.mesh2d_get_smoothness()

# TODO: x/y coordinates are valid for edge_orthogonality2 and face_circumcenters
# but are arrays with arbitrary values for edge_orthogonality. This is inconvenient for plotting
print(face_circumcenters.x_coordinates[:5])
print(edge_orthogonality.x_coordinates[:5]) # contains arbitrary/nan values
print(edge_orthogonality2.x_coordinates[:5])

Todo:

  • Mesh2d.Property is the only IntEnum under Mesh2d. Consider moving it outside of the Mesh2d class and renaming it to Mesh2dProperty for consistency. [Python-only, not used in dfm_tools/hydrolib-core/hydromt_delft3dfm/BaselineTools] >> Feature/gridedit 2297 rename mesh2d property and several functions #281
  • rename mkernel_set_property to mesh2d_set_property, to be consistent mesh2d_get_property. Also rename mkernel_delete_property and mkernel_mesh2d_casulli_refinement_based_on_depths to have mesh2d_ prefix. [Python only, but not used in dfm_tools/hydrolib-core/hydromt_delft3dfm/BaselineTools] >> Feature/gridedit 2297 rename mesh2d property and several functions #281
  • smoothness can now only be retrieved via mesh2d_get_smoothness, but this should be possible via mesh2d_get_property instead. [Python and C, but adding functionality so not a problem] >> Feature/gridedit 2300 add edge smoothness to get property MeshKernel#543 and GRIDEDIT-2300 Add edge smoothness enum value #282
  • deprecate mesh2d_get_smoothness and mesh2d_get_orthogonality >> follow-up in Deprecate/remove old APIs for smoothness and orthogonality #284
  • All properties are location-specific. Therefore it does not make sense to ask the user to provide both. Instead, consider deprecating the location argument. Incorrect combinations will just raise an error anyway: "MeshKernelError: Exception of type 'MeshKernelError': Property not supported at this location". Location is required for user-defined properties because it supports interpolation, but then it is good to split it into two APIs, one for predefined properties and one for user-defined properties. [C and Python, so first check if it is worth the API change] >> MKnet uses get_property C-API (edge_lenght/face_circumcenters/netlinkcontours/facebounds, as of 11 August 2026), also provides the location argument so would be a breaking change. >> won't do for now.

Version info

  • meshkernelpy version 8.2.1

Activity

  1. changed the title [-]Consider refactoring mesh2d_get_property and related methods[/-] [+]Refactor mesh2d_get_property and related methods[/+] on Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions