Solving Laplace equation with time-dependent boundary condition


47
views
0
11 days ago by
Leo  
Hello
I want to solve a mixed space problem including a time-dependent equation and a Laplace equation. For the Laplace equation I want to apply a time-dependent Dirichlet boundary condition. Here is a part of the code:

Element1 = FiniteElement("CG", mesh.ufl_cell(), 1)

# Defining the mixed function space
W_elem = MixedElement([Element1, Element1])

W = FunctionSpace(mesh, W_elem)

z = Function(W)
dz=TrialFunction(W)

#Two variables
ALPHA, BETA = split(z)

(v_1, v_2) = TestFunctions(W)

# Time variables
dt = 0.05
t = 0
T = 0.5​

#Variational form for the time-dependent equation (variable ALPHA)
F1 = dt * dot(grad(ALPHA), grad(v_1)) * dx + ...

#Variational form for the Laplace equation (variable BETA)
F2 = dot(grad(BETA), grad(v_2)) * dx

u_D = Expression('10*a', degree = 1, a=0)
u_F = Expression(' 20*b', degree = 1,  b=0)

#Applying Dirichlet Boundary condition for the Laplace equation
bc_up = DirichletBC(W.sub(1),u_D, boundaries, 5)
bc_left = DirichletBC(W.sub(1),u_F, boundaries, 6)
bc_down = DirichletBC(W.sub(1),u_D, boundaries, 7)
bc_right = DirichletBC(W.sub(1),u_F, boundaries, 8)

bcs = [bc_up , bc_left ,bc_down , bc_right]

F= F1 + F2

f = File("BETA.pvd")

while t <= T:
    J = derivative(F, z, dz)
    problem = NonlinearVariationalProblem(F, z, bcs, J)
    solver = NonlinearVariationalSolver(problem)
    solver.solve()

    (ALPHA , BETA) = z.split(True)

    t += dt
    u_D.a += dt
    u_F.b += dt

    f << BETA

When I want to extract the results for the variable BETA  I expect to get a contour variable in time because the Laplace equation is solved with a new boundary condition at each time step but I get a uniform contour for all time steps in which variable BETA is zero in all over the domain. It seems like Dirichlet boundary conditions are not event updated in time.

How can I implement time-dependent (or variable) Dirichlet boundary condition for the Laplace equation?
Thanks!

Community: FEniCS Project

2 Answers


2
10 days ago by
I cannot reproduce this error. I completed your code into a MWE by adding a mesh and defining the boundaries:
from fenics import *
mesh = UnitSquareMesh(32,32)

Element1 = FiniteElement("CG", mesh.ufl_cell(), 1)

# Defining the mixed function space
W_elem = MixedElement([Element1, Element1])

W = FunctionSpace(mesh, W_elem)

z = Function(W)
dz=TrialFunction(W)

#Two variables
ALPHA, BETA = split(z)

(v_1, v_2) = TestFunctions(W)

# Time variables
dt = 0.05
t = 0
T = 0.5

#Variational form for the time-dependent equation (variable ALPHA)
F1 = dt * dot(grad(ALPHA), grad(v_1)) * dx

#Variational form for the Laplace equation (variable BETA)
F2 = dot(grad(BETA), grad(v_2)) * dx

u_D = Expression('10*a', degree = 1, a=0)
u_F = Expression(' 20*b', degree = 1,  b=0)

#Applying Dirichlet Boundary condition for the Laplace equation
def left(x, on_boundary):
    return near(x[0],0.) and on_boundary
    
def right(x, on_boundary):
    return near(x[0],1.) and on_boundary

def down(x, on_boundary):
    return near(x[1],0.) and on_boundary
        
def up(x, on_boundary):
    return near(x[1],1.) and on_boundary
bc_up = DirichletBC(W.sub(1),u_D, up)
bc_left = DirichletBC(W.sub(1),u_F, left)
bc_down = DirichletBC(W.sub(1),u_D, down)
bc_right = DirichletBC(W.sub(1),u_F, right)

bcs = [bc_up , bc_left ,bc_down , bc_right]

F= F1 + F2

f = File("BETA.pvd")

while t <= T:
    J = derivative(F, z, dz)
    problem = NonlinearVariationalProblem(F, z, bcs, J)
    solver = NonlinearVariationalSolver(problem)
    solver.solve()

    (ALPHA , BETA) = z.split(True)

    t += dt
    u_D.a += dt
    u_F.b += dt

    f << BETA​

My result at the end looks like this:

0
10 days ago by
Leo  
Thanks! I played with BC and it is working right now.
Please login to add an answer/comment or follow this question.

Similar posts:
Search »