Encountering Errors when Converting MSH File to XDMF

Hello everyone, I hope you’re doing well. I have imported my mesh as an msh file from gmsh, but unfortunately, I encountered an issue when attempting to convert it to xdmf. I would greatly appreciate any assistance with resolving the following error:

<meshio mesh object>
  Number of points: 188
  Number of cells:
    line: 188
  Cell data: gmsh:physical, gmsh:geometrical
  Field data: inlet, outlet, walls, obstacle_bounadry
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[12], line 19
     16 line_mesh = create_mesh(mesh_from_file, "line", prune_z=True)
     17 meshio.write("facet_mesh.xdmf", line_mesh)
---> 19 triangle_mesh = create_mesh(mesh_from_file, "triangle", prune_z=True)
     20 meshio.write("mesh.xdmf", triangle_mesh)

Cell In[12], line 10
      8 def create_mesh(mesh, cell_type, prune_z=False):
      9     cells = mesh.get_cells_type(cell_type)
---> 10     cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
     11     points = mesh.points[:, :2] if prune_z else mesh.points
     12     out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={
     13                            "name_to_read": [cell_data]})

File ~/micromamba/envs/fenicsx/lib/python3.12/site-packages/meshio/_mesh.py:249, in Mesh.get_cell_data(self, name, cell_type)
    248 def get_cell_data(self, name: str, cell_type: str):
--> 249     return np.concatenate(
    250         [d for c, d in zip(self.cells, self.cell_data[name]) if c.type == cell_type]
    251     )

ValueError: need at least one array to concatenate

my code is

import meshio
import numpy
import gmsh
import pygmsh
mesh_from_file = meshio.read("msh_file.msh")
print(mesh_from_file)

def create_mesh(mesh, cell_type, prune_z=False):
    cells = mesh.get_cells_type(cell_type)
    cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
    points = mesh.points[:, :2] if prune_z else mesh.points
    out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={
                           "name_to_read": [cell_data]})
    return out_mesh

line_mesh = create_mesh(mesh_from_file, "line", prune_z=True)
meshio.write("facet_mesh.xdmf", line_mesh)

triangle_mesh = create_mesh(mesh_from_file, "triangle", prune_z=True)
meshio.write("mesh.xdmf", triangle_mesh)

the msh file is :

