Skip to content

Mesh Data Model ​

Mesh ​

A Mesh holds points, a list of cell blocks, point data aligned to the points, cell data as one array per block, field data and named regions

meshioplusplus.Mesh is the central object. All format readers produce one; all writers consume one.

python
class Mesh:
    points: np.ndarray            # shape (num_points, dim), float
    cells: list[CellBlock]
    point_data: dict[str, np.ndarray]         # shape (num_points, ...)
    cell_data: dict[str, list[np.ndarray]]    # one array per CellBlock
    field_data: dict[str, np.ndarray]         # scalar metadata (e.g. material ids)
    regions: list[Region]                     # named groups (see /regions)
    point_sets: dict[str, np.ndarray]         # compat view: the point regions
    cell_sets: dict[str, list[np.ndarray]]    # compat view: the cell regions
    gmsh_periodic: list | None                # Gmsh periodic section data
    info: any                                 # format-specific extra data

Constructor signature:

python
meshioplusplus.Mesh(
    points,
    cells,                    # list of CellBlock or (type, data) tuples, or a dict
    point_data=None,
    cell_data=None,
    field_data=None,
    point_sets=None,
    cell_sets=None,
    gmsh_periodic=None,
    info=None,
)

cells can also be passed as a dict {"triangle": array, ...} for backward compatibility; it is converted to a list internally.

CellBlock ​

Represents a homogeneous group of cells, all of the same element type.

python
class CellBlock:
    type: str               # meshio++ cell type name, e.g. "triangle", "tetra10"
    data: np.ndarray        # shape (num_cells, nodes_per_cell), int indices into points
    tags: list[str]         # optional labels
    dim: int                # topological dimension (0–3)

For polyhedron* types, data is a list of lists (variable number of faces per cell); it is not converted to a numpy array. A polygon* block whose polygons do not all have the same node count is a list of 1-D arrays, one per polygon; with a common count it is a 2-D array.

