本文最后更新于 2025年4月9日 晚上
需求: 使用openfoam计算了复杂地形场景下的风场,现在想要画出距离地面100m高度的曲面上的风速云图,并用Python进一步后处理
1. 导入计算结果
导入openfoam结果,在mesh regions中选择internalMesh和地表的patch,选择应用。
2. 提取地表
插入一个extract block的filter,选择地表的patch,应用即可得到一个单独的地表。
3. 使用caculator获得向上平移100m的曲面
对上一步提取到的地表插入一个caculator的filter,勾选Coordinate Results,输入表达式iHat*coordsX + jHat*coordsY + kHat*(coordsZ+100)
,选择应用,得到平移后的地表。
4. 使用Resample With Dataset将流场数据映射到新曲面上
插入一个Resample With Dataset的filter,源选择第一步中的流场数据,Destination Mesh选择第三步中的新曲面,选择应用。这时候应该能看到新曲面上有流场数据了。
5. 导出csv数据
画面上只留下映射了数据的新曲面,File->Save Data,选择csv格式,保存想要的数据列。
6. 使用Python绘制云图
代码如下(仅参考,需要根据具体需求修改):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import pandas as pd import matplotlib.pyplot as plt import numpy as np from scipy.interpolate import griddata
data = pd.read_csv("./30m_h=100m.csv")
x = data['Points:0'] y = data['Points:1'] u = data['U:0'] v = data['U:1'] w = data['U:2']
wind_speed = np.sqrt(u**2 + v**2 + w**2)
grid_x, grid_y = np.mgrid[x.min():x.max():100, y.min():y.max():100]
grid_wind_speed = griddata((x, y), wind_speed, (grid_x, grid_y), method='cubic') grid_z = griddata((x, y), data['Points:2'], (grid_x, grid_y), method='cubic')
plt.figure(figsize=(10, 8)) contourf = plt.contourf(grid_x, grid_y, grid_wind_speed, levels=20, cmap='viridis')
|
如果想要绘制等高线,可以在最后加上:
1 2 3
| contour = plt.contour(grid_x, grid_y, grid_z, levels=10, colors='black') plt.clabel(contour, inline=True, fontsize=8)
|
如果想要绘制风向箭头,可以在最后加上:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
num_arrows = 10 x_centers = np.linspace(x.min(), x.max(), num_arrows) y_centers = np.linspace(y.min(), y.max(), num_arrows) X, Y = np.meshgrid(x_centers, y_centers)
U = griddata((x, y), u, (X, Y), method='cubic') V = griddata((x, y), v, (X, Y), method='cubic')
plt.quiver(X, Y, U, V, pivot='middle', color='white')
|
最后输出图片:
1 2 3 4 5 6 7 8
| plt.colorbar(contourf, label='Wind Speed')
plt.xlabel('X Coordinate') plt.ylabel('Y Coordinate') plt.title('Wind Speed Contour at 100m Elevation') plt.show()
|