<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 The latest version of this IPython notebook is available at https
Step1:  Coefficient of Discharge
Based on Browning (1963)
$$C_D = 0.9 - 0.3\frac{P_a}{P}$$
Flow under Unchoked Conditions
Bernoulli's Equation
Bernoulli's equation for the behavior of a compressible fluid along a streamline
$$\frac{v_o^2}{2} + \frac{\gamma}{\gamma-1} \frac{P_o}{\rho_o}  = \frac{\gamma}{\gamma-1} \frac{P}{\rho}$$
which, for a streamline starting deep within the airbag, gives for $v_o$ 
$$v_o = \sqrt{\frac{2\gamma}{\gamma -1}\left(\frac{P}{\rho} - \frac{P_o}{\rho_o}\right)}$$
then for $\dot{m} = C_D A_o\rho_ov_o$
$$\dot{m} = C_D A_o \sqrt{\frac{2\gamma\rho_o^2}{\gamma -1}\left(\frac{P}{\rho} - \frac{P_o}{\rho_o}\right)}$$
Adiabatic Expansion
For the adiabatic expansion of an ideal gas,
$$\frac{\rho_o}{\rho} = \left(\frac{P_o}{P}\right)^{\frac{1}{\gamma}}$$
Substituting into the expression for $\rho_ov_o$,
$$\dot{m} = C_D A_o \sqrt{\frac{2\gamma\rho^2}{\gamma -1}\left(\frac{P_o}{P}\right)^\frac{2}{\gamma}\left(\frac{P}{\rho} - \frac{P_o}{\rho\left(\frac{P_o}{P}\right)^\frac{1}{\gamma}}\right)}$$
After rearrangement
$$\dot{m} = C_D A_o \sqrt{\frac{2\gamma\rho P}{\gamma -1}\left[\left(\frac{P_o}{P}\right)^\frac{2}{\gamma} - \left(\frac{P_o}{P}\right)^{1+\frac{1}{\gamma}}\right]}$$
Evaluating at the Orifice
Assuming the pressure at the orifice is equal to the surrounding ambient pressure, $P_o = P_a$.  With that assumption we have
$$\dot{m} = C_D A_o \sqrt{\frac{2\gamma\rho P}{\gamma -1}\left[\left(\frac{P_a}{P}\right)^\frac{2}{\gamma} - \left(\frac{P_a}{P}\right)^{1+\frac{1}{\gamma}}\right]}$$
This expression is found in Handbook of Chemical Hazard Analysis Procedures, Appendix B, Federal Emergency Management Agency, U.S. Dept. of Transportation, and U.S. Environmental Protection Agency, 1989. The relevant section is on page B-4 located on the page 391 of the pdf file.
Step2:  Choked Flow
When the flow is choked, flow through the orific depends only on the upstream pressure $P$
$$\dot{m} = C_D A \sqrt{\gamma \rho P \left(\frac{2}{\gamma + 1}\right)^\left(\frac{\gamma + 1}{\gamma - 1}\right)}$$
| Parameter | Description | Units |
| 
Step3:  Critical Pressure Ratio
The critical pressure ratio is the ratio of upstream to downstream pressure of a compressible gas at which leads to choked flow.  Given the ratio of heat capacities
$$\gamma = \frac{C_p}{C_v}$$
the critical ratio is
$$\left(\frac{P_2}{P_1}\right)_{critical} =  \left(\frac{\gamma+1}{2}\right)^\left({\frac{\gamma}{\gamma-1}}\right)$$
Step4:  Procedure to Calculate the Airbag Vent Mass Flowrate
Given the initial airbag pressure $P_i$ and absolute temperature $T_i$, the ambient pressure $P_a$, and gas constant $\gamma$, and orifice vent area $A_o$
<ASSISTANT_TASK:>
Python Code:
#Initializations
from IPython.core.display import HTML
HTML(open("../styles/custom.css", "r").read())
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import math
pi = math.pi
from pint import UnitRegistry
ur = UnitRegistry()
Explanation: The latest version of this IPython notebook is available at https://github.com/jckantor/Airbag-Design-for-Cargo-Airdrop under the MIT License.
J.C. Kantor (Kantor.1@nd.edu)
Venting Mass Flowrate
Mass Flow from an Orifice
The mass flow through the airbag vent orifice is given by
$$\dot{m} = C_DA_o \rho_o v_o$$
where the subscript $o$ refers to conditions at the orifice. We begin by constructing an estimate for $\rho_o v_o$ using Bernoulli's equation for compressible flow on a streamline, the pressure-density relationship for the isentropic expansion of an ideal gas, and an assumption regarding pressure at the orifice.
End of explanation
# ideal gas parameters for air
gamma = 1.4
R = 8.314 * ur.J/(ur.degK*ur.mol)
MW = 28.966 * ur.grams/ur.mol
# ambient conditions
Pa = 1.0 * ur.atm
Ta = ur.Quantity(20,ur.degC).to(ur.degK)
def Qunchoke(P,Pa):
    Pr = Pa/P
    rho = MW*P/(R*Ta)
    b = 2.0*(gamma/(gamma-1))*rho*P
    Q = np.sqrt(b*(Pr**(2.0/gamma)-Pr**((1.0+1.0/gamma))))
    return Q.to(ur.kg/ur.m**2/ur.sec)
