{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 16 - Internal combustion engines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 1: pg 553"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 16.1\n",
      " (a) The net power output is (kW) =  1014.0\n",
      " (b) The thermal efficiency of the plant is (percent) =  32.0\n",
      " (c) The work ratio is  =   0.446\n"
     ]
    }
   ],
   "source": [
    "#pg 553\n",
    "print('Example 16.1');\n",
    "\n",
    "# aim : To determine \n",
    "# (a) the net power output of the turbine plant if the turbine is coupled to the compresser\n",
    "# (b) the thermal efficiency of the plant\n",
    "# (c) the work ratio\n",
    "\n",
    "# Given values\n",
    "P1 = 100.;# inlet pressure of compressor, [kN/m^2]\n",
    "T1 = 273.+18;# inlet temperature, [K]\n",
    "P2 = 8*P1;# outlet pressure of compressor, [kN/m^2]\n",
    "n_com = .85;# isentropic efficiency of compressor\n",
    "T3 = 273.+1000;#inlet temperature of turbine, [K]\n",
    "P3 = P2;# inlet pressure of turbine, [kN/m^2]\n",
    "P4 = 100.;# outlet pressure of turbine, [kN/m^2]\n",
    "n_tur = .88;# isentropic efficiency of turbine\n",
    "m_dot = 4.5;# air mass flow rate, [kg/s]\n",
    "cp = 1.006;# [kJ/kg K]\n",
    "Gamma = 1.4;# heat capacity ratio\n",
    "\n",
    "# (a)\n",
    "# For the compressor\n",
    "T2_prime = T1*(P2/P1)**((Gamma-1)/Gamma);# [K]\n",
    "T2 = T1+(T2_prime-T1)/n_com;#  exit pressure of compressor, [K]\n",
    "\n",
    "# for turbine\n",
    "T4_prime = T3*(P4/P3)**((Gamma-1)/Gamma);# [K]\n",
    "T4 = T3-(T3-T4_prime)*n_tur;# exit temperature of turbine, [K]\n",
    "\n",
    "P_output = m_dot*cp*((T3-T4)-(T2-T1));# [kW]\n",
    "print ' (a) The net power output is (kW) = ',round(P_output)\n",
    "\n",
    "# (b)\n",
    "n_the = ((T3-T4)-(T2-T1))/(T3-T2)*100;# thermal efficiency\n",
    "print ' (b) The thermal efficiency of the plant is (percent) = ',round(n_the)\n",
    "\n",
    "# (c)\n",
    "P_pos = m_dot*cp*(T3-T4);# Positive cycle work, [kW]\n",
    "\n",
    "W_ratio = P_output/P_pos;# work ratio\n",
    "print ' (c) The work ratio is  =  ',round(W_ratio,3)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2: pg 554"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 16.2\n",
      " (a) The pressure ratio which give the maximum network output is  =  14.74\n",
      " (b) The maximum net specific work output is (kJ/kg) =  401.0\n",
      " (c) The thermal efficiency at maximum work output is (percent) =   54.0\n",
      " (d) The work ratio at maximum work output is  =  0.54\n",
      " (e) The carnot efficiency within the cycle temperature limits is (percent) =  79.0\n"
     ]
    }
   ],
   "source": [
    "#pg 554\n",
    "print('Example 16.2');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the pressure ratiowhich will give the maximum net work output\n",
    "# (b) the maximum net specific work output\n",
    "# (c) the thermal efficiency at maximum work output\n",
    "# (d) the work ratio at maximum work output\n",
    "# (e) the carnot efficiency within the cycle temperature limits\n",
    "from math import sqrt\n",
    "# Given values\n",
    "# taking the refrence as Fig.16.35\n",
    "T3 = 273.+1080;# [K]\n",
    "T1 = 273.+10;# [K]\n",
    "cp = 1.007;# [kJ/kg K]\n",
    "Gamma = 1.41;#  heat capacity ratio\n",
    "\n",
    "# (a)\n",
    "r_pmax = (T3/T1)**((Gamma)/(Gamma-1));# maximum pressure ratio\n",
    "# for maximum net work output\n",
    "r_p = sqrt(r_pmax);\n",
    "print ' (a) The pressure ratio which give the maximum network output is  = ',round(r_p,2)\n",
    "\n",
    "# (b)\n",
    "T2 = T1*(r_p)**((Gamma-1)/Gamma);# [K]\n",
    "# From equation [23]\n",
    "T4 = T2;\n",
    "W_max = cp*((T3-T4)-(T2-T1));# Maximum net specific work output, [kJ/kg]\n",
    "\n",
    "print ' (b) The maximum net specific work output is (kJ/kg) = ',round(W_max)\n",
    "\n",
    "# (c)\n",
    "W = cp*(T3-T2);\n",
    "n_the = W_max/W;# thermal efficiency\n",
    "print ' (c) The thermal efficiency at maximum work output is (percent) =  ',round(n_the*100)\n",
    "\n",
    "# (d)\n",
    "# From the equation [26]\n",
    "W_ratio = n_the;# Work ratio\n",
    "print ' (d) The work ratio at maximum work output is  = ',round(W_ratio,2)\n",
    "\n",
    "# (e)\n",
    "n_carnot = (T3-T1)/T3*100;# carnot efficiency\n",
    "print ' (e) The carnot efficiency within the cycle temperature limits is (percent) = ',round(n_carnot)\n",
    "\n",
    "# End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3: pg 558"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 16.3\n",
      " (a) The net power output of the plant is (kW) =  562.0\n",
      " (b) The exhaust temperature from the heat exchanger is (C) =  333.0\n",
      " (c) The thermal efficiency of the plant is (percent) =  30.5\n",
      " (d) The thermal efficiency of the plant if there wereno heat exchanger is (percent) =  22.3\n",
      " (e) The work ratio is  =   0.38\n"
     ]
    }
   ],
   "source": [
    "#pg 558\n",
    "print('Example 16.3');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the net power output of the plant\n",
    "# (b) the exhaust temperature from the heat exchanger\n",
    "# (c) the thermal efficiency of the plant\n",
    "# (d) the thermal efficiency of the plant if there were no heat exchanger\n",
    "# (e) the work ratio\n",
    "\n",
    "# Given values\n",
    "T1 = 273.+15;# temperature, [K]\n",
    "P1 = 101.;# pressure, [kN/m^2]\n",
    "P2 = 6*P1; # [kN/m^2]\n",
    "eff = .65;# effectiveness of the heat exchanger, \n",
    "T3 = 273.+870;# temperature, [K]\n",
    "P4 = 101.;# [kN/m^2]\n",
    "n_com = .85;# efficiency of compressor, \n",
    "n_tur = .80;# efficiency of turbine\n",
    "m_dot = 4.;# mass flow rate, [kg/s]\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "cp = 1.005;# [kJ/kg K]\n",
    "\n",
    "# solution\n",
    "# (a)\n",
    "# For compressor\n",
    "T2_prim = T1*(P2/P1)**((Gama-1)/Gama);# [K]\n",
    "\n",
    "# using n_com = (T2_prim-T1)/(T2-T1)')\n",
    "\n",
    "T2 = T1+(T2_prim-T1)/n_com\n",
    "# For turbine\n",
    "P3 = P2;\n",
    "T4_prim = T3*(P4/P3)**((Gama-1)/Gama);# [K]\n",
    "\n",
    "T4=T3-n_tur*(T3-T4_prim); #  [K]\n",
    "P_out = m_dot*cp*((T3-T4)-(T2-T1));#  net power output, [kW]\n",
    "print ' (a) The net power output of the plant is (kW) = ',round(P_out)\n",
    "\n",
    "# (b)\n",
    "mtd = T4-T2;# maximum temperature drop for heat transfer, [K]\n",
    "atd = eff*mtd;# actual temperature, [K]\n",
    "et = T4-atd;# Exhaust temperature from heat exchanger, [K]\n",
    "t6 = et-273;# [C]\n",
    "print ' (b) The exhaust temperature from the heat exchanger is (C) = ',round(t6)\n",
    "\n",
    "# (c)\n",
    "T5 = T2+atd;# [K]\n",
    "n_the = ((T3-T4)-(T2-T1))/(T3-T5)*100;# thermal effficiency \n",
    "print ' (c) The thermal efficiency of the plant is (percent) = ',round(n_the,1)\n",
    "\n",
    "# (d)\n",
    "# with no heat exchanger\n",
    "n_the = ((T3-T4)-(T2-T1))/(T3-T2)*100;# thermal efficiency without heat exchanger\n",
    "print ' (d) The thermal efficiency of the plant if there wereno heat exchanger is (percent) = ',round(n_the,1)\n",
    "\n",
    "# (e)\n",
    "P_pos = m_dot*cp*(T3-T4);# positive cycle work;# [kW]\n",
    "w_rat = P_out/P_pos;# work ratio\n",
    "print ' (e) The work ratio is  =  ',round(w_rat,2)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4: pg 562"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 16.4\n",
      " (a) The temperature as the air leaves the compressor turbine is (C) =  701.0\n",
      "      The pressure as the air leaves the compressor turbine is (kN/m^2) =  288.0\n",
      " (b) The power output from the free power turbine is (kW) =  1541.0\n",
      " (c) The thermal efficiency of the plant is (percent) =  32.0\n",
      " (d) The work ratio is  =  0.44\n",
      " (e) The carnot efficiency is (percent) =  77.0\n",
      "The answers are a bit different due to rounding off error in textbook\n"
     ]
    }
   ],
   "source": [
    "#pg 562\n",
    "print('Example 16.4');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the pressure and temperature as the air leaves the compressor turbine\n",
    "# (b) the power output from the free power turbine\n",
    "# (c) the thermal efficiency of the plant\n",
    "# (d) the work ratio\n",
    "# (e) the carnot efficiency within the cycle temperature limits\n",
    "\n",
    "# Given values\n",
    "T1 = 273.+19;# temperature, [K]\n",
    "P1 = 100.;# pressure, [kN/m^2]\n",
    "P2 = 8*P1; # [kN/m^2]\n",
    "P3 = P2;# [kN/m^2]\n",
    "T3 = 273.+980;# temperature, [K]\n",
    "n_com = .85;# efficiency of rotary compressor\n",
    "P5 = 100.;# [kN/m^2]\n",
    "n_cum = .88;# isentropic efficiency of combustion chamber compressor, \n",
    "n_tur = .86;# isentropic efficiency of turbine\n",
    "m_dot = 7.;# mass flow rate of air, [kg/s]\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "cp = 1.006;# [kJ/kg K]\n",
    "\n",
    "# solution\n",
    "# (a)\n",
    "# For compressor\n",
    "T2_prim = T1*(P2/P1)**((Gama-1)/Gama);# [K]\n",
    "\n",
    "T2 = T1+(T2_prim-T1)/n_com;# temperature, [K]\n",
    "\n",
    "# for compressor turbine\n",
    "# T3-T4 = T2-T1,because compressor turbine power=compressor power so\n",
    "T4 = T3-(T2-T1);#turbine exit temperature, [K]\n",
    "T4_prim = T3-(T3-T4)/n_cum;# [K]\n",
    "\n",
    "# For turbine\n",
    "# T4_prim = T3*(P4/P3)^((Gama-1)/Gama)\n",
    "P4 = P3*(T4_prim/T3)**(Gama/(Gama-1));# exit air pressure of air, [kN/m^2]\n",
    "\n",
    "print ' (a) The temperature as the air leaves the compressor turbine is (C) = ',round(T4-273)\n",
    "print '      The pressure as the air leaves the compressor turbine is (kN/m^2) = ',round(P4)\n",
    "\n",
    "# (b)\n",
    "T5_prim = T4*(P5/P4)**((Gama-1)/Gama);# [K]\n",
    "\n",
    "\n",
    "T5 = T4-n_tur*(T4-T5_prim);# temperature, [K]\n",
    "\n",
    "PO = m_dot*cp*(T4-T5);# power output\n",
    "print ' (b) The power output from the free power turbine is (kW) = ',round(PO)\n",
    "\n",
    "# (c)\n",
    "\n",
    "n_the = (T4-T5)/(T3-T2)*100;# thermal effficiency \n",
    "print ' (c) The thermal efficiency of the plant is (percent) = ',round(n_the)\n",
    "\n",
    "# (d)\n",
    "\n",
    "WR = (T4-T5)/(T3-T5);# work ratio\n",
    "print ' (d) The work ratio is  = ',round(WR,2)\n",
    "\n",
    "# (e)\n",
    "CE = (T3-T1)/T3;# carnot efficiency\n",
    "print ' (e) The carnot efficiency is (percent) = ',round(CE*100)\n",
    "\n",
    "print 'The answers are a bit different due to rounding off error in textbook'\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 5: pg 564"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 16.5\n",
      " (a) The pressure of the air after compression is (bar) =  14100.0\n",
      "      The temperature of the air after compression is (C) =  469.6\n",
      " (b) The power developed by the gas turbine is (MW) =  51.66\n",
      " (c) The air pressure as it leaves the gas turbine is (bar) =  714.0\n",
      "Result in the book is not matching because they have taken pressure in mbar  but in in question it is given in bar\n"
     ]
    }
   ],
   "source": [
    "#pg 564\n",
    "print('Example 16.5');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the pressure and temperature of the air compression \n",
    "# (b) the power developed by the gas turbine\n",
    "# (c) the temperature and pressure of the airentering the exhaust jet as it leaves the gas turbine \n",
    "from math import log\n",
    "# Given values\n",
    "T1 = 273-22.4;# temperature, [K]\n",
    "P1 = 470.;# pressure, [bar]\n",
    "P2 = 30*P1; # [kN/m**2]\n",
    "P3 = P2;# [kN/m**2]\n",
    "T3 = 273.+960;# temperature, [K]\n",
    "r = 1.25;# ratio of turbine power to compressor power\n",
    "n_tur = .86;# isentropic efficiency of turbine\n",
    "m_dot = 80.;# mass flow rate of air, [kg/s]\n",
    "Gama = 1.41;# heat capacity ratio\n",
    "cp = 1.05;# [kJ/kg K]\n",
    "\n",
    "# solution\n",
    "# (a)\n",
    "# For compressor\n",
    "T2_prim = T1*(P2/P1)**((Gama-1)/Gama);# [K]\n",
    "# using n_tur=(T2_prim-T1)/(T2-T1)\n",
    "T2 = T1+(T2_prim-T1)/n_tur;# temperature, [K]\n",
    "\n",
    "print ' (a) The pressure of the air after compression is (bar) = ',P2\n",
    "\n",
    "print '      The temperature of the air after compression is (C) = ',round(T2-273,1)\n",
    "\n",
    "# (b)\n",
    "Td  = r*(T2-T1);# temperature drop in turbine, [K]\n",
    "PO = m_dot*cp*Td;# power output, [kW]\n",
    "print ' (b) The power developed by the gas turbine is (MW) = ',round(PO*10**-3,2)\n",
    "\n",
    "# (c)\n",
    "t3 = T3-273;# [C]\n",
    "t4 = t3-Td;# temeprerature of air leaving turbine,[K]\n",
    "Tdi = Td/n_tur;# isentropic temperature drop, [K]\n",
    "T4_prim = t3-Tdi+273;# temperature, [K]\n",
    "# using T4_prim=T3*(P4/P3)**((Gama-1)/Gama)\n",
    "P4 = P3*(T4_prim/T3)**(Gama/(Gama-1));# exit air pressure of air, [kN/m**2]\n",
    "\n",
    "print ' (c) The air pressure as it leaves the gas turbine is (bar) = ',round(P4,0)\n",
    "\n",
    "print 'Result in the book is not matching because they have taken pressure in mbar  but in in question it is given in bar'\n",
    "\n",
    "#   End\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6: pg 566"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 16.6\n",
      " (a) The mass of fuel oil used by the gas is (tonne/h) =  35.9\n",
      " (b) The mass flow  rate of steam from the boiler is (tonne/h) =  252.4\n",
      " (c) The theoretical output from the steam turbine is (MW) =  40.06\n",
      " (d) The overall thermal efficiency is (percent) =  44.3\n"
     ]
    }
   ],
   "source": [
    "#pg 566\n",
    "print('Example 16.6');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the mass of fuel oil used by the gas turbine\n",
    "# (b) the mass flow of steam from the boiler \n",
    "# (c) the theoretical output from the steam turbine\n",
    "# (d) the overall theoretical thermal efficiency of the plant\n",
    "\n",
    "# given values\n",
    "Po = 150.;# generating plant output, [MW]\n",
    "n_the1 = .35;# thermal efficiency\n",
    "CV = 43.;# calorific value of fuel, [MJ]\n",
    "me = 400.;# flow rate of exhaust gas, [kg/s]\n",
    "T = 90.;# boiler exit temperature, [C]\n",
    "T1 = 550.;# exhaust gas temperature, [C]\n",
    "P2 = 10.;# steam generation pressure, [MN/m**2]\n",
    "T2 = 450.;# boiler exit temperature, [C]\n",
    "Tf = 140.;# feed water temperature, [C]\n",
    "n_tur = .86;# turbine efficiency\n",
    "P3 = .5;# exhaust temperature, [MN/m**2]\n",
    "n_boi = .92;# boiler thermal efficiency\n",
    "cp = 1.1;# heat capacity, [kJ/kg]\n",
    "\n",
    "\n",
    "#  solution\n",
    "# (a)\n",
    "ER = Po*3600/n_the1;# energy requirement from the fuel, [MJ/h]\n",
    "mf = ER/CV*10**-3;# fuel required, [tonne/h]\n",
    "print ' (a) The mass of fuel oil used by the gas is (tonne/h) = ',round(mf,1)\n",
    "\n",
    "# (b) \n",
    "\n",
    "ET = me*cp*(T1-T)*3600*n_boi;# energy transferred to steam,[kJ/h]\n",
    "# from steam table\n",
    "h1 = 3244;# specific enthalpy, [kJ/kg]\n",
    "hf = 588.5;# specific enthalpy, [kJ/kg]\n",
    "ERR = h1-hf;# energy required to raise steam, [kJ/kg]\n",
    "ms = ET/ERR*10**-3;# mass flow of steam, [tonne/h]\n",
    "print ' (b) The mass flow  rate of steam from the boiler is (tonne/h) = ',round(ms,1)\n",
    "\n",
    "# again from steam table\n",
    "s1 = 6.424;# specific entropy, [kJ/kg K]\n",
    "sf2 = 1.86;# specific entropy, [kJ/kg K\n",
    "sg2 = 6.819;# specific entropy, [kJ/kg K]\n",
    "\n",
    "hf2 = 640.1;# specific enthalpy,[kJ/kg]\n",
    "hg2 = 2747.5;# specific enthalpy, [kJ/kg]\n",
    "# for ths process s1=s2=sf2+x2*(sg2-sf2)\n",
    "s2 = s1;\n",
    "# hence\n",
    "x2 = (s2-sf2)/(sg2-sf2);# dryness fraction\n",
    "\n",
    "h2_prim = hf2+x2*(hg2-hf2);# specific enthalpy of steam, [kJ/kg]\n",
    "\n",
    "TO = n_tur*(h1-h2_prim);#theoretical steam turbine output, [kJ/kg]\n",
    "TOt = TO*ms/3600.;# total theoretical steam turbine output, [MW]\n",
    "\n",
    "print ' (c) The theoretical output from the steam turbine is (MW) = ',round(TOt,2)\n",
    "\n",
    "# (d)\n",
    "n_tho = (Po+TOt)*n_the1/Po;# overall theoretical thermal efficiency\n",
    "print ' (d) The overall thermal efficiency is (percent) = ',round(n_tho*100,1)\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
}