Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio

Didnt it previously work when you included a dimension argument? What have you changed Since two posts ago?

Well, you construct your XDMF-File with meshio with this (your code copied from above):

meshio.write("boundary_markers.xdmf",
             meshio.Mesh(points=msh.points,
             cells={"line": msh.cells["line"]},
             cell_data={"line": {"inlet": msh.cell_data["line"]["gmsh:physical"]}}))

Look at the last line:

cell_data={"line": {"inlet": msh.cell_data["line"]["gmsh:physical"]}}

You “mark” your boundary data with the name inlet.
When reading it back in to dolfin, you need to tell dolfin the name to look for:

infile.read(mvc, 'inlet')

(Simply open your XDMF file in a text editor. The file is XML, only the h5 file is binary. The name to look up is encoded as an attribute at the end of the file.)

I just used the string Boundary as a generic name for the boundary, rather than inlet, which usually describes only one specific boundary.

In the end, you are free to choose whatever name fits your needs.
You just have to be consistent in reading the same name with dolfin, which you wrote with meshio.

One further note:
gmsh always creates three dimensional meshes.
The dimensionality of the mesh written to XDMF with meshio depends on the dimensionality of the points-array.

Since you identify line elements as the boundary, it looks like you use a two dimensional mesh.
In this case, you probably want to strip away the unneeded dimension and create a two dimensional XDMF file:

# read input from infile
inmsh = meshio.read(infile)
# delete third (obj=2) column (axis=1)
# this strips the z-component
outpoints = np.delete(arr=inmsh.points, obj=2, axis=1)

# create (two dimensional!) triangle mesh file
outmsh = meshio.Mesh(points=outpoints,
                     cells={'triangle': inmsh.cells['triangle']},
                     cell_data={'triangle': {'Subdomain': inmsh.cell_data['triangle']['gmsh:physical']}},
                     field_data=inmsh.field_data)
# write mesh to file
meshio.write(filename=outfile_mesh, mesh=outmsh, file_format=outfile_format)

# create (two dimensional!) boundary data file
outboundary = meshio.Mesh(points=outpoints,
                          cells={'line': inmsh.cells['line']},
                          cell_data={'line': {'Boundary': inmsh.cell_data['line']['gmsh:physical']}},
                          field_data=inmsh.field_data)
# write boundary data to file
meshio.write(filename=outfile_boundary, mesh=outboundary, file_format=outfile_format)

You need to adjust the values of infile, outfile and so on to your needs.

The output in the boundary data file contains now markers for every single line in the mesh.
On your boundary, these are the numbers you initially set with the physical region mechanism of gmsh.
On the internal lines however, there are no such numbers in the original input if you didn’t add them to physical regions in gmsh, and so they are given the value -1, - and since the numbers are interpreted as unsigned 64-Bit integers afterwards, the value -1 corresponds to the value 18446744073709551615.

One final note:
When your physical regions in gmsh overlap, that is, if a volume / surface / line / point belongs to more than one physical region, meshio currently ignores all but the first:

4 Likes

Thanks for the explanations. The original snippet I posted doesn’t apply anymore since I adjusted it to the original one by @dokken. Indeed, using the same string from the meshio.Mesh() command eliminates the error.

Mixing numpy in there to eliminate z-0 is pretty useful. The geometry I’m analyzing at the moment is pretty simple, no overlaps and only one physical surface. But this question will arise when I get to the FSI point.

I’m glad the dolfin community switched to meshio, much more powerful/versatile than dolfin-convert :slightly_smiling_face:

Hi,
I have adapted your example, but I am unable to load the boundaries. What am I doing wrong?

The MSH to XDMF conversion is:
import meshio

msh = meshio.read('eighth_of_sphere.msh')

meshio.write("eighth_of_sphere.xdmf",
                     meshio.Mesh(points=msh.points,
                     cells={"tetra": msh.cells["tetra"]}))
meshio.write("eighth_of_sphere_boundaries.xdmf",
                      meshio.Mesh(points=msh.points,
                      cells={"triangle": msh.cells["triangle"]},
                      cell_data={"triangle": {"boundaries": msh.cell_data["triangle"]["gmsh:physical"]}}))

sadly, the loading:

from dolfin import *
mesh = Mesh()
with XDMFFile('eighth_of_sphere.xdmf') as fh:
    fh.read(mesh)
mvc = MeshValueCollection("size_t", mesh, 2)
with XDMFFile('eighth_of_sphere_boundaries.xdmf') as fh:
    fh.read(mvc, 'boundaries')

fails at reading to mvc with:
RuntimeError:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***     fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to find entity in map.
*** Reason:  Error reading MeshValueCollection.
*** Where:   This error was encountered inside HDF5File.cpp.
*** Process: 0
*** 
*** DOLFIN version: 2018.1.0
*** Git changeset:  948dc42cc4e06ed9227d0201ad50f94ac94cbf9f
*** -------------------------------------------------------------------------

What have I missed?

You need to supply the msh file, as your code runs for me with another tetrahedron mesh with boundary markers.