P = np.linspace(1.0,3.0,100) * ur.atm
Qu = [Qunchoke(p,Pa) for p in P]
plt.plot(P,[q.magnitude for q in Qu])
plt.title('Unchoked Flow from an Orifice, CD = 1')
plt.ylabel(Qu[0].units)
plt.xlabel(P.units)
Explanation: Coefficient of Discharge
Based on Browning (1963)
$$C_D = 0.9 - 0.3\frac{P_a}{P}$$
Flow under Unchoked Conditions
Bernoulli's Equation
Bernoulli's equation for the behavior of a compressible fluid along a streamline
$$\frac{v_o^2}{2} + \frac{\gamma}{\gamma-1} \frac{P_o}{\rho_o}  = \frac{\gamma}{\gamma-1} \frac{P}{\rho}$$
which, for a streamline starting deep within the airbag, gives for $v_o$ 
$$v_o = \sqrt{\frac{2\gamma}{\gamma -1}\left(\frac{P}{\rho} - \frac{P_o}{\rho_o}\right)}$$
then for $\dot{m} = C_D A_o\rho_ov_o$
$$\dot{m} = C_D A_o \sqrt{\frac{2\gamma\rho_o^2}{\gamma -1}\left(\frac{P}{\rho} - \frac{P_o}{\rho_o}\right)}$$
Adiabatic Expansion
For the adiabatic expansion of an ideal gas,
$$\frac{\rho_o}{\rho} = \left(\frac{P_o}{P}\right)^{\frac{1}{\gamma}}$$
Substituting into the expression for $\rho_ov_o$,
$$\dot{m} = C_D A_o \sqrt{\frac{2\gamma\rho^2}{\gamma -1}\left(\frac{P_o}{P}\right)^\frac{2}{\gamma}\left(\frac{P}{\rho} - \frac{P_o}{\rho\left(\frac{P_o}{P}\right)^\frac{1}{\gamma}}\right)}$$
After rearrangement
$$\dot{m} = C_D A_o \sqrt{\frac{2\gamma\rho P}{\gamma -1}\left[\left(\frac{P_o}{P}\right)^\frac{2}{\gamma} - \left(\frac{P_o}{P}\right)^{1+\frac{1}{\gamma}}\right]}$$
Evaluating at the Orifice
Assuming the pressure at the orifice is equal to the surrounding ambient pressure, $P_o = P_a$.  With that assumption we have
$$\dot{m} = C_D A_o \sqrt{\frac{2\gamma\rho P}{\gamma -1}\left[\left(\frac{P_a}{P}\right)^\frac{2}{\gamma} - \left(\frac{P_a}{P}\right)^{1+\frac{1}{\gamma}}\right]}$$
This expression is found in Handbook of Chemical Hazard Analysis Procedures, Appendix B, Federal Emergency Management Agency, U.S. Dept. of Transportation, and U.S. Environmental Protection Agency, 1989. The relevant section is on page B-4 located on the page 391 of the pdf file.
End of explanation
# ideal gas parameters for air
gamma = 1.4
R = 8.314 * ur.J/(ur.degK*ur.mol)
MW = 28.966 * ur.grams/ur.mol
# ambient conditions
Pa = 1.0 * ur.atm
Ta = ur.Quantity(20,ur.degC).to(ur.degK)
def Qchoke(P):
    rho = MW*P/(R*Ta)
    a = gamma*P*rho
    Q = np.sqrt(a*(2.0/(gamma+1))**((gamma+1)/(gamma-1)))
    return Q.to(ur.kg/ur.m**2/ur.sec)