$MeshFormat
2.2 0 8
$EndMeshFormat
$PhysicalNames
4
1 8 "inlet"
1 9 "outlet"
1 10 "walls"
1 11 "obstacle_bounadry"
$EndPhysicalNames
$Nodes
188
1 0 0 0
2 2.2 0 0
3 2.2 0.41 0
4 0 0.41 0
5 0.2 0.15 0
6 0.2 0.25 0
7 0.02972972972967379 0 0
8 0.05945945945934514 0 0
9 0.0891891891890156 0 0
10 0.1189189189186819 0 0
11 0.1486486486483589 0 0
12 0.1783783783780467 0 0
13 0.2081081081077304 0 0
14 0.2378378378374154 0 0
15 0.267567567567102 0 0
16 0.2972972972967651 0 0
17 0.3270270270264077 0 0
18 0.3567567567560318 0 0
19 0.386486486485672 0 0
20 0.4162162162153148 0 0
21 0.4459459459449577 0 0
22 0.4756756756745932 0 0
23 0.5054054054042216 0 0
24 0.5351351351338616 0 0
25 0.5648648648634671 0 0
26 0.5945945945931069 0 0
27 0.6243243243227496 0 0
28 0.6540540540523925 0 0
29 0.6837837837820353 0 0
30 0.7135135135116782 0 0
31 0.7432432432413211 0 0
32 0.772972972970964 0 0
33 0.8027027027006068 0 0
34 0.8324324324302186 0 0
35 0.8621621621598492 0 0
36 0.891891891889492 0 0
37 0.9216216216191349 0 0
38 0.9513513513487777 0 0
39 0.9810810810784205 0 0
40 1.010810810808081 0 0
41 1.040540540537728 0 0
42 1.070270270267414 0 0
43 1.099999999997057 0 0
44 1.12972972972685 0 0
45 1.159459459456663 0 0
46 1.189189189186479 0 0
47 1.218918918916272 0 0
48 1.248648648646093 0 0
49 1.278378378375901 0 0
50 1.308108108105694 0 0
51 1.337837837835523 0 0
52 1.367567567565323 0 0
53 1.397297297295116 0 0
54 1.427027027024952 0 0
55 1.456756756754745 0 0
56 1.486486486484538 0 0
57 1.516216216214374 0 0
58 1.545945945944167 0 0
59 1.57567567567396 0 0
60 1.605405405403796 0 0
61 1.635135135133589 0 0
62 1.664864864863382 0 0
63 1.694594594593175 0 0
64 1.724324324323011 0 0
65 1.754054054052804 0 0
66 1.783783783782597 0 0
67 1.813513513512433 0 0
68 1.843243243242226 0 0
69 1.872972972972019 0 0
70 1.902702702701855 0 0
71 1.932432432431648 0 0
72 1.962162162161447 0 0
73 1.991891891891276 0 0
74 2.021621621620999 0 0
75 2.051351351350747 0 0
76 2.081081081080702 0 0
77 2.110810810810499 0 0
78 2.140540540540264 0 0
79 2.170270270270219 0 0
80 2.2 0.02928571428565775 0
81 2.2 0.05857142857131794 0
82 2.2 0.08785714285694929 0
83 2.2 0.1171428571425731 0
84 2.2 0.1464285714282011 0
85 2.2 0.1757142857138497 0
86 2.2 0.2049999999994743 0
87 2.2 0.2342857142852724 0
88 2.2 0.2635714285710462 0
89 2.2 0.2928571428568132 0
90 2.2 0.3221428571426234 0
91 2.2 0.3514285714283903 0
92 2.2 0.3807142857142005 0
93 2.170270270270284 0.41 0
94 2.140540540540569 0.41 0
95 2.110810810810853 0.41 0
96 2.081081081081217 0.41 0
97 2.051351351351595 0.41 0
98 2.021621621621879 0.41 0
99 1.991891891892164 0.41 0
100 1.962162162162448 0.41 0
101 1.932432432432757 0.41 0
102 1.902702702703116 0.41 0
103 1.872972972973475 0.41 0
104 1.843243243243792 0.41 0
105 1.813513513514151 0.41 0
106 1.783783783784501 0.41 0
107 1.754054054054827 0.41 0
108 1.724324324325186 0.41 0
109 1.694594594595528 0.41 0
110 1.664864864865873 0.41 0
111 1.635135135136383 0.41 0
112 1.605405405406728 0.41 0
113 1.575675675677084 0.41 0
114 1.545945945947516 0.41 0
115 1.516216216217841 0.41 0
116 1.486486486488213 0.41 0
117 1.456756756758637 0.41 0
118 1.427027027028954 0.41 0
119 1.397297297299343 0.41 0
120 1.367567567569759 0.41 0
121 1.337837837840074 0.41 0
122 1.308108108110472 0.41 0
123 1.278378378380809 0.41 0
124 1.248648648651109 0.41 0
125 1.218918918921512 0.41 0
126 1.189189189191836 0.41 0
127 1.159459459462187 0.41 0
128 1.129729729732503 0.41 0
129 1.100000000002862 0.41 0
130 1.070270270273072 0.41 0
131 1.040540540543261 0.41 0
132 1.010810810813447 0.41 0
133 0.9810810810836565 0.41 0
134 0.9513513513538374 0.41 0
135 0.9216216216240316 0.41 0
136 0.8918918918942409 0.41 0
137 0.862162162164414 0.41 0
138 0.8324324324346164 0.41 0
139 0.8027027027048257 0.41 0
140 0.7729729729749915 0.41 0
141 0.7432432432452007 0.41 0
142 0.7135135135154103 0.41 0
143 0.6837837837855762 0.41 0
144 0.6540540540557855 0.41 0
145 0.6243243243259948 0.41 0
146 0.5945945945961608 0.41 0
147 0.5648648648663699 0.41 0
148 0.5351351351365794 0.41 0
149 0.5054054054067887 0.41 0
150 0.4756756756769547 0.41 0
151 0.445945945947164 0.41 0
152 0.4162162162173733 0.41 0
153 0.3864864864875392 0.41 0
154 0.3567567567577485 0.41 0
155 0.3270270270279578 0.41 0
156 0.2972972972981238 0.41 0
157 0.2675675675683331 0.41 0
158 0.237837837838536 0.41 0
159 0.2081081081087088 0.41 0
160 0.1783783783789885 0.41 0
161 0.148648648649242 0.41 0
162 0.1189189189192899 0.41 0
163 0.08918918918949448 0.41 0
164 0.05945945945973152 0.41 0
165 0.02972972972977939 0.41 0
166 0 0.3807142857143437 0
167 0 0.3514285714286952 0
168 0 0.3221428571430699 0
169 0 0.29285714285744 0
170 0 0.2635714285718054 0
171 0 0.2342857142861561 0
172 0 0.2050000000005308 0
173 0 0.175714285714732 0
174 0 0.1464285714289574 0
175 0 0.1171428571431897 0
176 0 0.08785714285737878 0
177 0 0.05857142857161107 0
178 0 0.02928571428580012 0
179 0.2250000000564932 0.1566987298433944 0
180 0.2433012702567949 0.1750000001170398 0
181 0.25 0.2000000002223989 0
182 0.2433012701106123 0.2250000001361559 0
183 0.2249999999271746 0.2433012702312677 0
184 0.174999999945906 0.2433012701579907 0
185 0.1566987297354793 0.2249999998695787 0
186 0.15 0.1999999997648275 0
187 0.1566987298981987 0.174999999848583 0
188 0.1750000000525014 0.1566987297804664 0
$EndNodes
$Elements
188
1 1 2 10 1 1 7
2 1 2 10 1 7 8
3 1 2 10 1 8 9
4 1 2 10 1 9 10
5 1 2 10 1 10 11
6 1 2 10 1 11 12
7 1 2 10 1 12 13
8 1 2 10 1 13 14
9 1 2 10 1 14 15
10 1 2 10 1 15 16
11 1 2 10 1 16 17
12 1 2 10 1 17 18
13 1 2 10 1 18 19
14 1 2 10 1 19 20
15 1 2 10 1 20 21
16 1 2 10 1 21 22
17 1 2 10 1 22 23
18 1 2 10 1 23 24
19 1 2 10 1 24 25
20 1 2 10 1 25 26
21 1 2 10 1 26 27
22 1 2 10 1 27 28
23 1 2 10 1 28 29
24 1 2 10 1 29 30
25 1 2 10 1 30 31
26 1 2 10 1 31 32
27 1 2 10 1 32 33
28 1 2 10 1 33 34
29 1 2 10 1 34 35
30 1 2 10 1 35 36
31 1 2 10 1 36 37
32 1 2 10 1 37 38
33 1 2 10 1 38 39
34 1 2 10 1 39 40
35 1 2 10 1 40 41
36 1 2 10 1 41 42
37 1 2 10 1 42 43
38 1 2 10 1 43 44
39 1 2 10 1 44 45
40 1 2 10 1 45 46
41 1 2 10 1 46 47
42 1 2 10 1 47 48
43 1 2 10 1 48 49
44 1 2 10 1 49 50
45 1 2 10 1 50 51
46 1 2 10 1 51 52
47 1 2 10 1 52 53
48 1 2 10 1 53 54
49 1 2 10 1 54 55
50 1 2 10 1 55 56
51 1 2 10 1 56 57
52 1 2 10 1 57 58
53 1 2 10 1 58 59
54 1 2 10 1 59 60
55 1 2 10 1 60 61
56 1 2 10 1 61 62
57 1 2 10 1 62 63
58 1 2 10 1 63 64
59 1 2 10 1 64 65
60 1 2 10 1 65 66
61 1 2 10 1 66 67
62 1 2 10 1 67 68
63 1 2 10 1 68 69
64 1 2 10 1 69 70
65 1 2 10 1 70 71
66 1 2 10 1 71 72
67 1 2 10 1 72 73
68 1 2 10 1 73 74
69 1 2 10 1 74 75
70 1 2 10 1 75 76
71 1 2 10 1 76 77
72 1 2 10 1 77 78
73 1 2 10 1 78 79
74 1 2 10 1 79 2
75 1 2 9 3 2 80
76 1 2 9 3 80 81
77 1 2 9 3 81 82
78 1 2 9 3 82 83
79 1 2 9 3 83 84
80 1 2 9 3 84 85
81 1 2 9 3 85 86
82 1 2 9 3 86 87
83 1 2 9 3 87 88
84 1 2 9 3 88 89
85 1 2 9 3 89 90
86 1 2 9 3 90 91
87 1 2 9 3 91 92
88 1 2 9 3 92 3
89 1 2 10 4 3 93
90 1 2 10 4 93 94
91 1 2 10 4 94 95
92 1 2 10 4 95 96
93 1 2 10 4 96 97
94 1 2 10 4 97 98
95 1 2 10 4 98 99
96 1 2 10 4 99 100
97 1 2 10 4 100 101
98 1 2 10 4 101 102
99 1 2 10 4 102 103
100 1 2 10 4 103 104
101 1 2 10 4 104 105
102 1 2 10 4 105 106
103 1 2 10 4 106 107
104 1 2 10 4 107 108
105 1 2 10 4 108 109
106 1 2 10 4 109 110
107 1 2 10 4 110 111
108 1 2 10 4 111 112
109 1 2 10 4 112 113
110 1 2 10 4 113 114
111 1 2 10 4 114 115
112 1 2 10 4 115 116
113 1 2 10 4 116 117
114 1 2 10 4 117 118
115 1 2 10 4 118 119
116 1 2 10 4 119 120
117 1 2 10 4 120 121
118 1 2 10 4 121 122
119 1 2 10 4 122 123
120 1 2 10 4 123 124
121 1 2 10 4 124 125
122 1 2 10 4 125 126
123 1 2 10 4 126 127
124 1 2 10 4 127 128
125 1 2 10 4 128 129
126 1 2 10 4 129 130
127 1 2 10 4 130 131
128 1 2 10 4 131 132
129 1 2 10 4 132 133
130 1 2 10 4 133 134
131 1 2 10 4 134 135
132 1 2 10 4 135 136
133 1 2 10 4 136 137
134 1 2 10 4 137 138
135 1 2 10 4 138 139
136 1 2 10 4 139 140
137 1 2 10 4 140 141
138 1 2 10 4 141 142
139 1 2 10 4 142 143
140 1 2 10 4 143 144
141 1 2 10 4 144 145
142 1 2 10 4 145 146
143 1 2 10 4 146 147
144 1 2 10 4 147 148
145 1 2 10 4 148 149
146 1 2 10 4 149 150
147 1 2 10 4 150 151
148 1 2 10 4 151 152
149 1 2 10 4 152 153
150 1 2 10 4 153 154
151 1 2 10 4 154 155
152 1 2 10 4 155 156
153 1 2 10 4 156 157
154 1 2 10 4 157 158
155 1 2 10 4 158 159
156 1 2 10 4 159 160
157 1 2 10 4 160 161
158 1 2 10 4 161 162
159 1 2 10 4 162 163
160 1 2 10 4 163 164
161 1 2 10 4 164 165
162 1 2 10 4 165 4
163 1 2 8 5 4 166
164 1 2 8 5 166 167
165 1 2 8 5 167 168
166 1 2 8 5 168 169
167 1 2 8 5 169 170
168 1 2 8 5 170 171
169 1 2 8 5 171 172
170 1 2 8 5 172 173
171 1 2 8 5 173 174
172 1 2 8 5 174 175
173 1 2 8 5 175 176
174 1 2 8 5 176 177
175 1 2 8 5 177 178
176 1 2 8 5 178 1
177 1 2 11 6 5 179
178 1 2 11 6 179 180
179 1 2 11 6 180 181
180 1 2 11 6 181 182
181 1 2 11 6 182 183
182 1 2 11 6 183 6
183 1 2 11 7 6 184
184 1 2 11 7 184 185
185 1 2 11 7 185 186
186 1 2 11 7 186 187
187 1 2 11 7 187 188
188 1 2 11 7 188 5
$EndElements

