{
 "metadata": {
  "name": "",
  "signature": "sha256:f22779b8fff9ea06914fd530daf547f36c0e2d7163616b6b2b466ab75a676e0a"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 7 : Entropy"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.1 Page No : 185"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "\n",
      "# Variables\n",
      "T1 = 37.+273;\n",
      "T2 = 35.+273;\n",
      "m = 1.;\n",
      "cv = 4.187;\n",
      "\n",
      "# Calculation\n",
      "S = m*cv*math.log(T1/T2); \t\t\t# S = S2-S1\n",
      "\n",
      "# Results\n",
      "print \"Change in the entropy of the water is %.4f KJ/K\"%S\n",
      "\n",
      "# note : answer is accurate. please check."
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Change in the entropy of the water is 0.0271 KJ/K\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.2 Page No : 186"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "\n",
      "# Part (a)\n",
      "T1 = 273.;\n",
      "T2 = 373.;\n",
      "m = 1. ;\n",
      "cv = 4.187;\n",
      "\n",
      "# Calculation and Results\n",
      "Ss = m*cv*math.log(T2/T1); \t\t\t# S = S2-S1\n",
      "Q = m*cv*(T2-T1);\n",
      "Sr = -(Q/T2);\n",
      "S = Ss+Sr;\n",
      "\n",
      "print \"The entropy change of the universe is %.3f kJ/K\"%S\n",
      "\n",
      "# Part (b)\n",
      "T3 = 323.;\n",
      "Sw = m*cv*(math.log(T3/T1)+math.log(T2/T3));\n",
      "Sr1 = -m*cv*(T3-T1)/T3;\n",
      "Sr2 = -m*cv*(T2-T3)/T2;\n",
      "Su = Sw+Sr1+Sr2;\n",
      "print \"The entropy change of the universe is %.3f kJ/K\"%Su\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The entropy change of the universe is 0.184 kJ/K\n",
        "The entropy change of the universe is 0.097 kJ/K\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.3 Page No : 187"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "\n",
      "# Variables\n",
      "# Part (a)\n",
      "m = 1.;\n",
      "T1 = -5.+273;\n",
      "T2 = 20.+273;\n",
      "T0 = 0.+273;\n",
      "cp = 2.093;\n",
      "cv = 4.187;\n",
      "lf = 333.3;\n",
      "\n",
      "# Calculation\n",
      "Q = m*cp*(T0-T1)+1*333.3+m*cv*(T2-T0);\n",
      "Sa = -Q/T2;\n",
      "Ss1 = m*cp*math.log(T0/T1);\n",
      "Ss2 = lf/T0;\n",
      "Ss3 = m*cv*math.log(T2/T0);\n",
      "St = Ss1+Ss2+Ss3;\n",
      "Su = St+Sa;\n",
      "\n",
      "# Results\n",
      "print \"The entropy change of the universe is %.4f kJ/K\"%Su\n",
      "\n",
      "# Part (b)\n",
      "S = 1.5549; \t\t\t# S = S4-S1\n",
      "Wmin = T2*(S)-Q;\n",
      "print \"The minimum risk required is %.1f kJ\"%Wmin\n",
      "\n",
      "# rounding off error. please check."
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The entropy change of the universe is 0.0965 kJ/K\n",
        "The minimum risk required is 28.1 kJ\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.5 Page No : 190"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "\n",
      "Vo = 8.4;\n",
      "Vh = 14.;\n",
      "n1 = Vo/22.4; \n",
      "n2 = Vh/22.4;\n",
      "R = 8.31;\n",
      "\n",
      "# Calculation\n",
      "x1 = n1/(n1+n2);\n",
      "x2 = n2/(n1+n2);\n",
      "S = -R*(n1*math.log(x1)+n2*math.log(x2));\n",
      "\n",
      "# Results\n",
      "print \"Entropy change for the process is %.2f J/K\"%S\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Entropy change for the process is 5.50 J/K\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.8 Page No : 192"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "from hornerc import horner\n",
      "import sys\n",
      "\n",
      "# Variables\n",
      "#T = poly(0,'T'); \t\t\t# T = Tf\n",
      "Tf_ = [700,-2]   #700-2*T; \t\t\t# Tf_ = Tf'\n",
      "\n",
      "# Calculation\n",
      "# Bisection method to solve for the polynomial\n",
      "def Temperature(a,b,f):\n",
      "    N = 100.;\n",
      "    eps = 1e-5;\n",
      "    if((f(a)*f(b))>0):\n",
      "        print ('no root possible f(a)*f(b)>0');\n",
      "        sys.exit(0);\n",
      "\n",
      "    if(abs(f(a))<eps):\n",
      "        print ('solution at a');\n",
      "        sys.exit(0)\n",
      "\n",
      "    if(abs(f(b))<eps):\n",
      "        print ('solution at b');\n",
      "        sys.exit(0)\n",
      "\n",
      "    while(N>0):\n",
      "        c = (a+b)/2\n",
      "        if(abs(f(c))<eps):\n",
      "            x = c ;\n",
      "            return x;\n",
      "\n",
      "        if((f(a)*f(c))<0 ):\n",
      "            b = c ;\n",
      "        else:\n",
      "            a = c ;\n",
      "        N = N-1;\n",
      "    print ('no convergence');\n",
      "    sys.exit(0);\n",
      "\n",
      "\n",
      "def p(T): \n",
      "\t return  2*T**3-700*T**2+9000000 \n",
      "T = Temperature(100,200,p);\n",
      "\n",
      "Tf_ = horner(Tf_,T);\n",
      "\n",
      "print \"The final temperature of the body C is\",Tf_,\"K\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The final temperature of the body C is 400 K\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.9 Page No : 193"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "from scipy.integrate import quad \n",
      "import sys\n",
      "from numpy import poly1d\n",
      "from sympy import Symbol\n",
      "\n",
      "# Variables\n",
      "T1 = 200.;\n",
      "T2 = 100.;\n",
      "A = 0.042;\n",
      "\n",
      "# Calculation\n",
      "def f10(T): \n",
      "\t return A*T**2\n",
      "\n",
      "Q1 =  quad(f10,T1,T2)[0]\n",
      "\n",
      "\n",
      "def f11(T): \n",
      "\t return A*T**2/T\n",
      "\n",
      "S =   quad(f11,T1,T2)[0]\n",
      "\n",
      "W = Symbol('W');\n",
      "Z = (-Q1-W)/T2 + S; \t\t\t# Polynomial to be solved for W\n",
      "\n",
      "# Bisection method to solve for the Work\n",
      "def Work(a,b,f):\n",
      "    N = 100.;\n",
      "    eps = 1e-5;\n",
      "    if((f(a)*f(b))>0):\n",
      "        print ('no root possible f(a)*f(b)>0');\n",
      "        sys.exit(0);\n",
      "\n",
      "    if(abs(f(a))<eps):\n",
      "        print ('solution at a');\n",
      "        sys.exit(0)\n",
      "\n",
      "    if(abs(f(b))<eps):\n",
      "        print ('solution at b');\n",
      "        sys.exit(0)\n",
      "\n",
      "    while(N>0):\n",
      "        c = (a+b)/2\n",
      "        if(abs(f(c))<eps):\n",
      "            x = c ;\n",
      "            return x;\n",
      "        if((f(a)*f(c))<0 ):\n",
      "            b = c ;\n",
      "        else:\n",
      "            a = c ;\n",
      "        N = N-1;\n",
      "    print ('no convergence');\n",
      "    sys.exit(0)\n",
      "\n",
      "def p(W): \n",
      "\t return  350-0.01*W \n",
      "\n",
      "W = Work(34000,36000,p);\n",
      "\n",
      "print \"The maximum work that can be recovered is\",W/1000,\"kJ\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The maximum work that can be recovered is 35 kJ\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.10 Page No : 194"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "from scipy.integrate import quad \n",
      "\n",
      "# Variables\n",
      "P1 = 0.5e06;\n",
      "V1 = 0.2\n",
      "V2 = 0.05;\n",
      "n = 1.3\n",
      "\n",
      "# Calculation\n",
      "P2 = P1*(V1/V2)**n;\n",
      "def H(p):\n",
      "    return ((P1*V1**n)/p)**(1./n);\n",
      "\n",
      "def f0(p): \n",
      "\t return H\n",
      "\n",
      "H =  quad(H,P1,P2)[0]\n",
      "\n",
      "U = H-(P2*V2-P1*V1);\n",
      "W12 = -U;\n",
      "\n",
      "# Results\n",
      "print \"Change in enthalpy is %.1f kJ\"%(H/1000)\n",
      "print \"Change in internal energy is %.2f kJ\"%(U/1000)\n",
      "print \"The change in entropy and heat transfer are\",0,\"and\",0,\"kJ\"\n",
      "print \"The work transfer during the process is %.2f kJ\"%(W12/1000)\n",
      "\n",
      "# rounding off error."
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Change in enthalpy is 223.5 kJ\n",
        "Change in internal energy is 171.91 kJ\n",
        "The change in entropy and heat transfer are 0 and 0 kJ\n",
        "The work transfer during the process is -171.91 kJ\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.11 Page No : 195"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "from scipy.integrate import quad \n",
      "\n",
      "# Variables\n",
      "Pa = 130e03; \n",
      "Pb = 100e03;\n",
      "Ta = 50+273; \n",
      "Tb = 13+273;\n",
      "cp = 1.005;\n",
      "\n",
      "# Calculation\n",
      "def f1(T): \n",
      "\t return cp/T\n",
      "\n",
      "def f2(p):\n",
      "    return .287/p\n",
      "Ss =  quad(f1,Ta,Tb)[0] - quad(f2,Pa,Pb)[0]\n",
      "Ssy = 0;\n",
      "Su = Ss+Ssy;\n",
      "\n",
      "# Results\n",
      "print \"Change in the entropy of the universe is %.3f kJ/Kg k\"%Su\n",
      "print \"As the change in entropy of the universe in the process A-B is negative so the flow must be from B-A\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Change in the entropy of the universe is -0.047 kJ/Kg k\n",
        "As the change in entropy of the universe in the process A-B is negative so the flow must be from B-A\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.12 Page No : 196"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "\n",
      "# Variables\n",
      "T1 = 300.\n",
      "T2 = 330.\n",
      "T3 = 270.\n",
      "P1 = 4.\n",
      "P2 =1.\n",
      "P3 =1.\n",
      "cp = 1.0005\n",
      "R = 0.287;\n",
      "\n",
      "# Calculation\n",
      "S21 = cp*math.log(T2/T1)-R*math.log(P2/P1); \t\t\t# S21 = S2-S1\n",
      "S31 = cp*math.log(T3/T1)-R*math.log(P3/P1); \t\t\t# S31 = S3-S1\n",
      "Sgen = 1*S21 + 1*S31;\n",
      "\n",
      "# Results\n",
      "print \"The entropy generated during the process is %.3f kW/K\"%Sgen\n",
      "print \"As the entropy generated is positive so such device is possible\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The entropy generated during the process is 0.786 kW/K\n",
        "As the entropy generated is positive so such device is possible\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7.13 Page No : 197"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "\n",
      "# Variables\n",
      "A = 5.*7;\n",
      "k = 0.71;\n",
      "L = 0.32;\n",
      "Ti = 21.+273;\n",
      "To = 6.+273;\n",
      "\n",
      "# Calculation and Results\n",
      "Q = k*A*(Ti-To)/L ;\n",
      "print \"The rate of heat transfer through the wall is %.2f W\"%Q\n",
      "\n",
      "Sgen_wall = Q/To - Q/Ti;\n",
      "print \"The rate of entropy through the wall is %.3f W/K\"%Sgen_wall\n",
      "\n",
      "Tr = 27+273.;\n",
      "Ts = 2+273.;\n",
      "Sgen_total = Q/Ts-Q/Tr;\n",
      "print \"The rate of total entropy generation with this heat transfer process is %.3f W/K\"%Sgen_total\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The rate of heat transfer through the wall is 1164.84 W\n",
        "The rate of entropy through the wall is 0.213 W/K\n",
        "The rate of total entropy generation with this heat transfer process is 0.353 W/K\n"
       ]
      }
     ],
     "prompt_number": 11
    }
   ],
   "metadata": {}
  }
 ]
}