{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 5 - Second law of themodynamics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.1 Page: 150"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.1 - Page: 150\n",
      "\n",
      "\n",
      "The theoretical efficiency of heat engine is 63.5 %\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "print \"Example: 5.1 - Page: 150\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "Th = 550 + 273## [K]\n",
    "Tl = 27 + 273## [K]\n",
    "#************#\n",
    "\n",
    "# The theoretical efficiency of a heat engine is given by:\n",
    "# eta = Net Work Output/Net Work Input\n",
    "# eta = Wnet/Qin\n",
    "# eta = (Qin - Qout)/Qin = (Th - Tl)/Th\n",
    "eta = (Th - Tl)/Th#\n",
    "print \"The theoretical efficiency of heat engine is %.1f %%\"%(eta * 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.2 Page: 150"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.2 - Page: 150\n",
      "\n",
      "\n",
      "(a) The efficiency of the heat engine is 63.0 %\n",
      "\n",
      "(b) The efficiency of the heat engine is 78.0 %\n",
      "\n",
      "(c) The efficiency of the heat engine is 57.5 %\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.2 - Page: 150\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "Th = 810## [K]\n",
    "Tl = 300## [K]\n",
    "#*************#\n",
    "\n",
    "# Solution (a)\n",
    "eta = (Th - Tl)/Th#\n",
    "print \"(a) The efficiency of the heat engine is %.1f %%\\n\"%(eta*100)#\n",
    "\n",
    "# Solution (b)\n",
    "Th = 1366## [K]\n",
    "Tl = 300## [K]\n",
    "eta = (Th - Tl)/Th#\n",
    "print \"(b) The efficiency of the heat engine is %.1f %%\\n\"%(eta*100)#\n",
    "\n",
    "# Solution (c)\n",
    "Th = 810## [K]\n",
    "Tl = 344## [K]\n",
    "eta = (Th - Tl)/Th#\n",
    "print \"(c) The efficiency of the heat engine is %.1f %%\\n\"%(eta*100)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.3 Page: 151"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.3 - Page: 151\n",
      "\n",
      "\n",
      "(a) The efficiency of the Carnot engine is 67.2 %\n",
      "\n",
      "(b) Heat released to cold reservoir is 192 kJ\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.3 - Page: 151\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "Th = 650 + 273## [K]\n",
    "Tl = 30 + 273## [K]\n",
    "Qh = 585## [kJ/cycle]\n",
    "#*************#\n",
    "\n",
    "# Solution (a)\n",
    "# From Eqn. (5.9)\n",
    "eta = (Th - Tl)/Th#\n",
    "print \"(a) The efficiency of the Carnot engine is %.1f %%\\n\"%(eta*100)#\n",
    "\n",
    "# Soluton (b)\n",
    "# From the knowledge of the ratio of heat and temperature between the two regions:\n",
    "Ql = Qh*Tl/Th## [kJ]\n",
    "print \"(b) Heat released to cold reservoir is %d kJ\\n\"%(Ql)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.4 Page: 151"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.4 - Page: 151\n",
      "\n",
      "\n",
      "Minimum Work requirement is 27 kJ\n",
      "\n",
      "Amount of heat released to the surrounding is 362 kJ\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.4 - Page: 151\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "m = 1## [kg]\n",
    "Tl = 273## [K]\n",
    "Th = 295## [K]\n",
    "Ql = 335## [kJ/kg]\n",
    "#*************#\n",
    "\n",
    "# Solution (a)\n",
    "# The coeffecient of performance of refrigerating machine is:\n",
    "# COP = Ql/Wnet = Tl/(Th - Tl)\n",
    "Wnet = Ql*(Th - Tl)/Tl## [kJ]\n",
    "print \"Minimum Work requirement is %d kJ\\n\"%(round(Wnet))#\n",
    "\n",
    "# Solution (b)\n",
    "# Amount of heat released:\n",
    "# Wnet = Qh - Ql\n",
    "Qh = Wnet + Ql## [kJ]\n",
    "print \"Amount of heat released to the surrounding is %d kJ\\n\"%(round(Qh))#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.5 Page: 152"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.5 - Page: 152\n",
      "\n",
      "\n",
      "Minimum Work Required is 13.1 kJ\n",
      "\n",
      "The efficiency of Heat Engine is 0.263\n",
      "\n",
      "Amount of Heat released is 36.9 kJ\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.5 - Page: 152\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "Th = 373## [K]\n",
    "Tl = 275## [K]\n",
    "Qh = 50## [kJ]\n",
    "#*************#\n",
    "\n",
    "# Solution (a)\n",
    "# Theral Efficiency of the engine can be given as:\n",
    "# eta_HE = Wnet/Qh#\n",
    "# Wnet = Qh*COP = Qh*(Th - Tl)/Th#\n",
    "Wnet = Qh*(Th - Tl)/Th## [kJ]\n",
    "print \"Minimum Work Required is %.1f kJ\\n\"%(Wnet)#\n",
    "\n",
    "# Solution (b)\n",
    "eta = (Th - Tl)/Th#\n",
    "print \"The efficiency of Heat Engine is %.3f\\n\"%(eta)#\n",
    "\n",
    "# Solution (c)\n",
    "# Amount of heat released can be calculated as:\n",
    "# eta = Net Work Output/Net Work Input#\n",
    "# eta = Wnet/Qin#\n",
    "# eta = (Qin - Qout)/Qin#\n",
    "Qin = Qh## [kJ]\n",
    "Qout = Qin*(1 - eta)#\n",
    "print \"Amount of Heat released is %.1f kJ\\n\"%(Qout)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.6 Page: 153"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.6 - Page: 153\n",
      "\n",
      "\n",
      "Claim is not Valid\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.6 - Page: 153\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "W = 5## [hp]\n",
    "Q = 7000## [J/s]\n",
    "Th = 400 + 273## [K]\n",
    "Tl = 24 + 273## [K]\n",
    "#*************#\n",
    "\n",
    "W = 5*745.7## [W]\n",
    "thermal_eta = W/Q#\n",
    "theoretical_eta = (Th - Tl)/Th#\n",
    "\n",
    "if theoretical_eta <= thermal_eta:\n",
    "    print \"Claim is Valid\"\n",
    "else:\n",
    "    print \"Claim is not Valid\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.7 Page: 162"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.7 - Page: 162\n",
      "\n",
      "\n",
      "Change in Entropy is 23.81 J/mol K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.7 - Page: 162\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "n = 1## [mol]\n",
    "deltaH_fusion = 6500## [J/mol]\n",
    "T_Tr = 273## [transition temperature, K]\n",
    "P = 1## [atm]\n",
    "#************#\n",
    "\n",
    "# By Eqn. (9.40)\n",
    "deltaS_fusion = deltaH_fusion/T_Tr## [J/mol K]\n",
    "print \"Change in Entropy is %.2f J/mol K\"%(deltaS_fusion)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.8 Page: 164"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.8 - Page: 164\n",
      "\n",
      "\n",
      "Change in Entropy is 22.876 eu\n"
     ]
    }
   ],
   "source": [
    "from math import log\n",
    "print \"Example: 5.8 - Page: 164\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "V1 = 5## [L]\n",
    "V2 = 50## [L]\n",
    "n = 5## [moles]\n",
    "R = 1.987## [cal/mol K]\n",
    "#**************#\n",
    "\n",
    "# Change in entropy for an isothermal change for an ideal gas:\n",
    "# deltaS = n*R*log(P1/P2) = n*R*log(V2/V1)\n",
    "deltaS = n*R*log(V2/V1)## [cal/degree]\n",
    "print \"Change in Entropy is %.3f eu\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.9 Page: 164"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.9 - Page: 164\n",
      "\n",
      "\n",
      "deltaS is 115.26 J/K\n",
      "Change in Entropy is 115.257 eu\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.9 - Page: 164\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "n = 8## [mol]\n",
    "R = 8.314## [J/mol K]\n",
    "T2 = 700## [K]\n",
    "T1 = 350## [K]\n",
    "Cp = (5/2)*R## [J/mol K]\n",
    "#*************#\n",
    "\n",
    "deltaS = n*Cp*log(T2/T1)## [J/K]\n",
    "print \"deltaS is %.2f J/K\"%(deltaS)#\n",
    "\n",
    "R*log(V2/V1)#// [cal/degree]\n",
    "print \"Change in Entropy is %.3f eu\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.10 Page: 164"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.10 - Page: 164\n",
      "\n",
      "\n",
      "Change in Entropy is -19.179548 kJ/K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.10 - Page: 164\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "n = 5## [moles]\n",
    "T1 = 300## [K]\n",
    "T2 = 400## [K]\n",
    "P1 = 3## [bars]\n",
    "P2 = 12## [bars]\n",
    "Cp = 26.73## [J/mol K]\n",
    "R = 8.314## [K/mol K]\n",
    "#*************#\n",
    "\n",
    "deltaS = n*((Cp*log(T2/T1)) + (R*log(P1/P2)))## [kJ/K]\n",
    "print \"Change in Entropy is %f kJ/K\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.11 Page: 166"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.11 - Page: 166\n",
      "\n",
      "\n",
      "Entropy Change is 4.27 kJ/kmol K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.11 - Page: 166\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "N = 1## [kmol]\n",
    "xA = 0.21## [for Oxygen]\n",
    "xB = 0.79## [for Nitrogen]\n",
    "R = 8.314## [kJ/kmol K]\n",
    "#*************#\n",
    "\n",
    "deltaS = - (N*R*(xA*log(xA) + xB*log(xB)))## [kJ/mol K]\n",
    "print \"Entropy Change is %.2f kJ/kmol K\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.12 Page: 167"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.12 - Page: 167\n",
      "\n",
      "\n",
      "Change in Entropy is 1.117 cal/K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.12 - Page: 167\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "Vol_O2 = 5.6## [L]\n",
    "Vol_H2 = 16.8## [L]\n",
    "R = 1.987## [cal/mol K]\n",
    "#*************#\n",
    "\n",
    "xA = Vol_O2/22.4## [mole fraction O2]\n",
    "xB = Vol_H2/22.4## [mle fraaction H2]\n",
    "N = xA + xB## [total number of moles]\n",
    "# From Eqn. 5.21:\n",
    "deltaS = - (N*R*(xA*log(xA) + xB*log(xB)))## [cal/K]\n",
    "print \"Change in Entropy is %.3f cal/K\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.13 Page: 168"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.13 - Page: 168\n",
      "\n",
      "\n",
      "Entropy Change is 0.319 kJ/K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.13 - Page: 168\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "m = 80## [mass of Argon, g]\n",
    "T1 = 300## [K]\n",
    "T2 = 500## [K]\n",
    "Cv = 0.3122## [kJ/kg K]\n",
    "#**************#\n",
    "\n",
    "Mw = 40## [Molecular Weight of Argon]\n",
    "n = m/Mw## [moles]\n",
    "deltaS = n*Cv*log(T2/T1)## [kJ/K]\n",
    "print \"Entropy Change is %.3f kJ/K\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.14 Page: 168"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.14 - Page: 168\n",
      "\n",
      "\n",
      "The upper temperature of the process is 864.929 K\n"
     ]
    }
   ],
   "source": [
    "from math import exp\n",
    "print \"Example: 5.14 - Page: 168\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "deltaS = 1## [kJ/kg K]\n",
    "Cv = 0.918## [kJ/kg K]\n",
    "T1 = 273 + 18## [K]\n",
    "#*************#\n",
    "\n",
    "# Let the upper temperature be T.\n",
    "# deltaS = integrate('Cv/T','T',T1,T)# \n",
    "# deltaS = Cv*log(T/T1)\n",
    "T = T1*exp(deltaS/Cv)## [K]\n",
    "print \"The upper temperature of the process is %.3f K\"%(T)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.15 Page: 169"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.15 - Page: 169\n",
      "\n",
      "\n",
      "Change in Entropy is -5.450 kJ/K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.15 - Page: 169\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "m1 = 5## [kg]\n",
    "m2 = 20## [kg]\n",
    "C = 4.2## [kJ/kg K]\n",
    "T1 = 350## [K]\n",
    "T2 = 250## [K]\n",
    "#**************#\n",
    "\n",
    "# Suppose the final temperature is T\n",
    "#deff('[y] = f(T)','y = m1*C*(T1 - T) - m2*C*(T - T2)')#\n",
    "def f(T):\n",
    "    y = m1*C*(T1 - T) - m2*C*(T - T2)\n",
    "    return y\n",
    "from scipy.optimize import fsolve\n",
    "T = fsolve(f, 7)[0]## [K]\n",
    "# Change in entropy of Hot Water:\n",
    "from sympy.mpmath import quad\n",
    "deltaS1 = m1*C*quad(lambda T:(1/T),[T1,T])## [kJ/K]\n",
    "# Change in Entopy of Hot Water:\n",
    "deltaS2 = m2*C*quad('(1/T)','T',T2,T)## [kJ/K]\n",
    "deltaS = deltaS1 + deltaS2## [kJ/K]\n",
    "print \"Change in Entropy is %.3f kJ/K\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.16 Page: 169"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.16 - Page: 169\n",
      "\n",
      "\n",
      "Change in Entropy is 0.540 kJ/K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.16 - Page: 169\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "m = 12## [g]\n",
    "T1 = 294## [K]\n",
    "T2 = 574## [K]\n",
    "T = 505## [melting point, K]\n",
    "H_fusion = 4.5## [cal/K]\n",
    "C_solid = 0.052## [cal/g K]\n",
    "C_liquid = 0.062## [cal/g K]\n",
    "#*************#\n",
    "\n",
    "# Entropy Change in heating 12 g of metal from T1 to T\n",
    "deltaS1 = m*C_solid*quad(lambda T:(1/T),[T1,T])## [kJ/K]\n",
    "# Entropy Change in fusion of metal:\n",
    "deltaS2 = m*H_fusion/T## [kJ/K]\n",
    "# Entropy Change in heating liquid metal from 505 K to 574 K\n",
    "deltaS3 =  m*C_liquid*quad(lambda T:(1/T),[T,T2])## [kJ/K]\n",
    "deltaS = deltaS1 + deltaS2 + deltaS3## [kJ/K]\n",
    "print \"Change in Entropy is %.3f kJ/K\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.17 Page: 170"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.17 - Page: 170\n",
      "\n",
      "\n",
      "Change in Entropy is 9.165 cal/K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.17 - Page: 170\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "#deff('[y] = Cp(T)','y = 7.25 + 2.28*10**(-3)*T')#\n",
    "def Cp(T):\n",
    "    y = (7.25 + 2.28*10**(-3)*T)/T\n",
    "    return y\n",
    "T1 = 273 + 137## [K]\n",
    "T2 = 273 + 877## [K]\n",
    "#************#\n",
    "\n",
    "deltaS = quad(Cp,[T1,T2])## [cal/K]\n",
    "print \"Change in Entropy is %.3f cal/K\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.18 Page: 170"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.18 - Page: 170\n",
      "\n",
      "\n",
      "Total Entropy Change is 7.68 kJ/K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.18 - Page: 170\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "m_iron = 40## [kg]\n",
    "T1 = 625## [K]\n",
    "m_water = 160## [kg]\n",
    "T2 = 276## [K]\n",
    "C_iron = 0.45## [kJ/kg K]\n",
    "C_water = 4.185## [kJ/kg K]\n",
    "#**************#\n",
    "\n",
    "#deff('[y] = f(T)','y = m_iron*C_iron*(T1 - T) - m_water*C_water*(T - T2)')#\n",
    "def f(T):\n",
    "    y = m_iron*C_iron*(T1 - T) - m_water*C_water*(T - T2)\n",
    "    return y\n",
    "from scipy.optimize import fsolve\n",
    "T = fsolve(f, 7)## [K]\n",
    "# Change in Entropy of the iron casting can be estimated as:\n",
    "deltaS1 = m_iron*C_iron*log(T/T1)## [kJ/K]\n",
    "# Change in Entropy of Water is given by:\n",
    "deltaS2 = m_water*C_water*log(T/T2)## [kJ/K]\n",
    "deltaS = deltaS1 + deltaS2## [kJ/K]\n",
    "print \"Total Entropy Change is %.2f kJ/K\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.19 Page: 172"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.19 - Page: 172\n",
      "\n",
      "\n",
      "Entropy at 500 K is 160.73 J/kmol\n"
     ]
    }
   ],
   "source": [
    "from math import log\n",
    "print \"Example: 5.19 - Page: 172\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "Cp = 21## [J/kmol]\n",
    "T1 = 300## [K]\n",
    "T2 = 500## [K]\n",
    "S1 = 150## [Entropy at T1, J/kmol]\n",
    "#*************#\n",
    "\n",
    "# This is a constant Entropy process. Therefore:\n",
    "deltaS = Cp*log(T2/T1)## [J/kmol]\n",
    "S2 = S1 + deltaS## [J/kmol]\n",
    "print \"Entropy at 500 K is %.2f J/kmol\"%(S2)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.20 Page: 173"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.20 - Page: 173\n",
      "\n",
      "\n",
      "Total Entropy Change is 369.49 kJ/K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.20 - Page: 173\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "T1_oil = 273 + 150## [K]\n",
    "T2_oil = 50 +273## [K]\n",
    "m_water = 4000## [kg]\n",
    "T1_water = 273 + 20## [K]\n",
    "T2_water = 273 + 130## [K]\n",
    "C_water = 4.185## [kJ/kg K]\n",
    "C_oil = 2.5## [kJ/kg K]\n",
    "#***************#\n",
    "\n",
    "# For oil:\n",
    "deltaT_oil = T1_oil - T2_oil## [K]\n",
    "# For water:\n",
    "deltaT_water = T2_water - T1_water## [K]\n",
    "# The mass flow rate of oil can be measured by the enthalpy balance over the process:\n",
    "m_oil = m_water*C_water*deltaT_water/(deltaT_oil*C_oil)## [kg]\n",
    "# Change in the Entropy of oil:\n",
    "deltaS_oil = m_oil*C_oil*log(T2_oil/T1_oil)## [kJ/K]\n",
    "# Change in Entropy of water:\n",
    "deltaS_water = m_water*C_water*log(T2_water/T1_water)## [kJ/K]\n",
    "deltaS = deltaS_oil + deltaS_water## [kJ/K]\n",
    "print \"Total Entropy Change is %.2f kJ/K\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.21 Page: 174"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.21 - Page: 174\n",
      "\n",
      "\n",
      "Change in Entropy is 1.49 kJ/K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.21 - Page: 174\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "t = 20*60## [s]\n",
    "P = 650## [W]\n",
    "T = 273 + 250## [K]\n",
    "#*************#\n",
    "\n",
    "Q = P*t/1000## [kJ]\n",
    "deltaS = Q/T## [kJ/K]\n",
    "print \"Change in Entropy is %.2f kJ/K\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.22 Page: 174"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.22 - Page: 174\n",
      "\n",
      "\n",
      "Change in Entropy of the gas is 0.5199 kJ/K\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.22 - Page: 174\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "#*****Data*****#\n",
    "T1 = 400## [K]\n",
    "P1 = 300## [kPa]\n",
    "V1 = 1## [cubic m]\n",
    "V2 =2## [cubic m]\n",
    "R = 8.314## [kJ/kmol K]\n",
    "#**************#\n",
    "\n",
    "# Since the system is well insulated, there is no scope of transferring heat between system & surrounding.\n",
    "deltaQ = 0## [kJ]\n",
    "deltaW = 0## [kJ]\n",
    "# By first law of thermodynamics:\n",
    "deltaU =deltaQ - deltaW## [kJ]\n",
    "# As the internal energy of the gas depends only on temperature,\n",
    "deltaT = 0## [K]\n",
    "T2 = T1 + deltaT## [K]\n",
    "P2 = (P1*V1/T1)*(T2/V2)## [kPa]\n",
    "n = P1*V1/(R*T1)## [kmol]\n",
    "deltaS_system = n*R*log(P1/P2)## [kJ/K]\n",
    "# Since process is adiabatic:\n",
    "deltaS_surrounding = 0## [kJ/K]\n",
    "deltaS = deltaS_system + deltaS_surrounding## [kJ/K]\n",
    "print \"Change in Entropy of the gas is %.4f kJ/K\"%(deltaS)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.23 Page: 174"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.23 - Page: 174\n",
      "\n",
      "\n",
      " This problem involves proving a relation in which no mathematics and no calculations are involved.\n",
      "\n",
      "\n",
      " For prove refer to this example 5.23 on page 174 of the book.\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.23 - Page: 174\\n\\n\"\n",
    "\n",
    "# This problem involves proving a relation in which no mathematics and no calculations are involved.\n",
    "# For prove refer to this example 5.23 on page number 174 of the book.\n",
    "\n",
    "print \" This problem involves proving a relation in which no mathematics and no calculations are involved.\\n\\n\"\n",
    "print \" For prove refer to this example 5.23 on page 174 of the book.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.24 Page: 182"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.24 - Page: 182\n",
      "\n",
      "\n",
      "Work lost is 2119.67 kJ\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.24 - Page: 182\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "# From Example 5.18 (Pg: 170)\n",
    "#*****Data*****#\n",
    "m_iron = 40## [kg]\n",
    "T1 = 625## [K]\n",
    "m_water = 160## [kg]\n",
    "T2 = 276## [K]\n",
    "C_iron = 0.45## [kJ/kg K]\n",
    "C_water = 4.185## [kJ/kg K]\n",
    "#**************#\n",
    "\n",
    "#deff('[y] = f(T)','y = m_iron*C_iron*(T1 - T) - m_water*C_water*(T - T2)')#\n",
    "def f(T):\n",
    "    y = m_iron*C_iron*(T1 - T) - m_water*C_water*(T - T2)\n",
    "    return y\n",
    "T = fsolve(f, 7)[0]## [K]\n",
    "# Change in Entropy of the iron casting can be estimated as:\n",
    "deltaS1 = m_iron*C_iron*quad(lambda T:(1/T),[T1,T])## [kJ/K]\n",
    "# Change in Entropy of Water is given by:\n",
    "deltaS2 = m_water*C_water*quad(lambda T:(1/T),[T2,T])## [kJ/K]\n",
    "deltaS = deltaS1 + deltaS2## [kJ/K]\n",
    "# By Eqn. 5.63:\n",
    "W_lost = T2 * deltaS## [kJ]\n",
    "print \"Work lost is %.2f kJ\"%(W_lost)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 5.25 Page: 182"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example: 5.25 - Page: 182\n",
      "\n",
      "\n",
      "Total Entropy Change is 1831.74 kJ/K\n",
      "\n",
      "Since deltaS is a positive quantity, process is irreversible\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print \"Example: 5.25 - Page: 182\\n\\n\"\n",
    "\n",
    "# Solution\n",
    "\n",
    "# *****Data******#\n",
    "m_oil = 4750## [kg]\n",
    "T1_oil = 515## [K]\n",
    "T2_oil = 315## [K]\n",
    "m_water = 9500## [kg]\n",
    "T1_water = 290## [K]\n",
    "Cp_oil = 3.2## [kJ/kg K]\n",
    "Cp_water = 4.185## [kJ/kg K]\n",
    "#*****************#\n",
    "\n",
    "# From enthalpy Balance:\n",
    "#def('[y] = f(T2_water)','y = m_oil*Cp_oil*(T1_oil - T2_oil) - m_water*Cp_water*(T2_water - T1_water)')#\n",
    "def f(T2_water):\n",
    "    y = m_oil*Cp_oil*(T1_oil - T2_oil) - m_water*Cp_water*(T2_water - T1_water)\n",
    "    return y\n",
    "T2_water = fsolve(f, 7)[0]## [K]\n",
    "# Change in the Entropy of oil:\n",
    "deltaS_oil = m_oil*Cp_oil*quad(lambda T : (1/T),[T1_oil,T2_oil])## [kJ/K]\n",
    "# Change in Entropy of water:\n",
    "deltaS_water = m_water*Cp_water*quad(lambda T: (1/T),[T1_water,T2_water])## [kJ/K]\n",
    "deltaS = deltaS_oil + deltaS_water## [kJ/K]\n",
    "print \"Total Entropy Change is %.2f kJ/K\\n\"%(deltaS)#\n",
    "if deltaS > 0:\n",
    "    print \"Since deltaS is a positive quantity, process is irreversible\\n\"\n",
    "else:\n",
    "    print \"Since deltaS is a negative quantity, process is reversible\\n\"\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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}