Hi, I was trying to apply the above code to my own mesh/problem. The mesh I want to work with is test3.msh, which is giving me the error:
cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
File "/usr/local/lib/python3.8/dist-packages/meshio/_mesh.py", line 213, in get_cell_data
return np.concatenate(
File "<__array_function__ internals>", line 5, in concatenate
ValueError: need at least one array to concatenate
Since I couldn’t figure out, what the problem is, I started working with a simpler mesh, test4.msh.
I can read this mesh without any problems. However, applying the DirichletBC gives the following answer:
*** Warning: Found no facets matching domain for boundary condition.
Here I also tried renaming “top” to “0” in the mesh file.
So my questions are:
- Why is create_mesh(test3.msh) causing and error while test4.msh is not?
- How do I access the boundary correctly using the boundary marker/tag/label?
Thank You.
The code and .msh files are listed below. Both are generated in gmsh and exported as .msh-files
from dolfin import cpp
from fenics import *
from vtkplotter.dolfin import plot as dolfin_plot
import meshio
from ufl import nabla_div
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
msh = meshio.read("test4.msh")
print(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)
#ds = Measure('ds', domain=mesh, subdomain_data=mf)
#ds_custom = Measure("ds", domain=mesh, subdomain_data=mf)
#print(assemble(1*ds_custom(1)), assemble(1*ds_custom(2)))
#dolfin_plot(mf)
#boundaries = MeshFunction("size_t", mesh, dim=0)
#dolfin_plot(boundaries)
# Define function space for system of PDEs
degree = 2
lambda_ = 1
mu = 1
V = VectorFunctionSpace(mesh, 'P', degree)
# Define boundary conditions
tol = 1E-14
def clamped_boundary(x, on_boundary): # beam is only fixed on the left end
return on_boundary and near(x[0], 0, tol)#x[0] < tol
def force_on_rhs_boundary(x, on_boundary):
return on_boundary and near(x[0], 1.0, tol)
# https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/93
# Boundary conditions
bc1 = DirichletBC(V, Constant((0, 1)), mf, 1)
#bc3 = DirichletBC(V, Constant((0, 0)), clamped_boundary)
#bc2 = DirichletBC(V, Constant((1, 0)), force_on_rhs_boundary)
# Combine dirichlet boundary conditions
bcs = [bc1] #, bc2, bc3]
# Define strain and stress
def epsilon(u): # strain
return 0.5*(nabla_grad(u) + nabla_grad(u).T)
# return sym(nabla_grad(u))
def sigma(u): # stress
return lambda_*nabla_div(u)*Identity(d) + 2*mu*epsilon(u)
# Define variational problem
u = TrialFunction(V)
d = u.geometric_dimension() # space dimension
v = TestFunction(V)
f = Constant((0, 0))
T = Constant((0, 0))
a = inner(sigma(u), epsilon(v)) * dx
L = dot(f, v) * dx + dot(T, v) * ds
# Compute solution
u = Function(V)
solve(a == L, u, bcs)
# Plot solution
print('sol u')
dolfin_plot(u, title='Displacement', mode='displacement')
test4.msh:
$MeshFormat
4.1 0 8
$EndMeshFormat
$PhysicalNames
1
1 8 "top"
$EndPhysicalNames
$Entities
6 7 2 0
1 0 0 0 0
2 1 0 0 0
3 0 1 0 0
4 1 1 0 0
5 0 0.5 0 0
6 1 0.5 0 0
1 0 0.5 0 0 1 0 1 8 2 3 -5
2 0 1 0 1 1 0 2 3 8 2 3 -4
3 1 0.5 0 1 1 0 2 4 8 2 4 -6
4 0 0.5 0 1 0.5 0 1 8 2 5 -6
5 0 0 0 0 0.5 0 0 2 5 -1
6 1 0 0 1 0.5 0 1 4 2 6 -2
7 0 0 0 1 0 0 0 2 2 -1
1 0 0.5 0 1 1 0 1 1 4 1 4 -3 -2
2 0 0 0 1 0.5 0 1 2 4 7 -5 4 6
$EndEntities
$Nodes
13 8 1 8
0 1 0 1
1
0 0 0
0 2 0 1
2
1 0 0
0 3 0 1
3
0 1 0
0 4 0 1
4
1 1 0
0 5 0 1
5
0 0.5 0
0 6 0 1
6
1 0.5 0
1 1 0 0
1 2 0 0
1 3 0 0
1 4 0 0
1 6 0 0
2 1 0 1
7
0.4999999999999999 0.75 0
2 2 0 1
8
0.4999999999999999 0.25 0
$EndNodes
$Elements
7 13 1 13
1 1 1 1
1 3 5
1 2 1 1
2 3 4
1 3 1 1
3 4 6
1 4 1 1
4 5 6
1 6 1 1
5 6 2
2 1 2 4
6 6 4 7
7 3 5 7
8 4 3 7
9 5 6 7
2 2 2 4
10 6 2 8
11 1 5 8
12 2 1 8
13 5 6 8
$EndElements
test3.msh was too big, I will post it in another answer or give a link.
Edit: test3.msh can be found here:
https://raw.githubusercontent.com/lukasschulth/EBZ/master/coding/Elasticity%20Problems/test3.msh?token=AJJXEY7LSDWXIBCB6AI7JNTARJ4IA