$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
24
1 0.001875917902028013 0.003007247670895905 0
2 0.001864700271398773 0.002907992359777485 0
3 0.001936837659022682 0.002845102384515791 0
4 0.002004706628270152 0.002805918328643685 0
5 0.001992254644027836 0.002971845956242147 0
6 0.00193478521257379 0.003007247670895905 0
7 0.001875917902028013 0.003007247670895905 2.265e-05
8 0.001864700271398773 0.002907992359777485 2.265e-05
9 0.001936837659022682 0.002845102384515791 2.265e-05
10 0.002004706628270152 0.002805918328643685 2.265e-05
11 0.001992254644027836 0.002971845956242147 2.265e-05
12 0.00193478521257379 0.003007247670895905 2.265e-05
13 0.001875917902028013 0.003007247670895905 1.1325e-05
14 0.001864700271398773 0.002907992359777485 1.1325e-05
15 0.001936837659022682 0.002845102384515791 1.1325e-05
16 0.002004706628270152 0.002805918328643685 1.1325e-05
17 0.001992254644027836 0.002971845956242147 1.1325e-05
18 0.00193478521257379 0.003007247670895905 1.1325e-05
19 0.001933366621165924 0.002939047805077183 0
20 0.001966791388121648 0.002890478618619701 0
21 0.001933366621165924 0.002939047805077183 2.265e-05
22 0.001966791388121648 0.002890478618619701 2.265e-05
23 0.001933366621165924 0.002939047805077183 1.1325e-05
24 0.001966791388121648 0.002890478618619701 1.1325e-05
$EndNodes
$Elements
64
1 2 2 6 127 1 19 6
2 2 2 6 127 1 2 19
3 2 2 6 127 3 4 20
4 2 2 6 127 3 20 19
5 2 2 6 127 2 3 19
6 2 2 6 127 5 19 20
7 2 2 6 127 5 6 19
8 2 2 6 127 4 5 20
9 2 2 3 134 7 21 12
10 2 2 3 134 7 8 21
11 2 2 3 134 9 10 22
12 2 2 3 134 9 22 21
13 2 2 3 134 8 9 21
14 2 2 3 134 11 21 22
15 2 2 3 134 11 12 21
16 2 2 3 134 10 11 22
17 4 2 5 1 6 1 19 23
18 4 2 5 1 13 18 23 1
19 4 2 5 1 1 6 18 23
20 4 2 5 1 18 13 23 21
21 4 2 5 1 7 12 21 18
22 4 2 5 1 7 18 21 13
23 4 2 5 1 19 1 2 23
24 4 2 5 1 13 23 14 2
25 4 2 5 1 13 1 23 2
26 4 2 5 1 23 13 14 21
27 4 2 5 1 7 21 8 14
28 4 2 5 1 7 13 21 14
29 4 2 5 1 20 3 4 24
30 4 2 5 1 15 24 16 3
31 4 2 5 1 3 4 24 16
32 4 2 5 1 24 15 16 22
33 4 2 5 1 9 22 10 16
34 4 2 5 1 9 15 22 16
35 4 2 5 1 19 3 20 23
36 4 2 5 1 15 23 24 3
37 4 2 5 1 3 20 23 24
38 4 2 5 1 23 15 24 21
39 4 2 5 1 9 21 22 15
40 4 2 5 1 15 24 21 22
41 4 2 5 1 19 2 3 23
42 4 2 5 1 14 23 15 2
43 4 2 5 1 2 3 23 15
44 4 2 5 1 23 14 15 21
45 4 2 5 1 8 21 9 14
46 4 2 5 1 14 15 21 9
47 4 2 5 1 20 5 19 23
48 4 2 5 1 17 24 23 5
49 4 2 5 1 5 20 24 23
50 4 2 5 1 24 17 23 21
51 4 2 5 1 11 22 21 17
52 4 2 5 1 17 24 22 21
53 4 2 5 1 19 5 6 23
54 4 2 5 1 17 23 18 5
55 4 2 5 1 5 6 23 18
56 4 2 5 1 23 17 18 21
57 4 2 5 1 11 21 12 17
58 4 2 5 1 17 18 21 12
59 4 2 5 1 20 4 5 24
60 4 2 5 1 16 24 17 4
61 4 2 5 1 4 5 24 17
62 4 2 5 1 24 16 17 22
63 4 2 5 1 10 22 11 16
64 4 2 5 1 16 17 22 11
$EndElements
1 Like

Indeed, it was a mesh problem. Thanks!

If you are interested, the troublemaker is linked here: https://bitbucket.org/fenics-project/dolfin/issues/1094/dolfin-convert-crashes-with-keyerror

Im more interested in the GEO file that made this msh file. Is there a particular reason for it generating physical surfaces not present in the mesh?

The GEO was something like (I have already fixed it, so needed to unfix):

Mesh.Algorithm = 5;

