# -*- coding: utf-8 -*-
"""
Created on Tue Jan 20 14:32:23 2015
1D burges equation
@author: myjiayan
"""import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animationdef u_initial():first = np.ones(40)second = np.zeros(41)result = np.array(list(first)+list(second))return resultdef computeF(u):F = 0.5*u**2return Fdef maccormack(u,nt,dt,dx):un = np.zeros((nt,len(u)))un[0] = u.copy()ustar = u.copy()for t in xrange(1,nt):F = computeF(u)ustar[:-1] = u[:-1] - (F[1:]-F[:-1])*dt/dxustar[-1] = 0.0Fstar = computeF(ustar)un[t,1:] = 0.5*(u[1:]+ustar[1:]-(Fstar[1:]-Fstar[:-1])*dt/dx)un[t,0] = 1.0u = un[t].copy()return unif __name__ == '__main__':nx = 81nt = 70dx = 4.0/(nx-1)def animate(data):x = np.linspace(0,4,nx)y = dataline.set_data(x,y)return line,u = u_initial()sigma = .5dt = sigma*dxun = maccormack(u,nt,dt,dx)fig = plt.figure();ax = plt.axes(xlim=(0,4),ylim=(-.5,2));line, = ax.plot([],[],lw=2);anim = animation.FuncAnimation(fig, animate, frames=un, interval=50)plt.show()
直接将代码保存为burgers.py文件,打开terminal: $python burgers.py 数值结果就以动态的形式表现出来了。
本文发布于:2024-02-02 22:34:21,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170688446046884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |