{
 "metadata": {
  "name": "",
  "signature": "sha256:45cc28c15d520b01f5e80bb34453647affc5cb5c7ebd0520e71c71e74dcb84cd"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 10 : Vapor Liquid Equilibrium VLE at High Pressures"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 10.1  Page: 260"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "import math \n",
      "\n",
      "# Variables\n",
      "P = 100.                #[psia] Bubble point pressure\n",
      "x_ethane = 0.10         # Mole fraction of ethane in liquid phase\n",
      "x_hepmath_tane = (1-x_ethane)\n",
      "\n",
      "# a) From figure 10.7( page 260 ) given in the book\n",
      "# We read the chart to get the bubble-point temperature\n",
      "# The dew point curve for 100 psia crosses the 10 mol% ethane line at about temperature \n",
      "T1 = 165.               #[C]\n",
      "# Now, we horizontally from that intersection point to the dew-point curve, finding the vapor phase composition of ethane \n",
      "y1_e = 0.92\n",
      "y1_h = (1- y1_e)\n",
      "\n",
      "# b) By Raoult's law, we use a trial and error procedureon the temperature\n",
      "# Antoine equation consmath.tants for ethanol are given \n",
      "A_e = 6.80267\n",
      "B_e = 656.4028\n",
      "C_e = 255.99\n",
      "\n",
      "# and that for n-hepmath_tane are\n",
      "A_h = 6.9024\n",
      "B_h = 1268.115\n",
      "C_h = 216.9\n",
      "\n",
      "# Antoine equation is given by\n",
      "# (math.log10p) = (A - B/(T+C))\n",
      "T = 50.                     #[C]\n",
      "err = 1.\n",
      " \n",
      "# Calculations\n",
      "while err > 10**(-4):\n",
      "    p1_e = (10**(A_e - B_e/(C_e + T)))*(14.7/760)\n",
      "    p1_h = (10**(A_h - B_h/(C_h + T)))*(14.7/760)\n",
      "    y2_e = p1_e*x_ethane/P\n",
      "    y2_h = p1_h*x_hepmath_tane/P\n",
      "    err = abs((y2_e + y2_h) - 1)\n",
      "    T = T + 0.0001\n",
      "\n",
      "# Changing the temperature in deg F \n",
      "T2 = T*9./5 + 32            #[F] Bubble-point temperature\n",
      "\n",
      "# c) In this method, we use L-R rule, instead of simple Raoult's law\n",
      "# So,\n",
      "# y_i = (x_i*p_i)/(v_i*P)\n",
      "# Where calculated values of v_i from EOS are given in the table 10.A and are \n",
      "v_e = 0.950                     # For ethane\n",
      "v_h = 0.459                     # For n-hepmath_tane\n",
      "\n",
      "# We again use trial and error on the temperature\n",
      "# Let us assume the initial temperature \n",
      "Ti = 50.                       #[C]\n",
      "err = 1\n",
      " \n",
      "while err > 10**(-4):\n",
      "    p2_e = (10**(A_e - B_e/(C_e + Ti)))*(14.7/760)\n",
      "    p2_h = (10**(A_h - B_h/(C_h + Ti)))*(14.7/760)\n",
      "    y3_e = p2_e*x_ethane/(P*v_e)\n",
      "    y3_h = p2_h*x_hepmath_tane/(P*v_h)\n",
      "    err = abs((y3_e + y3_h) - 1)\n",
      "    Ti = Ti + 0.0001\n",
      "\n",
      "# Changing the temperature in deg F \n",
      "T3 = Ti*9./5 + 32               #[F] Bubble-point temperature\n",
      "\n",
      "# Results\n",
      "print \" The results are summarized in the following table:\"\n",
      "print \" Variable \\t Values calculated from\\t Values calculated from \\t Values calculated  \"\n",
      "print \"               \\t from figure 10.7  \\t Raoult''s law \\t\\t\\t from L-R rule\"\n",
      "print \" Tdeg F) \\t %f \\t\\t %f \\t\\t\\t    %f\"%(T1,T2,T3)\n",
      "print \" y_ethane \\t %f \\t\\t %f \\t\\t\\t    %f\"%(y1_e,y2_e,y3_e)\n",
      "print \" y_heptane \\t %f \\t\\t %f \\t\\t\\t    %f\"%(y1_h,y2_h,y3_h)\n",
      "print \"\\nWhere T is boiling point temperature\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The results are summarized in the following table:\n",
        " Variable \t Values calculated from\t Values calculated from \t Values calculated  \n",
        "               \t from figure 10.7  \t Raoult''s law \t\t\t from L-R rule\n",
        " Tdeg F) \t 165.000000 \t\t 133.014380 \t\t\t    124.179620\n",
        " y_ethane \t 0.920000 \t\t 0.968397 \t\t\t    0.943469\n",
        " y_heptane \t 0.080000 \t\t 0.031504 \t\t\t    0.056431\n",
        "\n",
        "Where T is boiling point temperature\n"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      " Example 10.2  Page: 262\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import math \n",
      "\n",
      "# Variables\n",
      "P = 800.            #[psia] Bubble point pressure\n",
      "x_ethane = 0.60     # Mole fraction of ethane in liquid phase\n",
      "x_hepmath_tane = (1-x_ethane)\n",
      "\n",
      "# a) From figure 10.7( page 260 ) given in the book\n",
      "# We read the chart to get the bubble-point temperature\n",
      "# The dew point curve for 800 psia crosses the 60 mol% ethane line at about temperature \n",
      "# T1 = 165\n",
      "# Now, we horizontally from that intersection point to the dew-point curve, finding the vapor phase composition of ethane \n",
      "# y1_e = 0.95\n",
      "# But, by linear interpolation in the experimental data on which Figure 10.7 is based we make a slightly more reliable estimate and get \n",
      "T1 = 209.           #[F]\n",
      "y1_e = 0.945\n",
      "y1_h = (1- y1_e)\n",
      "\n",
      "# b) By Raoult's law, we use a trial and error procedureon the temperature\n",
      "# Antoine equation consmath.tants for ethanol are given \n",
      "A_e = 6.80267\n",
      "B_e = 656.4028\n",
      "C_e = 255.99\n",
      "\n",
      "# and that for n-hepmath_tane are\n",
      "A_h = 6.9024\n",
      "B_h = 1268.115\n",
      "C_h = 216.9\n",
      "\n",
      "# Antoine equation is given by\n",
      "# (math.log10p) = (A - B/(T+C))\n",
      "T = 50.             #[C]\n",
      "err = 1.\n",
      " \n",
      "# Calculations\n",
      "while err > 10**(-4):\n",
      "    p1_e = (10**(A_e - B_e/(C_e + T)))*(14.7/760)\n",
      "    p1_h = (10**(A_h - B_h/(C_h + T)))*(14.7/760)\n",
      "    y2_e = p1_e*x_ethane/P\n",
      "    y2_h = p1_h*x_hepmath_tane/P\n",
      "    err = abs((y2_e + y2_h) - 1)\n",
      "    T = T + 0.0001\n",
      "\n",
      "# Changing the temperature in deg F \n",
      "T2 = T*9./5 + 32            #[F] Bubble-point temperature\n",
      "\n",
      "# c) In this method, we use L-R rule, instead of simple Raoult's law\n",
      "# So,\n",
      "# y_i = (x_i*p_i)/(v_i*P)\n",
      "# Where calculated values of v_i from EOS are given  \n",
      "v_e = 0.6290642             # For ethane\n",
      "v_h = 0.0010113             # For n-hepmath_tane\n",
      "\n",
      "# We again use trial and error on the temperature\n",
      "# Let us assume the initial temperature \n",
      "Ti = 10.                    #[C]\n",
      "err = 1.\n",
      " \n",
      "while err > 10**(-4):\n",
      "    p2_e = (10**(A_e - B_e/(C_e + Ti)))*(14.7/760)\n",
      "    p2_h = (10**(A_h - B_h/(C_h + Ti)))*(14.7/760)\n",
      "    y3_e = p2_e*x_ethane/(P*v_e)\n",
      "    y3_h = p2_h*x_hepmath_tane/(P*v_h)\n",
      "    err = abs((y3_e + y3_h) - 1)\n",
      "    Ti = Ti + 0.0001\n",
      "\n",
      "# Changing the temperature in deg F \n",
      "T3 = Ti*9./5 + 32           #[F] Bubble-point temperature\n",
      "\n",
      "# Results\n",
      "print \" The results are summarized in the following table:\"\n",
      "print \" \\t   Variable \\t\\t Values calculated from\\t Values calculated from Values calculated\"\n",
      "print \" \\t\\t\\t\\t from figure 10.7 \\t Raoult''s law \\t\\t from L-R rule\"\n",
      "print \" \\n\\t  Tdeg F \\t\\t %f \\t\\t %f \\t\\t    %f\"%(T1,T2,T3)\n",
      "print \" \\t  y_ethane \\t\\t %f \\t\\t %f \\t\\t    %f\"%(y1_e,y2_e,y3_e)\n",
      "print \" \\t  y_heptane \\t\\t %f \\t\\t %f \\t\\t    %f\"%(y1_h,y2_h,y3_h)\n",
      "print \"\\nWhere T is boiling point temperature\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The results are summarized in the following table:\n",
        " \t   Variable \t\t Values calculated from\t Values calculated from Values calculated\n",
        " \t\t\t\t from figure 10.7 \t Raoult''s law \t\t from L-R rule\n",
        " \n",
        "\t  Tdeg F \t\t 209.000000 \t\t 172.210640 \t\t    70.854980\n",
        " \t  y_ethane \t\t 0.945000 \t\t 0.996044 \t\t    0.632080\n",
        " \t  y_heptane \t\t 0.055000 \t\t 0.003856 \t\t    0.367822\n",
        "\n",
        "Where T is boiling point temperature\n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      " Example 10.3  Page: 262\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "\n",
      "from scipy.optimize import fsolve \n",
      "import math \n",
      "\n",
      "# Variables\n",
      "# The initial data for this example is same as that of example 10.2, i.e.\n",
      "P = 800.             #[psia] Bubble point pressure\n",
      "x_e = 0.60           # Mole fraction of ethane in liquid phase\n",
      "x_h = (1-x_e)        # Mole fraction of n-hepmath.tane in the liquid phase\n",
      "R = 0.08314         #( L*bar/(mol*K)) Universal gas consmath.tant \n",
      "\n",
      "# Changing the pressure in bar\n",
      "Pb = (800/14.7)*(1.01325)           #[bar]\n",
      "\n",
      "# In this problem we will denote ethane by 'e' and that to n-hepmath.tane by 'h'\n",
      "# From table A.1 ( page 417 ) given in the book, critical temperatures of ethane and hepmath.tane are \n",
      "T_c_e = 305.3           #[K]\n",
      "T_c_h = 540.2           #[K]\n",
      "\n",
      "# and critical pressures are\n",
      "P_c_e = 48.72           #[bar]\n",
      "P_c_h = 27.40           #[bar]\n",
      "\n",
      "# also the accentric facors are \n",
      "w_e = 0.1\n",
      "w_h = 0.35\n",
      "\n",
      "# Thus we have\n",
      "P_r_e = Pb/P_c_e\n",
      "P_r_h = Pb/P_c_h\n",
      "\n",
      "# Now from equations (F.13) and (F.14) ( page 459 ) given in the book we have\n",
      "# A_e = 0.42747 + ( 1 + (0.480 + 1.574*w_e - 0.17*w_e**(2))*( 1 - T_r_e**(0.5)))**(2)*(P_r_e/T_r_e**(2))\n",
      "# A_h = 0.42747 + ( 1 + (0.480 + 1.574*w_h - 0.17*w_h**(2))*( 1 - T_r_h**(0.5)))**(2)*(P_r_h/T_r_h**(2))\n",
      "# and\n",
      "# B_e = 0.08664*(P_r_e/T_r_e)\n",
      "# B_h = 0.08664*(P_r_h/T_r_h)\n",
      "\n",
      "# We will take the help trial and error method both on Temperature and the vapor phase composition of ethane\n",
      "# Let us assume the starting temperature 200 deg F. Changing this temperature in K\n",
      "T = (200-32)*5./9 + 273.15           #[K]\n",
      "err = 1\n",
      "\n",
      "# Calculations\n",
      "while err > 10**(-4):\n",
      "    T_r_e = T/T_c_e\n",
      "    T_r_h = T/T_c_h\n",
      "    A_e = 0.42747*( 1 + (0.480 + 1.574*w_e - 0.17*w_e**(2))*( 1 - T_r_e**(0.5)))**(2)*(P_r_e/T_r_e**(2))\n",
      "    A_h = 0.42747*( 1 + (0.480 + 1.574*w_h - 0.17*w_h**(2))*( 1 - T_r_h**(0.5)))**(2)*(P_r_h/T_r_h**(2))\n",
      "    \n",
      "    B_e = 0.08664*(P_r_e/T_r_e)\n",
      "    B_h = 0.08664*(P_r_h/T_r_h)\n",
      "    \n",
      "    # Now we will take the starting value of vapor phase composition of ethane as \n",
      "    y_e = 0.9\n",
      "    err1 = 1\n",
      "    \n",
      "    while err1 > 10**(-6):\n",
      "        # Now value of A_mix and B_mix for both liquid and vapor phase are calculated as\n",
      "        \n",
      "        A_mix_l = (x_e*math.sqrt(A_e) + x_h*math.sqrt(A_h))**(2)        # For liquid phase\n",
      "        A_mix_v = (y_e*math.sqrt(A_e) + (1 - y_e)*math.sqrt(A_h))**(2)  # For vapor phase\n",
      "        B_mix_l = (x_e*B_e + x_h*B_h)                                   # For liquid \n",
      "        B_mix_v = (y_e*B_e + (1 - y_e)*B_h)                             # For liquid \n",
      "\n",
      "        def f(z1): \n",
      "            return  z1**(3) - z1**(2) + z1*(A_mix_l - B_mix_l - B_mix_l**(2)) - A_mix_l*B_mix_l\n",
      "        z_l = fsolve(f,0.2)\n",
      "        # and\n",
      "        def g(z2): \n",
      "            return  z2**(3) - z2**(2) + z2*(A_mix_v - B_mix_v - B_mix_v**(2)) - A_mix_v*B_mix_v\n",
      "        z_v = fsolve(g,0.3)\n",
      "        # Now\n",
      "        phi_el = B_e/B_mix_l*( z_l - 1) - math.log(z_l - B_mix_l) - (A_mix_l/B_mix_l)*(2*math.sqrt(A_e/A_mix_l)-B_e/B_mix_l)*math.log(1-B_mix_l/z_l)\n",
      "        phi_hl = B_h/B_mix_l*( z_l - 1) - math.log(z_l - B_mix_l) - (A_mix_l/B_mix_l)*(2*math.sqrt(A_h/A_mix_l)-B_h/B_mix_l)*math.log(1-B_mix_l/z_l)\n",
      "        phi_ev = B_e/B_mix_v*( z_v - 1) - math.log(z_v - B_mix_v) - (A_mix_v/B_mix_v)*(2*math.sqrt(A_e/A_mix_v)-B_e/B_mix_v)*math.log(1-B_mix_v/z_v)\n",
      "        phi_hv = B_h/B_mix_v*( z_v - 1) - math.log(z_v - B_mix_v) - (A_mix_v/B_mix_v)*(2*math.sqrt(A_h/A_mix_v)-B_h/B_mix_v)*math.log(1-B_mix_v/z_v)\n",
      "        K_e = phi_el/phi_ev\n",
      "        K_h = phi_hl/phi_hv\n",
      "        y_e1 = K_e*x_e\n",
      "        y_h1 = K_h*x_h\n",
      "        err1 =abs((y_e1 - y_e))\n",
      "        y_e = y_e1\n",
      "\n",
      "    err = abs((y_e1 + y_h1) -1)\n",
      "    T = T + 0.1\n",
      "\n",
      "\n",
      "# Changing the temperature in deg F, we have \n",
      "Tf = ( T - 273.15)*9./5 + 32         #[F]\n",
      "\n",
      "# Results\n",
      "print \" Bubble point of the given ethanol and n-hepmath.tane mixture at 800 psia is %f deg F\"%(Tf)\n",
      "print \" Amount of ethanol in the vapour phase of the mixture at the given condition is %f \"%(y_e1)\n",
      "print \" Amount of n-heptane in the vapour phase of the mixture at the given condition is %f \"%(y_h1)\n",
      "\n",
      "# Answers may vary because of rounding error."
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Bubble point of the given ethanol and n-hepmath.tane mixture at 800 psia is 200.180000 deg F\n",
        " Amount of ethanol in the vapour phase of the mixture at the given condition is 0.599997 \n",
        " Amount of n-heptane in the vapour phase of the mixture at the given condition is 0.399997 \n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stderr",
       "text": [
        "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py:227: RuntimeWarning: The iteration is not making good progress, as measured by the \n",
        "  improvement from the last five Jacobian evaluations.\n",
        "  warnings.warn(msg, RuntimeWarning)\n",
        "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py:227: RuntimeWarning: The iteration is not making good progress, as measured by the \n",
        "  improvement from the last ten iterations.\n",
        "  warnings.warn(msg, RuntimeWarning)\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}