Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

SSRG International Journal of Recent Engineering Science Volume 8 Issue 3, 12-22, May-June, 2021

ISSN: 2349 – 7157 /doi:10.14445/23497157/IJRES-V8I3P103 © 2021 Seventh Sense Research Group®

Newton’s Method Cubic Equation of State C++


Source Code for Iterative Volume Computation
Abdulhalim Musa Abubakar#1, Ahmad Abubakar Mustapha#2
#1
Academic Staff, Department of Chemical Engineering, Modibbo Adama University of Science and Technology
(MAUTECH), Girei LGA, Yola, Nigeria
#2
M. Tech, Department of Computer Science & Engineering, Vigan’s Foundation for Science, Technology &
Research (VFSTR) (Deemed to be University), Vadlamudi, Guntur, A.P., India
Received Date: 04 May 2021
Revised Date: 06 June 2021
Accepted Date: 11 June 2021

Abstract - This write-up enumerates the basic cubic c) Third class EOS which are non-analytical EOS that are
equations of state used for pressure, volume and temperature highly constrained for some specific fluids. These equations
determination for pure substances. It however focuses on of states, especially those requiring iteration to determine a
developing a Dev C++ program for molar volume given parameter can be difficult to handle. Numerical
computation. Since molar volume in all cubic equations of analysis can be applied here; and one of such method is
state appears unsolvable due to their non-linear nature, Newton’s Method. The methods starts by taking an initial
numerical solution methods, one of which is Newton- value or guess to get a new approximation to the root. These
Raphson method can be utilize to estimate the root of such other new value would then serve as initial to get the next
polynomial equations. The 257 line of code running from and so on. However, doing this manually might take time,
Figure 1 to Figure 6, if executed, will return the molar especially if it is not converging quickly. A program can be
volume to some certain iteration over a tolerance error written to forever prevent this grueling molar volume
specified. The program is a 4-in-1 calculator. The user is calculations. Example of such programming languages is
capable of working with a desired equation of state at a time. C++.
User should note that the source code converges for either
large or small error specified. The program did not however include a write-up that
evaluate, P, T and number of moles (n) for the cubic EOS as
Keywords – C++ Program, Cubic Equations of State, these parameters don’t require iterations. Where molar
Equation of state, Newton’s Method, Iteration volume is substituted with (V/n), the cubic EOS, an equation
showing number of moles (n) with up to degree 3 is seen.
I. INTRODUCTION Such equation too requires iteration. Temperature under
An Equation of State (EOS) is a semi-empirical functional Redlich-Kwong EOS would as well require iterations.
relationship between pressure (P), volume (V) and Deiters, et al (2014), wrote on “Calculation of Densities from
temperature (T) of a pure substance (Sahay, Edison and Cubic Equations of State: Revisited” but did not however
Mohamed). The simplest EOS is the ideal gas law itself. As capture the programming aspect of finding the densities or
per EOS, every new equation is theoretically believed to be molar volume of the EOS.
superior over those that precedes it. One equation is best for
calculating density, but not for vapor pressure, while another II. COMPARISON OF CUBIC EOS
is very accurate for predicting vapor pressure, but not for Cubic equations of state (CEOS) are widely used in phase-
density. There are different types of EOS which fall into equilibrium calculations because of their simplicity and
three categories (Mansour): a) First class EOS are basically accuracy (Soedarto). The cubic equations of state (CEOS)
cubic equation of state. The cubic equations of state such as such as Van der Waals, Redlich-Kwong, Soave, and Peng-
the Van der Waals, Redlich and Kwong, Soave-Redlich- Robinson are simple models that have been widely used in
Kwong, and Peng-Robinson equations give reasonable the oil industry and also to describe the state of reservoir
results for the thermodynamic behavior of real fluids; b) fluids at given conditions (Mansour). Table 1 displays the
Second class EOS are non-cubic. They provide accurate four CEOS and their constant parameter expressions
results for both vapor and liquid phases, of which Benedict et (Himmelblau and Riggs):
al. equation is a good example for this class of equations; and

