Introduction
Version 4 is the current form of Gmsh’s mesh format. It stores the model’s entities with their boundaries, and groups nodes and elements into one block per entity. There are two revisions, 4.0 and 4.1. Version 4.1 is specified in the Gmsh reference manual; this page follows that text and the reader and writer in Gmsh 4.15.2. The 4.0 differences follow the source of Gmsh 4.0.7.
The layouts below are for 4.1 unless a section says otherwise.
Structure
The file is a series of sections, each from a $Name line to a $EndName
line. Only $MeshFormat is required. A simple mesh needs $Nodes and
$Elements too, with nodes before the elements that use them. A section may be repeated,
and result sections may sit in separate files.
| Section | Contents | In 4.0 |
|---|---|---|
$MeshFormat | Version, text or binary, data size | Yes |
$PhysicalNames | Names of physical groups | Yes |
$Entities | Points, curves, surfaces and volumes of the model | Yes |
$PartitionedEntities | Entities of a partitioned mesh | Yes |
$Nodes | Node tags and coordinates | Yes |
$Elements | Element tags and node lists | Yes |
$Periodic | Periodic entity and node pairs | Yes |
$GhostElements | Ghost cells of partitions | Yes |
$Parametrizations | Parametrisations of discrete curves and surfaces | No |
$NodeData, $ElementData, $ElementNodeData | Result data | Yes |
$InterpolationScheme | Interpolation matrices for results | – |
The dash means the 4.0.7 mesh reader has no branch for that section. A section with any other name is
not an error: Gmsh keeps its lines as a model attribute, so comments can go between
$Comments and $EndComments. The option
Mesh.IgnoreUnknownSections makes it skip them instead.
All node, element, entity and physical group tags should be strictly positive; 0 is reserved. Tags need not be continuous or in order.
Text and binary
In a binary file the section lines stay text. Inside the sections, the values below are written as bytes with these types, except where a section is marked as always text.
| Type | Size in 4.1 |
|---|---|
int | 4 bytes, signed |
size_t | data-size bytes, unsigned; used for counts and for node and element tags |
double | 8 bytes, IEEE 754 |
The byte order is that of the machine that wrote the file, and is detected from the integer 1 in the
header. Gmsh 4.15.2 refuses a binary file whose data-size differs from its own
size_t.
$MeshFormat
$MeshFormat
4.1 file-type data-size
(int 1, binary files only)
$EndMeshFormat
version is 4.1 or 4. file-type is 0 for text and 1 for binary. In 4.1,
data-size is the size in bytes of size_t on the writing machine, normally 8.
In a binary file the integer 1 follows as four bytes and then a line break. If it does not read as 1,
every later value must be byte-swapped.
$PhysicalNames
$PhysicalNames
numPhysicalNames
dimension physicalTag "name"
…
$EndPhysicalNames
Always text. The manual limits a name to 127 characters. The layout is the same as in version 2.
$Entities
Optional in 4.1. The counts come first, then all points, curves, surfaces and volumes.
numPoints numCurves numSurfaces numVolumes (size_t)
each point:
pointTag(int) X Y Z (double)
numPhysicalTags(size_t) physicalTag(int) …
each curve, surface or volume:
tag(int) minX minY minZ maxX maxY maxZ (double)
numPhysicalTags(size_t) physicalTag(int) …
numBounding(size_t) boundingTag(int) …
A curve is bounded by points, a surface by curves and a volume by surfaces. The sign of a bounding tag gives its orientation.
$PartitionedEntities
numPartitions(size_t)
numGhostEntities(size_t)
ghostEntityTag(int) partition(int) repeated
numPoints numCurves numSurfaces numVolumes (size_t)
each entity:
tag(int) parentDim(int) parentTag(int)
numPartitions(size_t) partitionTag(int) …
points: X Y Z (double)
others: minX minY minZ maxX maxY maxZ (double)
numPhysicalTags(size_t) physicalTag(int) …
not points: numBounding(size_t) boundingTag(int) …
The parent is the entity of the unpartitioned model that this one came from; Gmsh writes 0 and 0 when there is none.
$Nodes
numEntityBlocks numNodes minNodeTag maxNodeTag (size_t)
each block:
entityDim(int) entityTag(int) parametric(int, 0 or 1)
numNodesInBlock(size_t)
nodeTag(size_t) … all the tags first
x y z (double) [u [v [w]]] then one set per node
Parametric coordinates are present only when parametric is 1: u for
entities of dimension 1 or more, v for dimension 2 or more, and w for
dimension 3. The block layout lets a binary reader load all tags, then all coordinates, in single
reads. If minNodeTag is 1 and maxNodeTag equals numNodes, the
tags are continuous.
$Elements
numEntityBlocks numElements minElementTag maxElementTag (size_t)
each block:
entityDim(int) entityTag(int) elementType(int)
numElementsInBlock(size_t)
elementTag(size_t) nodeTag(size_t) … one per element
All elements in a block have the same type, and the type fixes the number of node tags. See element types for the codes and node ordering.
$Periodic
numPeriodicLinks(size_t)
each link:
entityDim(int) entityTag(int) entityTagMaster(int)
numAffine(size_t) value(double) … 0 or the affine transform
numCorrespondingNodes(size_t)
nodeTag(size_t) nodeTagMaster(size_t) one per pair
When a transform is given it has 16 values.
$GhostElements
numGhostElements(size_t)
each entry:
elementTag(size_t) partitionTag(int)
numGhostPartitions(size_t) ghostPartitionTag(int) …
$Parametrizations
New in 4.1.
numCurveParam(size_t) numSurfaceParam(size_t)
each curve:
curveTag(int) numNodes(size_t)
nodeX nodeY nodeZ nodeU (double) one per node
each surface:
surfaceTag(int) numNodes(size_t) numTriangles(size_t)
nodeX nodeY nodeZ nodeU nodeV
curvMaxX curvMaxY curvMaxZ
curvMinX curvMinY curvMinZ (double) one per node
nodeIndex1 nodeIndex2 nodeIndex3 (int) one per triangle
Result data
$NodeData | $ElementData | $ElementNodeData
numStringTags (text)
"string tag" …
numRealTags (text)
realTag …
numIntegerTags (text)
integerTag …
rows
$End…
| Section | Row |
|---|---|
$NodeData | nodeTag(int) value(double) … |
$ElementData | elementTag(int) value(double) … |
$ElementNodeData | elementTag(int) numNodesPerElement(int) value(double) …, with values for each node |
The tags are always text; only the rows are binary in a binary file. Node and element tags here are
int, not size_t. Gmsh gives these meanings:
| Tag | Meaning |
|---|---|
| String 1 | Name of the view |
| String 2 | Name of the interpolation scheme |
| Real 1 | Time value |
| Integer 1 | Time step, from 0 |
| Integer 2 | Number of components: 1 (scalar), 3 (vector) or 9 (tensor) |
| Integer 3 | Number of nodes or elements listed |
| Integer 4 | Partition of the data, 0 for none |
$InterpolationScheme
$InterpolationScheme
"name"
numElementTopologies
elementTopology
numInterpolationMatrices
numRows numColumns value … values by row
…
$EndInterpolationScheme
Always text. The topology numbers are 1 points, 2 lines, 3 triangles, 4 quadrangles, 5 tetrahedra, 6 pyramids, 7 prisms, 8 hexahedra, 9 polygons and 10 polyhedra. The matrix count and the matrices repeat for each topology; the manual says two matrices should be given.
Version 4.0
The manual lists the changes made in 4.1. Read the other way round, with the 4.0.7 source, a 4.0 file differs like this:
| Part | 4.0 |
|---|---|
$MeshFormat | Version written as 4; data-size is written as the size of a double, 8 |
$Entities | Required. Points have a six-value bounding box like the other entities |
$Nodes header | numEntityBlocks numNodes only |
| Node block | entityTag entityDim parametric numNodes (tag first), then one record per node: nodeTag x y z, followed when parametric by u on a curve or u v on a surface |
$Elements header | numEntityBlocks numElements only |
| Element block | entityTag entityDim elementType numElements (tag first) |
$Periodic | No affine count: an optional line Affine with 16 values, then the node count and pairs |
4.0 binary
Counts that are size_t in 4.1 are unsigned long or int in 4.0.
unsigned long and long take the width of the writing platform’s C type,
and the header does not record it.
| Value | 4.0 type |
|---|---|
| Four entity counts; physical tag and bounding counts | unsigned long |
| Number of partitions, number of ghost entities, partitions per entity | int |
| Partitioned entity record | int tag, parent dimension, parent tag, partition count, then the partition tags and six doubles |
$Nodes and $Elements header counts, and the count in each block | unsigned long |
| Block entity tag, dimension, parametric flag or element type | int |
| Node tag, element tag and the node tags of an element | int |
| Periodic link count, dimension, tags and node pairs | int |
| Periodic node count | long. A negative value (the writer uses −1) means 16 doubles of affine transform follow, then the real count |
| Ghost element count, element tag, partition, number of ghost partitions and their tags | int |
Gmsh 4.15.2 reads version 4.0 only as text. It stops with an error on a binary 4.0 file.
Example
Two quadrangles with a value at each node, from the manual:
$MeshFormat
4.1 0 8
$EndMeshFormat
$Nodes
1 6 1 6
2 1 0 6
1
2
3
4
5
6
0. 0. 0.
1. 0. 0.
1. 1. 0.
0. 1. 0.
2. 0. 0.
2. 1. 0.
$EndNodes
$Elements
1 2 1 2
2 1 3 2
1 1 2 3 4
2 2 5 6 3
$EndElements
$NodeData
1
"My view"
1
0.0
3
0
1
6
1 0.0
2 0.1
3 0.2
4 0.0
5 0.2
6 0.4
$EndNodeData
Divergence in Gmsh
- The writer saves parametric coordinates only for nodes on curves and surfaces; it sets
parametricto 0 for point and volume blocks, although the manual allowsw. - The format has no field for entities embedded in another entity. The 4.15.2 writer adds an embedded curve to its surface’s bounding list under a false tag, above the highest real curve tag, and the text reader takes such a tag, less that highest tag, as an embedded curve.
- The 4.15.2 reader does not byte-swap the
$Parametrizationscounts and tags it reads itself.
Variants
- MSH version 4 – nodes and elements in blocks by entity
- MSH version 3 – entities, and an entity on every node and element
- MSH version 2 – flat node and element lists with integer tags
- MSH version 1 –
$NODand$ELM, text only
Links
Software
- Gmsh – mesh generator that reads and writes version 4 files (Windows, macOS, Linux)
Developers
- Gmsh source – the version 4 reader and writer (C++)
- meshio – library and converter for mesh files, including Gmsh MSH (Python)
Reference
- Gmsh reference manual: MSH file format – the version 4.1 specification
- GModelIO_MSH4.cpp, Gmsh 4.15.2 – the reader and writer
- GModelIO_MSH4.cpp, Gmsh 4.0.7 – the version 4.0 binary layout