{
 "metadata": {
  "name": "",
  "signature": "sha256:96bec67660df87f03597152ea9aebaa9244c05006b79d30c85f08e05c1a87e20"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 9 : Chemical Equilibria"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.1.A page : 359"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\t\t\t\n",
      "# Variables\n",
      "g11 = 178900 \t\t\t#kJ/kmol\n",
      "g12 = 207037 \t\t\t#kJ/kmol\n",
      "g21 = 211852 \t\t\t#kJ/kmol\n",
      "g22 = 228097 \t\t\t#kJ/kmol\n",
      "\t\t\t\n",
      "# Calculations\n",
      "dG = g21-g11\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"Standard free energy change  =  %d kJ/kmol\"%(dG)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Standard free energy change  =  32952 kJ/kmol\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.1B  page : 359"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "\t\t\t\n",
      "# Variables\n",
      "m1 = 54.1\n",
      "m2 = 56.1\n",
      "m3 = 2.\n",
      "cp1 = 2.122 \t\t\t#kJ/kmol K\n",
      "cp2 = 2.213 \t\t\t#kJ/kmol K\n",
      "cp3 = 14.499 \t\t\t#kJ/kmol K\n",
      "hf1 = 110200. \t\t\t#kJ/kmol\n",
      "hf2 = -126. \t\t\t#kJ/kmol\n",
      "T = 700.    \t\t\t#K\n",
      "Ts = 298. \t    \t\t#K\n",
      "\t\t\t\n",
      "# Calculations\n",
      "hf = hf1-hf2\n",
      "cpn = cp1*m1-cp2*m2+cp3*m3\n",
      "h700 = hf+ cpn*(T-Ts)\n",
      "s298 = 103.7\n",
      "s700  =  s298 + cpn*math.log(T/Ts)\n",
      "G700 = h700-T*s700\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"Change in gibbs energy  =  %d kJ/kmol\"%(G700)\n",
      "print (\"The answer is a bit different due to rounding off error in textbook\")\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Change in gibbs energy  =  33888 kJ/kmol\n",
        "The answer is a bit different due to rounding off error in textbook\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.2 page : 361"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "\t\t\t\n",
      "# Variables\n",
      "g1 = 150670. \t\t\t#kJ/kmol\n",
      "g2 = 71500. \t\t\t#kJ/kmol\n",
      "R = 8.314\n",
      "Ts = 298. \t\t\t#K\n",
      "T = 700. \t\t\t#K\n",
      "\n",
      "#calculation\n",
      "G = g1-g2\n",
      "G2 = 33875 \t\t\t#kJ/kmol\n",
      "K1 = math.exp(-G/R/Ts)\n",
      "K2 = math.exp(-G2/R/T)\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"In case 1, equilibrium constant  =  %.2e\"%(K1)\n",
      "print \" In case 2, equilibrium constant  =  %.5f\"%(K2)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "In case 1, equilibrium constant  =  1.33e-14\n",
        " In case 2, equilibrium constant  =  0.00297\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.3 page : 363"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "from scipy.integrate import quad\n",
      "\t\t\t\n",
      "# Variables\n",
      "R = 8.3143\n",
      "T1 = 1273 \t\t\t#K\n",
      "T2 = 2273 \t\t\t#K\n",
      "k2 = 0.0018\n",
      "A = 123.94\n",
      "B = 7.554\n",
      "C = 8.552*10**-3\n",
      "D = -13.25e-6\n",
      "E = 7.002e-9\n",
      "F = 13.494e-13\n",
      "\t\t\t\n",
      "# Calculations\n",
      "def  cp(T):\n",
      "    return A/T**2 +B/T +C +D*T +E*T**2 -F*T**3\n",
      "\n",
      "\n",
      "lnk = 1/R * quad(cp,T1,T2)[0]\n",
      "k1 = k2/ math.exp(lnk)\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"Equilibrium constant  =  %.5f \"%(k1)\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Equilibrium constant  =  0.00112 \n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.4 page : 368"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "from scipy.optimize import fsolve\n",
      "\t\t\t\n",
      "# Variables\n",
      "G = -30050. \t\t\t#kJ/kmol\n",
      "R = 8.314\n",
      "T = 573.     \t\t\t#K\n",
      "\t\t\t\n",
      "# Calculations\n",
      "lnk = G/(R*T)\n",
      "k = math.exp(lnk)\n",
      "\n",
      "def f(x):\n",
      "    return 4*x**2 - k*(1-x)**2\n",
      "\n",
      "vec = fsolve(f,0,full_output=1)\n",
      "\n",
      "x2 = vec[0]\n",
      "# Results\n",
      "print \"Mole fraction of HCN  =  %.4f\"%(x2)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Mole fraction of HCN  =  0.0209\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.4B page : 368"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "from numpy import roots\n",
      "\t\t\t\n",
      "# Variables\n",
      "G = -30050. \t\t\t#kJ/kmol\n",
      "R = 8.314\n",
      "T = 573. \t\t\t#K\n",
      "phi1 = 0.980\n",
      "phi2 = 0.915\n",
      "phi3 = 0.555\n",
      "\t\t\t\n",
      "# Calculations\n",
      "lnk = G/(R*T)\n",
      "k = math.exp(lnk)\n",
      "kexp =  k*phi1*phi2/phi3**2 /4\n",
      "vec = roots([1-kexp,2*kexp,-kexp])\n",
      "x2 = vec[1]\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"Mole fraction of HCN  =  %.4f\"%(x2)\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Mole fraction of HCN  =  0.0351\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.5 page : 370"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "from scipy.optimize import fsolve\n",
      "\t\t\t\n",
      "# Variables\n",
      "kp = 74.\n",
      "kp2 = kp**2\t\t\t\n",
      "# Calculations\n",
      "def fun(f):\n",
      "    return f**2 *(100-6*f) - kp**2 *(1-f)**2 *(9-6*f)\n",
      "\n",
      "vec = fsolve(fun,0,full_output=1)\n",
      "fn = vec[0]\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"Fractional conversion  =  %.3f\"%(fn)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Fractional conversion  =  0.934\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.6 page : 372"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\t\t\t\n",
      "# Variables\n",
      "C = 3.\n",
      "phi = 3.\n",
      "R = 1.\n",
      "Sc = 0.\n",
      "\n",
      "def fun(C,phi,R,Sc):\n",
      "    return 2+C-phi-R-Sc\n",
      "\t\t\t\n",
      "# Calculations\n",
      "V = fun(C,phi,R,Sc)\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"Degrees of freedom  =  %d \"%(V)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Degrees of freedom  =  1 \n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.6B page : 373"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\t\t\t\n",
      "# Variables\n",
      "C = 3.\n",
      "phi = 1.\n",
      "R = 1.\n",
      "Sc = 1.\n",
      "\n",
      "def fun(C,phi,R,Sc):\n",
      "    return 2+C-phi-R-Sc\n",
      "\n",
      "# Calculations\n",
      "V = fun(C,phi,R,Sc)\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"Degrees of freedom  =  %d \"%(V)\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Degrees of freedom  =  2 \n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.6C page : 373"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\t\t\t\n",
      "# Variables\n",
      "C = 6.\n",
      "phi = 1.\n",
      "R = 3.\n",
      "Sc = 0.\n",
      "\n",
      "def fun(C,phi,R,Sc):\n",
      "    return 2+C-phi-R-Sc\n",
      "\n",
      "\t\t\t\n",
      "# Calculations\n",
      "V = fun(C,phi,R,Sc)\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"Degrees of freedom  =  %d \"%(V)\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Degrees of freedom  =  4 \n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.7 page : 377"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\t\t\t\n",
      "# Variables\n",
      "a1 = 0.956\n",
      "y = 0.014\n",
      "x = 0.956\n",
      "M = 18.\n",
      "z = 0.475\n",
      "P = 8.37 \t\t\t#Mpa\n",
      "\t\t\t\n",
      "# Calculations\n",
      "m = y/(x*M) *10**3\n",
      "w = 0.0856\n",
      "phi1 = -0.04\n",
      "phi2 = 0.06\n",
      "phi = 10**(phi1+ w*phi2)\n",
      "f = z*phi*P\n",
      "K = m/(f*a1)\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"Equilibrium constant  =  %.3f\"%(K)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Equilibrium constant  =  0.232\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.9 page : 385"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from numpy import array\n",
      "\t\t\t\n",
      "# Variables\n",
      "y = 0.18\n",
      "z = 0.6\n",
      "\t\t\t\n",
      "# Calculations\n",
      "mole = array([1-y-z, 5-y-2*z, y, 3*y+4*z, z])\n",
      "s = sum( mole)\n",
      "molef = mole/s\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"Product composition moles   =  \",\n",
      "print (mole)\n",
      "print \"Mole fraction  =  \",\n",
      "print (molef)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Product composition moles   =   [ 0.22  3.62  0.18  2.94  0.6 ]\n",
        "Mole fraction  =   [ 0.02910053  0.47883598  0.02380952  0.38888889  0.07936508]\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.10 page : 388"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sympy import Symbol,solve\n",
      "\t\t\t\n",
      "# Variables\n",
      "kp = 1.09\n",
      "\t\t\t\n",
      "# Calculations\n",
      "x = Symbol('x')\n",
      "vec = solve(kp/4**4 /4 *(1-x)*(5-2*x)**2 *(6+2*x)**2 -x**5)\n",
      "x = vec[0]\n",
      "pro = [1-x, 5-2*x, x, 4*x, 0]\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"Equlibrium composition (moles) =  \",\n",
      "print (pro)\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Equlibrium composition (moles) =   [0.273026192093833, 3.54605238418767, 0.726973807906167, 2.90789523162467, 0]\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.10B page : 390"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "from numpy import array\n",
      "from scipy.optimize import fsolve\n",
      "\t\t\t\n",
      "# Variables\n",
      "kp = 1.09\n",
      "kp2 = 0.154\n",
      "feed = array([ 1, 5, 0, 0, 0 ])\n",
      "\t\t\t\n",
      "# Calculations\n",
      "\n",
      "def f1(x):\n",
      "    return kp/4**4 /4 *(1-x)*(5-2*x)**2 *(6+2*x)**2 -x**5\n",
      "\n",
      "vec = fsolve(f1,0,full_output=1)[0]\n",
      "x = vec[0]\n",
      "pro = feed - array([x, 2*x, -x, -4*x, 0])\n",
      "\n",
      "def f2(y):\n",
      "    return kp2*(0.273-y)*(0.727-y)*(7.454+2*y)**2 - 4*y**2 *(2.908+2*y)**2 *4\n",
      "\n",
      "vec2 = fsolve(f2,0,full_output=1)[0]    \n",
      "y = vec2[0]\n",
      "pro2 = pro- array([ y, 0, y, -2*y, -2*y])\n",
      "\n",
      "def f3(z):\n",
      "    return kp*(0.189-z)*(3.546-2*z)**2 *(7.622+2*z)**2 -(0.643+z)*(3.076+4*z)**4 *4\n",
      "    \n",
      "vec3 =  fsolve(f3,0,full_output=1)[0]\n",
      "z = vec3[0]\n",
      "pro3 = pro2 - array([z, 2*z, -z, -4*z, 0])\n",
      "\n",
      "def f4(w):\n",
      "    return kp2*(0.229-w)*(0.603-w)*(7.542+2*w) - (2.916+2*w)**2 *(0.168+2*w)**2 *4\n",
      "\n",
      "vec4 = fsolve(f4,0,full_output=1)[0]\n",
      "w = vec4[0]\n",
      "w = 0.01\n",
      "pro4 = pro3 - array([w, 0, w, -2*w, -2*w])\n",
      "\t\t\t\n",
      "# Results\n",
      "print \"feed  =  \",\n",
      "print (feed)\n",
      "print \"After reactor 1,\",\n",
      "print (pro)\n",
      "print \"After reactor 2,\",\n",
      "print (pro2)\n",
      "print \"After reactor 3,\",\n",
      "print (pro3)\n",
      "print \"After reactor 4\",\n",
      "print (pro4)\n",
      "print (\"The answers are a bit different due to rounding off error in textbook\")\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "feed  =   [1 5 0 0 0]\n",
        "After reactor 1, [ 0.27302619  3.54605238  0.72697381  2.90789523  0.        ]\n",
        "After reactor 2, [ 0.18846228  3.54605238  0.6424099   3.07702305  0.16912782]\n",
        "After reactor 3, [ 0.22284245  3.61481272  0.60802973  2.93950238  0.16912782]\n",
        "After reactor 4 [ 0.21284245  3.61481272  0.59802973  2.95950238  0.18912782]\n",
        "The answers are a bit different due to rounding off error in textbook\n"
       ]
      }
     ],
     "prompt_number": 6
    }
   ],
   "metadata": {}
  }
 ]
}