This is an open access article under the CC BY-NC-ND license (http://creativecommons.org/licenses/by-nc-nd/4.0/)


Abdulhalim Musa Abubakar et al. / IJRES, 8(3), 12-22, 2021

Table 1: Cubic Equations of State


Van der Waals EOS Redlich-Kwong EOS
𝑎
(𝑃 + ̂ 2) (𝑉̂ − 𝑏) = 𝑅𝑇 𝑎
𝑉
[𝑃 + 1 ] (𝑉̂ − 𝑏) = 𝑅𝑇
27 𝑅 2 𝑇𝑐 2 𝑅𝑇𝑐 𝑇2 𝑉̂(𝑉̂ + 𝑏)
where 𝑎 = 64 𝑃𝑐
and 𝑏 = 8𝑃
𝑐
𝑅 2 𝑇𝑐 2.5
where 𝑎 = 0.42748 𝑃𝑐
and
𝑅𝑇𝑐
𝑏 = 0.08664 𝑃𝑐

Soave Redlich-Kwong EOS Peng-Robinson EOS


𝑅𝑇 𝑎𝛼 𝑅𝑇 𝑎𝛼
𝑃= − 𝑃= −
̂
𝑉 − 𝑏 𝑉 (𝑉̂ + 𝑏)
̂ ̂
𝑉 − 𝑏 𝑉(𝑉 + 𝑏) + 𝑏(𝑉̂ − 𝑏)
̂ ̂

𝑅 2 𝑇𝑐 2 𝑅 2 𝑇𝑐 2
where 𝑎 = 0.42747 𝑃𝑐
; where 𝑎 = 0.45724 𝑃𝑐
and
𝑅𝑇𝑐 𝑇 𝑅𝑇𝑐
𝑏 = 0.08664 and 𝑇𝑟 = 𝑇 𝑏 = 0.07780
𝑃𝑐 𝑐 𝑃𝑐

2
𝛼 = [1 + (0.48508 + 1.55171𝑤 − 0.15613𝑤 2 )(1 1
𝑇
2 𝛼 = [1 + 𝑘 (1 − 𝑇𝑟 2 )] where 𝑇𝑟 = 𝑇
0.5 𝑐
− 𝑇𝑟 )]
𝑘 = 0.37464 + 1.54226𝑤 − 0.26992𝑤 2

The van der Waals EOS is the basis of various EOS. It is not temperature, pressure and composition, is needed for the
capable of predicting phase equilibrium accurately. Redlich- mixing rules; and d) PENG EOS should be applicable in
Kwong EOS needs critical temperature and pressure for P, V natural gas processes. The PENG EOS provided results
or T calculations. This equation is not accurate enough for similar to the SRK EOS, although it is generally superior in
density and phase-equilibria calculations, including vapor estimating the liquid densities of many materials, especially
pressures and solubility of solids in supercritical fluids nonpolar ones. The applicability of the Peng Robinson
(Soedarto). The Redlich Kwong EOS can satisfactorily be equation of state in the computation of thermodynamic
applied for fugacity, enthalpy and entropy departure interactions of volatile organic compounds and biodiesel has
calculations as well as gas phase properties. This equation also been tested by Sahay et al (2013).
poorly predicts liquid phase properties (Sahay, Edison and
Mohamed). The Soave-Redlich-Kwong (SRK) and Peng Working with these set of equations might sometimes be a
Robinson EOS are the most applicable in the petroleum tedious task, especially if the user is after the molar volume
industry (Oliveira, Ribeiro and Queimada). Soave replaced (𝑉̂) given other conditions. The term “cubic” in CEOS earns
the term 𝑇 −0.5 of the RK equation of state by a function  its name because they result to a polynomial of degree 3 in
involving temperature and acentric factor. The parameter was volume if manipulated. These type of nonlinear equations
formulated to make the equation fit the vapor pressure data can be solved by applying Numerical Methods like Secant,
of hydrocarbons (Housam and Zakia). Fixed-point iteration, Regula-Falsi, Bisection or Newton-
Raphson Method.
The Peng-Robinson EOS was suggested to satisfy the
following objectives (Housam and Nasri, Applications of the III. APPLICATION OF NEWTON-RAPHSON
peng-robinson equation of state using MATLAB): a) METHOD
parameters of this EOS should be defined in terms of the This Journal hand-picked Newton-Raphson analytical
critical properties and the acentric factor; b) reasonable method because of its rapid convergence to the root as one
accuracy near the critical point, particularly for calculations reason. By Newton’s Method, if 𝑓 is differentiable and 𝑥 is
of the compressibility factor and liquid density; c) a single an approximate solution of the equation f(x) = 0, then a
binary interaction parameter, which should be independent of better approximation might be obtained using

