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  return GetTemperatureKernel(rho, e);
64 }
65 
67 {
68  return GetTemperatureKernel(rho, e);
69 }
70 
72 {
73  return GetPressureKernel(rho, e);
74 }
75 
77 {
78  return GetPressureKernel(rho, e);
79 }
80 
82  const NekDouble &e)
83 {
84  NekDouble T = GetTemperature(rho, e);
85  NekDouble logTerm = LogTerm(rho);
86  // Entropy for an ideal gas
87  NekDouble sIg =
88  m_gasConstant / (m_gamma - 1) * log(T) - m_gasConstant * log(rho);
89 
90  // First sqrt(Alpha) = 1+_w*(1-sqrt(Tr)) and sqrt(Tr)
91  NekDouble sqrtA = sqrt(Alpha(T));
92  NekDouble sqrtTr = sqrt(T / m_Tc);
93 
94  NekDouble deltaS = m_gasConstant * log(1 - m_b * rho);
95  deltaS += m_a * sqrtA * m_fw * logTerm * (sqrtTr / T) / (m_b * sqrt(8));
96 
97  return sIg + deltaS;
98 }
99 
101  const NekDouble &e)
102 {
103  NekDouble T = GetTemperature(rho, e);
104  NekDouble dPde = GetDPDe_rho(rho, e);
105 
106  // First calculate the denominator 1/rho^2 + 2*b/rho - b^2
107  // and alpha = [1+f_w*(1-sqrt(Tr))]^2
108  NekDouble denom = 1.0 / (rho * rho) + 2.0 * m_b / rho - m_b * m_b;
109  NekDouble alpha = Alpha(T);
110 
111  // Calculate dPdrho_T
112  NekDouble dPdrho_T =
113  m_gasConstant * T / ((1.0 - m_b * rho) * (1.0 - m_b * rho)) -
114  2 * m_a * alpha * rho * (1 + m_b * rho) /
115  ((denom * rho * rho) * (denom * rho * rho));
116 
117  // Calculate dedrho_T
118  NekDouble dedrho_T =
119  -m_a * sqrt(alpha) * (1.0 + m_fw) / (denom * rho * rho);
120 
121  // The result is dPdrho_e = dPdrho_T - dPde_rho * dedrho_T
122  return dPdrho_T - dPde * dedrho_T;
123 }
124 
126  const NekDouble &e)
127 {
128  NekDouble T = GetTemperature(rho, e);
129  NekDouble logTerm = LogTerm(rho);
130 
131  // First calculate the denominator 1/rho^2 + 2*b/rho - b^2
132  // and sqrt(Alpha) = 1+f_w*(1-sqrt(Tr))
133  NekDouble denom = 1.0 / (rho * rho) + 2.0 * m_b / rho - m_b * m_b;
134  NekDouble sqrtA = sqrt(Alpha(T));
135 
136  // Compute cv = dedT_rho
137  NekDouble cv = m_gasConstant / (m_gamma - 1) -
138  m_a / (2 * m_b * sqrt(8)) * logTerm * (m_fw * (1 + m_fw)) /
139  sqrt(T * m_Tc);
140 
141  // Now we obtain dPdT_rho
142  NekDouble dPdT = m_gasConstant / (1.0 / rho - m_b) +
143  m_a / sqrt(T * m_Tc) * m_fw * sqrtA / denom;
144 
145  // The result is dPde_rho = dPdT_rho / cv
146  return dPdT / cv;
147 }
148 
150  const NekDouble &p)
151 {
152  NekDouble denom = 1.0 / (rho * rho) + 2.0 * m_b / rho - m_b * m_b;
153  NekDouble logTerm = LogTerm(rho);
154  // First we solve for the temperature, which can be expressed as
155  // A * (T^1/2)^2 + B * T^1/2 + C = 0
156  NekDouble A, B, C;
157 
158  A = m_gasConstant / (1.0 / rho - m_b) -
159  (m_a * m_fw * m_fw) / (denom * m_Tc);
160  B = 2 * m_a / denom * m_fw * (1.0 + m_fw) / sqrt(m_Tc);
161  C = -m_a * (1.0 + m_fw) * (1 + m_fw) / denom - p;
162 
163  // Solve for T^1/2 (positive root)
164  NekDouble T = (-B + sqrt(B * B - 4 * A * C)) / (2 * A);
165  T = T * T;
166 
167  // Calculate alpha(T))
168  NekDouble alpha = Alpha(T);
169  // sqrt(Tr)
170  NekDouble sqrtTr = sqrt(T / m_Tc);
171  // Calculate internal energy
172  return m_gasConstant * T / (m_gamma - 1) +
173  m_a / (m_b * sqrt(8)) * logTerm *
174  (alpha + sqrt(alpha) * m_fw * sqrtTr);
175 }
176 
178  const NekDouble &T)
179 {
180  // First solve for the compressibility factor Z using the cubic equation
181  // Z^3 + k1 * Z^2 + k2 * Z + k3 = 0
182  // for PengRobinson:
183  // k1 = B-1, k2 = A - 2*B - 3*B^2, k3 = - AB + B^2 + B^3
184  // where A = a*alpha(T)*P/(RT)^2, B = bP/(RT)
185  NekDouble A = m_a * Alpha(T) * p / (m_gasConstant * m_gasConstant * T * T);
186  NekDouble B = m_b * p / (m_gasConstant * T);
187 
188  NekDouble k1 = B - 1.0;
189  NekDouble k2 = A - 2 * B - 3 * B * B;
190  NekDouble k3 = -A * B + B * B + B * B * B;
191 
192  // Use ideal gas (Z=1) as starting guess for iteration
193  NekDouble Z = 1.0;
194  // Newton-Raphson iteration to find Z
195  NekDouble tol = 1e-6;
196  NekDouble maxIter = 100;
197  NekDouble residual = 1;
198  NekDouble f, df;
199  unsigned int cnt = 0;
200  while ((fabs(residual) > tol) && cnt < maxIter)
201  {
202  f = Z * Z * Z + k1 * Z * Z + k2 * Z + k3;
203  df = 3 * Z * Z + 2 * k1 * Z + k2;
204  residual = f / df;
205  Z -= residual;
206  ++cnt;
207  }
208  if (cnt == maxIter)
209  {
210  cout << "Newton-Raphson in PengRobinsonEoS::v_GetRhoFromPT did not "
211  "converge in "
212  << maxIter << " iterations (residual = " << residual << ")"
213  << endl;
214  }
215 
216  // Now calculate rho = p/(ZRT)
217  return p / (Z * m_gasConstant * T);
218 }
219 
220 } // namespace Nektar
Encapsulates equations of state allowing us to obtain thermodynamic properties: most relations are in...
NekDouble GetDPDe_rho(const NekDouble &rho, const NekDouble &e)
Calculate the partial derivative of P(rho,e) with respect to e.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:198
T GetTemperatureKernel(const T &rho, const T &e)
T GetPressureKernel(const T &rho, const T &e)
NekDouble v_GetEFromRhoP(const NekDouble &rho, const NekDouble &p) final
NekDouble GetTemperature(const NekDouble &rho, const NekDouble &e) final
Calculate the temperature.
NekDouble v_GetDPDe_rho(const NekDouble &rho, const NekDouble &e) final
NekDouble GetPressure(const NekDouble &rho, const NekDouble &e) final
Calculate the pressure.
NekDouble v_GetRhoFromPT(const NekDouble &rho, const NekDouble &p) final
NekDouble v_GetDPDrho_e(const NekDouble &rho, const NekDouble &e) final
T Alpha(const T &temp)
NekDouble v_GetEntropy(const NekDouble &rho, const NekDouble &e) final
std::shared_ptr< SessionReader > SessionReaderSharedPtr
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
EquationOfStateFactory & GetEquationOfStateFactory()
Declaration of the equation of state factory singleton.
tinysimd::simd< NekDouble > vec_t
double NekDouble
scalarT< T > log(scalarT< T > in)
Definition: scalar.hpp:300
scalarT< T > sqrt(scalarT< T > in)
Definition: scalar.hpp:291