Converting The Heat Equation function into a time dependent function

Hello,

I would like to convert the heat equation into a time dependent function. Please how would I do this ? I have found this code used in firedrakeproject for time dependent boundaries but not sure if it would serve any purpose in fenics.

c = Constant(sin(t))
bc = DirichletBC(V, 0, boundary)
while T_initial < T_final:
solve(F == 0, bcs=[bc])
T_initial += dt
c.assign(sin(T_initial))

As for my regular code, I used the fenics heat equation code and made a few edits which I have provided below.

Thanks,

FEniCS tutorial demo program: Diffusion of a Gaussian hill.

u'= Laplace(u) + f  in a square domain
u = u_D             on the boundary
u = u_0             at t = 0

u_D = f = 0
The initial condition u_0 is chosen as a Gaussian hill.

from dolfin import *
from mshr import *
from fenics import *
import time
#  Define time things.

T_initial = 0.0 # initial time
T_final = 2 # final time
num_steps = 50     # number of time steps
dt = ( T_final - T_initial ) / num_steps # time step size


#Making a cylindrical geometry 10 cm radius and 15 cm height in S.I

cylinder = Cylinder(Point(0, 0, -7.5), Point(0, 0, 7.5), 5, 5)
domain = cylinder


# Making Mesh ( THe value corresponds to the mesh density)
mesh = generate_mesh(domain, 15) # generates 3D model of the cylindrical geometry in x, y, and z                 
axes. 
V = FunctionSpace(mesh, 'P', 1)


# Define boundary condition
def boundary(x, on_boundary):
return on_boundary

# Boundary Conditions 
bc = DirichletBC(V, Constant(0), boundary)

# Definining the intial temperature. 300 K
u_0 = Constant('300')
a = 5

# Expression('300', degree=2, a=5)
u_n = interpolate(u_0, V)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0)

         
# ( not sure what this really represents ) turn the F function as time dependent function 
F = u*v*dx + dt*dot(grad(u), grad(v))*dx - (u_n + dt*f)*v*dx
a, L = lhs(F), rhs(F)

# Create VTK file for saving solution
vtkfile = File('heat_gaussian/solution.pvd')


# Time-stepping
u = Function(V)
T_inital = 0
for n in range(num_steps):

# Update current time
T_inital += dt

# Compute solution
solve(a == L, u, bc)

# Save to file and plot solution
vtkfile << (u, T_inital)
plot(u)

# Update previous solution
u_n.assign(u)

import matplotlib.pyplot as plt

plt.show()

‘’’

Please format your code appropriately by encapsulated it with ```.

You can find examples of the heat equation at for instance:
http://www.dolfin-adjoint.org/en/latest/documentation/klein/klein.html

1 Like

I just did some indentations so the code can be better seen. Also thanks for the link.