Skip to content

mrambausek/meshio

 
 

Repository files navigation

meshio

I/O for mesh files.

gh-actions codecov Code style: black PyPI pyversions PyPi Version Anaconda Cloud Debian CI DOI GitHub stars PyPi downloads Slack

There are various mesh formats available for representing unstructured meshes. meshio can read and write all of the following and smoothly converts between them:

Abaqus, ANSYS msh, AVS-UCD, CGNS, DOLFIN XML, Exodus, FLAC3D, H5M, Kratos/MDPA, Medit, MED/Salome, Nastran (bulk data), Neuroglancer precomputed format, Gmsh (format versions 2.2, 4.0, and 4.1), OBJ, OFF, PERMAS, PLY, STL, Tecplot .dat, TetGen .node/.ele, SVG (2D only, output only), UGRID, VTK, VTU (not raw binary data), WKT (TIN), XDMF.

Install with

pip install meshio[all]

([all] pulls in all optional dependencies. By default, meshio only uses numpy.) You can then use the command-line tools

meshio-convert    input.msh output.vtk   # convert between two formats

meshio-info       input.xdmf             # show some info about the mesh

meshio-compress   input.vtu              # compress the mesh file
meshio-decompress input.vtu              # decompress the mesh file

meshio-binary     input.msh              # convert to binary format
meshio-ascii      input.msh              # convert to ASCII format

with any of the supported formats.

In Python, simply do

import meshio

mesh = meshio.read(
    filename,  # string, os.PathLike, or a buffer/open file
    file_format="stl"  # optional if filename is a path; inferred from extension
)
# mesh.points, mesh.cells, mesh.cells_dict, ...

# mesh.vtk.read() is also possible

to read a mesh. To write, do

points = numpy.array([
    [0.0, 0.0, 0.0],
    [0.0, 1.0, 0.0],
    [0.0, 0.0, 1.0],
    ])
cells = [
    ("triangle", numpy.array([[0, 1, 2]]))
]
meshio.write_points_cells(
    "foo.vtk",
    points,
    cells,
    # Optionally provide extra data on points, cells, etc.
    # point_data=point_data,
    # cell_data=cell_data,
    # field_data=field_data
    )

or explicitly create a mesh object for writing

mesh = meshio.Mesh(points, cells)
meshio.write(
    "foo.vtk",  # str, os.PathLike, or buffer/ open file
    mesh,
    # file_format="vtk",  # optional if first argument is a path; inferred from extension
)

# mesh.vtk.write() is also possible

For both input and output, you can optionally specify the exact file_format (in case you would like to enforce ASCII over binary VTK, for example).

Reading and writing can also be handled directly by the Mesh object:

m = meshio.Mesh.read(filename, "vtk")  # same arguments as meshio.read
m.write("foo.vtk")  # same arguments as meshio.write, besides `mesh`

Time series

The XDMF format supports time series with a shared mesh. You can write times series data using meshio with

with meshio.xdmf.TimeSeriesWriter(filename) as writer:
    writer.write_points_cells(points, cells)
    for t in [0.0, 0.1, 0.21]:
        writer.write_data(t, point_data={"phi": data})

and read it with

with meshio.xdmf.TimeSeriesReader(filename) as reader:
    points, cells = reader.read_points_cells()
    for k in range(reader.num_steps):
        t, point_data, cell_data = reader.read_data(k)

ParaView plugin

gmsh paraview

*A Gmsh file opened with ParaView.*

If you have downloaded a binary version of ParaView, you may proceed as follows.

  • Make sure that ParaView uses a Python version that supports meshio. (That is at least Python 3.)
  • Install meshio
  • Open ParaView
  • Find the file paraview-meshio-plugin.py of your meshio installation (on Linux: ~/.local/share/paraview/plugins/) and load it under Tools / Manage Plugins / Load New
  • Optional: Activate Auto Load

You can now open all meshio-supported files in ParaView.

Performance comparison

The comparisons here are for a triangular mesh with about 900k points and 1.8M triangles. The red lines mark the size of the mesh in memory.

File sizes

file size

I/O speed

performance

Maximum memory usage

memory usage

Installation

meshio is available from the Python Package Index, so simply do

pip install meshio

to install.

Additional dependencies (netcdf4, h5py) are required for some of the output formats and can be pulled in by

pip install meshio[all]

You can also install meshio from anaconda:

conda install -c conda-forge meshio

Testing

To run the meshio unit tests, check out this repository and type

pytest

License

meshio is published under the MIT license.

About

I/O for various mesh formats

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 99.8%
  • Makefile 0.2%