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],1.0,tol)
Initialize sub-domain instances
left = Left()
top = Top()
right = Right()
bottom = Bottom()
outside = Outside()
inside = Inside()
Define mesh
mesh = UnitCubeMesh(20,20,20)
Initialize mesh function for interior domains
domains = MeshFunction(‘size_t’,mesh,mesh.topology().dim())
domains.set_all(0)
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
dt=0.1
u_0= Constant(0.0)
g_L = Expression(“- 10*exp(- pow(x[1] - 0.5, 2))”,degree=2)
g_R = Constant(“1.0”)
f = Constant(0.0)
Define function space and basis functions
V = FunctionSpace(mesh, “CG”, 1)
u = TrialFunction(V)
v = TestFunction(V)
Define Dirichlet boundary conditions at top and bottom boundaries
bcs = [DirichletBC(V, 0.0,boundaries,2),
DirichletBC(V, 0.0, boundaries, 4),DirichletBC(V,0.0,boundaries,5),DirichletBC(V,0.0,boundaries,6)]
#interpolate initial condition
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)
Define variational form
a = uvdx + dtdot(grad(u), grad(v))dx
L = (u_n + dtf)vdx +g_Lvds(1)+g_Rv*ds(3)
Separate left and right hand sides of equation
u=Function(V)
file =File(“dfd.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)
Error [