{
 "metadata": {
  "name": "",
  "signature": "sha256:70e5d31cbd675202c3287b597ce34b28fa488bc5446177836cd0448444371ac9"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 9 :  Chemical Reaction Equilibria"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.6"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import math\n",
      "#Given:\n",
      "Go_reac = 97540.; \t\t\t#standard free energy of formation of reactant (J/mol)\n",
      "Go_pdt = 51310.; \t\t\t#standard free energy of formation of product (J/mol)\n",
      "R = 8.314; \t\t\t#ideal gas constant\n",
      "T = 298.; \t\t\t#temperature (K)\n",
      "#Reaction: N2O4(g) --> 2NO2(g)\n",
      "\n",
      "# Calculations\n",
      "#To calculate equilibrium constant\n",
      "#Using eq. 9.50 (Page no.413)\n",
      "Go = 2*Go_pdt - Go_reac;\n",
      "#Using eq. 9.31 (Page no. 406)\n",
      "K = math.e**(-Go/(R*T))\n",
      "\n",
      "# Results\n",
      "print 'The equilbrium constant %f'%K\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The equilbrium constant 0.128684\n"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.7"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "T1 = 298.; \t\t\t#temperature in K\n",
      "Hf = -46100.; \t\t\t#standard heat of formation (J/mol)\n",
      "Go = -16500.; \t\t\t#standard free energy change (J/mol)\n",
      "R = 8.314; \t\t\t#ideal gas constant\n",
      "T = 500.; \n",
      "#Reaction: N2(g) + 3H2(g) --> 2NH3(g)\n",
      "#To calculate the equilibrium constant at 500 K\n",
      "#Using eq. 9.50 (Page no. 413)\n",
      "\n",
      "# Calculations\n",
      "del_Go = 2*Go;\n",
      "import math\n",
      "#Using eq. 9.31 (Page no. 406)\n",
      "K1 = math.e**(-del_Go/(R*T1)) \t\t\t#equilibrium const at 298 K\n",
      "Ho = 2*Hf; \t\t\t#standard heat of reaction\n",
      "\n",
      "#Using eq. 9.37 (Page no. 411)\n",
      "K = K1*(math.e**((-Ho/R)*(1/T - 1/T1)))\n",
      "\n",
      "# Results\n",
      "print 'The equilibrium constant at 500 K is %f'%K\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The equilibrium constant at 500 K is 0.179981\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.8"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variablesa\n",
      "R = 8.314; \t\t\t#ideal gas constant\n",
      "T2 = 317.; \t\t\t#temperature in K\n",
      "T1 = 391.; \t\t\t#(K)\n",
      "x2 = 0.31; \t\t\t#mol fraction of n-butane at 317 K\n",
      "x1 = 0.43; \t\t\t#mol fraction of iso-butane at 391 K\n",
      "\n",
      "# Calculations\n",
      "#To calculate standard free energy change and heat of reaction\n",
      "#At 317 K\n",
      "K2 = (1-x2)/x2; \t\t\t#equilibrium constant at 317 K\n",
      "K1 = (1-x1)/x1; \t\t\t#equilibrium constant at 391 K\n",
      "import math\n",
      "#Using eq. 9.31 (Page no. 406)\n",
      "#Standard free energy change\n",
      "G2 = -R*T2*math.log(K2 )\t\t\t#at 317 K (J/mol)\n",
      "G1 = -R*T1*math.log(K1 )\t\t\t#at 391 K (J/mol)\n",
      "\n",
      "#Using eq. 9.37 (Page no. 411)\n",
      "Ho = -math.log(K2/K1)*R/(1/T2 - 1/T1)\n",
      "\n",
      "# Calculations\n",
      "print 'Standard free energy change of the reaction'\n",
      "print ' At 317 K is %f J/mol'%G2\n",
      "print ' At 391 K is %f J/mol'%G1\n",
      "print ' Average value of heat of reaction is %f J/mol'%Ho\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Standard free energy change of the reaction\n",
        " At 317 K is -2108.744820 J/mol\n",
        " At 391 K is -916.234397 J/mol\n",
        " Average value of heat of reaction is -7217.201631 J/mol\n"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.9"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "To = 298.; \t\t\t#temperature in K\n",
      "T = 700.; \t\t\t#(K)\n",
      "R = 8.314; \t\t\t#ideal gas constant\n",
      "Hf = -46100.; \t\t\t#standard heat of formation (J/mol)\n",
      "Gf = -16500.; \t\t\t#standard free energy of formtion of ammonia (J/mol)\n",
      "\n",
      "# Calculations\n",
      "Ho = 2*Hf;\n",
      "Go = 2*Gf;\n",
      "alpha = 2*29.75 - 27.27 - 3*27.01;\n",
      "betta = (2*25.11 - 4.93 - 3*3.51)*10**-3;\n",
      "import math\n",
      "#Using eq. 9.46 (Page no. 412)\n",
      "del_H = Ho - alpha*To - (betta/2)*To**2;\n",
      "#Using eq. 9.48 (Page no. 413)\n",
      "A = -(Go - del_H + alpha*To*math.log(To) + (betta/2)*To**2)/(R*To)\n",
      "\n",
      "#Using eq. 9.47 and 9.48 (Page no. 412)\n",
      "K = math.e**((-del_H/(R*T)) + (alpha/R)*math.log(T) + (betta/(2*R))*T + A)\n",
      "G = del_H - alpha*T*math.log(T) -(betta/2)*T**2 - A*R*T;\n",
      "\n",
      "# Results\n",
      "print 'At 700 K'\n",
      "print ' Equilibrium constant is %3.2e'%K\n",
      "print ' Standard free energy change is %f J/mol'%G\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "At 700 K\n",
        " Equilibrium constant is 9.99e-05\n",
        " Standard free energy change is 53606.315911 J/mol\n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.10"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "# Variables\n",
      "T = 600.; \t\t\t#temperature in K\n",
      "R = 8.314; \t\t\t#ideal gas constant\n",
      "\n",
      "#Gibbs free energy at 600 K (J/mol K)\n",
      "Gc = -203.81; \t\t\t#for CO\n",
      "Gh = -136.39; \t\t\t#for hydrogen\n",
      "Gm = -249.83; \t\t\t#for methanol\n",
      "\n",
      "#Heats of formation at 298 K (J/mol)\n",
      "Hc = -110500.; \t\t\t#for CO\n",
      "Hm = -200700.; \t\t\t#for methanol\n",
      "import math\n",
      "\n",
      "# Calculations\n",
      "#To calculate equilibrium constant at 600 K\n",
      "Go = T*((Gm-Gc-(2*Gh)) + (1/T)*(Hm-Hc))\n",
      "\t\t\t#Using eq. 9.31 (Page no. 406)\n",
      "K = math.e**(-Go/(R*T))\n",
      "\n",
      "# Results\n",
      "print 'Equilibrium constant is %4.3e'%K\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Equilibrium constant is 1.018e-04\n"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.11"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "T = 500.; \t\t\t#temperature in K\n",
      "R = 8.314; \t\t\t#ideal gas constant\n",
      "\n",
      "#Free energy at 500 K (J/mol K)\n",
      "Fn = -177.5; \t\t\t#for nitrogen\n",
      "Fh = -116.9; \t\t\t#for hydrogen\n",
      "Fa = -176.9; \t\t\t#for ammonia\n",
      "\n",
      "#The function (Ho at 298 K - Ho at 0 K) [J/mol]\n",
      "Hn = 8669.; \t\t\t#for nitrogen\n",
      "Hh = 8468.; \t\t\t#for hydrogen\n",
      "Ha = 9920.; \t\t\t#for methanol\n",
      "\n",
      "#Free energy of formation at 298 K (J/mol)\n",
      "Hf = -46100.;\n",
      "import math\n",
      "#To calculate equilibrium constant at 500 K\n",
      "\n",
      "# Calculations\n",
      "#Using eq. 9.53 (Page no. 414)\n",
      "sum_F = (2*Fa - Fn - 3*Fh) - (2*Ha - Hn - 3*Hh)/T; \t\t\t#(J/mol K)\n",
      "#Using eq. 9.57 (Page no.415)\n",
      "Go = T*(sum_F + 2*Hf/T)\n",
      "K = math.e**(-Go/(R*T))\n",
      "\n",
      "# Results\n",
      "print 'Equilibrium constant is %f'%K\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Equilibrium constant is 0.108493\n"
       ]
      }
     ],
     "prompt_number": 25
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.12"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "P1 = 1.; \t\t\t#pressure (bar)\n",
      "P2 = 2.; \t\t\t#(bar)\n",
      "x1 = 0.15; \t\t\t#mol fraction of polymer at 1 bar\n",
      "x2 = 0.367; \t\t\t#mol fraction of polymer at 2 bar\n",
      "\n",
      "# Calculations\n",
      "import math\n",
      "n = round((math.log(x2/x1)+math.log(2))/(math.log(2)-math.log((1-x1)/(1-x2))))\n",
      "\n",
      "# Results\n",
      "print 'The value of n is %i'%n\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The value of n is 4\n"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.13"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from scipy.optimize import root\n",
      "\n",
      "# Variables\n",
      "#Reaction: N2 + 3H2 --> 2NH3\n",
      "K = 2*10**-4; \t\t\t#equilibrium constant of reaction\n",
      "P = 20; \t\t\t#(bar)\n",
      "\n",
      "# Calculations and Results\n",
      "#e(4-2e)/(1-e)**2 = 0.73485\n",
      "#e = poly(0,'e')\n",
      "def f(e):\n",
      "    return 2.73845*e**2 - 5.4697*e + 0.73485;\n",
      "x = root(f,0)\n",
      "\n",
      "print '(a) Percentage conversion is %f percent'%(x.x[0]*100)\n",
      "\n",
      "#(b)\n",
      "P = 200; \t\t\t#(bar)\n",
      "\n",
      "#e(4-2e)/(1-e)**2 = 7.3485\n",
      "\n",
      "def f2(e):\n",
      "    return  9.3485*e**2 - 18.697*e + 7.3485;\n",
      "x = root(f2,0)\n",
      "print ' (b) Percentage conversion is %f percent'%(x.x[0]*100)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(a) Percentage conversion is 14.485445 percent\n",
        " (b) Percentage conversion is 53.746561 percent\n"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.14"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "K = 1; \t\t\t#equilibrium constant for reaction\n",
      "\n",
      "# Calculations and Results\n",
      "e = 1./2;\n",
      "print '(a) Fractional dissociation of steam is %i percent'%(e*100)\n",
      "\n",
      "#On solving we get\n",
      "e = 1./2;\n",
      "print ' (b) After dilution fractional distillation of steam is %i percent'%(e*100)\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(a) Fractional dissociation of steam is 50 percent\n",
        " (b) After dilution fractional distillation of steam is 50 percent\n"
       ]
      }
     ],
     "prompt_number": 28
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.15"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from scipy.optimize import root\n",
      "\n",
      "# Variables\n",
      "K = 2.*10**-4; \t\t\t#equilibrium constant of reaction\n",
      "P = 20.; \t\t\t#pressure in bar\n",
      "\n",
      "\n",
      "\n",
      "def f(e):\n",
      "    return 1.3674*e**2 - 3.7348*e + 0.3674;\n",
      "x = root(f,0)\n",
      "\n",
      "# Results\n",
      "print 'Percentage coversion in presence of argon is %f percent'%(x.x[0]*100)\n",
      "print ' while in absence of argon is 14.48 percent' \t\t\t#From example 9.13\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Percentage coversion in presence of argon is 10.219587 percent\n",
        " while in absence of argon is 14.48 percent\n"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.16"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "P = 1.; \t\t\t#pressure in bar\n",
      "K = 1.; \t\t\t#equilibrium constant of reaction\n",
      "\n",
      "e)(2-e)] = K = 1% so\n",
      "\n",
      "e = 2./3;\n",
      "print '(a). The conversion of steam is %f percent'%(e*100)\n",
      "\n",
      "#(b). CO supplied is only 50% of the theoretical requirement\n",
      "#Mole fraction of components\n",
      "#CO: (0.5-e)/1.5\n",
      "#H20: (1-e)/1.5\n",
      "#CO2: e/1.5\n",
      "#H2: e/1.5\n",
      "\n",
      "#e**2/[(0.5-e)(1-e)] = K = 1\n",
      "#1.5e-0.5 = 1\n",
      "e = 0.5/1.5;\n",
      "print ' (b). Percentage conversion of steam is %f percent'%(e*100)\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(a). The conversion of steam is 66.666667 percent\n",
        " (b). Percentage conversion of steam is 33.333333 percent\n"
       ]
      }
     ],
     "prompt_number": 30
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.17"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "K = 1.; \t\t\t#equilibrium constant of reaction\n",
      "\n",
      "# Calculations\n",
      "e = 1./3;\n",
      "\n",
      "# Results\n",
      "print 'Percentage conversion of steam is %f percent'%(e*100)\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Percentage conversion of steam is 33.333333 percent\n"
       ]
      }
     ],
     "prompt_number": 31
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.18"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "#Reaction: CO(g) + 2H2(g) --> CH3OH(g)\n",
      "Kf = 4.9*10**-5;\n",
      "Kfi = 0.35;\n",
      "P = 300.; \t\t\t#pressure in bar\n",
      "n_CO = 25.;\n",
      "n_H2 = 55.;\n",
      "n_inert = 20.;\n",
      "v = -1-2+1; \t\t\t#change in number of moles in reaction\n",
      "\n",
      "# Calculations\n",
      "#Mole fractions in the equilibrium mixture\n",
      "#CO = (25-e)/(100-2e)\n",
      "#H2 = (55-2e)/(100-2e)\n",
      "#CH3OH = e/(100-2e)\n",
      "\n",
      "Ky = (Kf/Kfi)*P**(-v)\n",
      "\t\t\t#[e/(100-2e)]/[(25-e)/(100-2e)][(55-2e)/(100-2e)]**2 = Ky% so\n",
      "\n",
      "def f(e):\n",
      "    return  (4+4*Ky)*e**3 - (400+320*Ky)*e**2 + (10000+8525*Ky)*e - 75625*Ky\n",
      "\n",
      "x = root(f,0)\n",
      "conv = x.x[0]/n_CO; \t\t\t#first two roots are complex\n",
      "\n",
      "# Results\n",
      "print 'Percentage conversion of CO is %f percent'%(conv*100)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Percentage conversion of CO is 61.015734 percent\n"
       ]
      }
     ],
     "prompt_number": 32
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.19"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "#Reaction: 1/2N2 + 3/2H2 --> NH3\n",
      "Kp = 1.25*10**-2 ;\t\t\t#equilibrium constant\n",
      "P = 50; \t\t\t#pressure in bar\n",
      "v = 1-(3./2)-(1./2) \t\t\t#change in number of moles in reaction\n",
      "\n",
      "#Initial composition of gas mixture\n",
      "n_h = 60.;\n",
      "n_n = 20.;\n",
      "n_inert = 100-n_h-n_n;\n",
      "\n",
      "# Calculations\n",
      "\n",
      "Ky = Kp*(P**-v)\n",
      "#e/(100-e)/[(20-(e/2)]**1/2[{60-(3e/2)}/(100-e)]**3/2 = Ky\n",
      "#e = poly(0%'e'\n",
      "\n",
      "def f(e):\n",
      "    return (1.6875*Ky**2-1)*e**4 - (270*Ky**2+200)*e**3 + (16200*Ky**2-10000)*e**2 - (334800*Ky**2)*e + 4320000*Ky**2;\n",
      "x = root(f,0)\n",
      "e = x.x[0]\n",
      "\n",
      "#x(4) being the only positive root is the percentage conversion\n",
      "#Mole fractions in equilibrium mixture\n",
      "x_n = (20-(e/2))/(100-e)\n",
      "x_h = (60-3*(e/2))/(100-e)\n",
      "x_a = e/(100-e)\n",
      "x_inert = 1 - x_n - x_h - x_a;\n",
      "\n",
      "# Results\n",
      "print 'Composition of gas leaving the reactor is'\n",
      "print ' Nitrogen  : %f percent'%(x_n*100)\n",
      "print ' Hydrogen  : %f percent'%(x_h*100)\n",
      "print ' Ammonia   : %f percent'%(x_a*100)\n",
      "print ' Inert gas : %f percent'%(x_inert*100)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Composition of gas leaving the reactor is\n",
        " Nitrogen  : 17.048802 percent\n",
        " Hydrogen  : 51.146406 percent\n",
        " Ammonia   : 9.837327 percent\n",
        " Inert gas : 21.967465 percent\n"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.20"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "P = 85.; \t\t\t#pressure in bar\n",
      "n_e = 0.015; \t\t\t#mol percent of ethanol\n",
      "n_w = 0.95; \t\t\t#mole percent of water\n",
      "n_a = 0.48; \t\t\t#mol percent of ethylene in vapour phase\n",
      "M = 18.; \t\t\t#molecular mass of water\n",
      "fc = 0.9; \t\t\t#fugacity coeffecient for ethylene\n",
      "\n",
      "#To evaluate the equilibrium constant\n",
      "#K = a_c/(a_a*a_b)\n",
      "# Calculations\n",
      "m_e = n_e/(n_w*M*10**-3) \t\t\t#mol/kg water\n",
      "a_c = m_e;\n",
      "fa = fc*n_a*P; \t\t\t#bar\n",
      "a_a = fa;\n",
      "\n",
      "#Since mol fraction of water is close to unity% so fugacity coeffecient of water is assumed to be 1\n",
      "a_b = n_w;\n",
      "K = a_c/(a_a*a_b)\n",
      "\n",
      "# Results\n",
      "print 'The equilibrium constant is %5.4e (mol C2H4)/(kg water bar)'%K\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The equilibrium constant is 2.5146e-02 (mol C2H4)/(kg water bar)\n"
       ]
      }
     ],
     "prompt_number": 34
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.21"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "T = 1000.; \t\t\t#temperature of reaction in K\n",
      "P = 1.; \t\t\t#pressure in bar\n",
      "R = 8.314; \t\t\t#ideal gas constant\n",
      "import math\n",
      "\n",
      "#Function for standard free energy of the reaction\n",
      "def G(T):\n",
      "    y = 1.8856*10**5 - 243.42*T + 11.8478*T*math.log(T) - 3.1045*10**-3*T**2 + 1.7271*10**-6*T**3 - (4.1784*10**5)/T\n",
      "    return y\n",
      "\n",
      "# Calculations and Results\n",
      "K = math.e**(-Go/(R*T))\n",
      "#Using eq. 9.75 (Page no. 432)\n",
      "p_CO2 = K; \t\t\t#decomposition pressure\n",
      "print 'Decomposition pressure of limestone at 1000 K s %f bar'%p_CO2\n",
      "\n",
      "#At pressure = 1 bar\n",
      "K = 1.;\n",
      "Go = 0.; \t\t\t#since K = 1\n",
      "\n",
      "T = 1160.; \t\t\t#assumed temperature (K)\n",
      "flag = 1.;\n",
      "while(flag==1):\n",
      "    res = round(G(T))\n",
      "    if(res<=0):\n",
      "        flag = 0;\n",
      "    else:\n",
      "        T = T+1;\n",
      "\n",
      "print 'Decomposition temperature at 1 bar is %i K'%T\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Decomposition pressure of limestone at 1000 K s 0.048344 bar\n",
        "Decomposition temperature at 1 bar is 1169 K\n"
       ]
      }
     ],
     "prompt_number": 35
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.22"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "K = 0.403; \t\t\t#equilibrium constant of reaction\n",
      "T = 1200.; \t\t\t#temperature of reaction (K)\n",
      "To = 273.; \t\t\t#standard temperature (K)\n",
      "Vo = 22.4*10**-3; \t\t\t#molar volume at STP \n",
      "M = 55.8; \t\t\t#molecular mass of iron\n",
      "n = 100.; \t\t\t#moles of gas entering\n",
      "n_C = 20.; \t\t\t#moles of carbon mono oxide\n",
      "n_N = 80.; \t\t\t#moles of nitrogen\n",
      "\n",
      "\n",
      "# Calculations\n",
      "#Let e be the extent of reaction\n",
      "#Mole fractions in equilibrium mixture\n",
      "#CO = (20-e)/100\n",
      "#CO2 = e/100\n",
      "#e/(20-e) = K\n",
      "e = (20*K)/(1+K)\n",
      "n_CO2 = e; \t\t\t#moles of CO2 at equilibrium\n",
      "n_Fe = n_CO2; \t\t\t#by stoichiometry\n",
      "V = (n*Vo*T)/To; \t\t\t#volume of 100 mol of gas at 1200 K and 1 bar\n",
      "\n",
      "#Let m be iron produced per 100 m**3 gas\n",
      "m = (n_Fe*100*M)/V;\n",
      "\n",
      "# Results\n",
      "print 'Iron produced per 100 cubic m of gas is %f kg'%(m/1000)\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Iron produced per 100 cubic m of gas is 3.255704 kg\n"
       ]
      }
     ],
     "prompt_number": 36
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.23"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from scipy.optimize import fsolve\n",
      "\n",
      "# Variables\n",
      "P = 1.; \t\t\t#pressure in bar\n",
      "K1 = 0.574; \t\t\t#equilibrium constant for eq. 9.88 (Page no. 437)\n",
      "K2 = 2.21; \t\t\t#equilibrium constant for eq. 9.89 (Page no. 437)\n",
      "\n",
      "v1 = 1+3-1-1;\n",
      "v2 = 1+1-1-1;\n",
      "Ky1 = K1*P**-v1;\n",
      "Ky2 = K2*P**-v2;\n",
      "\n",
      "# Calculations\n",
      "#mole fractions in equilibrium mixture are:\n",
      "#CH4: (1-e1)/(6+2e1)\n",
      "#H2O: (5-e1-e2)/(6+2e1)\n",
      "#CO: (e1-e2)/(6+2e1)\n",
      "#H2: (3e1+e2)/(6+2e1)\n",
      "#CO2: e2/(6+2e1)\n",
      "\n",
      "#For 1st reaction:\n",
      "#Ky1 = [(e1-e2)(3e1+e2)**3]/[(1-e1)(5-e1-e2)(6+2e1)**2]\n",
      "#For 2nd reaction:\n",
      "#Ky2 = [e2(3e1+e2)]/[(e1-e2)(5-e1-e2)]\n",
      "#on solving% we get:\n",
      "\n",
      "def f2(e):\n",
      "    f_1 = ((e[0]-e[1])*(3*e[0]+e[1])**3)/((1-e[0])*(5-e[0]-e[1])*(6+2*e[0])**2)-Ky1\n",
      "    f_2 = (e[1]*(3*e[0]+e[1]))/((e[0]-e[1])*(5-e[0]-e[1]))-Ky2\n",
      "    y = [f_1,f_2]\n",
      "    return y\n",
      "eo = [0.9,0.6]; \t\t\t#initial guesses\n",
      "e = fsolve(f2,eo)\n",
      "\n",
      "\t\t\t#Mole fraction of components:\n",
      "n_m = (1-e[0])/(6+2*e[0])\n",
      "n_w = (5-e[0]-e[1])/(6+2*e[0])\n",
      "n_CO = (e[0]-e[1])/(6+2*e[0])\n",
      "n_h = (3*e[0]+e[1])/(6+2*e[0])\n",
      "n_c = e[1]/(6+2*e[0])\n",
      "\n",
      "# Results\n",
      "print 'Mole fraction of the components are:'\n",
      "print ' Methane = %f'%n_m\n",
      "print ' Water = %f'%n_w\n",
      "print ' Carbon monoxide = %f'% n_CO\n",
      "print ' Hydrogen = %f'%n_h\n",
      "print ' Carbon dioxide = %f'%n_c\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Mole fraction of the components are:\n",
        " Methane = 0.011240\n",
        " Water = 0.441597\n",
        " Carbon monoxide = 0.035687\n",
        " Hydrogen = 0.430593\n",
        " Carbon dioxide = 0.080883\n"
       ]
      }
     ],
     "prompt_number": 37
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 9.24"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "# Variables\n",
      "#A system consisting of CO% CO2% H2% H2O% CH4\n",
      "\n",
      "\n",
      "r = 2.;\n",
      "C = 5.; \t\t\t#no. of components\n",
      "pi = 1.; \t\t\t#no. of phases\n",
      "\n",
      "# Calculations\n",
      "#From eq. 9.90 (Page no. 438)\n",
      "F = C-pi-r+2;\n",
      "\n",
      "# Results\n",
      "print 'The number of degrees of freedom are %i'%F\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The number of degrees of freedom are 4\n"
       ]
      }
     ],
     "prompt_number": 38
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}