Function EighthOfSphere 
  p0 = newp; Point(p0) = {x,  y,  z,  central_element_size} ;
  p1 = newp; Point(p1) = {x+surface_r,y,  z,  surface_element_size} ;
  p2 = newp; Point(p2) = {x,  y+surface_r,z,  surface_element_size} ;
  p3 = newp; Point(p3) = {x,  y,  z+surface_r,surface_element_size} ;

  p4 = newp; Point(p4) = {x+middle_r,y,  z,  middle_element_size} ;
  p5 = newp; Point(p5) = {x,  y+middle_r,z,  middle_element_size} ;
  p6 = newp; Point(p6) = {x,  y,  z+middle_r,middle_element_size} ;

  arc301 = newl; Circle(arc301) = {p3,p0,p1};
  arc102 = newl; Circle(arc102) = {p1,p0,p2};
  arc203 = newl; Circle(arc203) = {p2,p0,p3};

  arc604 = newl; Circle(arc604) = {p6,p0,p4};
  arc405 = newl; Circle(arc405) = {p4,p0,p5};
  arc506 = newl; Circle(arc506) = {p5,p0,p6};

  line04 = newl; Line(line04) = {p0, p4};
  line05 = newl; Line(line05) = {p0, p5};
  line06 = newl; Line(line06) = {p0, p6};

  line41 = newl; Line(line41) = {p4, p1};
  line52 = newl; Line(line52) = {p5, p2};
  line63 = newl; Line(line63) = {p6, p3};

  loop321 = newll; Line Loop(loop321) = {-arc102,-arc203,-arc301};
  loop654 = newll; Line Loop(loop654) = {-arc405,-arc506,-arc604};

  loop012 = newll; Line Loop(loop012) = {line04,line41,arc102,-line52,-line05};
  loop023 = newll; Line Loop(loop023) = {line05,line52,arc203,-line63,-line06};
  loop031 = newll; Line Loop(loop031) = {line06,line63,arc301,-line41,-line04};

  loop045 = newll; Line Loop(loop045) = {line04,arc405,-line05};
  loop056 = newll; Line Loop(loop056) = {line05,arc506,-line06};
  loop064 = newll; Line Loop(loop064) = {line06,arc604,-line04};

  loop4125 = newll; Line Loop(loop4125) = {line41,arc102,-line52,-arc405};
  loop5236 = newll; Line Loop(loop5236) = {line52,arc203,-line63,-arc506};
  loop6314 = newll; Line Loop(loop6314) = {line63,arc301,-line41,-arc604};

  surface321 = news;  Surface(surface321) = {loop321};
  surface654 = news;  Surface(surface654) = {loop654};
  surface012 = news;  Plane Surface(surface012) = {loop012};
  surface023 = news;  Plane Surface(surface023) = {loop023};
  surface031 = news;  Plane Surface(surface031) = {loop031};

  surface045 = news;  Plane Surface(surface045) = {loop045};
  surface056 = news;  Plane Surface(surface056) = {loop056};
  surface064 = news;  Plane Surface(surface064) = {loop064};

  surface4125 = news;  Plane Surface(surface4125) = {loop4125};
  surface5236 = news;  Plane Surface(surface5236) = {loop5236};
  surface6314 = news;  Plane Surface(surface6314) = {loop6314};

  //all_loops = newsl; Surface Loop(all_loops) = {surface321,surface012,surface023,surface031};
  central_loops = newsl; Surface Loop(central_loops) = {surface654,surface045,surface056,surface064};
  external_loops = newsl; Surface Loop(external_loops) = {-surface654,surface4125,surface5236,surface6314,surface321};
Return
central_element_size = 0.0625;
middle_element_size = 0.0625;
surface_element_size = 1.0;
x = 0.; y= 0.; z=0.;
surface_r = 55.5;
// surface_r = sqrt(3) * 32 = 55.42562584220407
middle_r = 3.0;

Call EighthOfSphere ;

Physical Surface("surface_boundary") = surface321;
Physical Surface("middle") = surface654;
//when uncommented while whole_volume is commented dolfin-convert crashes
Physical Surface("internal_boundary") = {surface012,surface023,surface031};
//When uncommented doubles the integration space
//and makes external_volume crashing everything
//whole_volume = newv;
//Volume(whole_volume) = {all_loops};
central_volume = newv;
Volume(central_volume) = {central_loops};
external_volume = newv;
Volume(external_volume) = {external_loops};

Physical Volume("whole") = {central_volume, external_volume};
//Physical Volume("central") = {central_volume};
//Physical Volume("external") = {external_volume};

The main reason was learning the hard way.

Hi all.
I get an error trying to read a boundary XDMF file, created with pygmsh and meshio, into a dolfin MeshValueCollection.

*** Error:   Unable to find entity in map.
*** Reason:  Error reading MeshValueCollection.
*** Where:   This error was encountered inside HDF5File.cpp.

I am using dolfin master.

2D MWE:

import pygmsh
import meshio

geom = pygmsh.built_in.Geometry()
rect = geom.add_rectangle(0, 1, 0, 1, 0, lcar=0.1)
geom.add_physical(rect.surface, 0)

for i, line in enumerate(rect.lines):
    geom.add_physical(line, i + 1)

mesh = pygmsh.generate_mesh(geom)

# write to XDMF, strip 3rd dimension
meshio.write('test.xdmf',
             meshio.Mesh(points=mesh.points[:, 0:2],
                         cells={'triangle':
                                mesh.cells['triangle']}))

# write boundary markers to separate file

meshio.write(
    'test_bound.xdmf',
    meshio.Mesh(
        points=mesh.points[:, 0:2],
        cells={'line': mesh.cells['line']},
        cell_data={'line':
                   {'markers': mesh.cell_data['line']['gmsh:physical']}
                   }
    )
)


# read mesh and markers

from dolfin import *

mesh = Mesh()
XDMFFile('test.xdmf').read(mesh)

mvc = MeshValueCollection('size_t', mesh)
# gives an error
XDMFFile('test_bound.xdmf').read(mvc, 'markers')

Any help is much appreciated!

Which version of dolfin are you using? I cant reproduce this behavior in dolfin.2019.1.0.

