Skip to content

Commit

Permalink
Remove mesher_base: generate method need finalize keyword for GmshMesher
Browse files Browse the repository at this point in the history
  • Loading branch information
Huite committed Aug 31, 2024
1 parent 71b5212 commit 2d98d85
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 37 deletions.
2 changes: 2 additions & 0 deletions docs/api/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Added
:meth:`pandamesh.GmshMesher.add_structured_field`, and
:meth:`pandamesh.GmshMesher.add_structured_field_from_dataarray` to enable
Gmsh fields from geometry or from raster data.
- Added ``finalize`` keyword to :meth:`pandamesh.GmshMesher.generate` to
automatically finalize after mesh generation.

Changed
~~~~~~~
Expand Down
35 changes: 33 additions & 2 deletions pandamesh/gmsh_mesher.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
move_origin,
repr,
separate_geodataframe,
to_geodataframe,
to_ugrid,
)
from pandamesh.gmsh_enums import (
FieldCombination,
Expand All @@ -31,7 +33,6 @@
ThresholdField,
)
from pandamesh.gmsh_geometry import add_distance_geometry, add_geometry
from pandamesh.mesher_base import MesherBase


@contextmanager
Expand All @@ -49,7 +50,7 @@ def gmsh_env(read_config_files: bool = True, interruptible: bool = True):
gmsh.finalize()


class GmshMesher(MesherBase):
class GmshMesher:
"""
Wrapper for the python bindings to Gmsh. This class must be initialized
with a geopandas GeoDataFrame containing at least one polygon, and a column
Expand Down Expand Up @@ -684,6 +685,36 @@ def generate(self, finalize: bool = False) -> Tuple[FloatArray, IntArray]:
self.finalize()
return vertices, faces

def generate_geodataframe(self, finalize: bool = False) -> gpd.GeoDataFrame:
"""
Generate a mesh and return it as a geopandas GeoDataFrame.
Parameters
----------
finalize: bool, default False
Automatically finalize after generating.
Returns
-------
mesh: geopandas.GeoDataFrame
"""
return to_geodataframe(*self.generate(finalize=finalize))

def generate_ugrid(self, finalize: bool = False) -> "xugrid.Ugrid2d": # type: ignore # noqa pragma: no cover
"""
Generate a mesh and return it as an xugrid Ugrid2d.
Parameters
----------
finalize: bool, default False
Automatically finalize after generating.
Returns
-------
mesh: xugrid.Ugrid2d
"""
return to_ugrid(*self.generate(finalize=finalize))

def write(self, path: Union[str, pathlib.Path]):
"""
Write a gmsh .msh file
Expand Down
32 changes: 0 additions & 32 deletions pandamesh/mesher_base.py

This file was deleted.

25 changes: 23 additions & 2 deletions pandamesh/triangle_mesher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
check_geodataframe,
repr,
separate_geodataframe,
to_geodataframe,
to_ugrid,
)
from pandamesh.mesher_base import MesherBase
from pandamesh.triangle_enums import DelaunayAlgorithm
from pandamesh.triangle_geometry import collect_geometry, polygon_holes


class TriangleMesher(MesherBase):
class TriangleMesher:
"""
Wrapper for the python bindings to Triangle. This class must be initialized
with a geopandas GeoDataFrame containing at least one polygon, and a column
Expand Down Expand Up @@ -213,3 +214,23 @@ def generate(self) -> Tuple[FloatArray, IntArray]:
vertices[:, 0] += self._xoff
vertices[:, 1] += self._yoff
return vertices, result["triangles"]

def generate_geodataframe(self) -> gpd.GeoDataFrame:
"""
Generate a mesh and return it as a geopandas GeoDataFrame.
Returns
-------
mesh: geopandas.GeoDataFrame
"""
return to_geodataframe(*self.generate())

def generate_ugrid(self) -> "xugrid.Ugrid2d": # type: ignore # noqa pragma: no cover
"""
Generate a mesh and return it as an xugrid Ugrid2d.
Returns
-------
mesh: xugrid.Ugrid2d
"""
return to_ugrid(*self.generate())
2 changes: 1 addition & 1 deletion tests/test_meshers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_singleton_gmsh_mesher():
mesher.generate()

new_mesher.generate(finalize=True)
assert not new_mesher._intialized
assert not new_mesher._initialized


def bounds(vertices):
Expand Down

0 comments on commit 2d98d85

Please sign in to comment.