{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7.18 Gas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A natural gas pipeline (schedule 20 14\"\") is 100 miles long. Inlet pressure is 1300 psia, and outlet pressure is 300 psia.\n",
    "\n",
    "The average temperature is 40 deg F. The gas composition is 75% methane, 21% ethane, and 4 % propane.\n",
    "\n",
    "Find the flow rate in MMscfd.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fluids.units import *\n",
    "from math import pi\n",
    "P1 = 1300*u.psi\n",
    "P2 = 300*u.psi\n",
    "T = 40*u.degF\n",
    "L = 100*u.miles\n",
    "\n",
    "mu = 1.1e-5*u.Pa # \n",
    "# mu = 1.915E-5*u.Pa # A more correct value, but hinders matching the problem\n",
    "NPS, Di, Do, t = nearest_pipe(NPS=14, schedule='20')\n",
    "A = 0.25*pi*Di**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from thermo import *\n",
    "from thermo import PRMIX\n",
    "Tcs = [190.564, 305.32, 369.83]\n",
    "Pcs = [4599000.0, 4872000.0, 4248000.0]\n",
    "omegas = [0.008, 0.098, 0.152]\n",
    "MWs = [16.04246, 30.06904, 44.09562]\n",
    "zs = [0.75, 0.21, .04]\n",
    "MW = sum(zs[i]*MWs[i] for i in range(3))*u.g/u.mol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "According to the full isothermal equation, the flowrate is 103.434 MMscfd\n"
     ]
    }
   ],
   "source": [
    "eos_1 = PRMIX(T=T.to(u.K).magnitude, P=P1.to(u.Pa).magnitude, zs=zs, Tcs=Tcs, Pcs=Pcs, omegas=omegas)\n",
    "eos_2 = PRMIX(T=T.to(u.K).magnitude, P=P2.to(u.Pa).magnitude, zs=zs, Tcs=Tcs, Pcs=Pcs, omegas=omegas)\n",
    "eos_std = PRMIX(T=288.15, P=101325.0, zs=zs, Tcs=Tcs, Pcs=Pcs, omegas=omegas)\n",
    "\n",
    "Vm1 = eos_1.V_g*u.m**3/u.mol\n",
    "rho1 = (Vm1)**-1*MW\n",
    "\n",
    "Vm2 = eos_2.V_g*u.m**3/u.mol\n",
    "rho2 = (Vm2)**-1*MW\n",
    "\n",
    "Vm_std = eos_std.V_g*u.m**3/u.mol\n",
    "rho_std = (Vm_std)**-1*MW\n",
    "\n",
    "roughness = 0.0018*u.inch\n",
    "\n",
    "rho = 0.5*(rho1 + rho2)\n",
    "\n",
    "v = 10.0 # Initial guess for velocity\n",
    "Re = rho*v*Di/mu\n",
    "fd = friction_factor(Re=Re, eD=roughness/Di)\n",
    "\n",
    "for i in range(8):\n",
    "    # Solve for velocity with sequential substitution\n",
    "    m = isothermal_gas(rho, fd, P1=P1, P2=P2, L=L, D=Di)\n",
    "    v = m/(A*rho)\n",
    "    Re = rho*v*Di/mu\n",
    "    fd = friction_factor(Re=Re, eD=roughness/Di)\n",
    "Q = v*A\n",
    "correction = rho_std/rho\n",
    "\n",
    "# pint does not support mmscfd\n",
    "mmscfd = Q.to(u.ft**3/u.day).magnitude/1e6/correction\n",
    "print('According to the full isothermal equation, the flowrate is %g MMscfd' %(mmscfd))\n",
    "# Q.to(u.m**3/u.s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Note Z_avg should be used in the Panhandle and Weymouth equations\n",
    "# However Crane omits them; they are not used here to match Crane.\n",
    "Z_avg = 0.5*(eos_1.Z_g + eos_2.Z_g)\n",
    "MW_air = 28.966*u.g/u.mol\n",
    "SG = MW/MW_air"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "According to the Weymouth equation, the flowrate is 105.047 MMscfd\n"
     ]
    }
   ],
   "source": [
    "# Crane does not use the efficiency term on Weymouth\n",
    "Q_Weymouth = Weymouth(SG, Tavg=T, L=L, D=Di, P1=P1, P2=P2, Zavg=1, E=1)\n",
    "\n",
    "mmscfd = Q_Weymouth.to(u.ft**3/u.day).magnitude/1e6\n",
    "print('According to the Weymouth equation, the flowrate is %g MMscfd' %(mmscfd))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "According to the Panhandle A equation, the flowrate is 128.143 MMscfd\n"
     ]
    }
   ],
   "source": [
    "Q_panhandle = Panhandle_A(SG, Tavg=T, L=L, D=Di, P1=P1, P2=P2, Zavg=1, E=0.92)\n",
    "\n",
    "mmscfd = Q_panhandle.to(u.ft**3/u.day).magnitude/1e6\n",
    "print('According to the Panhandle A equation, the flowrate is %g MMscfd' %(mmscfd))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Besides the simplifications Crane makes, the methods are fair approximations. \n",
    "The values in Crane are 107.8, 105.1, and 128.2 mmscfd respectively. The isothermal calculation employed by Crane is a simplified one, explaining the difference - it does not account for expansion rigorously."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
