from dolfin import *
Create classes for defining parts of the boundaries and the interior
of the domain
class Left(SubDomain):
def inside(self, x, on_boundary):
tol= 1E-15
return near(x[0], 0.0,tol)
class Right(SubDomain):
def inside(self, x, on_boundary):
tol= 1E-15
return near(x[0], 1.0,tol)
class Bottom(SubDomain):
def inside(self, x, on_boundary):
tol= 1E-15
return near(x[2], 0.0,tol)
class Top(SubDomain):
def inside(self, x, on_boundary):
tol= 1E-15
return near(x[2], 1.0,tol)
class Outside(SubDomain):
def inside(self, x, on_boundary):
tol= 1E-15
return near(x[1],1.0,tol)
class Inside(SubDomain):
def inside(self, x, on_boundary):
tol= 1E-15
return near(x[1],0.0,tol)
class Subdomain1(SubDomain):
def inside(self, x, on_boundary):
return (between(x[0], (0.2,0.5)) , between(x[1], (0.4, 0.5)) and between(x[2],(0.7,0.9)))
Initialize sub-domain instances
left = Left()
top = Top()
right = Right()
bottom = Bottom()
inside = Inside()
outside = Outside()
subdomain1 = Subdomain1()
Define mesh
mesh = UnitCubeMesh(15,15,15)
Initialize mesh function for interior domains
subdomains = MeshFunction(‘size_t’,mesh,mesh.topology().dim())
subdomains.set_all(0)
subdomain1.mark(subdomains,1) #IN THIS LINE IT IS SHOWING ERROR
Initialize mesh function for boundary domains
boundaries = MeshFunction(‘size_t’,mesh,mesh.topology().dim()-1)
boundaries.set_all(0)
left.mark(boundaries, 1)
top.mark(boundaries, 2)
right.mark(boundaries, 3)
bottom.mark(boundaries, 4)
outside.mark(boundaries, 5)
inside.mark(boundaries, 6)
Define input data
a0 = Constant(1.0)
a1 = Constant(0.01)
g_L = Expression(“- 10*exp(- pow(x[1] - 0.5, 2))”,degree=2)
g_R = Constant(“1.0”)
f = Constant(1.0)
u_n = Constant(0.0)
dt= 0.1
u_0 = Constant(0.0)
Define function space and basis functions
V = FunctionSpace(mesh, “CG”, 2)
u = TrialFunction(V)
v = TestFunction(V)
Define Dirichlet boundary conditions at top and bottom boundaries
bcs = [DirichletBC(V, 5.0, boundaries, 2),DirichletBC(V, 5.0, boundaries, 4),DirichletBC(V, 5.0, boundaries, 5),
DirichletBC(V, 0.0, boundaries, 6)]
#interpolate
u_n = interpolate(u_0, V)
Define new measures associated with the interior domains and
exterior boundaries
ds = Measure(‘ds’, domain=mesh, subdomain_data=boundaries)
dx = Measure(‘dx’, domain=mesh,subdomain_data=domains)
Define variational form
a = a0uvdx(0) + a1uvdx(1) + a0dtdot(grad(u),grad(v))dx(0) +a1dtdot(grad(u),grad(v))dx(1)
L = a0(u_n + dtf)vdx(0) + a1*(u_n + dtf)vdx(1) + g_Lvds(1) + g_Rv*ds(3)
Solve problem
u = Function(V)
file =File(“dtd.pvd”)
t = 0
T=50*dt
while (t<T):
# Update current time
t += dt
# Compute solution
solve(a == L, u, bcs)
# Update previous solution
u_n.assign(u)
file << (u,t)