13
Abdulhalim Musa Abubakar et al. / IJRES, 8(3), 12-22, 2021

𝑓(𝑥𝑖) d) Peng-Robinson
𝑥𝑖+1 = 𝑥𝑖 − , for 𝑖 = 0, 1, 2, …, where 𝑖 = number of
𝑓′ (𝑥𝑖 )
iterations or repetitions (Mate). Re-writing Peng-Robinson EOS:

𝑅𝑇 2𝑅𝑇𝑏 𝑎𝛼
EOS in Table 1 can be rewritten in the following 𝑓(𝑉̂) = 𝑉̂ 3 + (𝑏 − ) 𝑉̂ 2 − (3𝑏 2 + − ) 𝑉̂ +
𝑃 𝑃 𝑃
way to compute molar volume (𝑉̂) of a given gas: 𝑏3 +𝑅𝑇𝑏2 −𝑎𝑏𝛼
𝑃
=0 ---------- (7)

𝑅𝑇 2𝑅𝑇𝑏 𝑎𝛼
𝑓 ′ (𝑉̂) = 3𝑉̂ 2 + 2 (𝑏 − ) 𝑉̂ − (3𝑏 2 + − )
a) Van der Waals EOS 𝑃 𝑃 𝑃
---------- (8)
Writing the Van der Waals equation as a function of molar
volume: For all the EOS, we have:

𝑓(𝑉 )̂
𝑅𝑇 𝑎 𝑎𝑏
𝑓(𝑉̂) = 𝑉̂ 3 − (𝑏 + 𝑃 ) 𝑉̂ 2 + (𝑃 ) 𝑉̂ − 𝑃 = 0 𝑉̂𝑖+1 = 𝑉̂𝑖 − 𝑓′ (𝑉̂𝑖 ) ---------- (9)
𝑖

---------- (1)
There is always a starting value (say 𝑉̂0 ) to help
̂) ̂2 𝑅𝑇 𝑎 calculate the root of equation (1), (3), (5) and (7). For a given
𝑓 ′ (𝑉
= 3𝑉 − 2 (𝑏 + ) 𝑉̂ +
𝑃 𝑃 problem, guess the volume calculated from ideal gas law as
---------- (2) 𝑅𝑇
the initial value (that is 𝑉̂𝑖𝑑𝑒𝑎𝑙 = 𝑃 ). Going the manual way
b) Redlich-Kwong EOS by paper and calculator is quite time-consuming and make
one prone to error. In some situations, available online web
The Redlich-Kwong EOS written as function of volume, calculator for any of the EOS might help but one might have
to grapple with unit conversion as there is hardly an online
𝑓(𝑉̂) is as follows
web calculator that makes provision for selecting different
units. The easiest or fastest of all is the use of MS Excel.
𝑅𝑇 𝑅𝑇𝑏 𝑎 𝑎𝑏
𝑓(𝑉̂) = 𝑉̂ 3 − ( 𝑃 ) 𝑉̂ 2 − (𝑏 2 + 𝑃 − 1 ) 𝑉̂ − 1 =0 Limitation common to all the aforementioned solution
𝑃 𝑇2 𝑃 𝑇2 approaches is that, one has to face the same stress for every
---------- (3) new problem encountered/given. Therefore, this write-up
wish to present a multiple option selection C++ program that
2𝑅𝑇 𝑅𝑇𝑏 𝑎
𝑓 ′ (𝑉̂) = 3𝑉̂ 2 − ( 𝑃 ) 𝑉̂ − (𝑏 2 + 𝑃 − 1 ) computes molar volume, 𝑉̂, by Newton’s iterations for the
𝑃 𝑇2 CEOS.
---------- (4)
IV. C++ SOURCE CODE FOR MOLAR VOLUME
c) Soave Redlich-Kwong EOS EVALUATION
The C++ language is a better version of the C language. One
SRK EOS can be presented in the following form: of its main features is that it supports object-oriented
𝑅𝑇 𝑅𝑇𝑏 𝑎𝛼 𝑎𝑏𝛼
programming (OOP). C++ is used for scientific computing as
𝑓(𝑉̂) = 𝑉̂ 3 − ( ) 𝑉̂ 2 − (𝑏 2 + − ) 𝑉̂ − =0 well as system programming (Ghosh). The below C++
𝑃 𝑃 𝑃 𝑃
---------- (5) syntax uses do-while statement to solve for numerical
solution or roots of non-linear equation by Newton’s method:
2𝑅𝑇 𝑅𝑇𝑏 𝑎𝛼
𝑓 ′ (𝑉̂) = 3𝑉̂ 2 − ( 𝑃
) 𝑉̂ − (𝑏 2 +
𝑃
− 𝑃
)
---------- (6)

