{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 14 - Air and gas compressors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 1: pg 400"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Example 14.1\n",
      " (a) The free air delivered is (m^3/min) =  3.814\n",
      " (b) The volumetric efficiency is (percent) =  80.9\n",
      " (c) The air delivery temperature is (C) =  164.3\n",
      " (d) The cycle power is (kW) =  14.0\n",
      " (e) The isothermal efficiency neglecting clearence  is (percent) =  81.3\n"
     ]
    }
   ],
   "source": [
    "#pg 400\n",
    "print(' Example 14.1');\n",
    "\n",
    "# aim : To determine \n",
    "# (a) the free air delivered\n",
    "# (b) the volumetric efficiency\n",
    "# (c) the air delivery temperature\n",
    "# (d) the cycle power\n",
    "# (e) the isothermal efficiency\n",
    "from math import pi,log\n",
    "# given values\n",
    "d = 200.*10**-3;# bore, [m]\n",
    "L = 300.*10**-3;# stroke, [m]\n",
    "N = 500.;# speed, [rev/min]\n",
    "n = 1.3;# polytropic index\n",
    "P1 = 97.;# intake pressure, [kN/m**2]\n",
    "T1 = 273.+20;# intake temperature, [K]\n",
    "P3 = 550.;# compression pressure, [kN/m**2]\n",
    "\n",
    "# solution\n",
    "# (a)\n",
    "P4 = P1;\n",
    "P2 = P3;\n",
    "Pf = 101.325;# free air pressure, [kN/m**2]\n",
    "Tf = 273+15;# free air temperature, [K]\n",
    "SV = pi/4*d**2*L;# swept volume, [m**3]\n",
    "V3 = .05*SV;# [m**3]\n",
    "V1 = SV+V3;# [m**3]\n",
    "V4 = V3*(P3/P4)**(1/n);# [m**3]\n",
    "ESV = (V1-V4)*N;# effective swept volume/min, [m**3]\n",
    "# using PV/T=constant\n",
    "Vf = P1*ESV*Tf/(Pf*T1);# free air delivered, [m**3/min]\n",
    "print ' (a) The free air delivered is (m^3/min) = ',round(Vf,3)\n",
    "\n",
    "# (b)\n",
    "VE = Vf/(N*(V1-V3));# volumetric efficiency\n",
    "print ' (b) The volumetric efficiency is (percent) = ',round(VE*100,1)\n",
    "\n",
    "# (c)\n",
    "T2 = T1*(P2/P1)**((n-1)/n);#  free air temperature, [K]\n",
    "print ' (c) The air delivery temperature is (C) = ',round(T2-273,1)\n",
    "\n",
    "#  (d)\n",
    "CP = n/(n-1)*P1*(V1-V4)*((P2/P1)**((n-1)/n)-1)*N/60;# cycle power, [kW]\n",
    "print ' (d) The cycle power is (kW) = ',round(CP)\n",
    "\n",
    "# (e)\n",
    "# neglecting clearence\n",
    "W = n/(n-1)*P1*V1*((P2/P1)**((n-1)/n)-1)\n",
    "Wi = P1*V1*log(P2/P1);# isothermal efficiency\n",
    "IE = Wi/W;# isothermal efficiency\n",
    "print ' (e) The isothermal efficiency neglecting clearence  is (percent) = ',round(IE*100,1)\n",
    "\n",
    "# End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2: pg 408"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Example 14.2\n",
      " (a) The intermediate pressure is (MN/m^2) =  0.265\n",
      " (b) The total volume of the HP cylinder is (litres) =  7.6\n",
      " (c) The cycle power is (MW) =  43.0\n",
      " there is rounding mistake in the book so answer is not matching\n"
     ]
    }
   ],
   "source": [
    "#pg 408\n",
    "print(' Example 14.2');\n",
    "\n",
    "# aim : To determine \n",
    "# (a) the intermediate pressure\n",
    "# (b) the total volume of each cylinder\n",
    "# (c) the cycle power\n",
    "from math import sqrt\n",
    "# given values\n",
    "v1 = .2;# air intake, [m^3/s]\n",
    "P1 = .1;# intake pressure, [MN/m^2]\n",
    "T1 = 273.+16;# intake temperature, [K]\n",
    "P3 = .7;# final pressure, [MN/m^2]\n",
    "n = 1.25;# compression index\n",
    "N = 10;# speed, [rev/s]\n",
    "\n",
    "# solution\n",
    "# (a)\n",
    "P2 = sqrt(P1*P3);# intermediate pressure, [MN/m^2]\n",
    "print ' (a) The intermediate pressure is (MN/m^2) = ',round(P2,3)\n",
    "\n",
    "# (b)\n",
    "V1  = v1/N;# total volume,[m^3]\n",
    "# since intercooling is perfect so 2 lie on the isothermal through1, P1*V1=P2*V2\n",
    "V2 = P1*V1/P2;# volume, [m^3]\n",
    "print ' (b) The total volume of the HP cylinder is (litres) = ',round(V2*10**3,1)\n",
    "\n",
    "# (c)\n",
    "CP = 2*n/(n-1)*P1*v1*((P2/P1)**((n-1)/n)-1);# cycle power, [MW]\n",
    "print ' (c) The cycle power is (MW) = ',round(CP*10**3)\n",
    "\n",
    "print ' there is rounding mistake in the book so answer is not matching'\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3: pg 409"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Example 14.3\n",
      " (a) The intermediate pressure is P2 (bar) =  2.466\n",
      "     The intermediate pressure is  P3 (bar) =  6.082\n",
      " (b) The effective swept volume of the LP cylinder is (litres) =  45.32\n",
      " (c) The delivery temperature is (C) =  85.4\n",
      "      The delivery volume is (litres) =  3.72\n",
      " (d) The work done per kilogram of air is (kJ) =  254.1\n",
      "The answer is a bit different due to rounding off error in textbook\n"
     ]
    }
   ],
   "source": [
    "#pg 409\n",
    "print(' Example 14.3');\n",
    "\n",
    "# aim : To determine \n",
    "# (a) the intermediate pressures\n",
    "# (b) the effective swept volume of the LP cylinder\n",
    "# (c) the temperature and the volume of air delivered per stroke at 15 bar\n",
    "# (d) the work done per kilogram of air\n",
    "import math\n",
    "# given values\n",
    "d = 450.*10**-3;#  bore , [m]\n",
    "L = 300.*10**-3;#  stroke, [m]\n",
    "cl = .05;#  clearence\n",
    "P1 = 1.; # intake pressure, [bar]\n",
    "T1 = 273.+18;# intake temperature, [K]\n",
    "P4 = 15.;# final delivery pressure, [bar]\n",
    "n = 1.3;#  compression and expansion index\n",
    "R = .29;# gas constant, [kJ/kg K]\n",
    "\n",
    "# solution\n",
    "# (a)\n",
    "k=(P4/P1)**(1./3); \n",
    "# hence\n",
    "P2 = k*P1;#  intermediare pressure, [bar]\n",
    "P3 = k*P2;#  intermediate pressure, [bar]\n",
    "\n",
    "print ' (a) The intermediate pressure is P2 (bar) = ',round(P2,3)\n",
    "print '     The intermediate pressure is  P3 (bar) = ',round(P3,3)\n",
    "\n",
    "# (b)\n",
    "SV = math.pi*d**2/4*L;#  swept volume of LP cylinder, [m**3]\n",
    "# hence\n",
    "V7 = cl*SV;# volume, [m**3]\n",
    "V1 = SV+V7;# volume, [m**3]\n",
    "# also\n",
    "P7 = P2;\n",
    "P8 = P1;\n",
    "V8 = V7*(P7/P8)**(1/n);#  volume, [m**3]\n",
    "ESV = V1-V8;# effective swept volume of LP cylinder, [m**3]\n",
    "\n",
    "print ' (b) The effective swept volume of the LP cylinder is (litres) = ',round(ESV*10**3,2)\n",
    "\n",
    "# (c)\n",
    "T9 = T1;\n",
    "P9 = P3;\n",
    "T4 = T9*(P4/P9)**((n-1)/n);# delivery temperature, [K]\n",
    "# now using P4*(V4-V5)/T4=P1*(V1-V8)/T1\n",
    "V4_minus_V5 = P1*T4*(V1-V8)/(P4*T1);# delivery volume, [m**3]\n",
    " \n",
    "print ' (c) The delivery temperature is (C) = ',round(T4-273,1)\n",
    "print '      The delivery volume is (litres) = ',round(V4_minus_V5*10**3,2)\n",
    "\n",
    "#  (d)\n",
    "\n",
    "W = 3*n*R*T1*((P2/P1)**((n-1)/n)-1)/(n-1);#  work done/kg ,[kJ]\n",
    "print ' (d) The work done per kilogram of air is (kJ) = ',round(W,1)\n",
    " \n",
    "print 'The answer is a bit different due to rounding off error in textbook'\n",
    "# End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4: pg 416"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Example 14.4\n",
      " (a) The final temperature is (C) =  221.0\n",
      " (b) The final pressure is (kN/m^2) =  500.0\n",
      " (b) The energy required to drive the compressor is (kW) =  -2023.0\n",
      "The negative sign indicates energy input\n",
      "The answer is a bit different due to rounding off error in textbook\n"
     ]
    }
   ],
   "source": [
    "#pg 416\n",
    "print(' Example 14.4');\n",
    "\n",
    "# aim : To determine \n",
    "# (a) the final pressure and temperature\n",
    "# (b) the energy required to drive the compressor\n",
    "\n",
    "# given values\n",
    "rv = 5.;# pressure compression ratio\n",
    "m_dot = 10.;# air flow rate, [kg/s]\n",
    "P1 = 100.;# initial pressure, [kN/m**2]\n",
    "T1 = 273.+20;# initial temperature, [K]\n",
    "n_com = .85;# isentropic efficiency of compressor\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "cp = 1.005;# specific heat capacity, [kJ/kg K]\n",
    "\n",
    "# solution\n",
    "# (a)\n",
    "T2_prim = T1*(rv)**((Gama-1)/Gama);# temperature after compression, [K]\n",
    "# using isentropic efficiency=(T2_prim-T1)/(T2-T1)\n",
    "T2 = T1+(T2_prim-T1)/n_com;#  final temperature, [K]\n",
    "P2 = rv*P1;# final pressure, [kN/m**2]\n",
    "print ' (a) The final temperature is (C) = ',round(T2-273)\n",
    "print ' (b) The final pressure is (kN/m^2) = ',P2\n",
    "\n",
    "# (b)\n",
    "E = m_dot*cp*(T1-T2);# energy required, [kW]\n",
    "print ' (b) The energy required to drive the compressor is (kW) = ',round(E)\n",
    "if(E<0):\n",
    "    print('The negative sign indicates energy input');\n",
    "else:\n",
    "    print('The positive sign indicates energy output');\n",
    "\n",
    "print 'The answer is a bit different due to rounding off error in textbook'\n",
    " #  End\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 5: pg 417"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Example 14.5\n",
      " The power absorbed by compressor is (kW) =  12.64\n"
     ]
    }
   ],
   "source": [
    "#pg 417\n",
    "print(' Example 14.5');\n",
    "\n",
    "# aim : To determine \n",
    "#  the power absorbed in driving the compressor\n",
    "\n",
    "# given values\n",
    "FC = .68;# fuel consumption rate, [kg/min]\n",
    "P1 = 93.;# initial pressure, [kN/m^2]\n",
    "P2 = 200.;# final pressure, [kN/m^2]\n",
    "T1 = 273.+15;# initial temperature, [K]\n",
    "d = 1.3;# density of mixture, [kg/m^3]\n",
    "n_com = .82;# isentropic efficiency of compressor\n",
    "Gama = 1.38;# heat capacity ratio\n",
    "\n",
    "# solution\n",
    "R = P1/(d*T1);# gas constant, [kJ/kg K]\n",
    "# for mixture\n",
    "cp = Gama*R/(Gama-1);# heat capacity, [kJ/kg K]\n",
    "T2_prim = T1*(P2/P1)**((Gama-1)/Gama);# temperature after compression, [K]\n",
    "# using isentropic efficiency=(T2_prim-T1)/(T2-T1)\n",
    "T2 = T1+(T2_prim-T1)/n_com;#  final temperature, [K]\n",
    "m_dot = FC*15/60.;# given condition, [kg/s]\n",
    "P = m_dot*cp*(T2-T1);# power absorbed by compressor, [kW]\n",
    "#results\n",
    "print ' The power absorbed by compressor is (kW) = ',round(P,2)\n",
    "\n",
    "# End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6: pg 418"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Example 14.6\n",
      " The power required to drive the blower is (kW) =  99.5\n"
     ]
    }
   ],
   "source": [
    "#pg 418\n",
    "print(' Example 14.6');\n",
    "\n",
    "# aim : To determine \n",
    "#  the power required to drive the blower\n",
    "\n",
    "# given values\n",
    "m_dot = 1;# air capacity, [kg/s]\n",
    "rp = 2;# pressure ratio\n",
    "P1 = 1*10**5;# intake pressure, [N/m^2]\n",
    "T1 = 273+70.;# intake temperature, [K]\n",
    "R = .29;# gas constant, [kJ/kg k]\n",
    "\n",
    "# solution\n",
    "V1_dot = m_dot*R*T1/P1*10**3;# [m^3/s]\n",
    "P2 = rp*P1;# final pressure, [n/m^2]\n",
    "P = V1_dot*(P2-P1);# power required, [W]\n",
    "#results\n",
    "print ' The power required to drive the blower is (kW) = ',round(P*10**-3,1)\n",
    "\n",
    "# End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7:  pg 418"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Example 14.7\n",
      " The power required to drive the vane pump is (kW) =  78.0\n"
     ]
    }
   ],
   "source": [
    "#pg 418\n",
    "print(' Example 14.7');\n",
    "\n",
    "# aim : To determine \n",
    "#  the power required to drive the vane pump\n",
    "\n",
    "# given values\n",
    "m_dot = 1;# air capacity, [kg/s]\n",
    "rp = 2;# pressure ratio\n",
    "P1 = 1*10**5;# intake pressure, [N/m^2]\n",
    "T1 = 273.+70;# intake temperature, [K]\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "rv = .7;# volume ratio\n",
    "\n",
    "# solution\n",
    "V1 = .995;# intake pressure(as given previous question),[m^3/s] \n",
    "# using P1*V1^Gama=P2*V2^Gama, so\n",
    "P2 = P1*(1/rv)**Gama;# pressure, [N/m^2]\n",
    "V2 = rv*V1;# volume,[m^3/s]\n",
    "P3 = rp*P1;# final pressure, [N/m^2]\n",
    "P = Gama/(Gama-1)*P1*V1*((P2/P1)**((Gama-1)/Gama)-1)+V2*(P3-P2);# power required,[W]\n",
    "#results\n",
    "print ' The power required to drive the vane pump is (kW) = ',round(P*10**-3)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 8: pg 420"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Example 14.8\n",
      " The power required to drive compressor is (kW) =  54.6\n",
      " The temperature in the engine is (C) =  109.19\n",
      " The pressure in the engine cylinder is (kN/m^2) =  160.5\n"
     ]
    }
   ],
   "source": [
    "#pg 420\n",
    "print(' Example 14.8');\n",
    "\n",
    "# aim : To determine \n",
    "#  the total temperature and pressure of the mixture\n",
    "\n",
    "# given values\n",
    "rp = 2.5;# static pressure ratio\n",
    "FC = .04;# fuel consumption rate, [kg/min]\n",
    "P1 = 60.;# inilet pressure, [kN/m^2]\n",
    "T1 = 273.+5;# inilet temperature, [K]\n",
    "n_com = .84;# isentropic efficiency of compressor\n",
    "Gama = 1.39;# heat capacity ratio\n",
    "C2 = 120.;#exit velocity from compressor, [m/s]\n",
    "rm = 13.;# air-fuel ratio\n",
    "cp = 1.005;# heat capacity ratio\n",
    "\n",
    "# solution\n",
    "P2 = rp*P1;# given condition, [kN/m^2]\n",
    "T2_prim = T1*(P2/P1)**((Gama-1)/Gama);# temperature after compression, [K]\n",
    "# using isentropic efficiency=(T2_prim-T1)/(T2-T1)\n",
    "T2 = T1+(T2_prim-T1)/n_com;#  final temperature, [K]\n",
    "m_dot = FC*(rm+1);# mass of air-fuel mixture, [kg/s]\n",
    "P = m_dot*cp*(T2-T1);# power to drive compressor, [kW]\n",
    "print ' The power required to drive compressor is (kW) = ',round(P,1)\n",
    "\n",
    "Tt2 = T2+C2**2/(2*cp*10**3);# total temperature,[K]\n",
    "Pt2 = P2*(Tt2/T2)**(Gama/(Gama-1));# total pressure, [kN/m^2]\n",
    "print ' The temperature in the engine is (C) = ',round(Tt2-273,2)\n",
    "print ' The pressure in the engine cylinder is (kN/m^2) = ',round(Pt2,1)\n",
    "\n",
    "print ' There is rounding mistake in the book'\n",
    "\n",
    "\n",
    "#  End\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}