简体   繁体   English

使用两个自变量计算numpy数据的最有效方法

[英]Most efficient way to calculate numpy data with two independent variables

I'm fairly new to python/numpy and I'm calculating airflow based on a few different conditions. 我是python / numpy的新手,我正在根据一些不同的条件来计算气流。 I have array x (consisting of 100 elements) and array y (consisting of 2 elements...but eventually will expand). 我有数组x(由100个元素组成)和数组y(由2个元素组成...但最终会扩展)。 I can't figure out how to properly use numpy to combine all x elements with each y element separately and plot multiple lines on the same axis. 我无法弄清楚如何正确地使用numpy将所有x元素与每个y元素分别组合并在同一轴上绘制多条线。

The following Python code works, but isn't efficient for my larger applications. 以下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()

The goal is to combine m_dot0 and m_dot1 into the same object and plot them against dp_inWC using the simplest possible method. 目标是将m_dot0和m_dot1组合到同一对象中,并使用最简单的方法将它们针对dp_inWC进行绘制。

First, create a function that gets the mass flow rate based on the k-factor of the probe; 首先,创建一个基于探头的k因子获取质量流速的函数; this is bad practice (probably best to input everything into the function) but will serve your purpose: 这是不好的做法(最好将所有内容输入到函数中),但会达到您的目的:

def get_mdot(k_factor):
    mdot = Fna * k_factor * d **2 * Faa * Ya * np.sqrt(rho * dp_inWC)
    return mdot

Then, you can automate the process by using a list (provided you have a list of k-factors). 然后,您可以使用一个列表来自动执行该过程(前提是您有一个k因子列表)。

all_flow_rates = np.array([get_mdot(i) for i in ks])

Finally, to get a plot of all of them: 最后,要获得所有这些图:

plt.plot(dp_inWC, all_flow_rates.T)

Let me know if you have any questions. 如果您有任何疑问,请告诉我。 I will let you figure out the legend by yourself. 我会让你自己找出传说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM