{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 9 - Air Compressors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 1 - pg 9.18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a)Work required in a isothermal compression is (kJ/kg) =  155.813\n",
      "(b)Work required in a polytropic compression is (kJ/kg) =  181.578\n",
      "(c)Work required in a isentropic compression is (kJ/kg) =  203.47\n"
     ]
    }
   ],
   "source": [
    "#pg 9.18\n",
    "#calculate the Work required in all cases\n",
    "#Input data\n",
    "import math\n",
    "m=1.;#Mass of air that has to be compressed in kg\n",
    "P1=1.;#Initial pressure of a single stage reciprocating air compressor in bar\n",
    "P2=6.;#Final pressure in bar\n",
    "T1=303.;#Initial temperature of air in K\n",
    "n=1.2;#Polytropic index of air\n",
    "R=287.;#Gas constant for air in J/kg K\n",
    "r=1.4;#Isentropic index\n",
    "\n",
    "#Calculations\n",
    "W1=(m*R*T1*math.log(P2/P1))/1000;#Work required for compression in kJ/kg in Isothermal compression process\n",
    "W2=((n/(n-1))*m*R*T1*((P2/P1)**((n-1)/n)-1))/1000;#Work required for compression in a polytropic compression process in kJ/kg\n",
    "W3=((r/(r-1))*m*R*T1*((P2/P1)**((r-1)/r)-1))/1000;#Work required for compression in a Isentropic compression process in kJ/kg\n",
    "\n",
    "#Output\n",
    "print '(a)Work required in a isothermal compression is (kJ/kg) = ',round(W1,3)\n",
    "print '(b)Work required in a polytropic compression is (kJ/kg) = ',round(W2,3)\n",
    "print '(c)Work required in a isentropic compression is (kJ/kg) = ',round(W3,3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2 - pg 9.19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a)Length of the cylinder is (m) =  0.625\n",
      "(b)Diameter of the cylinder is (mm) =  351.0\n"
     ]
    }
   ],
   "source": [
    "#pg 9.19\n",
    "#calculate the Length and diameter of the cylinder\n",
    "#Input data\n",
    "Pi=60000.;#Indicated power of a double acting air compressor in W\n",
    "P1=1.;#Initial pressure in bar\n",
    "T1=293.;#Initial temperature in K\n",
    "n=1.2;#Polytropic index of the process\n",
    "P2=8.;#Final pressure in bar\n",
    "N=120.;#Speed at which the cylinder operates in rpm\n",
    "S=150.;#Average piston speed in m/min\n",
    "\n",
    "#Calculations\n",
    "L=S/(2*N);#Length of the stroke in m\n",
    "X=(3.14*L)/4;#X=V/D**2 i.e.,Volume of air before compression/square of the diameter in m\n",
    "Y=((n/(n-1))*P1*10**5*X*(((P2/P1)**((n-1)/n))-1));#Y=W/D**2 Work done by the compressor per cycle in N/m\n",
    "Nw=2*N;#Number of working strokes per minute since it is a double acting cylinder\n",
    "D=(((Pi*60)/(Y*Nw))**(0.5))*1000;#Diameter of the cylinder in mm\n",
    "\n",
    "#Output\n",
    "print '(a)Length of the cylinder is (m) = ',L\n",
    "print '(b)Diameter of the cylinder is (mm) = ',round(D,0)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3 -pg 9.20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The indicated power of the compressor is (kW) =  2.684\n"
     ]
    }
   ],
   "source": [
    "#pg 9.20\n",
    "#calculate the Indicated power of compressor\n",
    "#Input data\n",
    "D=0.15;#Diameter of a cylinder of a single acting reciprocating air compressor in m\n",
    "L=0.2;#Length of the stroke in m\n",
    "P1=1.;#The pressure at which compressor sucks air in bar\n",
    "P2=10.;#Final pressure in bar\n",
    "T1=298.;#Initial Temperature in K\n",
    "N=150.;#Operating speed of the compressor in rpm\n",
    "n=1.3;#Polytropic index of the process\n",
    "\n",
    "#Calculations\n",
    "V1=((3.14*D**2*L)/4);#Volume of air before compression in m**3\n",
    "W=((n/(n-1))*P1*10**5*V1*((P2/P1)**((n-1)/n)-1));#Work done by the compressor for a polytropic compression of air in Nm\n",
    "Pi=((W*N)/60)/1000;#Indicated power of the compressor in kW\n",
    "\n",
    "#Output\n",
    "print 'The indicated power of the compressor is (kW) = ',round(Pi,3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4 - pg 9.21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mass of air delivered per minute is (kg/min) =  5.64\n"
     ]
    }
   ],
   "source": [
    "#pg 9.21\n",
    "#calculate the mass of air delivered\n",
    "#Input data\n",
    "D=0.25;#Diameter of the cylinder of a single acting air compressor in m\n",
    "L=0.4;#Length of the stroke in m\n",
    "P1=1.;#Initial Pressure of the compressor in bar\n",
    "T1=303.;#Initial temperature of the compressor in K\n",
    "P2=6.;#Pressure during running in bar\n",
    "N=250.;#Operating speed of the compressor in rpm\n",
    "R=287.;#Gas constant in J/kg K \n",
    "\n",
    "#Calculations\n",
    "V1=(3.14*D**2*L)/4;#Volume of air before compression in m**3\n",
    "m=(P1*10**5*V1)/(R*T1);#Mass of air delivered by the compressor per stroke in kg/stroke\n",
    "Nw=N;#Since single acting cylinder number of working stroke is equal to Operating speed of the compressor in rpm\n",
    "ma=m*Nw;#Mass of air delivered per minute in kg/min\n",
    "\n",
    "#Output\n",
    "print 'Mass of air delivered per minute is (kg/min) = ',round(ma,2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 5 - pg 9.22"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temperature of air delivered by the compressor is (K) =  546.5\n"
     ]
    }
   ],
   "source": [
    "#pg 9.22\n",
    "#calculate the temperature of air\n",
    "#Input data\n",
    "P1=1.;#Initial pressure of a single acting compressor in bar\n",
    "P2=12.;#Final pressure in bar\n",
    "N=500.;#Operating speed of the compressor in rpm\n",
    "T1=308.;#Inlet air temperature in K\n",
    "n=1.3;#Polytropic index\n",
    "\n",
    "#Calculations\n",
    "T2=T1*(P2/P1)**((n-1)/n);#Temperature of air delivered by the compressor in K\n",
    "\n",
    "#Output\n",
    "print 'Temperature of air delivered by the compressor is (K) = ',round(T2,1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6 - pg 9.22"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a)Temperature at the end of isentropic compression is (K) =  555.06\n",
      "(b)Temperature at the end of polytropic compression is (K) =  486.49\n",
      "(c)Temperature at the end of isotropic compression is (K) =  293.0\n"
     ]
    }
   ],
   "source": [
    "#pg 9.22\n",
    "#calculate the temperature in all cases\n",
    "#Input data\n",
    "P1=1.;#Pressure at which air is sucked by a compressor in bar\n",
    "T1=293.;#Initial temperature in K\n",
    "P2=9.;#Delivery pressure after compression in bar\n",
    "r=1.41;#Isentropic index\n",
    "n=1.3;#Polytropic index\n",
    "\n",
    "#Calculations\n",
    "T21=T1*((P2/P1)**((r-1)/r));#Temperature at the end of isentropic compression process in K\n",
    "T22=T1*((P2/P1)**((n-1)/n));#Temperature at the end of isentropic compression process in K\n",
    "T23=T1;#Temperature at the end of isotropic compression process in K (Temperature remains constant)\n",
    "\n",
    "#Output\n",
    "print '(a)Temperature at the end of isentropic compression is (K) = ',round(T21,2)\n",
    "print '(b)Temperature at the end of polytropic compression is (K) = ',round(T22,2)\n",
    "print '(c)Temperature at the end of isotropic compression is (K) = ',T23\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7 - pg 9.23"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a)Work done by air during suction is (J) =  7000.0\n",
      "(b)Work done on air during compression is (J) =  -14754.0\n",
      "(c)Work done on air during delivery is (J) =  12902.0\n",
      "(d)Net work done on air during the cycle is (kJ) =  20.656\n"
     ]
    }
   ],
   "source": [
    "#pg 9.23\n",
    "#calculate the work done in all cases\n",
    "#Input data\n",
    "V1=0.07;#Displacement of the piston of a single stage single cylinder air compressor in m**3\n",
    "P1=1;#Initial pressure in bar\n",
    "T1=308;#Initial temperature of air in K\n",
    "P2=8.5;#Pressure after the compression process in bar\n",
    "r=1.4;#Isentropic compression \n",
    "\n",
    "#Calculations\n",
    "V2=V1*((P1/P2)**(1/1.4));#Final volume of the cylinder in m**3\n",
    "W1=P1*10**5*V1;#Work done by air during suction in Nm (or) J\n",
    "W2=(P1*10**5*V1*(1-(P2/P1)**((r-1)/r)))/(r-1);#Work done by air during compression in Nm or J\n",
    "Wa1=P2*10**5*V2;#Work done on air during delivery in Nm or J\n",
    "Wa2=((-W2)+Wa1-W1)/1000;#Net work done on air during the cycle in kJ\n",
    "\n",
    "#Output\n",
    "print '(a)Work done by air during suction is (J) = ',W1\n",
    "print '(b)Work done on air during compression is (J) = ',round(W2,0)\n",
    "print '(c)Work done on air during delivery is (J) = ',round(Wa1,0)\n",
    "print '(d)Net work done on air during the cycle is (kJ) = ',round(Wa2,3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 8 - pg 9.25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a)Work done by air during suction is (Nm) =  5000.0\n",
      "(b)Work done on air during Isothermal compression is (Nm) =  -9730.0\n",
      "(c)Heat transferred during this process is (J) =  9730.0\n",
      "(d)Work done on air during delivery is (Nm) =  5000.0\n",
      "(e)Net work done during the cycle is (Nm) =  9729.0\n"
     ]
    }
   ],
   "source": [
    "#pg 9.25\n",
    "#calculate the work done in all cases\n",
    "import math\n",
    "#Input data\n",
    "V1=0.05;#displacement of a piston of a single cylinder single stage reciprocating compressor in m**3\n",
    "P1=1.;#pressure of air sucked in the compressor in bar\n",
    "T1=300.;#Initial Temperature of air in K\n",
    "P2=7.;#Pressure after the compression process in bar\n",
    "\n",
    "#Calculations\n",
    "V2=(P1*V1)/P2;#Volume after the compression in m**3\n",
    "W1=P1*10**5*V1;#Work done by air during suction in Nm\n",
    "W2=P1*10**5*V1*math.log(V2/V1);#Work done on sir during isothermal compression in Nm\n",
    "H=-W2;#Heat transferred to the cylinder walls in Nm or J\n",
    "W3=P1*10**5*V1;#Work done on air during delivery in Nm\n",
    "Wn=W1+(-W2)-W3;#Net work done during the cycke in N m\n",
    "\n",
    "#Output\n",
    "print '(a)Work done by air during suction is (Nm) = ',W1\n",
    "print '(b)Work done on air during Isothermal compression is (Nm) = ',round(W2,0)\n",
    "print '(c)Heat transferred during this process is (J) = ',round(H,0)\n",
    "print '(d)Work done on air during delivery is (Nm) = ',W3\n",
    "print '(e)Net work done during the cycle is (Nm) = ',math.floor(Wn)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9 - pg 9.26"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Power required to compress and deliver 2kg of air per minute is (kW) =  7.296\n"
     ]
    }
   ],
   "source": [
    "#pg 9.26\n",
    "#calculate the Power required\n",
    "#Input data\n",
    "m=2.;#Mass of air delivered per second in kg\n",
    "P1=1.;#Initial pressure of a single stage compressor in bar\n",
    "T1=293.;#Initial temperature in K\n",
    "P2=7.;#Final pressure in bar\n",
    "n=1.4;#Polytropic index\n",
    "R=287.;#Gas constant in J/kg K\n",
    "\n",
    "#Calculations\n",
    "W=((n/(n-1))*m*R*T1*(((P2/P1)**((n-1)/n))-1))/(60*1000);#Work done by compressor in kW\n",
    "\n",
    "#Output\n",
    "print 'Power required to compress and deliver 2kg of air per minute is (kW) = ',round(W,3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 10 - pg 9.27"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Work done by the compressor per cycle is (Nm) =  781.0\n"
     ]
    }
   ],
   "source": [
    "#pg 9.27\n",
    "#calculate the Work done\n",
    "#Input data\n",
    "import math\n",
    "D=0.15;#Diameter of the bore of a single stage single acting reciprocating air compressor in m\n",
    "L=0.225;#Stroke length in m\n",
    "P1=1;#Pressure of air received in bar\n",
    "T1=308.;#Temperature of initial air in K\n",
    "P2=6.5;#Delivery pressure in bar\n",
    "n=1.3;#Polytropic index\n",
    "\n",
    "#Calculations\n",
    "Vs=(math.pi*D**2*L)/4;#Stroke volume of the compressor in m**3\n",
    "Vc=0.05*Vs;#Clearance volume in m**3\n",
    "V1=Vs+Vc;#Initial volume of air in m**3\n",
    "V4=Vc*(P2/P1)**(1/n);#The air in the clearance volume expands during suction stroke in m**3\n",
    "V=V1-V4;#Effective swept volume in m**3\n",
    "W=((n/(n-1))*P1*10**5*(V1-V4)*(((P2/P1)**((n-1)/n))-1));#Work done by the compressor per cycle in Nm\n",
    "\n",
    "#Output\n",
    "print 'Work done by the compressor per cycle is (Nm) = ',round(W,0)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 11 - pg 9.28"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a)Free air delivered per cycle is (m^3) =  0.000356\n",
      "(b)Free air delivered per minute is (m^3/min) =  0.1424\n"
     ]
    }
   ],
   "source": [
    "#pg 9.28\n",
    "#calculate the free air delivered\n",
    "#Input data\n",
    "D=0.1;#Diameter of the bore of a single acting compressor in m\n",
    "L=0.1;#Length of the stroke in m\n",
    "N=400.;#Operating speed of the compressor in in rpm\n",
    "Vc=0.00008;#Clearance volume in m**3\n",
    "n=1.2;#Polytropic index\n",
    "T1=303.;#Initial temperature in K\n",
    "Tf=293.;#Final temperature in K\n",
    "P1=0.95;#Initial pressure in bar\n",
    "P2=8.;#Final pressure in bar\n",
    "Pf=1.013;#Free air pressure in bar\n",
    "\n",
    "#Calculations\n",
    "Vs=(3.14*D**2*L)/4.;#Stroke volume of the compressors in m**3\n",
    "V1=Vc+Vs;#Initial volume of air is equal to cylinder volume in m**3\n",
    "V4=Vc*(P2/P1)**(1/n);#Air in the clearance volume expands during suction stroke to V4\n",
    "Ve=V1-V4;#Effective swept volume in m**3\n",
    "Vf=(P1*(V1-V4)*Tf)/(T1*Pf);#Free air delivered per cycle can be obtained in m**3\n",
    "A=Vf*N;#Free air delivered per minute in m**3/min\n",
    "\n",
    "#Output\n",
    "print '(a)Free air delivered per cycle is (m^3) = ',round(Vf,6)\n",
    "print '(b)Free air delivered per minute is (m^3/min) = ',round(A,4)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 12 - pg 9.29"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Power of the compressor when it runs at 300 rpm is (kW) =   10.718\n"
     ]
    }
   ],
   "source": [
    "#pg 9.29\n",
    "#calculate the Power of the compressor\n",
    "#Input data\n",
    "P1=1.;#Pressure of air drawn by a two stage single acting reciprocating air compressor in bar\n",
    "T1=293.;#Initial temperature in K\n",
    "P3=60.;#Final pressure after the compression in bar\n",
    "P2=10.;#Pressure after compression in the LP cylinder in bar\n",
    "T2=303.;#Temperature after cooling in K\n",
    "D=0.16;#Diameter of a cylinder in m\n",
    "L=0.2;#Stroke length of the cylinder in m\n",
    "n=1.3;#Polytropic index\n",
    "N=300.;#Operating speed of the compressor in rpm\n",
    "R=287.;#Gas constant in J/kg K\n",
    "\n",
    "#Calculations\n",
    "V1=(3.14*D**2*L)/4;#Volume of the LP cylinder in m**3\n",
    "V2=(P1*V1*T2)/(T1*P2);#Volume of the HP cylinder in m**3\n",
    "W=(n/(n-1))*(P1*10**5*V1*(((P2/P1)**((n-1)/n))-1)+(P2*10**5*V2*(((P3/P2)**((n-1)/n))-1)));#Work done by the compressor per working cycle in N m\n",
    "P=(W*N)/(60.*1000);#Power of the compressor in kW\n",
    "\n",
    "#Output\n",
    "print 'Power of the compressor when it runs at 300 rpm is (kW) =  ',round(P,3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13 - pg 9.30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Percentage saving in the work of compression of air in two stages instead of single stage is (percent) =  12.6\n"
     ]
    }
   ],
   "source": [
    "#pg 9.30\n",
    "#calculate the percentage saving\n",
    "#Input data\n",
    "P1=1.;#Initial pressure in bar\n",
    "P3=9.;#Final pressure in bar\n",
    "n=1.3;#Compression index\n",
    "\n",
    "#Calculations\n",
    "W1=(n/(n-1))*(P1*10**5*(((P3/P1)**((n-1)/n))-1));#Work done in compression in a single stage per unit volume per kg of air in N m \n",
    "P2=(P1*P3)**(0.5);#Intercooler pressure for perfect intercooling in bar\n",
    "W2=2*(n/(n-1))*(P1*10**5*(((P2/P1)**((n-1)/n))-1));#Work done in compression in a two stage compressor per unit volume per kg of air in N m\n",
    "Wc=W1-W2;#Saving in work of compression in N m\n",
    "nw=((W1-W2)/W1)*100;#Percentage saving in work of compression in percentage\n",
    "\n",
    "#Output\n",
    "print 'Percentage saving in the work of compression of air in two stages instead of single stage is (percent) = ',round(nw,1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 14 - pg 9.31"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Minimum work required to compress 1kg of air for given conditions is (Nm) =  338979.0\n"
     ]
    }
   ],
   "source": [
    "#pg 9.31\n",
    "#calculate the Minimum work required\n",
    "#Input data\n",
    "m=1.;#Mass of air to be compressed in kg\n",
    "P1=1.;#Pressure of air before compression in bar\n",
    "T1=303.;#Initial temperature in K\n",
    "P3=25.;#Final pressure of air after compression in bar\n",
    "n=1.3;#Polytropic index\n",
    "R=287.;#Gas constant in J/kg K\n",
    "\n",
    "#Calculations\n",
    "P2=(P1*P3)**(0.5);#Intermediate pressure in the case of perfect intercooling in bar\n",
    "W=2*(n/(n-1))*(m*R*T1*(((P2/P1)**((n-1)/n))-1));#Work done in compression in a two stage compressor per unit volume per kg of air in N m\n",
    "\n",
    "#Output data\n",
    "print 'Minimum work required to compress 1kg of air for given conditions is (Nm) = ',round(W,0)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 15 - pg 9.32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The minimum power required to drive the compressor is (kW) =  12.524\n"
     ]
    }
   ],
   "source": [
    "#pg 9.32\n",
    "#calculate the minimum power\n",
    "#Input data\n",
    "V1=3;#Volume of air sucked in by a two stage compressor in m**3\n",
    "P1=1.04;#Initial pressure in bar\n",
    "T1=298;#Initial temperature in K\n",
    "P2=9;#Delivery pressure in bar\n",
    "n=1.25;#Polytropic index\n",
    "\n",
    "#Calculations\n",
    "P2=(P1*P2)**(0.5);#Intermediate pressure for perfect intercooling and for minimum work of compression in bar\n",
    "W=2*(n/(n-1))*(P1*10**5*V1*(((P2/P1)**((n-1)/n))-1));#Work done in compression in a two stage compressor per unit volume per kg of air in Nm\n",
    "P=W/(60*1000);#Power required to drive the compressor in kW\n",
    "\n",
    "#Output\n",
    "print 'The minimum power required to drive the compressor is (kW) = ',round(P,3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 16 - pg 9.32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mass of water to circulate in the intercooler for abstracting heat is (kg) =  2.098\n"
     ]
    }
   ],
   "source": [
    "#pg 9.32\n",
    "#calculate the mass of water\n",
    "#Input data\n",
    "P1=1.;#Initial pressure of a two stage air compressor in bar\n",
    "P3=36.;#Final pressure in bar\n",
    "T1=298.;#Initial temperature in K\n",
    "n=1.35;#Polytropic index\n",
    "T3=298.;#Temperature after intercooling in K\n",
    "Tc=20.;#Permissible temperature rise of the cooling water in K\n",
    "R=287.;#Gas constant in J/kg K\n",
    "Cp=1.;#Specific heat of air in kJ/kg K\n",
    "Cw=4.2;#Specific heat of water in kJ/kg K\n",
    "ma=1.;#Mass of air in the compressor in kg\n",
    "\n",
    "#Calculations\n",
    "P2=(P1*P3)**(0.5);#Intercooler pressure for complete intercooling and for minimum work of compression in bar\n",
    "T2=T1*(P2/P1)**((n-1)/n);#Temperature after the compression process in K\n",
    "mw=(ma*Cp*(T2-T3))/(Cw*(Tc));#Mass of water to circulate in the intercooler per kg of air in kg\n",
    "\n",
    "#Output\n",
    "print 'Mass of water to circulate in the intercooler for abstracting heat is (kg) = ',round(mw,3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 17 - pg 9.33"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The volume ratio of LP to HP cylinders =  2.83\n"
     ]
    }
   ],
   "source": [
    "#pg 9.33\n",
    "#calculate the volume ratio\n",
    "#Input data\n",
    "V1=0.2;#Volume of air flow per second in a two stage single acting reciprocating compressor in m**3\n",
    "P1=0.1;#Intake pressure of air in MPa\n",
    "T1=293.;#Initial temperature in K\n",
    "P3=0.8;#Final pressure after the air is compressed in MPa\n",
    "N=600.;#Operating speed of the compressor in rpm\n",
    "\n",
    "#Calculations\n",
    "P2=(P1*P3)**(0.5);#Intercooler pressure for perfect intercooling and for minimum work of compression in bar\n",
    "Vl=(V1*60)/600;#Volume of the LP cylinder in m**3\n",
    "Vh=(P1*Vl)/P2;#Volume of the high pressure cylinder in m**3\n",
    "R=Vl/Vh;#Ratio of cylinder volumes\n",
    "\n",
    "#Output\n",
    "print 'The volume ratio of LP to HP cylinders = ',round(R,2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 18 - pg 9.34"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The ratio of cylinder diameters for the efficiency of compression to be maximum  =  2.236\n"
     ]
    }
   ],
   "source": [
    "#pg 9.34\n",
    "#calculate the ratio required\n",
    "#Input data\n",
    "P1=1.;#Initial pressure of air entering a two stage air compressor with complete intercooling in bar\n",
    "P3=25.;#Delivery pressure of air toe the mains in bar\n",
    "T1=303.;#Initial temperature in K\n",
    "n=1.35;#Compression index\n",
    "\n",
    "#Calculations\n",
    "P2=(P1*P3)**(0.5);#Inter cooler pressure for perfect intercooling in bar\n",
    "R=(P2/P1)**(0.5);#Ratio of cylindrical diameters\n",
    "\n",
    "#Output\n",
    "print 'The ratio of cylinder diameters for the efficiency of compression to be maximum  = ',round(R,3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 19 - pg 9.34"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a)Number of stages =  4\n",
      "(b)Intermediate pressures are, P2 =  3.31 bar, P3 =  10.95 bar, P4 = 36.26 bar\n"
     ]
    }
   ],
   "source": [
    "#pg 9.34\n",
    "#calculate the number of stages and Intermediate pressures\n",
    "#Input data\n",
    "import math\n",
    "P1=1.;#Initial pressure of a multistage compression in bar\n",
    "Pn1=120.;#Final pressure in bar\n",
    "r=4;#Permissible pressure ratios per stage\n",
    "\n",
    "#Calculations\n",
    "n=math.log(Pn1/P1)/math.log(r)\n",
    "n1=4;#As n=3.45 say 4 stages\n",
    "P5=Pn1;#Since number of stages is 4\n",
    "P4=P5/(Pn1/P1)**(1./n1);#Pressure after the stage 3 in bar\n",
    "P3=P4/(Pn1/P1)**(1./n1);#Pressure after the stage 2 in bar\n",
    "P2=P3/(Pn1/P1)**(1./n1);#Pressure after the stage 1 in bar\n",
    "\n",
    "#Output\n",
    "print '(a)Number of stages = ',n1\n",
    "print '(b)Intermediate pressures are, P2 = ',round(P2,2),'bar, P3 = ',round(P3,2),'bar, P4 =',round(P4,2),'bar'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 20 - pg 9.35"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a)Power required to deliver 15 m^3/min air at suction condition is (kW) =  106.6\n",
      "(b)Intermediate pressures are P2 =  3.42 bar P3 =  11.696 bar\n"
     ]
    }
   ],
   "source": [
    "#pg 9.35\n",
    "#calculate the Power required and Intermediate pressures\n",
    "#Input data\n",
    "P1=1.;#Initial pressure of a 3 stage compressor in bar\n",
    "P4=40.;#Final pressure in bar\n",
    "T1=293.;#Initial temperature in K\n",
    "n=1.3;#Polytropic index\n",
    "V1=15.;#Air delivered per minute in m**3/min\n",
    "\n",
    "#Calculations\n",
    "W=((3*n)/(n-1))*P1*10**5*V1*(((P4/P1)**((n-1)/(3*n)))-1);#Work done by the compressor in kJ/min\n",
    "P=W/(60*1000.);#Power required to deliver 15 m**3/min air in kW\n",
    "P2=P1*(P4/P1)**(1./3);#Intermediate pressure after stage 1 in bar\n",
    "P3=P2*(P4/P1)**(1./3);#Intermediate pressure after stage 2 in bar\n",
    "\n",
    "#Output\n",
    "print '(a)Power required to deliver 15 m^3/min air at suction condition is (kW) = ',round(P,1)\n",
    "print '(b)Intermediate pressures are P2 = ',round(P2,2),'bar P3 = ',round(P3,3),'bar'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 21 - pg 9.36"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Amount of heat rejected in each intercooler is (kJ) =  113.0\n"
     ]
    }
   ],
   "source": [
    "#pg 9.36\n",
    "#calculate the Amount of heat rejected\n",
    "#Input data\n",
    "P1=1.;#Atmospheric pressure in bar\n",
    "P4=60.;#Delivery pressure in bar\n",
    "T1=303.;#Initial temperature in K\n",
    "n=1.3;#Index of compression\n",
    "Cp=1.005;#Specific heat of air at constant pressure in kJ/kg K\n",
    "S=3.;#Number of stages\n",
    "\n",
    "#Calculations\n",
    "P2=P1*(P4/P1)**(1./3);#Intermediate pressure in bar\n",
    "T2=T1*(P2/P1)**((n-1)/n);#Temperature of air entering the intercoolers in K\n",
    "H=Cp*(T2-T1);#Heat rejected in each intercooler in kJ\n",
    "\n",
    "#Output\n",
    "print 'Amount of heat rejected in each intercooler is (kJ) = ',round(H,0)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 22 - pg 9.37"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a)L.P. and I.P.compressor delivery pressure is P2 =  4.021 bar P3 = 16.17 bar\n",
      "(b)Ratio of cylinder volumes is V1:V2:V3 =  16.17 : 4.021 : 1\n",
      "(c)Total indicated power is (kW) =  72.2\n"
     ]
    }
   ],
   "source": [
    "#pg 9.37\n",
    "#calculate the compressor delivery pressure and Ratio of cylinder volumes\n",
    "#Input data\n",
    "P1=1.;#Pressure at the end of suction stroke in LP cylinder of a 3 stage single acting reciprocating compressor in bar\n",
    "T1=293.;#Temperature at the end of suction stroke in LP cylinder in K\n",
    "V=9.;#Free air delivered by the compressor in m**3\n",
    "P4=65.;#Pressure delivered by the compressor in bar\n",
    "n=1.25;#Polytropic index\n",
    "\n",
    "#Calculations\n",
    "P2=P1*(P4/P1)**(1./3);#Intermediate pressure after stage 1 in bar\n",
    "P3=P2*(P4/P1)**(1./3);#Intermediate pressure after stage 2 in bar\n",
    "V3=1;#The volume of cylinder for the third stage in m**3\n",
    "V2=V3*(P3/P2);#Volume of the cylinder for second stage in m**3\n",
    "V1=(P2/P1)*V2;#Volume of the cylinder for first stage in m**3\n",
    "W=(((3*n)/(n-1))*P1*10**5*V*(((P4/P1)**((n-1)/(3*n)))-1))/1000;#Work done by the compressor in kJ/min\n",
    "Pi=W/60;#Indicated power in kW\n",
    "\n",
    "#Output\n",
    "print '(a)L.P. and I.P.compressor delivery pressure is P2 = ',round(P2,3),'bar P3 =',round(P3,2),'bar'\n",
    "print '(b)Ratio of cylinder volumes is V1:V2:V3 = ',round(V1,2),':',round(V2,3),':',V3\n",
    "print '(c)Total indicated power is (kW) = ',round(Pi,2)\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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}