I tried to write the code of Poisson Equation .
-\Delta u = -4 on 0 <x< 1 , 0<y<2
u(x,0) = x^2, u(x,2)= (x-2)^2
u(0,y)=y^2, u(1,y)=(y-1)^2
I am new to Fenics, and don’t know how to define the different boundaries on each edge. Can anyone help me in this problem?
Can I define the boundary values by following way?
# Define boundary conditions
u_D_x_0 = Expression('x[0]*x[0]', degree=2)
def boundary(x, on_boundary):
if on_boundary:
return on_boundary and near(x[0],0)
bc = DirichletBC(V, u_D_x_0, boundary)
u_D_x_2 = Expression('(x[0]-2)*(x[0]-2)', degree=2)
def boundary(x, on_boundary):
return on_boundary and near(x[0],2)
bc = DirichletBC(V, u_D_x_2, boundary)
u_D_0_y = Expression('x[1]*x[1]', degree=2)
def boundary(x, on_boundary):
return on_boundary and near(0,x[1])
bc = DirichletBC(V, u_D_0_y, boundary)
u_D_1_y = Expression('(x[1]-1)*(x[1]-1)', degree=2)
def boundary(x, on_boundary):
return on_boundary and near(2,x[1])
bc = DirichletBC(V, u_D_0_y, boundary)
dokken
4
As you keep on redefining variables in the code above, you cannot use it. I would suggest something like the following:
from dolfin import *
mesh = RectangleMesh(Point(0, 0), Point(1, 2), 10, 10)
V = FunctionSpace(mesh, "CG", 1)
def top_boundary(x, on_boundary):
return on_boundary and near(2, x[1])
def bottom_boundary(x, on_boundary):
return on_boundary and near(0, x[1])
def left_boundary(x, on_boundary):
return on_boundary and near(x[0], 0)
def right_boundary(x, on_boundary):
return on_boundary and near(x[0], 1)
# Define boundary conditions
u_D_x_0 = Expression('x[0]*x[0]', degree=2)
u_D_x_2 = Expression('(x[0]-2)*(x[0]-2)', degree=2)
u_D_0_y = Expression('x[1]*x[1]', degree=2)
u_D_1_y = Expression('(x[1]-1)*(x[1]-1)', degree=2)
bc_top = DirichletBC(V, u_D_x_2, top_boundary)
bc_bottom = DirichletBC(V, u_D_x_0, bottom_boundary)
bc_left = DirichletBC(V, u_D_0_y, left_boundary)
bc_right = DirichletBC(V, u_D_1_y, right_boundary)
bcs = [bc_left, bc_right, bc_bottom, bc_top]