I am trying to understand the structure of a msh file, in order to be able to extract the nodes coordinates and their connectivity (for use in a plot with pgf/tikz). To do so I decided to make the first tutorial (t1.py) with a coarse mesh. When the file is generated, some info are written in the terminal and I can see the right number of nodes. But for elements, it is not the same. And I also can't understand a lot of things in the msh file.
Below is the script I use (t1 with some modified parameters):
import gmsh
import sys
gmsh.initialize()
gmsh.model.add("t1")
lc = 10
gmsh.model.geo.addPoint(0, 0, 0, lc, 1)
gmsh.model.geo.addPoint(10, 0, 0, lc, 2)
gmsh.model.geo.addPoint(10, 10, 0, lc, 3)
p4 = gmsh.model.geo.addPoint(0, 10, 0, lc)
gmsh.model.geo.addLine(1, 2, 1)
gmsh.model.geo.addLine(3, 2, 2)
gmsh.model.geo.addLine(3, p4, 3)
gmsh.model.geo.addLine(4, 1, p4)
gmsh.model.geo.addCurveLoop([4, 1, -2, 3], 1)
gmsh.model.geo.addPlaneSurface([1], 1)
gmsh.model.geo.synchronize()
gmsh.model.addPhysicalGroup(1, [1, 2, 4], 5)
gmsh.model.addPhysicalGroup(2, [1], name="My surface")
gmsh.model.mesh.generate(2)
gmsh.write("t1.msh")
if '-nopopup' not in sys.argv:
gmsh.fltk.run()
gmsh.finalize()
When I launch the script, the info in the terminal says 5 nodes (ok) and 12 elements (how is that ?). But when I open the file, now gmsh counts 7 elements. But I count 5 nodes, 8 lines, and 4 faces (see mesh.png). So I do not understand what "elements" means.
Then for the msh file. I just would like to know how to decode it. It is written below:
$MeshFormat
4.1 0 8
$EndMeshFormat
$PhysicalNames
1
2 6 "My surface"
$EndPhysicalNames
$Entities
4 4 1 0
1 0 0 0 0
2 10 0 0 0
3 10 10 0 0
4 0 10 0 0
1 0 0 0 10 0 0 1 5 2 1 -2
2 10 0 0 10 10 0 1 5 2 3 -2
3 0 10 0 10 10 0 0 2 3 -4
4 0 0 0 0 10 0 1 5 2 4 -1
1 0 0 0 10 10 0 1 6 4 4 1 -2 3
$EndEntities
$Nodes
8 5 1 5
0 1 0 1
1
0 0 0
0 2 0 1
2
10 0 0
0 3 0 1
3
10 10 0
0 4 0 1
4
0 10 0
1 1 0 0
1 2 0 0
1 4 0 0
2 1 0 1
5
5 5 0
$EndNodes
$Elements
4 7 1 7
1 1 1 1
1 1 2
1 2 1 1
2 3 2
1 4 1 1
3 4 1
2 1 2 4
4 1 2 5
5 4 1 5
6 2 3 5
7 3 4 5
$EndElements
For example, with the Nodes
section. I don't know (except that there are 5 nodes) what the first line 8 5 1 5
means. The second line is a mystery to me also. But then I have 1
, followed by 0 0 0
. As I understand it, those two lines stand for the point number, and its coordinates (x, y, z). So following this logic, every line with 1 or 3 numbers I can understand. But the ones with 4 numbers are not clear for me. Can anyone tell me what they mean ?
For the Elements
part it is worse, only thing I can make logic is the number 4
, I suppose it refers to the number of faces... But I have no clue what the rest is.
For the lines with 4 numbers, I suppose the first number of each line is a label, and then the next three numbers refers to my nodes, as the numbers range from 1 to 5.
Anyway, I just would like to have an explanation about this file. It is not very large so I assume the explanation should be quite easy, but I can't find it.
Look at https://gmsh.info/doc/texinfo/gmsh.html section 10.1 for a thorough explanation. A simple example (taken from this website) is
$MeshFormat
4.1 0 8 MSH4.1, ASCII
$EndMeshFormat
$Nodes
1 6 1 6 1 entity bloc, 6 nodes total, min/max node tags: 1 and 6
2 1 0 6 2D entity (surface) 1, no parametric coordinates, 6 nodes
1 node tag #1
2 node tag #2
3 etc.
4
5
6
0. 0. 0. node #1 coordinates (0., 0., 0.)
1. 0. 0. node #2 coordinates (1., 0., 0.)
1. 1. 0. etc.
0. 1. 0.
2. 0. 0.
2. 1. 0.
$EndNodes
$Elements
1 2 1 2 1 entity bloc, 2 elements total, min/max element tags: 1 and 2
2 1 3 2 2D entity (surface) 1, element type 3 (4-node quad), 2 elements
1 1 2 3 4 quad tag #1, nodes 1 2 3 4
2 2 5 6 3 quad tag #2, nodes 2 5 6 3
$EndElements
$NodeData
1 1 string tag:
"My view" the name of the view ("My view")
1 1 real tag:
0.0 the time value (0.0)
3 3 integer tags:
0 the time step (0; time steps always start at 0)
1 1-component (scalar) field
6 6 associated nodal values
1 0.0 value associated with node #1 (0.0)
2 0.1 value associated with node #2 (0.1)
3 0.2 etc.
4 0.0
5 0.2
6 0.4
$EndNodeData
Hope that helps!