12步解N-S方程
本系列教程是美国Prof. Lorena教授的网络教程。学习的前提是你有偏微分方程、计算流体力学以及Python的基础知识。
先看一个完成学习后所编程序执行后Cavity flow的数值解 :
Cavity flow solution at Reynolds number of 200 with a 41x41 mesh.
Python源代码:
# -*- coding: utf-8 -*- #This is step 1 of python cfd courses. #Text and code provided under a Creative Commons Attribution license, CC-BY #. (c) Lorena A. Barba, 2013. Thanks: Gilbert Forsyth for help writing the #notebooks. NSF for support via CAREER award #1149784. # # date author description # 2014/08/31 wubin 1-d linear convection # # pu/pt+c*pu/px=0 # a little equation can teach us so much!!!! # import numpy as np import matplotlib.pyplot as plt import time,sys #load some system utilities from IPython.core.display import clear_output #used for inline animation # #gridgen part nx=41 dx=2.0/(nx-1) nt=25 #total timesteps,like iterations dt=0.025 # c=1.0 #assume wavespeed is 1.0 # #initialize part #give every nx vertex a initial value u=np.ones(nx) u[0.5/dx:1.0/dx+1]=2 #slice only 1.0/dx,but not 1.0/dx+1 #print u # #plt.figure() #plt.plot(np.linspace(0,2,nx),u) #plt.show() u0=np.empty_like(u) u0=u.copy() # #iteration part un=np.ones(nx) #un is a temporary array #python's array begins from 0,but why not slice the first element?because in #iteration ,there is a Ui-1,0-1 is -1,will out of a array bounds. for n in range(nt): un=u.copy() #copy the intial values from u to un for i in range(1,nx): u[i]=un[i]-c*dt/dx*(un[i]-un[i-1]) plt.figure() xc=np.linspace(0,2,nx) plt.plot(xc,u,linewidth=2.0,label="converged") plt.plot(xc,u0,linewidth=2.0,label="initial") plt.legend(loc='upper right') plt.savefig("F:\PYTHON\python_cfd\comparision.png",dpi=150) plt.show()
运行结果如下图所示。