Simple heat problem also doesn't vary due to time

Hey everyone. I also did a very simple heat equation problem. However, When I plot all the solutions stored in the array. they are all the same and don’t vary. such as the plot of u_list[20] is same as u_list[40]. Any suggestions and advice will be appreciated!

from dolfin import *
import matplotlib.pyplot as plt

# Define 1D mesh and space
mesh = IntervalMesh(1000, 0, 1)
Element1 = FiniteElement("CG", mesh.ufl_cell(), 5)
E = FunctionSpace(mesh, Element1)


# Define functions
u_old = Function(E)
u_new = Function(E)
u_mid = 0.5*u_old + 0.5*u_new
q = TestFunction(E)
dt = 0.01

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


bc = DirichletBC(E, Constant(0), boundary)

#Initial Condition
u_init = Expression('x[0]*x[0]', degree=1)
u_old.interpolate(u_init)

# Weak statement of the equations
F = (u_new - u_old)/dt*q*dx+u_mid.dx(0)*q.dx(0)*dx

t = 0.0
T = 50*dt
u_list = []

while t < T:
    t += dt
    solve(F == 0, u_new, bc)
    u_old.vector()[:] = u_new.vector()
    u_list.append(u_new)


plot(u_list[49])
plt.show()

u_new is a reference to a dolfin.fem.Function. If you want to store the data at each time step you should copy it at each step.

Thank you very much for your reply. So it is mean that I only need to change u_new to another name such as u_2. Would you explain a bit more about copying? Like how can I copy it or what function I need to use. Sorry if the question is stupid I am pretty new to Fenics.

I saved each data in the array defined as u_list in my code. Each time I do this

u_list.append(u_new)

to copy and store each data at different times. However, when I am plotting the array. They are all the same, which really confused me.

This is related to basic Python and not FEniCS. I suggest reading any Python tutorial.

If you don’t care about that, try this quick fix (I don’t have old DOLFIN installed so I can’t check it):


while t < T:
    t += dt
    solve(F == 0, u_new, bc)
    u_old.vector()[:] = u_new.vector()
    u_copy = Function(E)
    u_copy.vector()[:] = u_new.vector()
    u_list.append(u_copy)

Thank you so much! I realize I need to learn more about python. I hope you are having a good day!