{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CHAPTER 4 - Fuel-Air & Actual Cycles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 4.2 PAGE 187"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The percentage change in the efficiency of otto cycle = -0.71 %\n"
     ]
    }
   ],
   "source": [
    "from math import log\n",
    "# Initialisation of Variables\n",
    "r=8#..........................#Compression Ratio\n",
    "ga=1.4#.......................#Degree of freedom for the gas\n",
    "Cvinc=1.1#....................#Increase of specific heat at constant volume in percentage\n",
    "#Calculations\n",
    "eta=1-1/(r**(ga-1))#...........#efficiency of otto cycle\n",
    "deta=(1-eta)*(ga-1)*log(r)*(Cvinc/100)#............#Change in efficiency\n",
    "etach=-deta/eta#............................#Percentage change in efficiency of change in efficiency\n",
    "print \"The percentage change in the efficiency of otto cycle = %0.2f %%\"%(etach*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 4.3 PAGE 187"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The percentage change in the efficiency of otto cycle = -1.98 %\n"
     ]
    }
   ],
   "source": [
    "from math import log\n",
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "r=7#..........................#Compression Ratio\n",
    "ga=1.4#.......................#Degree of freedom for the gas\n",
    "Cvinc=3#....................#Increase of specific heat at constant volume in percentage\n",
    "#Calculations\n",
    "eta=1-1/(r**(ga-1))#...........#efficiency of otto cycle\n",
    "deta=(1-eta)*(ga-1)*log(r)*(Cvinc/100)#............#Change in efficiency\n",
    "etach=-deta/eta#............................#Percentage change in efficiency of change in efficiency\n",
    "print \"The percentage change in the efficiency of otto cycle = %0.2f %%\"%(etach*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 4.4 PAGE 188"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Percentage change = -1.15 efficiency\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import log\n",
    "# Initialisation of Variables\n",
    "r=18#..........................#Compression Ratio\n",
    "co=5#..........................#Cut off percent of stroke\n",
    "cv=0.71#.......................#Mean specific heat for cycle in kJ/kg K\n",
    "R=0.285#.......................#Charecteristic gas constant in kJ/kh K\n",
    "cvinc=2#.......................#Percentage increase in mean specific heat of the cycle\n",
    "#Calculation\n",
    "rho=(co/100)*(r-1)+1#\n",
    "ga=1+(R/cv)#\n",
    "eta=1-(1/(ga*(r**(ga-1))))*((rho**ga)-1)/(rho-1)#.....................#Efficiency of diesel cycle \n",
    "etach=-((1-eta)/eta)*(ga-1)*(log(r)-(((rho**ga)*log(rho))/((rho**ga)-1))+(1/ga))*(cvinc/100)#...#Variation in the air standard efficiency\n",
    "print \"Percentage change = %0.2f efficiency\"%(etach*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 4.5 PAGE 189"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Max pressure in the cylinder = 65.52 bar \n",
      "Max pressure for constant specific heat = 93.52 bar \n"
     ]
    }
   ],
   "source": [
    "from math import sqrt,log\n",
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "r=7#......................#Compression ratio\n",
    "C=44000#..................#Calorific value of fuel used in kJ/kg\n",
    "afr=15#...................#Air fuel ratio\n",
    "t1=338#....................#Temperature of the charge at the end of the stroke in Kelvin\n",
    "p1=1#......................#Pressure of the charge at the end of the stroke in bar\n",
    "n=1.33#....................#Index of compression\n",
    "cv=0.71#......#Specific heat constant at constant volume in kJ/kgK\n",
    "k=20*10**(-5)#\n",
    "#Calculations\n",
    "p2=p1*(r)**n#\n",
    "t2=(t1*p2)/(p1*r)#\n",
    "ha=C/(afr+1)#......................#Heat added per kg of charge in kJ\n",
    "t3=((-2*cv)+sqrt((4*cv*cv)+(4*k*((2*cv*t2)+(k*t2*t2)+(2*ha)))))/(2*k)#\n",
    "p3=(p2*t3)/t2#.............................#Max pressure for constant volume process in bar\n",
    "P3=p2*((ha/cv)+t2)/t2#.....................#Max pressure for constant specific heat in bar\n",
    "print \"Max pressure in the cylinder = %0.2f bar \"%(p3)\n",
    "print \"Max pressure for constant specific heat = %0.2f bar \"%P3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 4.6 PAGE 190"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Max pressure in the cylinder = 102.27 bar \n",
      "Max pressure for constant specific heat = 150.64 bar \n"
     ]
    }
   ],
   "source": [
    "from math import sqrt,log\n",
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "r=10#......................#Compression ratio\n",
    "C=48000#..................#Calorific value of fuel used in kJ/kg\n",
    "afr=15#...................#Air fuel ratio\n",
    "t1=330#....................#Temperature of the charge at the end of the stroke in Kelvin\n",
    "p1=1#......................#Pressure of the charge at the end of the stroke in bar\n",
    "n=1.36#....................#Index of compression\n",
    "cv=0.7117#......#Specific heat constant at constant volume in kJ/kgK\n",
    "k=2.1*10**(-4)#\n",
    "#Calculations\n",
    "p2=p1*(r)**n#\n",
    "t2=t1*((p2/p1)**((n-1)/n))#\n",
    "ha=C/(afr+1)#......................#Heat added per kg of charge in kJ\n",
    "t3=((-2*cv)+sqrt((4*cv*cv)+(4*k*((2*cv*t2)+(k*t2*t2)+(2*ha)))))/(2*k)#\n",
    "p3=(p2*t3)/t2#.............................#Max pressure for constant volume process in bar\n",
    "P3=p2*((ha/cv)+t2)/t2#.....................#Max pressure for constant specific heat in bar\n",
    "print \"Max pressure in the cylinder = %0.2f bar \"%p3\n",
    "print \"Max pressure for constant specific heat = %0.2f bar \"%P3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 4.7 PAGE 191"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Percentage of the stroke when the combustion is completed is  9.77\n"
     ]
    }
   ],
   "source": [
    "from math import sqrt,log\n",
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "r=15#......................#Compression ratio\n",
    "C=43000#..................#Calorific value of fuel used in kJ/kg\n",
    "afr=27#...................#Air fuel ratio\n",
    "t2=870#....................#Temperature of the charge at the end of the stroke in Kelvin\n",
    "cv=0.71#......#Specific heat constant at constant volume in kJ/kgK\n",
    "R=0.287#.........................#Gas constant in kJ/kgK\n",
    "k=20*10**(-5)#\n",
    "#Calculations\n",
    "cp=cv+R#............................#Specific heat at constant pressure\n",
    "ha=C/(afr+1)#......................#Heat added per kg of charge in kJ\n",
    "t3=((-2*cp)+sqrt((4*cp*cp)+(4*k*((2*cp*t2)+(k*t2*t2)+(2*ha)))))/(2*k)#\n",
    "co=((t3/t2)-1)/(r-1)#.............#combustion occupies this amt of stroke \n",
    "print \"Percentage of the stroke when the combustion is completed is \",round(co*100,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 4.8 PAGE 192"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Max pressure in the cylinder = 73.23 bar \n",
      "Percentage of the stroke when the combustion is completed is  2.42\n"
     ]
    }
   ],
   "source": [
    "from math import sqrt,log\n",
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "r=14#......................#Compression ratio\n",
    "t1=87+273#....................#Temperature of the charge at the end of the stroke in Kelvin\n",
    "p1=1#......................#Pressure of the charge at the end of the stroke in bar\n",
    "hsupa=1700#............................#heat supplied per kg of air in kJ\n",
    "cv=0.71#......#Specific heat constant at constant volume in kJ/kgK\n",
    "k=20*10**(-5)#\n",
    "ga=1.4#.....................#Degree of freedom \n",
    "R=0.287#......................#Gas constant in kJ/kgK\n",
    "#Calculations\n",
    "p2=p1*(r)**ga#\n",
    "t2=t1*(r**(ga-1))#\n",
    "ha=hsupa/2#......................#Heat added per kg of charge in kJ\n",
    "t3=((-2*cv)+sqrt((4*cv*cv)+(4*k*((2*cv*t2)+(k*t2*t2)+(2*ha)))))/(2*k)#\n",
    "p3=(p2*t3)/t2#.............................#Max pressure for constant volume process in bar\n",
    "P3=p2*((ha/cv)+t2)/t2#.....................#Max pressure for constant specific heat in bar\n",
    "print \"Max pressure in the cylinder = %0.2f bar \"%p3\n",
    "cp=cv+R#.................................#Heat capacity at constant pressure in kJ/kgK\n",
    "t4=((-2*cp)+sqrt((4*cp*cp)+(4*k*((2*cp*t3)+(k*t3*t3)+(2*ha)))))/(2*k)#\n",
    "co=((t4/t3)-1)/(r-1)#.............#combustion occupies this amt of stroke \n",
    "print \"Percentage of the stroke when the combustion is completed is \",round(co*100,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 4.9 PAGE 193"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Maximum temperature ignoring molecular expansion = 4862.39 Kelvin \n",
      "Maximum pressure ignoring molecular expansion = 113.41 bar \n",
      "Maximum temperature considering molecular expansion = 4862.39 Kelvin \n",
      "Maximum pressure considering molecular expansion = 122.86 bar\n"
     ]
    }
   ],
   "source": [
    "from math import sqrt,log\n",
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "r=8#...........................#Compression ratio\n",
    "C=44000#............#Calorific value of fuel in kJ/kg\n",
    "afr=13.8#.....................#Air fuel ratio\n",
    "t1=343#.............................#Temperature of the mixture at the beginning of the compression in Kelvin\n",
    "p1=1#........................#Pressure of the mixture at the beginning of the compression in bar\n",
    "cv=0.716#.....................#Specific heat at constant volume in kJ/kgK\n",
    "In=1.35#.......................#Index of compression\n",
    "nc=6#..........................#No of carbon elements in the given fuel\n",
    "nh=14#.........................#No of hydrogen elements in the given fuel\n",
    "mc=12#...........................#Atomic mass of carbon in amu\n",
    "mh=2#.............................#atomic mass of hydrogen molecule in amu\n",
    "mo=32#...........................#Atomic mass of oxygen molecule in amu\n",
    "#Calculations\n",
    "#The chemical equation is C6H14 + xO2 ==> yCO2 + zH2O \n",
    "#x is the no of oxygen molecules required for complete combustion\n",
    "#y is the no of carbon dioxide molecules produced in complete combustion\n",
    "#z is the no of Water molecules produced in complete combustion\n",
    "y=nc#............................#As no of CO2 molecules is equal to no of C atoms in the fuel\n",
    "z=nh/2#..........................#No of H2O molecules is equal to half the no of H atoms in the fuel\n",
    "x=(z/2)+y#...........................#No of oxygen molecules required for combustionis half the no of water molecules plus the no of oxygen molecules \n",
    "gafr=((x*32)*(100/23))/((mc*y)+(mh*z))#.................#Gravimetric air fuel ratio\n",
    "ms=(gafr/afr)*100#......................#Actual mixture strength\n",
    "#Since the mixture strength is greater than 100 %\n",
    "#The mixture is rich in fuel. The combustion is therefore incompplete and hence CO will be formed\n",
    "d=ms/100#......................#No of fuel molecules required for combustion\n",
    "#The chemical equation is d(C6H14) + 9.5(O2) ==> a(CO2) + b(CO) + c(H2O) \n",
    "c=(d*nh)/2#...............................#No of H2O molecules is equal to half the no of H atoms in the fuel\n",
    "a=(x*2)-(d*nc)-c#........................#Equating atoms of the same element on both sides of equation\n",
    "b=(d*nc)-a#\n",
    "#By adding nitrogen on both sides, we are adding the same molecular weight on both sides.\n",
    "#Air is 79 % nitrogen and 21 % oxygen\n",
    "#Both N2 and O2 are diatomic molecules\n",
    "n=x*(79/21)#.............................#No of nitrogen molecules \n",
    "mbc=d+x+n#.............................#Moles before combustion\n",
    "mac=a+b+c+n#.............................#Moles after expansion\n",
    "me=(mac-mbc)/mbc#........................#Molecular expansion\n",
    "t2=(t1*(r**(In-1)))#\n",
    "t3=(t2+(C/((afr+1)*cv)))#..................#Maximum temperature ignoring molecular expansion in Kelvin\n",
    "p3=p1*r*(t3/t1)#...........................#Maximum pressure ignoring molecular expansion in bar\n",
    "t3me=t3#...............................#Maximum temperature considering molecular expansion in Kelvin\n",
    "p3me=p3*(mac/mbc)#....................#Maximum pressure considering molecular expansion in bar\n",
    "print \"Maximum temperature ignoring molecular expansion = %0.2f Kelvin \"%t3\n",
    "print \"Maximum pressure ignoring molecular expansion = %0.2f bar \"%p3\n",
    "print \"Maximum temperature considering molecular expansion = %0.2f Kelvin \"%t3me\n",
    "print \"Maximum pressure considering molecular expansion = %0.2f bar\"%p3me"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 4.10 PAGE 194"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The cycle work = 872.05 kJ/kg \n",
      "The cycle efficiency = 44.99 %\n"
     ]
    }
   ],
   "source": [
    "from math import sqrt,log\n",
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "r=7#....................#Compression Ratio\n",
    "t2=715#.................#Temperature at the end of isentropic compression in Kelvin\n",
    "t4=1610#................#Temperature at the end of expansion in Kelvin\n",
    "#Calculations\n",
    "vr2=65.8#..................#From steam table\n",
    "u2=524.2#..................#From steam table\n",
    "vr4=5.69#..................#From steam table\n",
    "u4=1307.63#..................#From steam table\n",
    "vr1=r*vr2#\n",
    "t1=338#..................#From steam table\n",
    "u1=241.38#..................#From steam table\n",
    "vr3=vr4/r#\n",
    "t3=2800#..................#From steam table\n",
    "u3=2462.5#..................#From steam table\n",
    "W=(u3-u2)-(u4-u1)#..................#Work done\n",
    "Qa=(u3-u2)#..........................#Heat added\n",
    "eta=W/Qa#...........................#Cycle efficiency\n",
    "print \"The cycle work = %0.2f kJ/kg \"%W\n",
    "print \"The cycle efficiency = %0.2f %%\"%(eta*100)"
   ]
  }
 ],
 "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
}