14
Abdulhalim Musa Abubakar et al. / IJRES, 8(3), 12-22, 2021

Fig. 1: Dev C++ Syntax Showing Various Headers

Figure 1 defines the EOS and their derivatives (line 6-17) as any of the EOS (based on the input statement keyword ‘cin’
well as declared many parameters as floats and int (line 20- of line 32). The user is expected to press either 1, 2, 3 or 4 to
24). Equations 1-8 are defined in line 6-17. SE1 to SE14 are pick an equation of state. The options are well interpreted
using different cases in C++. They are Case 1 (line 35; Fig.
expression derived out of the general function, 𝑓(𝑉̂). It is to
2), Case 2 (line 86; Fig. 2), Case 3 (line 138; Fig. 4a) and
further reduce the complexity of the equation.
Case 4 (line 197; Fig. 4b).
In C++ programming, ‘int’ stands for integer. Variables
C++ is case sensitive. If a variable is declared using a lower
of type ‘int’ declared in line 20 of Figure 1 are examples.
case alphabet, it should be declared so throughout the coding.
‘Float’ are decimal point numbers. Variables declared as
Mistakes like that will prevent the program from running.
‘float’ are expected to be to some certain decimal places.
This information is for readers who wish to type and run the
source code in this article either for usage or confirmation. It
Output statements in C++ always begins with the
is worthy of note that almost all line of code in C++ ends
keyword ‘cout’. Line 27-30 are output statements showing
with semi-colon (;). Forgetting to type this, will result to
selectable options to execute. When the program is run, it
error notification when the program is eventually run.
requests the user to select an option, either 1, 2, 3, or 4 for

15
Abdulhalim Musa Abubakar et al. / IJRES, 8(3), 12-22, 2021

Fig. 2: C++ Syntax for Case 1 and 2


Figure 2 contains coding of the Van der Waal and Redlich- to specify the error, e, as well as the number of iterations
Kwong EOS as Case 1 and 2 respectively. Line 62-82 is a or steps, N.
do-while loop for Newton’s iteration. The user is allowed

16
Abdulhalim Musa Abubakar et al. / IJRES, 8(3), 12-22, 2021

Fig. 3: Syntax for Redlich-Kwong Execution

Abbreviations like aVDW, bVDW, simply implies ‘a’ and


‘b’ constant parameters for Van der Waals EOS. Similarly
that of Redlich-Kwong are subscripted as REDL, SRK for
Soave-Redlich-Kwong and PENG for Peng-Robinson (See
Figure 3, line 98-101). Also see Figure 1 (line 21-23). The
function in form of ‘pow(x,y)’ as seen in line 47, 98, 100-
101, 152, 155-156, 211, 214, 216 and 218-220 in almost
all the figures, defines variable say ‘x’ to power of ‘y’.
The program requests T, P, critical temperature and
pressure, Tc, and Pc, molar gas constant, R as well as
acentric factor, 𝜔 to be entered for program execution (one
of such is line139-151 of Figure 4a). It is left for the user
to ensure unit consistency of the properties requested as
well as their accuracy. Wrong entry will definitely give a
wrong output.
Note that the coding is almost the same for all cases.
Differences are in the written equations and parameter
expressions (for instance, aSRK in line 152 differs from
aPENG in line 211 of Figure 5). Fig. 4a: Syntax for Soave-Redlich-Kwong Execution
(Case 3)

