Nektar++
Loading...
Searching...
No Matches
ProcessCompressibleFields.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: ProcessCompressibleFields.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: Computes compressible flow fields such as velocity, pressure,
32// temperature, entropy, sound speed and Mach number.
33//
34////////////////////////////////////////////////////////////////////////////////
35
36#include <algorithm>
37#include <string>
38#include <vector>
39
41
43
45
46namespace Nektar::FieldUtils
47{
48
51 ModuleKey(eProcessModule, "compressiblefields"),
53 "Computes velocity, pressure, temperature, entropy, sound speed and "
54 "Mach number from compressible flow fields.");
55
60
64
65void ProcessCompressibleFields::v_Process(po::variables_map &vm)
66{
67 m_f->SetUpExp(vm);
68
69 ASSERTL0(m_f->m_session && m_f->m_graph,
70 "ProcessCompressibleFields requires a session file.");
71
72 std::string eosType;
73 m_f->m_session->LoadSolverInfo("EquationOfState", eosType, "IdealGas");
74 ASSERTL0(StringUtils::IEquals(eosType, "IdealGas"),
75 "ProcessCompressibleFields only supports the IdealGas equation "
76 "of state.");
77
78 int nfields = m_f->m_variables.size();
79 int spacedim = m_f->m_graph->GetMeshDimension() + m_f->m_numHomogeneousDir;
80
81 // Locate the conservative variables by name, so that the input may contain
82 // other fields in any order
83 auto findField = [&](const std::string &name) {
84 auto it =
85 std::find(m_f->m_variables.begin(), m_f->m_variables.end(), name);
86 ASSERTL0(it != m_f->m_variables.end(),
87 "ProcessCompressibleFields requires the field '" + name +
88 "' (conservative variables of a compressible flow).");
89 return static_cast<int>(it - m_f->m_variables.begin());
90 };
91
92 const std::string momNames[3] = {"rhou", "rhov", "rhow"};
93 const std::string velNames[3] = {"u", "v", "w"};
94
95 int rhoId = findField("rho");
96 int EId = findField("E");
97 std::vector<int> momId(spacedim);
98 for (int i = 0; i < spacedim; ++i)
99 {
100 momId[i] = findField(momNames[i]);
101 }
102
103 // New fields: velocity components, p, T, s, a and Mach
104 std::vector<std::string> newNames(velNames, velNames + spacedim);
105 newNames.insert(newNames.end(), {"p", "T", "s", "a", "Mach"});
106 int addfields = newNames.size();
107
108 for (auto &name : newNames)
109 {
110 ASSERTL0(std::find(m_f->m_variables.begin(), m_f->m_variables.end(),
111 name) == m_f->m_variables.end(),
112 "Field '" + name +
113 "' already exists (e.g. the file was written with "
114 "OutputExtraFields); remove it first with the "
115 "removefield module.");
116 }
117
118 // Append field names
119 m_f->m_variables.insert(m_f->m_variables.end(), newNames.begin(),
120 newNames.end());
121
122 // Skip in case of empty partition
123 if (m_f->m_exp[0]->GetNumElmts() == 0)
124 {
125 return;
126 }
127
128 int nstrips;
129 m_f->m_session->LoadParameter("Strip_Z", nstrips, 1);
130
131 // Add in new fields
132 for (int s = 0; s < nstrips; ++s)
133 {
134 for (int i = 0; i < addfields; ++i)
135 {
137 m_f->AppendExpList(m_f->m_numHomogeneousDir);
138 m_f->m_exp.insert(m_f->m_exp.begin() + s * (nfields + addfields) +
139 nfields + i,
140 Exp);
141 }
142 }
143
144 NekDouble gamma, gasConstant;
145 m_f->m_session->LoadParameter("Gamma", gamma, 1.4);
146 m_f->m_session->LoadParameter("GasConstant", gasConstant, 287.058);
147 NekDouble cv = gasConstant / (gamma - 1.0);
148
149 int npoints = m_f->m_exp[0]->GetNpoints();
150 bool waveSpace = m_f->m_exp[0]->GetWaveSpace();
151
152 Array<OneD, NekDouble> rho(npoints), e(npoints), vel2(npoints);
153 Array<OneD, NekDouble> tmp(npoints);
154 Array<OneD, Array<OneD, NekDouble>> outfield(addfields);
155 for (int i = 0; i < addfields; ++i)
156 {
157 outfield[i] = Array<OneD, NekDouble>(npoints);
158 }
159 Array<OneD, NekDouble> &pressure = outfield[spacedim];
160 Array<OneD, NekDouble> &temperature = outfield[spacedim + 1];
161 Array<OneD, NekDouble> &entropy = outfield[spacedim + 2];
162 Array<OneD, NekDouble> &soundspeed = outfield[spacedim + 3];
163 Array<OneD, NekDouble> &mach = outfield[spacedim + 4];
164
165 // The fields are nonlinear functions of the conservative variables, so
166 // they have to be computed in physical space
167 auto getPhys = [&](int fid, Array<OneD, NekDouble> &out) {
168 if (waveSpace)
169 {
170 m_f->m_exp[fid]->HomogeneousBwdTrans(
171 npoints, m_f->m_exp[fid]->GetPhys(), out);
172 }
173 else
174 {
175 Vmath::Vcopy(npoints, m_f->m_exp[fid]->GetPhys(), 1, out, 1);
176 }
177 };
178
179 for (int s = 0; s < nstrips; ++s)
180 {
181 int offset = s * (nfields + addfields);
182
183 getPhys(offset + rhoId, rho);
184
185 // Velocity u_i = (rho u_i) / rho and its squared magnitude |u|^2
186 Vmath::Zero(npoints, vel2, 1);
187 for (int i = 0; i < spacedim; ++i)
188 {
189 getPhys(offset + momId[i], outfield[i]);
190 Vmath::Vdiv(npoints, outfield[i], 1, rho, 1, outfield[i], 1);
191 Vmath::Vvtvp(npoints, outfield[i], 1, outfield[i], 1, vel2, 1, vel2,
192 1);
193 }
194
195 // Internal energy e = E / rho - 0.5 |u|^2
196 getPhys(offset + EId, e);
197 Vmath::Vdiv(npoints, e, 1, rho, 1, e, 1);
198 Vmath::Svtvp(npoints, -0.5, vel2, 1, e, 1, e, 1);
199
200 // Pressure p = (gamma - 1) rho e
201 Vmath::Vmul(npoints, rho, 1, e, 1, pressure, 1);
202 Vmath::Smul(npoints, gamma - 1.0, pressure, 1, pressure, 1);
203
204 // Temperature T = e / cv
205 Vmath::Smul(npoints, 1.0 / cv, e, 1, temperature, 1);
206
207 // Entropy s = cv log(T) - R log(rho)
208 Vmath::Vlog(npoints, temperature, 1, entropy, 1);
209 Vmath::Smul(npoints, cv, entropy, 1, entropy, 1);
210 Vmath::Vlog(npoints, rho, 1, tmp, 1);
211 Vmath::Svtvp(npoints, -gasConstant, tmp, 1, entropy, 1, entropy, 1);
212
213 // Sound speed a = sqrt(gamma R T)
214 Vmath::Smul(npoints, gamma * gasConstant, temperature, 1, soundspeed,
215 1);
216 Vmath::Vsqrt(npoints, soundspeed, 1, soundspeed, 1);
217
218 // Mach number M = |u| / a
219 Vmath::Vsqrt(npoints, vel2, 1, mach, 1);
220 Vmath::Vdiv(npoints, mach, 1, soundspeed, 1, mach, 1);
221
222 for (int i = 0; i < addfields; ++i)
223 {
224 int fid = offset + nfields + i;
225 if (waveSpace)
226 {
227 m_f->m_exp[fid]->HomogeneousFwdTrans(npoints, outfield[i],
228 outfield[i]);
229 }
230 Vmath::Vcopy(npoints, outfield[i], 1, m_f->m_exp[fid]->UpdatePhys(),
231 1);
232 m_f->m_exp[fid]->FwdTransLocalElmt(outfield[i],
233 m_f->m_exp[fid]->UpdateCoeffs());
234 }
235 }
236}
237
238} // namespace Nektar::FieldUtils
#define ASSERTL0(condition, msg)
FieldSharedPtr m_f
Field object.
Definition Module.h:239
void v_Process(po::variables_map &vm) override
Compute the compressible fields and append them to the field.
static std::shared_ptr< Module > create(FieldSharedPtr f)
Creates an instance of this class.
Abstract base class for processing modules.
Definition Module.h:301
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
std::shared_ptr< Field > FieldSharedPtr
std::pair< ModuleType, std::string > ModuleKey
Definition Module.h:180
ModuleFactory & GetModuleFactory()
Definition Module.cpp:48
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
bool IEquals(std::string_view a, std::string_view b)
void Vsqrt(int n, const T *x, const int incx, T *y, const int incy)
sqrt y = sqrt(x)
Definition Vmath.hpp:342
void Vmul(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x*y.
Definition Vmath.hpp:74
void Vlog(int n, const T *x, const int incx, T *y, const int incy)
log y = log(x)
Definition Vmath.hpp:305
void Svtvp(int n, const T alpha, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Svtvp (scalar times vector plus vector): z = alpha*x + y.
Definition Vmath.hpp:398
void Vvtvp(int n, const T *w, const int incw, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
vvtvp (vector times vector plus vector): z = w*x + y
Definition Vmath.hpp:368
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*x.
Definition Vmath.hpp:102
void Vdiv(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x/y.
Definition Vmath.hpp:128
void Zero(int n, T *x, const int incx)
Zero vector.
Definition Vmath.hpp:275
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition Vmath.hpp:828