Converting simple 2D mesh from gmsh to dolfin

The code I’ve posted there is for a 3D mesh, however, it generalizes to a 2D mesh.
Given this simple geo file

// the square
Point(1) = {0, 0, 0,0.1};
Point(2) = {0, 1, 0,0.1};
Point(3) = {1, 1, 0,0.1};
Point(4) = {1, 0, 0,0.1};
Line(1) = {1, 4};
Line(2) = {4, 3};
Line(3) = {3, 2};
Line(4) = {2, 1};
// creating the surfaces
Line Loop(6) = {2, 3, 4, 1};
Plane Surface(8) = {6};

Physical Surface(2) = {8};

I call gmsh -2 mesh.geo to create the msh file
Then using meshio I load the meshio

import meshio
msh = meshio.read("mesh.msh")

This loads all the data of the mesh. to generate the mesh, I look at the data inside msh.
We need the points, located at msh.points. Since we do a 2D mesh, using triangles, we have to save it with triangles as cell data, with the data, as:
meshio.write("mesh.xdmf", meshio.Mesh(points=msh.points, cells={"triangle": msh.cells["triangle"]})).
To create a file containing the Facet data described with Physical line. We save the meshfunction mf as
meshio.write("mf.xdmf", meshio.Mesh(points=msh.points, cells={"line": msh.cells["line"]}, cell_data={"line": {"name_to_read": msh.cell_data["line"]["gmsh:physical"]}}))
Similarly, if you would like to write the physical surface to file. You have to save it with triangle as cells.

5 Likes