声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

声振论坛 展示 基础理论 流体力学 查看内容

[系列]Python计算CFD问题

2016-4-5 14:45| 发布者: aspen| 查看: 11710| 评论: 0|原作者: funi|来自: lorenabarba.com

摘要: Python计算CFD问题1:一维线性对流问题 #-*-coding=utf-8 -*- import numpy as np import matplotlib.pyplot as plt import time,sys nx=41 #空间节点数 dx=2.0/(nx-1) #网格间距 nt=25 #时间步数 dt ...
[系列]Python计算CFD问题<6>:2D线性对流问题


 在第一个例子中,我们提到了解一维线性对流问题。在本例中,我们将利用python数值求解二维线性对流问题。
2D线性对流问题可以用此方程进行表达:
[attach]69963[/attach]
时间项采用向前差分,空间项采用先后差分。离散后的方程为:
[attach]69965[/attach]
换成迭代格式为:
[attach]69964[/attach]
初始条件为:
[attach]69966[/attach]
边界条件:
[attach]69967[/attach]
编制计算程序为:
  1. from mpl_toolkits.mplot3d import Axes3D ##New Library required for projected 3d plots

  2. import numpy as np
  3. import matplotlib.pyplot as plt

  4. ###variable declarations
  5. nx = 81
  6. ny = 81
  7. nt = 100
  8. c = 1
  9. dx = 2.0/(nx-1)
  10. dy = 2.0/(ny-1)
  11. sigma = .2
  12. dt = sigma*dx

  13. x = np.linspace(0,2,nx)
  14. y = np.linspace(0,2,ny)

  15. u = np.ones((ny,nx)) ##create a 1xn vector of 1's
  16. un = np.ones((ny,nx)) ##

  17. ###Assign initial conditions
  18. u[.5/dy:1/dy+1,.5/dx:1/dx+1]=2
  19. ###Plot Initial Condition
  20. fig = plt.figure(figsize=(11,7), dpi=100)
  21. ax = fig.gca(projection='3d')
  22. X, Y = np.meshgrid(x,y)
  23. surf = ax.plot_surface(X,Y,u[:])

  24. u = np.ones((ny,nx))
  25. u[.5/dy:1/dy+1,.5/dx:1/dx+1]=2

  26. for n in range(nt+1): ##loop across number of time steps
  27. un = u.copy()
  28. for i in range(1, len(u)):
  29. for j in range(1, len(u)):
  30. u[i,j] = un[i, j] - (c*dt/dx*(un[i,j] - un[i-1,j]))-(c*dt/dy*(un[i,j]-un[i,j-1]))
  31. u[0,:] = 1
  32. u[-1,:] = 1
  33. u[:,0] = 1
  34. u[:,-1] = 1
  35. fig = plt.figure(figsize=(11,7), dpi=100)
  36. ax = fig.gca(projection='3d')
  37. surf2 = ax.plot_surface(X,Y,u[:])
复制代码


输出结果:
初始条件:
[attach]69968[/attach]
最终结果:
[attach]69969[/attach]
做CFD计算的童鞋可能更容易理解云图,这里以云图的方式显示:
[attach]69970[/attach]
[attach]69971[/attach]
只需修改图形显示代码:
  1. plt.figure()
  2. im=plt.imshow(u,interpolation='bilinear',origin='lower',cmap=cm.rainbow)
  3. plt.contour(X,Y,u)
  4. plt.colorbar(im,orientation='vertical')
复制代码

【来源:http://nbviewer.ipython.org/gith ... ons/07_Step_5.ipynb


最新评论

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-4-28 06:43 , Processed in 0.102986 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部