Skip to content

Commit

Permalink
update meshio.load
Browse files Browse the repository at this point in the history
  • Loading branch information
j042 committed Sep 22, 2023
1 parent 882127e commit 1957978
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
7 changes: 4 additions & 3 deletions examples/show_gmsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import load_sample_file

import gustaf
from gustaf import io

if __name__ == "__main__":
Expand All @@ -20,15 +21,15 @@
# 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.show()
gustaf.show(loaded_mesh_default)
52 changes: 29 additions & 23 deletions gustaf/io/meshio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@

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,
}


def load(fname):
Expand All @@ -36,40 +46,36 @@ def load(fname):
--------
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)

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

mesh = mesh_type(vertices=vertices, elements=elements)
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 1957978

Please sign in to comment.