I have the following python code for solving the 2D heat equation. I can run this code for some number of iterations and save all the computations, then use FuncAnimation to animate the iterations. However, what I would prefer to do, is create an initial plot based on the initial conditions, then update this figure after each iteration. I am somewhat able to do this in the below code. However, I must close each figure in order for it to update and what I am seeking is the full animation of the code in the same figure without the need to close each figure after each time integration. I would then like the last figure to remain (not close.)
My current code is:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation
plate_length = 50
max_iter = 25
alpha = 2
delta_x = 1
delta_t = (delta_x ** 2)/(4 * alpha)
gamma = (alpha * delta_t) / (delta_x ** 2)
# Initialize solution: the grid of u(k, i, j)
u = np.empty((max_iter, plate_length, plate_length))
# Boundary conditions
u_top = 100.0
u_left = 0.0
u_bottom = 0.0
u_right = 0.0
# Set the initial condition
u.fill(0.)
# Set the boundary conditions
u[:, (plate_length-1):, :] = u_top
u[:, :, :1] = u_left
u[:, :1, 1:] = u_bottom
u[:, :, (plate_length-1):] = u_right
# Plot initial condition
cm = plt.pcolormesh(u[0], cmap=plt.cm.jet, vmin=0, vmax=100)
plt.show()
# Do the calculation here, update u for each k iteration
for k in range(0, max_iter, 1):
for i in range(1, plate_length-1, delta_x):
for j in range(1, plate_length-1, delta_x):
u[k + 1, i, j] = u[k][i][j] + \
gamma * (u[k][i+1][j] + u[k][i-1][j] + \
u[k][i][j+1] + u[k][i][j-1] - 4*u[k][i][j])
# Plot current value of u
cm = plt.pcolormesh(u[k], cmap=plt.cm.jet, vmin=0, vmax=100)
plt.show()
Any help you could provide would be greatly appreciated.
Thanks,