*** DOLFIN version: 2019.2.0.dev0
*** Git changeset: ec57db53f2b13f1768214829e8b4f80fc32205cf

should be current master

Have you tried running the dolfin code (after you generated the meshes in xdmf format) with docker?

docker run -ti -v $(pwd):/home/fenics/shared -w /home/fenics/shared/ --rm  quay.io/fenicsproject/dev:latest

With the following mesh file (using file_format="xdmf-xml" with meshio.write) i can load the mesh.

<Xdmf Version="3.0">
  <Domain>
    <Grid Name="Grid">
      <Information Name="Information" Value="0"><![CDATA[<main/>]]></Information>
      <Geometry GeometryType="XY">
        <DataItem DataType="Float" Dimensions="142 2" Format="XML" Precision="8">
0.000000000000000e+00 0.000000000000000e+00
1.000000000000000e+00 0.000000000000000e+00
1.000000000000000e+00 1.000000000000000e+00
0.000000000000000e+00 1.000000000000000e+00
9.999999999981467e-02 0.000000000000000e+00
1.999999999995579e-01 0.000000000000000e+00
2.999999999992664e-01 0.000000000000000e+00
3.999999999989749e-01 0.000000000000000e+00
4.999999999986943e-01 0.000000000000000e+00
5.999999999989468e-01 0.000000000000000e+00
6.999999999992100e-01 0.000000000000000e+00
7.999999999994734e-01 0.000000000000000e+00
8.999999999997368e-01 0.000000000000000e+00
1.000000000000000e+00 9.999999999981467e-02
1.000000000000000e+00 1.999999999995579e-01
1.000000000000000e+00 2.999999999992664e-01
1.000000000000000e+00 3.999999999989749e-01
1.000000000000000e+00 4.999999999986943e-01
1.000000000000000e+00 5.999999999989468e-01
1.000000000000000e+00 6.999999999992100e-01
1.000000000000000e+00 7.999999999994734e-01
1.000000000000000e+00 8.999999999997368e-01
8.999999999995836e-01 1.000000000000000e+00
7.999999999999998e-01 1.000000000000000e+00
7.000000000006934e-01 1.000000000000000e+00
6.000000000013869e-01 1.000000000000000e+00
5.000000000020587e-01 1.000000000000000e+00
4.000000000016644e-01 1.000000000000000e+00
3.000000000012483e-01 1.000000000000000e+00
2.000000000008322e-01 1.000000000000000e+00
1.000000000004160e-01 1.000000000000000e+00
0.000000000000000e+00 8.999999999995836e-01
0.000000000000000e+00 7.999999999999998e-01
0.000000000000000e+00 7.000000000006934e-01
0.000000000000000e+00 6.000000000013869e-01
0.000000000000000e+00 5.000000000020587e-01
0.000000000000000e+00 4.000000000016644e-01
0.000000000000000e+00 3.000000000012483e-01
0.000000000000000e+00 2.000000000008322e-01
0.000000000000000e+00 1.000000000004160e-01
5.000000000000000e-01 5.000000000000000e-01
7.071428571428752e-01 7.071428571428401e-01
2.928571428571630e-01 7.071428571428829e-01
7.071428571428393e-01 2.928571428571245e-01
2.928571428571166e-01 2.928571428571638e-01
4.931499629732459e-01 7.759084652632129e-01
2.240915347367899e-01 4.931499629732514e-01
7.759084652631878e-01 5.068500370267568e-01
5.068500370267515e-01 2.240915347368150e-01
8.294973544974140e-01 8.294973544973363e-01
1.705026455026733e-01 8.294973544974497e-01
8.294973544972866e-01 1.705026455025708e-01
1.705026455025349e-01 1.705026455027228e-01
6.407101126393269e-01 8.488660690838693e-01
1.511339309161314e-01 6.407101126393338e-01
3.592052746310420e-01 8.481751902264412e-01
8.481751902263315e-01 6.407947253690729e-01
8.488660690839256e-01 3.592898873606544e-01
1.518248097735569e-01 3.592052746310573e-01
3.592898873606481e-01 1.511339309160752e-01
6.407947253690573e-01 1.518248097736667e-01
5.669860627177817e-01 6.401567944250819e-01
3.639360530549317e-01 5.720108710844392e-01
6.401567944250810e-01 4.330139372822172e-01
4.330139372822104e-01 3.598432055749214e-01
4.376151275479142e-01 6.699666628188780e-01
6.661733647266973e-01 5.586987495249737e-01
3.383879245597805e-01 4.383753387593649e-01
5.586987495249669e-01 3.338266352733065e-01
4.664603708642711e-01 8.901567944253984e-01
1.098432055746039e-01 4.664603708642769e-01
8.901567944252827e-01 5.335396291357923e-01
5.335396291357863e-01 1.098432055747196e-01
7.390736031440464e-01 9.023348642130153e-01
9.766513578698906e-02 7.390736031440743e-01
9.023348642130204e-01 2.609263968558969e-01
2.609263968558690e-01 9.766513578698409e-02
9.005590740427145e-01 7.403047640902881e-01
2.596952359098002e-01 9.005590740428264e-01
7.403047640902634e-01 9.944092595728007e-02
9.944092595716839e-02 2.596952359098248e-01
6.041206249238610e-01 7.452829717179740e-01
2.547170282820282e-01 6.041206249238678e-01
7.452829717180283e-01 3.958793750761200e-01
3.958793750761137e-01 2.547170282819741e-01
3.847048916132225e-01 7.505210099668934e-01
7.505210099668249e-01 6.152951083867982e-01
2.494789900331101e-01 3.847048916132270e-01
6.152951083867945e-01 2.494789900331789e-01
9.001218934945729e-01 9.001218934945733e-01
9.987810650541903e-02 9.001218934945807e-01
9.001218934945625e-01 9.987810650543673e-02
9.987810650542896e-02 9.987810650542966e-02
5.700787557216642e-01 9.167513246759351e-01
8.324867532406605e-02 5.700787557216673e-01
9.167513246759277e-01 4.299212442785455e-01
4.299212442785424e-01 8.324867532407335e-02
7.939482183690576e-01 7.223833510180222e-01
2.776166489820220e-01 7.939482183691393e-01
7.223833510180057e-01 2.060517816309321e-01
2.060517816308507e-01 2.776166489820385e-01
7.277938243381867e-01 8.012652966217981e-01
1.987347033782313e-01 7.277938243383423e-01
8.012652966217936e-01 2.722061756617718e-01
2.722061756616174e-01 1.987347033782355e-01
4.860442197229556e-01 5.897234776367575e-01
5.801638557001146e-01 5.411773695432560e-01
4.090939062547123e-01 4.972014056715824e-01
5.450795590319500e-01 4.212072445256153e-01
6.519524273377167e-01 6.424993192059454e-01
3.535886428483550e-01 6.551594163499938e-01
6.424993192059415e-01 3.480475726622802e-01
3.464534195227987e-01 3.545328654627582e-01
6.499724943014235e-01 9.335904515945640e-01
6.640954840543729e-02 6.499724943014311e-01
9.335904515945748e-01 3.500275056986676e-01
3.500275056986601e-01 6.640954840542651e-02
9.277782117388658e-01 6.429278237186621e-01
6.429278237186529e-01 7.222178826113328e-02
3.570721762816051e-01 9.277782117389333e-01
7.222178826106583e-02 3.570721762816143e-01
8.201017201097205e-01 9.200285942774968e-01
7.997140572249439e-02 8.201017201097339e-01
9.200285942774356e-01 1.798982798902575e-01
1.798982798902443e-01 7.997140572255569e-02
9.200285942777351e-01 8.201017201098527e-01
8.201017201098352e-01 7.997140572227604e-02
1.798982798901033e-01 9.200285942778359e-01
7.997140572217544e-02 1.798982798901216e-01
5.149562121044042e-01 6.965799460685014e-01
3.087493467514952e-01 5.175725043545487e-01
7.068803990332486e-01 4.736105247275169e-01
4.736105247275106e-01 2.931196009667543e-01
5.549039654244737e-01 8.353931250332778e-01
1.646068749667239e-01 5.549039654244794e-01
8.353931250332703e-01 4.450960345755738e-01
4.450960345755684e-01 1.646068749667315e-01
4.276562509998072e-01 8.171708331229295e-01
1.828291668770721e-01 4.276562509998143e-01
8.171708331228448e-01 5.723437490002324e-01
5.723437490002254e-01 1.828291668771570e-01
4.529073225651297e-01 4.380437503040254e-01
</DataItem>
      </Geometry>
      <Topology NumberOfElements="242" TopologyType="Triangle">
        <DataItem DataType="Int" Dimensions="242 3" Format="XML" Precision="8">
