Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FilterModalEnergy.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File FilterModalEnergy.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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Output values of the modal energy
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #include <iomanip>
37 
40 
41 using namespace std;
42 
43 namespace Nektar
44 {
45 namespace SolverUtils
46 {
47 std::string FilterModalEnergy::className = GetFilterFactory().
48  RegisterCreatorFunction("ModalEnergy", FilterModalEnergy::create);
49 
50 /**
51  * Constructor.
52  */
53 FilterModalEnergy::FilterModalEnergy(
55  const ParamMap &pParams) :
56  Filter(pSession)
57 {
58  ParamMap::const_iterator it;
59 
60  // OutputFile
61  it = pParams.find("OutputFile");
62  if (it == pParams.end())
63  {
64  m_outputFile = m_session->GetSessionName();
65  }
66  else
67  {
68  ASSERTL0(it->second.length() > 0, "Missing parameter 'OutputFile'.");
69  m_outputFile = it->second;
70  }
71  if (!(m_outputFile.length() >= 4
72  && m_outputFile.substr(m_outputFile.length() - 4) == ".mdl"))
73  {
74  m_outputFile += ".mdl";
75  }
76 
77  // OutputFrequency
78  it = pParams.find("OutputFrequency");
79  if (it == pParams.end())
80  {
82  }
83  else
84  {
85  LibUtilities::Equation equ(m_session, it->second);
86  m_outputFrequency = floor(equ.Evaluate());
87  }
88 
89 
90  m_session->MatchSolverInfo("Homogeneous", "1D", m_isHomogeneous1D, false);
91  m_session->MatchSolverInfo("Homogeneous", "2D", m_isHomogeneous2D, false);
92  m_session->MatchSolverInfo("CalculatePerturbationEnergy", "True",
93  m_PertEnergy, false);
94  m_session->LoadParameter ("NumQuadPointsError", m_NumQuadPointsError, 0);
95  m_EqTypeStr = m_session->GetSolverInfo("EQTYPE");
96 
97  // OutputPlane
99  {
100  m_session->LoadParameter("LZ", m_LhomZ);
101 
102  it = pParams.find("OutputPlane");
103  if (it == pParams.end())
104  {
105  m_outputPlane = 0;
106  }
107  else
108  {
109  LibUtilities::Equation equ(m_session, it->second);
110  m_outputPlane = floor(equ.Evaluate());
111  }
112  }
113 
115  AllocateSharedPtr(pSession->GetComm());
116 
117 }
118 
119 /**
120  * Destructor.
121  */
123 {
124 
125 }
126 
127 /**
128  * Initialize the parallel communication and the output stream.
129  */
132  const NekDouble &time)
133 {
134  LibUtilities::CommSharedPtr vComm = pFields[0]->GetComm();
135 
136  if (vComm->GetRank() == 0)
137  {
138  // Open output stream
140  {
141  m_outputStream.open(m_outputFile.c_str());
142  m_outputStream << "# Time, Fourier Mode, Energy ";
143  m_outputStream << endl;
144  }
145  else
146  {
147  m_outputStream.open(m_outputFile.c_str());
148  m_outputStream << "# Time, Energy ";
149  m_outputStream << endl;
150  }
151 
152  }
153 
154  m_index = 0;
155  v_Update(pFields, time);
156 }
157 
158 
159 /**
160  * Update the modal energy every m_outputFrequency.
161  */
164  const NekDouble &time)
165 {
166  // Only output every m_outputFrequency
167  if ((m_index++) % m_outputFrequency)
168  {
169  return;
170  }
171 
172  LibUtilities::CommSharedPtr vComm = pFields[0]->GetComm();
173 
174  // Homogeneous 1D implementation
175  if (m_isHomogeneous1D)
176  {
177  int colrank = vComm->GetColumnComm()->GetRank();
178  int nproc = vComm->GetColumnComm()->GetSize();
179  m_npointsZ = (m_session->GetParameter("HomModesZ"));
180  int locsize = m_npointsZ/nproc/2;
181 
182  Array<OneD, NekDouble> energy (locsize, 0.0);
183  Array<OneD, NekDouble> energy_tmp(locsize, 0.0);
185 
186  // Calculate the energy of the perturbation for stability
187  // analysis
188  if (m_PertEnergy)
189  {
190  // Compressible Flow Solver
191  if (m_EqTypeStr=="EulerCFE" ||
192  m_EqTypeStr=="EulerADCFE" ||
193  m_EqTypeStr=="NavierStokesCFE")
194  {
195  ASSERTL0(false, "Stability analysis module not "
196  "implemented for the Compressible Flow "
197  "Solver. Please remove the function BaseFlow "
198  "from your .xml file");
199  }
200  // Incompressible Navier-Stokes Solver
201  else
202  {
205  SetUpBaseFields(graphShrPtr);
206  string file = m_session->
207  GetFunctionFilename("BaseFlow", 0);
208  ImportFldBase(file, graphShrPtr);
209 
210  for (int i = 0; i < pFields.num_elements()-1; ++i)
211  {
212  Vmath::Vsub(pFields[i]->GetNcoeffs(),
213  pFields[i]->GetCoeffs(), 1,
214  m_base [i]->GetCoeffs(), 1,
215  pFields[i]->UpdateCoeffs(), 1);
216 
217  energy_tmp = pFields[i]->HomogeneousEnergy();
218  Vmath::Vadd(locsize, energy_tmp, 1,
219  energy, 1, energy, 1);
220 
221  Vmath::Vadd(pFields[i]->GetNcoeffs(),
222  pFields[i]->GetCoeffs(), 1,
223  m_base[i]->GetCoeffs(), 1,
224  pFields[i]->UpdateCoeffs(), 1);
225  }
226  }
227  }
228  // Calculate the modal energy for general simulation
229  else
230  {
231  // Compressible Flow Solver
232  if (m_EqTypeStr=="EulerCFE" ||
233  m_EqTypeStr=="EulerADCFE" ||
234  m_EqTypeStr=="NavierStokesCFE")
235  {
236  // Extracting kinetic energy
237  for (int i = 1; i < pFields.num_elements()-1; ++i)
238  {
239  energy_tmp = pFields[i]->HomogeneousEnergy();
240  Vmath::Vadd(locsize, energy_tmp, 1,
241  energy, 1, energy, 1);
242  }
243  }
244  // Incompressible Navier-Stokes Solver
245  else
246  {
247  // Extracting kinetic energy
248  for (int i = 0; i < pFields.num_elements()-1; ++i)
249  {
250  energy_tmp = pFields[i]->HomogeneousEnergy();
251  Vmath::Vadd(locsize, energy_tmp, 1,
252  energy, 1, energy, 1);
253  }
254  }
255  }
256 
257  // Send to root process
258  if (colrank == 0)
259  {
260  int j, m = 0;
261 
262  for (j = 0; j < energy.num_elements(); ++j, ++m)
263  {
264  m_outputStream << setw(10) << time
265  << setw(5) << m
266  << setw(18) << energy[j] << endl;
267  }
268 
269  for (int i = 1; i < nproc; ++i)
270  {
271  vComm->GetColumnComm()->Recv(i, energy);
272 
273  for (j = 0; j < energy.num_elements(); ++j, ++m)
274  {
275  m_outputStream << setw(10) << time
276  << setw(5) << m
277  << setw(18) << energy[j] << endl;
278  }
279  }
280  }
281  else
282  {
283  vComm->GetColumnComm()->Send(0, energy);
284  }
285  }
286  // Homogeneous 2D implementation
287  else if (m_isHomogeneous2D)
288  {
289  ASSERTL0(false, "3D Homogeneous 2D energy "
290  "dumping not implemented yet");
291  }
292  // General implementation
293  else
294  {
295  // Compressible Flow Solver
296  if (m_EqTypeStr=="EulerCFE" ||
297  m_EqTypeStr=="EulerADCFE" ||
298  m_EqTypeStr=="NavierStokesCFE")
299  {
300  // Total energy
301  NekDouble energy = 0.0;
302  for (int i = 1; i < pFields.num_elements()-1; ++i)
303  {
304  pFields[i]->SetPhysState(true);
305  NekDouble norm = L2Error(pFields, i, time);
306  energy += norm * norm;
307  }
308 
309  m_outputStream << setprecision(6) << time;
310  m_outputStream.width(25);
311  m_outputStream << setprecision(8) << 0.5*energy;
312  m_outputStream << endl;
313  }
314  // Incompressible Navier-Stokes Solver
315  else
316  {
317  // Kinetic energy
318  NekDouble energy = 0.0;
319  for (int i = 0; i < pFields.num_elements()-1; ++i)
320  {
321  pFields[i]->SetPhysState(true);
322  NekDouble norm = L2Error(pFields, i, time);
323  energy += norm * norm;
324  }
325  m_outputStream << setprecision(6) << time;
326  m_outputStream.width(25);
327  m_outputStream << setprecision(8) << 0.5*energy;
328  m_outputStream << endl;
329  }
330  }
331 }
332 
333 /**
334  * Close the output stream.
335  */
338  const NekDouble &time)
339 {
340  if (pFields[0]->GetComm()->GetRank() == 0)
341  {
342  m_outputStream.close();
343  }
344 }
345 
346 /**
347  * Calculate the L2 norm of a given field for calculating the
348  * modal energy.
349  */
352  unsigned int field,
353  const NekDouble &time)
354 {
355  NekDouble L2error = -1.0;
356  LibUtilities::CommSharedPtr vComm = pFields[0]->GetComm();
357 
358  if (m_NumQuadPointsError == 0)
359  {
360  if (pFields[field]->GetPhysState() == false)
361  {
362  pFields[field]->BwdTrans(pFields[field]->GetCoeffs(),
363  pFields[field]->UpdatePhys());
364  }
365  }
366 
367  L2error = pFields[field]->L2(pFields[field]->GetPhys());
368  return L2error;
369 }
370 
371 /**
372  * Setup the base fields in case of stability analyses.
373  */
376 {
377  int i;
378  int m_expdim = graphShrPtr->GetMeshDimension();
379 
380  //definition of the projection tipe:
381  if(m_session->DefinesSolverInfo("PROJECTION"))
382  {
383  std::string ProjectStr = m_session->GetSolverInfo("PROJECTION");
384 
385  if ((ProjectStr == "Continuous") ||
386  (ProjectStr == "Galerkin") ||
387  (ProjectStr == "CONTINUOUS") ||
388  (ProjectStr == "GALERKIN"))
389  {
391  }
392  else if ((ProjectStr == "MixedCGDG") ||
393  (ProjectStr == "Mixed_CG_Discontinuous"))
394  {
396  }
397  else if(ProjectStr == "DisContinuous")
398  {
400  }
401  else
402  {
403  ASSERTL0(false, "PROJECTION value not recognised");
404  }
405  }
406  else
407  {
408  cerr << "Projection type not specified in SOLVERINFO,"
409  "defaulting to continuous Galerkin" << endl;
411  }
412 
413  if (m_session->DefinesSolverInfo("ModeType"))
414  {
415  m_session->MatchSolverInfo("ModeType", "SingleMode",
416  m_SingleMode, false);
417  m_session->MatchSolverInfo("ModeType", "HalfMode",
418  m_HalfMode, false);
419  m_session->MatchSolverInfo("ModeType", "MultipleModes",
420  m_MultipleModes, false);
421  }
422 
423  m_session->MatchSolverInfo("USEFFT","FFTW", m_useFFT, false);
424  m_session->MatchSolverInfo("DEALIASING", "True",
425  m_homogen_dealiasing, false);
426 
427  if (m_homogen_dealiasing == false)
428  {
429  m_session->MatchSolverInfo("DEALIASING", "On",
430  m_homogen_dealiasing, false);
431  }
432 
433  // Stability Analysis flags
434  if (m_session->DefinesSolverInfo("ModeType"))
435  {
436  if (m_SingleMode)
437  {
438  m_npointsZ = 2;
439  }
440  else if (m_HalfMode)
441  {
442  m_npointsZ = 1;
443  }
444  else if (m_MultipleModes)
445  {
446  m_npointsZ = m_session->GetParameter("HomModesZ");
447  }
448  else
449  {
450  ASSERTL0(false, "SolverInfo ModeType not valid");
451  }
452  }
453  else
454  {
455  m_npointsZ = m_session->GetParameter("HomModesZ");
456  }
457 
460  {
461  switch (m_expdim)
462  {
463  case 1:
464  {
465  for(i = 0; i < m_base.num_elements(); i++)
466  {
468  ::AllocateSharedPtr(m_session, graphShrPtr,
469  m_session->GetVariable(0));
470  }
471  }
472  break;
473  case 2:
474  {
475  if (m_isHomogeneous1D)
476  {
477  if (m_SingleMode)
478  {
479  const LibUtilities::PointsKey PkeyZ(
480  m_npointsZ,
482  const LibUtilities::BasisKey BkeyZ(
484  m_npointsZ, PkeyZ);
485 
486  for (i = 0 ; i < m_base.num_elements(); i++)
487  {
490  AllocateSharedPtr(
491  m_session, BkeyZ, m_LhomZ,
493  graphShrPtr,
494  m_session->GetVariable(i));
495 
496  m_base[i]->SetWaveSpace(true);
497  }
498  }
499  else if (m_HalfMode)
500  {
501  //1 plane field (half mode expansion)
502  const LibUtilities::PointsKey PkeyZ(
503  m_npointsZ,
505  const LibUtilities::BasisKey BkeyZ(
507  m_npointsZ,PkeyZ);
508 
509  for (i = 0 ; i < m_base.num_elements(); i++)
510  {
513  AllocateSharedPtr(
514  m_session, BkeyZ, m_LhomZ,
516  graphShrPtr,
517  m_session->GetVariable(i));
518 
519  m_base[i]->SetWaveSpace(true);
520  }
521  }
522  else
523  {
524  const LibUtilities::PointsKey PkeyZ(
525  m_npointsZ,
527  const LibUtilities::BasisKey BkeyZ(
529 
530  for (i = 0 ; i < m_base.num_elements(); i++)
531  {
534  AllocateSharedPtr(
535  m_session, BkeyZ, m_LhomZ,
537  graphShrPtr,
538  m_session->GetVariable(i));
539 
540  m_base[i]->SetWaveSpace(false);
541  }
542  }
543  }
544  else
545  {
546  i = 0;
550  m_session,graphShrPtr,
551  m_session->GetVariable(i));
552 
553  m_base[0] = firstbase;
554 
555  for (i = 1 ; i < m_base.num_elements(); i++)
556  {
557  m_base[i] = MemoryManager<MultiRegions::
558  ContField2D>::AllocateSharedPtr(
559  *firstbase, graphShrPtr,
560  m_session->GetVariable(i));
561  }
562  }
563  }
564  break;
565  case 3:
566  {
569  AllocateSharedPtr(m_session, graphShrPtr,
570  m_session->GetVariable(0));
571  m_base[0] = firstbase;
572  for (i = 1 ; i < m_base.num_elements(); i++)
573  {
574  m_base[i] = MemoryManager<MultiRegions::
575  ContField3D>::AllocateSharedPtr(
576  *firstbase, graphShrPtr,
577  m_session->GetVariable(0));
578  }
579  }
580  break;
581  default:
582  ASSERTL0(false, "Expansion dimension not recognised");
583  break;
584  }
585  }
586  else
587  {
588  switch (m_expdim)
589  {
590  case 1:
591  {
592  // need to use zero for variable as may be more base
593  // flows than variables
594  for (i = 0 ; i < m_base.num_elements(); i++)
595  {
597  DisContField1D>::AllocateSharedPtr(
598  m_session, graphShrPtr,
599  m_session->GetVariable(0));
600  }
601  break;
602  }
603  case 2:
604  {
605  for (i = 0 ; i < m_base.num_elements(); i++)
606  {
608  DisContField2D>::AllocateSharedPtr(
609  m_session, graphShrPtr,
610  m_session->GetVariable(0));
611  }
612  break;
613  }
614  case 3:
615  ASSERTL0(false, "3D not set up");
616  default:
617  ASSERTL0(false, "Expansion dimension not recognised");
618  break;
619  }
620  }
621 }
622 
623 /**
624  * Import the base flow fld file.
625  */
627  std::string pInfile,
629 {
630  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef;
631  std::vector<std::vector<NekDouble> > FieldData;
632 
633  // Get Homogeneous
634  m_fld->Import(pInfile,FieldDef,FieldData);
635 
636  int nvar = m_session->GetVariables().size();
637  if(m_session->DefinesSolverInfo("HOMOGENEOUS"))
638  {
639  std::string HomoStr = m_session->GetSolverInfo("HOMOGENEOUS");
640  }
641  // Copy FieldData into m_fields
642  for (int j = 0; j < nvar; ++j)
643  {
644  for (int i = 0; i < FieldDef.size(); ++i)
645  {
646  bool flag =
647  FieldDef[i]->m_fields[j] == m_session->GetVariable(j);
648 
649  ASSERTL0(flag, (std::string("Order of ") + pInfile
650  + std::string(" data and that defined in "
651  "m_boundaryconditions differs")).c_str());
652 
653  m_base[j]->ExtractDataToCoeffs(FieldDef[i], FieldData[i],
654  FieldDef[i]->m_fields[j],
655  m_base[j]->UpdateCoeffs());
656  }
657  }
658 }
659 
660 /**
661  * Flag for time-dependent flows.
662  */
664 {
665  return true;
666 }
667 }
668 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:188
enum MultiRegions::ProjectionType m_projectionType
static boost::shared_ptr< MeshGraph > Read(const LibUtilities::SessionReaderSharedPtr &pSession, DomainRangeShPtr &rng=NullDomainRangeShPtr)
Definition: MeshGraph.cpp:121
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
virtual void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
virtual void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
STL namespace.
boost::shared_ptr< ContField2D > ContField2DSharedPtr
Definition: ContField2D.h:293
Array< OneD, MultiRegions::ExpListSharedPtr > m_base
Fourier Expansion .
Definition: BasisType.h:52
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:51
LibUtilities::FieldIOSharedPtr m_fld
boost::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:53
1D Evenly-spaced points using Fourier Fit
Definition: PointsType.h:64
Fourier Modified expansions with just the real part of the first mode .
Definition: BasisType.h:59
This class is the abstraction of a global continuous two- dimensional spectral/hp element expansion w...
Definition: ContField2D.h:56
void SetUpBaseFields(SpatialDomains::MeshGraphSharedPtr &mesh)
NekDouble Evaluate() const
Definition: Equation.h:102
This class is the abstraction of a global discontinuous two- dimensional spectral/hp element expansio...
Defines a specification for a set of points.
Definition: Points.h:58
double NekDouble
void ImportFldBase(std::string pInfile, SpatialDomains::MeshGraphSharedPtr pGraph)
std::map< std::string, std::string > ParamMap
Definition: Filter.h:67
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:84
NekDouble L2Error(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, unsigned int field, const NekDouble &time)
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:329
virtual void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
1D Non Evenly-spaced points for Single Mode analysis
Definition: PointsType.h:65
FilterFactory & GetFilterFactory()
Definition: Filter.cpp:42
virtual SOLVER_UTILS_EXPORT ~FilterModalEnergy()
boost::shared_ptr< ContField3D > ContField3DSharedPtr
Definition: ContField3D.h:191
boost::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:442
Describes the specification for a Basis.
Definition: Basis.h:50
void Vadd(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Add vector z = x+y.
Definition: Vmath.cpp:285