meshio

repository·main·Indexed 25 days ago

https://github.com/nschloe/meshio

A Python library for reading and writing various unstructured mesh formats, providing conversion between them. It includes a command-line interface for quick conversions and inspections, and supports formats such as Abaqus .inp, FLAC3D .f3grid, Gmsh .msh, KratosMultiphysics .mdpa, Medit, Neuroglancer, and Wavefront .obj. It also provides a plugin for ParaView and specialized handling for XDMF time series.

Tokens
4.2K
Snippets
5
Records
36
Agent score
81%

What's inside meshio

  1. Install meshio via pip or conda

    main

    You can install meshio using pip or conda. By default, meshio only uses numpy. To include all optional dependencies (such as netcdf4 and h5py) required for certain output formats, use the [all] extra.

    Using pip:

    pip install meshio[all]

    Using conda:

    conda install -c conda-forge meshio
    pip install meshio[all]
  2. Use meshio as a ParaView plugin

    main

    You can use meshio to open supported mesh files directly in ParaView by loading the paraview-meshio-plugin.py plugin.

    1. Install meshio for the specific Python version used by ParaView (check this via pvpython --version).
    2. Locate the plugin: Find paraview-meshio-plugin.py within your meshio installation (e.g., on Linux: ~/.local/share/paraview-5.9/plugins/).
    3. Load in ParaView: Open ParaView, go to Tools > Manage Plugins > Load New, and select the plugin file.
    4. Optional: Activate Auto Load to automatically use the plugin for supported files.
  3. Abaqus element type mapping for writing

    main
    When writing to Abaqus, meshio uses the meshio_to_abaqus_type mapping to translate meshio cell types back to Abaqus TYPE keywords. If translate_cell_names is set to False, the meshio cell type name is used directly as the TYPE value in the *ELEMENT block.
  4. XDMF Data Formats and Storage

    main

    When writing XDMF files, the data_format parameter controls the storage mechanism for the mesh's numerical arrays (points, cells, and attributes):

    FormatDescription
    HDFCreates a companion .h5 file. Data is stored as HDF5 datasets. This is the default and most efficient for large datasets.
    XMLData is embedded directly in the .xdmf file as text.
    BinaryData is written to separate .bin files (e.g., filename.0.bin).

    Note: When using HDF, the HDF5 file path is relative to the XDMF (XML) file location.

  5. Read mesh files in Python

    main

    Use meshio.read() to load a mesh file. The file_format argument is optional if the filename includes a supported extension, as it will be inferred automatically.

    import meshio
    
    mesh = meshio.read("filename.msh")
    # Access mesh data via:
    # mesh.points
    # mesh.cells
    # mesh.cells_dict
    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
        # see meshio-convert -h for all possible formats
    )
    # mesh.points, mesh.cells, mesh.cells_dict, ...
    
    # mesh.vtk.read() is also possible
  6. Write mesh files in Python

    main

    You can write meshes using the meshio.Mesh object or the convenience function meshio.write_points_cells(). You can optionally specify the file_format to enforce specific encoding (e.g., ASCII vs binary).

    Using the Mesh object:

    import meshio
    
    points = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0], [2.0, 0.0], [2.0, 1.0]]
    cells = [
        ("triangle", [[0, 1, 2], [1, 3, 2]]),
        ("quad", [[1, 4, 5, 3]]),
    ]
    
    mesh = meshio.Mesh(
        points,
        cells,
        point_data={"T": [0.3, -1.2, 0.5, 0.7, 0.0, -3.0]},
        cell_data={"a": [[0.1, 0.2], [0.4]]},
    )
    mesh.write("foo.vtk")

    Using write_points_cells:

    import meshio
    
    meshio.write_points_cells("foo.vtk", points, cells)
    import meshio
    
    # two triangles and one quad
    points = [
        [0.0, 0.0],
        [1.0, 0.0],
        [0.0, 1.0],
        [1.0, 1.0],
        [2.0, 0.0],
        [2.0, 1.0],
    ]
    cells = [
        ("triangle", [[0, 1, 2], [1, 3, 2]]),
        ("quad", [[1, 4, 5, 3]]),
    ]
    
    mesh = meshio.Mesh(
        points,
        cells,
        # Optionally provide extra data on points, cells, etc.
        point_data={"T": [0.3, -1.2, 0.5, 0.7, 0.0, -3.0]},
        # Each item in cell data must match the cells array
        cell_data={"a": [[0.1, 0.2], [0.4]]},
    )
    mesh.write(
        "foo.vtk",  # str, os.PathLike, or buffer/open file
        # file_format="vtk",  # optional if first argument is a path; inferred from extension
    )
    
    # Alternative with the same options
    meshio.write_points_cells("foo.vtk", points, cells)
  7. Handle XDMF time series in Python

    main

    The XDMF format supports time series with a shared mesh. Use meshio.xdmf.TimeSeriesWriter to write and meshio.xdmf.TimeSeriesReader to read time-dependent data.

    Writing a time series:

    import meshio
    
    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})

    Reading a time series:

    import meshio
    
    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)
    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)
  8. Use the meshio CLI tool

    main

    The meshio command-line interface allows for quick mesh conversions and inspections without writing Python code.

    Common commands:

    • meshio convert <input> <output>: Convert a mesh between two supported formats.
    • meshio info <input>: Display information about the mesh.
    • meshio compress <input>: Compress a mesh file (e.g., .vtu).
    • meshio decompress <input>: Decompress a mesh file.
    • meshio binary <input>: Convert a mesh to binary format.
    • meshio ascii <input>: Convert a mesh to ASCII format.

    Run meshio-convert -h to see all supported formats.

    meshio convert input.msh output.vtk
  9. Read FLAC3D f3grid files

    main

    Use the read function to load a FLAC3D .f3grid file into a meshio.Mesh object. The function automatically detects whether the file is in binary or ASCII format by inspecting the first few bytes.

    Returns a Mesh object containing points, cells (split into zones and faces), cell data, and cell sets.

  10. Write Abaqus .inp files

    main

    Use the write function to export a meshio.Mesh object to the Abaqus .inp format.

    By default, translate_cell_names is set to True, which maps meshio cell types (like quad or hexahedron) back to their corresponding Abaqus element types (like S4 or C3D8). If you have custom cell types that you want to preserve exactly, set translate_cell_names=False.

  11. Write a Gmsh msh file with write()

    main

    Use write(filename, mesh, ...) to save a mesh object to a Gmsh .msh file.

    Parameters:

    • filename: The path to the output file.
    • mesh: The mesh object to write.
    • fmt_version: The Gmsh format version to use. Supported versions include "2.2", "4.0", and "4.1". Defaults to "4.1".
    • binary: Boolean indicating whether to write in binary mode. Defaults to True.
    • float_fmt: The floating-point format string. Defaults to ".16e" (exponential notation).