1 Like

Your mesh is surely wrong. If you look at the elements section

$Elements
188
...
2 [here]1[/here] 2 10 1 7 8

the number that I highlighted with here denotes the cell type, and 1 is a segment. If you scroll down the file, all elements I can see are segments, and therefore there are no triangles, as the error says.

1 Like

ok, i understand. Thank you so much for your help.

Think you, the mesh issue has been resolved. I have an additional question: How can I extract the boundary conditions from my XDMF file? I need to use them as parameters in the Dirichlet function. For example:

facets = locate_entities_boundary(msh, 1, inlet)
dofs = locate_dofs_topological(V, 1, facets)
 bc0 = dirichletbc(u_in, dofs,V)

Think you in advance, have a good day.

<meshio mesh object>
  Number of points: 1332
  Number of cells:
    vertex: 7
    line: 189
    triangle: 2474
  Cell data: gmsh:physical, gmsh:geometrical
  Field data: inlet, outlet, walls, obstacle_bounadry

What do you want to extract? Do you want to extract the dof indices or some values to put in u_in. Your snippet above doesn’t explain what doesn’t work or you and what you want to achieve.

1 Like

I want to recuperate the boundaries from my XDMF mesh. and i don’t know how.

There are many examples of reading in boundary conditions from file across the forum.
See for instance: Want to import already defined physical groups of Microstructure in Gmsh to FEnics - #23 by dokken
Defining subdomains for different materials — FEniCSx tutorial

1 Like