Nektar++
FilterError.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: FilterError.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: Outputs errors during time-stepping.
32//
33///////////////////////////////////////////////////////////////////////////////
34
36
37namespace Nektar::SolverUtils
38{
39std::string FilterError::className =
41
43 const std::weak_ptr<EquationSystem> &pEquation,
44 const ParamMap &pParams)
45 : Filter(pSession, pEquation)
46{
47 std::string outName;
48
49 // OutputFile
50 auto it = pParams.find("OutputFile");
51 if (it == pParams.end())
52 {
53 outName = m_session->GetSessionName();
54 }
55 else
56 {
57 ASSERTL0(it->second.length() > 0, "Empty parameter 'OutputFile'.");
58 outName = it->second;
59 }
60 outName += ".err";
61
62 // Lock equation system pointer
63 auto equationSys = m_equ.lock();
64 ASSERTL0(equationSys, "Weak pointer expired");
65
66 m_numVariables = equationSys->GetNvariables();
67
68 m_comm = pSession->GetComm();
69 if (m_comm->GetRank() == 0)
70 {
71 m_outFile.open(outName);
72 ASSERTL0(m_outFile.good(), "Unable to open: '" + outName + "'");
73 m_outFile.setf(std::ios::scientific, std::ios::floatfield);
74
75 m_outFile << "Time";
76 for (size_t i = 0; i < m_numVariables; ++i)
77 {
78 std::string varName = equationSys->GetVariable(i);
79 m_outFile << " " + varName + "_L2"
80 << " " + varName + "_Linf";
81 }
82
83 m_outFile << std::endl;
84 }
85
86 // OutputFrequency
87 it = pParams.find("OutputFrequency");
88 if (it == pParams.end())
89 {
91 }
92 else
93 {
94 ASSERTL0(it->second.length() > 0, "Empty parameter 'OutputFrequency'.");
95 LibUtilities::Equation equ(m_session->GetInterpreter(), it->second);
96 m_outputFrequency = round(equ.Evaluate());
97 }
98}
99
102 const NekDouble &time)
103{
104
105 // Check for homogeneous expansion
106 m_homogeneous = pFields[0]->GetExpType() == MultiRegions::e3DH1D ||
107 pFields[0]->GetExpType() == MultiRegions::e3DH2D;
108
109 v_Update(pFields, time);
110}
111
114 const NekDouble &time)
115{
116 // Check whether output frequency matches
117 if (m_index++ % m_outputFrequency > 0)
118 {
119 return;
120 }
121
122 // Output to file L2 and Linf error for each variable
123 if (m_comm->GetRank() == 0)
124 {
125 m_outFile << time;
126 }
127
128 // Lock equation system pointer
129 auto equationSys = m_equ.lock();
130 ASSERTL0(equationSys, "Weak pointer expired");
131
132 for (size_t i = 0; i < m_numVariables; ++i)
133 {
134 // Evaluate "ExactSolution" function, or zero array
135 Array<OneD, NekDouble> exactsoln(pFields[i]->GetTotPoints(), 0.0);
136 equationSys->EvaluateExactSolution(i, exactsoln, time);
137
138 // If homogeneous expansion is used, transform the solution to
139 // Fourier (Wave) space
140 if (m_homogeneous)
141 {
142 pFields[i]->HomogeneousFwdTrans(pFields[i]->GetTotPoints(),
143 exactsoln, exactsoln);
144 }
145
146 NekDouble vL2Error = equationSys->L2Error(i, exactsoln);
147 NekDouble vLinfError = equationSys->LinfError(i, exactsoln);
148
149 if (m_comm->GetRank() == 0)
150 {
151 m_outFile << " " << vL2Error << " " << vLinfError;
152 }
153 }
154
155 if (m_comm->GetRank() == 0)
156 {
157 m_outFile << std::endl;
158 }
159}
160
163 &pFields,
164 [[maybe_unused]] const NekDouble &time)
165{
166 if (m_comm->GetRank() == 0)
167 {
168 m_outFile.close();
169 }
170}
171
173{
174 return true;
175}
176} // 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.
Definition: NekFactory.hpp:197
void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) final
SOLVER_UTILS_EXPORT FilterError(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation, const ParamMap &pParams)
Definition: FilterError.cpp:42
LibUtilities::CommSharedPtr m_comm
Definition: FilterError.h:85
static FilterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation, const std::map< std::string, std::string > &pParams)
Creates an instance of this class.
Definition: FilterError.h:48
static std::string className
Name of the class.
Definition: FilterError.h:59
void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) final
void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) final
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
FilterFactory & GetFilterFactory()
Definition: Filter.cpp:39
double NekDouble