1 13 91
0 4 92
2 22 89
3 31 90
12 1 91
39 0 92
21 2 89
30 3 90
105 62 107
65 62 105
63 106 108
63 66 106
64 68 108
64 108 141
54 82 102
53 81 101
59 84 104
57 83 103
82 42 102
81 41 101
84 44 104
83 43 103
65 45 85
66 47 86
67 46 87
68 48 88
86 56 97
85 55 98
88 60 99
87 58 100
67 64 141
26 27 69
35 36 70
17 18 71
8 9 72
71 18 117
72 9 118
69 27 119
70 36 120
45 65 129
46 67 130
66 63 131
47 66 131
68 64 132
48 68 132
41 86 97
42 85 98
43 88 99
44 87 100
62 65 110
64 67 112
107 67 141
40 105 107
106 40 108
56 71 117
60 72 118
55 69 119
58 70 120
23 24 73
32 33 74
14 15 75
5 6 76
19 20 77
10 11 79
28 29 78
37 38 80
26 69 93
35 70 94
17 71 95
8 72 96
56 77 97
55 78 98
60 79 99
58 80 100
69 45 133
70 46 134
71 47 135
72 48 136
81 61 109
82 62 110
83 63 111
84 64 112
49 73 101
50 74 102
51 75 103
52 76 104
41 81 109
42 82 110
43 83 111
44 84 112
61 106 109
106 66 109
63 108 111
108 68 111
25 26 93
34 35 94
16 17 95
7 8 96
85 45 137
86 47 139
87 46 138
88 48 140
23 73 121
32 74 122
14 75 123
5 76 124
77 20 125
79 11 126
78 29 127
80 38 128
74 54 102
73 53 101
76 59 104
75 57 103
77 49 97
78 50 98
79 51 99
80 52 100
13 14 123
22 23 121
4 5 124
31 32 122
20 21 125
11 12 126
29 30 127
38 39 128
93 69 133
94 70 134
95 71 135
96 72 136
91 13 123
92 4 124
89 22 121
90 31 122
21 89 125
12 91 126
30 90 127
39 92 128
97 49 101
98 50 102
99 51 103
100 52 104
61 81 129
62 82 130
63 83 131
64 84 132
45 69 137
46 70 138
69 55 137
70 58 138
47 71 139
48 72 140
71 56 139
72 60 140
73 24 113
74 33 114
78 55 119
53 73 113
54 74 114
77 56 117
80 58 120
79 60 118
28 78 119
19 77 117
37 80 120
10 79 118
75 15 115
76 6 116
57 75 115
59 76 116
81 45 129
82 46 130
83 47 131
84 48 132
105 61 129
65 105 129
107 62 130
67 107 130
73 49 121
74 50 122
49 77 125
75 51 123
50 78 127
76 52 124
51 79 126
52 80 128
81 53 133
45 81 133
82 54 134
46 82 134
47 83 135
83 57 135
84 59 136
48 84 136
65 85 110
85 42 110
66 86 109
86 41 109
67 87 112
68 88 111
87 44 112
88 43 111
41 97 101
42 98 102
43 99 103
44 100 104
49 89 121
50 90 122
89 49 125
90 50 127
51 91 123
91 51 126
52 92 124
92 52 128
105 40 106
61 105 106
25 93 113
34 94 114
93 53 113
94 54 114
16 95 115
7 96 116
95 57 115
96 59 116
24 25 113
33 34 114
15 16 115
6 7 116
18 19 117
9 10 118
27 28 119
36 37 120
55 85 137
56 86 139
58 87 138
60 88 140
53 93 133
54 94 134
57 95 135
59 96 136
108 40 141
40 107 141
</DataItem>
      </Topology>
    </Grid>
  </Domain>
