{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CHAPTER 3 - Air Standard Cycles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.1 PAGE 87"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Engine thermal efficiency = 53.49  %\n",
      "Head added = 243.03 kJ\n",
      "Change in entropy = 0.361 kJ/K\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "t1=673#.....................#Max temp in Kelvin\n",
    "t3=313##...................#Min temp in Kelvin\n",
    "W=130#.................#Work produced in kJ\n",
    "#calculations\n",
    "etath=(t1-t3)/t1#................#Engine thermal efficiency\n",
    "print \"Engine thermal efficiency = %0.2f  %%\"%(etath*100)\n",
    "ha=W/etath#.................#Heat added in kJ\n",
    "print \"Head added = %0.2f kJ\"%ha\n",
    "dels=(ha-W)/t3#...........#Change in entropy\n",
    "print \"Change in entropy = %0.3f kJ/K\"%dels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.2 PAGE 88"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The maximum temperature = 585.37 Kelvin\n",
      "The minimum temperature = 292.68 Kelvin\n",
      "Volume at the end of isothermal expansion = 0.19 m**3\n",
      "\n",
      "\n",
      "\n",
      "Process                                     Heat transfer\n",
      "\n",
      "_______________________________________________________________\n",
      "\n",
      "Isothermal expansion                               40 kJ\n",
      "\n",
      "Adiabatic reversible expansion                     0 kJ\n",
      "\n",
      "Isothermal compression                            -40 kJ\n",
      "\n",
      "Adiabatic reversible compressions                  0 kJ\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "m=0.5#.....................#Mass of air in kg\n",
    "etath=0.5#.................#Thermal efficiency of engine\n",
    "hie=40#...................#Heat transferred during isothermal expansion in kJ\n",
    "p1=7#....................#Pressure in bar at the beginning of expansion\n",
    "v1=0.12#..................#Volume in m**3 at the beginning of expansion\n",
    "cv=0.721#...................#Specific heat at constant volume in kJ/kgK\n",
    "cp=1.008#..................#Specific heat at constant pressure in kJ/kgK\n",
    "R=287#......................#Gas constant in J/kgK\n",
    "#Calculations\n",
    "t1=(p1*10**5*v1)/(R*m)#....................#Max temp in K\n",
    "t2=t1*(1-etath)#.......................#Min temp in K\n",
    "print \"The maximum temperature = %0.2f Kelvin\"%t1\n",
    "print \"The minimum temperature = %0.2f Kelvin\"%t2\n",
    "from math import exp\n",
    "v2=(exp((hie*1000)/(m*R*t1)))*v1#..................#Volume at the end of isothermal expansion in m**3\n",
    "print \"Volume at the end of isothermal expansion = %0.2f m**3\"%v2\n",
    "print \"\\n\\n\"\n",
    "print \"Process                                     Heat transfer\\n\"\n",
    "print \"_______________________________________________________________\\n\"\n",
    "print \"Isothermal expansion                               %d kJ\\n\"%(hie)\n",
    "print \"Adiabatic reversible expansion                     %d kJ\\n\"%(0)\n",
    "print \"Isothermal compression                            %d kJ\\n\"%(-hie)\n",
    "print \"Adiabatic reversible compressions                  %d kJ\"%(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.3 PAGE 89"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p1=18 bar \n",
      "p2=12 bar \n",
      "p3=0.977 bar \n",
      "p4=1.465 bar \n",
      "t1=t2=683 Kelvin \n",
      "t3=t4=334 Kelvin \n",
      "\n",
      "Change in entropy = 0.19 kJ/K\n",
      "Mean effective pressure of the cycle = 0.47 bar\n",
      "Mean effective pressure of the cycle = 235.25 bar\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import log\n",
    "# Initialisation of Variables\n",
    "p1=18#..................#Maximum pressure in bar\n",
    "t1=410+273#.............#Maximum temperature in Kelvin\n",
    "ric=6#.....................#Ratio of isentropic compression\n",
    "rie=1.5#.................#Ratio of isothermal expansion\n",
    "v1=0.18#..................#Volume of air at the beginning of expansion\n",
    "ga=1.4#...................#Degree of freedom of gas\n",
    "R=287#.....................#Gas constant in J/kgK\n",
    "nc=210#..................#no of working cycles\n",
    "#Calculations\n",
    "\n",
    "t4=t1/(ric**(ga-1))#.............#Min temp in K\n",
    "t3=t4#\n",
    "p4=p1/(ric**ga)#..................#Min pressure in bar\n",
    "p2=p1/rie#.......................#pressure of gas before isentropic expansion in bar\n",
    "p3=p2*((1/6)**ga)#.................#Pressure of gas after isentropic expansion in bar\n",
    "print \"p1=%.f bar \\np2=%.f bar \\np3=%.3f bar \\np4=%.3f bar \\nt1=t2=%0.f Kelvin \\nt3=t4=%.f Kelvin \\n\"%(p1,p2,p3,p4,t1,t3)\n",
    "dels=(p1*10**5*v1*log(rie))/(1000*t1)#....................#Change in entropy\n",
    "print \"Change in entropy = %0.2f kJ/K\"%dels\n",
    "qs=t1*dels#.......................#Heat supplied in kJ\n",
    "Qr=t4*dels#.......................#Heat rejected in kJ\n",
    "eta=(qs-Qr)/qs#............#Efficiency of the cycle\n",
    "v3byv1=ric*rie#\n",
    "Vs=(v3byv1-1)*v1#.................#Stroke volume\n",
    "pm=((qs-Qr)*10**3)/(Vs*10**5)#........#Mean effective pressure of the cycle in bar\n",
    "print \"Mean effective pressure of the cycle = %0.2f bar\"%pm\n",
    "P=(qs-Qr)*(nc/60)#.........................#Power of engine\n",
    "print \"Mean effective pressure of the cycle = %0.2f bar\"%P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.4 PAGE 92"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temperature of the sink = 1442 Celsius\n",
      "temperature of source = 1785 Celsius:\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "eta=1/6#...................#Efficiency of the engine\n",
    "rts=70#.................#The amount of temp which is reduced in the sink in C\n",
    "#Calculation\n",
    "t1byt2=1/(1-eta)#\n",
    "t2=(rts+273)/((2*eta*t1byt2)-t1byt2+1)#............#Temperature of the sink in K\n",
    "print \"Temperature of the sink = %0.f Celsius\"%(t2-273)\n",
    "t1=t1byt2*t2#...............#Temperature of source in K\n",
    "print \"temperature of source = %0.f Celsius:\"%(t1-273)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.5 PAGE 92"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Since the efficiency of the given engine is more than efficiency of carnot engine, the claim is not true.\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "t1=1990#....................#Temperature of the heat source in K\n",
    "t2=850#..................#Temperature of the sink in K\n",
    "Q=32.5#...................#Heat supplied in kJ/min\n",
    "P=0.4#....................#Power developed by the engine in kW\n",
    "#Calculations\n",
    "eta=1-(t2/t1)#..........#Efficiency of carnot engine\n",
    "etath=P/(Q/60)#..................#Efficiency of the given engine\n",
    "if (etath>eta):\n",
    "    print \"Since the efficiency of the given engine is more than efficiency of carnot engine, the claim is not true.\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.7 PAGE 96"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The compression ratio of the engine is: 6.25\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "etaotto=0.6#............#Efficiency of otto engine\n",
    "ga=1.5#.................#Ratio of specific heats\n",
    "#Calculations\n",
    "r=(1/(1-etaotto))**(1/(ga-1))#................#Compression ratio\n",
    "print \"The compression ratio of the engine is:\",r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.8 PAGE 96"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The air standard efficiency of otto cycle = 56.47 %\n",
      "Mean effective pressure = 1.34 bar\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import pi\n",
    "# Initialisation of Variables\n",
    "D=0.25#......................#Engine bore in m\n",
    "L=0.375#...................#Engine stroke in m\n",
    "Vc=0.00263#................#Clearence volume in m**3\n",
    "p1=1#..................#Initial pressure in bar\n",
    "t1=323#...................#Initial temperature in K\n",
    "p3=25#....................#Max pressure in bar\n",
    "ga=1.4#....................#Ratio of specific heats\n",
    "#Calculations\n",
    "Vs=(pi/4)*D*D*L#................#Swept volume in m**3\n",
    "r=round((Vs+Vc)/Vc)#..........................#Compression ratio\n",
    "etaotto=1-(1/(r**(ga-1)))#..................#Air standard efficiency of otto cycle\n",
    "print \"The air standard efficiency of otto cycle = %0.2f %%\"%(etaotto*100)\n",
    "p2=p1*((r)**ga)#\n",
    "rp=p3/p2#..........................#Pressure ratio\n",
    "pm=(p1*r*((r**(ga-1))-1)*(rp-1))/((ga-1)*(r-1))#................#Mean effective pressure in bar\n",
    "print \"Mean effective pressure = %0.2f bar\"%pm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.9 PAGE 98"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temperature after adiabatic compression = 689.22 K\n",
      "Pressure after adiabatic compression = 18.38 bar\n",
      "Temperature after isochoric compression = 2772.55 K\n",
      "Pressure after isochoric compression = 73.93 bar\n",
      "Temperature after adiabatic expansion = 1206.82 K\n",
      "Pressure after adiabatic expansion = 4.02 bar\n",
      "Specific work = 847.09 kJ/kg\n",
      "Thermal efficiency = 56.47 %\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "p1=1#.....................#Pressure in bar\n",
    "t1=300#......................#Temperature in K\n",
    "Q=1500#.....................#Heat added in kJ/kg\n",
    "r=8#.......................#Compression ratio\n",
    "Cv=0.72#....................#Specific heat at constant volume\n",
    "ga=1.4#......................#Ratio of specific heats\n",
    "#Calculations\n",
    "t2=t1*(r)**(ga-1)#..........#Temperature after adiabatic compression in K\n",
    "p2=p1*(r**ga)#..............#Pressure after adiabatic compression in bar\n",
    "t3=(Q/Cv)+t2#.................#Temperature after isochoric compression in K\n",
    "p3=(p2*t3)/t2#................#Pressure after isochoric compression in bar\n",
    "t4=t3/(r**(ga-1))#.......................#Temperature after adiabatic expansion in K\n",
    "p4=p3*(1/(r**(ga)))#................#Pressure after adiabatic expansion in bar\n",
    "Ws=Cv*(t3-t2-t4+t1)#.........#Specific work in kJ/kg\n",
    "etath=1-(1/(r**(ga-1)))#............#Thermal efficiency\n",
    "print \"Temperature after adiabatic compression = %0.2f K\"%t2\n",
    "print \"Pressure after adiabatic compression = %0.2f bar\"%p2\n",
    "print \"Temperature after isochoric compression = %0.2f K\"%t3\n",
    "print \"Pressure after isochoric compression = %0.2f bar\"%p3\n",
    "print \"Temperature after adiabatic expansion = %0.2f K\"%t4\n",
    "print \"Pressure after adiabatic expansion = %0.2f bar\"%p4\n",
    "print \"Specific work = %0.2f kJ/kg\"%Ws\n",
    "print \"Thermal efficiency = %0.2f %%\"%(etath*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.10 PAGE 99"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temperature after adiabatic compression = 614.30 K\n",
      "Pressure after adiabatic compression = 12.29 bar\n",
      "Temperature after isochoric compression = 1842.00 K\n",
      "Pressure after isochoric compression = 36.84 bar\n",
      "Temperature after adiabatic expansion = 899.56 K\n",
      "Pressure after adiabatic expansion = 3.00 bar\n",
      "Thermal efficiency = 56.47 %\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "r=6#..............#Compression ratio\n",
    "p1=1#................#Pressure after isochoric expansion in bar\n",
    "t1=300#................#Temperature after isochoric expansion in K\n",
    "t3=1842#...............#Temperature after isochoric compression in K\n",
    "ga=1.4#...............#Ratio of specific heats\n",
    "#Calculations\n",
    "p2=p1*(r**ga)#...............#Pressure after adiabatic compression in bar\n",
    "t2=t1*(r**(ga-1))#.............#Temperature after adiabatic compression in K\n",
    "p3=p2*(t3/t2)#..................#pressure after isochoric compression in bar\n",
    "t4=t3/(r**(ga-1))#..............#Temperature after adiabatic expansion in K\n",
    "p4=p3*(1/(r**(ga)))#...........#Pressure after adiabatic expansion in bar\n",
    "etaotto=1-(1/(r**(ga-1)))#............#Efficiency of otto cycle\n",
    "p5=p1#\n",
    "t5=((p5/p3)**((ga-1)/ga))*t3#................#Atkinson cycle temp after further adiabatic expansion in K\n",
    "etatk=1-((ga*(t5-t1))/(t3-t2))#...........#Efficiency of atkinson cycle\n",
    "print \"Temperature after adiabatic compression = %0.2f K\"%t2\n",
    "print \"Pressure after adiabatic compression = %0.2f bar\"%p2\n",
    "print \"Temperature after isochoric compression = %0.2f K\"%t3\n",
    "print \"Pressure after isochoric compression = %0.2f bar\"%p3\n",
    "print \"Temperature after adiabatic expansion = %0.2f K\"%t4\n",
    "print \"Pressure after adiabatic expansion = %0.2f bar\"%p4\n",
    "print \"Thermal efficiency = %0.2f %%\"%(etath*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.11 PAGE 101"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compression ratio: 3.95\n",
      "Temperature at the end of compression = 607.79 K\n",
      "Temperature at the end of heat addition = 1266.43 K\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "p1=1#...................#Initial pressure in bar\n",
    "t1=343#..................#Initial temperature in K\n",
    "p2=7#....................#Pressure after adiabatic compression\n",
    "Qs=465#...............#Heat addition at constant volume in kJ/kg\n",
    "cp=1#.....................#Specific heat at constant pressure in kJ/kg\n",
    "cv=0.706#..................#Specific heat at constant volume in kJ/kg\n",
    "ga=cp/cv#.................#Ratio of specific heats\n",
    "#Calculations\n",
    "r=(p2/p1)**(1/ga)#...............#Compression ratio\n",
    "t2=t1*(r**(ga-1))#.....................#Temperature at the end of compression in K\n",
    "t3=t2+(Qs/cv)#.............#Temperature at the end of heat addition in K\n",
    "print \"Compression ratio:\",round(r,2)\n",
    "print \"Temperature at the end of compression = %0.2f K\"%t2\n",
    "print \"Temperature at the end of heat addition = %0.2f K\"%t3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.12 PAGE 102"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compression ratio: 6.92\n",
      "Thermal efficiency = 53.87 %\n",
      "Work done = 598.65 kJ\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "ga=1.4#..............#Ratio of specific heats\n",
    "p2byp1=15#...............#Ratio pressure at the end of compression to that of pressure at the start\n",
    "t1=311#................#Initial temperature in K\n",
    "t3=2223#...............#Maximum temperature in K\n",
    "R=0.287#...............#Gas constant in kJ/kg K\n",
    "#Calculations\n",
    "r=p2byp1**(1/ga)#...............#Compression ratio\n",
    "etath=1-(1/(r**(ga-1)))#.............#Thermal efficiency\n",
    "t2=t1*(r**(ga-1))#............#Temperature at the end of compression in K\n",
    "t4=t3/(r**(ga-1))#...........#Temperature at the end of isothermal expansion in K\n",
    "cv=R/(ga-1)#................#Specific heat at constant volume in kJ/kg\n",
    "Q=cv*(t3-t2)#..............#Heat supplied in kJ/kg of air\n",
    "Qr=cv*(t4-t1)#.................#Heat rejected in kJ/kg of air\n",
    "W=Q-Qr#.................#Work done\n",
    "print \"Compression ratio:\",round(r,2)\n",
    "print \"Thermal efficiency = %0.2f %%\"%(etath*100)\n",
    "print \"Work done = %0.2f kJ\"%W"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.13 PAGE 104"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temperature after adiabatic compression: 601.15 K\n",
      "\n",
      "Pressure after adiabatic compression: 11.00 bar\n",
      "\n",
      "Volume after adiabatic compression: 0.08 m**3\n",
      "\n",
      "Temperature after isochoric compression: 1172.73 K\n",
      "\n",
      "Pressure after isochoric compression: 21.46 bar\n",
      "\n",
      "Volume after isochoric compression: 0.08 m**3\n",
      "\n",
      "Temperature after adiabatic expansion: 591.09 K\n",
      "\n",
      "Pressure after adiabatic expansion: 1.95 bar\n",
      "\n",
      "Volume after adiabatic expansion: 0.45 m**3\n",
      "\n",
      "Percentage clearance: 22.01\n",
      "\n",
      "Efficiency of otto cycle: 49.60\n",
      "\n",
      "Mean effective pressure: 2.82 bar:\n",
      "\n",
      "Power developed: 364.54 kW\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "v1=0.45#.............#Initial volume in m**3 \n",
    "p1=1#...............#Initial pressure in bar\n",
    "t1=303#.............#Initial temperature in K\n",
    "p2=11#...................#Pressure at the end of compression stroke in bar\n",
    "Q=210#...................#heat added at constant volume in kJ\n",
    "N=210#.................#No of working cycles per min\n",
    "ga=1.4#.............#Ratio of specific heats\n",
    "R=287#.................#Gas constant in kJ/kgK\n",
    "cv=0.71#.................#Specific heat at constant volume in kJ/kg\n",
    "#Calculations\n",
    "r=(p2/p1)**(1/ga)#...................#Compression ratio\n",
    "t2=t1*(r**(ga-1))#...................#Temperature at the end of adiabatic compression\n",
    "v2=(t2*p1*v1)/(t1*p2)#.................#Volume at the end of adiabatic compression in m**3\n",
    "m=(p1*v1*10**5)/(R*t1)#................#Mass of engine fluid in kg\n",
    "t3=(Q/(m*cv))+t2#...................#Temperature at the end of isochoric compression in K\n",
    "p3=(t3/t2)*p2#................#Pressure at the end of isochoric compression in bar\n",
    "v3=v2#\n",
    "t4=t3*(1/r)**(ga-1)#...................#Temperature at the end of adiabatic expansion in K\n",
    "p4=p3*(1/r)**ga#......................#Pressure at the end of adiabatic expansion in bar\n",
    "v4=v1#\n",
    "pc=(v2*100)/(v1-v2)#..................#Percentage clearence\n",
    "etaotto=1-(1/(r**(ga-1)))#........................#Efficiency of otto cycle\n",
    "Qr=m*cv*(t4-t1)#...............................#Heat rejected in kJ/kg\n",
    "pm=((Q-Qr)*1000)/((v1-v2)*100000)#......#Mean effective pressure in bar\n",
    "P=(Q-Qr)*(N/60)#........................#Power developed in kW\n",
    "print \"Temperature after adiabatic compression: %0.2f K\\n\"%(t2)\n",
    "print \"Pressure after adiabatic compression: %0.2f bar\\n\"%(p2)\n",
    "print \"Volume after adiabatic compression: %0.2f m**3\\n\"%(v2)\n",
    "print \"Temperature after isochoric compression: %0.2f K\\n\"%(t3)\n",
    "print \"Pressure after isochoric compression: %0.2f bar\\n\"%(p3)\n",
    "print \"Volume after isochoric compression: %0.2f m**3\\n\"%(v3)\n",
    "print \"Temperature after adiabatic expansion: %0.2f K\\n\"%(t4)\n",
    "print \"Pressure after adiabatic expansion: %0.2f bar\\n\"%(p4)\n",
    "print \"Volume after adiabatic expansion: %0.2f m**3\\n\"%(v4)\n",
    "print \"Percentage clearance: %0.2f\\n\"%(pc)\n",
    "print \"Efficiency of otto cycle: %0.2f\\n\"%(etaotto*100)\n",
    "print \"Mean effective pressure: %0.2f bar:\\n\"%(pm)\n",
    "print \"Power developed: %0.2f kW\\n\"%(P)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.14 PAGE 106"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Air standard efficiency of the engine = 49.59 %\n",
      "There is no change in efficiency when Helium is used as working fluid instead of air\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "t1=310#................#Min temperature in K\n",
    "t3=1220#................#Max temperature in K\n",
    "ga=1.4#................#Ratio of specific heats for air\n",
    "cph=5.22#............#Specific heat at constant volume for helium in kJ/kg\n",
    "cvh=3.13#...............#Specific heat at constant pressure for helium in kJ/kg\n",
    "#Calculations \n",
    "r=(t3/t1)**(1/((ga-1)*2))#..............#Compression ratio\n",
    "etaotto=1-(1/(r**(ga-1)))#................#Air standard efficiency\n",
    "gah=cph/cvh#................#Ratio of specific heats for Helium\n",
    "rh=(t3/t1)**(1/((gah-1)*2))#..............#Compression ratio when Helium is used\n",
    "etaottoh=1-(1/(rh**(gah-1)))#................#Air standard efficiency when Helium is used\n",
    "print \"Air standard efficiency of the engine = %0.2f %%\"%(etaotto*100)\n",
    "if ((round (etaotto)- round (etaottoh)) == 0):\n",
    "    print \"There is no change in efficiency when Helium is used as working fluid instead of air\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.15 PAGE 108"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Power developed = 1.88 kW\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt\n",
    "# Initialisation of Variables\n",
    "t1=310#.........#Minimum temperature in K\n",
    "t3=1450#............#maximum temperature in K\n",
    "m=0.38#...........#Mass of working fluid in kg\n",
    "cv=0.71#...........#Specific heat at constant volume in kJ/kg\n",
    "#Calculations\n",
    "t4=sqrt(t1*t3)#............#Temperature at the end of adiabatic expansion in K\n",
    "t2=t4#\n",
    "W=cv*(t3-t2-t4+t1)#..................#Work done in kJ/kg\n",
    "P=W*(m/60)#.................#Power developed in kW\n",
    "print \"Power developed = %0.2f kW\"%P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.17 PAGE 113"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Efficiency of diesel engine = 61.19 %\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "r=15#...................#Compression ratio\n",
    "ga=1.4#..............#Ratio os fpecific heats for air\n",
    "perQ=6#................#Heat addition at constant pressure takes place at 6% of stroke\n",
    "#Calculations\n",
    "rho=1+((perQ/100)*(r-1))#.............#Cut off ratio\n",
    "etad=1-((((rho**ga)-1)/(rho-1))*(1/(ga*(r**(ga-1)))))#..................#Efficiency of diesel engine\n",
    "print \"Efficiency of diesel engine = %0.2f %%\"%(etad*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.18 PAGE 114"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Efficiency of diesel engine = 59.34 %\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt,pi\n",
    "# Initialisation of Variables\n",
    "L=0.25#...............#Engine stroke in m\n",
    "D=0.15#..................#Engine bore in m\n",
    "v2=0.0004#...............#Clearance volume in m**3\n",
    "pers=5#...............#Percentage of stroke when fuel injection occurs\n",
    "ga=1.4#..............#Ratio of specific heats\n",
    "#Calculations\n",
    "Vs=(pi/4)*D*D*L#..............#Swept volume in m**3\n",
    "Vt=Vs+v2#....................#Total cylinder volume in m**3\n",
    "v3=v2+((pers/100)*Vs)#..............#Volume at point of cut off\n",
    "rho=v3/v2#............#Cut off ratio\n",
    "r=1+(Vs/v2)#.............#Compression ratio\n",
    "etad=1-((((rho**ga)-1)/(rho-1))*(1/(ga*(r**(ga-1)))))#..................#Efficiency of diesel engine\n",
    "print \"Efficiency of diesel engine = %0.2f %%\"%(etad*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.19 PAGE 114"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Percentage loss in efficiency due to delay in cut off : 2.10\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "r=14#....................#Compression ratio\n",
    "pers1=5#...............#Percentage of stroke when fuel cut off occurs\n",
    "pers2=8#...............#Percentage of stroke when delayed fuel cut off occurs\n",
    "v2=1#.....................#Clearance volume in m**3\n",
    "ga=1.4#..................#Ratio of specific heats\n",
    "#Calculations\n",
    "#When the fuel is cut off at 5 %\n",
    "rho1=((pers1/100)*(r-1))+1#.............#Cut off ratio\n",
    "etad1=1-((((rho1**ga)-1)/(rho1-1))*(1/(ga*(r**(ga-1)))))#..................#Efficiency of diesel engine\n",
    "#When the fuel is cut off at 8 %\n",
    "rho2=((pers2/100)*(r-1))+1#.............# Delayed Cut off ratio\n",
    "etad2=1-((((rho2**ga)-1)/(rho2-1))*(1/(ga*(r**(ga-1)))))#..................#Efficiency of diesel engine when cut off ratio is deyaled\n",
    "print \"Percentage loss in efficiency due to delay in cut off : %0.2f\"%((etad1-etad2)*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.20 PAGE 115"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cut off Percentage : 11.17\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from scipy.optimize import fsolve\n",
    "# Initialisation of Variables\n",
    "pm=7.5#.................#Mean effective pressure in bar\n",
    "r=12.5#..................#Compression ratio\n",
    "p1=1#....................#Initial pressure in bar\n",
    "ga=1.4#.................#Ratio of specific heats\n",
    "#Calculations\n",
    "k=(pm*(ga-1)*(r-1))/(p1*(r**ga))#\n",
    "c1=(r**(1-ga))/k#\n",
    "c2=(-ga)/k#\n",
    "c=1+(ga/k)-((r**(1-ga))/k)#\n",
    "def F(rho):\n",
    "    f=c1*(rho**ga)+c2*rho+c#\n",
    "    return f\n",
    "#Initial guess\n",
    "rho=2#\n",
    "#Derivative\n",
    "def D(rho):\n",
    "    z=c1*ga*(rho**(ga-1))+c2#\n",
    "    return z\n",
    "y=fsolve(F,rho)\n",
    "perc=((y-1)/(r-1))*100#..................#Percentage of cutoff\n",
    "print \"Cut off Percentage : %0.2f\"%perc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.21 PAGE 115"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temperature after adiabatic compression: 886.253082 K\n",
      "\n",
      "Pressure after adiabatic compression: 44.312654 bar\n",
      "\n",
      "Volume after adiabatic compression: 0.000673 m**3\n",
      "\n",
      "Temperature after isobaric compression: 1878.856533 K\n",
      "\n",
      "Pressure after isobaric compression: 44.312654 bar\n",
      "\n",
      "Volume after isobaric compression: 0.001427 m**3\n",
      "\n",
      "Temperature after adiabatic expansion: 858.996630 K\n",
      "\n",
      "Pressure after adiabatic expansion: 2.863322 bar\n",
      "\n",
      "Volume after adiabatic expansion: 0.010098 m**3\n",
      "\n",
      "Efficiency of diesel engine = 59.77 %\n",
      "Mean effective pressure : 7.42\n",
      "Power developed = 44.27 kW \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import pi\n",
    "# Initialisation of Variables\n",
    "D=0.2#.................#Engine bore in m\n",
    "L=0.3#.............#Engine stroke in m\n",
    "p1=1#................#Initial pressure in bar\n",
    "N=380#.................#No of working cycles per min\n",
    "t1=300#..............#Initial temperature in K\n",
    "co=8#................#Cut off percentage\n",
    "r=15#..................#Compression ratio\n",
    "R=287#.................#gas constant in J/kg\n",
    "ga=1.4#................#Ratio of specific heats\n",
    "#Calculations\n",
    "Vs=(pi/4)*D*D*L#.............#Stroke volume in m\n",
    "v1=(r/(r-1))*Vs#................#Volume at the end of isochoric compression in m**3\n",
    "m=(p1*v1*10**5)/(R*t1)#................#Mass of air in cylinder in kg/cycle\n",
    "p2=p1*(r**ga)#.......................#Pressure at the end of isentropic compression in bar\n",
    "t2=t1*(r**(ga-1))#....................#Temperature at the end of isentropic compression in K\n",
    "v2=Vs/(r-1)#..................#Volume at the end of isentropic compressionin m**3\n",
    "p3=p2#\n",
    "rho=((r-1)*(co/100))+1#................#Cut off ratio\n",
    "v3=rho*v2#.......................#Volume at the end of isobaric expansion in m**3\n",
    "t3=t2*(v3/v2)#..................#Temperature at the end of isobaric expansion in K\n",
    "p4=((rho/r)**ga)*p3#..............#Pressure at the end of adiabatic expansion in bar\n",
    "t4=((rho/r)**(ga-1))*t3#..............#Temperature at the end of adiabatic expansion in K\n",
    "v4=v1#\n",
    "print \"Temperature after adiabatic compression: %f K\\n\"%(t2)\n",
    "print \"Pressure after adiabatic compression: %f bar\\n\"%(p2)\n",
    "print \"Volume after adiabatic compression: %f m**3\\n\"%(v2)\n",
    "print \"Temperature after isobaric compression: %f K\\n\"%(t3)\n",
    "print \"Pressure after isobaric compression: %f bar\\n\"%(p3)\n",
    "print \"Volume after isobaric compression: %f m**3\\n\"%(v3)\n",
    "print \"Temperature after adiabatic expansion: %f K\\n\"%(t4)\n",
    "print \"Pressure after adiabatic expansion: %f bar\\n\"%(p4)\n",
    "print \"Volume after adiabatic expansion: %f m**3\\n\"%(v4)\n",
    "etad=1-((((rho**ga)-1)/(rho-1))*(1/(ga*(r**(ga-1)))))#..................#Efficiency of diesel engine\n",
    "print \"Efficiency of diesel engine = %0.2f %%\"%(etad*100)\n",
    "pm=p1*(r**ga)*(ga*(rho-1)-((r**(1-ga))*((rho**ga)-1)))*(1/(ga-1))*1/(r-1)#.......#Mean effective pressure \n",
    "print \"Mean effective pressure : %0.2f\"%pm\n",
    "Wdc=(pm*Vs*10**5)/1000#..................#Work done per cycle in kJ/cycle\n",
    "P=(Wdc*N)/60#...........................#Power developed in kW\n",
    "print \"Power developed = %0.2f kW \"%P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.22 PAGE 118"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean effective pressure = 7.02 bar \n",
      "Ratio of maximum pressure to mean effective pressure 6.49\n",
      "Cycle efficiency = 60.48 %\n",
      "Fuel consumption = 0.35 kg/kWh\n"
     ]
    }
   ],
   "source": [
    "# Initialisation of Variables\n",
    "rc=15.3#....................#Compression ratio\n",
    "re=7.5#...................#Expansion ratio\n",
    "cp=1.005#.................#Specific heat at constant pressure in kJ/kg K\n",
    "cv=0.718#..................#Specific heat at constant volume in kJ/kgK\n",
    "ga=1.4#....................#Ratio of specific heats\n",
    "p1=1#....................#Initial pressure in bar\n",
    "t1=300#..................#Initial temperature in K\n",
    "etamech=0.8#..................#Mechanical efficiency\n",
    "C=42000#...........................#Calorific value of fuel in kJ/kg\n",
    "rita=0.5#.........................#Ratio of indicated thermal efficiency to air standard efficiency\n",
    "R=287#..........................#Gas constant in kJ/kgK\n",
    "#Calculations\n",
    "t2=t1*(rc**(ga-1))#.................#Temperature at the end of adiabatic compression in K\n",
    "p2=p1*(rc**ga)#...................#Pressure at the end of adiabatic compression in bar\n",
    "t3=(rc*t2)/re#....................#Temperature at the end of constant pressure process in K\n",
    "v2=1#..................#Volume at the end of adiabatic process in m**3\n",
    "m=(p2*v2*10**5)/(R*t2)#..................#Mass of working fluid in kg\n",
    "t4=t3*((1/re)**(ga-1))#...................#Temperature at the end of adiabatic expansion in K\n",
    "W=(m*(cp*(t3-t2)))-(m*(cv*(t4-t1)))#........#Work done in kJ\n",
    "pm=W/(rc-1)#..............................#Mean effective pressure in kN/m**2\n",
    "print \"Mean effective pressure = %0.2f bar \"%(pm/100)\n",
    "print \"Ratio of maximum pressure to mean effective pressure %0.2f\"%((p2*100)/(pm))\n",
    "etacy=W/(m*cp*(t3-t2))#...............#Cycle efficiency\n",
    "print \"Cycle efficiency = %0.2f %%\"%(etacy*100)\n",
    "etaith=rita*etacy#..................#Indicated thermal efficiency\n",
    "etabth=etaith*etamech#...............#Brake thermal efficiency\n",
    "mf=3600/(etabth*C)#................#Fuel consumption per kWh\n",
    "print \"Fuel consumption = %0.2f kg/kWh\"%mf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.23 PAGE 123"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Efficiency of dual cycle : 63.76\n"
     ]
    }
   ],
   "source": [
    "# Initialisation of Variables\n",
    "Vs=0.0053#................#Swept volume in m**3\n",
    "Vc=0.00035#...............#Clearance volume in m**3\n",
    "v3=Vc#\n",
    "v2=Vc#\n",
    "p3=65#..................#Max pressure in bar\n",
    "co=5#...................#Cut off percentage\n",
    "p4=p3#ga=1.4#...............#Ratio of specific heats\n",
    "t1=353#....................#Temperature at the start of compression in K\n",
    "p1=0.9#...................#Pressure at the start of compression in bar\n",
    "#Calculations\n",
    "r=1+(Vs/Vc)#...................#Compression ratio\n",
    "rho=(((co/100)*Vs)/Vc)+1#...................#Cut off ratio\n",
    "p2=p1*(r**ga)#\n",
    "Beta=p3/p2#.............................#Explosion ratio\n",
    "etadual=1-((1/(r**(ga-1)))*((Beta*(rho**ga))-1)*(1/((Beta-1)+(Beta*ga*(rho-1)))))#............#Efficiency of dual cycle\n",
    "print \"Efficiency of dual cycle : %0.2f\"%(etadual*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.24 PAGE 124"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Efficiency of dual cycle : 61.42\n"
     ]
    }
   ],
   "source": [
    "# Initialisation of Variables\n",
    "r=14#......................#Compression ratio\n",
    "Beta=1.4#................#Explosion ratio\n",
    "co=6#..................#Cut off percentage\n",
    "ga=1.4#.................#Ratio of specific heats\n",
    "#Calculation\n",
    "rho=((co/100)*(r-1))+1#...............#Cut off ratio\n",
    "etadual=1-((1/(r**(ga-1)))*((Beta*(rho**ga))-1)*(1/((Beta-1)+(Beta*ga*(rho-1)))))#............#Efficiency of dual cycle\n",
    "print \"Efficiency of dual cycle : %0.2f\"%(etadual*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.25 PAGE 124"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Air standard efficiency = 57.55 %\n",
      "Power developed = 51.39 kW\n"
     ]
    }
   ],
   "source": [
    "from math import pi\n",
    "# Initialisation of Variables\n",
    "D=0.25#.................#Engine bore in m\n",
    "L=0.3#.............#Engine stroke in m\n",
    "p1=1#................#Initial pressure in bar\n",
    "N=3#...............#No of cycles per second\n",
    "p3=60#................#Maximum pressure in bar\n",
    "t1=303#..............#Initial temperature in K\n",
    "co=4#................#Cut off percentage\n",
    "r=9#..................#Compression ratio\n",
    "R=287#.................#gas constant in J/kg\n",
    "cv=0.71#...............#Specific heat at constant volume in kJ/kgK\n",
    "cp=1.0#.................#Specific heat at constant pressure in kJ/kgK\n",
    "ga=1.4#...............#Ratio of specific heats\n",
    "#Calculations\n",
    "p4=p3#\n",
    "Vs=(pi/4)*D*D*L#.............#Stroke volume in m**3\n",
    "Vc=Vs/(r-1)#..................#Clearance volume in m**3\n",
    "rho=((r-1)*(co/100))+1#................#Cut off ratio\n",
    "v1=Vc+Vs#.................#Volume after isochoric compression in m**3\n",
    "p2=p1*(r**ga)#................#Pressure after adiabatic compression in bar\n",
    "t2=t1*(r**(ga-1))#..............#Temperature after adiabatic expansion in K\n",
    "t3=(p3*t2)/p2#..............#Temperature after isochoric compression in K\n",
    "t4=t3*rho#.....................#Temperature after isobaric expansion in K\n",
    "t5=t4*((rho/r)**(ga-1))#.........#Temperature after adiabatic expansion in K\n",
    "p5=p4*(rho/r)**ga#...............#Pressure after adiabatic expansion in bar\n",
    "Qs=(cv*(t3-t2)+cp*(t4-t3))#.....#Heat supplied in kJ/kg\n",
    "Qr=cv*(t5-t1)#...................#Heat rejected in kJ/kg\n",
    "etast=1-(Qr/Qs)#.................#Air standard efficiency\n",
    "print \"Air standard efficiency = %0.2f %%\"%(etast*100)\n",
    "m=(p1*v1*10**5)/(R*t1)#...............#Mass of air in cycle\n",
    "W=m*(Qs-Qr)#....................#Work done per cycle in kJ\n",
    "P=W*N#............................#Power developed in kW\n",
    "print \"Power developed = %0.2f kW\"%P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.26 PAGE 127"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temperature after adiabatic compression: 874.19 K\n",
      "\n",
      "Pressure after adiabatic compression: 21.67 bar\n",
      "\n",
      "Temperature after isochoric compression: 2742.67 K\n",
      "\n",
      "Pressure after isochoric compression: 68.00 bar\n",
      "\n",
      "Temperature after isobaric expansion: 3166.05 K\n",
      "\n",
      "Pressure after isobaric expansion: 68.00 bar\n",
      "\n",
      "Temperature after adiabatic expansion: 1392.38 K\n",
      "\n",
      "Pressure after adiabatic expansion: 3.84 bar\n",
      "\n",
      "Air standard efficiency = 58.24 %\n",
      "Mean effective pressure = 11.09 bar\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "p1=1#................#Initial pressure in bar\n",
    "t1=363#.............#Initial temperature in K\n",
    "r=9#.................#Compression ratio\n",
    "p3=68#...............#Max pressure\n",
    "p4=p3#\n",
    "Qs=1750#..............#Total heat supplied\n",
    "ga=1.4#...............#Ratio of specific heats\n",
    "R=287#................#Gas constant in kJ/kgK\n",
    "cv=0.71#..............#Specific heat at constant volume in kJ/kgK\n",
    "cp=1#................#Specific heat at constant pressure in kJ/kgK\n",
    "#Calculations\n",
    "p2=p1*((r)**ga)#............#Pressure at the end of adiabatic compression in bar\n",
    "t2=t1*((r)**(ga-1))#..........#Temperature at the end of adiabatic compression in K\n",
    "t3=t2*(p3/p2)#............#Temperature at the end of isochoric compression in K\n",
    "Qv=cv*(t3-t2)#.............#Heat added at constant volume in kJ/kg\n",
    "Qp=Qs-Qv#.....................#Heat added at constant pressure in kJ/kg\n",
    "t4=(Qp/cp)+t3#................#Temperature at the end of isobaric expansion in kJ/kg\n",
    "rho=t4/t3#.....................#Cut off ratio\n",
    "p5=p4*((rho/r)**ga)#................#Pressure at the end of adiabatic expansion in kJ/kg\n",
    "t5=t4*((rho/r)**(ga-1))#...........#Temperature at the end of adiabatic expansion in kJ/kg\n",
    "print \"Temperature after adiabatic compression: %0.2f K\\n\"%(t2)\n",
    "print \"Pressure after adiabatic compression: %0.2f bar\\n\"%(p2)\n",
    "print \"Temperature after isochoric compression: %0.2f K\\n\"%(t3)\n",
    "print \"Pressure after isochoric compression: %0.2f bar\\n\"%(p3)\n",
    "print \"Temperature after isobaric expansion: %0.2f K\\n\"%(t4)\n",
    "print \"Pressure after isobaric expansion: %0.2f bar\\n\"%(p4)\n",
    "print \"Temperature after adiabatic expansion: %0.2f K\\n\"%(t5)\n",
    "print \"Pressure after adiabatic expansion: %0.2f bar\\n\"%(p5)\n",
    "Qr=cv*(t5-t1)#....................#Heat rejected in kJ\n",
    "etast=1-(Qr/Qs)#.................#Air standard efficiency\n",
    "print \"Air standard efficiency = %0.2f %%\"%(etast*100)\n",
    "pm=(1/(r-1))*((68*(rho-1))+(((p4*rho)-(p5*r))/(ga-1))-((p2-r)/(ga-1)))#................#Mean effective pressure in bar\n",
    "print \"Mean effective pressure = %0.2f bar\"%pm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.27 PAGE 129"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Air standard efficiency = 65.30 %\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "t1=300#...............#Initial temperature\n",
    "rmami=70#....................#Ratio of max pressure and min pressure\n",
    "r=15#....................#Compression ratio\n",
    "ga=1.4#.................#Ratio of specific heats\n",
    "R=287#....................#Gas constant in kJ/kgK\n",
    "t2=t1*(r**(ga-1))#.................#Temperature at the end of adiabatic compression in K\n",
    "t3=t2*(rmami/(r**ga))#............#Temperature at the end of isochoric compression in K\n",
    "t4=t3+((t3-t2)/ga)#..............#Temperature at the end of isobaric process in K\n",
    "t5=t4/((1/(t4/(t3*r)))**(ga-1))#..........#Temperature at the end of adiabatic expansion in K\n",
    "etast=1-((t5-t1)/((t3-t2)+ga*(t4-t3)))#..............#Air standard efficiency\n",
    "print \"Air standard efficiency = %0.2f %%\"%(etast*100,)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.28 PAGE 131"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thermal efficiency = 61.80 %\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "t1=373#.............#Initial temperature in K\n",
    "p1=1#...............#Initial pressure in bar\n",
    "p3=65#..............#Maximum pressure in bar\n",
    "R=287#.................#Gas constant in kJ/kg\n",
    "p4=p3#\n",
    "ga=1.41#.................#Ratio of specific heats\n",
    "Vs=0.0085#............#Swept volume in m**3\n",
    "afr=21#...............#Air fuel ratio\n",
    "r=15#.................#Compression ratio\n",
    "C=43890#..............#Calorific value of fuel in kJ/kg\n",
    "cp=1#................#Specific heat at constant pressure in kJ/kgK\n",
    "cv=0.71#..............#Specific heat at constant volume in kJ/kgK\n",
    "#Calculations\n",
    "Vc=Vs/(r-1)#...............#Clearance volume in m**3\n",
    "v2=Vc\n",
    "v1=Vs+v2#\n",
    "v3=Vc\n",
    "v5=v1#\n",
    "p2=p1*(r**ga)#.....................#Pressure at the end of adiabatic compression in bar\n",
    "t2=t1*(r**(ga-1))#................#Temperature at the end of adiabatic compression in K\n",
    "t3=(t2*p3)/p2#...................#Temperature at the end of isochoric compression in K\n",
    "m=(p1*v1*10**5)/(R*t1)#............#Mass of air in the cycle in kg\n",
    "Qv=m*cv*(t3-t2)#.....................#Heat added during constant volume process in kJ\n",
    "fv=Qv/C#.............................#Fuel added during constant volume process in kg\n",
    "mf=m/afr#..................#Total amount of fuel added in kg\n",
    "mfib=mf-fv#....................#Total amount of fuel added in isobaric process in kg\n",
    "Qib=mfib*C#....................#Total amount of heat added in isobaric process in kJ\n",
    "t4=(Qib/((m+mf)*cp))+t3#........#Temperature at the end of isobaric process in K\n",
    "v4=(v3*t4)/t3#..................#Volume at the end of isobaric process in m**3\n",
    "t5=t4/((v5/v4)**(ga-1))#.........#Temperature at the end of isochoric expansion in K\n",
    "Qrv=(m+mf)*cv*(t5-t1)#...............#Heat rejected during constant volume process in kJ\n",
    "W=(Qib+Qv)-Qrv#................#Work done in kJ\n",
    "etath=W/(Qib+Qv)#..................#Thermal efficiency\n",
    "print \"Thermal efficiency = %0.2f %%\"%(etath*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.29 PAGE 133"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temperature after adiabatic compression: 524.81 K\n",
      "\n",
      "Pressure after adiabatic compression: 15.59 bar\n",
      "\n",
      "Temperature after isochoric compression: 1201.99 K\n",
      "\n",
      "Pressure after isochoric compression: 35.70 bar\n",
      "\n",
      "Temperature after isobaric expansion: 3283.19 K\n",
      "\n",
      "Pressure after isobaric expansion: 35.70 bar\n",
      "\n",
      "Temperature after adiabatic expansion: 2195.60 K\n",
      "\n",
      "Pressure after adiabatic expansion: 4.78 bar\n",
      "\n",
      "Mean effective pressure = 10.92 bar\n",
      "Engine efficiency = 32.95 %\n",
      "Power of the engine = 171.53 kW\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import pi\n",
    "# Initialisation of Variables\n",
    "D=0.25#.............#Engine bore in m\n",
    "L=0.4#..............#Engine stroke in m\n",
    "t1=303#.............#Initial temperature in K\n",
    "R=287#...............#Gas constant in kJ/kgK\n",
    "p1=1#...............#Initial pressure in bar\n",
    "N=8#................#No of working cycles per sec\n",
    "cv=0.71#.............#Specific heat at constant volume in kJ/kgK\n",
    "cp=1#.................#Specific heat at constant pressure in kJ/kgK\n",
    "n=1.25#.............#Adiabatic index\n",
    "rc=9#...............#Compression ratio\n",
    "re=5#...............#Expansion ratio\n",
    "rqptqe=2#...........#Ratio of heat liberated at constant pressure to heat liberated at constant volume\n",
    "#Calculations\n",
    "p2=p1*(rc**n)#.......................#Pressure at the end of adiabatic compression in bar\n",
    "t2=t1*(rc**(n-1))#...................#Temperature at the end of adiabatic compression in K\n",
    "rho=rc/re#..........................#Cut off ratio\n",
    "t3=(2*cv*t2)/((2*cv)-(cp*(rho-1)))#...............#Temperature at the end of isochoric compression in K\n",
    "p3=p2*(t3/t2)#....................................#Pressure at the end of isochoric compression in bar\n",
    "p4=p3#t4=rho*t3#.................................#Temperature and pressure at the end of isobaric process\n",
    "p5=p4*(1/(re**n))#.................................#Pressure at the end of adiabatic expansion in bar\n",
    "t5=t4*(1/(re**(n-1)))#.............................#Temperature at the end of adiabatic expansion in K\n",
    "pm=(1/(rc-1))*((p3*(rho-1))+(((p4*rho)-(p5*rc))/(n-1))-((p2-(p1*rc))/(n-1)))#...............#Mean effective pressure \n",
    "print \"Temperature after adiabatic compression: %0.2f K\\n\"%(t2)\n",
    "print \"Pressure after adiabatic compression: %0.2f bar\\n\"%(p2)\n",
    "print \"Temperature after isochoric compression: %0.2f K\\n\"%(t3)\n",
    "print \"Pressure after isochoric compression: %0.2f bar\\n\"%(p3)\n",
    "print \"Temperature after isobaric expansion: %0.2f K\\n\"%(t4)\n",
    "print \"Pressure after isobaric expansion: %0.2f bar\\n\"%(p4)\n",
    "print \"Temperature after adiabatic expansion: %0.2f K\\n\"%(t5)\n",
    "print \"Pressure after adiabatic expansion: %0.2f bar\\n\"%(p5)\n",
    "print \"Mean effective pressure = %0.2f bar\"%pm\n",
    "Vs=(pi/4)*D*D*L#....................#Swept volume in m**3\n",
    "W=(pm*(10**5)*Vs)/1000#.................#Work done per cycle in kJ\n",
    "m=(p1*(10**5)*(rc/(rc-1))*Vs)/(R*t1)#.....................#Mass of air per cycle in kg\n",
    "Qs=m*(cv*(t3-t2)+cp*(t4-t3))#.....................#Heat supplied per cycle in kJ\n",
    "eta=W/Qs#....................#Engine efficiency\n",
    "print \"Engine efficiency = %0.2f %%\"%(eta*100)\n",
    "P=W*N#.................#Power of the engine in kW\n",
    "print \"Power of the engine = %0.2f kW\"%P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.31 PAGE 140"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Work done = 282.90 kJ/kg\n",
      "Efficiency of cycle = 32.64 %\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "cp=0.92#..................#Specific heat at constant pressure in kJ/kgK\n",
    "cv=0.75#..................#Specific heat at constant volume in kJ/kgK\n",
    "p1=1#...................#Pressure at the end of adiabatic expansion in bar\n",
    "p2=p1#...................#Pressure at the end of isobaric compression in bar\n",
    "p3=4#....................#Pressure at the end of isobaric compression in bar\n",
    "p4=16#...................#Final pressure after heat addition in bar\n",
    "t2=300#.....................#Temperature at the end of isobaric compression in K\n",
    "ga=1.22#................#Ratio of specific heats\n",
    "#Calculations\n",
    "t3=t2*((p3/p2)**((ga-1)/ga))#............#Temperature at the end of isobaric compression in K\n",
    "t4=(p4*t3)/p3#........................#Final temperature after heat addition in K\n",
    "t1=t4/((p4/p1)**((ga-1)/ga))#...................#Temperature at the end of adiabatic compression in K\n",
    "Qs=cv*(t4-t3)#.........................#Heat supplied in kJ/kg\n",
    "Qr=cp*(t1-t2)#.........................#Heat rejected in kJ/kg\n",
    "W=Qs-Qr#.......................#Work done per kg of gas in kJ\n",
    "print \"Work done = %0.2f kJ/kg\"%W\n",
    "eta=W/Qs#......................#Efficiency of cycle\n",
    "print \"Efficiency of cycle = %0.2f %%\"%(eta*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.32 PAGE 145"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cycle efficiency = 40.07 %\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "p1=101.325#....................#Pressure of intake air in kPa\n",
    "t1=300#.......................#Temperature of intake air in kPa\n",
    "rp=6#.........................#Pressure ratio in the cycle\n",
    "ga=1.4#.........................#Ratio of specific heats\n",
    "rtc=2.5#...........................#Ratio of turbine work and compressor work\n",
    "#Calculations\n",
    "t2=t1*(rp**((ga-1)/ga))#..................#Temperature at the end of isentropic expansion in K\n",
    "t3=(rtc*(t2-t1))/(1-(1/(rp**((ga-1)/ga))))#........#Temperature at the end of isobaric expansion in K\n",
    "t4=t3/(rp**((ga-1)/ga))#.......................#Temperature at the end of isentropic compression in K\n",
    "eta=(t3-t4-t2+t1)/(t3-t2)#...................#Cycle efficiency\n",
    "print \"Cycle efficiency = %0.2f %%\"%(eta*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.33 PAGE 147"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Power developed per kg of gas per second = 362.01 kW \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "p1=1#....................#Intake pressure in bar\n",
    "p2=5#....................#Supply pressure in bar\n",
    "t3=1000#..................#Supply temperature in Kelvin\n",
    "cp=1.0425#................#Specific heat at constant pressure in kJ/kgK\n",
    "cv=0.7662#.................#Specific heat at constant volume in kJ/kgK\n",
    "ga=cp/cv#..................#Ratio of specific heats\n",
    "#Calculations\n",
    "t4=t3*((p1/p2)**((ga-1)/ga))#\n",
    "P=cp*(t3-t4)#.....................#Power developed per kg of gas per second in kW\n",
    "print \"Power developed per kg of gas per second = %0.2f kW \"%P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.34 PAGE 147"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temperature at turbine inlet = 423.51 K\n",
      "Power developed = 13.85 kW\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "ma=0.1#...................#Air supplied in kg/s\n",
    "p1=1#.....................#Supply pressure in bar\n",
    "t4=285#.................#Temperature of air when supplied to cabin in K\n",
    "p2=4#...................#Pressure at inlet to turbine in bar\n",
    "cp=1.0#..................#Specific heat at constant pressure in kJ/kgK\n",
    "ga=1.4#..................#Ratio of specific heats\n",
    "#Calculations\n",
    "t3=t4*((p2/p1)**((ga-1)/ga))#................#Temperature at turbine inlet in K\n",
    "print \"Temperature at turbine inlet = %0.2f K\"%t3\n",
    "P=ma*cp*(t3-t4)#...........................#Power developed in kW\n",
    "print \"Power developed = %0.2f kW\"%P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.35 PAGE 148"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Efficiency of the cycle : 30.09\n",
      "Heat supplied to the air = 456.17 kJ/kg \n",
      "Work available at the shaft = 137.25 kJ/kg \n",
      "Heat rejected in the cooler = 318.92 kJ/kg \n",
      "Temperature of air leaving the turbine = 610.33 K \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "p1=1#......................#Pressure of air entering the compressor in bar\n",
    "p2=3.5#.................#Pressure of air while leaving the compressor in bar\n",
    "t1=293#..................#Temperature of air at the onlet of the compressor in K\n",
    "t3=873#.................#Temperature of air at the turbine inlet in K\n",
    "cp=1.005#...............#Specific heat at constant pressure in kJ/kgK\n",
    "ga=1.4#...................#Ratio of specific heats\n",
    "#Calculations\n",
    "rp=p2/p1#....................#Pressure ratio of the cycle\n",
    "eta=1-(1/(rp**((ga-1)/ga)))#..............#Efficiency of the cycle\n",
    "print \"Efficiency of the cycle : %0.2f\"%(eta*100)\n",
    "t2=t1*((rp**((ga-1)/ga)))#................#Temperature of air while leaving the compressor in K\n",
    "q1=cp*(t3-t2)#................#Heat supplied to the air in kJ/kg\n",
    "print \"Heat supplied to the air = %0.2f kJ/kg \"%(q1)\n",
    "W=eta*q1#........................#Work available at the shaft in kJ/kg\n",
    "print \"Work available at the shaft = %0.2f kJ/kg \"%W\n",
    "q2=q1-W#................#Heat rejected in the cooler in kJ/kg\n",
    "print \"Heat rejected in the cooler = %0.2f kJ/kg \"%q2\n",
    "t4=t3/(rp**((ga-1)/ga))#.......................#Temperature of air leaving the turbine in K\n",
    "print \"Temperature of air leaving the turbine = %0.2f K \"%t4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.36 PAGE 149"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Maximum temperature = 1251.38 K \n",
      "Cycle efficiency = 40.07 %\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "p1=1#...................#Pressure of air entering the compressor in bar\n",
    "t1=300#.................#Temperature of air entering the compressor in bar\n",
    "rp=6#...................#Pressure ratio\n",
    "rtc=2.5#.................#Ratio of turbine work to compressor work\n",
    "ga=1.4#............#Ratio of specific heats\n",
    "#calculations\n",
    "t2=t1*(rp**((ga-1)/ga))#..................#Temperature at the end of isentropic expansion in K\n",
    "t3=(rtc*(t2-t1))/(1-(1/(rp**((ga-1)/ga))))#........#Temperature at the end of isobaric expansion in K\n",
    "t4=t3/(rp**((ga-1)/ga))#.......................#Temperature at the end of isentropic compression in K\n",
    "eta=(t3-t4-t2+t1)/(t3-t2)#...................#Cycle efficiency\n",
    "print \"Maximum temperature = %0.2f K \"%t3\n",
    "print \"Cycle efficiency = %0.2f %%\"%(eta*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.37 PAGE 150"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rate of fuel consumption = 0.02 kg/s \n",
      "Mass flow rate of air = 1.68 kg/s \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt\n",
    "# Initialisation of Variables\n",
    "t1=303#........................#Min temperature in K\n",
    "t3=1073#........................#Max temperature in K\n",
    "C=45000#.....................#Calorific value of fuel in kJ/kg\n",
    "cp=1#....................#Specific heat at constant pressure in kJ/kgK\n",
    "ga=1.4#........................#Ratio os specific heats\n",
    "diftc=100#..................#Difference between work done by turbine and compressor in kW\n",
    "#Calculations\n",
    "t2=sqrt(t1*t3)# t4 = t2#.....#Assumed\n",
    "mf=diftc/(C*(1-((t4-t1)/(t3-t2))))#................#Fuel used in kg per second\n",
    "print \"Rate of fuel consumption = %0.2f kg/s \"%mf\n",
    "ma=(diftc-(mf*(t3-t4)))/((t3-t4-cp*(t2-t1)))#............#Rate of air consumption in kg/s\n",
    "print \"Mass flow rate of air = %0.2f kg/s \"%(ma)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.38 PAGE 151"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compressor work = 259.32 kJ/kg \n",
      "Turbine work = 351.64 kJ/kg \n",
      "Heat supplied = 517.54 kJ/kg \n",
      "Cycle efficiency = 17.84 %\n",
      "Actual exhaust temperature of turbine = 723.11 K\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Initialisation of Variables\n",
    "t1=300#.................#Inlet temperature in K\n",
    "p1=1#....................#Inlet pressure in bar\n",
    "ma=1#....................#Mass of air in kg\n",
    "rp=6.25#.............#Pressure ratio\n",
    "t3=1073#.....#Maximum temperature in K\n",
    "etac=0.8#............#Efficiency of compressor\n",
    "etat=0.8#.............#Efficiency of turbine\n",
    "ga=1.4#.................#Ratio of specific heats\n",
    "cp=1.005#.............#Specific heat at constant pressure in kJ/kgK\n",
    "#Calculations\n",
    "t2=t1*(rp**((ga-1)/ga))#...........#Ideal Temperature of air while leaviing the compressor in K\n",
    "t21=((t2-t1)/etac)+t1#............#Actual Temperature of air while leaviing the compressor in K\n",
    "Wcomp=ma*cp*(t21-t1)#.............#Compressor work in kJ/kg\n",
    "t4=t3/(rp**((ga-1)/ga))#........#Ideal temperature of air while leaving the turbine in K\n",
    "t41=t3-(etat*(t3-t4))#..........#Actual temperature of air while leaving the turbine in K\n",
    "Wtur=ma*cp*(t3-t41)#..............#Turbine work in kJ/kg\n",
    "Wnet=Wtur-Wcomp#.................#Net work produced in kJ/kg\n",
    "Qs=ma*cp*(t3-t21)#.................#Heat supplied in kJ/kg\n",
    "print \"Compressor work = %0.2f kJ/kg \"%(Wcomp)\n",
    "print \"Turbine work = %0.2f kJ/kg \"%Wtur\n",
    "print \"Heat supplied = %0.2f kJ/kg \"%Qs\n",
    "print \"Cycle efficiency = %0.2f %%\"%((Wnet/Qs)*100)\n",
    "print \"Actual exhaust temperature of turbine = %0.2f K\"%t41"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.39 PAGE 153"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Air fuel ratio is 56:1\n"
     ]
    }
   ],
   "source": [
    "# Initialisation of Variables\n",
    "etat=0.85#..............#Turbine efficiency\n",
    "etac=0.8#...............#Compressor efficiency\n",
    "t3=1148#................#Max temperature in K\n",
    "t1=300#................#Temperature of working fluid when entering the compressor in Kelvin\n",
    "cp=1#...................#specific heat at constant pressure in kJ/kgK\n",
    "ga=1.4#................#ratio of specific heats\n",
    "p1=1#...................#Pressure of working fluid while entering the compressor in bar\n",
    "rp=4#...................#Pressure ratio\n",
    "C=42000#...............#Calorific value of fuel used in kJ/kgK\n",
    "perlcc=10#.............#Percentage loss of calorific value in combustion chamber\n",
    "\n",
    "#calculations\n",
    "p2=p1*rp#.................#pressure of air while leaving the compressor in bar\n",
    "etacc=1-(perlcc/100)#............#efficiency of combustion chamber\n",
    "t2=t1*(rp**((ga-1)/ga))#...........#Ideal Temperature of air while leaviing the compressor in K\n",
    "t21=((t2-t1)/etac)+t1#............#Actual Temperature of air while leaviing the compressor in K\n",
    "afr=((C*etacc)/(cp*(t3-t21)))-1#...........#Air fuel ratio\n",
    "print \"Air fuel ratio is %d:1\"%round(afr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EXAMPLE 3.40 PAGE 154"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Work done in process 1-2 = 148.93 kJ/kg \n",
      "Heat rejected in process 1-2 = 148.93 kJ/kg \n",
      "Work done in process 2-3 = 0.00 kJ/kg \n",
      "Heat rejected in process 2-3 = 0.00 kJ/kg \n",
      "Work done in process 3-4 = 446.80 kJ/kg \n",
      "Heat rejected in process 3-4 = 446.80 kJ/kg\n",
      "Work done in process 4-1 = 297.87 kJ/kg \n",
      "Heat rejected in process 4-1 = 297.87 kJ/kg \n",
      "Thermal efficiency of the cycle = 66.67 %\n"
     ]
    }
   ],
   "source": [
    "from math import log\n",
    "# Initialisation of Variables\n",
    "p1=1#...........#pressure before isothermal compression in bar\n",
    "t1=310#.........#temperature before isothermal compression in K\n",
    "p3=16#.........#pressure before isothermal expansion in bar\n",
    "t3=930#.........#temperature before isothermal expansion in K\n",
    "R=287#.............#Gas constant in kJ/kgK\n",
    "#Calculations\n",
    "v1=(R*t1)/(p1*10**5)#...............#Volume before isothermal compression in m**3\n",
    "v3=(R*t3)/(p3*10**5)#...............#Volume before isothermal expansion in m**3\n",
    "v2=v3#v4=v1#.................#2-3 and 1-4 are isochoric processes\n",
    "r=v1/v2#...................#Compression ratio\n",
    "q12=R*t1*log(r)#...............#Work done and heat rejected in process 1-2\n",
    "w12=q12#\n",
    "print \"Work done in process 1-2 = %0.2f kJ/kg \"%(q12/1000)\n",
    "print \"Heat rejected in process 1-2 = %0.2f kJ/kg \"%(w12/1000)\n",
    "q23=0#w23=q23#..................#COnstant volume process and hence work done is zero\n",
    "print \"Work done in process 2-3 = %0.2f kJ/kg \"%(q23/1000)\n",
    "print \"Heat rejected in process 2-3 = %0.2f kJ/kg \"%(q23/1000)\n",
    "q34=R*t3*log(r)#...............#Work done and heat rejected in process 1-2\n",
    "w34=q34#\n",
    "print \"Work done in process 3-4 = %0.2f kJ/kg \"%(q34/1000)\n",
    "print \"Heat rejected in process 3-4 = %0.2f kJ/kg\"%(w34/1000)\n",
    "q41=q34-q12\n",
    "w41=q41#\n",
    "print \"Work done in process 4-1 = %0.2f kJ/kg \"%(q41/1000)\n",
    "print \"Heat rejected in process 4-1 = %0.2f kJ/kg \"%(w41/1000)\n",
    "etath=w41/q34#.....................#Thermal efficiency\n",
    "print \"Thermal efficiency of the cycle = %0.2f %%\"%(etath*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
}