17
Abdulhalim Musa Abubakar et al. / IJRES, 8(3), 12-22, 2021

The lengthy equations where shorten to a ‘short affect the result accuracy. The precision is set to 4 (i.e. 4
expression’ tagged ‘SE’, see line 155-157 of Figure 4(a), decimal places) in line 164; to be maintained throughout
so as to avoid mistake in equation entry that would clearly the calculations.

Fig. 4b: Syntax for Soave-Redlich-Kwong Execution (Case 3)


Figure 4b is a continuation of the codes for SRK EOS. The clearly in line 182. Initial guess was taken as 𝑉𝑖𝑑𝑒𝑎𝑙 for the
variable ‘m’ and ‘n’ stands for 𝑓(𝑉̂ ) and 𝑓 ′ (𝑉̂) functions. Figure 5 has the execution for Peng Robinson
corresponding to equation (5) and (6) respectively. The EOS:
Newton expression in terms of these functions can be seen

18
Abdulhalim Musa Abubakar et al. / IJRES, 8(3), 12-22, 2021

Fig. 5: Syntax for Soave-Redlich-Kwong Execution (Case 3)

𝑇
In line 215 of Figure 5, the reduced pressure, 𝑇𝑟 has been requested as 𝑇𝑟 = . Figure 6 is the remaining of the
𝑇𝑐
accounted for. The user would not be asked to enter the
coding:
value of 𝑇𝑟 but rather the critical temperature, 𝑇𝑐 will be

Fig. 6: Syntax for Peng-Robinson Execution (Case 4)

19
Abdulhalim Musa Abubakar et al. / IJRES, 8(3), 12-22, 2021

The keyword break (line 255 of Figure 6) causes the 679.7 𝑝𝑠𝑖, calculate the molar volume using the four CEOS.
program control to exit from switch block. The keyword Do this for iteration number N = 10 and error, e = 0.001.
break must be included at the end of each case statement. Compare the result.
What then is the initial guess? The initial guess as stated
𝑅𝑇 10.73×683
V. PROGRAM EXECUTION earlier is, 𝑉̂ = 𝑃 = 679.7 = 10.782 𝑓 3 /𝑚𝑜𝑙. This
Sample Problem: For propane, C3H8: 𝑇𝑐 = 205.92℉ = together with five other variables will be requested by the
665.92𝑅, 𝑃𝑐 = 615.5 𝑝𝑠𝑖 and acentric factor, 𝜔 = 0.1529 program when run. An additional variable (i.e. acentric
(Mansour), with number of moles, 𝑛 = 1.136 𝑙𝑏𝑚𝑜𝑙, 𝑅 = factor, 𝜔) will be requested by the program when the user
𝑝𝑠𝑖 𝑓𝑡 3
10.73 , at temperature, 𝑇 = 683𝑅 and pressure, 𝑃 = chooses SRK and PENG equations of state.
𝑙𝑏𝑚𝑜𝑙 𝑅

Table 2: Result Showing Iteration


1. VAN DER WAALS 2. REDLICH_KWONG

3. SOAVE-REDLICH-KWONG 4. PENG-ROBINSON

20
Abdulhalim Musa Abubakar et al. / IJRES, 8(3), 12-22, 2021