</Xdmf>

and boundary_mesh

<Xdmf Version="3.0">
  <Domain>
    <Grid Name="Grid">
      <Information Name="Information" Value="0"><![CDATA[<main/>]]></Information>
      <Geometry GeometryType="XY">
        <DataItem DataType="Float" Dimensions="142 2" Format="XML" Precision="8">
0.000000000000000e+00 0.000000000000000e+00
1.000000000000000e+00 0.000000000000000e+00
1.000000000000000e+00 1.000000000000000e+00
0.000000000000000e+00 1.000000000000000e+00
9.999999999981467e-02 0.000000000000000e+00
1.999999999995579e-01 0.000000000000000e+00
2.999999999992664e-01 0.000000000000000e+00
3.999999999989749e-01 0.000000000000000e+00
4.999999999986943e-01 0.000000000000000e+00
5.999999999989468e-01 0.000000000000000e+00
6.999999999992100e-01 0.000000000000000e+00
7.999999999994734e-01 0.000000000000000e+00
8.999999999997368e-01 0.000000000000000e+00
1.000000000000000e+00 9.999999999981467e-02
1.000000000000000e+00 1.999999999995579e-01
1.000000000000000e+00 2.999999999992664e-01
1.000000000000000e+00 3.999999999989749e-01
1.000000000000000e+00 4.999999999986943e-01
1.000000000000000e+00 5.999999999989468e-01
1.000000000000000e+00 6.999999999992100e-01
1.000000000000000e+00 7.999999999994734e-01
1.000000000000000e+00 8.999999999997368e-01
8.999999999995836e-01 1.000000000000000e+00
7.999999999999998e-01 1.000000000000000e+00
7.000000000006934e-01 1.000000000000000e+00
6.000000000013869e-01 1.000000000000000e+00
5.000000000020587e-01 1.000000000000000e+00
4.000000000016644e-01 1.000000000000000e+00
3.000000000012483e-01 1.000000000000000e+00
2.000000000008322e-01 1.000000000000000e+00
1.000000000004160e-01 1.000000000000000e+00
0.000000000000000e+00 8.999999999995836e-01
0.000000000000000e+00 7.999999999999998e-01
0.000000000000000e+00 7.000000000006934e-01
0.000000000000000e+00 6.000000000013869e-01
0.000000000000000e+00 5.000000000020587e-01
0.000000000000000e+00 4.000000000016644e-01
0.000000000000000e+00 3.000000000012483e-01
0.000000000000000e+00 2.000000000008322e-01
0.000000000000000e+00 1.000000000004160e-01
5.000000000000000e-01 5.000000000000000e-01
7.071428571428752e-01 7.071428571428401e-01
2.928571428571630e-01 7.071428571428829e-01
7.071428571428393e-01 2.928571428571245e-01
2.928571428571166e-01 2.928571428571638e-01
4.931499629732459e-01 7.759084652632129e-01
2.240915347367899e-01 4.931499629732514e-01
7.759084652631878e-01 5.068500370267568e-01
5.068500370267515e-01 2.240915347368150e-01
8.294973544974140e-01 8.294973544973363e-01
1.705026455026733e-01 8.294973544974497e-01
8.294973544972866e-01 1.705026455025708e-01
1.705026455025349e-01 1.705026455027228e-01
6.407101126393269e-01 8.488660690838693e-01
1.511339309161314e-01 6.407101126393338e-01
3.592052746310420e-01 8.481751902264412e-01
8.481751902263315e-01 6.407947253690729e-01
8.488660690839256e-01 3.592898873606544e-01
1.518248097735569e-01 3.592052746310573e-01
3.592898873606481e-01 1.511339309160752e-01
6.407947253690573e-01 1.518248097736667e-01
5.669860627177817e-01 6.401567944250819e-01
3.639360530549317e-01 5.720108710844392e-01
6.401567944250810e-01 4.330139372822172e-01
4.330139372822104e-01 3.598432055749214e-01
4.376151275479142e-01 6.699666628188780e-01
6.661733647266973e-01 5.586987495249737e-01
3.383879245597805e-01 4.383753387593649e-01
5.586987495249669e-01 3.338266352733065e-01
4.664603708642711e-01 8.901567944253984e-01
1.098432055746039e-01 4.664603708642769e-01
8.901567944252827e-01 5.335396291357923e-01
5.335396291357863e-01 1.098432055747196e-01
7.390736031440464e-01 9.023348642130153e-01
9.766513578698906e-02 7.390736031440743e-01
9.023348642130204e-01 2.609263968558969e-01
2.609263968558690e-01 9.766513578698409e-02
9.005590740427145e-01 7.403047640902881e-01
2.596952359098002e-01 9.005590740428264e-01
7.403047640902634e-01 9.944092595728007e-02
9.944092595716839e-02 2.596952359098248e-01
6.041206249238610e-01 7.452829717179740e-01
2.547170282820282e-01 6.041206249238678e-01
7.452829717180283e-01 3.958793750761200e-01
3.958793750761137e-01 2.547170282819741e-01
3.847048916132225e-01 7.505210099668934e-01
7.505210099668249e-01 6.152951083867982e-01
2.494789900331101e-01 3.847048916132270e-01
6.152951083867945e-01 2.494789900331789e-01
9.001218934945729e-01 9.001218934945733e-01
9.987810650541903e-02 9.001218934945807e-01
9.001218934945625e-01 9.987810650543673e-02
9.987810650542896e-02 9.987810650542966e-02
5.700787557216642e-01 9.167513246759351e-01
8.324867532406605e-02 5.700787557216673e-01
9.167513246759277e-01 4.299212442785455e-01
4.299212442785424e-01 8.324867532407335e-02
7.939482183690576e-01 7.223833510180222e-01
2.776166489820220e-01 7.939482183691393e-01
7.223833510180057e-01 2.060517816309321e-01
2.060517816308507e-01 2.776166489820385e-01
7.277938243381867e-01 8.012652966217981e-01
1.987347033782313e-01 7.277938243383423e-01
8.012652966217936e-01 2.722061756617718e-01
2.722061756616174e-01 1.987347033782355e-01
4.860442197229556e-01 5.897234776367575e-01
5.801638557001146e-01 5.411773695432560e-01
4.090939062547123e-01 4.972014056715824e-01
5.450795590319500e-01 4.212072445256153e-01
6.519524273377167e-01 6.424993192059454e-01
3.535886428483550e-01 6.551594163499938e-01
6.424993192059415e-01 3.480475726622802e-01
3.464534195227987e-01 3.545328654627582e-01
6.499724943014235e-01 9.335904515945640e-01
6.640954840543729e-02 6.499724943014311e-01
9.335904515945748e-01 3.500275056986676e-01
3.500275056986601e-01 6.640954840542651e-02
9.277782117388658e-01 6.429278237186621e-01
6.429278237186529e-01 7.222178826113328e-02
3.570721762816051e-01 9.277782117389333e-01
7.222178826106583e-02 3.570721762816143e-01
8.201017201097205e-01 9.200285942774968e-01
7.997140572249439e-02 8.201017201097339e-01
9.200285942774356e-01 1.798982798902575e-01
1.798982798902443e-01 7.997140572255569e-02
9.200285942777351e-01 8.201017201098527e-01
8.201017201098352e-01 7.997140572227604e-02
1.798982798901033e-01 9.200285942778359e-01
7.997140572217544e-02 1.798982798901216e-01
5.149562121044042e-01 6.965799460685014e-01
3.087493467514952e-01 5.175725043545487e-01
7.068803990332486e-01 4.736105247275169e-01
4.736105247275106e-01 2.931196009667543e-01
5.549039654244737e-01 8.353931250332778e-01
1.646068749667239e-01 5.549039654244794e-01
8.353931250332703e-01 4.450960345755738e-01
4.450960345755684e-01 1.646068749667315e-01
4.276562509998072e-01 8.171708331229295e-01
1.828291668770721e-01 4.276562509998143e-01
8.171708331228448e-01 5.723437490002324e-01
5.723437490002254e-01 1.828291668771570e-01
4.529073225651297e-01 4.380437503040254e-01
</DataItem>
      </Geometry>
      <Topology NumberOfElements="40" TopologyType="Polyline">
        <DataItem DataType="Int" Dimensions="40 2" Format="XML" Precision="8">
