{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 07: Entropy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex7.1:pg-191"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Example 7.1\n",
      "\n",
      " Change in entropy of the water is  0.0271  kJ/K\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import math\n",
    "T1 = 37.0 # Final water temperature in degree Celsius \n",
    "T2 = 35.0 # Initial water temperature in degree Celsius \n",
    "m = 1.0  # Mass of water in kg\n",
    "cv = 4.187 # Specific heat capacity of water in kJ/kgK\n",
    "print \"\\n Example 7.1\"\n",
    "S = m*cv*math.log((T1+273)/(T2+273))  # Change in entropy of the water\n",
    "print \"\\n Change in entropy of the water is \",round(S,4) ,\" kJ/K\"\n",
    "#The answer provided in the textbook is wrong\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex7.2:pg-192"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Example 7.2\n",
      "\n",
      " The entropy change of the universe is  -1.12252010724  kJ/K\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'log' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-726fd0a673a1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     16\u001b[0m \u001b[1;31m# Part (b)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     17\u001b[0m \u001b[0mT3\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m323\u001b[0m \u001b[1;31m# Temperature of intermediate reservoir in K\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m \u001b[0mSw\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mm\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mcv\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mT3\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mT1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mT2\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mT3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# entropy change of water\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     19\u001b[0m \u001b[0mSr1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m-\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mcv\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mT3\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mT1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mT3\u001b[0m \u001b[1;31m# Entropy change of universe\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     20\u001b[0m \u001b[0mSr2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m-\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mcv\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mT2\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mT3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mT2\u001b[0m \u001b[1;31m# Entropy change of universe\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'log' is not defined"
     ]
    }
   ],
   "source": [
    "import math\n",
    "# Part (a)\n",
    "T1 = 273 # Initial temperature of water in Kelvin\n",
    "T2 = 373 # Temperature of heat reservoir in Kelvin\n",
    "m = 1 # Mass of water in kg\n",
    "cv = 4.187 # Specific heat capacity of water\n",
    "\n",
    "print \"\\n Example 7.2\"\n",
    "Ss = m*cv*math.log(T2/T1) # entropy change of water\n",
    "Q = m*cv*(T2-T1) # Heat transfer \n",
    "Sr = -(Q/T2) # Entropy change of universe\n",
    "S = Ss+Sr # Total entropy change\n",
    "\n",
    "print \"\\n The entropy change of the universe is \",S ,\" kJ/K\"\n",
    "\n",
    "# Part (b)\n",
    "T3 = 323 # Temperature of intermediate reservoir in K\n",
    "Sw = m*cv*(log(T3/T1)+log(T2/T3)) # entropy change of water\n",
    "Sr1 = -m*cv*(T3-T1)/T3 # Entropy change of universe\n",
    "Sr2 = -m*cv*(T2-T3)/T2 # Entropy change of universe\n",
    "Su = Sw+Sr1+Sr2 # Total entropy change\n",
    "print \"\\n The entropy change of the universe is \",Su ,\" kJ/K\"\n",
    "#The answers vary due to round off error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex7.3:pg-193"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Example 7.3\n",
      "\n",
      " The entropy change of the universe is  -0.238182312568  kJ/K\n",
      "\n",
      " The minimum work required is  -69.7874175824  kJ\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "m = 1 # Mass of ice in kg\n",
    "\n",
    "T1 = -5 # Initial temperature of ice in degree Celsius\n",
    "\n",
    "T2 = 20# Atmospheric temperature in degree Celsius\n",
    "\n",
    "T0 = 0# Phase change temperature of ice in degree Celsius\n",
    "\n",
    "cp = 2.093 # Specific heat capacity of ice in kJ/kgK\n",
    "\n",
    "cv = 4.187 # Specific heat capacity of water in kJ/kgK\n",
    "\n",
    "lf = 333.3 # Latent heat of fusion in kJ/kgK\n",
    "\n",
    "\n",
    "\n",
    "print \"\\n Example 7.3\"\n",
    "\n",
    "Q = m*cp*(T0-T1)+1*333.3+m*cv*(T2-T0) # Net heat transfer\n",
    "\n",
    "Sa = -Q/(T2+273) # Entropy change of surrounding\n",
    "\n",
    "Ss1 = m*cp*math.log((T0+273)/(T1+273)) # entropy change during \n",
    "\n",
    "Ss2 = lf/(T0+273) # Entropy change during phase change\n",
    "\n",
    "Ss3 = m*cv*math.log((T2+273)/(T0+273)) # entropy change of water\n",
    "\n",
    "St = Ss1+Ss2+Ss3 # total entropy change of ice to convert into water at atmospheric temperature\n",
    "\n",
    "Su = St+Sa # Net entropy change of universe\n",
    "\n",
    "print \"\\n The entropy change of the universe is \",Su ,\" kJ/K\"\n",
    "\n",
    "\n",
    "\n",
    "#The answer provided in the textbook is wrong\n",
    "\n",
    "# Part (b)\n",
    "\n",
    "S = St  # Entropy change of system\n",
    "\n",
    "Wmin = (T2+273)*(S)-Q # minimum work required\n",
    "\n",
    "print \"\\n The minimum work required is \",Wmin ,\" kJ\"\n",
    "\n",
    "#The answers vary due to round off error\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex7.7:pg-200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Example 7.7\n",
      "\n",
      " Change in enthalpy is  223.48  kJ\n",
      "\n",
      " Change in internal energy is  171.91  kJ\n",
      "\n",
      " The change in entropy and heat transfer are is  0  kJ\n",
      "\n",
      " The work transfer during the process is  -171.91  kJ\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "P1 = 0.5 # Initial pressure in MPa\n",
    "\n",
    "V1 = 0.2  # Initial volume in m**3\n",
    "\n",
    "V2 = 0.05 # Final volume in m**3\n",
    "\n",
    "n = 1.3 # Polytropic index\n",
    "\n",
    "\n",
    "\n",
    "from scipy import integrate \n",
    "\n",
    "print \"\\n Example 7.7\"\n",
    "\n",
    "P2 = P1*(V1/V2)**n \n",
    "\n",
    "def f(p):\n",
    "    y = ((P1*V1**n)/p)**(1/n) \n",
    "    return y\n",
    "    \n",
    "\n",
    "    \n",
    "H, err = integrate.quad(f,P1,P2)   # H = H2-H1\n",
    "\n",
    "U = H-(P2*V2-P1*V1) \n",
    "    \n",
    "W12 = -U \n",
    "    \n",
    "print \"\\n Change in enthalpy is \",round(H*1e3,2),\" kJ\"\n",
    "    \n",
    "print \"\\n Change in internal energy is \",round(U*1000,2),\" kJ\"\n",
    "    \n",
    "print \"\\n The change in entropy and heat transfer are is \",0 ,\" kJ\"\n",
    "    \n",
    "print \"\\n The work transfer during the process is \",round(W12*1000,2) ,\" kJ\"\n",
    "    \n",
    "    #The answers vary due to round off error\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex7.8:pg-201"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Example 7.8\n",
      "\n",
      " Change in the entropy of the universe is  -1.2785104723  kJ/Kg K\n",
      "\n",
      " As the change in entropy of the universe in the process A-B is negative \n",
      " so the flow must be from B-A\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import math\n",
    "from scipy import integrate \n",
    "\n",
    "\n",
    "Pa = 130.0 # Pressure at station A in kPa\n",
    "\n",
    "Pb = 100.0# Pressure at station B in kPa\n",
    "\n",
    "Ta = 50.0 # Temperature at station A in degree Celsius\n",
    "\n",
    "Tb = 13.0# Temperature at station B in degree Celsius\n",
    "\n",
    "cp = 1.005  # Specific heat capacity of air in kJ/kgK\n",
    "\n",
    "x= lambda t:cp/t\n",
    "y= lambda p:0.287/p\n",
    "\n",
    "print \"\\n Example 7.8\"\n",
    "\n",
    "Sb,error = integrate.quad(x,Ta,Tb)#-\n",
    "Sa,eror=integrate.quad(y,Pa,Pb) \n",
    "\n",
    "Ss=Sb-Sa\n",
    "Ssur=0 \n",
    "Su = Ss+Ssur\n",
    "\n",
    "print \"\\n Change in the entropy of the universe is \",Su ,\" kJ/Kg K\"\n",
    "\n",
    "#The answers given in the book is wrong\n",
    "\n",
    "print \"\\n As the change in entropy of the universe in the process A-B is negative \\n so the flow must be from B-A\"\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex7.9:pg-202"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Example 7.9\n",
      "\n",
      " The entropy generated during the process is  0.785677602261  kW/K\n",
      "\n",
      " As the entropy generated is positive so such device is possible\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "T1 = 300.0  # Inlet temperature of air in K\n",
    "\n",
    "T2 = 330.0  # Exit temperature of first air stream in K\n",
    "\n",
    "T3 = 270.0 # Exit temperature of second air stream in K\n",
    "\n",
    "P1 = 4.0  # Pressure of inlet air stream in bar\n",
    "\n",
    "P2 =1.0   # Pressure of first exit air stream in bar\n",
    "\n",
    "P3 =1.0  # Pressure of second exit air stream in bar\n",
    "\n",
    "cp = 1.0005  # Specific heat capacity of air in kJ/kgK\n",
    "\n",
    "R = 0.287 # Gas constant\n",
    "\n",
    "\n",
    "\n",
    "print \"\\n Example 7.9\"\n",
    "\n",
    "S21 = cp*math.log(T2/T1)-R*math.log(P2/P1)  # Entropy generation\n",
    "\n",
    "S31 = cp*math.log(T3/T1)-R*math.log(P3/P1) # Entropy generation\n",
    "\n",
    "Sgen = (1.0*S21) + (1.0*S31) # Total entropy generation\n",
    "\n",
    "print \"\\n The entropy generated during the process is \",Sgen ,\" kW/K\"\n",
    "\n",
    "#The answers vary due to round off error\n",
    "\n",
    "\n",
    "\n",
    "print \"\\n As the entropy generated is positive so such device is possible\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex7.10:pg-203"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Example 7.10\n",
      "\n",
      " The rate of heat transfer through the wall is  1164.84375  W\n",
      "\n",
      " The rate of entropy through the wall is  0.213013632873  W/K\n",
      "\n",
      " The rate of total entropy generation with this heat transfer process is  0.352982954545  W/K\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "A = 5*7 # Area of wall in m**2\n",
    "k = 0.71# Thermal conductivity in W/mK \n",
    "L = 0.32 # Thickness of wall in m\n",
    "Ti = 21 # Room temperature in degree Celsius \n",
    "To = 6 # Surrounding temperature in degree Celsius\n",
    "print \"\\n Example 7.10\"\n",
    "Q = k*A*(Ti-To)/L  # Heat transfer\n",
    "Sgen_wall = Q/(To+273) - Q/(Ti+273)  # Entropy generation in wall\n",
    "print \"\\n The rate of heat transfer through the wall is \",Q ,\" W\"\n",
    "print \"\\n The rate of entropy through the wall is \",Sgen_wall ,\" W/K\"\n",
    "Tr = 27 # Inner surface temperature of wall in degree Celsius \n",
    "Ts = 2 # Outer surface temperature of wall in degree Celsius \n",
    "Sgen_total = Q/(Ts+273)-Q/(Tr+273) # Total entropy generation in process \n",
    "print \"\\n The rate of total entropy generation with this heat transfer process is \",Sgen_total ,\" W/K\"\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
}