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

**URL:** <https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412>\
**Category:** mesh\
**Created:** [March 22, 2019, 9:01am UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412 "2019-03-22T09:01:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![raboynics](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@raboynics](https://fenicsproject.discourse.group/u/raboynics)\
**Post date:** [March 22, 2019, 9:01am UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/1 "2019-03-22T09:01:33Z")

</div>

I have read that the support from dolfin-convert is almost nill and will soon be deprecated. This means I have to change the way I read the mesh file created with Gmsh in my codes, but so far I am unable to do it. My goal is to make the transition as smooth as possible.  
As of now I create a file.geo with Gmsh, convert it to a file.msh using the command `gmsh file.geo -format msh2 -3`. Then I use `dolfin-convert file.msh file.xml` which creates 2 files I can read with dolfin, and everything is easy and fine. More precisely, I do

> subdomains = MeshFunction(“size\_t”, mesh, file\_physical\_region.xml)  
> boundary\_parts = MeshFunction(“size\_t”, mesh, file\_facet\_region.xml)

This is what I would like to achieve with meshio. From what I have read, meshio supports some features of dolfin-convert regarding the xml file format, but it isn’t quite similar so it is impossible to reproduce what dolfin-convert does in the above example. Furthermore, XDMF is instead the suggested alternative.  
So, let’s take the following file.geo:

```
SetFactory("OpenCASCADE");
Mesh.RandomFactor=1.0e-6;
lc=0.00017;
a = 0.002;
b = 0.001;
c = 0.00003;
Point(1) = {-a/2, -b/2, -c/2, lc};
Point(2) = {-a/2, b/2, -c/2, lc};
Point(3) = {a/2, b/2, -c/2, lc};
Point(4) = {a/2, -b/2, -c/2, lc};
Line(1) = {1,2};
Line(2) = {2,3};
Line(3) = {3,4};
Line(4) = {4,1};
Line Loop(5) = {1,2,3,4};
Point(7) = {-a/10 + a/4.4,-b/10-b/3, -c/2, lc};
Point(8) = {-a/10 + a/4.4,b/10-b/3,-c/2, lc};
Point(9) = {a/10 + a/4.4, b/10-b/3,-c/2, lc};
Point(10) = {a/10 + a/4.4,-b/10-b/3,-c/2, lc};
Line(11) = {7,8};
Line(12) = {8,9};
Line(13) = {9,10};
Line(14) = {10,7};
Line Loop(15) = {11,12,13,14};
Plane Surface(16) = {15};
Point(17) = {-a/16+a/4,-b/16+b/3.2, -c/2, lc};
Point(18) = {-a/16+a/4,b/16+b/3.2,-c/2, lc};
Point(19) = {a/16+a/4, b/16+b/3.2,-c/2, lc};
Point(20) = {a/16+a/4,-b/16+b/3.2,-c/2, lc};
Line(21) = {17,18};
Line(22) = {18,19};
Line(23) = {19,20};
Line(24) = {20,17};
Line Loop(25) = {21,22,23,24};
Plane Surface(26) = {25};
Point(27) = {-a/20-a/2.5,-b/3, -c/2, lc};
Point(28) = {-a/20-a/2.5,b/3,-c/2, lc};
Point(29) = {a/20-a/2.5, b/3,-c/2, lc};
Point(30) = {a/20-a/2.5,-b/3,-c/2, lc};
Line(31) = {27,28};
Line(32) = {28,29};
Line(33) = {29,30};
Line(34) = {30,27};
Line Loop(35) = {31,32,33,34};
Plane Surface(36) = {35};
Point(37) = {-a/4,-b/3, -c/2, lc};
Point(38) = {-a/4,b/3,-c/2, lc};
Point(39) = {-a/5, b/3,-c/2, lc};
Point(40) = {-a/5,-b/3,-c/2, lc};
Line(41) = {37,38};
Line(42) = {38,39};
Line(43) = {39,40};
Line(44) = {40,37};
Line Loop(45) = {41,42,43,44};
Plane Surface(46) = {45};
Plane Surface(47)={5,25,15,35,45};
Physical Surface("bottom")={47};
Physical Surface("bot_1")={16};
Physical Surface("bot_2")={26};
Physical Surface("bot_3")={36};
Physical Surface("bot_4")={46};
surfaceVector[] = Extrude {0, 0, c} {
     Surface{47,26,16,36,46};
     Layers{31};
    };
Physical Surface("top") = {surfaceVector[0]};
Physical Volume("parallelepiped") = {surfaceVector[1]};
Physical Surface("left") = {surfaceVector[2]};
Physical Surface("back") = {surfaceVector[3]};
Physical Surface("right") = {surfaceVector[4]};
Physical Surface("front") = {surfaceVector[5]};
Physical Surface("top_2") = {surfaceVector[22]};
Physical Volume("stuff_2") = {surfaceVector[23]};
Physical Surface("top_1") = {surfaceVector[28]};
Physical Volume("stuff_1") = {surfaceVector[29]};
Physical Surface("top_3") = {surfaceVector[34]};
Physical Volume("stuff_3") = {surfaceVector[35]};
Physical Surface("top_4") = {surfaceVector[40]};
Physical Volume("stuff_4") = {surfaceVector[41]};

```

Then we create the file.msh using `gmsh file.geo -3`. Then we use `meshio-convert file.msh file.xdmf`. We now have a file.h5 and a file.xdmf.

And this is where I am stuck, I do not know how to read the mesh and assign the boundaries as I used to do with the file.xml that was produced with dolfin-convert.  
I tried:

```
from dolfin import *
mesh = Mesh()
with XDMFFile("file.xdmf") as infile:
    infile.read(mesh)

```

But this yields

> * * *
> 
> RuntimeError Traceback (most recent call last)  
> in   
> 8 with XDMFFile(“file.xdmf”) as infile:  
> ----\> 9 infile.read(mesh)  
> 10
> 
> 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](mailto: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 recognise cell type.  
> \*\*\* Reason: Unknown value “mixed”.  
> \*\*\* Where: This error was encountered inside XDMFFile.cpp.  
> \*\*\* Process: 0
> 
> * * *
> 
> \*\*\* DOLFIN version: 2018.1.0  
> \*\*\* Git changeset: unknown  
> \*\*\* -------------------------------------------------------------------------

So it seems unable to recognize the cell type. I get exactly the same error if I use the deprecated -format msh2 flag in the gmsh command to produce the file.msh. Thus, overall, I do not know how to reproduce the simple commands that work with the file.xml produced with dolfin-convert, using meshio.

Note that I had posted a similiar question [there](https://fenicsproject.discourse.group/t/create-mesh-with-gmsh-convert-it-and-import-it-in-dolfin-problems/107/2) but I eventually ran into errors and so this lead to no solution.

---

<div class="post-metadata">

**Author:** ![tianyikillua](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/tianyikillua/32/3516_2.png) [@tianyikillua](https://fenicsproject.discourse.group/u/tianyikillua)\
**Post date:** [March 22, 2019, 2:29pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/2 "2019-03-22T14:29:25Z")

</div>

See this [gist](https://gist.github.com/michalhabera/bbe8a17f788192e53fd758a67cbf3bed), before converting to XDMF you’d need to “prune” all lower-dimensional elements, as currently FEniCS does not support “mixed” mesh composing from several element types (may they belong to a same dimension or not!) According to their recent released [roadmap](https://fenicsproject.org/fenics-project-roadmap-2019/), the support for mixed elements is for long term…

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [March 22, 2019, 2:35pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/3 "2019-03-22T14:35:40Z")

</div>

This can be done using meshio and writing the explicit data to xdmf file.

```
import meshio
msh = meshio.read("test.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"]}}))

from dolfin import * 
mesh = Mesh()
with XDMFFile("mesh.xdmf") as infile:
    infile.read(mesh)
mvc = MeshValueCollection("size_t", mesh, 2) 
with XDMFFile("mf.xdmf") as infile:
    infile.read(mvc, "name_to_read")
mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
File("dolfinmesh.pvd").write(mesh)
File("dolfincellfunc.pvd").write(mf)
```

---

<div class="post-metadata">

**Author:** ![raboynics](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@raboynics](https://fenicsproject.discourse.group/u/raboynics)\
**Post date:** [March 22, 2019, 3:47pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/4 "2019-03-22T15:47:12Z")

</div>

Thank you very much! If that’s not too much asked, how can I proceed and do the equivalent of the lines

> subdomains = MeshFunction(“size\_t”, mesh, file\_physical\_region.xml)  
> boundary\_parts = MeshFunction(“size\_t”, mesh, file\_facet\_region.xml)

?  
I cannot simply replace `file_physical_region.xml` by `dolfinmesh.pvd` and `file_facet_region.xml` by `dolfincellfunc.pvd`.

---

<div class="post-metadata">

**Author:** ![raboynics](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@raboynics](https://fenicsproject.discourse.group/u/raboynics)\
**Post date:** [March 22, 2019, 3:48pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/5 "2019-03-22T15:48:33Z")

</div>

Hmm but isn’t it strange that I do not need any manual pruning when I use dolfin-convert to xml? As far as I understand, the elements of the mesh shouldn’t depend on the file extension… I guess I’m missing something.

---

<div class="post-metadata">

**Author:** ![tianyikillua](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/tianyikillua/32/3516_2.png) [@tianyikillua](https://fenicsproject.discourse.group/u/tianyikillua)\
**Post date:** [March 22, 2019, 4:08pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/6 "2019-03-22T16:08:58Z")

</div>

Well dolfin\_convert is specially designed for FEniCS so I suppose these lower dimensional elements are taken care of during conversion. meshio is a generic mesh convertor so we try to conserve all original mesh information.

Le ven. 22 mars 2019 à 16:58, raboynics via FEniCS Project [fenicsproject1@discoursemail.com](mailto:fenicsproject1@discoursemail.com) a écrit :

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [March 22, 2019, 4:11pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/7 "2019-03-22T16:11:15Z")

</div>

As you can see in the code snippet above, I first read the boundary data to the file `mf.xdmf`. this is equivalent to `facet_region.xml`.  
For the cellfunction:

```auto
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"]}}))
mvc = MeshValueCollection("size_t", mesh, 3)
with XDMFFile("cf.xdmf") as infile:
    infile.read(mvc, "name_to_read")
cf = cpp.mesh.MeshFunctionSizet(mesh, mvc)

```

---

<div class="post-metadata">

**Author:** ![raboynics](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@raboynics](https://fenicsproject.discourse.group/u/raboynics)\
**Post date:** [March 25, 2019, 9:22am UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/8 "2019-03-25T09:22:37Z")

</div>

If I understand well, what I call `subdomains` corresponds to your `mvc`, while what I call `boundary_parts` corresponds to your `cf`.  
But when I use your code and then `subdomains = mvc` and `boundary_parts = cf`, I get nonsensical results.  
For example a non-zero surface area `assemble(1 * ds(12, domain=mesh))` returns 0 now, instead of the correct area. I suspect it’s because in my original code I have `mesh = Mesh("file.xml")` while in my new code I have `mesh = Mesh()`. I do not know which argument I can put inside the function Mesh(). I tried the XDMF file, but this returns an error. So I still don’t know how to make the full transition to XDMF.

Furthermore I am only able to reach that far on an Ubuntu machine, where dolfin has been compiled with hdf5 support. On Arch Linux (at home), I get an error message when I use your snippet codes as described in an earlier question [here](https://fenicsproject.discourse.group/t/create-mesh-with-gmsh-convert-it-and-import-it-in-dolfin-problems/107/3), and on the [fenics support mailing list](https://groups.google.com/forum/#!topic/fenics-support/-ApSpB4-UuY). I do not know how to compile dolfin with hdf5 support, it doesn’t do it by default even when the hdf5 package(s) are installed.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [March 25, 2019, 9:57am UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/9 "2019-03-25T09:57:47Z")

</div>

I dont really now how to help you further. If you use the docker to install fenics, and then run the following:

```auto
 docker run -ti --rm -v $(pwd):/home/fenics/shared quay.io/fenicsproject/stable:latest
 sudo pip install meshio lxml h5py

```

I used gmsh 3.0.6 to generate the msh file.  
Then running the following script:

```auto
import meshio
msh = meshio.read("file.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"]}}))

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

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

ds_custom = Measure("ds", domain=mesh, subdomain_data=mf, subdomain_id=12)
print(assemble(1*ds_custom))

```

I get a non-zero output:

```auto
3.125e-08

```

---

<div class="post-metadata">

**Author:** ![raboynics](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@raboynics](https://fenicsproject.discourse.group/u/raboynics)\
**Post date:** [March 25, 2019, 2:18pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/10 "2019-03-25T14:18:00Z")

</div>

Thank you very much for the information and code snippet. I can finally get it to work (on Ubuntu). However there is a slight discrepancy with the result of solving a weak form problem I get with the file.xml from dolfin-convert, and I do not know which one is more accurate (xdmf or xml). Can I simply compare the number of mesh elements in the two cases and the ones with a higher number should in principle be the most accurate?

More precisely, for a calculation I get **0.0103113** 9055668843 with the xdmf mesh and **0.0103113** 76092968772 with the mesh from the xml file. I have put in bold where they match.

Edit: Hmm, that’s extremely strange!  
`print(mesh.num_edges(), mesh.num_cells(), mesh.num_facets())` yields exactly the same numbers for both the xml and xdmf meshes. I am not sure what’s causing the solution to a complicated weak form to differ, since I copied and pasted the whole code and modified only the mesh part.

---

<div class="post-metadata">

**Author:** ![Corwinpro](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Corwinpro](https://fenicsproject.discourse.group/u/Corwinpro)\
**Post date:** [April 10, 2019, 9:59pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/11 "2019-04-10T21:59:18Z")

</div>

Hello!

I’m having a similar problem. I have mesh.msh file generated with

gmsh mesh.geo -2 -format msh2 -o mesh.msh

And when I read it a try to create a new meshio Mesh as

mesh = meshio.read(‘mesh.msh’)  
mesh.points = mesh.points[:, :2] # remove z-values to force 2d  
mesh = meshio.Mesh(points=mesh.points, cells={“triangle”: mesh.cells[“triangle”]})

I got KeyValue error ‘triangle’ since mesh.cells are only ‘lines’ not ‘triangle’.

Could anybody please help me with that?

Thanks!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 11, 2019, 8:50pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/12 "2019-04-11T20:50:24Z")

</div>

Could you please post your GEO file?

---

<div class="post-metadata">

**Author:** ![Leo\_Costa](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/leo_costa/32/131_2.png) [@Leo\_Costa](https://fenicsproject.discourse.group/u/Leo_Costa)\
**Post date:** [April 12, 2019, 4:53pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/13 "2019-04-12T16:53:27Z")

</div>

// the square  
Point(1) = {0, 0, 0, 0.1E+3};  
Point(2) = {0, 11E+3, 0, 0.1E+3};  
Point(3) = {11E+3, 11E+3, 0, 0.1E+3};  
Point(4) = {11E+3, 0, 0, 0.1E+3};  
Line(1) = {1, 4};  
Line(2) = {4, 3};  
Line(3) = {3, 2};  
Line(4) = {2, 1};  
// the circle in the centre  
Point(5) = {5.5E+3, 5.5E+3, 0, 0.1E+3};  
Point(6) = {1E+3, 5.5E+3, 0, 0.1E+3};  
Circle(5) = {6, 5, 6};  
// creating the surfaces  
Line Loop(6) = {2, 3, 4, 1};  
Line Loop(7) = {5};  
Plane Surface(8) = {6, 7};  
Plane Surface(9) = {7};  
//extruding them  
Extrude {0, 0, 0.55E+3} {  
Surface{9, 8}; Layers{10};  
}//+  
Physical Volume(0) = {1, 2};  
//+  
Physical Surface(1) = {16};  
//+  
Physical Surface(2) = {9};

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 12, 2019, 6:18pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/14 "2019-04-12T18:18:55Z")

</div>

Couple of notes, are you doing a 2D mesh ? When i Look at you mesh in gmsh, it strikes me as you would like to have a 3D mesh?

---

<div class="post-metadata">

**Author:** ![Leo\_Costa](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/leo_costa/32/131_2.png) [@Leo\_Costa](https://fenicsproject.discourse.group/u/Leo_Costa)\
**Post date:** [April 12, 2019, 6:31pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/15 "2019-04-12T18:31:10Z")

</div>

I am doing 3D mesh, how can i use to write and read it for fenics?  
I got errors

---

<div class="post-metadata">

**Author:** ![Leo\_Costa](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/leo_costa/32/131_2.png) [@Leo\_Costa](https://fenicsproject.discourse.group/u/Leo_Costa)\
**Post date:** [April 12, 2019, 6:34pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/16 "2019-04-12T18:34:14Z")

</div>

I am aplying Dirichilet boundary on Physical surface 1 and Newman in surface 2.  
I am solving a poisson system like in transient.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 12, 2019, 6:51pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/17 "2019-04-12T18:51:16Z")

</div>

Could you supply me with your error messages, as I your mesh file, with `gmsh -3 mesh3d.geo`, and

```auto
import meshio

msh = meshio.read("mesh3d.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"]}}))

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

```

works for me

---

<div class="post-metadata">

**Author:** ![JulianDwain](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@JulianDwain](https://fenicsproject.discourse.group/u/JulianDwain)\
**Post date:** [June 16, 2019, 4:06pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/18 "2019-06-16T16:06:57Z")

</div>

Hi everyone,

i’m currently trying to import a complex mesh geometry in Fenics.  
The original file is from the Nastran format **.nas**. I opened the file in gmsh and exported it as **.msh** file. So far, so good.  
I can read the file succesfully using **meshio.read** and write it to one **.xdmf** file using **meshio.write** :

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

```

But when i want to execute the second line

```auto
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"]}}))

```

i get the following error:

```auto
cells={"triangle": msh.cells["triangle"]},
KeyError: 'triangle'

```

I’m using Dolfin 2019.1.0 and used gmsh version 3.0.6, the mesh should be a 3D mesh.  
I’m not quite sure if its possible to open a **.nas** file with gmsh and export it as **.msh** file.

Thanks in advance, i appreciate every little help i can get!

---

<div class="post-metadata">

**Author:** ![Julius](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Julius](https://fenicsproject.discourse.group/u/Julius)\
**Post date:** [June 17, 2019, 2:04pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/19 "2019-06-17T14:04:26Z")

</div>

Does your msh file contain a ‘PhysicalNames’ section with 2D information? [http://gmsh.info/doc/texinfo/gmsh.html#MSH-file-format](http://gmsh.info/doc/texinfo/gmsh.html#MSH-file-format)

Maybe post the header of the msh file, this would help

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 17, 2019, 2:14pm UTC](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/20 "2019-06-17T14:14:24Z")

</div>

Hi,  
If your mesh function is a cell function, you need to change the value `"triangle"` to `"tetra"`. The second line reads a facet function, whose cells are triangles.

[Next page](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412.md?page=2)
