Nektar++
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// 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 values of the modal energy
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <iomanip>
36
39
40using namespace std;
41
42namespace Nektar::SolverUtils
43{
47
48/**
49 * Constructor.
50 */
53 const std::weak_ptr<EquationSystem> &pEquation, const ParamMap &pParams)
54 : Filter(pSession, pEquation)
55{
56 // OutputFile
57 auto it = pParams.find("OutputFile");
58 if (it == pParams.end())
59 {
60 m_outputFile = m_session->GetSessionName();
61 }
62 else
63 {
64 ASSERTL0(it->second.length() > 0, "Missing parameter 'OutputFile'.");
65 m_outputFile = it->second;
66 }
67 if (!(m_outputFile.length() >= 4 &&
68 m_outputFile.substr(m_outputFile.length() - 4) == ".mdl"))
69 {
70 m_outputFile += ".mdl";
71 }
72
73 // OutputFrequency
74 it = pParams.find("OutputFrequency");
75 if (it == pParams.end())
76 {
78 }
79 else
80 {
81 LibUtilities::Equation equ(m_session->GetInterpreter(), it->second);
82 m_outputFrequency = round(equ.Evaluate());
83 }
84
85 m_session->MatchSolverInfo("Homogeneous", "1D", m_isHomogeneous1D, false);
86 m_session->MatchSolverInfo("Homogeneous", "2D", m_isHomogeneous2D, false);
87 m_session->MatchSolverInfo("CalculatePerturbationEnergy", "True",
88 m_PertEnergy, false);
89 m_session->LoadParameter("NumQuadPointsError", m_NumQuadPointsError, 0);
90 m_EqTypeStr = m_session->GetSolverInfo("EQTYPE");
91
92 // OutputPlane
94 {
95 m_session->LoadParameter("LZ", m_LhomZ);
96
97 it = pParams.find("OutputPlane");
98 if (it == pParams.end())
99 {
100 m_outputPlane = 0;
101 }
102 else
103 {
104 LibUtilities::Equation equ(m_session->GetInterpreter(), it->second);
105 m_outputPlane = round(equ.Evaluate());
106 }
107 }
108
110}
111
112/**
113 * Destructor.
114 */
116{
117}
118
119/**
120 * Initialize the parallel communication and the output stream.
121 */
124 const NekDouble &time)
125{
126 LibUtilities::CommSharedPtr vComm = pFields[0]->GetComm();
127
128 if (vComm->GetRank() == 0)
129 {
130 // Open output stream
131 bool adaptive;
132 m_session->MatchSolverInfo("Driver", "Adaptive", adaptive, false);
133 if (adaptive)
134 {
135 m_outputStream.open(m_outputFile.c_str(), ofstream::app);
136 }
137 else
138 {
139 m_outputStream.open(m_outputFile.c_str());
140 }
142 {
143 m_outputStream << "# Time, Fourier Mode, Energy ";
144 m_outputStream << endl;
145 }
146 else
147 {
148 m_outputStream << "# Time, Energy ";
149 m_outputStream << endl;
150 }
151 }
152
153 m_index = 0;
154 v_Update(pFields, time);
155}
156
157/**
158 * Update the modal energy every m_outputFrequency.
159 */
162 const NekDouble &time)
163{
164 // Only output every m_outputFrequency
165 if ((m_index++) % m_outputFrequency)
166 {
167 return;
168 }
169
170 LibUtilities::CommSharedPtr vComm = pFields[0]->GetComm();
171
172 // Homogeneous 1D implementation
174 {
175 int colrank = vComm->GetColumnComm()->GetRank();
176 int nproc = vComm->GetColumnComm()->GetSize();
177 m_npointsZ = (m_session->GetParameter("HomModesZ"));
178 int locsize = m_npointsZ / nproc / 2;
179
180 Array<OneD, NekDouble> energy(locsize, 0.0);
181 Array<OneD, NekDouble> energy_tmp(locsize, 0.0);
183
184 // Calculate the energy of the perturbation for stability
185 // analysis
186 if (m_PertEnergy)
187 {
188 // Compressible Flow Solver
189 if (m_EqTypeStr == "EulerCFE" || m_EqTypeStr == "EulerADCFE" ||
190 m_EqTypeStr == "NavierStokesCFE")
191 {
192 ASSERTL0(false, "Stability analysis module not "
193 "implemented for the Compressible Flow "
194 "Solver. Please remove the function BaseFlow "
195 "from your .xml file");
196 }
197 // Incompressible Navier-Stokes Solver
198 else
199 {
202 SetUpBaseFields(graphShrPtr);
203 string file = m_session->GetFunctionFilename("BaseFlow", 0);
204 ImportFldBase(file);
205
206 for (int i = 0; i < pFields.size() - 1; ++i)
207 {
208 Vmath::Vsub(pFields[i]->GetNcoeffs(),
209 pFields[i]->GetCoeffs(), 1,
210 m_base[i]->GetCoeffs(), 1,
211 pFields[i]->UpdateCoeffs(), 1);
212
213 energy_tmp = pFields[i]->HomogeneousEnergy();
214 Vmath::Vadd(locsize, energy_tmp, 1, energy, 1, energy, 1);
215
216 Vmath::Vadd(pFields[i]->GetNcoeffs(),
217 pFields[i]->GetCoeffs(), 1,
218 m_base[i]->GetCoeffs(), 1,
219 pFields[i]->UpdateCoeffs(), 1);
220 }
221 }
222 }
223 // Calculate the modal energy for general simulation
224 else
225 {
226 // Compressible Flow Solver
227 if (m_EqTypeStr == "EulerCFE" || m_EqTypeStr == "EulerADCFE" ||
228 m_EqTypeStr == "NavierStokesCFE")
229 {
230 // Extracting kinetic energy
231 for (int i = 1; i < pFields.size() - 1; ++i)
232 {
233 energy_tmp = pFields[i]->HomogeneousEnergy();
234 Vmath::Vadd(locsize, energy_tmp, 1, energy, 1, energy, 1);
235 }
236 }
237 // Incompressible Navier-Stokes Solver
238 else
239 {
240 // Extracting kinetic energy
241 for (int i = 0; i < pFields.size() - 1; ++i)
242 {
243 energy_tmp = pFields[i]->HomogeneousEnergy();
244 Vmath::Vadd(locsize, energy_tmp, 1, energy, 1, energy, 1);
245 }
246 }
247 }
248
249 // Send to root process
250 if (colrank == 0)
251 {
252 int j, m = 0;
253
254 for (j = 0; j < energy.size(); ++j, ++m)
255 {
256 m_outputStream << setw(10) << time << setw(5) << m << setw(18)
257 << energy[j] << endl;
258 }
259
260 for (int i = 1; i < nproc; ++i)
261 {
262 vComm->GetColumnComm()->Recv(i, energy);
263
264 for (j = 0; j < energy.size(); ++j, ++m)
265 {
266 m_outputStream << setw(10) << time << setw(5) << m
267 << setw(18) << energy[j] << endl;
268 }
269 }
270 }
271 else
272 {
273 vComm->GetColumnComm()->Send(0, energy);
274 }
275 }
276 // Homogeneous 2D implementation
277 else if (m_isHomogeneous2D)
278 {
279 ASSERTL0(false, "3D Homogeneous 2D energy "
280 "dumping not implemented yet");
281 }
282 // General implementation
283 else
284 {
285 // Compressible Flow Solver
286 if (m_EqTypeStr == "EulerCFE" || m_EqTypeStr == "EulerADCFE" ||
287 m_EqTypeStr == "NavierStokesCFE")
288 {
289 // Total energy
290 NekDouble energy = 0.0;
291 for (int i = 1; i < pFields.size() - 1; ++i)
292 {
293 pFields[i]->SetPhysState(true);
294 NekDouble norm = L2Error(pFields, i, time);
295 energy += norm * norm;
296 }
297
298 m_outputStream << setprecision(6) << time;
299 m_outputStream.width(25);
300 m_outputStream << setprecision(8) << 0.5 * energy;
301 m_outputStream << endl;
302 }
303 // Incompressible Navier-Stokes Solver
304 else
305 {
306 // Kinetic energy
307 NekDouble energy = 0.0;
308 for (int i = 0; i < pFields.size() - 1; ++i)
309 {
310 pFields[i]->SetPhysState(true);
311 NekDouble norm = L2Error(pFields, i, time);
312 energy += norm * norm;
313 }
314 m_outputStream << setprecision(6) << time;
315 m_outputStream.width(25);
316 m_outputStream << setprecision(8) << 0.5 * energy;
317 m_outputStream << endl;
318 }
319 }
320}
321
322/**
323 * Close the output stream.
324 */
327 [[maybe_unused]] const NekDouble &time)
328{
329 if (pFields[0]->GetComm()->GetRank() == 0)
330 {
331 m_outputStream.close();
332 }
333}
334
335/**
336 * Calculate the L2 norm of a given field for calculating the
337 * modal energy.
338 */
341 unsigned int field, [[maybe_unused]] const NekDouble &time)
342{
343 NekDouble L2error = -1.0;
344 LibUtilities::CommSharedPtr vComm = pFields[0]->GetComm();
345
346 if (m_NumQuadPointsError == 0)
347 {
348 if (pFields[field]->GetPhysState() == false)
349 {
350 pFields[field]->BwdTrans(pFields[field]->GetCoeffs(),
351 pFields[field]->UpdatePhys());
352 }
353 }
354
355 L2error = pFields[field]->L2(pFields[field]->GetPhys());
356 return L2error;
357}
358
359/**
360 * Setup the base fields in case of stability analyses.
361 */
364{
365 int i;
366 int m_expdim = graphShrPtr->GetMeshDimension();
367
368 // definition of the projection tipe:
369 if (m_session->DefinesSolverInfo("PROJECTION"))
370 {
371 std::string ProjectStr = m_session->GetSolverInfo("PROJECTION");
372
373 if ((ProjectStr == "Continuous") || (ProjectStr == "Galerkin") ||
374 (ProjectStr == "CONTINUOUS") || (ProjectStr == "GALERKIN"))
375 {
377 }
378 else if ((ProjectStr == "MixedCGDG") ||
379 (ProjectStr == "Mixed_CG_Discontinuous"))
380 {
382 }
383 else if (ProjectStr == "DisContinuous")
384 {
386 }
387 else
388 {
389 ASSERTL0(false, "PROJECTION value not recognised");
390 }
391 }
392 else
393 {
394 cerr << "Projection type not specified in SOLVERINFO,"
395 "defaulting to continuous Galerkin"
396 << endl;
398 }
399
400 if (m_session->DefinesSolverInfo("ModeType"))
401 {
402 m_session->MatchSolverInfo("ModeType", "SingleMode", m_SingleMode,
403 false);
404 m_session->MatchSolverInfo("ModeType", "HalfMode", m_HalfMode, false);
405 m_session->MatchSolverInfo("ModeType", "MultipleModes", m_MultipleModes,
406 false);
407 }
408
409 m_session->MatchSolverInfo("USEFFT", "FFTW", m_useFFT, false);
410 m_session->MatchSolverInfo("DEALIASING", "True", m_homogen_dealiasing,
411 false);
412
413 // Stability Analysis flags
414 if (m_session->DefinesSolverInfo("ModeType"))
415 {
416 if (m_SingleMode)
417 {
418 m_npointsZ = 2;
419 }
420 else if (m_HalfMode)
421 {
422 m_npointsZ = 1;
423 }
424 else if (m_MultipleModes)
425 {
426 m_npointsZ = m_session->GetParameter("HomModesZ");
427 }
428 else
429 {
430 ASSERTL0(false, "SolverInfo ModeType not valid");
431 }
432 }
433 else
434 {
435 m_npointsZ = m_session->GetParameter("HomModesZ");
436 }
437
440 {
441 switch (m_expdim)
442 {
443 case 1:
444 {
445 for (i = 0; i < m_base.size(); i++)
446 {
448 AllocateSharedPtr(m_session, graphShrPtr,
449 m_session->GetVariable(0));
450 }
451 }
452 break;
453 case 2:
454 {
456 {
457 if (m_SingleMode)
458 {
459 const LibUtilities::PointsKey PkeyZ(
461 const LibUtilities::BasisKey BkeyZ(
463
464 for (i = 0; i < m_base.size(); i++)
465 {
468 AllocateSharedPtr(
469 m_session, BkeyZ, m_LhomZ, m_useFFT,
470 m_homogen_dealiasing, graphShrPtr,
471 m_session->GetVariable(i));
472
473 m_base[i]->SetWaveSpace(true);
474 }
475 }
476 else if (m_HalfMode)
477 {
478 // 1 plane field (half mode expansion)
479 const LibUtilities::PointsKey PkeyZ(
481 const LibUtilities::BasisKey BkeyZ(
483 PkeyZ);
484
485 for (i = 0; i < m_base.size(); i++)
486 {
489 AllocateSharedPtr(
490 m_session, BkeyZ, m_LhomZ, m_useFFT,
491 m_homogen_dealiasing, graphShrPtr,
492 m_session->GetVariable(i));
493
494 m_base[i]->SetWaveSpace(true);
495 }
496 }
497 else
498 {
499 const LibUtilities::PointsKey PkeyZ(
501 const LibUtilities::BasisKey BkeyZ(
503
504 for (i = 0; i < m_base.size(); i++)
505 {
508 AllocateSharedPtr(
509 m_session, BkeyZ, m_LhomZ, m_useFFT,
510 m_homogen_dealiasing, graphShrPtr,
511 m_session->GetVariable(i));
512
513 m_base[i]->SetWaveSpace(false);
514 }
515 }
516 }
517 else
518 {
519 i = 0;
522 AllocateSharedPtr(m_session, graphShrPtr,
523 m_session->GetVariable(i));
524
525 m_base[0] = firstbase;
526
527 for (i = 1; i < m_base.size(); i++)
528 {
530 AllocateSharedPtr(*firstbase, graphShrPtr,
531 m_session->GetVariable(i));
532 }
533 }
534 }
535 break;
536 case 3:
537 {
540 m_session, graphShrPtr, m_session->GetVariable(0));
541 m_base[0] = firstbase;
542 for (i = 1; i < m_base.size(); i++)
543 {
545 AllocateSharedPtr(*firstbase, graphShrPtr,
546 m_session->GetVariable(0));
547 }
548 }
549 break;
550 default:
552 "Expansion dimension not recognised");
553 break;
554 }
555 }
556 else
557 {
558 switch (m_expdim)
559 {
560 case 1:
561 {
562 // need to use zero for variable as may be more base
563 // flows than variables
564 for (i = 0; i < m_base.size(); i++)
565 {
567 AllocateSharedPtr(m_session, graphShrPtr,
568 m_session->GetVariable(0));
569 }
570 break;
571 }
572 case 2:
573 {
574 for (i = 0; i < m_base.size(); i++)
575 {
577 AllocateSharedPtr(m_session, graphShrPtr,
578 m_session->GetVariable(0));
579 }
580 break;
581 }
582 case 3:
583 NEKERROR(ErrorUtil::efatal, "3D not set up");
584 break;
585 default:
587 "Expansion dimension not recognised");
588 break;
589 }
590 }
591}
592
593/**
594 * Import the base flow fld file.
595 */
596void FilterModalEnergy::ImportFldBase(std::string pInfile)
597{
598 std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef;
599 std::vector<std::vector<NekDouble>> FieldData;
600
601 // Get Homogeneous
602 m_fld->Import(pInfile, FieldDef, FieldData);
603
604 int nvar = m_session->GetVariables().size();
605 if (m_session->DefinesSolverInfo("HOMOGENEOUS"))
606 {
607 std::string HomoStr = m_session->GetSolverInfo("HOMOGENEOUS");
608 }
609 // Copy FieldData into m_fields
610 for (int j = 0; j < nvar; ++j)
611 {
612 for (int i = 0; i < FieldDef.size(); ++i)
613 {
614 bool flag = FieldDef[i]->m_fields[j] == m_session->GetVariable(j);
615
616 ASSERTL0(flag, (std::string("Order of ") + pInfile +
617 std::string(" data and that defined in "
618 "m_boundaryconditions differs"))
619 .c_str());
620
621 m_base[j]->ExtractDataToCoeffs(FieldDef[i], FieldData[i],
622 FieldDef[i]->m_fields[j],
623 m_base[j]->UpdateCoeffs());
624 }
625 }
626}
627
628/**
629 * Flag for time-dependent flows.
630 */
632{
633 return true;
634}
635} // namespace Nektar::SolverUtils
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:202
Describes the specification for a Basis.
Definition: Basis.h:45
static std::shared_ptr< FieldIO > CreateDefault(const LibUtilities::SessionReaderSharedPtr session)
Returns an object for the default FieldIO method.
Definition: FieldIO.cpp:195
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:197
Defines a specification for a set of points.
Definition: Points.h:50
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:83
std::map< std::string, std::string > ParamMap
Definition: Filter.h:65
void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
enum MultiRegions::ProjectionType m_projectionType
static FilterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation, const ParamMap &pParams)
SOLVER_UTILS_EXPORT ~FilterModalEnergy() override
LibUtilities::FieldIOSharedPtr m_fld
SOLVER_UTILS_EXPORT FilterModalEnergy(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation, const ParamMap &pParams)
NekDouble L2Error(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, unsigned int field, const NekDouble &time)
Array< OneD, MultiRegions::ExpListSharedPtr > m_base
void SetUpBaseFields(SpatialDomains::MeshGraphSharedPtr &mesh)
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
Definition: MeshGraph.cpp:115
std::shared_ptr< SessionReader > SessionReaderSharedPtr
@ eFourierEvenlySpaced
1D Evenly-spaced points using Fourier Fit
Definition: PointsType.h:74
@ eFourierSingleModeSpaced
1D Non Evenly-spaced points for Single Mode analysis
Definition: PointsType.h:75
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:55
@ eFourierHalfModeRe
Fourier Modified expansions with just the real part of the first mode .
Definition: BasisType.h:66
@ eFourier
Fourier Expansion .
Definition: BasisType.h:55
std::shared_ptr< ContField > ContFieldSharedPtr
Definition: ContField.h:268
FilterFactory & GetFilterFactory()
Definition: Filter.cpp:39
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
double NekDouble
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.hpp:180
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