{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 6 : Second Law of Thermodynamics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.1 Page No : 138"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temperature of the source in °C is :  37.0\n",
      "Temperature of the math.sink in °C is :  99.0\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "# In first case     (T1-T2)/T1=1/6           or    T1= 1.2*T2                  (i)\n",
    "# In seond case  (T1-(T2-62))/T1= 2/6  or   2*T1 -3*(T2-62)=0      (ii)\n",
    "# From eq (i) and (ii)\n",
    "T2= 186/0.6;\t\t\t# in K\n",
    "\n",
    "# Calculations\n",
    "T1= 1.2*T2;\t\t\t# in K\n",
    "\n",
    "# Results\n",
    "print \"Temperature of the source in °C is : \",T2-273\n",
    "print \"Temperature of the math.sink in °C is : \",T1-273\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.2 Page No : 138"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Power rquired for operating the pump in kW is : 1.074\n",
      "The value of T1 in °C is : 49.00\n"
     ]
    }
   ],
   "source": [
    "import math \n",
    "\n",
    "# Variables\n",
    "T1 = 25.;\t\t\t# in °C\n",
    "T2 = 1.; \t\t\t# in °C\n",
    "T1 = T1 + 273.;\t\t\t# in K\n",
    "T2 = T2 + 273.;\t\t\t# in K\n",
    "HT= 2.;\t    \t\t# heat transfer across the wall and the roof in MJ/hr\n",
    "\n",
    "# Calculations and Results\n",
    "HT= HT*10**6;   \t\t\t# in J/hr\n",
    "Q = HT* (T1-T2);\t\t\t# in J/hr\n",
    "COP_heat = T1/(T1-T2);\n",
    "W_net = Q/COP_heat;\t\t\t# in J/hr\n",
    "print \"Power rquired for operating the pump in kW is : %.3f\"%(W_net*10.**-3./3600)\n",
    "\n",
    "# Part (b)\n",
    "T2= 25.;\t\t\t# in °C\n",
    "T2=T2+273;\t\t\t# in K\n",
    "# COP= T2/(T1-T2)          (i)\n",
    "# COP= HT*(T1-T2)/W_net        (ii)\n",
    "# From (i) and (ii)\n",
    "T1= math.sqrt(W_net*T2/HT)+T2;\t\t\t# in K\n",
    "T1= T1-273;\t\t\t# in °C\n",
    "print \"The value of T1 in °C is : %.2f\"%T1\n",
    "\n",
    "# rounding off error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.3 Page No : 142"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ratio of heat trasferred to the circulating water to heat trasferred to the engine is :  2.28\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "# Variables\n",
    "heatEngineEffi= 32./100;\t\t\t# heat engine efficiency\n",
    "COP= 5.                 \t\t\t# COP of heat pump\n",
    "\n",
    "# Calculations\n",
    "# heat engine efficiency = Wnet/Q1 = (Q1-Q2)/Q1\n",
    "Q1byWnet= 1/heatEngineEffi;\n",
    "Q2byWnet= (1-heatEngineEffi)*Q1byWnet;\n",
    "# COP = Q4/Wnet = Q4/(Q4-Q3)\n",
    "Q4byWnet= COP;\n",
    "ratio= (Q2byWnet+Q4byWnet)/Q1byWnet;\t\t\t# ratio of heat transferred to the circulating water to heat trasferred to the engine\n",
    "\n",
    "# Results\n",
    "print \"Ratio of heat trasferred to the circulating water to heat trasferred to the engine is : \",ratio\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.4 Page No : 147"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of Carnot Power in kWh is : 10.823\n",
      "As the actual power produced by the invented engine is more than the Carnot Power, \n",
      "so inverter claim is  not true\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "Q = 88;\t\t\t# in MJ\n",
    "Q=Q*10**3;\t\t\t# in kJ\n",
    "T1 = 190.;\t\t\t# in °C\n",
    "\n",
    "# Calculations\n",
    "T1 = T1 + 273;\t\t\t# in K\n",
    "T3 = -15;\t\t\t# in °C\n",
    "T3 = T3 + 273;\t\t\t# in K\n",
    "Eta_carnot = (T1 - T3)/T1;\n",
    "Wnet= Eta_carnot * Q;\t\t\t# in kJ\n",
    "CarnotPower= Wnet/3600.;\t\t\t# in kWh\n",
    "\n",
    "# Results\n",
    "print \"The value of Carnot Power in kWh is : %.3f\"%CarnotPower\n",
    "print (\"As the actual power produced by the invented engine is more than the Carnot Power, \");\n",
    "print (\"so inverter claim is  not true\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.5 Page No : 148"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The theoretical minimum power required to drive the heat pump in kW is : 2.058\n",
      "The theoretical power required to drive the heat pump when it is used as a refrigerator in kW is : 2.22\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "T1 = 24.;\t\t    \t# in °C\n",
    "T1 = T1 + 273;\t\t\t# in K\n",
    "T2 = 2;\t\t        \t# in °C\n",
    "T2 = T2 + 273;\t\t\t# in K\n",
    "Q = 100;    \t\t\t#in MJ/h\n",
    "Q = Q * 10**3;\t\t\t#in kJ/h\n",
    "\n",
    "# Calculations and Results\n",
    "COP_heatPump = T1/(T1-T2);\n",
    "W = Q/COP_heatPump;\t\t\t#in kJ/h\n",
    "W = W/3600.;\t\t\t        # in kW\n",
    "print \"The theoretical minimum power required to drive the heat pump in kW is : %.3f\"%W\n",
    "\n",
    "COP_refrigerator = T2/(T1-T2);\n",
    "W = Q/COP_refrigerator;\t\t\t# in kJ/h\n",
    "W = W/3600.;         \t\t\t# in kW\n",
    "print \"The theoretical power required to drive the heat pump when it is used as a refrigerator in kW is : %.2f\"%W\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.6 Page No : 150"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Part (a)\n",
      "The cycle is irreversible\n",
      "Part (b)\n",
      "Reversible or irreversible cycle is not possible and the result is impossible\n",
      "Part (c)\n",
      "The cycle is reversible\n"
     ]
    }
   ],
   "source": [
    "# Variables\n",
    "Q1= 278.;\t\t\t# in kJ/s\n",
    "T1= 283.+273;\t\t\t# in K\n",
    "T2= 50.+273;\t\t\t# in K\n",
    "print (\"Part (a)\")\n",
    "Q2= 208.;\t\t\t# in kJ/s\n",
    "\n",
    "# Calculations and Results\n",
    "# By Clausius inequality \n",
    "V= Q1/T1-Q2/T2;\n",
    "if V<0:\n",
    "    print (\"The cycle is irreversible\")\n",
    "elif  V>0:\n",
    "    print (\"Reversible or irreversible cycle is not possible and the result is impossible\")\n",
    "else:\n",
    "    print (\"The cycle is reversible\")\n",
    "\n",
    "print (\"Part (b)\")\n",
    "Q2= 139.;\t\t\t# in kJ/s\n",
    "V= Q1/T1-Q2/T2;\n",
    "if V<0:\n",
    "    print (\"The cycle is irreversible\")\n",
    "elif V>0:\n",
    "    print (\"Reversible or irreversible cycle is not possible and the result is impossible\")\n",
    "else:\n",
    "    print (\"The cycle is reversible\")\n",
    "\n",
    "print (\"Part (c)\")\n",
    "Q2= 161.5;\t\t\t# in kJ/s\n",
    "V= Q1/T1-Q2/T2;\n",
    "if V<0:\n",
    "    print (\"The cycle is irreversible\")\n",
    "elif V>0:\n",
    "    print (\"Reversible or irreversible cycle is not possible and the result is impossible\")\n",
    "else:\n",
    "    print (\"The cycle is reversible\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.16 Page No : 155"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The heat rejected in MJ/h is :  60.8\n",
      "Irreversibility in kJ/h is : 8.114\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "Wnet_compresser= 3.;\t                    \t\t# in kW\n",
    "Wnet_compresser=Wnet_compresser*3600.;\t\t\t# in kJ/h\n",
    "Qabsorbed= 50.;\t    \t\t                    # in MJ/h\n",
    "Qabsorbed=Qabsorbed*10**3;\t\t\t            # in kJ/h\n",
    "\n",
    "# Calculations and Results\n",
    "T1 = 46+273;\t\t\t# in K\n",
    "T2 = 1+273; \t\t\t# in K\n",
    "Qrejected= Wnet_compresser+Qabsorbed;\t\t\t# in kJ/h\n",
    "print \"The heat rejected in MJ/h is : \",Qrejected*10**-3\n",
    "\n",
    "I= -(-Qrejected/T1+Qabsorbed/T2);\t\t\t# in kJ/h\n",
    "print \"Irreversibility in kJ/h is : %.3f\"%I\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.17 Page No : 156"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken by the heater to raise the temperature in sec is : 3.34\n",
      "Entrophy generated during the process in kJ/K is : 20.72\n"
     ]
    }
   ],
   "source": [
    "#Exa 6.17\n",
    "import math \n",
    "\n",
    "\n",
    "# Variables\n",
    "T1 = 12.;    \t\t\t# in °C\n",
    "T2 = 92.;\t    \t\t# in °C\n",
    "T1 = T1 + 273.;\t\t\t# in K\n",
    "T2 = T2 + 273.;\t\t\t# in K\n",
    "del_T = T2 - T1;\t\t\t# in K\n",
    "m = 20;\t\t\t        # in kg\n",
    "C_v = 4.187;\n",
    "s= 1;\n",
    "\n",
    "# Calculations and Results\n",
    "Q = m * s * del_T;\t\t\t# in cal\n",
    "Q = Q * 4.18;\t    \t\t# in J\n",
    "H = 2;\t        \t    \t# heat given by the heater in kw\n",
    "H = H * 10**3;\t\t    \t# in J/sec\n",
    "t = Q/H;\t\t\t        #time taken by the heater to raise the temp. in sec\n",
    "print \"Time taken by the heater to raise the temperature in sec is : %.2f\"%t\n",
    "\n",
    "del_phi = m * C_v * math.log(T2/T1);\t\t\t# in kJ/K\n",
    "print \"Entrophy generated during the process in kJ/K is : %.2f\"%del_phi\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.18 Page No : 156"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Carnot efficiency in % is : 50.18\n",
      "Efficiency of the heat engine in % is :  50.8\n",
      "As the efficiency of heat engine cannot be more than Carnot efficiency, Hence    engine cannot execute irreversible cycle.\n"
     ]
    }
   ],
   "source": [
    "# Exa 6.18\n",
    "import math \n",
    "\n",
    "# Variables\n",
    "Q1 = 1000.;\t\t\t# in kW\n",
    "Q2 = 492.;\t\t\t# in kW\n",
    "T1 = 285.;\t\t\t# in °C\n",
    "T1 = T1 + 273;\t\t\t# in K\n",
    "T2 = 5.;\t\t\t    # in °C\n",
    "T2 = T2 + 273;\t\t\t# in K\n",
    "\n",
    "# Calculations and Results\n",
    "Eta_carnot = (T1-T2)/T1*100;\t\t\t# in percentage\n",
    "print \"Carnot efficiency in %% is : %.2f\"%Eta_carnot\n",
    "\n",
    "Eta_heat = (Q1 - Q2)/Q1*100;\t\t\t# in percentage\n",
    "print \"Efficiency of the heat engine in % is : \",Eta_heat\n",
    "if Eta_heat>Eta_carnot:\n",
    "    print (\"As the efficiency of heat engine cannot be more than Carnot efficiency, Hence\\\n",
    "    engine cannot execute irreversible cycle.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.19 Page No : 156"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The output of the engine in watt is 36.0\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "n = 1080.;\t\t\t# in cycle/min\n",
    "Q_s = 57.;\t\t\t# in J/cycle\n",
    "T1 = 12.;\t\t\t# in °C\n",
    "T1 = T1 + 273;\t\t\t# in K\n",
    "T2 = 2.;\t\t\t# in °C\n",
    "T2 = T2 + 273;\t\t\t# in K\n",
    "\n",
    "# Calculations\n",
    "# 1-(Q_r/Q_s) = 1- (T2/T1)\n",
    "Q_r = (T2/T1)*Q_s;\t\t\t# in J/cycle\n",
    "W = Q_s - Q_r;\t\t\t# in J/cycle\n",
    "P_o = W * n;\t\t\t# in J/min\n",
    "P_o = P_o/60;\t\t\t# in W\n",
    "\n",
    "# Results\n",
    "print \"The output of the engine in watt is\",P_o\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.20 Page No : 157"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Minimum power required in kW is : 2.354\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "Q2 = 10.**5;\t\t\t# in kJ/hr\n",
    "T1 = -3.;\t\t\t# in °C\n",
    "T1 = T1 + 273;\t\t\t# in K\n",
    "T2 = 22.;\t\t\t# in °C\n",
    "T2 = T2 + 273;\t\t\t# in K\n",
    "\n",
    "# Calculations\n",
    "COP_heat = 1./(1-T1/T2);\n",
    "W = Q2/COP_heat;\t\t\t# in kJ/hr\n",
    "W = W/3600.;    \t\t\t# in kW\n",
    "\n",
    "# Results\n",
    "print \"Minimum power required in kW is : %.3f\"%W\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.21 Page No : 158"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "engine efficiency in % is : 66.7\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "T_A= 927.+273;\t\t\t# in K\n",
    "T_B= 127.+273;\t\t\t# in K\n",
    "T_C= T_B;\t\t\t# in K\n",
    "\n",
    "# Calculations\n",
    "# Q_A= Q_B+Q_C+W = 2*Q_B+W (math.since Q_B=Q_C)     (i)\n",
    "# Q_A/T_A= Q_B/T_B+Q_C/T_C or\n",
    "# Q_A= 2*Q_B*T_A/T_B           (ii)\n",
    "# From eq (i) and (ii)W= 2*Q_B*(T_A/T_B-1)     (iii)\n",
    "# Dividing (iii) by (ii)\n",
    "WbyQ_A= (T_A/T_B-1)/(T_A/T_B);\n",
    "\n",
    "# Results\n",
    "print \"engine efficiency in %% is : %.1f\"%(WbyQ_A*100)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.22 Page No : 158"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The heat rejected at B in kJ is : 142.86\n",
      "The heat rejected at C in kJ is : 857.14\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "T_A= 700.;\t\t\t# in K\n",
    "T_B= 600.;\t\t\t# in K\n",
    "T_C= 500.;\t\t\t# in K\n",
    "Q_A= 1200.;\t\t\t# in kJ\n",
    "\n",
    "# Calculations\n",
    "# Q_B+Q_C= Q_A-200                             (i)\n",
    "# Q_A/T_A = Q_B/T_B+Q_C/T_C    (ii)\n",
    "# From eq(i) and (ii)\n",
    "Q_B= (Q_A*(1/T_B-1/T_A)-200/T_B)/(1/T_B-1/T_C);\t\t\t# in kJ\n",
    "Q_C= Q_A-Q_B-200;\t\t\t# in kJ\n",
    "\n",
    "# Results\n",
    "print \"The heat rejected at B in kJ is : %.2f\"%Q_B\n",
    "print \"The heat rejected at C in kJ is : %.2f\"%Q_C\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.23 Page No : 159"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "intermediate temperature in °C is :  100\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "T1= 180+273;\t\t\t# in K\n",
    "T2= 20+273;\t\t\t# in K\n",
    "\n",
    "# Calculations\n",
    "# W_A/Q1= 1-T3/T1      (i)\n",
    "# W_B/QB= 1-T2/T3      (ii)\n",
    "# W_A= W_B                 (iii)\n",
    "# Q1= Q_B+W_A          (iv)\n",
    "# From eq(i),(ii),(iii) and (iv)\n",
    "T3= (T1+T2)/2;\t\t\t# in K\n",
    "\n",
    "# Results\n",
    "print  \"intermediate temperature in °C is : \",T3-273\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6.24 Page No : 160"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Least power required in kW is : 0.305\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "Q2 = 1.75;\t\t\t# in kJ/sec\n",
    "T1 = -15;\t\t\t# in °C\n",
    "T1 = T1 + 273;\t\t\t# in K\n",
    "T2 = 30;\t\t\t# in °C\n",
    "T2 = T2 + 273;\t\t\t# in K\n",
    "\n",
    "# Calculations\n",
    "del_T = T2 - T1;\t\t\t# in K\n",
    "# Q2/W_net = T2/(del_T)\n",
    "W_net = Q2 * del_T/T1;\t\t\t# in kW\n",
    "\n",
    "# Results\n",
    "print \"Least power required in kW is : %.3f\"%W_net\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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
