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
139  bool adaptive;
140  m_session->MatchSolverInfo("Driver", "Adaptive",
141  adaptive, false);
142  if (adaptive)
143  {
144  m_outputStream.open(m_outputFile.c_str(), ofstream::app);
145  }
146  else
147  {
148  m_outputStream.open(m_outputFile.c_str());
149  }
151  {
152  m_outputStream << "# Time, Fourier Mode, Energy ";
153  m_outputStream << endl;
154  }
155  else
156  {
157  m_outputStream << "# Time, Energy ";
158  m_outputStream << endl;
159  }
160 
161  }
162 
163  m_index = 0;
164  v_Update(pFields, time);
165 }
166 
167 
168 /**
169  * Update the modal energy every m_outputFrequency.
170  */
173  const NekDouble &time)
174 {
175  // Only output every m_outputFrequency
176  if ((m_index++) % m_outputFrequency)
177  {
178  return;
179  }
180 
181  LibUtilities::CommSharedPtr vComm = pFields[0]->GetComm();
182 
183  // Homogeneous 1D implementation
184  if (m_isHomogeneous1D)
185  {
186  int colrank = vComm->GetColumnComm()->GetRank();
187  int nproc = vComm->GetColumnComm()->GetSize();
188  m_npointsZ = (m_session->GetParameter("HomModesZ"));
189  int locsize = m_npointsZ/nproc/2;
190 
191  Array<OneD, NekDouble> energy (locsize, 0.0);
192  Array<OneD, NekDouble> energy_tmp(locsize, 0.0);
194 
195  // Calculate the energy of the perturbation for stability
196  // analysis
197  if (m_PertEnergy)
198  {
199  // Compressible Flow Solver
200  if (m_EqTypeStr=="EulerCFE" ||
201  m_EqTypeStr=="EulerADCFE" ||
202  m_EqTypeStr=="NavierStokesCFE")
203  {
204  ASSERTL0(false, "Stability analysis module not "
205  "implemented for the Compressible Flow "
206  "Solver. Please remove the function BaseFlow "
207  "from your .xml file");
208  }
209  // Incompressible Navier-Stokes Solver
210  else
211  {
214  SetUpBaseFields(graphShrPtr);
215  string file = m_session->
216  GetFunctionFilename("BaseFlow", 0);
217  ImportFldBase(file, graphShrPtr);
218 
219  for (int i = 0; i < pFields.num_elements()-1; ++i)
220  {
221  Vmath::Vsub(pFields[i]->GetNcoeffs(),
222  pFields[i]->GetCoeffs(), 1,
223  m_base [i]->GetCoeffs(), 1,
224  pFields[i]->UpdateCoeffs(), 1);
225 
226  energy_tmp = pFields[i]->HomogeneousEnergy();
227  Vmath::Vadd(locsize, energy_tmp, 1,
228  energy, 1, energy, 1);
229 
230  Vmath::Vadd(pFields[i]->GetNcoeffs(),
231  pFields[i]->GetCoeffs(), 1,
232  m_base[i]->GetCoeffs(), 1,
233  pFields[i]->UpdateCoeffs(), 1);
234  }
235  }
236  }
237  // Calculate the modal energy for general simulation
238  else
239  {
240  // Compressible Flow Solver
241  if (m_EqTypeStr=="EulerCFE" ||
242  m_EqTypeStr=="EulerADCFE" ||
243  m_EqTypeStr=="NavierStokesCFE")
244  {
245  // Extracting kinetic energy
246  for (int i = 1; i < pFields.num_elements()-1; ++i)
247  {
248  energy_tmp = pFields[i]->HomogeneousEnergy();
249  Vmath::Vadd(locsize, energy_tmp, 1,
250  energy, 1, energy, 1);
251  }
252  }
253  // Incompressible Navier-Stokes Solver
254  else
255  {
256  // Extracting kinetic energy
257  for (int i = 0; i < pFields.num_elements()-1; ++i)
258  {
259  energy_tmp = pFields[i]->HomogeneousEnergy();
260  Vmath::Vadd(locsize, energy_tmp, 1,
261  energy, 1, energy, 1);
262  }
263  }
264  }
265 
266  // Send to root process
267  if (colrank == 0)
268  {
269  int j, m = 0;
270 
271  for (j = 0; j < energy.num_elements(); ++j, ++m)
272  {
273  m_outputStream << setw(10) << time
274  << setw(5) << m
275  << setw(18) << energy[j] << endl;
276  }
277 
278  for (int i = 1; i < nproc; ++i)
279  {
280  vComm->GetColumnComm()->Recv(i, energy);
281 
282  for (j = 0; j < energy.num_elements(); ++j, ++m)
283  {
284  m_outputStream << setw(10) << time
285  << setw(5) << m
286  << setw(18) << energy[j] << endl;
287  }
288  }
289  }
290  else
291  {
292  vComm->GetColumnComm()->Send(0, energy);
293  }
294  }
295  // Homogeneous 2D implementation
296  else if (m_isHomogeneous2D)
297  {
298  ASSERTL0(false, "3D Homogeneous 2D energy "
299  "dumping not implemented yet");
300  }
301  // General implementation
302  else
303  {
304  // Compressible Flow Solver
305  if (m_EqTypeStr=="EulerCFE" ||
306  m_EqTypeStr=="EulerADCFE" ||
307  m_EqTypeStr=="NavierStokesCFE")
308  {
309  // Total energy
310  NekDouble energy = 0.0;
311  for (int i = 1; i < pFields.num_elements()-1; ++i)
312  {
313  pFields[i]->SetPhysState(true);
314  NekDouble norm = L2Error(pFields, i, time);
315  energy += norm * norm;
316  }
317 
318  m_outputStream << setprecision(6) << time;
319  m_outputStream.width(25);
320  m_outputStream << setprecision(8) << 0.5*energy;
321  m_outputStream << endl;
322  }
323  // Incompressible Navier-Stokes Solver
324  else
325  {
326  // Kinetic energy
327  NekDouble energy = 0.0;
328  for (int i = 0; i < pFields.num_elements()-1; ++i)
329  {
330  pFields[i]->SetPhysState(true);
331  NekDouble norm = L2Error(pFields, i, time);
332  energy += norm * norm;
333  }
334  m_outputStream << setprecision(6) << time;
335  m_outputStream.width(25);
336  m_outputStream << setprecision(8) << 0.5*energy;
337  m_outputStream << endl;
338  }
339  }
340 }
341 
342 /**
343  * Close the output stream.
344  */
347  const NekDouble &time)
348 {
349  if (pFields[0]->GetComm()->GetRank() == 0)
350  {
351  m_outputStream.close();
352  }
353 }
354 
355 /**
356  * Calculate the L2 norm of a given field for calculating the
357  * modal energy.
358  */
361  unsigned int field,
362  const NekDouble &time)
363 {
364  NekDouble L2error = -1.0;
365  LibUtilities::CommSharedPtr vComm = pFields[0]->GetComm();
366 
367  if (m_NumQuadPointsError == 0)
368  {
369  if (pFields[field]->GetPhysState() == false)
370  {
371  pFields[field]->BwdTrans(pFields[field]->GetCoeffs(),
372  pFields[field]->UpdatePhys());
373  }
374  }
375 
376  L2error = pFields[field]->L2(pFields[field]->GetPhys());
377  return L2error;
378 }
379 
380 /**
381  * Setup the base fields in case of stability analyses.
382  */
385 {
386  int i;
387  int m_expdim = graphShrPtr->GetMeshDimension();
388 
389  //definition of the projection tipe:
390  if(m_session->DefinesSolverInfo("PROJECTION"))
391  {
392  std::string ProjectStr = m_session->GetSolverInfo("PROJECTION");
393 
394  if ((ProjectStr == "Continuous") ||
395  (ProjectStr == "Galerkin") ||
396  (ProjectStr == "CONTINUOUS") ||
397  (ProjectStr == "GALERKIN"))
398  {
400  }
401  else if ((ProjectStr == "MixedCGDG") ||
402  (ProjectStr == "Mixed_CG_Discontinuous"))
403  {
405  }
406  else if(ProjectStr == "DisContinuous")
407  {
409  }
410  else
411  {
412  ASSERTL0(false, "PROJECTION value not recognised");
413  }
414  }
415  else
416  {
417  cerr << "Projection type not specified in SOLVERINFO,"
418  "defaulting to continuous Galerkin" << endl;
420  }
421 
422  if (m_session->DefinesSolverInfo("ModeType"))
423  {
424  m_session->MatchSolverInfo("ModeType", "SingleMode",
425  m_SingleMode, false);
426  m_session->MatchSolverInfo("ModeType", "HalfMode",
427  m_HalfMode, false);
428  m_session->MatchSolverInfo("ModeType", "MultipleModes",
429  m_MultipleModes, false);
430  }
431 
432  m_session->MatchSolverInfo("USEFFT","FFTW", m_useFFT, false);
433  m_session->MatchSolverInfo("DEALIASING", "True",
434  m_homogen_dealiasing, false);
435 
436  if (m_homogen_dealiasing == false)
437  {
438  m_session->MatchSolverInfo("DEALIASING", "On",
439  m_homogen_dealiasing, false);
440  }
441 
442  // Stability Analysis flags
443  if (m_session->DefinesSolverInfo("ModeType"))
444  {
445  if (m_SingleMode)
446  {
447  m_npointsZ = 2;
448  }
449  else if (m_HalfMode)
450  {
451  m_npointsZ = 1;
452  }
453  else if (m_MultipleModes)
454  {
455  m_npointsZ = m_session->GetParameter("HomModesZ");
456  }
457  else
458  {
459  ASSERTL0(false, "SolverInfo ModeType not valid");
460  }
461  }
462  else
463  {
464  m_npointsZ = m_session->GetParameter("HomModesZ");
465  }
466 
469  {
470  switch (m_expdim)
471  {
472  case 1:
473  {
474  for(i = 0; i < m_base.num_elements(); i++)
475  {
477  ::AllocateSharedPtr(m_session, graphShrPtr,
478  m_session->GetVariable(0));
479  }
480  }
481  break;
482  case 2:
483  {
484  if (m_isHomogeneous1D)
485  {
486  if (m_SingleMode)
487  {
488  const LibUtilities::PointsKey PkeyZ(
489  m_npointsZ,
491  const LibUtilities::BasisKey BkeyZ(
493  m_npointsZ, PkeyZ);
494 
495  for (i = 0 ; i < m_base.num_elements(); i++)
496  {
499  AllocateSharedPtr(
500  m_session, BkeyZ, m_LhomZ,
502  graphShrPtr,
503  m_session->GetVariable(i));
504 
505  m_base[i]->SetWaveSpace(true);
506  }
507  }
508  else if (m_HalfMode)
509  {
510  //1 plane field (half mode expansion)
511  const LibUtilities::PointsKey PkeyZ(
512  m_npointsZ,
514  const LibUtilities::BasisKey BkeyZ(
516  m_npointsZ,PkeyZ);
517 
518  for (i = 0 ; i < m_base.num_elements(); i++)
519  {
522  AllocateSharedPtr(
523  m_session, BkeyZ, m_LhomZ,
525  graphShrPtr,
526  m_session->GetVariable(i));
527 
528  m_base[i]->SetWaveSpace(true);
529  }
530  }
531  else
532  {
533  const LibUtilities::PointsKey PkeyZ(
534  m_npointsZ,
536  const LibUtilities::BasisKey BkeyZ(
538 
539  for (i = 0 ; i < m_base.num_elements(); i++)
540  {
543  AllocateSharedPtr(
544  m_session, BkeyZ, m_LhomZ,
546  graphShrPtr,
547  m_session->GetVariable(i));
548 
549  m_base[i]->SetWaveSpace(false);
550  }
551  }
552  }
553  else
554  {
555  i = 0;
559  m_session,graphShrPtr,
560  m_session->GetVariable(i));
561 
562  m_base[0] = firstbase;
563 
564  for (i = 1 ; i < m_base.num_elements(); i++)
565  {
566  m_base[i] = MemoryManager<MultiRegions::
567  ContField2D>::AllocateSharedPtr(
568  *firstbase, graphShrPtr,
569  m_session->GetVariable(i));
570  }
571  }
572  }
573  break;
574  case 3:
575  {
578  AllocateSharedPtr(m_session, graphShrPtr,
579  m_session->GetVariable(0));
580  m_base[0] = firstbase;
581  for (i = 1 ; i < m_base.num_elements(); i++)
582  {
583  m_base[i] = MemoryManager<MultiRegions::
584  ContField3D>::AllocateSharedPtr(
585  *firstbase, graphShrPtr,
586  m_session->GetVariable(0));
587  }
588  }
589  break;
590  default:
591  ASSERTL0(false, "Expansion dimension not recognised");
592  break;
593  }
594  }
595  else
596  {
597  switch (m_expdim)
598  {
599  case 1:
600  {
601  // need to use zero for variable as may be more base
602  // flows than variables
603  for (i = 0 ; i < m_base.num_elements(); i++)
604  {
606  DisContField1D>::AllocateSharedPtr(
607  m_session, graphShrPtr,
608  m_session->GetVariable(0));
609  }
610  break;
611  }
612  case 2:
613  {
614  for (i = 0 ; i < m_base.num_elements(); i++)
615  {
617  DisContField2D>::AllocateSharedPtr(
618  m_session, graphShrPtr,
619  m_session->GetVariable(0));
620  }
621  break;
622  }
623  case 3:
624  ASSERTL0(false, "3D not set up");
625  default:
626  ASSERTL0(false, "Expansion dimension not recognised");
627  break;
628  }
629  }
630 }
631 
632 /**
633  * Import the base flow fld file.
634  */
636  std::string pInfile,
638 {
639  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef;
640  std::vector<std::vector<NekDouble> > FieldData;
641 
642  // Get Homogeneous
643  m_fld->Import(pInfile,FieldDef,FieldData);
644 
645  int nvar = m_session->GetVariables().size();
646  if(m_session->DefinesSolverInfo("HOMOGENEOUS"))
647  {
648  std::string HomoStr = m_session->GetSolverInfo("HOMOGENEOUS");
649  }
650  // Copy FieldData into m_fields
651  for (int j = 0; j < nvar; ++j)
652  {
653  for (int i = 0; i < FieldDef.size(); ++i)
654  {
655  bool flag =
656  FieldDef[i]->m_fields[j] == m_session->GetVariable(j);
657 
658  ASSERTL0(flag, (std::string("Order of ") + pInfile
659  + std::string(" data and that defined in "
660  "m_boundaryconditions differs")).c_str());
661 
662  m_base[j]->ExtractDataToCoeffs(FieldDef[i], FieldData[i],
663  FieldDef[i]->m_fields[j],
664  m_base[j]->UpdateCoeffs());
665  }
666  }
667 }
668 
669 /**
670  * Flag for time-dependent flows.
671  */
673 {
674  return true;
675 }
676 }
677 }
#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