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