Table 2 are amazing display of the program source code VI. CONCLUSION
output window for each EOS selected. The window has a The program was written using Dev-Cpp 5.11 TDM-GCC
black background and the contents are white. The molar 4.9.2 version. The results are molar volume computations
volumes obtained are to four decimal places considering N only by Newton’s Iterative Method. They exclude
= 10. This program aims to be a CEOS calculator to be temperature, pressure and number of moles determination.
applied by Chemistry and Chemical Engineering students of They also exclude alternative numerical techniques for such
Polytechnic and Universities. Results of Table 2 can always problems. For this program, entering the right number of
be tested manually using a calculator. At this point, there is iteration and percentage error determines the convergent of
need to define some nomenclature: the initial guess to the desired root.
User should note that, the source code converges for
Nomenclature either large or small error values depending on the number
𝑇 Temperature (absolute) of iterations entered. Entering 10 as number of iterations
𝑃 Pressure will not necessarily mean that C++ should display the result
𝑎, 𝑏 Constant parameter up to the 10th iteration. In general, this coding is for CEOS
it excludes other classes of EOS. Similar codes that
𝑇𝑐 , 𝑃𝑐 Critical temperature and pressure
incorporates other EOS by either C++ or other
𝑅 Molar gas constant
programming languages can be proposed for further studies.
𝑇𝑟 Reduced temperature
This work is not discouraging manual calculations. It is
𝑘, 𝛼 Constant parameters
however, simplifying the rigorous calculation steps
𝑤 Acentric factor
involved before arriving at a solution. It will serve as a
𝑛 No. of moles confirmation calculator for students and teachers alike who
𝑉 Volume of fluid might encounter CEOS problems in the course of their
𝑉
𝑉̂ Molar volume (= 𝑛) studies.
𝑓(𝑉̂) Function of 𝑉̂ = f(V) = g(V) = m(V) =
ACKNOWLEDGEMENT
x(V) for VDW, REDL, SRK and PENG Avalanche of gratitude goes to Mr. David Asimiakhuini
EOS respectively (see Figure 1) who mentored us the basis of C++ Computer Programming
𝑓 ′ (𝑉̂) Derivative of 𝑉̂ = d(V) = h(V) = n(V) = at University of Maiduguri in the year 2014-2016. I
y(V) for VDW, REDL, SRK and PENG specifically acknowledge Dr. Abdu Zubairu of the
EOS respectively (see Figure 1) Department of Chemical Engineering, University of
Maiduguri for his lecture on Equations of State in the year
2014 when I was an undergraduate student of the

21
Abdulhalim Musa Abubakar et al. / IJRES, 8(3), 12-22, 2021

institution. Your immense contributions are worthy of Chemical Engineering of Japan 10.6 (2007): 534–538.
<https://www.researchgate.net/publication/239269619>.
recognition. I throw more weight of recognition to M. Tech. [5] Housam, Binous and Zakia Nasri. Applications of the peng-robinson
Ahmad Abubakar Mustapha, (a friend at VFSTR equation of state using MATLAB. Chemical Engineering Education
University, India) who is also a C++ expert. Ultimately, (2015): 1-11.
glory be to Allah Almighty for granting us the wisdom to <https://www.researchgate.net/publication/279621077>.
[6] Mansour, Eman Mohamed. Equation of State. Cairo: IntechOpen,
put this knowledge into use. 2020.
[7] Mate, Attila. Introduction to Numerical Analysis with C Program.
REFERENCES New York: Brooklyn College of the City University of New York,
[1] Deiters, Ulrich K and Ricardo Macías-Salinas. Calculation of 2014.
Densities from Cubic Equations of State: Revisited. Industrial & [8] Oliveira, M B, et al. Modeling Phase Equilibria Relevant to Biodiesel
Engineering Chemistry Research (2014): 2529−2536. Production: A Comparison of gE Models, Cubic EoS, EoS-gE and
<pubs.acs.org/IECR>. Association EoS,. Industrial & Engineering Chemistry and Research
[2] Ghosh, Pallab. Numerical Methods with Computer Programs in C++. 50 (2011): 2348–2358.
Delhi: PHI Learning Private Limited, 2009. [9] Sahay, Ramdharee, Muzenda Edison and Belaid Mohamed. A
<http://www.kopykitab.com/product/7407>. Review of the Equations of State and their Applicability in Phase
[3] Himmelblau, David M and James B Riggs. Basic Principles and Equilibrium Modeling. International Conference on Chemical and
Calculations in Chemical Engineering. 5th. New Jersey: Prentice Environmental Engineering (ICCEE'2013) . Johannesburg, 2013. 1-4.
Hall, 1982. [10] Soedarto, S H. Ratnawati. Improvement of the Redlich-Kwong
[4] Housam, Binous and Nasri Zakia. Applications of the Soave– Equation of State By Modification of Co-Volume Parameter. Vol. 13.
Redlich–Kwong Equation of State Using Mathematica. Journal of Semarang, 2010. 8-65.

22

You might also like