使用两个自变量计算 numpy 数据的最有效方法
Most efficient way to calculate numpy data with two independent variables
我是 python/numpy 的新手,我正在根据几种不同的条件计算气流。我有数组 x(由 100 个元素组成)和数组 y(由 2 个元素组成……但最终会扩展)。我不知道如何正确使用 numpy 将所有 x 元素与每个 y 元素分别组合并在同一轴上绘制多条线。
以下 Python 代码有效,但对我的大型应用程序效率不高。
import numpy as np
import matplotlib.pyplot as plt
#-------------------------------------------------------------------------
# Flow calculations
#-------------------------------------------------------------------------
# Input
ps = 14.4 # static pressure (psi)
d = 47.25 # ID (inch)
dp_inWC = np.linspace(0,15,100) # pressure head (inH2O)
# Airflow Calculation
Fna = 0.0997 # Unit conversion factor for lbm/s
Faa = 1.0 # Thermal expansion factor
blck = 0.02856 # Probe blockage
gam = 1.401 # Ratio of specific heats
rho = 0.0729 # humid air density (lbm/ft3)
Ya = 1 - (0.011332*(1-blck)**2 - 0.00342)*dp_inWC/(ps*gam) # Gas expansion factor
ks = [0.6748, 0.6615] # K-factor of probe
m_dot0 = Fna * ks[0] * d**2 * Faa * Ya * np.sqrt(rho*dp_inWC) # mass flow rate (lbm/s)
m_dot1 = Fna * ks[1] * d**2 * Faa * Ya * np.sqrt(rho*dp_inWC) # mass flow rate (lbm/s)
#----------------------------------------------------------------------------
# Plots
#----------------------------------------------------------------------------
# Initialize plot axis
fig1, ax1 = plt.subplots()
# Flow vs dP
ax1.set_title('Delta Pressure vs Mass Flow Rate',
color='tab:red',fontweight='bold')
ax1.set_xlabel('dP (inH2O)')
ax1.set_ylabel('Mass Flow (lbm/s)')
ax1.plot(dp_inWC, m_dot0, label='k={}'.format(ks[0]))
ax1.plot(dp_inWC, m_dot1, label='k={}'.format(ks[1]))
ax1.legend()
fig1.tight_layout()
plt.show()
目标是将 m_dot0 和 m_dot1 合并到同一个对象中,并使用最简单的方法将它们与 dp_inWC 进行对比。
首先,创建一个函数,根据探头的 k 因子获取质量流量;这是不好的做法(可能最好将所有内容都输入到函数中)但会达到你的目的:
def get_mdot(k_factor):
mdot = Fna * k_factor * d **2 * Faa * Ya * np.sqrt(rho * dp_inWC)
return mdot
然后,您可以使用一个列表(前提是您有一个 k 因子列表)使该过程自动化。
all_flow_rates = np.array([get_mdot(i) for i in ks])
最后,得到所有这些的情节:
plt.plot(dp_inWC, all_flow_rates.T)
如果您有任何问题,请告诉我。我会让你自己去揣摩传说。
我是 python/numpy 的新手,我正在根据几种不同的条件计算气流。我有数组 x(由 100 个元素组成)和数组 y(由 2 个元素组成……但最终会扩展)。我不知道如何正确使用 numpy 将所有 x 元素与每个 y 元素分别组合并在同一轴上绘制多条线。
以下 Python 代码有效,但对我的大型应用程序效率不高。
import numpy as np
import matplotlib.pyplot as plt
#-------------------------------------------------------------------------
# Flow calculations
#-------------------------------------------------------------------------
# Input
ps = 14.4 # static pressure (psi)
d = 47.25 # ID (inch)
dp_inWC = np.linspace(0,15,100) # pressure head (inH2O)
# Airflow Calculation
Fna = 0.0997 # Unit conversion factor for lbm/s
Faa = 1.0 # Thermal expansion factor
blck = 0.02856 # Probe blockage
gam = 1.401 # Ratio of specific heats
rho = 0.0729 # humid air density (lbm/ft3)
Ya = 1 - (0.011332*(1-blck)**2 - 0.00342)*dp_inWC/(ps*gam) # Gas expansion factor
ks = [0.6748, 0.6615] # K-factor of probe
m_dot0 = Fna * ks[0] * d**2 * Faa * Ya * np.sqrt(rho*dp_inWC) # mass flow rate (lbm/s)
m_dot1 = Fna * ks[1] * d**2 * Faa * Ya * np.sqrt(rho*dp_inWC) # mass flow rate (lbm/s)
#----------------------------------------------------------------------------
# Plots
#----------------------------------------------------------------------------
# Initialize plot axis
fig1, ax1 = plt.subplots()
# Flow vs dP
ax1.set_title('Delta Pressure vs Mass Flow Rate',
color='tab:red',fontweight='bold')
ax1.set_xlabel('dP (inH2O)')
ax1.set_ylabel('Mass Flow (lbm/s)')
ax1.plot(dp_inWC, m_dot0, label='k={}'.format(ks[0]))
ax1.plot(dp_inWC, m_dot1, label='k={}'.format(ks[1]))
ax1.legend()
fig1.tight_layout()
plt.show()
目标是将 m_dot0 和 m_dot1 合并到同一个对象中,并使用最简单的方法将它们与 dp_inWC 进行对比。
首先,创建一个函数,根据探头的 k 因子获取质量流量;这是不好的做法(可能最好将所有内容都输入到函数中)但会达到你的目的:
def get_mdot(k_factor):
mdot = Fna * k_factor * d **2 * Faa * Ya * np.sqrt(rho * dp_inWC)
return mdot
然后,您可以使用一个列表(前提是您有一个 k 因子列表)使该过程自动化。
all_flow_rates = np.array([get_mdot(i) for i in ks])
最后,得到所有这些的情节:
plt.plot(dp_inWC, all_flow_rates.T)
如果您有任何问题,请告诉我。我会让你自己去揣摩传说。