{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 15 - Ideal gas power cycles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 1: pg 436"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.1\n",
      " The thermal efficiency of the cycle is (percent) =  49.0\n"
     ]
    }
   ],
   "source": [
    "#pg 436\n",
    "print('Example 15.1');\n",
    "\n",
    "# aim : To determine \n",
    "# the thermal efficiency of the cycle\n",
    "\n",
    "# given values\n",
    "T1 = 273.+400;#  temperature limit, [K]\n",
    "T3 = 273.+70;# temperature limit, [K]\n",
    "\n",
    "#  solution\n",
    "#  using equation [15] of section 15.3\n",
    "n_the = (T1-T3)/T1*100;# thermal efficiency \n",
    "#results\n",
    "print ' The thermal efficiency of the cycle is (percent) = ',round(n_the)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2: pg 437"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.2\n",
      " (a) The volume ratio of the adiabatic process is  =   4.425\n",
      "      The volume ratio of the isothermal process is  =  3.39\n",
      " (b) The thermal efficiency of the cycle is (percent) =  44.8\n"
     ]
    }
   ],
   "source": [
    "#pg 437\n",
    "print('Example 15.2');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the volume ratios of the isothermal and adiabatic processes\n",
    "# (b) the thermal efficiency of the cycle\n",
    "\n",
    "# given values\n",
    "T1 = 273.+260;# temperature, [K]\n",
    "T3 = 273.+21;# temperature, [K]\n",
    "er = 15.;# expansion ratio\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "\n",
    "# solution\n",
    "# (a)\n",
    "T2 = T1;\n",
    "T4 = T3;\n",
    "# for adiabatic process\n",
    "rva = (T1/T4)**(1/(Gama-1));# volume ratio of adiabatic\n",
    "rvi = er/rva;# volume ratio of isothermal\n",
    "print ' (a) The volume ratio of the adiabatic process is  =  ',round(rva,3)\n",
    "print '      The volume ratio of the isothermal process is  = ',round(rvi,2)\n",
    "\n",
    "# (b)\n",
    "n_the = (T1-T4)/T1*100;# thermal efficiency\n",
    "print ' (b) The thermal efficiency of the cycle is (percent) = ',round(n_the,1)\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3: pg 438"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.3\n",
      "(a) At line 1\n",
      " V1  =  0.096  m^3, t1  =  300.0  C,  P1  =  1730.0  kN/m^2 \n",
      "At line 2\n",
      " V2  =  0.288  m^3, t2  =  300.0  C,  P2  =  576.7  kN/m^2\n",
      "At line 3\n",
      " V3  =  0.576  m^3, t3  =  161.0  C,  P3  =  218.5  kN/m^2\n",
      "At line 4\n",
      " V4  =  0.192  m^3, t4  =  161.3  C,  P4  =  655.5  kN/m^2\n",
      " (b) The thermal efficiency of the cycle (percent) =  24.0\n",
      " (c) The work done per cycle is (kJ) =  44.2\n",
      " (d) The work ratio is  =   0.156\n",
      "there is calculation mistake in the book so answer is not matching\n"
     ]
    }
   ],
   "source": [
    "#pg 438\n",
    "print('Example 15.3');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the pressure, volume and temperature at each corner of the cycle\n",
    "# (b) the thermal efficiency of the cycle\n",
    "# (c) the work done per cycle\n",
    "# (d) the work ratio\n",
    "from math import log\n",
    "# given values\n",
    "m = 1;# mass of air, [kg]\n",
    "P1 = 1730.;# initial pressure of carnot engine, [kN/m^2]\n",
    "T1 = 273.+300;# initial temperature, [K]\n",
    "R = .29;# [kJ/kg K]\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "\n",
    "# solution\n",
    "# taking reference  Fig. 15.15\n",
    "# (a)\n",
    "# for the isothermal process 1-2\n",
    "# using ideal gas law\n",
    "V1 = m*R*T1/P1;# initial volume, [m^3]\n",
    "T2 = T1;\n",
    "V2 = 3.*V1;# given condition\n",
    "# for isothermal process, P1*V1=P2*V2, so\n",
    "P2 = P1*(V1/V2);# [MN/m^2]\n",
    "# for the adiabatic process 2-3\n",
    "V3 = 6.*V1;# given condition\n",
    "T3 = T2*(V2/V3)**(Gama-1);\n",
    "# also for adiabatic process, P2*V2^Gama=P3*V3^Gama, so\n",
    "P3 = P2*(V2/V3)**Gama;\n",
    "# for the isothermal process 3-4\n",
    "T4 = T3;\n",
    "# for both adiabatic processes, the temperataure ratio is same, \n",
    "# T1/T4 = T2/T3=(V4/V1)^(Gama-1)=(V3/V2)^(Gama-1), so\n",
    "V4 = 2*V1;\n",
    "# for isothermal process, 3-4, P3*V3=P4*V4, so\n",
    "P4 = P3*(V3/V4);\n",
    "print '(a) At line 1'\n",
    "print ' V1  = ',round(V1,3),' m^3, t1  = ',T1-273,' C,  P1  = ',P1,' kN/m^2 '\n",
    "\n",
    "print 'At line 2'\n",
    "print ' V2  = ',round(V2,3),' m^3, t2  = ',T2-273,' C,  P2  = ',round(P2,1),' kN/m^2'\n",
    "\n",
    "print 'At line 3'\n",
    "print ' V3  = ',round(V3,3),' m^3, t3  = ',round(T3-273),' C,  P3  = ',round(P3,1),' kN/m^2'\n",
    "\n",
    "\n",
    "print 'At line 4'\n",
    "print ' V4  = ',round(V4,3),' m^3, t4  = ',round(T4-273,1),' C,  P4  = ',round(P4,1),' kN/m^2'\n",
    "\n",
    "\n",
    "# (b)\n",
    "n_the = (T1-T3)/T1;# thermal efficiency\n",
    "print ' (b) The thermal efficiency of the cycle (percent) = ',round(n_the*100)\n",
    "\n",
    "# (c)\n",
    "W = m*R*T1*log(V2/V1)*n_the;#  work done, [J]\n",
    "print ' (c) The work done per cycle is (kJ) = ',round(W,1)\n",
    "\n",
    "#  (d)\n",
    "wr = (T1-T3)*log(V2/V1)/(T1*log(V2/V1)+(T1-T3)/(Gama-1));# work ratio\n",
    "print ' (d) The work ratio is  =  ',round(wr,3)\n",
    "\n",
    "print 'there is calculation mistake in the book so answer is not matching'\n",
    "\n",
    "#   End\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4: pg 446"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.4\n",
      " (a)  P1  =   100.0  kN/m^2,    V1  =  0.084  m^3,       t1  =  28.0  C,\n",
      "      P2  =   1229.0  kN/m^2,    V2  =  0.014  m^3,      t2  =  343.0  C,\n",
      "      P3  =  1229.0  kN/m^2,    V3  =  0.019  m^3,       t3  =  549.0  C,\n",
      "      P4  =  100.0  kN/m^2,      V4  =  0.112  m^3,       t4  =  128.0  C\n",
      " (b) The heat received is (kJ) =  20.07\n",
      " (c) The work done is (kJ) =  10.27\n",
      " (d) The thermal efficiency is (percent) =  51.2\n",
      " (e) The carnot efficiency is (percent) =  63.4\n",
      " (f) The work ratio is  =   0.293\n",
      " (g) The mean effective pressure is (kN/m^2) =  104.77\n",
      " there is minor variation in answer reported in the book due to rounding off error\n"
     ]
    }
   ],
   "source": [
    "#pg 446\n",
    "print('Example 15.4');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the pressure, volume and temperature at cycle state points\n",
    "# (b) the heat received\n",
    "# (c) the work done \n",
    "# (d) the thermal efficiency\n",
    "# (e) the carnot efficiency\n",
    "# (f) the work ration\n",
    "# (g) the mean effective pressure\n",
    "\n",
    "# given values\n",
    "ro = 8.;# overall volume ratio;\n",
    "rv = 6.;# volume ratio of adiabatic compression\n",
    "P1 = 100.;# initial pressure , [kN/m^2]\n",
    "V1 = .084;# initial volume, [m^3]\n",
    "T1 = 273.+28;# initial temperature, [K]\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "cp = 1.006;# specific heat capacity, [kJ/kg K]\n",
    "\n",
    "# solution\n",
    "# taking reference  Fig. 15.18\n",
    "# (a)\n",
    "V2 = V1/rv;# volume at stage2, [m^3] \n",
    "V4 = ro*V2;# volume at stage 4;[m^3]\n",
    "# using PV^(Gama)=constant for process 1-2\n",
    "P2 = P1*(V1/V2)**(Gama);# pressure at stage2,. [kN/m^2]\n",
    "T2 = T1*(V1/V2)**(Gama-1);# [K]\n",
    "\n",
    "P3 = P2;# pressure at stage 3, [kN/m^2]\n",
    "V3 = V4/rv;# volume at stage 3, [m^3]\n",
    "# since pressure is constant in process 2-3 , so using V/T=constant, so\n",
    "T3 = T2*(V3/V2);# temperature at stage 3, [K]\n",
    "\n",
    "# for process 1-4\n",
    "T4 = T1*(V4/V1);# temperature at stage4, [K\n",
    "P4 = P1;# pressure at stage4, [kN/m^2]\n",
    "\n",
    "print ' (a)  P1  =  ',P1,' kN/m^2,    V1  = ',V1,' m^3,       t1  = ',T1-273,' C,\\n      P2  =  ',round(P2),' kN/m^2,    V2  = ',V2,' m^3,      t2  = ',round(T2-273),' C,\\n      P3  = ',round(P3),' kN/m^2,    V3  = ',round(V3,3),' m^3,       t3  = ',round(T3-273),' C,\\n      P4  = ',P4,' kN/m^2,      V4  = ',V4,' m^3,       t4  = ',round(T4-273),' C'\n",
    "\n",
    "# (b)\n",
    "R = cp*(Gama-1)/Gama;# gas constant, [kJ/kg K]\n",
    "m = P1*V1/(R*T1);# mass of gas, [kg]\n",
    "Q = m*cp*(T3-T2);# heat received, [kJ]\n",
    "print ' (b) The heat received is (kJ) = ',round(Q,2)\n",
    "\n",
    "# (c) \n",
    "W = P2*(V3-V2)-P1*(V4-V1)+((P3*V3-P4*V4)-(P2*V2-P1*V1))/(Gama-1);# work done, [kJ]\n",
    "print ' (c) The work done is (kJ) = ',round(W,2)\n",
    "\n",
    "#  (d)\n",
    "TE = 1-T1/T2;# thermal efficiency\n",
    "print ' (d) The thermal efficiency is (percent) = ',round(TE*100,1)\n",
    "\n",
    "# (e)\n",
    "CE = (T3-T1)/T3;# carnot efficiency\n",
    "print ' (e) The carnot efficiency is (percent) = ',round(CE*100,1)\n",
    "\n",
    "# (f)\n",
    "PW = P2*(V3-V2)+(P3*V3-P4*V4)/(Gama-1);# positive work done, [kj]\n",
    "WR = W/PW;#  work ratio\n",
    "print ' (f) The work ratio is  =  ',round(WR,3)\n",
    "\n",
    "# (g)\n",
    "Pm = W/(V4-V2);# mean effective pressure, [kN/m^2]\n",
    "print ' (g) The mean effective pressure is (kN/m^2) = ',round(Pm,2)\n",
    "\n",
    "print ' there is minor variation in answer reported in the book due to rounding off error'\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 5: pg 450"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.5\n",
      " (a) The actual thermal efficiency of the turbine is (percent) =  26.88\n",
      " (b) The specific fuel consumption of the turbine is (kg/kWh) =  0.311\n"
     ]
    }
   ],
   "source": [
    "#pg 450\n",
    "print('Example 15.5');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the actual thermal efficiency of the turbine\n",
    "# (b) the specific fuel consumption of the turbine in kg/kWh\n",
    "\n",
    "# given values\n",
    "P2_by_P1 = 8;\n",
    "n_tur = .6;# ideal turbine thermal efficiency\n",
    "c = 43.*10**3;# calorific value of fuel, [kJ/kg]\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "\n",
    "# solution\n",
    "# (a)\n",
    "rv = P2_by_P1;\n",
    "n_tur_ide = 1-1/(P2_by_P1)**((Gama-1)/Gama);# ideal thermal efficiency\n",
    "ate = n_tur_ide*n_tur;# actual thermal efficiency\n",
    "print ' (a) The actual thermal efficiency of the turbine is (percent) = ',round(ate*100,2)\n",
    "\n",
    "# (b)\n",
    "ewf = c*ate;# energy to work fuel, [kJ/kg]\n",
    "kWh = 3600.;# energy equivalent ,[kJ]\n",
    "sfc = kWh/ewf;# specific fuel consumption, [kg/kWh]\n",
    "print ' (b) The specific fuel consumption of the turbine is (kg/kWh) = ',round(sfc,3)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6: pg 456"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.6\n",
      " The relative efficiency of the engine is (percent) =  38.8\n"
     ]
    }
   ],
   "source": [
    "#pg 456\n",
    "print('Example 15.6');\n",
    "\n",
    "# aim : To determine\n",
    "# the relative efficiency of the engine\n",
    "import math\n",
    "# given values\n",
    "d = 80;# bore, [mm]\n",
    "l = 85;# stroke, [mm]\n",
    "V1 = .06*10**6;# clearence volume, [mm^3]\n",
    "ate = .22;# actual thermal efficiency of the engine\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "\n",
    "# solution\n",
    "sv = math.pi*d**2/4*l;# stroke volume, [mm^3]\n",
    "V2 = sv+V1;# [mm^3]\n",
    "rv = V2/V1;\n",
    "ite = 1-(1/rv)**(Gama-1);# ideal thermal efficiency\n",
    "re = ate/ite;# relative thermal efficiency\n",
    "#results\n",
    "print ' The relative efficiency of the engine is (percent) = ',round(re*100,1)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7: pg 457"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.7\n",
      " (a)  P1  =   103.0  kN/m^2,    V1  =  1.039  m^3,       t1  =  100.0  C,\n",
      "      P2  =   1265.0  kN/m^2,    V2  =  0.173  m^3,      t2  =  491.0  C,\n",
      "      P3  =  3450.0  kN/m^2,    V3  =  0.173  m^3,       t3  =  1809.0  C,\n",
      "      P4  =  280.8  kN/m^2,      V4  =  1.039  m^3,       t4  =  744.0  C\n",
      " (b) The heat transferred to the air is (kJ) =  946.0\n",
      " (c) The heat rejected by the air is (kJ) =  462.0\n",
      " (d) The ideal thermal efficiency is (percent) =  51.2\n",
      " (e) The work done  is (kJ) =  484.0\n",
      " (f)  The mean effective pressure is (kN/m^2) =  558.85\n",
      "The answers are a bit different due to rounding off error in textbook\n"
     ]
    }
   ],
   "source": [
    "#pg 457\n",
    "print('Example 15.7');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the pressure, volume and temperature at each cycle process change points\n",
    "# (b) the heat transferred to air\n",
    "# (c) the heat rejected by the air\n",
    "# (d) the ideal thermal efficiency\n",
    "# (e) the work done \n",
    "# (f) the mean effective pressure\n",
    "\n",
    "# given values\n",
    "m = 1.;# mass of air, [kg]\n",
    "rv = 6.;# volume ratio of adiabatic compression\n",
    "P1 = 103.;# initial pressure , [kN/m^2]\n",
    "T1 = 273.+100;# initial temperature, [K]\n",
    "P3 = 3450.;# maximum pressure, [kN/m^2]\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "R = .287;# gas constant, [kJ/kg K]\n",
    "\n",
    "# solution\n",
    "# taking reference  Fig. 15.20\n",
    "# (a)\n",
    "# for point 1\n",
    "V1 = m*R*T1/P1;# initial volume, [m^3]\n",
    "\n",
    "# for point 2\n",
    "V2 = V1/rv;# volume at point 2, [m^3] \n",
    "# using PV^(Gama)=constant for process 1-2\n",
    "P2 = P1*(V1/V2)**(Gama);# pressure at point 2,. [kN/m^2]\n",
    "T2 = T1*(V1/V2)**(Gama-1);# temperature at point 2,[K]\n",
    "\n",
    "# for point 3\n",
    "V3 = V2;# volume at point 3, [m^3]\n",
    "# since volume is constant in process 2-3 , so using P/T=constant, so\n",
    "T3 = T2*(P3/P2);# temperature at stage 3, [K]\n",
    "\n",
    "# for point 4\n",
    "V4 = V1;#  volume at point 4, [m^3]\n",
    "P4 = P3*(V3/V4)**Gama;# pressure at point 4, [kN/m^2] \n",
    "# again  since volume is constant in process 4-1 , so using P/T=constant, so\n",
    "T4 = T1*(P4/P1);# temperature at point 4, [K]\n",
    "\n",
    "print ' (a)  P1  =  ',P1,' kN/m^2,    V1  = ',round(V1,3),' m^3,       t1  = ',T1-273,' C,\\n      P2  =  ',round(P2),' kN/m^2,    V2  = ',round(V2,3),' m^3,      t2  = ',round(T2-273),' C,\\n      P3  = ',round(P3),' kN/m^2,    V3  = ',round(V3,3),' m^3,       t3  = ',round(T3-273),' C,\\n      P4  = ',round(P4,1),' kN/m^2,      V4  = ',round(V4,3),' m^3,       t4  = ',round(T4-273),' C'\n",
    "\n",
    "#  (b)\n",
    "cv = R/(Gama-1);#  specific heat capacity, [kJ/kg K]\n",
    "Q23 = m*cv*(T3-T2);# heat transferred, [kJ]\n",
    "print ' (b) The heat transferred to the air is (kJ) = ',round(Q23)\n",
    "\n",
    "# (c) \n",
    "Q34 = m*cv*(T4-T1);# heat rejected by air, [kJ]\n",
    "print ' (c) The heat rejected by the air is (kJ) = ',round(Q34,1)\n",
    "\n",
    "# (d)\n",
    "TE = 1-Q34/Q23;# ideal thermal efficiency\n",
    "print ' (d) The ideal thermal efficiency is (percent) = ',round(TE*100,1)\n",
    "\n",
    "# (e)\n",
    "W = Q23-Q34;# work done ,[kJ]\n",
    "print ' (e) The work done  is (kJ) = ',round(W,1)\n",
    "# (f)\n",
    "Pm = W/(V1-V2);# mean effective pressure, [kN/m^2]\n",
    "print ' (f)  The mean effective pressure is (kN/m^2) = ',round(Pm,2)\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 8: pg 460"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.8\n",
      " (a)  P1  =   101.0  kN/m^2,    V1  =  0.003  m^3,       t1  =  18.0  C,\n",
      "      P2  =   2213.0  kN/m^2,    V2  =  0.0  m^3,      t2  =  436.0  C,\n",
      "      P3  =  4500.0  kN/m^2,    V3  =  0.0  m^3,       t3  =  1168.0  C,\n",
      "      P4  =  205.3  kN/m^2,      V4  =  0.003  m^3,       t4  =  319.0  C\n",
      " (b) The thermal efficiency is (percent) =  59.0\n",
      " (c) The theoretical output  is (kW) =  55.5\n",
      " (g) The mean effefctive pressure is (kN/m^2) =  415.9\n",
      " (e) The carnot efficiency is (percent) =  79.8\n"
     ]
    }
   ],
   "source": [
    "#pg 460\n",
    "print('Example 15.8');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the pressure, volume and temperature at cycle state points\n",
    "# (b) the thermal efficiency\n",
    "# (c) the theoretical output\n",
    "# (d) the mean effective pressure\n",
    "# (e) the carnot efficiency\n",
    "\n",
    "# given values\n",
    "rv = 9.;# volume ratio\n",
    "P1 = 101.;# initial pressure , [kN/m^2]\n",
    "V1 = .003;# initial volume, [m^3]\n",
    "T1 = 273.+18;# initial temperature, [K]\n",
    "P3 = 4500.;# maximum pressure, [kN/m^2]\n",
    "N = 3000.;\n",
    "cp = 1.006;# specific heat capacity at constant pressure, [kJ/kg K]\n",
    "cv = .716;# specific heat capacity at constant volume, [kJ/kg K]\n",
    "\n",
    "# solution\n",
    "# taking reference  Fig. 15.20\n",
    "# (a)\n",
    "# for process 1-2\n",
    "Gama = cp/cv;# heat capacity ratio\n",
    "R = cp-cv;# gas constant, [kJ/kg K]\n",
    "V2 = V1/rv;# volume at stage2, [m^3] \n",
    "# using PV^(Gama)=constant for process 1-2\n",
    "P2 = P1*(V1/V2)**(Gama);# pressure at stage2,. [kN/m^2]\n",
    "T2 = T1*(V1/V2)**(Gama-1);# [K]\n",
    "\n",
    "# for process 2-3\n",
    "V3 = V2;# volume at stage 3, [m^3]\n",
    "# since volume is constant in process 2-3 , so using P/T=constant, so\n",
    "T3 = T2*(P3/P2);# temperature at stage 3, [K]\n",
    "\n",
    "# for process 3-4\n",
    "V4 = V1;# volume at stage 4\n",
    "# using PV^(Gama)=constant for process 3-4\n",
    "P4 = P3*(V3/V4)**(Gama);# pressure at stage2,. [kN/m^2]\n",
    "T4 = T3*(V3/V4)**(Gama-1);# temperature at stage  4,[K]\n",
    "\n",
    "print ' (a)  P1  =  ',P1,' kN/m^2,    V1  = ',round(V1,3),' m^3,       t1  = ',T1-273,' C,\\n      P2  =  ',round(P2),' kN/m^2,    V2  = ',round(V2,3),' m^3,      t2  = ',round(T2-273),' C,\\n      P3  = ',round(P3),' kN/m^2,    V3  = ',round(V3,3),' m^3,       t3  = ',round(T3-273),' C,\\n      P4  = ',round(P4,1),' kN/m^2,      V4  = ',round(V4,3),' m^3,       t4  = ',round(T4-273),' C'\n",
    "\n",
    "# (b)\n",
    "TE = 1-(T4-T1)/(T3-T2);# thermal efficiency\n",
    "print ' (b) The thermal efficiency is (percent) = ',round(TE*100)\n",
    "\n",
    "# (c)\n",
    "m = P1*V1/(R*T1);# mass os gas, [kg] \n",
    "W = m*cv*((T3-T2)-(T4-T1));# work done, [kJ]\n",
    "Wt = W*N/60;# workdone per minute, [kW]\n",
    "print ' (c) The theoretical output  is (kW) = ',round(Wt,1)\n",
    "\n",
    "# (d)\n",
    "Pm = W/(V1-V2);# mean effective pressure, [kN/m^2]\n",
    "print ' (g) The mean effefctive pressure is (kN/m^2) = ',round(Pm,1)\n",
    "\n",
    "# (e)\n",
    "CE = (T3-T1)/T3;# carnot efficiency\n",
    "print ' (e) The carnot efficiency is (percent) = ',CE*100\n",
    "\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9: pg 467"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.9\n",
      " (a)  P1  =   90.0  kN/m^2,    V1  =  0.998  m^3,       t1  =  40.0  C,\n",
      "      P2  =   4365.0  kN/m^2,    V2  =  0.062  m^3,      t2  =  676.0  C,\n",
      "      P3  =  4365.0  kN/m^2,    V3  =  0.11  m^3,       t3  =  1400.0  C,\n",
      "      P4  =  199.1  kN/m^2,      V4  =  0.998  m^3,       t4  =  419.0  C\n",
      " (b) The work done is (kJ) =  454.959974156\n",
      " (c) The thermal efficiency is (percent) =  62.6\n",
      " (d) The work ratio is  =   0.318\n",
      " (e) The mean effefctive pressure is (kN/m^2) =  486.4\n",
      " (f) The carnot efficiency is (percent) =  81.3\n",
      "value of t2 printed in the book is incorrect\n"
     ]
    }
   ],
   "source": [
    "#pg 467\n",
    "print('Example 15.9');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the pressure and temperature at cycle process change points\n",
    "# (b) the work done \n",
    "# (c)  the thermal efficiency\n",
    "# (d)  the work ratio\n",
    "# (e)  the mean effective pressure\n",
    "# (f)  the carnot efficiency\n",
    "\n",
    "\n",
    "# given values\n",
    "rv = 16.;# volume ratio of compression\n",
    "P1 = 90.;# initial pressure , [kN/m^2]\n",
    "T1 = 273.+40;# initial temperature, [K]\n",
    "T3 = 273.+1400;# maximum temperature, [K]\n",
    "cp = 1.004;# specific heat capacity at constant pressure, [kJ/kg K]\n",
    "Gama = 1.4;# heat capacoty ratio\n",
    "\n",
    "# solution\n",
    "cv = cp/Gama;# specific heat capacity at constant volume, [kJ/kg K]\n",
    "R = cp-cv;# gas constant, [kJ/kg K]\n",
    "# for one kg of gas\n",
    "V1 = R*T1/P1;# initial volume, [m^3]\n",
    "# taking reference  Fig. 15.22\n",
    "# (a)\n",
    "# for process 1-2\n",
    "# using PV^(Gama)=constant for process 1-2\n",
    "# also rv = V1/V2\n",
    "P2 = P1*(rv)**(Gama);# pressure at stage2,. [kN/m^2]\n",
    "T2 = T1*(rv)**(Gama-1);# temperature at stage 2, [K]\n",
    "\n",
    "# for process 2-3\n",
    "P3 = P2;# pressure at stage 3, [kN/m^2]\n",
    "V2 = V1/rv;#[m^3]\n",
    "# since pressure is constant in process 2-3 , so using V/T=constant, so\n",
    "V3 = V2*(T3/T2);# volume at stage 3, [m^3]\n",
    "\n",
    "# for process 1-4\n",
    "V4 = V1;# [m^3]\n",
    "P4 = P3*(V3/V4)**(Gama)\n",
    "# since in stage 1-4 volume is constant, so P/T=constant, \n",
    "T4 = T1*(P4/P1);# temperature at stage  4,[K]\n",
    "\n",
    "print ' (a)  P1  =  ',P1,' kN/m^2,    V1  = ',round(V1,3),' m^3,       t1  = ',T1-273,' C,\\n      P2  =  ',round(P2),' kN/m^2,    V2  = ',round(V2,3),' m^3,      t2  = ',round(T2-273),' C,\\n      P3  = ',round(P3),' kN/m^2,    V3  = ',round(V3,3),' m^3,       t3  = ',round(T3-273),' C,\\n      P4  = ',round(P4,1),' kN/m^2,      V4  = ',round(V4,3),' m^3,       t4  = ',round(T4-273),' C'\n",
    "\n",
    "# (b)\n",
    "W = cp*(T3-T2)-cv*(T4-T1);# work done, [kJ]\n",
    "print ' (b) The work done is (kJ) = ',W\n",
    "\n",
    "# (c) \n",
    "TE = 1-(T4-T1)/((T3-T2)*Gama);# thermal efficiency\n",
    "print ' (c) The thermal efficiency is (percent) = ',round(TE*100,1)\n",
    "\n",
    "# (d)\n",
    "PW = cp*(T3-T2)+R*(T3-T4)/(Gama-1);# positive work done\n",
    "WR = W/PW;#  work ratio\n",
    "print ' (d) The work ratio is  =  ',round(WR,3)\n",
    "\n",
    "# (e)\n",
    "Pm = W/(V1-V2);# mean effective pressure, [kN/m^2]\n",
    "print ' (e) The mean effefctive pressure is (kN/m^2) = ',round(Pm,1)\n",
    "\n",
    "# (f)\n",
    "CE = (T3-T1)/T3;# carnot efficiency\n",
    "print ' (f) The carnot efficiency is (percent) = ',round(CE*100,1)\n",
    "\n",
    "print 'value of t2 printed in the book is incorrect'\n",
    "\n",
    "# End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 10: pg 470"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 10\n",
      " (a) The maximum temperature during the cycle is (C) =  1595.4\n",
      " (b) The thermal efficiency of the cycle is (percent) =  60.3\n"
     ]
    }
   ],
   "source": [
    "#pg 470\n",
    "print('Example 10');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the maximum temperature attained during the cycle\n",
    "# (b) the thermal efficiency of the cycle\n",
    "\n",
    "# given value\n",
    "rva  =7.5;# volume ratio of adiabatic expansion\n",
    "rvc  =15.;# volume ratio of compression\n",
    "P1 = 98.;# initial pressure, [kn/m^2]\n",
    "T1 = 273.+44;# initial temperature, [K]\n",
    "P4 = 258.;# pressure at the end of the adiabatic expansion, [kN/m^2]\n",
    "Gama = 1.4;#  heat capacity  ratio\n",
    "\n",
    "# solution\n",
    "# by seeing diagram\n",
    "# for process 4-1, P4/T4=P1/T1\n",
    "T4 = T1*(P4/P1);# [K]\n",
    "# for process 3-4\n",
    "T3 = T4*(rva)**(Gama-1);\n",
    "print ' (a) The maximum temperature during the cycle is (C) = ',round(T3-273,1)\n",
    "\n",
    "# (b)\n",
    "\n",
    "# for process 1-2,\n",
    "T2 = T1*(rvc)**(Gama-1);# [K]\n",
    "n_the = 1-(T4-T1)/((Gama)*(T3-T2));# thermal efficiency\n",
    "print ' (b) The thermal efficiency of the cycle is (percent) = ',round(n_the*100,1)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 11: pg 471"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.11\n",
      " (a) the thermal efficiency of the cycle is (percent) =  55.1\n",
      " (c) The indicated power of the cycle is (kW) =  26.59\n"
     ]
    }
   ],
   "source": [
    "#pg 471\n",
    "print('Example 15.11');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the thermal efficiency of the cycle\n",
    "# (b) the indicared power of the cycle\n",
    "\n",
    "# given values\n",
    "# taking basis one second\n",
    "rv = 11.;# volume ratio\n",
    "P1 = 96.;# initial pressure , [kN/m^2]\n",
    "T1 = 273.+18;# initial temperature, [K]\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "\n",
    "# solution\n",
    "# taking reference  Fig. 15.24\n",
    "# (a)\n",
    "Beta = 2;# ratio of V3 and V2\n",
    "TE = 1-(Beta**(Gama)-1)/((rv**(Gama-1))*Gama*(Beta-1));# thermal efficiency\n",
    "print ' (a) the thermal efficiency of the cycle is (percent) = ',round(TE*100,1)\n",
    "\n",
    "# (b) \n",
    "# let V1-V2=.05, so\n",
    "V2 = .05*.1;# [m^3]\n",
    "# from this\n",
    "V1 = rv*V2;# [m^3]\n",
    "V3 = Beta*V2;# [m^3]\n",
    "V4 = V1;# [m^3]\n",
    "P2 = P1*(V1/V2)**(Gama);# [kN/m^2]\n",
    "P3 = P2;# [kn/m^2]\n",
    "P4=P3*(V3/V4)**(Gama);# [kN/m^2]\n",
    "# indicated power\n",
    "W = P2*(V3-V2)+((P3*V3-P4*V4)-(P2*V2-P1*V1))/(Gama-1);# indicated power, [kW]\n",
    "print ' (c) The indicated power of the cycle is (kW) = ',round(W,2)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 12: pg 477"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.12\n",
      " (a) The pressure at the end of compression is (MN/m^2) =  5.0\n",
      "      The temperature at the end of compression is (C) =  621.0\n",
      " (b) The pressure at the end of constant volume process is  (MN/m^2) =  6.9\n",
      "     The temperature at the end of constant volume process is (C) =  962.0\n",
      " (c) The temperature at the end of constant pressure process is (C) =  1517.0\n"
     ]
    }
   ],
   "source": [
    "#pg 477\n",
    "print('Example 15.12');\n",
    "# aim : To determine\n",
    "# (a) the pressure and temperature at the end of compression\n",
    "# (b) the pressure and temperature at the end of the constant volume process\n",
    "# (c) the temperature at the end of constant pressure process\n",
    "\n",
    "# given values\n",
    "P1 = 103.;# initial pressure, [kN/m^2]\n",
    "T1 = 273.+22;# initial temperature, [K]\n",
    "rv = 16.;# volume ratio of the compression\n",
    "Q = 244.;#heat added, [kJ/kg]\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "cv = .717;# heat capacity, [kJ/kg k]\n",
    "\n",
    "# solution\n",
    "#  taking reference as Fig.15.26\n",
    "# (a)\n",
    "# for compression\n",
    "# rv = V1/V2\n",
    "P2 = P1*(rv)**Gama;# pressure at end of compression, [kN/m^2]\n",
    "T2 = T1*(rv)**(Gama-1);# temperature at end of compression, [K]\n",
    "print ' (a) The pressure at the end of compression is (MN/m^2) = ',round(P2*10**-3)\n",
    "print '      The temperature at the end of compression is (C) = ',round(T2-273)\n",
    "\n",
    "# (b)\n",
    "# for constant volume process, \n",
    "# Q = cv*(T3-T2), so\n",
    "T3 = T2+Q/cv;# temperature at the end of constant volume, [K]\n",
    "\n",
    "# so for constant volume, P/T=constant, hence\n",
    "P3 = P2*(T3/T2);# pressure at the end of constant volume process, [kN/m^2]\n",
    "print ' (b) The pressure at the end of constant volume process is  (MN/m^2) = ',round(P3*10**-3,1)\n",
    "print '     The temperature at the end of constant volume process is (C) = ',round(T3-273)\n",
    "\n",
    "# (c)\n",
    "S = rv-1;# stroke\n",
    "# assuming \n",
    "V3 = 1;# [volume]\n",
    "#so\n",
    "V4 = V3+S*.03;# [volume]\n",
    "# also for constant process V/T=constant, hence\n",
    "T4 = T3*(V4/V3);# temperature at the end of constant presure process, [k] \n",
    "print ' (c) The temperature at the end of constant pressure process is (C) = ',round(T4-273)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 13: pg 479"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.13\n",
      " (a)  P1  =   0.097  kN/m^2,    V1  =  0.084  m^3,       t1  =  28.0  C,\n",
      "      P2  =   4.0  kN/m^2,    V2  =  0.0056  m^3,      t2  =  620.0  C,\n",
      "      P3  =  6.0  kN/m^2,    V3  =  0.0056  m^3,       t3  =  1010.0  C,\n",
      "      P4  =  6.2  kN/m^2,      V4  =  0.006955  m^3,       t4  =  1320.0  C   \n",
      " P5  =  0.2  kN/m^2,      V5  =  0.084  m^3,       t5  =  313.0  C\n",
      " (b) The net work done is (kJ) =  36.4\n",
      " (c) The thermal efficiency is (percent) =  65.5\n",
      " (d) The heat received is (kJ) =  55.6\n",
      " (f) The work ratio is  =  0.477\n",
      " (e) The mean effective pressure is (kN/m^2) =  464.0\n",
      " (f) The carnot efficiency is (percent) =  81.0\n"
     ]
    }
   ],
   "source": [
    "#pg 479\n",
    "print('Example 15.13');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the pressure, volume and temperature at cycle process change points\n",
    "# (b) the net work done \n",
    "# (c)  the thermal efficiency\n",
    "# (d) the heat received\n",
    "# (e)  the work ratio\n",
    "# (f)  the mean effective pressure\n",
    "# (g)  the carnot efficiency\n",
    "\n",
    "\n",
    "# given values\n",
    "rv = 15.;# volume ratio\n",
    "P1 = 97.*10**-3;# initial pressure , [MN/m^2]\n",
    "V1 = .084;# initial volume, [m^3]\n",
    "T1 = 273.+28;# initial temperature, [K]\n",
    "T4 = 273.+1320;# maximum temperature, [K]\n",
    "P3 = 6.2;# maximum pressure, [MN/m^2]\n",
    "cp = 1.005;# specific heat capacity at constant pressure, [kJ/kg K]\n",
    "cv = .717;# specific heat capacity at constant volume, [kJ/kg K]\n",
    "\n",
    "# solution\n",
    "# taking reference  Fig. 15.27\n",
    "# (a)\n",
    "R = cp-cv;# gas constant, [kJ/kg K]\n",
    "Gama = cp/cv;# heat capacity ratio\n",
    "# for process 1-2\n",
    "V2 = V1/rv;# volume at stage2, [m^3] \n",
    "# using PV^(Gama)=constant for process 1-2\n",
    "P2 = P1*(V1/V2)**(Gama);# pressure at stage2,. [MN/m^2]\n",
    "T2 = T1*(V1/V2)**(Gama-1);# temperature at stage 2, [K]\n",
    "\n",
    "# for process 2-3\n",
    "# since volumee is constant in process 2-3 , so using P/T=constant, so\n",
    "T3 = T2*(P3/P2);# volume at stage 3, [K]\n",
    "V3 = V2;# volume at stage 3, [MN/m^2]\n",
    "\n",
    "# for process 3-4\n",
    "P4 = P3;# pressure at stage 4, [m^3]\n",
    "# since in stage 3-4 P is constant, so V/T=constant, \n",
    "V4 = V3*(T4/T3);# temperature at stage  4,[K]\n",
    "\n",
    "# for process 4-5\n",
    "V5 = V1;# volume at stage 5, [m^3]\n",
    "P5 = P4*(V4/V5)**(Gama);# pressure at stage5,. [MN/m^2]\n",
    "T5 = T4*(V4/V5)**(Gama-1);# temperature at stage 5, [K]\n",
    "\n",
    "print ' (a)  P1  =  ',P1,' kN/m^2,    V1  = ',round(V1,3),' m^3,       t1  = ',T1-273,' C,\\n      P2  =  ',round(P2),' kN/m^2,    V2  = ',round(V2,6),' m^3,      t2  = ',round(T2-273),' C,\\n      P3  = ',round(P3),' kN/m^2,    V3  = ',round(V3,6),' m^3,       t3  = ',round(T3-273),' C,\\n      P4  = ',round(P4,1),' kN/m^2,      V4  = ',round(V4,6),' m^3,       t4  = ',round(T4-273),' C   \\n P5  = ',round(P5,1),' kN/m^2,      V5  = ',round(V5,3),' m^3,       t5  = ',round(T5-273),' C'\n",
    "\n",
    "\n",
    "# (b)\n",
    "W = (P3*(V4-V3)+((P4*V4-P5*V5)-(P2*V2-P1*V1))/(Gama-1))*10**3;# work done, [kJ]\n",
    "print ' (b) The net work done is (kJ) = ',round(W,1)\n",
    "\n",
    "# (c) \n",
    "TE = 1-(T5-T1)/((T3-T2)+Gama*(T4-T3));# thermal efficiency\n",
    "print ' (c) The thermal efficiency is (percent) = ',round(TE*100,1)\n",
    "\n",
    "# (d)\n",
    "Q = W/TE;# heat received, [kJ]\n",
    "print ' (d) The heat received is (kJ) = ',round(Q,1)\n",
    "\n",
    "# (e)\n",
    "PW = P3*(V4-V3)+(P4*V4-P5*V5)/(Gama-1)\n",
    "WR = W*10**-3/PW;#  work ratio\n",
    "print ' (f) The work ratio is  = ',round(WR,3)\n",
    "\n",
    "# (e)\n",
    "Pm = W/(V1-V2);# mean effective pressure, [kN/m^2]\n",
    "print ' (e) The mean effective pressure is (kN/m^2) = ',round(Pm,1)\n",
    "\n",
    "# (f)\n",
    "CE = (T4-T1)/T4;# carnot efficiency\n",
    "print ' (f) The carnot efficiency is (percent) = ',round(CE*100)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 14: pg 487"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.14\n",
      " (a) The thermal efficiency is (percent) =  52.6\n",
      " (b) The heat received is (kJ/cycle) =  6.22\n",
      " (c) The heat rejected is (kJ/cycle) =  2.95\n",
      " (d) The net work is (kJ/cycle) =  3.27\n",
      " (e) The work ratio is  =   0.347\n",
      " (f) The mean effefctive pressure is (kN/m^2) =  167.32\n",
      " (g) The carnot efficiency is (percent) =  72.7\n",
      "there is minor variation in answer reported in the book due to rounding off error\n"
     ]
    }
   ],
   "source": [
    "#pg 487\n",
    "print('Example 15.14');\n",
    "\n",
    "# aim : To determine\n",
    "# (a)  the thermal efficiency\n",
    "# (b)  the heat received\n",
    "# (c) the heat rejected\n",
    "# (d) the net work \n",
    "# (e)  the work ratio\n",
    "# (f)  the mean effective pressure\n",
    "# (g)  the carnot efficiency\n",
    "\n",
    "\n",
    "# given values\n",
    "P1 = 101.;# initial pressure , [kN/m**2]\n",
    "V1 = 14.*10**-3;# initial volume, [m**3]\n",
    "T1 = 273.+15;# initial temperature, [K]\n",
    "P3 = 1850.;# maximum pressure, [kN/m**2]\n",
    "V2 = 2.8*10**-3;# compressed volume, [m**3]\n",
    "Gama = 1.4;# heat capacity\n",
    "R = .29;# gas constant, [kJ/kg k]\n",
    "\n",
    "# solution\n",
    "# taking reference  Fig. 15.29\n",
    "# (a)\n",
    "# for process 1-2\n",
    "# using PV**(Gama)=constant for process 1-2\n",
    "P2 = P1*(V1/V2)**(Gama);# pressure at stage2,. [MN/m**2]\n",
    "T2 = T1*(V1/V2)**(Gama-1);# temperature at stage 2, [K]\n",
    "\n",
    "# for process 2-3\n",
    "# since volumee is constant in process 2-3 , so using P/T=constant, so\n",
    "T3 = T2*(P3/P2);# volume at stage 3, [K]\n",
    "\n",
    "# for process 3-4\n",
    "P4 = P1;\n",
    "T4 = T3*(P4/P3)**((Gama-1)/Gama);# temperature\n",
    "\n",
    "TE = 1-Gama*(T4-T1)/(T3-T2);# thermal efficiency\n",
    "print ' (a) The thermal efficiency is (percent) = ',round(TE*100,1)\n",
    "\n",
    "# (b)\n",
    "cv = R/(Gama-1);# heat capacity at copnstant volume, [kJ/kg k]\n",
    "m = P1*V1/(R*T1);# mass of gas, [kg]\n",
    "Q1 = m*cv*(T3-T2);# heat received, [kJ/cycle]\n",
    "print ' (b) The heat received is (kJ/cycle) = ',round(Q1,2)\n",
    "\n",
    "# (c)\n",
    "cp = Gama*cv;# heat capacity at constant at constant pressure, [kJ/kg K]\n",
    "Q2 = m*cp*(T4-T1);# heat rejected, [kJ/cycle]\n",
    "print ' (c) The heat rejected is (kJ/cycle) = ',round(Q2,2)\n",
    "\n",
    "# (d)\n",
    "W = Q1-Q2;# net work , [kJ/cycle]\n",
    "print ' (d) The net work is (kJ/cycle) = ',round(W,2)\n",
    "\n",
    "# (e)\n",
    "# pressure is constant for process 1-4, so V/T=constant\n",
    "V4 = V1*(T4/T1);# volume, [m**3]\n",
    "V3 = V2;# for process 2-3\n",
    "P4 = P1;# for process 1-4\n",
    "PW = (P3*V3-P1*V1)/(Gama-1);# positive work done, [kJ/cycle]\n",
    "WR = W/PW;#  work ratio\n",
    "print ' (e) The work ratio is  =  ',round(WR,3)\n",
    "\n",
    "# (f)\n",
    "Pm = W/(V4-V2);# mean effective pressure, [kN/m**2]\n",
    "print ' (f) The mean effefctive pressure is (kN/m^2) = ',round(Pm,2)\n",
    "\n",
    "# (g)\n",
    "CE = (T3-T1)/T3;# carnot efficiency\n",
    "print ' (g) The carnot efficiency is (percent) = ',round(CE*100,1)\n",
    "\n",
    "print 'there is minor variation in answer reported in the book due to rounding off error'\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 15: pg 492"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.15\n",
      " (a) The net work done is (kJ) =  28.0\n",
      " (b) The ideal thermal efficiency is (percent) =  68.9\n",
      " (c) the thermal efficiency if the process of regeneration is not included is (percent) =  39.5\n"
     ]
    }
   ],
   "source": [
    "#pg 492\n",
    "print('Example 15.15');\n",
    "\n",
    "# aim : To determine\n",
    "# (a) the net work done\n",
    "# (b) the ideal thermal efficiency\n",
    "# (c) the thermal efficiency if the process of generation is not included\n",
    "from math import log\n",
    "# given values\n",
    "P1 = 110.;# initial pressure, [kN/m^2)\n",
    "T1 = 273.+30;# initial temperature, [K]\n",
    "V1 = .05;# initial volume, [m^3]\n",
    "V2 = .005;# volume, [m^3]\n",
    "T3 = 273.+700;# temperature, [m^3]\n",
    "R = .289;# gas constant, [kJ/kg K]\n",
    "cv = .718;# heat capacity, [kJ/kg K]\n",
    "\n",
    "# solution\n",
    "# (a)\n",
    "m = P1*V1/(R*T1);# mass , [kg]\n",
    "W = m*R*(T3-T1)*log(V1/V2);# work done, [kJ]\n",
    "print ' (a) The net work done is (kJ) = ',round(W)\n",
    "\n",
    "# (b)\n",
    "n_the = (T3-T1)/T3;# ideal thermal efficiency\n",
    "print ' (b) The ideal thermal efficiency is (percent) = ',round(n_the*100,1)\n",
    "\n",
    "# (c)\n",
    "V4 = V1;\n",
    "V3 = V2;\n",
    "T4 = T3;\n",
    "T2 = T1;\n",
    "\n",
    "Q_rej = m*cv*(T4-T1)+m*R*T1*log(V1/V2);# heat rejected\n",
    "Q_rec = m*cv*(T3-T2)+m*R*T3*log(V4/V3);# heat received\n",
    "\n",
    "n_th = (1-Q_rej/Q_rec);# thermal efficiency\n",
    "print ' (c) the thermal efficiency if the process of regeneration is not included is (percent) = ',round(n_th*100,1)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 16: pg 493"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.16\n",
      " (a) The maximum temperature is (C) =  313.0\n",
      " (b) The net work done is (kJ) =  12.88\n",
      " (c) The ideal thermal efficiency is (percent) =  50.0\n",
      " (d) the thermal efficiency if the process of regeneration is not included is (percent) =  24.0\n"
     ]
    }
   ],
   "source": [
    "#pg 493\n",
    "print('Example 15.16');\n",
    "from math import log\n",
    "# aim : To determine\n",
    "# (a) the maximum temperature\n",
    "# (b) the net work done\n",
    "# (c) the ideal thermal efficiency\n",
    "# (d) the thermal efficiency if the process of regeneration is not included\n",
    "\n",
    "# given values\n",
    "P1 = 100.;# initial pressure, [kN/m^2)\n",
    "T1 = 273.+20;# initial temperature, [K]\n",
    "V1 = .08;# initial volume, [m^3]\n",
    "rv = 5;# volume ratio\n",
    "R = .287;# gas constant, [kJ/kg K]\n",
    "cp = 1.006;# heat capacity, [kJ/kg K]\n",
    "V3_by_V2 = 2;\n",
    "\n",
    "# solution\n",
    "# (a)\n",
    "# using Fig.15.33\n",
    "# process 1-2 is isothermal\n",
    "T2 = T1;\n",
    "# since process 2-3 isisobaric, so V/T=constant\n",
    "T3 = T2*(V3_by_V2);# maximumtemperature, [K]\n",
    "print ' (a) The maximum temperature is (C) = ',T3-273\n",
    "\n",
    "# (b)\n",
    "m = P1*V1/(R*T1);# mass , [kg]\n",
    "W = m*R*(T3-T1)*log(rv);# work done, [kJ]\n",
    "print ' (b) The net work done is (kJ) = ',round(W,2)\n",
    "\n",
    "# (c)\n",
    "TE = (T3-T1)/T3;# ideal thermal efficiency\n",
    "print ' (c) The ideal thermal efficiency is (percent) = ',TE*100\n",
    "\n",
    "# (d)\n",
    "T4 = T3;\n",
    "T2 = T1;\n",
    "\n",
    "Q_rej = m*cp*(T4-T1)+m*R*T1*log(rv);# heat rejected\n",
    "Q_rec = m*cp*(T3-T2)+m*R*T3*log(rv);# heat received\n",
    "\n",
    "n_th = (1-Q_rej/Q_rec);# thermal efficiency\n",
    "print ' (d) the thermal efficiency if the process of regeneration is not included is (percent) = ',round(n_th*100)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 17: pg 495"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.17\n",
      " (a) The net work done is (kJ) =  44.7\n",
      " (b) The thermal efficiency is (percent) =  56.7\n"
     ]
    }
   ],
   "source": [
    "#pg 495\n",
    "print('Example 15.17');\n",
    "\n",
    "# aim : To determine \n",
    "# (a) the net work done\n",
    "# (b) thethermal efficiency\n",
    "from math import log\n",
    "# given values\n",
    "m = 1.;# mass of air, [kg]\n",
    "T1 = 273.+230;# initial temperature, [K]\n",
    "P1 = 3450.;# initial pressure, [kN/m^2]\n",
    "P2 = 2000.;# pressure, [kN/m^2]\n",
    "P3 = 140.;# pressure, [kN/m^2]\n",
    "P4 = P3;\n",
    "Gama = 1.4; # heat capacity ratio\n",
    "cp = 1.006;# heat capacity, [kJ/kg k]\n",
    "\n",
    "# solution\n",
    "T2 =T1;# isothermal process 1-2\n",
    "# process 2-3 and 1-4 are adiabatic so\n",
    "T3 = T2*(P3/P2)**((Gama-1)/Gama);# temperature, [K] \n",
    "T4 = T1*(P4/P1)**((Gama-1)/Gama);# [K]\n",
    "R = cp*(Gama-1)/Gama;# gas constant, [kJ/kg K]\n",
    "Q1 = m*R*T1*log(P1/P2);# heat received, [kJ]\n",
    "Q2 = m*cp*(T3-T4);# heat rejected\n",
    "\n",
    "#hence\n",
    "W = Q1-Q2;# work done\n",
    "print ' (a) The net work done is (kJ) = ',round(W,1)\n",
    "\n",
    "# (b)\n",
    "TE = 1-Q2/Q1;# thermal efficiency\n",
    "print ' (b) The thermal efficiency is (percent) = ',round(TE*100,1)\n",
    "\n",
    "#  End\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 18: pg 497"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 15.18\n",
      " The thermal efficiency is (percent) =  31.0\n",
      " The carnot efficiency is  =  73.7\n"
     ]
    }
   ],
   "source": [
    "#pg 497\n",
    "print('Example 15.18');\n",
    "\n",
    "# aim : To determine \n",
    "# thermal eficiency\n",
    "# carnot efficiency\n",
    "from math import log\n",
    "# given values\n",
    "rv = 5.;# volume ratio\n",
    "Gama = 1.4;# heat capacity ratio\n",
    "\n",
    "# solution\n",
    "# under given condition\n",
    "\n",
    "TE = 1-(1/Gama*(2-1/rv**(Gama-1)))/(1+2*((Gama-1)/Gama)*log(rv/2));# thermal efficiency\n",
    "print ' The thermal efficiency is (percent) = ',round(TE*100)\n",
    "\n",
    "CE = 1-1/(2*rv**(Gama-1));# carnot efficiency\n",
    "print ' The carnot efficiency is  = ',round(CE*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
}