As an MWE, I came up with this simple problem:
I created a simple geometry in gmsh, imported it to FEniCS. The geometry looks like below (followed by the gmsh script). I extracted the annulus part and tried to create xdmf files for the cells and the boundaries of the annulus using meshio.write().
gmsh script:
SetFactory("OpenCASCADE");
//+
Circle(1) = {0, 0, 0, 1, 0, 2*Pi};
//+
Circle(2) = {0, 0, 0, 2, 0, 2*Pi};
//+
Curve Loop(1) = {1};
//+
Plane Surface(1) = {1};
//+
Curve Loop(2) = {2};
//+
Curve Loop(3) = {1};
//+
Plane Surface(2) = {2, 3};
//+
Physical Curve("C1", 1) = {1};
//+
Physical Curve("C2", 2) = {2};
//+
Physical Surface("S1", 3) = {1};
//+
Physical Surface("S2", 4) = {2};
Python script:
from dolfin import *
import meshio
from dolfin import Mesh, XDMFFile, File, MeshValueCollection, cpp
import numpy
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)
out_mesh = meshio.Mesh(points=mesh.points, cells={
cell_type: cells}, cell_data={"name_to_read": [cell_data]})
if prune_z:
out_mesh.prune_z_0()
return out_mesh
#--------------------------------------------------------------IMPORTING GEOMETRY FROM GMSH-----------------------------------------------------------------------------------------------
msh = meshio.read("annulus_mesh.msh")
triangle_mesh = create_mesh(msh, "triangle", True)
line_mesh = create_mesh(msh, "line", True)
meshio.write("mesh.xdmf", triangle_mesh)
meshio.write("mf.xdmf", line_mesh)
from dolfin import *
mesh = Mesh()
mvc = MeshValueCollection("size_t", mesh, mesh.topology().dim())
with XDMFFile("mesh.xdmf") as infile:
infile.read(mesh)
infile.read(mvc, "name_to_read")
cf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
mvc = MeshValueCollection("size_t", mesh, mesh.topology().dim()-1)
with XDMFFile("mf.xdmf") as infile:
infile.read(mvc, "name_to_read")
mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
# Extracting the annulus
annulus = SubMesh(mesh,cf,4)
xdmf = XDMFFile("annulus_mesh.xdmf")
xdmf.write(annulus)
xdmf.close()
annulus_mesh = meshio.read("annulus_mesh.xdmf")
meshio.write("mesh_annulus.msh", annulus_mesh)
mesh_from_file = meshio.read("mesh_annulus.msh")
triangle_mesh = create_mesh(mesh_from_file, "triangle", prune_z=True)
line_mesh = create_mesh(mesh_from_file, "line", prune_z=True)
meshio.write("mesh_2d.xdmf", triangle_mesh)
meshio.write("mesh_1d.xdmf", line_mesh)
The error reads:
triangle_mesh = create_mesh(mesh_from_file, "triangle", prune_z=True)
TypeError: create_mesh(): incompatible function arguments. The following argument types are supported:
1. (arg0: dolfin.cpp.function.Function) -> dolfin.cpp.mesh.Mesh
2. (arg0: object) -> dolfin.cpp.mesh.Mesh
Invoked with: <meshio mesh object>
Number of points: 983
Number of cells:
triangle: 1798
Point data: gmsh:dim_tags
Cell data: gmsh:geometrical, 'triangle'; kwargs: prune_z=True
Any advice would be extremely helpful.