This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to apply boundary conditions on subdomains in Fenics

0 votes

Hi all,

Please am having hard time applying the following boundary conditions on subdomains. Below is a sample of my code and the boundary conditions that I want to apply:

Am solving a system of nonlinear coupled pdes on the domain [-1,1]*[-1,1].
p1, p2 are scalar functions in 2d and U=(u1,u2), V=(v1,v2) are vector functions also in 2d.

Now I want to apply the following boundary conditions

Periodic condition on left and right
u1(-1,y)=u1(1,y)
u2(-1,y)=u2(1,y)

v1(-1,y)=v1(1,y)
v2(-1,y)=v2(1,y)

p1(-1,y)=p1(1,y)

p2(-1,y)=p1(1,y)

Dirichlet boundary condition for top and bottom for U=(u1,u2)
u1(x,-1) = -1 u1(x,1)= 1
u2(x,-1)= -1 u2(x,1)= 1

Periodic for V=(v1,v2) alone for top and bottom
v1(x,-1)= 0 v1(x,-1)= 0
v2(x,-1)= 0 v2(x,-1)=0

This is how my is code arranged.

from dolfin import *
mesh = RectangleMesh(Point(-1,-1),Point(1,1),200,200) #Domain[-1,1]*[-1,1]

V1= VectorFunctionSpace(mesh, 'CG', degree=2)
V2=VectorFunctionSpace(mesh, 'Lagrange', degree=2)
Q1 = FunctionSpace(mesh, 'CG', degree=1)
Q2 = FunctionSpace(mesh, 'CG', degree=1)
VQ = MixedFunctionSpace([V1,V2, Q1, Q2])

(U, V, p1,p2)= TrialFunctions(VQ)
(m,n, q, r) = TestFunctions(VQ)
H= Function(VQ)

Need to define boundary conditions here and put them in a list like

bcs=[ Boundary conditions]

Am not sure how to proceed. whether to define on each subspace or on each each subdomain. And if that how to do them. Could someone help me with this please.

F = (All the terms of the system have been added here)

F1 = action(F,H)
J = derivative(F1,H)
problem = NonlinearVariationalProblem(F1,H,bcs, J)
solver = NonlinearVariationalSolver(problem)
solver.solve()
(U,V, p1,p2) = H.split(deepcopy=True)

Any help is greatly appreciated. Thank you.

asked Nov 11, 2015 by Vivian FEniCS Novice (550 points)
edited Nov 11, 2015 by Vivian

1 Answer

+1 vote

The answer to your question is in the documentation: dirichlet and periodic boundary conditions.

Regards!

answered Nov 11, 2015 by hernan_mella FEniCS Expert (19,460 points)
edited Nov 11, 2015 by hernan_mella

Ok thank you hernan.

So I think I understand the periodic one and I did the following. For the Diritchlet am still not very sure since I have values for only some of the functions. Do I have to restrict it on the subspaces? Please help. Thanks.

Periodic Boundary conditions

class PeriodicBoundary(SubDomain):

# Left boundary is " target domain"
def inside(self, x, on_boundary):
    return bool(x[0] < DOLFIN_EPS and x[0]> -DOLFIN_EPS and on_boundary)

# map right boundary to left boundary
def map(self, y,x):
    y[0]=x[0] - 1.0
    y[1] = x[1]

pbc = PeriodicBoundary()

mesh = RectangleMesh(Point(-1,-1),Point(1,1),200,200) #Domain[-1,1]*[-1,1]

V1= VectorFunctionSpace(mesh, 'CG', degree=2, constrained_domain = pbc)
V2= VectorFunctionSpace(mesh, 'CG', degree=2, constrained_domain= pbc)
Q1 = FunctionSpace(mesh, 'CG', degree=1, constrained_domain = pbc )
Q2 = FunctionSpace(mesh, 'CG', degree=1, constrained_domain = pbc)
VQ = MixedFunctionSpace([V1,V2, Q1, Q2])

Thank you

For the Dirichlet Boundary condition. I don't understand what is going on but this is what I want to do:

I want to define a class such that

Bottom

U=(u1,u1): u1(x,-1)= -1, u2(x,-1)=0
V=(v1,v2): v1(x,-1)=-1, v2(x,-1) =0

Top

U=(u1,u1): u1(x,1)= 1, u2(x,1)=0
V=(v1,v2): v1(x,1)=1, v2(x,1) =0


class DirichletBoundary(SubDomain):
def inside(self, x, on_boundary):

{I am not sure of the statement below. My domain is [-1,1]*[-1,1]}

return bool((x[1] < DOLFIN_EPS or x[1] > (1.0 - DOLFIN_EPS)) \
and on_boundary)


And then call that class here as:

U0= Constant([-1,0])
V0= Constant([-1,0])
dbc = DirichletBoundary()
bc1= DirichletBC( VQ.sub(0), UB ,dbc ) # VQ.sub(0) is vector function space for U
bc2= DirichletBC(VQ.sub(1), ub ,dbc ) # VQ.sub(1) is vector function space for V

bcs = [bc1,bc2]

Please help. I spent so many hours trying to understand this but still not. Thank you.

The Dirichlet boundaries that you want to define are

class Top(SubDomain):
def inside(self, x, on_boundary):
    return (x[1] > 1 - DOLFIN_EPS and on_boundary)

class Bottom(SubDomain):
def inside(self, x, on_boundary):
    return (x[1] < -1 + DOLFIN_EPS and on_boundary)

The class Bottom (or Top) returns a boolean value: True if x is on the bottom (or top) boundary of the mesh (in this case, an object of the bottom class will mark all the points on the physical boundary of the mesh, with coordinates (x[0], x[1]), that satisfy the condition x[1] < -1 + DOLFIN_EPS). Of this manner, if V is a vector function space and u a function on V, which Dirichlet boundary condition on the bottom (top) boundary is equal to [-1, 0] (on all the top boundary), the way to implement this is:

u0  = Constant((-1, 0))
top = Top()
bc  = DirichletBC(V, u0, top)

If VQ.sub(0) = V1 and VQ.sub(1) = V2 () what you have done is ok.

Thank you very much.

...