Hi! I am having trouble in carrying over the boundary and the surface tags to a refined mesh. I tried some of the suggestions in previous posts but couldn’t make them work. In the following MWE, I created and meshed a disk with the boundary tagged 2 and the surface of the disk tagged 1. Then I did
mesh1 = refine(mesh)
But the refined mesh doesn’t preserve the tags of the coarser mesh.
from __future__ import print_function
from fenics import *
import matplotlib.pyplot as plt
from dolfin import *
import meshio
from dolfin import Mesh, XDMFFile, File, MeshValueCollection, cpp
# Optimization options for the form compiler
parameters["form_compiler"]["cpp_optimize"] = True
ffc_options = {"optimize": True, \
"eliminate_zeros": True, \
"precompute_basis_const": True, \
"precompute_ip_const": True}
import numpy as np
import meshio
from mshr import Circle, generate_mesh
from dolfin import Mesh, File, MeshFunction, Point, BoundaryMesh, SubDomain, plot, File
import matplotlib.pyplot as plt
from dolfin import *
from mshr import *
import numpy as np
class boundary(SubDomain):
def inside(self, x, on_boundary):
return on_boundary
class disk(SubDomain):
def inside(self, x, on_boundary):
return True
C = Circle(Point(0, 0), sqrt(3))
mesh = generate_mesh(C, 100)
boundary_nodes = BoundaryMesh(mesh, "exterior", True)
boundary_coordinates = boundary_nodes.coordinates()
print(len(boundary_coordinates))
boundary_markers = MeshFunction("size_t", mesh, mesh.topology().dim()-1, 0)
surface_markers = MeshFunction("size_t", mesh, mesh.topology().dim(), 0)
boundary().mark(boundary_markers, 2)
disk().mark(surface_markers, 1)
mesh1 = refine(mesh)
#bmarkers = adapt(mesh1,boundary_markers)
#smarkers = adapt(mesh1,surface_markers)
boundary_nodes = BoundaryMesh(mesh1, "exterior", True)
boundary_coordinates = boundary_nodes.coordinates()
print(len(boundary_coordinates))
ds = Measure('ds', subdomain_data=boundary_markers)
dx = Measure('dx', subdomain_data=surface_markers)
n = FacetNormal(mesh1)
W1 = FunctionSpace(mesh1, "Lagrange", 1)
n = FacetNormal(mesh1)
G , mu = 1, 0.1
u_D=Constant(0.0)
bc = DirichletBC(W1, u_D, boundary_markers, 2)
# Define variational problem
u = TrialFunction(W1)
v = TestFunction(W1)
f = Constant(-G/mu) # f=-G/mu
a = dot(grad(u), grad(v))*dx
L = -f*v*dx
# Compute solution
u = Function(W1)
solve(a == L, u, bc)
Error:
Error: Unable to create Dirichlet boundary condition.
*** Reason: User MeshFunction and FunctionSpace meshes are different.
I also tried (but didn’t work!)
bmarkers = adapt(mesh1,boundary_markers)
smarkers = adapt(mesh1,surface_markers)
hoping that boundary and surface markers of the coarser mesh will be carried over to the refined one. Also according to python.org, adapt(X,Y)" means “continue by using X according to protocol Y” (Reference: https://www.python.org/dev/peps/pep-0246/)
So is it that every time I refine a mesh, I have to redefine the markers? Is there a way to preserve the tags of the coarser mesh?