find a polynomial \(pi(x)\), of degree at most k − 1, for each cell \(I_{i}\), such that it is a k-th order accurate
approximation to the function \(v(x)\) inside \(I_{i}\):
\[
p_{i}(x)=v(x)+O(\Delta x^{k}),\quad x \in I_{i},\quad i=1,\cdots,N.
\]
In particular, this gives approximations to the function v(x) at the cell boundaries
Given the location \(I_{i}\) and the order of accuracy \(k\), we first choose a “stencil”, based on \(r\) cells to the
left, \(s\) cells to the right, and \(I_{i}\) itself if \(r, s ≥ 0\), with \(r + s +1= k\):
\[
S(i)\equiv \{I_{i-r},\cdots,I_{i+s}\}
\]
There is a unique polynomial of degree at most \(k−1 = r+s\), denoted by \(p(x)\) (we will drop the subscript
i when it does not cause confusion), whose cell average in each of the cells in \(S(i)\) agrees with that of \(v(x)\):
Given the location \(I_i\) and the order of accuracy \(k\), we first choose a “stencil”, based on \(r\) cells to the
left, \(s\) cells to the right, and \(I_i\) itself if \(r, s ≥ 0\), with \(r + s +1= k\):
importnumpyasnpimportmatplotlib.pyplotaspltdefplot_all_cell_center(xcc,yref):plt.scatter(xcc,np.full_like(xcc,yref),s=20,facecolor='black',edgecolor='black',linewidth=1)returndefplot_cell_center(xcc,yref):nx=xcc.sizeii=nx//2im=ii-1ip=ii+1xcc_new=[]foriinrange(0,nx):ifi>0andi<im:continueifi>ipandi<nx-1:continuexcc_new.append(xcc[i])plt.scatter(xcc_new,np.full_like(xcc_new,yref),s=20,facecolor='black',edgecolor='black',linewidth=1)returndefplot_mesh(x,yref):dx=x[1]-x[0]dy=0.1*dxforxminx:plt.plot([xm,xm],[yref-dy,yref+dy],'k-')# 绘制垂直线#nxc=x.size-1ii=nxc//2im=ii-1im1=ii-2ip=ii+1ip1=ii+2foriinrange(0,nxc):ifi>0andi<im1:plt.plot([x[i],x[i+1]],[yref,yref],'k--',linewidth=1)elifi>ip1andi<nx-1:plt.plot([x[i],x[i+1]],[yref,yref],'k--',linewidth=1)else:plt.plot([x[i],x[i+1]],[yref,yref],'b-',linewidth=1)#plt.plot(x, np.full_like(x, yref), 'k--', linewidth=1)returndefplot_label(x,xcc,yref):dx=x[1]-x[0]dyb=0.5*dxdyt=dyb*0.6yb=yref-dybyt=yref+dytybc=yref-0.5*dybplt.text(x[0],yb,r'$x_{i-r-\frac{1}{2}}$',fontsize=12,ha='center')plt.text(x[1],yb,r'$x_{i-r+\frac{1}{2}}$',fontsize=12,ha='center')plt.text(x[2],yb,r'$x_{i-\frac{5}{2}}$',fontsize=12,ha='center')plt.text(x[3],yb,r'$x_{i-\frac{3}{2}}$',fontsize=12,ha='center')plt.text(x[4],yb,r'$x_{i-\frac{1}{2}}$',fontsize=12,ha='center')plt.text(x[5],yb,r'$x_{i+\frac{1}{2}}$',fontsize=12,ha='center')plt.text(x[6],yb,r'$x_{i+\frac{3}{2}}$',fontsize=12,ha='center')plt.text(x[7],yb,r'$x_{i+\frac{5}{2}}$',fontsize=12,ha='center')plt.text(x[8],yb,r'$x_{i+s-\frac{1}{2}}$',fontsize=12,ha='center')plt.text(x[9],yb,r'$x_{i+s+\frac{1}{2}}$',fontsize=12,ha='center')plt.text(xcc[0],ybc,r'$i-r$',fontsize=12,ha='center')plt.text(xcc[1],ybc,r'$\cdots\cdots$',fontsize=12,ha='center')plt.text(xcc[-2],ybc,r'$\cdots\cdots$',fontsize=12,ha='center')plt.text(xcc[-1],ybc,r'$i+s$',fontsize=12,ha='center')nx=xcc.sizei=nx//2print("i=",i)im=i-1im1=i-2ip=i+1ip1=i+2plt.text(xcc[im1],ybc,r'$i-2$',fontsize=12,ha='center')plt.text(xcc[im],ybc,r'$i-1$',fontsize=12,ha='center')plt.text(xcc[i],ybc,r'$i$',fontsize=12,ha='center')plt.text(xcc[ip],ybc,r'$i+1$',fontsize=12,ha='center')plt.text(xcc[ip1],ybc,r'$i+2$',fontsize=12,ha='center')return# 设置字体为 Times New Romanplt.rc('text',usetex=True)plt.rc('font',family='serif',serif=['Times New Roman'])# 设置图形大小和样式plt.figure(figsize=(12,5))nx=9L=1.0x_l=0.0dx=L/nxx=np.zeros(nx+1,dtype=np.float64)xcc=np.zeros(nx,dtype=np.float64)foriinrange(0,nx+1):x[i]=x_l+dx*(i)foriinrange(0,nx):xcc[i]=0.5*(x[i]+x[i+1])print("x=",x)print("xcc=",xcc)yref=0.0plot_cell_center(xcc,yref)plot_mesh(x,yref)plot_label(x,xcc,yref)plt.axis('equal')plt.axis('off')plt.savefig('cfd.png',bbox_inches='tight',dpi=300)plt.show()
importnumpyasnpimportmatplotlib.pyplotaspltdefplot_all_cell_center(xcc,yref):plt.scatter(xcc,np.full_like(xcc,yref),s=20,facecolor='black',edgecolor='black',linewidth=1)returndefplot_cell_center(xcc,yref):nx=xcc.sizeii=nx//2im=ii-1ip=ii+1xcc_new=[]foriinrange(0,nx):ifi>1andi<im:continueifi>ipandi<nx-2:continuexcc_new.append(xcc[i])plt.scatter(xcc_new,np.full_like(xcc_new,yref),s=20,facecolor='black',edgecolor='black',linewidth=1)returndefplot_mesh(x,yref):dx=x[1]-x[0]dy=0.1*dxforxminx:plt.plot([xm,xm],[yref-dy,yref+dy],'k-')# 绘制垂直线#nxc=x.size-1ii=nxc//2im=ii-1ip=ii+1foriinrange(0,nxc):ifi>1andi<im:plt.plot([x[i],x[i+1]],[yref,yref],'k--',linewidth=1)elifi>ipandi<nx-2:plt.plot([x[i],x[i+1]],[yref,yref],'k--',linewidth=1)else:plt.plot([x[i],x[i+1]],[yref,yref],'b-',linewidth=1)#plt.plot(x, np.full_like(x, yref), 'k--', linewidth=1)returndefplot_label(x,xcc,yref):x0=x[0]dx=x[1]-x[0]dyb=0.5*dxdyt=dyb*0.6yb=yref-dybyt=yref+dytybc=yref-0.5*dybplt.text(x[0],yb,r'$x_{\frac{1}{2}}$',fontsize=12,ha='center')plt.text(x[1],yb,r'$x_{\frac{3}{2}}$',fontsize=12,ha='center')plt.text(x[-2],yb,r'$x_{N-\frac{1}{2}}$',fontsize=12,ha='center')plt.text(x[-1],yb,r'$x_{N+\frac{1}{2}}$',fontsize=12,ha='center')plt.text(x[0],yt,r'$x=0$',fontsize=12,ha='center')plt.text(x[-1],yt,r'$x=L$',fontsize=12,ha='center')plt.text(xcc[0],ybc,r'$i=1$',fontsize=12,ha='center')plt.text(xcc[1],ybc,r'$2$',fontsize=12,ha='center')plt.text(xcc[-2],ybc,r'$N-1$',fontsize=12,ha='center')plt.text(xcc[-1],ybc,r'$N$',fontsize=12,ha='center')nx=xcc.sizei=nx//2print("i=",i)im=i-1ip=i+1plt.text(xcc[im],ybc,r'$i-1$',fontsize=12,ha='center')plt.text(xcc[i],ybc,r'$i$',fontsize=12,ha='center')plt.text(xcc[ip],ybc,r'$i+1$',fontsize=12,ha='center')return# 设置字体为 Times New Romanplt.rc('text',usetex=True)plt.rc('font',family='serif',serif=['Times New Roman'])# 设置图形大小和样式plt.figure(figsize=(12,5))nx=9L=1.0x_l=0.0dx=L/nxx=np.zeros(nx+1,dtype=np.float64)xcc=np.zeros(nx,dtype=np.float64)foriinrange(0,nx+1):x[i]=x_l+dx*(i)foriinrange(0,nx):xcc[i]=0.5*(x[i]+x[i+1])print("x=",x)print("xcc=",xcc)yref=0.0plot_cell_center(xcc,yref)plot_mesh(x,yref)plot_label(x,xcc,yref)plt.axis('equal')plt.axis('off')plt.savefig('cfd.png',bbox_inches='tight',dpi=300)plt.show()
importnumpyasnpimportmatplotlib.pyplotaspltdefplot_all_cell_center(xcc,yref):plt.scatter(xcc,np.full_like(xcc,yref),s=20,facecolor='black',edgecolor='black',linewidth=1)returndefplot_cell_center(xcc,yref):nx=xcc.sizeii=nx//2im=ii-1ip=ii+1xcc_new=[]foriinrange(0,nx):ifi>1andi<im:continueifi>ipandi<nx-2:continuexcc_new.append(xcc[i])plt.scatter(xcc_new,np.full_like(xcc_new,yref),s=20,facecolor='black',edgecolor='black',linewidth=1)returndefplot_ghost_cell_center(xcc,yref):plt.scatter(xcc,np.full_like(xcc,yref),s=20,facecolor='red',edgecolor='black',linewidth=1)returndefplot_ghost_mesh(x,yref):dx=x[1]-x[0]dy=0.1*dxforxminx:plt.plot([xm,xm],[yref-dy,yref+dy],'k-')# 绘制垂直线returndefplot_mesh(x,yref):dx=x[1]-x[0]dy=0.1*dxforxminx:plt.plot([xm,xm],[yref-dy,yref+dy],'k-')# 绘制垂直线#nxc=x.size-1ii=nxc//2im=ii-1ip=ii+1foriinrange(0,nxc):ifi>1andi<im:plt.plot([x[i],x[i+1]],[yref,yref],'k--',linewidth=1)elifi>ipandi<nx-2:plt.plot([x[i],x[i+1]],[yref,yref],'k--',linewidth=1)else:plt.plot([x[i],x[i+1]],[yref,yref],'b-',linewidth=1)#plt.plot(x, np.full_like(x, yref), 'k--', linewidth=1)returndefplot_label(x,xcc,yref):x0=x[0]dx=x[1]-x[0]dyb=0.8*dxdyt=dyb*0.6yb=yref-dybyt=yref+dytybc=yref-0.5*dybplt.text(x[0],yb,r'$x_{\frac{1}{2}}$',fontsize=12,ha='center')plt.text(x[1],yb,r'$x_{\frac{3}{2}}$',fontsize=12,ha='center')plt.text(x[-2],yb,r'$x_{N-\frac{1}{2}}$',fontsize=12,ha='center')plt.text(x[-1],yb,r'$x_{N+\frac{1}{2}}$',fontsize=12,ha='center')plt.text(x[0],yt,r'$x=0$',fontsize=12,ha='center')plt.text(x[-1],yt,r'$x=L$',fontsize=12,ha='center')plt.text(xcc[0],ybc,r'$i=1$',fontsize=12,ha='center')plt.text(xcc[1],ybc,r'$2$',fontsize=12,ha='center')plt.text(xcc[-2],ybc,r'$N-1$',fontsize=12,ha='center')plt.text(xcc[-1],ybc,r'$N$',fontsize=12,ha='center')nx=xcc.sizei=nx//2print("i=",i)im=i-1ip=i+1plt.text(xcc[im],ybc,r'$i-1$',fontsize=12,ha='center')plt.text(xcc[i],ybc,r'$i$',fontsize=12,ha='center')plt.text(xcc[ip],ybc,r'$i+1$',fontsize=12,ha='center')str=r'$a=x_{\frac{1}{2}}<x_{\frac{3}{2}}<\cdots<x_{N-\frac{1}{2}}<x_{N+\frac{1}{2}}=b$'str='Grid: '+strnx=xcc.sizeii=nx//2plt.text(x[ii],yb-dx,str,fontsize=12,ha='center')returndefplot_ghost_label_left(xg,xgcc,yref):dx=abs(xg[1]-xg[0])dyb=0.8*dxdyt=dyb*0.6yb=yref-dybyt=yref+dytybc=yref-0.5*dybplt.text(xg[1],yb,r'$x_{-\frac{1}{2}}$',fontsize=12,ha='center')plt.text(xg[2],yb,r'$x_{-\frac{3}{2}}$',fontsize=12,ha='center')plt.text(xgcc[0],ybc,r'$0$',fontsize=12,ha='center')plt.text(xgcc[1],ybc,r'$-1$',fontsize=12,ha='center')returndefplot_ghost_label_right(xg,xgcc,yref):dx=abs(xg[1]-xg[0])dyb=0.8*dxdyt=dyb*0.6yb=yref-dybyt=yref+dytybc=yref-0.5*dybplt.text(xg[1],yb,r'$x_{N+\frac{3}{2}}$',fontsize=12,ha='center')plt.text(xg[2],yb,r'$x_{N+\frac{5}{2}}$',fontsize=12,ha='center')plt.text(xgcc[0],ybc,r'$N+1$',fontsize=12,ha='center')plt.text(xgcc[1],ybc,r'$N+2$',fontsize=12,ha='center')return# 设置字体为 Times New Romanplt.rc('text',usetex=True)plt.rc('font',family='serif',serif=['Times New Roman'])# 设置图形大小和样式plt.figure(figsize=(12,5))nx=9L=1.0x_l=0.0dx=L/nxx=np.zeros(nx+1,dtype=np.float64)xcc=np.zeros(nx,dtype=np.float64)nghost=2x_ghost_l=np.zeros(nghost+1,dtype=np.float64)xcc_ghost_l=np.zeros(nghost,dtype=np.float64)x_ghost_r=np.zeros(nghost+1,dtype=np.float64)xcc_ghost_r=np.zeros(nghost,dtype=np.float64)foriinrange(0,nx+1):x[i]=x_l+dx*(i)foriinrange(0,nx):xcc[i]=0.5*(x[i]+x[i+1])x_ghost_l[0]=x[0]forighostinrange(1,nghost+1):dx=x[0]-x[ighost]x_ghost_l[ighost]=x[0]+dxx_ghost_r[0]=x[nx]forighostinrange(1,nghost+1):dx=x[nx]-x[nx-ighost]x_ghost_r[ighost]=x[nx]+dxforighostinrange(0,nghost):xcc_ghost_l[ighost]=0.5*(x_ghost_l[ighost]+x_ghost_l[ighost+1])xcc_ghost_r[ighost]=0.5*(x_ghost_r[ighost]+x_ghost_r[ighost+1])print("x=",x)print("xcc=",xcc)print("x_ghost_l=",x_ghost_l)print("xcc_ghost_l=",xcc_ghost_l)print("x_ghost_r=",x_ghost_r)print("xcc_ghost_r=",xcc_ghost_r)yref=0.0plot_ghost_cell_center(xcc_ghost_l,yref)plot_ghost_cell_center(xcc_ghost_r,yref)plot_ghost_mesh(x_ghost_l,yref)plot_ghost_mesh(x_ghost_r,yref)plot_ghost_label_left(x_ghost_l,xcc_ghost_l,yref)plot_ghost_label_right(x_ghost_r,xcc_ghost_r,yref)plot_cell_center(xcc,yref)plot_mesh(x,yref)plot_label(x,xcc,yref)plt.axis('equal')plt.axis('off')plt.savefig('cfd.png',bbox_inches='tight',dpi=300)plt.show()