P = np.linspace(1.0,3.0,100) * ur.atm
Qc = [Qchoke(p) for p in P]
plt.plot(P,[q.magnitude for q in Qc],P,[q.magnitude for q in Qc])
plt.title('Choked Flow from an Orifice, CD = 1')
plt.ylabel(Qc[0].units)
plt.xlabel(P.units)
Explanation: Choked Flow
When the flow is choked, flow through the orific depends only on the upstream pressure $P$
$$\dot{m} = C_D A \sqrt{\gamma \rho P \left(\frac{2}{\gamma + 1}\right)^\left(\frac{\gamma + 1}{\gamma - 1}\right)}$$
| Parameter | Description | Units |
| :-------: | :-: | :-: |
| $\dot{m}$ | mass flow rate | kg/s |
| $C_d$ | discharge coefficient | dimensionless|
| $A$ | area of discharge | m2 |
| $\gamma$ | ratio of heat capacities $\frac{C_p}{C_v}$ | dimensionless |
| $\rho$ | gas density | kg/m3 |
| $P$ | pressure | Pa |
| $T$ | temperature | K |
End of explanation
gamma = dict()
gamma['air (dry)'] = 1.4
gamma['argon'] = 1.67
gamma['butane'] = 1.096
gamma['carbon dioxide'] = 1.30
gamma['chlorine'] = 1.33
gamma['helium'] = 1.660
gamma['hydrogen'] = 1.41
gamma['nitrogen'] = 1.404
gamma['oxygen'] = 1.400
gamma['propane'] = 1.15
print "{0:<15s}   {1:6s}  {2:6s}".format("Species","CR", "1/CR")
for s in sorted(gamma.keys()):
    r = ((gamma[s]+1)/2)**(gamma[s]/(gamma[s]-1))
    print "{0:<15s}   {1:6.4f}  {2:6.4f}".format(s, r, 1.0/r)
def isChoked(Phi, Plo = 101325.0, gamma = 1.4):
    r = ((gamma+1)/2)**(gamma/(gamma-1))
    return Phi > r*Plo
Explanation: Critical Pressure Ratio
The critical pressure ratio is the ratio of upstream to downstream pressure of a compressible gas at which leads to choked flow.  Given the ratio of heat capacities
$$\gamma = \frac{C_p}{C_v}$$
the critical ratio is
$$\left(\frac{P_2}{P_1}\right)_{critical} =  \left(\frac{\gamma+1}{2}\right)^\left({\frac{\gamma}{\gamma-1}}\right)$$
End of explanation
from pint import UnitRegistry
ur = UnitRegistry()
# ideal gas parameters for air
gamma = 1.4
R = 8.314 * ur.J/(ur.degK*ur.mol)
MW = 28.966 * ur.grams/ur.mol
# ambient conditions
Pa = 1.0 * ur.atm
Ta = ur.Quantity(20,ur.degC).to(ur.degK)
def CD(P, Pa = 1.0 * ur.atm):
    return 0.9 - 0.3*Pa/P
def Qflow(T, P, Pa = 1.0 * ur.atm):
    rho = MW*P/(R*T)
    r = ((gamma+1)/2)**(gamma/(gamma-1))
    if P <= r*Pa:
        Pr = Pa/P
        b = 2.0*(gamma/(gamma-1))*rho*P
        Q = np.sqrt(b*(Pr**(2.0/gamma)-Pr**((1.0+1.0/gamma))))
    else:
        a = gamma*P*rho
        Q = np.sqrt(a*(2.0/(gamma+1))**((gamma+1)/(gamma-1)))
    return Q.to(ur.kg/ur.m**2/ur.sec)
def Mflow(T, P, Pa, A = 1.0*ur.m**2):
    return (CD(P,Pa)*A*Qflow(T,P,Pa)).to_base_units()
P = np.linspace(1.0,2.5,100) * ur.atm
M = [Mflow(Ta,p,Pa,A = 100*ur.cm**2) for p in P]
plt.plot(P,[m.magnitude for m in M])
plt.title('Mass Flow from an 100 cm**2 Orifice')
plt.ylabel(M[0].units)
plt.xlabel(P.units)
Explanation: Procedure to Calculate the Airbag Vent Mass Flowrate
Given the initial airbag pressure $P_i$ and absolute temperature $T_i$, the ambient pressure $P_a$, and gas constant $\gamma$, and orifice vent area $A_o$:
Step 1. Calculate the intial air density as
$$\rho_i = \frac{M P_i}{R T_i}$$
For adiabatic (isentropic) compression, subsequent values of the airbag pressure, temperature and air density satisfy
$$ \frac{P}{P_i} = \left(\frac{\rho}{\rho_i}\right)^\gamma =
\left(\frac{T}{T_i}\right)^\frac{\gamma}{\gamma-1}$$
Step 2. Given the ambient pressure $P_a$, compute the discharge coefficient
$$C_D = 0.9 - 0.3\frac{P_a}{P}$$
Step 3. Compute the critical pressure ratio
$$r = \left(\frac{\gamma+1}{2}\right)^\left({\frac{\gamma}{\gamma-1}}\right)$$
Step 4. Compute the mass flow for either unchoked or choked flow conditions
$$ \dot{m} = \begin{cases}
C_D A_o \sqrt{\frac{2\gamma\rho P}{\gamma -1}\left[\left(\frac{P_a}{P}\right)^\frac{2}{\gamma} - \left(\frac{P_a}{P}\right)^{1+\frac{1}{\gamma}}\right]}
& \mbox{if } P \leq r P_a \
C_D A_o \sqrt{\gamma \rho P \left(\frac{2}{\gamma + 1}\right)^\left(\frac{\gamma + 1}{\gamma - 1}\right)}
& \mbox{otherwise}
\end{cases}
$$
End of explanation
<END_TASK>