Skip to content

Commit

Permalink
Merge pull request #158 from tataratat/fix-meshio-load
Browse files Browse the repository at this point in the history
update meshio.load
  • Loading branch information
j042 authored Sep 22, 2023
2 parents 882127e + 2f35838 commit fb33092
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
14 changes: 10 additions & 4 deletions examples/show_gmsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,34 @@

import load_sample_file

import gustaf
from gustaf import io

if __name__ == "__main__":
mesh_file_tri = pathlib.Path("faces/tri/2DChannelTria.msh")
mesh_file_quad = pathlib.Path("faces/quad/2DChannelQuad.msh")
mesh_file_tetra = pathlib.Path("volumes/tet/3DBrickTet.msh")

base_samples_path = pathlib.Path(__file__).parent / "samples"
load_sample_file.load_sample_file(str(mesh_file_tri))
load_sample_file.load_sample_file(str(mesh_file_quad))
load_sample_file.load_sample_file(str(mesh_file_tetra))

# load the .msh file directly with the correct io module (meshio)
loaded_mesh_tri = io.meshio.load(base_samples_path / mesh_file_tri)

loaded_mesh_tri.show()
gustaf.show(loaded_mesh_tri)

# load the .msh file directly with the correct io module (meshio)
loaded_mesh_quad = io.meshio.load(base_samples_path / mesh_file_quad)

loaded_mesh_quad.show()
gustaf.show(loaded_mesh_quad)

# load the .msh file with the default load function which needs to find out
# it self which module is the correct one.
loaded_mesh_default = io.load(base_samples_path / mesh_file_tri)
loaded_mesh_default = io.load(base_samples_path / mesh_file_tetra)

loaded_mesh_default.show()
gustaf.show(
*[[msh.__class__.__name__, msh] for msh in loaded_mesh_default],
title="3D mesh with tetrahedrons",
)
2 changes: 1 addition & 1 deletion gustaf/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Current version.
"""
version = "0.0.17"
version = "0.0.18"
59 changes: 35 additions & 24 deletions gustaf/io/meshio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@

import numpy as np

from gustaf.edges import Edges
from gustaf.faces import Faces
from gustaf.helpers.raise_if import ModuleImportRaiser
from gustaf.utils import log
from gustaf.vertices import Vertices
from gustaf.volumes import Volumes

try:
import meshio
except ModuleNotFoundError as err:
meshio = ModuleImportRaiser("meshio", err)
# from meshio import Mesh as MeshioMesh

_meshio2gus = {
"hexahedron": Volumes,
"tetra": Volumes,
"quad": Faces,
"triangle": Faces,
"line": Edges,
"vertex": Vertices,
}


def load(fname):
Expand All @@ -34,42 +45,42 @@ def load(fname):
Returns
--------
MESH_TYPES
MESH_TYPES | List[MESH_TYPES]
"""
mesh_type = Faces

# fname sanity check
fname = pathlib.Path(fname)
if not (fname.exists() and fname.is_file()):
raise ValueError(
"The given file does not point to file. The given path is: "
f"{fname.resolve()}"
)

# load
meshio_mesh: meshio.Mesh = meshio.read(fname)

# first get vertices
vertices = meshio_mesh.points

# check for 2D mesh
# Try for triangle grid
cells = meshio_mesh.get_cells_type("triangle")
# If no triangle elements, try for square
if len(cells) == 0:
cells = meshio_mesh.get_cells_type("quad")
if not len(cells) > 0:
# 3D mesh
mesh_type = Volumes
cells = meshio_mesh.get_cells_type("tetra")
if len(cells) == 0:
cells = meshio_mesh.get_cells_type("hexahedron")

for i, cell in enumerate(cells):
if i == 0:
elements = cell.data
else:
elements = np.vstack((elements, cell.data))
# early exit if cells doesn't exist
if len(meshio_mesh.cells_dict) == 0:
return Vertices(vertices)

mesh = mesh_type(vertices=vertices, elements=elements)
meshes = []
for element_type, elements in meshio_mesh.cells_dict.items():
# skip unsupported
if element_type not in _meshio2gus:
log.warning(
f"`{element_type}`-elements are not supported in gustaf"
)
continue
if element_type.startswith("vertex"):
meshes.append(Vertices(vertices[elements.ravel()]))
else:
meshes.append(
_meshio2gus[element_type](vertices, elements=elements)
)

return mesh
return meshes[0] if len(meshes) == 1 else meshes


def export(mesh, fname, submeshes=None, **kwargs):
Expand Down

0 comments on commit fb33092

Please sign in to comment.