0

I want to create a simple animation of how bad the Forward Time Central Space (FTCS) solves the flux conservation equation for a Gaussian velocity distribution ("Physics... yeah!"). I have written a small animation based on this tutorial. I have attached the code below. I'm satisfied with it (given that I don't really know anything about matplotlib's animation package), but I cannot get the animation to be slow enough so that I can actually see something.

This boils down to me not understanding how to set the parameters in the animation.FuncAnimation in the last line of the code. Could anybody explain, help?

import math
import numpy as np
import scipy as sci
import matplotlib.pyplot as plt
from matplotlib import animation

#generate velocity distribution
sigma = 1.
xZero = 0.
N = 101
x = np.linspace(-10,10,N)
uZero = 1. / math.sqrt(2 * math.pi * (sigma**2)) * np.exp(-0.5*((x - xZero)/(2*sigma))**2)


v = 1
xStep = x[2]-x[1]
tStep = 0.1
alpha = v * tStep/xStep * 0.5

#include boundary conditions
u = np.hstack((0.,uZero,0.))
uNext = np.zeros(N + 2)

#solve with forward time central space and store each outer loop in data
#so it can be used in the animation
data = []
data.append(u[1:-1])
for n in range(0,100):
    for i in range(1,N+1):
        uNext[i] = -alpha * u[i+1] + u[i] + alpha*u[i-1] 
    u = uNext
    data.append(u[1:-1])
data = np.array(data)

#launch up the animation
fig = plt.figure()
ax = plt.axes(xlim=(-10,10),ylim=(-1,1))
line, = ax.plot([],[],lw=2)

def init():
    line.set_data([],[])
    return line,

#get the data for animation from the data array
def animate(i):
    y = data[i]
    line.set_data(x,y)
    return line,

#the actual animation
anim = animation.FuncAnimation(fig,animate,init_func=init,frames=200,interval=2000,blit=True)

plt.show()
4

1 に答える 1

1

最初にデータ変数を調べます。コードを実行すると、data[0] と data[1] のみが異なり、data[1] 以降のすべてのデータ (フレーム) は同じです。

np.allclose(data[1], data[100])
True
于 2013-05-17T12:38:01.070 に答える