A ragged block read through the C++ core is a meshioplusplus._ragged.CsrRows, a list subclass whose rows are views of one flat int64 buffer: data.csr() returns that buffer and its offsets ((flat, offsets) for polygons, (flat, offsets, face_offsets) for polyhedra, row r being flat[offsets[r]:offsets[r + 1]]), and the core takes the block back with one copy of each instead of visiting every row. Writing into a row writes into the buffer; any change to the list itself (assigning, appending, deleting, sorting, or changing one polyhedron's face list) drops the arrays, csr() returns None, and the block is read row by row like any other list.

points ​

mesh.points is a 2-D numpy float array of shape (N, d) where d is 2 or 3. Points are always written as returned by readers. Some writers (e.g. VTK) require points to be a C-contiguous array; call np.ascontiguousarray(mesh.points) if converting between formats programmatically.

cell_data ​

cell_data maps a data name to a list of numpy arrays — one array per CellBlock in mesh.cells, in the same order:

python
mesh.cell_data = {
    "material": [
        np.array([1, 1]),   # data for cells block 0 (triangles)
        np.array([2]),      # data for cells block 1 (quad)
    ]
}

Symmetric tensors ​

A symmetric tensor stored as six components per point or cell uses the order xx yy zz xy yz zx, the order of VTK's six-component arrays and of CalculiX's STRESS/TOSTRAIN blocks. Readers keep the order the file uses rather than reordering it; the CalculiX reader is the one that documents it, and the ones that leave a six-component array untouched (GiD's Sxx Syy Szz Sxy Syz Sxz, XDMF's Tensor6) name their own.

Complex values ​

Complex-valued fields are stored as matched pairs of real-valued arrays named <base>_real and <base>_imag (the convention shared by UNV, Nastran OP2, Nastran HDF5 and EnSight). Writers for formats with native complex fields pair matching <base>_real and <base>_imag arrays into one complex field on output; transform(..., rotate_vector_data=True) rotates both halves together, interpolate(..., arrays=["u"]) expands a complex base name to both arrays, data_calc evaluates abs(u) (hypot(u_real, u_imag)) and phase(u) (atan2(u_imag, u_real)), and data_info annotates matched pairs with complex_pair and complex_part. In Python, complex_pairs(mesh) lists complex base names, to_complex(mesh, name) combines a pair into a complex64/complex128 NumPy array, and from_complex(mesh, name, array) stores a complex array as <name>_real and <name>_imag.

Structured grids ​

Structured readers (vti, vtr, vts, legacy structured VTK, VTKHDF ImageData, CGNS structured zones and PLOT3D) annotate each expanded structured block with field_data["meshio:structured:<block>"] = [ni, nj, nk] (int64 point counts, i fastest). Structured writers check this annotation before falling back to geometric uniform-lattice detection, allowing graded rectilinear grids (.vtr) and curvilinear structured grids (.vts, PLOT3D) to round-trip without re-deriving topology. Operations that modify geometry or topology (such as smooth, and any non-data/non-rigid pipeline step) strip meshio:structured:* so deformed or unstructured meshes are explicitly refused by structured writers rather than silently miswritten. In Python, to_grid(mesh) and from_grid(grid_array) bridge regular-lattice Mesh objects and GridArray.

Strings and booleans ​

String field_data entries cross the C++ core without a dedicated string DType: a scalar UTF-8 string name is stored as a 1-D uint8 byte array under field_data[name] paired with a 1-element int64 array field_data["<name>:offsets"] = [len], and a list of k strings is stored as concatenated UTF-8 bytes paired with int64 CSR offsets of length k + 1. Boolean arrays in point_data, cell_data or field_data are stored as uint8 (0/1) with a companion marker field_data["meshio:bool:<loc>:<name>"] = [1] (loc in point, cell, field). The Python conversion encodes and decodes both transparently (hiding the companion keys), and the C API exposes mio_mesh_set_field_string and mio_mesh_get_field_string.

Convenience properties ​

PropertyReturns
mesh.cells_dictdict[str, np.ndarray] — arrays concatenated across blocks of same type
mesh.cell_data_dictdict[str, dict[str, np.ndarray]] — same concatenation for cell data
mesh.cell_sets_dictdict[str, dict[str, np.ndarray]] — cell-set indices resolved per type

meshioplusplus.topological_dimension is the module-level dict backing the "dimension" every operation that groups cells by dimension (decimate, gradient, partition's dual, normals, the SDF builder…) looks a cell type up in: {"vertex": 0, "line": 1, "triangle": 2, "tetra": 3, ...}, -1 for a type it does not carry.

Convenience methods ​

python
# Get all cells of a given type as one array
arr = mesh.get_cells_type("triangle")           # shape (N, 3)

# Get cell data for a specific cell type
arr = mesh.get_cell_data("material", "triangle")

# Copy
mesh2 = mesh.copy()

# Read / write (equivalent to meshioplusplus.read / meshioplusplus.write)
mesh = meshioplusplus.Mesh.read("file.msh")   # deprecated; use meshioplusplus.read()
mesh.write("out.vtk")

Named regions ​

Since v8.1.0 the named groups live in mesh.regions as Region objects, which the C++ core carries, every binding surface sees, and every operation either remaps or explicitly drops. point_sets and cell_sets are unchanged compat views over them, so existing code keeps working; a third kind, side sets (groups of cell facets), has no view equivalent and is reachable only through mesh.regions. See Named regions.

Converting sets ↔ data ​

Some formats only support sets (named groups), others only support integer arrays. meshio++ provides conversion helpers:

python
# Flatten point/cell sets into integer-valued data arrays
mesh.point_sets_to_data()           # adds data key joined from set names
mesh.cell_sets_to_data("groups")    # custom key name

# Split an integer data array back into named sets
mesh.point_data_to_sets("groups")
mesh.cell_data_to_sets("material")

The CLI meshioplusplus convert exposes --sets-to-int-data and --int-data-to-sets for the same operations.

gmsh_periodic ​

Only populated when reading Gmsh files that contain a $Periodic section. The value is a list of periodic link entries, each of the form:

python
[edim, (slave_tag, master_tag), affine_transform_or_None, [[slave_node, master_node], ...]]

Roundtrips back into a Gmsh file correctly when passed through meshioplusplus.gmsh.write.

Threads ​

A Python Mesh is a plain Python object: the binding converts it into a C++ mesh of its own for each call, so calls running in several threads never share one (threads and the GIL). For C++ code that shares a meshioplusplus::Mesh between threads, the contract is that of a standard container: any number of threads may read a const Mesh at once, and a thread that changes it needs it to itself.

With the default MESHIO backend that holds for every accessor, the sorted data names included: PointDataNames(), CellDataNames() and FieldDataNames() sort the names on each call and keep nothing, so a direct write to the public data maps shows at once (InvalidateNameCaches() is kept for existing callers and does nothing). The NATIVE and KRATOS backends build some caches on the first const call (the sorted names of detail::NamedItems, NATIVE's GlobalConnectivity(), KRATOS's GetModelPart()), so with them make that first call before the threads start. A C API handle is not thread-safe, though distinct handles may be used from distinct threads (C API).

Released under the MIT License.