Nektar++
RedlichKwongEoS.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: RedlichKwongEoS.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: Redlich-Kwong equation of state
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #include "RedlichKwongEoS.h"
36 
37 using namespace std;
38 
39 namespace Nektar
40 {
41 
42 std::string RedlichKwongEoS::className =
44  "RedlichKwong", RedlichKwongEoS::create,
45  "Redlich-Kwong equation of state.");
46 
47 RedlichKwongEoS::RedlichKwongEoS(
49  : EquationOfState(pSession)
50 {
51  pSession->LoadParameter("Tcrit", m_Tc);
52  pSession->LoadParameter("Pcrit", m_Pc);
53 
54  m_a = 0.42748 * m_gasConstant * m_gasConstant * m_Tc * m_Tc / m_Pc;
55  m_b = 0.08664 * m_gasConstant * m_Tc / m_Pc;
56 }
57 
59  const NekDouble &e)
60 {
61  return GetTemperatureKernel(rho, e);
62 }
63 
65 {
66  return GetTemperatureKernel(rho, e);
67 }
68 
70 {
71  return GetPressureKernel(rho, e);
72 }
73 
75 {
76  return GetPressureKernel(rho, e);
77 }
78 
80  const NekDouble &e)
81 {
82  NekDouble T = GetTemperature(rho, e);
83  NekDouble logTerm = LogTerm(rho);
84  // Entropy for an ideal gas
85  NekDouble sIg =
86  m_gasConstant / (m_gamma - 1) * log(T) - m_gasConstant * log(rho);
87 
88  NekDouble deltaS = m_gasConstant * log(1 - m_b * rho);
89  deltaS -= m_a * Alpha(T) * logTerm / (2 * m_b * T);
90 
91  return sIg + deltaS;
92 }
93 
95  const NekDouble &e)
96 {
97  NekDouble T = GetTemperature(rho, e);
98  NekDouble alpha = Alpha(T);
99  NekDouble dPde = GetDPDe_rho(rho, e);
100 
101  // Calculate dPdrho_T
102  NekDouble dPdrho_T =
103  m_gasConstant * T / ((1.0 - m_b * rho) * (1.0 - m_b * rho)) -
104  m_a * alpha * rho * (m_b * rho + 2) /
105  ((1 + m_b * rho) * (1 + m_b * rho));
106 
107  // Calculate dedrho_T
108  NekDouble dedrho_T = -3 * m_a * alpha / (2 * (1 + m_b * rho));
109 
110  // The result is dPdrho_e = dPdrho_T - dPde_rho * dedrho_T
111  return dPdrho_T - dPde * dedrho_T;
112 }
113 
115  const NekDouble &e)
116 {
117  NekDouble T = GetTemperature(rho, e);
118  NekDouble alpha = Alpha(T);
119  NekDouble logTerm = LogTerm(rho);
120 
121  // First calculate the denominator 1/rho^2 + 2*b/rho - b^2
122  // and sqrt(Alpha) = 1+f_w*(1-sqrt(Tr))
123  NekDouble denom = 1.0 / (rho * rho) + m_b / rho;
124 
125  // Compute cv = dedT_rho
126  NekDouble cv = m_gasConstant / (m_gamma - 1) +
127  3 * m_a * alpha * logTerm / (4 * m_b * T);
128 
129  // Now we obtain dPdT_rho
130  NekDouble dPdT =
131  m_gasConstant / (1.0 / rho - m_b) + m_a * alpha / (denom * 2 * T);
132 
133  // The result is dPde_rho = dPdT_rho / cv
134  return dPdT / cv;
135 }
136 
138  const NekDouble &p)
139 {
140  NekDouble logTerm = LogTerm(rho);
141  // First calculate the temperature, which can be expressed as
142  // (T^1/2)^3 + A* T^1/2 + B = 0
143  NekDouble A, B;
144 
145  A = -p * (1.0 / rho - m_b) / m_gasConstant;
146  B = -m_a * sqrt(m_Tc) * (1.0 / rho - m_b) /
147  (1.0 / (rho * rho) + m_b / rho) / m_gasConstant;
148 
149  // Use ideal gas solution as starting guess for iteration
150  NekDouble sqrtT = sqrt(p / (rho * (m_gamma - 1)));
151  // Newton-Raphson iteration to find T^(1/2)
152  NekDouble tol = 1e-6;
153  NekDouble maxIter = 100;
154  NekDouble residual = 1;
155  NekDouble f, df;
156  unsigned int cnt = 0;
157  while (abs(residual) > tol && cnt < maxIter)
158  {
159  f = sqrtT * sqrtT * sqrtT + A * sqrtT + B;
160  df = 3 * sqrtT * sqrtT + A;
161  residual = f / df;
162  sqrtT -= residual;
163  ++cnt;
164  }
165  if (cnt == maxIter)
166  {
167  cout << "Newton-Raphson in RedlichKwongEoS::v_GetEFromRhoP did not "
168  "converge in "
169  << maxIter << " iterations (residual = " << residual << ")"
170  << endl;
171  }
172 
173  // Calculate T
174  NekDouble T = sqrtT * sqrtT;
175 
176  // Calculate internal energy
177  return m_gasConstant * T / (m_gamma - 1) -
178  3 * m_a * Alpha(T) / (2 * m_b) * logTerm;
179 }
180 
182  const NekDouble &T)
183 {
184  // First solve for the compressibility factor Z using the cubic equation
185  // Z^3 + k1 * Z^2 + k2 * Z + k3 = 0
186  // for RedlichKwong:
187  // k1 = -1.0, k2 = A - B - B^2, k3 = -AB
188  // where A = a*alpha(T)*P/(RT)^2, B = bP/(RT)
189  NekDouble A = m_a * Alpha(T) * p / (m_gasConstant * m_gasConstant * T * T);
190  NekDouble B = m_b * p / (m_gasConstant * T);
191 
192  NekDouble k1 = -1.0;
193  NekDouble k2 = A - B - B * B;
194  NekDouble k3 = -A * B;
195 
196  // Use ideal gas (Z=1) as starting guess for iteration
197  NekDouble Z = 1.0;
198  // Newton-Raphson iteration to find Z
199  NekDouble tol = 1e-6;
200  NekDouble maxIter = 100;
201  NekDouble residual = 1;
202  NekDouble f, df;
203  unsigned int cnt = 0;
204  while (abs(residual) > tol && cnt < maxIter)
205  {
206  f = Z * Z * Z + k1 * Z * Z + k2 * Z + k3;
207  df = 3 * Z * Z + 2 * k1 * Z + k2;
208  residual = f / df;
209  Z -= residual;
210  ++cnt;
211  }
212  if (cnt == maxIter)
213  {
214  cout << "Newton-Raphson in RedlichKwongEoS::v_GetRhoFromPT did not "
215  "converge in "
216  << maxIter << " iterations (residual = " << residual << ")"
217  << endl;
218  }
219 
220  // Now calculate rho = p/(ZRT)
221  return p / (Z * m_gasConstant * T);
222 }
223 
224 } // 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)
NekDouble v_GetDPDrho_e(const NekDouble &rho, const NekDouble &e) final
NekDouble GetPressure(const NekDouble &rho, const NekDouble &e) final
Calculate the pressure.
NekDouble GetTemperature(const NekDouble &rho, const NekDouble &e) final
Calculate the temperature.
T Alpha(const T &temp)
NekDouble v_GetRhoFromPT(const NekDouble &rho, const NekDouble &p) final
NekDouble v_GetDPDe_rho(const NekDouble &rho, const NekDouble &e) final
NekDouble v_GetEntropy(const NekDouble &rho, const NekDouble &e) final
T GetPressureKernel(const T &rho, const T &e)
NekDouble v_GetEFromRhoP(const NekDouble &rho, const NekDouble &p) 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 > abs(scalarT< T > in)
Definition: scalar.hpp:295
scalarT< T > sqrt(scalarT< T > in)
Definition: scalar.hpp:291