Heat equation with time dependent Dirichlet bcs

Hello, I am trying to find forward solution of unsteady heat equation with time dependent boundary conditions for four edges of a square domain. I am getting error in solve function . Do you have any idea, please?

from fenics import *
from fenics_adjoint import *
from collections import OrderedDict

mesh = RectangleMesh(Point(1,1), Point(2,2),5,5)
plot(mesh)

T=1
dt = Constant(0.1)
t = float(dt)

u_0 = Expression(’ x[0] * x[0] + x[1] * x[1] + sin(10t) ‘, t=0, degree=2)
f = Expression(’-6
(x[0] + x[1]) + 10 * cos(10 * t)‘, t=0, degree=2)
kappa = Expression(’ x[0] + x[1]', degree=1)

def boundary_L(x, on_boundary):
tol = 1E-14
return on_boundary and near(x[0], 1, tol)

def boundary_R(x, on_boundary):
tol = 1E-14
return on_boundary and near(x[0], 2, tol)

def boundary_B(x, on_boundary):
tol = 1E-14
return on_boundary and near(x[1], 1, tol)

def boundary_T(x, on_boundary):
tol = 1E-14
return on_boundary and near(x[1], 2, tol)

u_L= Expression(‘1 + x[1]*x[1] + s’, s=s, degree=2)
u_R= Expression(‘4 + x[1]*x[1] + s’, s=s, degree=2)
u_B= Expression('x[0]*x[0] + 1 + s ', s=s, degree=2)
u_T = Expression(‘x[0]*x[0] + 4 + s’, s=s, degree=2)

V = FunctionSpace(mesh, “CG”, 1)
u = TrialFunction(V)
v = TestFunction(V)
kappa = interpolate(kappa, V)
F = ( (u - u_0) /dt * v +inner(kappa * grad(u), grad(v)) - f*v)*dx
u=Function(V)

while t <= T:

s = sin(10*t) 

bc_L = DirichletBC(V, u_L, boundary_L)
bc_R = DirichletBC(V, u_R, boundary_R)
bc_B = DirichletBC(V, u_B, boundary_B)
bc_T = DirichletBC(V, u_T, boundary_T)

bcs = [bc_L, bc_R, bc_B, bc_T]

solve(F==0, u, bcs)

t += float(dt)

plot(u)

As you are solving your problem on residual form,

The u used in the variational formulation F should be a Function, not a TrialFunction

Thank you Dokken, ı changed it and add initial s value. the last script is below but still not working. Do you think the bcs generating is ok?

from fenics import *

mesh = RectangleMesh(Point(1,1), Point(2,2),5,5)
plot(mesh)

T=1
dt = Constant(0.1)
t = float(dt)
u_0 = Expression(“x[0] * x[0] + x[1] * x[1] + sin(10 * t)”, t=0, degree=2)
f = Expression(“-6 * (x[0] + x[1]) + 10 * cos(10*t)”, t=0, degree=2)
kappa = Expression(’ x[0] + x[1]', degree=1)

def boundary_L(x, on_boundary):
tol = 1E-14
return on_boundary and near(x[0], 1, tol)

def boundary_R(x, on_boundary):
tol = 1E-14
return on_boundary and near(x[0], 2, tol)

def boundary_B(x, on_boundary):
tol = 1E-14
return on_boundary and near(x[1], 1, tol)

def boundary_T(x, on_boundary):
tol = 1E-14
return on_boundary and near(x[1], 2, tol)
s=0
u_L= Expression(‘1 + x[1] * x[1] + s’, s=s, degree=2)
u_R= Expression(‘4 + x[1]*x[1] + s’, s=s, degree=2)
u_B= Expression('x[0]*x[0] + 1 + s ', s=s, degree=2)
u_T = Expression(‘x[0]*x[0] + 4 + s’, s=s, degree=2)

V = FunctionSpace(mesh, “CG”, 1)
v = TestFunction(V)
u = Function(V)
kappa = interpolate(kappa, V)

F = ( (u - u_0)/dt*v +inner(kappa * grad(u), grad(v)) - f * v)*dx

while t <= T:

s = sin(10*t) 


bc_L = DirichletBC(V, u_L, boundary_L)
bc_R = DirichletBC(V, u_R, boundary_R)
bc_B = DirichletBC(V, u_B, boundary_B)
bc_T = DirichletBC(V, u_T, boundary_T)

bcs = [bc_L, bc_R, bc_B, bc_T]

solve(F==0, u, bcs)

t += float(dt)

plot(u)

Running your code does not produce any error.
Please make sure that your code can be copy-pasted into a text editor and run without any extra formatting. Currently one has to do quite a few manipulations (indentations and changes of strings) to make it run.

Please supply what error message you obtain.