Medit / GMF (.mesh, .meshb)
The Medit mesh format, also known as the INRIA libMeshb GMF (Gamma Mesh Format): keyword sections in ASCII (.mesh) or a binary, position-indexed record stream (.meshb). Dispatch is purely by whether the filename ends in "b".
| Format name | medit |
| Extensions | .mesh, .meshb (.mesh is shared with MFEM: a file whose first line names an MFEM mesh is read as mfem) |
| Read / Write | ✓ / ✓ |
| Extra dependencies | — |
Reading & writing
import meshioplusplus
mesh = meshioplusplus.read("mesh.mesh")
meshioplusplus.medit.write("out.mesh", mesh, float_fmt=".16e")float_fmt(default".16e") — coordinate format for the ASCII writer (the binary path's float width is dictated by the file'sMeshVersionFormattedversion, not by this kwarg).sol— on read, a Medit solution to attach: a path, orTruefor<stem>.sol(else<stem>.solb) beside the mesh; its fields becomepoint_data/cell_data["medit:sol:<i>"]. On write,sol=Truealso writes the floating-point point and cell data as<stem>.sol(beside a.mesh) or<stem>.solb(beside a.meshb).
The native ASCII tokenizer reads bounded views over a FileSource, retaining comment skipping, numeric-prefix behavior and version-driven dtypes. Returned arrays are owned; the writer and binary fallback are unchanged. See the text-I/O measurements.
File structure
ASCII (.mesh)
Whitespace/#-comment tokenized, keyword-driven:
MeshVersionFormatted <v> # 0/1 -> float32 coords, 2 -> float64
Dimension <d>
Vertices
<n>
x1 x2 [x3] ref # n rows
Triangles
<n>
v0 v1 v2 ref # n rows, 1-based
...
EndRecognized element keywords: Edges, Triangles, Quadrilaterals, Tetrahedra, Prisms, Pyramids, Hexahedra (and the alternate spelling Hexaedra), and since v17.16.0 the high-order EdgesP2, EdgesP3, TrianglesP2, TrianglesP3 and TetrahedraP2 with their <Keyword>Ordering sections (below). Every element keyword is followed by a count then that many rows of <node ids> <ref>. Normals with NormalAtVertices and Tangents with TangentAtVertices are kept as point_data["medit:normal"] and point_data["medit:tangent"] (below). Several other keywords are recognized but discarded on read: SubDomainFromMesh, VertexOnGeometricVertex/Edge, EdgeOnGeometricEdge, Identifier, Geometry, and the solution keywords SolAtVertices … SolAtPyramids (with a warning: a solution belongs in a .sol beside the mesh); the id lists Corners, RequiredVertices, Ridges, RequiredEdges and RequiredTriangles are kept as regions (see below).
Binary (.meshb, libMeshb GMF)
A leading int32 magic code — 1 for native byte order, or its byte-swap (16777216) to signal the opposite endianness, which flips every subsequent typed read. Then an int32 version (1-4), which fixes the integer/float/ position field widths: v1 → 4-byte int, 4-byte float; v2 → 4-byte int, 8-byte float; v3 → 4-byte int, 8-byte float, 8-byte positions; v4 → 8-byte int, 8-byte float, 8-byte positions. The rest of the file is a sequence of (keyword_code, [position], [count], payload) records, each keyed by a numeric GMF field code (from a large internal table of ~200 known libMeshb field codes — only a handful are meaningful to meshio++: GmfVertices (4), GmfEdges (5), GmfTriangles (6), GmfQuadrilaterals (7), GmfTetrahedra (8), GmfPrisms (9), GmfHexahedra (10), GmfPyramids (49), the id lists GmfCorners … GmfRequiredTriangles (13–17), the high-order GmfTrianglesP2 (24), GmfEdgesP2 (25), GmfTetrahedraP2 (30), GmfTrianglesP3 (90) and GmfEdgesP3 (92) with their orderings (127, 128, 129, 130 and 133), plus GmfMeshVersionFormatted (1), GmfDimension (3) and GmfEnd (54)). Records with any other code are skipped with a warning.
Cell types
| keyword | meshio++ type | nodes |
|---|---|---|
Edges | line | 2 |
Triangles | triangle | 3 |
Quadrilaterals | quad | 4 |
Tetrahedra | tetra | 4 |
Prisms | wedge | 6 |
Pyramids | pyramid | 5 |
Hexahedra / Hexaedra | hexahedron | 8 |
EdgesP2 | line3 | 3 |
EdgesP3 | line4 | 4 |
TrianglesP2 | triangle6 | 6 |
TrianglesP3 | triangle10 | 10 |
TetrahedraP2 | tetra10 | 10 |
A cell type with no keyword (quad8, hexahedron20, a polygon ...) is skipped with a warning by every writer; the C++ ASCII writer skipped it silently before v17.16.0.
High-order elements
libMeshb defines no node order for its high-order elements ("as many HO nodes ordering … as there are programmers"): a file states its own in a <Keyword>Ordering section (EdgesP2Ordering, TrianglesP3Ordering, …), a count equal to the element's nodes followed by one integer tuple per node, and a reader maps it onto its own order by matching tuples (libMeshb's GmfSetHONodesOrdering). meshio++'s tuples are meshio's node order, with these meanings:
- Simplices. The tuple is the barycentric multiplicities, component k counting vertex k.
triangle6is2 0 0,0 2 0,0 0 2,1 1 0,0 1 1,1 0 1.triangle10lists its corners, then each edge's two nodes from the edge's first vertex, then1 1 1.tetra10lists its corners, then the edges 01, 12, 20, 03, 13, 23. - Edges. The tuple is the node's position from the first vertex:
0 2 1forline3,0 3 1 2forline4.
How the engines handle the orderings:
- Writers. Both engines emit the ordering before a type's first section, in ASCII and binary. A mesh with only linear cells is written exactly as before.
- Readers. They apply a file's ordering wherever in the file it comes. A section with no ordering is read in meshio's order, which is the order libMeshb's own example writes
TrianglesP2in. An ordering that is not a permutation of meshio++'s tuples (a tuple listed twice, one of the wrong degree, the wrong node count) is refused rather than guessed at.
Data mapping
point_data["medit:ref"]— the per-vertex trailing reference integer.cell_data["medit:ref"]— the per-element trailing reference integer, one array per cell block.
Quirks & limitations
- A file without a
Dimensionkeyword (FEconv writes none) is read with the dimension its firstVerticesrow implies: the number of values on that row minus the trailing reference. Before v16.6.0 both engines refused such a file. - Only one integer point-data array and one integer cell-data array can be written (Medit's single-
ref-column limitation): if more than one candidate exists, the first is used and the rest silently dropped (a warning is emitted for the dropped ones). - Coordinate dtype is version-driven, not user-selectable in binary mode: ASCII
MeshVersionFormatted0 or 1 →float32, 2 →float64; binary version 1 →float32, versions 2-4 →float64. The binary writer always writesfloat64coordinates, little-endian, and version 3 (32-bit integers); it upgrades to version 4 when an index or reference needs 64 bits. - Binary record headers embed absolute byte offsets (
posfields) that the writer must track precisely while emitting records — a strict requirement for libMeshb compatibility, not merely a convenience field. NormalswithNormalAtVerticesandTangentswithTangentAtVertices(ASCII, both engines) are read intopoint_data["medit:normal"]andpoint_data["medit:tangent"], one row per vertex,NaNwhere the vertex has none, and written back from them as one row per vertex that has one; a pair naming a vertex or a row that does not exist is refused. The binary.meshbpath still skips them (GmfNormals,GmfNormalAtVertices,GmfTangents,GmfTangentAtVertices). The rest are read but discarded.Corners,RequiredVertices(point regionsmedit:Corners,medit:RequiredVertices),RidgesandRequiredEdges(cell regionsmedit:Ridges,medit:RequiredEdgesover the file's Edges) andRequiredTriangles(cell regionmedit:RequiredTrianglesover its Triangles) are kept as regions and written back as those keywords, in the ASCII variant of both engines. The binary.meshbpath carries them as the libMeshb sectionsGmfCorners,GmfRidges,GmfRequiredVertices,GmfRequiredEdgesandGmfRequiredTriangles. A cell region is written relative to the first block of its type, so entries elsewhere are dropped with a warning.- Both variants, ASCII
.meshand binary.meshb(including its little/big-endian variants), are read and written by the C++ core and by the Python twin, which write the same bytes. The binary reader skips the sections it has no home for (solutions, which belong in a.solb, normals ...) through their next-section position, with a warning, and refuses a file whose position is outside it. - Cells are written one section per cell block: in block order by the binary writers and the Python ASCII writer, grouped by keyword (in the order of the table above) by the C++ ASCII writer.
Notes
tests/python/meshes/medit/cube86.mesh(ascii) — 39 points, 72 triangles, 86 tets, boundary tag counts{1:14, 2:14, 3:14, 4:8, 5:14, 6:8}.sphere_mixed.1.meshb(binary) — 3270 points, 864 triangles, 3024 wedges, 9072 tets, tag counts{1:432, 2:216, 3:216}.hch_strct.4.meshb/hch_strct.4.be.meshb(binary, little/big-endian pair, exercising the endian-swap path) — 306 points, 12 triangles, 178 quads, 96 wedges, 144 hexahedra, tag counts{1:15, 2:15, 3:160}.- All four originate from UGRID files converted with UGC (simcenter.msstate.edu).