Skip to content

Commit

Permalink
Merge pull request #23 from Deltares/gmsh_init_kwargs
Browse files Browse the repository at this point in the history
Add gmsh initialization options
  • Loading branch information
Huite authored Jul 12, 2024
2 parents 792f180 + 116ae10 commit d9e197e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
35 changes: 28 additions & 7 deletions pandamesh/gmsh_mesher.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@


@contextmanager
def gmsh_env():
def gmsh_env(read_config_files: bool = True, interruptible: bool = True):
try:
gmsh.initialize()
gmsh.initialize(
readConfigFiles=read_config_files, run=False, interruptible=interruptible
)
# There's gotta be a better way to actually raise proper errors...
gmsh.option.setNumber("General.Terminal", 1)
yield
Expand Down Expand Up @@ -155,10 +157,28 @@ class GmshMesher:
A helpful index can be found near the bottom:
https://gmsh.info/doc/texinfo/gmsh.html#Syntax-index
Parameters
----------
gdf: gpd.GeoDataFrame
GeoDataFrame containing the vector geometry.
read_config_files: bool
Gmsh initialization option: Read system Gmsh configuration files
(gmshrc and gmsh-options).
interruptible: bool
Gmsh initialization option.
"""

def __init__(self, gdf: gpd.GeoDataFrame) -> None:
self._initialize_gmsh()
def __init__(
self,
gdf: gpd.GeoDataFrame,
read_config_files: bool = True,
run: bool = False,
interruptible: bool = True,
) -> None:
self._initialize_gmsh(
read_config_files=read_config_files, interruptible=interruptible
)
check_geodataframe(gdf)
polygons, linestrings, points = separate(gdf)

Expand Down Expand Up @@ -188,9 +208,11 @@ def __repr__(self):
return repr(self)

@staticmethod
def _initialize_gmsh():
def _initialize_gmsh(read_config_files: bool = True, interruptible: bool = True):
GmshMesher.finalize_gmsh()
gmsh.initialize()
gmsh.initialize(
readConfigFiles=read_config_files, run=False, interruptible=interruptible
)
gmsh.option.setNumber("General.Terminal", 1)

@staticmethod
Expand All @@ -201,7 +223,6 @@ def finalize_gmsh():

# Properties
# ----------

@property
def mesh_algorithm(self):
"""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_meshers.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,16 @@ def test_gmsh_properties():
assert mesher.mesh_size_from_curvature is True
assert mesher.field_combination == pm.FieldCombination.MEAN
assert mesher.subdivision_algorithm == pm.SubdivisionAlgorithm.BARYCENTRIC


@pytest.mark.parametrize("read_config_files", [True, False])
@pytest.mark.parametrize("interruptible", [True, False])
def test_gmsh_initialization_kwargs(read_config_files, interruptible):
gdf = gpd.GeoDataFrame(geometry=[donut])
gdf["cellsize"] = 1.0
mesher = pm.GmshMesher(
gdf, read_config_files=read_config_files, interruptible=interruptible
)
vertices, triangles = mesher.generate()
mesh_area = area(vertices, triangles).sum()
assert np.allclose(mesh_area, donut.area)

0 comments on commit d9e197e

Please sign in to comment.