Nektar++
VanDerWaalsEoS.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: VanDerWaalsEoS.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: Van der Waals equation of state
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #include <boost/core/ignore_unused.hpp>
36 
37 #include "VanDerWaalsEoS.h"
38 
39 using namespace std;
40 
41 namespace Nektar
42 {
43 
44 std::string VanDerWaalsEoS::className =
46  "VanDerWaals", VanDerWaalsEoS::create,
47  "Van der Waals equation of state.");
48 
49 VanDerWaalsEoS::VanDerWaalsEoS(
51  : EquationOfState(pSession)
52 {
53  NekDouble Tcrit, Pcrit;
54  pSession->LoadParameter("Tcrit", Tcrit);
55  pSession->LoadParameter("Pcrit", Pcrit);
56 
57  m_a = 27.0 / 64.0 * m_gasConstant * m_gasConstant * Tcrit * Tcrit / Pcrit;
58  m_b = 1.0 / 8.0 * m_gasConstant * Tcrit / Pcrit;
59 }
60 
62  const NekDouble &e)
63 {
64  return (e + m_a * rho) * (m_gamma - 1) / m_gasConstant;
65 }
66 
68  const NekDouble &e)
69 {
70  return (e + m_a * rho) * (m_gamma - 1) / (1.0 / rho - m_b) -
71  m_a * rho * rho;
72 }
73 
75 {
76  NekDouble T = GetTemperature(rho, e);
77  NekDouble sIg =
78  m_gasConstant / (m_gamma - 1) * log(T) - m_gasConstant * log(rho);
79 
80  return sIg + m_gasConstant * log(1 - m_b * rho);
81 }
82 
84  const NekDouble &e)
85 {
86  NekDouble result;
87 
88  result = (m_gamma - 1) * (e + 2 * m_a * rho - m_a * m_b * rho * rho);
89  result = result / ((1 - m_b * rho) * (1 - m_b * rho));
90  result = result - 2 * m_a * rho;
91 
92  return result;
93 }
94 
96  const NekDouble &e)
97 {
98  boost::ignore_unused(e);
99  return (m_gamma - 1) / (1.0 / rho - m_b);
100 }
101 
103  const NekDouble &p)
104 {
105  return (p + m_a * rho * rho) * (1.0 / rho - m_b) / (m_gamma - 1) -
106  m_a * rho;
107 }
108 
110 {
111  // First solve for the compressibility factor Z using the cubic equation
112  // Z^3 + k1 * Z^2 + k2 * Z + k3 = 0
113  // for van der Waals:
114  // k1 = -(B+1), k2 = A, k3 = -AB
115  // where A = aP/(RT)^2, B = bP/(RT)
116  NekDouble A = m_a * p / (m_gasConstant * m_gasConstant * T * T);
117  NekDouble B = m_b * p / (m_gasConstant * T);
118 
119  NekDouble k1 = -(B + 1);
120  NekDouble k2 = A;
121  NekDouble k3 = -A * B;
122 
123  // Use ideal gas (Z=1) as starting guess for iteration
124  NekDouble Z = 1.0;
125  // Newton-Raphson iteration to find Z
126  NekDouble tol = 1e-6;
127  NekDouble maxIter = 100;
128  NekDouble residual = 1;
129  NekDouble f, df;
130  unsigned int cnt = 0;
131  while (abs(residual) > tol && cnt < maxIter)
132  {
133  f = Z * Z * Z + k1 * Z * Z + k2 * Z + k3;
134  df = 3 * Z * Z + 2 * k1 * Z + k2;
135  residual = f / df;
136  Z -= residual;
137  ++cnt;
138  }
139  if (cnt == maxIter)
140  {
141  cout << "Newton-Raphson in VanDerWaalsEoS::v_GetRhoFromPT did not "
142  "converge in "
143  << maxIter << " iterations (residual = " << residual << ")"
144  << endl;
145  }
146 
147  // Now calculate rho = p/(ZRT)
148  return p / (Z * m_gasConstant * T);
149 }
150 }
virtual NekDouble v_GetEFromRhoP(const NekDouble &rho, const NekDouble &p)
Encapsulates equations of state allowing us to obtain thermodynamic properties: most relations are in...
virtual NekDouble v_GetDPDrho_e(const NekDouble &rho, const NekDouble &e)
STL namespace.
virtual NekDouble v_GetEntropy(const NekDouble &rho, const NekDouble &e)
virtual NekDouble v_GetDPDe_rho(const NekDouble &rho, const NekDouble &e)
virtual NekDouble v_GetPressure(const NekDouble &rho, const NekDouble &e)
EquationOfStateFactory & GetEquationOfStateFactory()
Declaration of the equation of state factory singleton.
virtual NekDouble v_GetRhoFromPT(const NekDouble &rho, const NekDouble &p)
virtual NekDouble v_GetTemperature(const NekDouble &rho, const NekDouble &e)
double NekDouble
NekDouble GetTemperature(const NekDouble &rho, const NekDouble &e)
Calculate the temperature.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
std::shared_ptr< SessionReader > SessionReaderSharedPtr