Nektar++
FilterEnergy.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: FilterEnergy.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: Output kinetic energy and enstrophy.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <iomanip>
36
39
40using namespace std;
41
42namespace Nektar::SolverUtils
43{
44std::string FilterEnergy::className =
46 "Energy", FilterEnergy::create);
47
49 const std::shared_ptr<EquationSystem> &pEquation,
50 const ParamMap &pParams)
51 : Filter(pSession, pEquation), m_index(-1), m_homogeneous(false), m_planes()
52{
53 std::string outName;
54
55 // OutputFile
56 auto it = pParams.find("OutputFile");
57 if (it == pParams.end())
58 {
59 outName = m_session->GetSessionName();
60 }
61 else
62 {
63 ASSERTL0(it->second.length() > 0, "Missing parameter 'OutputFile'.");
64 outName = it->second;
65 }
66 outName += ".eny";
67
68 m_comm = pSession->GetComm();
69 if (m_comm->GetRank() == 0)
70 {
71 m_outFile.open(outName.c_str());
72 ASSERTL0(m_outFile.good(), "Unable to open: '" + outName + "'");
73 m_outFile.setf(ios::scientific, ios::floatfield);
74 m_outFile << "# Time Kinetic energy "
75 << "Enstrophy" << endl
76 << "# ---------------------------------------------"
77 << "--------------" << endl;
78 }
79 pSession->LoadParameter("LZ", m_homogeneousLength, 0.0);
80
81 // OutputFrequency
82 it = pParams.find("OutputFrequency");
83 ASSERTL0(it != pParams.end(), "Missing parameter 'OutputFrequency'.");
84 LibUtilities::Equation equ(m_session->GetInterpreter(), it->second);
85 m_outputFrequency = round(equ.Evaluate());
86}
87
89{
90}
91
94 const NekDouble &time)
95{
96 m_index = -1;
98
99 ASSERTL0(pFields[0]->GetExpType() != MultiRegions::e1D,
100 "1D expansion not supported for energy filter");
101
102 ASSERTL0(pFields[0]->GetExpType() != MultiRegions::e2D,
103 "2D expansion not supported for energy filter");
104
105 ASSERTL0(pFields[0]->GetExpType() != MultiRegions::e3DH2D,
106 "Homogeneous 2D expansion not supported for energy filter");
107
108 if (pFields[0]->GetExpType() == MultiRegions::e3DH1D)
109 {
110 m_homogeneous = true;
111 }
112
113 // Calculate area/volume of domain.
114 if (m_homogeneous)
115 {
116 m_planes = pFields[0]->GetZIDs();
117 areaField = pFields[0]->GetPlane(0);
118 }
119 else
120 {
121 areaField = pFields[0];
122 }
123
124 Array<OneD, NekDouble> inarray(areaField->GetNpoints(), 1.0);
125 m_area = areaField->Integral(inarray);
126
127 if (m_homogeneous)
128 {
130 }
131
132 // Output values at initial time.
133 v_Update(pFields, time);
134}
135
138 const NekDouble &time)
139{
140 int i, nPoints = pFields[0]->GetNpoints();
141
142 m_index++;
143
144 if (m_index % m_outputFrequency > 0)
145 {
146 return;
147 }
148
149 // Lock equation system pointer
150 auto equ = m_equ.lock();
151 ASSERTL0(equ, "Weak pointer expired");
152
153 auto fluidEqu = std::dynamic_pointer_cast<FluidInterface>(equ);
154 ASSERTL0(fluidEqu, "Energy filter is incompatible with this solver.");
155
156 // Store physical values in an array
157 Array<OneD, Array<OneD, NekDouble>> physfields(pFields.size());
158 for (i = 0; i < pFields.size(); ++i)
159 {
160 physfields[i] = pFields[i]->GetPhys();
161 }
162
163 // Calculate kinetic energy.
164 NekDouble Ek = 0.0;
165 Array<OneD, NekDouble> tmp(nPoints, 0.0);
168 for (i = 0; i < 3; ++i)
169 {
170 u[i] = Array<OneD, NekDouble>(nPoints);
171 }
172 fluidEqu->GetVelocity(physfields, u);
173
174 for (i = 0; i < 3; ++i)
175 {
176 if (m_homogeneous && pFields[i]->GetWaveSpace())
177 {
178 pFields[i]->HomogeneousBwdTrans(nPoints, u[i], u[i]);
179 }
180
181 Vmath::Vvtvp(nPoints, u[i], 1, u[i], 1, tmp, 1, tmp, 1);
182 }
183
184 if (!fluidEqu->HasConstantDensity())
185 {
186 density = Array<OneD, NekDouble>(nPoints);
187 fluidEqu->GetDensity(physfields, density);
188 Vmath::Vmul(nPoints, density, 1, tmp, 1, tmp, 1);
189 }
190
191 if (m_homogeneous)
192 {
193 Array<OneD, NekDouble> tmp2(nPoints, 0.0);
194 pFields[0]->HomogeneousFwdTrans(nPoints, tmp, tmp2);
195 Ek = pFields[0]->GetPlane(0)->Integral(tmp2) * m_homogeneousLength;
196 }
197 else
198 {
199 Ek = pFields[0]->Integral(tmp);
200 }
201
202 Ek /= 2.0 * m_area;
203
204 if (m_comm->GetRank() == 0)
205 {
206 m_outFile << setw(17) << setprecision(8) << time << setw(22)
207 << setprecision(11) << Ek;
208 }
209
210 bool waveSpace[3] = {pFields[0]->GetWaveSpace(), pFields[1]->GetWaveSpace(),
211 pFields[2]->GetWaveSpace()};
212
213 if (m_homogeneous)
214 {
215 for (i = 0; i < 3; ++i)
216 {
217 pFields[i]->SetWaveSpace(false);
218 }
219 }
220
221 // First calculate vorticity field.
222 Array<OneD, NekDouble> tmp2(nPoints), tmp3(nPoints);
223 Vmath::Zero(nPoints, tmp, 1);
224 for (i = 0; i < 3; ++i)
225 {
226 int f1 = (i + 2) % 3, c2 = f1;
227 int c1 = (i + 1) % 3, f2 = c1;
228 pFields[f1]->PhysDeriv(c1, u[f1], tmp2);
229 pFields[f2]->PhysDeriv(c2, u[f2], tmp3);
230 Vmath::Vsub(nPoints, tmp2, 1, tmp3, 1, tmp2, 1);
231 Vmath::Vvtvp(nPoints, tmp2, 1, tmp2, 1, tmp, 1, tmp, 1);
232 }
233
234 if (!fluidEqu->HasConstantDensity())
235 {
236 Vmath::Vmul(nPoints, density, 1, tmp, 1, tmp, 1);
237 }
238
239 if (m_homogeneous)
240 {
241 for (i = 0; i < 3; ++i)
242 {
243 pFields[i]->SetWaveSpace(waveSpace[i]);
244 }
245 pFields[0]->HomogeneousFwdTrans(nPoints, tmp, tmp);
246 Ek = pFields[0]->GetPlane(0)->Integral(tmp) * m_homogeneousLength;
247 }
248 else
249 {
250 Ek = pFields[0]->Integral(tmp);
251 }
252
253 Ek /= 2.0 * m_area;
254
255 if (m_comm->GetRank() == 0)
256 {
257 m_outFile << setw(22) << setprecision(11) << Ek << endl;
258 }
259}
260
263 &pFields,
264 [[maybe_unused]] const NekDouble &time)
265{
266 m_outFile.close();
267}
268
270{
271 return true;
272}
273
274} // namespace Nektar::SolverUtils
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
SOLVER_UTILS_EXPORT void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pField, const NekDouble &time) override
SOLVER_UTILS_EXPORT void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pField, const NekDouble &time) override
static std::string className
Name of the class.
Definition: FilterEnergy.h:58
SOLVER_UTILS_EXPORT void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pField, const NekDouble &time) override
LibUtilities::CommSharedPtr m_comm
Definition: FilterEnergy.h:85
SOLVER_UTILS_EXPORT FilterEnergy(const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< EquationSystem > &pEquation, const ParamMap &pParams)
Array< OneD, unsigned int > m_planes
Definition: FilterEnergy.h:86
SOLVER_UTILS_EXPORT ~FilterEnergy() override
static SolverUtils::FilterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< SolverUtils::EquationSystem > &pEquation, const ParamMap &pParams)
Creates an instance of this class.
Definition: FilterEnergy.h:46
SOLVER_UTILS_EXPORT bool v_IsTimeDependent() override
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:83
const std::weak_ptr< EquationSystem > m_equ
Definition: Filter.h:84
std::map< std::string, std::string > ParamMap
Definition: Filter.h:65
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
FilterFactory & GetFilterFactory()
double NekDouble
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:72
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:366
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.hpp:273
void Vsub(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Subtract vector z = x-y.
Definition: Vmath.hpp:220
STL namespace.