Hello,
Given this mesh
I can successfully label in it the circle and the rectangle, save the mesh to file, and read the circle and rectangle. I prepared a minimal working example to illustrate the issue:
First, generate_mesh.py generates the mesh and saves it into line_mesh.xdmf and triangle_mesh.xdmf
#generate_mesh.py:
import numpy
import meshio
import gmsh
import pygmsh
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("resolution")
args = parser.parse_args()
resolution = (float)(args.resolution)
# Channel parameters
L = 2.2
h = 0.41
r = 0.05
c_r = [0.2, 0.2, 0]
geometry = pygmsh.geo.Geometry()
model = geometry.__enter__()
circle_r = model.add_circle(c_r, r, mesh_size=resolution/2)
rectangle_Lh = model.add_rectangle(0, L, 0, h, 0, mesh_size=resolution)
points = [model.add_point((0, 0, 0), mesh_size=5*resolution),
model.add_point((L, 0, 0), mesh_size=5*resolution),
model.add_point((L, h, 0), mesh_size=5*resolution),
model.add_point((0, h, 0), mesh_size=5*resolution)
]
channel_lines_1 = [model.add_line(points[0], points[1]),
model.add_line(points[1], points[2]),
model.add_line(points[2], points[3]),
model.add_line(points[3], points[0])]
channel_loop_1 = model.add_curve_loop(channel_lines_1)
plane_surface = model.add_plane_surface( rectangle_Lh.curve_loop, holes=[circle_r.curve_loop])
model.synchronize()
model.add_physical([plane_surface], "Volume")
model.add_physical(circle_r.curve_loop.curves, "Circle")
model.add_physical(rectangle_Lh.curve_loop.curves, "Rectangle")
# model.add_physical([channel_lines_1[3]], "Inflow")
geometry.generate_mesh(64)
gmsh.write("membrane_mesh.msh")
gmsh.clear()
geometry.__exit__()
mesh_from_file = meshio.read("membrane_mesh.msh")
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)
points = mesh.points[:, :2] if prune_z else mesh.points
out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={
"name_to_read": [cell_data]})
return out_mesh
line_mesh = create_mesh(mesh_from_file, "line", prune_z=True)
meshio.write("line_mesh.xdmf", line_mesh)
triangle_mesh = create_mesh(mesh_from_file, "triangle", prune_z=True)
meshio.write("triangle_mesh.xdmf", triangle_mesh)
Then mwe.py reads line_mesh.xdmf and triangle_mesh.xdmf and finds the circle and rectangle.
#mwe.py:
import math
from fenics import *
from mshr import *
import numpy as np
import meshio
import ufl as ufl
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("input_directory")
args = parser.parse_args()
L = 2.2
h = 0.41
r = 0.05
c_r = [0.2, 0.2]
#create mesh
mesh=Mesh()
with XDMFFile((args.input_directory) + "/triangle_mesh.xdmf") as infile:
infile.read(mesh)
mvc = MeshValueCollection("size_t", mesh, 2)
with XDMFFile((args.input_directory) + "/line_mesh.xdmf") as infile:
infile.read(mvc, "name_to_read")
mesh_coordinates = mesh.coordinates()
#read an object with label subdomain_id from xdmf file and assign to it the ds `ds_inner`
mf = dolfin.cpp.mesh.MeshFunctionSizet(mesh, mvc)
ds_circle = Measure("ds", domain=mesh, subdomain_data=mf, subdomain_id=2)
ds_rectangle = Measure("ds", domain=mesh, subdomain_data=mf, subdomain_id=3)
circle_perimeter = assemble(1*ds_circle)
rectangle_perimeter = assemble(1*ds_rectangle)
print("Circle perimeter = ", circle_perimeter)
print("Rectangle perimeter = ", rectangle_perimeter)
The circle and rectangle seem to be read correctly by mwe.py, because the values of their length are correct:
$ python3 generate_mesh.py 0.1
$ python3 mwe.py [folder where generate_mesh.py is stored]
Circle perimeter = 0.3078181289931019
Rectangle perimeter = 5.220000000000003
So far, so good. However, I would like to do the same thing that I did for the circle and the rectangle, but this time for a line. If I comment out # model.add_physical([channel_lines_1[3]], "Inflow")
in generate_mesh.py
in order to add the left edge of the rectangle, I get an error:
$ python3 generate_mesh.py 0.1
$ python3 mwe.py /home/fenics/shared/mesh/membrane_mesh
Traceback (most recent call last):
File "mwe.py", line 25, in <module>
infile.read(mvc, "name_to_read")
RuntimeError:
*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
*** fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error: Unable to find entity in map.
*** Reason: Error reading MeshValueCollection.
*** Where: This error was encountered inside HDF5File.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.1.0
*** Git changeset: 74d7efe1e84d65e9433fd96c50f1d278fa3e3f3f
*** -------------------------------------------------------------------------
May you please tell me what I am doing wrong ? Thank you