In [1]:
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.animation as manimation
In [2]:
r = 1
r2 = r ** 2
c = 1.0
t0 = 0
tf = 0.5
dt = 0.01
t = np.arange(t0, tf+dt, dt)
len_t = len(t)
x0 = 0
xf = 1
dx = c * dt / r
x = np.arange(x0, xf+dx, dx)
len_x = len(x)
y0 = 0
yf = 1
dy = dx
y = np.arange(y0, yf+dy, dy)
len_y = len(y)
z = np.zeros((len(x), len(y), len(t)))
zc = np.zeros((len(x), len(y)))
zc[0, :] = np.ones(len(zc[0, :]))
zc[len(x)-1, :] = np.ones(len(zc[len(x)-1, :]))
zc[:, 0] = np.ones(len(zc[:, 0]))
zc[:, len(y)-1] = np.ones(len(zc[:, len(y)-1]))
In [3]:
k1 = 1000
k2 = 500
xc1 = 0.3
xc2 = 0.6
In [4]:
for i in range(1, len_x-1):
for j in range(1, len_y-1):
if(zc[i,j] != 1):
x_i = i * dx
y_i = j * dy
z[i, j, 0] = np.exp(-k1 * (x_i - xc1) ** 2) + np.exp(-k1 * (y_i - xc2) ** 2)
In [5]:
for k in range(0, len_t-1):
for i in range(0, len_x):
for j in range(0, len_y):
if zc[i,j] == 1:
continue
if k > 0:
zp = z[i,j,k-1]
else:
zp = 0
(t_i, x_i, y_i) = (k*dt, i*dx, j*dy)
z[i, j, k+1] = r2 * (z[i+1,j,k] + z[i-1,j, k] + z[i,j+1,k] + z[i, j-1, k]) - zp + 2*(1-2*r2)*z[i, j, k]
if k % 25 == 0:
print(len_t - k)
In [6]:
plt.matshow(z[:,:,])
plt.colorbar()
plt.show()
In [14]:
FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='Movie Test', artist='Matplotlib',
comment='Movie support!')
writer = FFMpegWriter(fps=4, metadata=metadata)
fig = plt.figure()
ax = fig.add_subplot(111)
with writer.saving(fig, "writer_test.mp4", 60):
for i in range(len_t):
ax.matshow(z[:,:,i])
plt.clim(-5, 5)
writer.grab_frame()
In [ ]:
In [ ]: