matplotlib 如何避免在绘制CFD速度数据的等值线插值中使用零

bgtovc5b  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(140)

从Ansys Fluent中的CFD模拟,我有一个包含4列的csv数据框:x、y、z(不规则网格的节点坐标)和u(流向速度大小)。我想在x-z平面上绘制u的等值线图(y在此数据中已经固定)。空气流过的实体具有u=0,我希望在插值时将其屏蔽,即我想创建一个边界不规则的等高线图。第一个附加的图像或多或少是我想用Python获得的,用Ansys CFD-Post获得-实体的几何形状用黑色勾勒出来:

下面是基本代码,我不希望它工作,因为实心区域只是读取为速度= 0进行插值:

### geometric parameters, in m
L = 0.3048 
hwall = 0.0095
Lin = L-hwall*2
H = 0.073 
Lpad = Lin/10
Hpad = H/10

### get df: x,y,z coordinates and instantaneous streamwise velocity
df = pd.read_csv('test2.csv',names=['x','y','z','u'])
dfsub = df.loc[(df.x>-Lin/2-Lpad)&(df.x<Lin/2+Lpad)&(df.z<H+Hpad)] # limit to smaller region

### contour plot
crange = 4
fig,ax=plt.subplots(figsize=(16,3))
tcf = ax.tricontourf(dfsub.x,dfsub.z,dfsub.u,levels=np.arange(
crange,crange,0.1),cmap='seismic',extend='both')
plt.colorbar(tcf,ax=ax,label='$u$ (m/s)')
ax.set_title('basic',size=14)
plt.show()

这里是输出:

我尝试过用几种方法来屏蔽这些值,例如:将0 s替换为nans,这将给出“ValueError:z数组在三角剖分内不得包含非有限值”:

### get df: x,y,z coordinates and instantaneous streamwise velocity
df = pd.read_csv('test2.csv',names=['x','y','z','u'])
df.replace(0, np.nan, inplace=True)
dfsub = df.loc[(df.x>-Lin/2-Lpad)&(df.x<Lin/2+Lpad)&(df.z<H+Hpad)] # limit to smaller region

或者从 Dataframe 中删除0:

### get df: x,y,z coordinates and instantaneous streamwise velocity
df = pd.read_csv('test2.csv',names=['x','y','z','u'])
df = df.loc[df.u != 0]
dfsub = df.loc[(df.x>-Lin/2-Lpad)&(df.x<Lin/2+Lpad)&(df.z<H+Hpad)] # limit to smaller region

此输出在所附图像中。它仍然插入在我想要排除的区域中。

68de4m5k

68de4m5k1#

如果我没理解错的话,你应该使用masked array。绘制掩蔽阵列的轮廓时,将忽略掩蔽区域。如果没有实际数据来理解它是如何格式化的,我就把它留给您来弄清楚如何创建掩码数组。

import numpy as np
import matplotlib.pyplot as plt

plt.close("all")

x = np.linspace(-1, 1, 100)
y = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, y)

U = X + 2*Y

# create a region where we do not want the contour to exist
solid = (X >= -0.25) & (X <= 0) & (Y >= 0) & (Y <= 0.75)

# the mask argument takes a boolean array that says which elements are masked
U_masked = np.ma.array(U, mask=solid)

fig, ax = plt.subplots()
# contourf will not plot the masked regions
p = ax.contourf(X, Y, U_masked, corner_mask=False)
fig.colorbar(p, ax=ax, label="u")
ax.set_xlabel("x")
ax.set_ylabel("y")

相关问题