0 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 1
1 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 2
2 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 3
3 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 0
</DataItem>
      </Topology>
      <Attribute AttributeType="Scalar" Center="Cell" Name="markers">
        <DataItem DataType="Int" Dimensions="40" Format="XML" Precision="8">
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
4
4
4
4
4
4
4
4
4
4
</DataItem>
      </Attribute>
    </Grid>
  </Domain>
</Xdmf>

Thanks @dokken. I am able to read your XDMF files with both master and the latest stable build.
Comparing your boundary mesh file with mine, written with the option file_format='xdmf-xml', showed one difference:

My file:

<Topology TopologyType="Polyline" NumberOfElements="40">
   <DataItem DataType="Int" Dimensions="40 3" Format="XML" Precision="8">
       2 0 4
       2 4 5
       2 5 6
       ...

Your file:

<Topology NumberOfElements="40" TopologyType="Polyline">
   <DataItem DataType="Int" Dimensions="40 2" Format="XML" Precision="8">
       0 4
       4 5
       5 6
       ...

Did you modify anything else in the meshio.write call for the boundary file? What would cause this difference? This seems to be the cause of the entity map error.

I am using meshio 3.3.0, pygmsh 6.0.2 and gmsh 4.0.2.

Thanks!

1 Like

The problem is with the meshio 3.3.0.
Im using: 3.2.14. I am able to reproduce your problem by upgrading meshio.
I would suggest contacting the meshio developers regarding this change of behavior.

