I can confirm that I am able to generate my 3d mesh using gmsh app but cannot convert it to xdmf on FEniCS somehow. Here is my code:
x_max = 2
y_max = 3
z_max = 4
# Mesh size
h = 0.1
mesh_script = "Point(1) = {{{0}, {1}, {2}, {3}}};\n".format(0, 0, 0, 1)
mesh_script += "Point(2) = {{{0}, {1}, {2}, {3}}};\n".format(x_max, 0, 0, 1)
mesh_script += "Point(3) = {{{0}, {1}, {2}, {3}}};\n".format(x_max, y_max, 0, 1)
mesh_script += "Point(4) = {{{0}, {1}, {2}, {3}}};\n".format(0, y_max, 0, 1)
mesh_script += "Point(5) = {{{0}, {1}, {2}, {3}}};\n".format(0, 0, z_max, 1)
mesh_script += "Point(6) = {{{0}, {1}, {2}, {3}}};\n".format(x_max, 0, z_max, 1)
mesh_script += "Point(7) = {{{0}, {1}, {2}, {3}}};\n".format(x_max, y_max, z_max, 1)
mesh_script += "Point(8) = {{{0}, {1}, {2}, {3}}};\n".format(0, y_max, z_max, 1)
mesh_script += "Line(1) = {1, 2};\n"
mesh_script += "Line(2) = {2, 3};\n"
mesh_script += "Line(3) = {3, 4};\n"
mesh_script += "Line(4) = {4, 1};\n"
mesh_script += "Line(5) = {1, 5};\n"
mesh_script += "Line(6) = {2, 6};\n"
mesh_script += "Line(7) = {3, 7};\n"
mesh_script += "Line(8) = {4, 8};\n"
mesh_script += "Line(9) = {5, 6};\n"
mesh_script += "Line(10) = {6, 7};\n"
mesh_script += "Line(11) = {7, 8};\n"
mesh_script += "Line(12) = {8, 5};\n"
mesh_script += "Curve Loop(1) = {8, -11, -7, 3};\n"
mesh_script += "Plane Surface(1) = {1};\n"
mesh_script += "Curve Loop(2) = {7, -10, -6, 2};\n"
mesh_script += "Plane Surface(2) = {2};\n"
mesh_script += "Curve Loop(3) = {2, 3, 4, 1};\n"
mesh_script += "Plane Surface(3) = {3};\n"
mesh_script += "Curve Loop(4) = {4, 5, -12, -8};\n"
mesh_script += "Plane Surface(4) = {4};\n"
mesh_script += "Curve Loop(5) = {12, 9, 10, 11};\n"
mesh_script += "Plane Surface(5) = {5};\n"
mesh_script += "Curve Loop(6) = {6, -9, -5, 1};\n"
mesh_script += "Plane Surface(6) = {6};\n"
mesh_script += "Surface Loop(1) = {4, 3, 2, 1, 5, 6};\n"
mesh_script += "Volume(1) = {1};\n"
mesh_script += "MeshSize {1, 2, 3, 4, 5, 6, 7, 8}"
mesh_script += " = {0};\n".format(h)
mesh_script += "Physical Surface(201) = {1, 2, 3, 4, 5, 6};\n"
mesh_script += "Physical Volume(301) = {1};\n"
print("Running Gmsh...")
geo_file=open("mesh.geo",'w')
geo_file.write(mesh_script)
geo_file.close()
gmsh_err=call(["gmsh", "-3", "mesh.geo", "-o", "mesh.msh"])
if gmsh_err:
print("...mesh cannot be generated!")
else:
print("...mesh generated successfully!")
def mesh_generation(mesh_script):
print("Running Gmsh...")
geo_file=open("mesh.geo",'w')
geo_file.write(mesh_script)
geo_file.close()
gmsh_err=call(["gmsh", "-3", "mesh.geo", "-o", "mesh.msh"])
if gmsh_err:
print("...something bad happened \
when the mesh was being generated...")
else:
print("...mesh generated successfully!")
msh = meshio.read("mesh.msh")
line_cells = []
for cell in msh.cells:
if cell.type == "triangle":
triangle_cells = cell.data
elif cell.type == "line":
if len(line_cells) == 0:
line_cells = cell.data
else:
line_cells = np.vstack([line_cells, cell.data])
line_data = []
for key in msh.cell_data_dict["gmsh:physical"].keys():
if key == "line":
if len(line_data) == 0:
line_data = \
msh.cell_data_dict["gmsh:physical"][key]
else:
line_data = \
np.vstack([line_data, \
msh.cell_data_dict["gmsh:physical"][key]])
elif key == "triangle":
triangle_data = \
msh.cell_data_dict["gmsh:physical"][key]
triangle_mesh = \
meshio.Mesh(points=msh.points, \
cells={"triangle": triangle_cells})
line_mesh = \
meshio.Mesh(points=msh.points,\
cells=[("line", line_cells)],\
cell_data={"name_to_read":[line_data]})
meshio.write("mesh.xdmf", triangle_mesh)
meshio.xdmf.write("mf.xdmf", line_mesh)
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)
mesh_data = [mesh, mvc, mf]
return mesh_data
from subprocess import call
mesh_data = mesh_generation(mesh_script)
mesh = mesh_data[0]
mvc = mesh_data[1]
mf = mesh_data[2]
I am getting an “IndexError: tuple index out of range”.
I am trying to follow up .msh to xdmf for 3D from here. Can anyone help? Thanks!