Nektar++
PengRobinsonEoS.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: PengRobinsonEoS.cpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Peng-Robinson equation of state
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #include "PengRobinsonEoS.h"
36 
37 using namespace std;
38 
39 namespace Nektar
40 {
41 
42 std::string PengRobinsonEoS::className =
44  "PengRobinson", PengRobinsonEoS::create,
45  "Peng-Robinson equation of state.");
46 
47 PengRobinsonEoS::PengRobinsonEoS(
49  : EquationOfState(pSession)
50 {
51  pSession->LoadParameter("Tcrit", m_Tc);
52  pSession->LoadParameter("Pcrit", m_Pc);
53  pSession->LoadParameter("AcentricFactor", m_omega);
54 
55  m_a = 0.45724 * m_gasConstant * m_gasConstant * m_Tc * m_Tc / m_Pc;
56  m_b = 0.0778 * m_gasConstant * m_Tc / m_Pc;
57  m_fw = 0.37464 + 1.54226 * m_omega - 0.2699 * m_omega * m_omega;
58 }
59 
61  const NekDouble &e)
62 {
63  // First we need to evaluate the log term
64  // ln[(1/rho + b - b*sqrt(2)) / (1/rho + b + b*sqrt(2))]
65  NekDouble sqrt2 = sqrt(2.0);
66  NekDouble logTerm = LogTerm(rho);
67 
68  // The temperature can be expressed as an equation in the form
69  // A * (T^1/2)^2 + B * T^1/2 + C = 0
70  NekDouble A, B, C;
71 
72  A = m_gasConstant / (m_gamma - 1);
73  B = -m_a / (m_b * 2 * sqrt2) * logTerm / sqrt(m_Tc) * m_fw * (1 + m_fw);
74  C = m_a / (m_b * 2 * sqrt2) * logTerm * (1 + m_fw) * (1 + m_fw) - e;
75 
76  // Solve for T^1/2 (positive root)
77  NekDouble sqrtT = (-B + sqrt(B * B - 4 * A * C)) / (2 * A);
78  // Calculate the temperature
79  return sqrtT * sqrtT;
80 }
81 
83  const NekDouble &e)
84 {
85  NekDouble T = GetTemperature(rho, e);
86 
87  NekDouble p =
88  m_gasConstant * T / (1.0 / rho - m_b) -
89  m_a * Alpha(T) / (1.0 / (rho * rho) + 2.0 * m_b / rho - m_b * m_b);
90 
91  return p;
92 }
93 
95  const NekDouble &e)
96 {
97  NekDouble T = GetTemperature(rho, e);
98  NekDouble logTerm = LogTerm(rho);
99  // Entropy for an ideal gas
100  NekDouble sIg =
101  m_gasConstant / (m_gamma - 1) * log(T) - m_gasConstant * log(rho);
102 
103  // First sqrt(Alpha) = 1+_w*(1-sqrt(Tr)) and sqrt(Tr)
104  NekDouble sqrtA = sqrt(Alpha(T));
105  NekDouble sqrtTr = sqrt(T / m_Tc);
106 
107  NekDouble deltaS = m_gasConstant * log(1 - m_b * rho);
108  deltaS += m_a * sqrtA * m_fw * logTerm * (sqrtTr / T) / (m_b * sqrt(8));
109 
110  return sIg + deltaS;
111 }
112 
114  const NekDouble &e)
115 {
116  NekDouble T = GetTemperature(rho, e);
117  NekDouble dPde = GetDPDe_rho(rho, e);
118 
119  // First calculate the denominator 1/rho^2 + 2*b/rho - b^2
120  // and alpha = [1+f_w*(1-sqrt(Tr))]^2
121  NekDouble denom = 1.0 / (rho * rho) + 2.0 * m_b / rho - m_b * m_b;
122  NekDouble alpha = Alpha(T);
123 
124  // Calculate dPdrho_T
125  NekDouble dPdrho_T =
126  m_gasConstant * T / ((1.0 - m_b * rho) * (1.0 - m_b * rho)) -
127  2 * m_a * alpha * rho * (1 + m_b * rho) /
128  ((denom * rho * rho) * (denom * rho * rho));
129 
130  // Calculate dedrho_T
131  NekDouble dedrho_T =
132  -m_a * sqrt(alpha) * (1.0 + m_fw) / (denom * rho * rho);
133 
134  // The result is dPdrho_e = dPdrho_T - dPde_rho * dedrho_T
135  return dPdrho_T - dPde * dedrho_T;
136 }
137 
139  const NekDouble &e)
140 {
141  NekDouble T = GetTemperature(rho, e);
142  NekDouble logTerm = LogTerm(rho);
143 
144  // First calculate the denominator 1/rho^2 + 2*b/rho - b^2
145  // and sqrt(Alpha) = 1+f_w*(1-sqrt(Tr))
146  NekDouble denom = 1.0 / (rho * rho) + 2.0 * m_b / rho - m_b * m_b;
147  NekDouble sqrtA = sqrt(Alpha(T));
148 
149  // Compute cv = dedT_rho
150  NekDouble cv = m_gasConstant / (m_gamma - 1) -
151  m_a / (2 * m_b * sqrt(8)) * logTerm * (m_fw * (1 + m_fw)) /
152  sqrt(T * m_Tc);
153 
154  // Now we obtain dPdT_rho
155  NekDouble dPdT = m_gasConstant / (1.0 / rho - m_b) +
156  m_a / sqrt(T * m_Tc) * m_fw * sqrtA / denom;
157 
158  // The result is dPde_rho = dPdT_rho / cv
159  return dPdT / cv;
160 }
161 
163  const NekDouble &p)
164 {
165  NekDouble denom = 1.0 / (rho * rho) + 2.0 * m_b / rho - m_b * m_b;
166  NekDouble logTerm = LogTerm(rho);
167  // First we solve for the temperature, which can be expressed as
168  // A * (T^1/2)^2 + B * T^1/2 + C = 0
169  NekDouble A, B, C;
170 
171  A = m_gasConstant / (1.0 / rho - m_b) -
172  (m_a * m_fw * m_fw) / (denom * m_Tc);
173  B = 2 * m_a / denom * m_fw * (1.0 + m_fw) / sqrt(m_Tc);
174  C = -m_a * (1.0 + m_fw) * (1 + m_fw) / denom - p;
175 
176  // Solve for T^1/2 (positive root)
177  NekDouble T = (-B + sqrt(B * B - 4 * A * C)) / (2 * A);
178  T = T * T;
179 
180  // Calculate alpha(T))
181  NekDouble alpha = Alpha(T);
182  // sqrt(Tr)
183  NekDouble sqrtTr = sqrt(T / m_Tc);
184  // Calculate internal energy
185  return m_gasConstant * T / (m_gamma - 1) +
186  m_a / (m_b * sqrt(8)) * logTerm *
187  (alpha + sqrt(alpha) * m_fw * sqrtTr);
188 }
189 
191  const NekDouble &T)
192 {
193  // First solve for the compressibility factor Z using the cubic equation
194  // Z^3 + k1 * Z^2 + k2 * Z + k3 = 0
195  // for PengRobinson:
196  // k1 = B-1, k2 = A - 2*B - 3*B^2, k3 = - AB + B^2 + B^3
197  // where A = a*alpha(T)*P/(RT)^2, B = bP/(RT)
198  NekDouble A = m_a * Alpha(T) * p / (m_gasConstant * m_gasConstant * T * T);
199  NekDouble B = m_b * p / (m_gasConstant * T);
200 
201  NekDouble k1 = B - 1.0;
202  NekDouble k2 = A - 2 * B - 3 * B * B;
203  NekDouble k3 = -A * B + B * B + B * B * B;
204 
205  // Use ideal gas (Z=1) as starting guess for iteration
206  NekDouble Z = 1.0;
207  // Newton-Raphson iteration to find Z
208  NekDouble tol = 1e-6;
209  NekDouble maxIter = 100;
210  NekDouble residual = 1;
211  NekDouble f, df;
212  unsigned int cnt = 0;
213  while (abs(residual) > tol && cnt < maxIter)
214  {
215  f = Z * Z * Z + k1 * Z * Z + k2 * Z + k3;
216  df = 3 * Z * Z + 2 * k1 * Z + k2;
217  residual = f / df;
218  Z -= residual;
219  ++cnt;
220  }
221  if (cnt == maxIter)
222  {
223  cout << "Newton-Raphson in PengRobinsonEoS::v_GetRhoFromPT did not "
224  "converge in "
225  << maxIter << " iterations (residual = " << residual << ")"
226  << endl;
227  }
228 
229  // Now calculate rho = p/(ZRT)
230  return p / (Z * m_gasConstant * T);
231 }
232 
234 {
235  NekDouble sqrtAlpha = 1.0 + m_fw * (1.0 - sqrt(T / m_Tc));
236  return sqrtAlpha * sqrtAlpha;
237 }
238 
240 {
241  return log((1.0 / rho + m_b - m_b * sqrt(2)) /
242  (1.0 / rho + m_b + m_b * sqrt(2)));
243 }
244 }
NekDouble LogTerm(const NekDouble &rho)
NekDouble GetDPDe_rho(const NekDouble &rho, const NekDouble &e)
Calculate the partial derivative of P(rho,e) with respect to e.
Encapsulates equations of state allowing us to obtain thermodynamic properties: most relations are in...
STL namespace.
virtual NekDouble v_GetEntropy(const NekDouble &rho, const NekDouble &e)
virtual NekDouble v_GetTemperature(const NekDouble &rho, const NekDouble &e)
NekDouble Alpha(const NekDouble &T)
EquationOfStateFactory & GetEquationOfStateFactory()
Declaration of the equation of state factory singleton.
double NekDouble
virtual NekDouble v_GetEFromRhoP(const NekDouble &rho, const NekDouble &p)
NekDouble GetTemperature(const NekDouble &rho, const NekDouble &e)
Calculate the temperature.
virtual NekDouble v_GetDPDrho_e(const NekDouble &rho, const NekDouble &e)
virtual NekDouble v_GetPressure(const NekDouble &rho, const NekDouble &e)
virtual NekDouble v_GetDPDe_rho(const NekDouble &rho, const NekDouble &e)
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
std::shared_ptr< SessionReader > SessionReaderSharedPtr
virtual NekDouble v_GetRhoFromPT(const NekDouble &rho, const NekDouble &p)