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::shared_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 // ConsoleOutput
100 it = pParams.find("ConsoleOutput");
101 if (it == pParams.end())
102 {
103 m_consoleOutput = false;
104 }
105 else
106 {
107 ASSERTL0(it->second.length() > 0, "Empty parameter 'ConsoleOutput'.");
108 ASSERTL0(it->second == "0" || it->second == "1",
109 "Parameter 'ConsoleOutput' can only be '0' or '1'.");
110 m_consoleOutput = boost::lexical_cast<bool>(it->second);
111 }
112}
113
116 const NekDouble &time)
117{
118
119 // Check for homogeneous expansion
120 m_homogeneous = pFields[0]->GetExpType() == MultiRegions::e3DH1D ||
121 pFields[0]->GetExpType() == MultiRegions::e3DH2D;
122
123 v_Update(pFields, time);
124}
125
128 const NekDouble &time)
129{
130 // Check whether output frequency matches
131 if (m_index++ % m_outputFrequency > 0)
132 {
133 return;
134 }
135
136 // Output to file L2 and Linf error for each variable
137 if (m_comm->GetRank() == 0)
138 {
139 m_outFile << time;
140 }
141
142 // Lock equation system pointer
143 auto equationSys = m_equ.lock();
144 ASSERTL0(equationSys, "Weak pointer expired");
145
146 for (size_t i = 0; i < m_numVariables; ++i)
147 {
148 // Evaluate "ExactSolution" function, or zero array
149 Array<OneD, NekDouble> exactsoln(pFields[i]->GetTotPoints(), 0.0);
150 equationSys->EvaluateExactSolution(i, exactsoln, time);
151
152 NekDouble vL2Error = equationSys->L2Error(i, exactsoln);
153 NekDouble vLinfError = equationSys->LinfError(i, exactsoln);
154
155 if (m_comm->GetRank() == 0)
156 {
157 m_outFile << " " << vL2Error << " " << vLinfError;
158
159 if (m_consoleOutput)
160 {
161 std::cout << "L 2 error (variable "
162 << equationSys->GetVariable(i) << ") : " << vL2Error
163 << std::endl;
164 std::cout << "L inf error (variable "
165 << equationSys->GetVariable(i) << ") : " << vLinfError
166 << std::endl;
167 }
168 }
169 }
170
171 if (m_comm->GetRank() == 0)
172 {
173 m_outFile << std::endl;
174 }
175}
176
179 &pFields,
180 [[maybe_unused]] const NekDouble &time)
181{
182 if (m_comm->GetRank() == 0)
183 {
184 m_outFile.close();
185 }
186}
187
189{
190 return true;
191}
192} // 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.
void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) final
SOLVER_UTILS_EXPORT FilterError(const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< EquationSystem > &pEquation, const ParamMap &pParams)
Definition: FilterError.cpp:42
LibUtilities::CommSharedPtr m_comm
Definition: FilterError.h:86
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
static FilterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< EquationSystem > &pEquation, const std::map< std::string, std::string > &pParams)
Creates an instance of this class.
Definition: FilterError.h:48
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()
double NekDouble