Hi all,
For my simulations (with fenics 2019.2.0
), I’m currently using gmsh
for the mesh generation, and then I use meshio
for transforming the .msh
file into .xdmf
files, and finally I load these files as MeshFunction
. A typical workflow is:
from dolfin import *
import gmsh
import sys
import meshio
import numpy as np
gmsh.initialize(sys.argv)
if MPI.comm_world.rank == 0:
gmsh.model.add("geometry")
gmsh.model.occ.addCircle(0, 0, 0, 1, angle1 = 0, angle2 = 2*pi, tag=1)
gmsh.model.occ.addCircle(0, 0, 0, 2, angle1 = 0, angle2 = 2*pi, tag=2)
gmsh.model.occ.addCurveLoop([1], tag=1)
gmsh.model.occ.addPlaneSurface([1], tag=1)
gmsh.model.occ.addCurveLoop([2], tag=2)
gmsh.model.occ.addCurveLoop([1], tag=3)
gmsh.model.occ.addPlaneSurface([2, 3], tag=2)
gmsh.model.occ.synchronize()
gmsh.model.addPhysicalGroup(2, [1], tag=1)
gmsh.model.addPhysicalGroup(2, [2], tag=2)
gmsh.model.addPhysicalGroup(1, [2], tag=3)
gmsh.model.mesh.setSize([(0, 1)], size = 0.1)
gmsh.model.mesh.setSize([(0, 2)], size = 0.1)
gmsh.model.mesh.generate(2)
gmsh.write("mesh.msh")
gmsh.finalize()
if MPI.comm_world.rank == 0:
msh = meshio.read("mesh.msh")
cells = np.vstack(np.array([cells.data for cells in msh.cells
if cells.type == "triangle"]))
total_mesh = meshio.Mesh(points=msh.points,
cells=[("triangle", cells)])
total_mesh.prune_z_0()
cells_data = msh.cell_data_dict["gmsh:physical"]["triangle"]
cells_mesh = meshio.Mesh(points=msh.points,
cells=[("triangle", cells)],
cell_data={"name_to_read": [cells_data]})
cells_mesh.prune_z_0()
facet_cells = np.vstack(np.array([cells.data for cells in msh.cells
if cells.type == "line"]))
facet_data = msh.cell_data_dict["gmsh:physical"]["line"]
facet_mesh = meshio.Mesh(points=msh.points,
cells=[("line", facet_cells)],
cell_data={"name_to_read": [facet_data]})
facet_mesh.prune_z_0()
meshio.write("total.xdmf", total_mesh)
meshio.write("facets.xdmf", facet_mesh)
meshio.write("cells.xdmf", cells_mesh)
MPI.comm_world.barrier()
parameters["ghost_mode"] = "shared_vertex"
comm = MPI.comm_world
mesh = Mesh(MPI.comm_world)
with XDMFFile(MPI.comm_world, "cells.xdmf") as infile:
infile.read(mesh)
mvc_facets = MeshValueCollection("size_t", mesh, 1)
with XDMFFile(MPI.comm_world, "facets.xdmf") as infile:
infile.read(mvc_facets, "name_to_read")
mf_facets = cpp.mesh.MeshFunctionSizet(mesh, mvc_facets)
mvc_cells = MeshValueCollection("size_t", mesh, 2)
with XDMFFile(MPI.comm_world, "cells.xdmf") as infile:
infile.read(mvc_cells, "name_to_read")
mf_cells = cpp.mesh.MeshFunctionSizet(mesh, mvc_cells)
I’m wondering if someone has already worked out a way to shortcut the mesh loading into MeshFunction
from gmsh
without having to write .msh
and .xdmf
file. Maybe something can be done with MeshEditor
, as suggested here, but I’m having troubles in understanding how I should translate this code into a gmsh
compatible code.