Yes, probably it’s this commit here…

and this discussion:

I’ll see what they say.

I commented on the meshio issue. But it seems the change was intended as it is. Consistency.

I commented on the meshio issue. But it seems the change was intended as it is. Consistency.

Ha, yes, it was intended, but mistakenly. It seems that XDMF writes Polylines differently depending on whether they’re in TopologyType="Polyline" or part of TopologyType="mixed". Sorry about that. Please continue the discussion on meshio#599.

2 Likes

Great, it works now with meshio master from git. Thanks @gdmcbain!

Hi, I am having a problem very similar to @abukaj
I want to convert a 3d mesh generated in gmsh with volume and surface subdomains data.
I used the following gmsh script (generated trough GUI and compiled with gmsh 4.5.2)

// Gmsh project created on Wed Feb 12 11:25:22 2020
//+
SetFactory("OpenCASCADE");
Box(1) = {0, 0, 0, 1, 1, .285};
//+
Box(2) = {0.4, 0.4, 0.285, .2, .2, .03};
//+
Curve Loop(13) = {2, 12, -6, -10};
//+
Curve Loop(14) = {21, 20, -23, -16};
//+
Plane Surface(13) = {13, 14};
//+
Rectangle(14) = {0.0, 0., 0.315, 1, 1, 0};
//+
Curve Loop(16) = {28, 25, 26, 27};
//+
Curve Loop(17) = {24, -18, -22, 14};
//+
Plane Surface(15) = {16, 17};
//+
Physical Surface("patterned_gate", 29) = {10, 8, 9, 7, 15, 13};
//+
Physical Volume("substrate", 30) = {1};
//+
Physical Volume("cavity", 31) = {2};
//+
Box(3) = {0., 0, 0.315, 1, 1, .01};
//+
Physical Volume("hBN", 32) = {3};
//+
Box(4) = {0.0, 0.0, 0.325, 1, 1, 1};
//+
Physical Volume("air", 33) = {4};
//+
Physical Surface("back_gate") = {5};

Looking at at the mesh using GUI looks fine.
Now, followin @dokken I do (meshio version 3.3.1)

msh = meshio.read('cavity.msh')
meshio.write("mesh.xdmf", 
             meshio.Mesh(points = msh.points, 
                         cells = {"tetra": msh.cells["tetra"]}))

meshio.write("mf.xdmf", meshio.Mesh(points=msh.points, 
                                    cells={"triangle": msh.cells["triangle"]},
                                    cell_data={"triangle": {"name_to_read": msh.cell_data["triangle"]["gmsh:physical"]}}))

meshio.write("cf.xdmf", 
             meshio.Mesh(points=msh.points, 
             cells={"tetra": msh.cells["tetra"]},
             cell_data={"tetra": {"name_to_read": msh.cell_data["tetra"]["gmsh:physical"]}}))

I get the following warning

/usr/lib/python3/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

but files are created
Then I try to import in dolfin (2019.1.0)

mesh = Mesh()
with XDMFFile("mesh.xdmf") as infile:
    infile.read(mesh)
mvc = MeshValueCollection("size_t", mesh, 3)
with XDMFFile("cf.xdmf") as infile:
    infile.read(mvc, "name_to_read")
cf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
``
both work but 

mvc = MeshValueCollection("size_t", mesh, 2) 
with XDMFFile("mf.xdmf") as infile:
    infile.read(mvc, "name_to_read")
mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)


returns 

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-10-05ba402968ca> in <module>
      1 mvc = MeshValueCollection("size_t", mesh, 2)
      2 with XDMFFile("mf.xdmf") as infile:
----> 3     infile.read(mvc, "name_to_read")
      4 mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)

RuntimeError: 

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***     fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to find entity in map.
*** Reason:  Error reading MeshValueCollection.
*** Where:   This error was encountered inside HDF5File.cpp.
*** Process: 0
*** 
*** DOLFIN version: 2019.1.0
*** Git changeset:  unknown
*** -------------------------------------------------------------------------

The mesh can be displayed regularly and I can also extract a mesh from the boundary information file

mesh_surface = Mesh()
with XDMFFile("mf.xdmf") as infile:
    infile.read(mesh_surface)

that displays the marked surfaces.

Hope I am bumping in some well known issue.
Best.
Iacopo

As far as I can tell, this is because of your mesh. It seems like you are trying to combine a 2D and a 3D mesh, with both triangular and tetrahedron elements, which is not supported in dolfin.