Example of A Chemical Engineering Problem Solved Using Python
Example of A Chemical Engineering Problem Solved Using Python
Find the specific volume of n-butane at 500 ºK and 18 atm using the Redlich-Kwong
equation of state.
Step 1: First, you need to define the function that will calculate the f(x), here specvol(v),
given the temperature, pressure, and thermodynamic properties. The file is shown below.
def specvol(v):
# in K, atm, l/gmol
# for n-butane
Tc = 425.2
pc = 37.5
T = 393.3
p = 16.6
R = 0.08206
aRK = 0.42748 * (R * Tc) ** 2 / pc
aRK = aRK * (Tc / T) ** 0.5
bRK = 0.08664 * (R * Tc / pc)
return p * v ** 3 - R * T * v ** 2 + (aRK - p * bRK ** 2 - R * T * bRK) * v - aRK * bRK
This function, called specvol, defines the problem you wish to solve.
Step 2: To test the function specvol you issue the command:
print(specvol(2))
and get 25.98, which is correct. The specvol function causes Python to compute the value of
the function specvol when v = 2. You should check these results line by line, especially the
calculation of aRK, bRK, and y (just copy the code except for the return statement and
calculate aRK and bRK with a calculator.)
Step 3: Next you issue the command:
v = fsolve(specvol,2)
print(v)
and get 1.5064. In specvol the 2 is an initial guess of the answer. To check, you might
evaluate the function to find how close to zero f(v) is.
print (specvol(v))
and get 1.8e-15. Of course you expect this to be zero (or very close to zero) because you
expect Python to work properly. If Python can’t find a solution, it will tell you. If you use an
initial guess of 0.2, you might get the specific volume of the liquid rather than the gas. Python
gives
0.18147.
For low pressures, where the gas is ideal, the compressibility factor will be close to 1.0. As
the pressure increases, it will change. Thus, the results will indicate the pressure region where
the ideal gas is no longer a good assumption. The following code solves for the Redlich-
Kwong, Redlich-Kwong-Soave, and Peng-Robinson equations of state and plots the
compressibility factor versus pressure as shown in the Figure.
from scipy . optimize import fsolve
import numpy as np
import pylab as plt
# n-butane Redlich-Kwong,
def specvolRK(v, p):
# in K, atm, l/gmol
# for n-butane
Tc = 425.2
pc = 37.5
T = 500
R = 0.08206
aRK = 0.42748 * (R * Tc) ** 2 / pc
aRK = aRK * (Tc / T) ** 0.5
bRK = 0.08664 * (R * Tc / pc)
return p * v ** 3 - R * T * v ** 2 + (aRK - p * bRK ** 2 - R * T * bRK) * v - aRK * bRK
# n-butane Redlich-Kwong-Soave
def specvolRKS(v, p):
# in K, atm, l/gmol
# for n-butane
Tc = 425.2
pc = 37.5
T = 500
R = 0.08206
acentric = 0.193
mRKS = 0.480 + (1.574 - 0.176*acentric)*acentric
alphaRKS = (1 + mRKS *(1-(T/Tc)**0.5)) ** 2
aRKS = 0.42748 * alphaRKS * (R * Tc) ** 2 / pc
bRKS = 0.08664 * (R * Tc / pc)
return p * v ** 3 - R * T * v ** 2 + (aRKS - p * bRKS ** 2 - R * T * bRKS) * v - aRKS *
bRKS
# n-butane Peng-Robinson
def specvolPR(v, p):
# in K, atm, l/gmol
# for n-butane
Tc = 425.2
pc = 37.5
T = 500
R = 0.08206
acentric = 0.193
mPR = 0.37363 + (1.54226 - 0.26992*acentric)*acentric
alphaPR = (1 + mPR *(1-(T/Tc)**0.5)) ** 2
aPR = 0.45724 * alphaPR * (R * Tc) ** 2 / pc
bPR = 0.07780 * (R * Tc / pc)
return p*v**3+(bPR*p - R*T)*v**2+(aPR- *p*bPR**2- *R*T*bPR)*v +
(p*bPR**3 + R*T*bPR**2-aPR*bPR)
T = 500
R = 0.08206
pressure = np.arange(1, 27, 5)
print(pressure)
print(pressure[0])
print(pressure[5])
zcompRK = np.zeros(6,dtype=float)
zcompRKS = np.zeros(6,dtype=float)
zcompPR = np.zeros(6,dtype=float)
print(zcompRK)
for i in range(0, 6, 1):
p = pressure[i]
guess = R*T/p
v = fsolve(specvolRK, guess, p)
z = p * v / (R * T)
zcompRK[i] = z
v = fsolve(specvolRKS,v,p)
z = p * v / (R * T)
zcompRKS[i] = z
v = fsolve(specvolPR,v,p)
z = p * v / (R * T)
zcompPR[i] = z
print(zcompRK)
print(zcompRKS)
print(zcompPR)
plt . plot (pressure,zcompRK,'o-g',label='Redlich-Kwong')
plt . plot (pressure,zcompRKS,'x-b',label='Redlich-Kwong-Soave')
plt . plot (pressure,zcompPR,'s-r',label='Peng-Robinson')
plt . legend(loc='best)')
plt . xlabel('Pressure (atm)')
plt . ylabel('Z')
plt . title ('n-Butane')
plt . show()