Hi,
I am having a problem in converting a 1D mesh generated with gmsh to dolfin.
(I am using gmsh 4.5.5, meshio 4.2.0 and dolfin 2019.1.0)
I use the following code to convert the .msh file to XDMF
import numpy as np
import meshio
#read msh
msh = meshio.read('example1D.msh')
fn = 'example1D'
dim_keep = 2
#get cells
line_cells = []
vertex_cells = []
for cell in msh.cells:
if cell.type == 'line':
if len(line_cells) == 0:
line_cells = cell.data
else:
line_cells = np.vstack([line_cells, cell.data])
if cell.type == 'vertex':
if len(vertex_cells) == 0:
vertex_cells = cell.data
else:
vertex_cells = np.vstack([vertex_cells, cell.data])
#get cells data
line_data = None
vertex_data = None
for key in msh.cell_data_dict["gmsh:physical"].keys():
if key == "vertex":
vertex_data = msh.cell_data_dict["gmsh:physical"][key]
elif key == "line":
line_data = msh.cell_data_dict["gmsh:physical"][key]
line_mesh = meshio.Mesh(points = np.atleast_2d(msh.points[:,:dim_keep]),
cells = [("line", line_cells)],
cell_data = {"name_to_read":[line_data]})
meshio.write(fn + '_1D_mesh.xdmf', line_mesh)
if not (vertex_data is None):
vertex_mesh = meshio.Mesh(points = msh.points[:,:dim_keep],
cells = [("vertex", vertex_cells)],
cell_data = {"name_to_read":[vertex_data]})
meshio.write(fn + '_0D_mesh.xdmf', vertex_mesh)
note that I am using the parameter dim_keep to keep only the first dimensions.
I then import the mesh and the markers using
import dolfin as dol
mesh = dol.Mesh()
with dol.XDMFFile(fn + '_1D_mesh.xdmf') as infile:
infile.read(mesh)
mvc = dol.MeshValueCollection('size_t', mesh= mesh, dim = 1)
with dol.XDMFFile(fn + '_1D_mesh.xdmf') as infile:
infile.read(mvc, 'name_to_read')
marker_1D = dol.cpp.mesh.MeshFunctionSizet(mesh, mvc)
mvc = dol.MeshValueCollection('size_t', mesh= mesh, dim = 0)
with dol.XDMFFile(fn + '_0D_mesh.xdmf') as infile:
infile.read(mvc, 'name_to_read')
marker_0D = dol.cpp.mesh.MeshFunctionSizet(mesh, mvc)
This works as long as I use dim_keep =2,3 but if I use dim_keep =1 (I want to have a truly 1D mesh, not embedded in 2 or 3D space) I get the following error when trying to load the mesh (infile.read(mesh)):
*** Error: Unable to determine geometric dimension.
*** Reason: GeometryType "X" in XDMF file is unknown or unsupported.
*** Where: This error was encountered inside XDMFFile.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.1.0
*** Git changeset: unknown
It is not a big deal since i can always embed my 1D mesh in a 2d space but I don’t understand what is happening.
For the sake of completeness my .geo file reads
SetFactory("OpenCASCADE");
Point(1) = {0,0,0,0.1};
Point(2) = {1,0,0,0.2};
Line(1) ={1,2};
Physical Line('domain',1) = {1};
Physical Point('left',1) = {1};
Physical Point('right', 2) = {2};
Thanks for helping!