Skip to content

Commit

Permalink
fix get mesh; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobEliasWagner committed Jan 23, 2024
1 parent 210f373 commit 5089664
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/nos/data/helmholtz/solver/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def get_mesh(
mesh_file: pathlib.Path, comm: MPI.Comm = MPI.COMM_SELF
) -> Tuple[dolfinx.mesh.Mesh, dolfinx.mesh.MeshTags, dolfinx.mesh.MeshTags]:
) -> (Tuple)[dolfinx.mesh.Mesh, dolfinx.mesh.MeshTags, dolfinx.mesh.MeshTags]:
"""Loads mesh from a msh file and returns it as a dolfinx mesh. Initializes mesh on rank 0 of the given
communicator.
Expand All @@ -19,5 +19,8 @@ def get_mesh(
Returns:
Tuple containing the dolfinx mesh, mesh-tags and boundary tags.
"""
gmsh.initialize()
gmsh.open(mesh_file)
return dolfinx.io.gmshio.model_to_mesh(gmsh.model, comm, 0)
mesh, cell_tags, boundary_tags = dolfinx.io.gmshio.model_to_mesh(gmsh.model, comm, 0)
gmsh.finalize()
return mesh, cell_tags, boundary_tags
Empty file.
52 changes: 52 additions & 0 deletions test/data/helmholtz/solver/test_solver_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pathlib
import tempfile

import dolfinx
import gmsh
from mpi4py import MPI

from nos.data.helmholtz.solver import get_mesh


def test_get_mesh():
# create mesh
gmsh.initialize()
gmsh.model.add("test_get_mesh")

rect = gmsh.model.occ.add_rectangle(0.0, 0.0, 0.0, 1.0, 1.0)
circ = gmsh.model.occ.add_disk(0.0, 0.0, 0.0, 0.5, 0.5)

objs, _ = gmsh.model.occ.fragment([(2, rect)], [(2, circ)])

gmsh.model.occ.synchronize()

surfs = gmsh.model.occ.get_entities(2)
surfs = [surf[1] for surf in surfs]
lines = gmsh.model.occ.get_entities(1)
lines = [line[1] for line in lines]

for i, surf in enumerate(surfs):
gmsh.model.add_physical_group(2, [surf], i)
for i, line in enumerate(lines):
gmsh.model.add_physical_group(1, [line], i)

gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(2)

with tempfile.TemporaryDirectory() as tmp:
# write file
file_path = pathlib.Path(tmp).joinpath("test_mesh.msh")
gmsh.write(file_path)
gmsh.finalize()

# load
mesh, cell_tags, boundary_tags = get_mesh(file_path, MPI.COMM_WORLD)

# test types
assert isinstance(mesh, dolfinx.mesh.Mesh)
assert isinstance(cell_tags, dolfinx.mesh.MeshTags)
assert isinstance(boundary_tags, dolfinx.mesh.MeshTags)

# test size
assert len(lines) == len(set(boundary_tags.values))
assert len(surfs) == len(set(cell_tags.values))

0 comments on commit 5089664

Please sign in to comment.