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 std::string ext = ".err";
49 outName = Filter::SetupOutput(ext, pParams);
50
51 // Lock equation system pointer
52 auto equationSys = m_equ.lock();
53 ASSERTL0(equationSys, "Weak pointer expired");
54
55 m_numVariables = equationSys->GetNvariables();
56
57 // Check if we have homogeneous expansion
58 // If yes, disable H1 error norm as it is not implemented for homogeneous
59 // expansions
60 m_session->MatchSolverInfo("Homogeneous", "1D", m_isHomogeneous1D, false);
61
62 m_comm = pSession->GetComm();
63 if (m_comm->GetRank() == 0)
64 {
65 m_outFile.open(outName);
66 ASSERTL0(m_outFile.good(), "Unable to open: '" + outName + "'");
67 m_outFile.setf(std::ios::scientific, std::ios::floatfield);
68
69 m_outFile << "Time";
70 for (size_t i = 0; i < m_numVariables; ++i)
71 {
72 std::string varName = equationSys->GetVariable(i);
73 m_outFile << " " + varName + "_L2"
74 << " " + varName + "_Linf";
75
77 {
78 m_outFile << " " + varName + "_H1";
79 }
80 }
81
82 m_outFile << std::endl;
83 }
84
85 // OutputFrequency
86 auto it = pParams.find("OutputFrequency");
87 if (it == pParams.end())
88 {
90 }
91 else
92 {
93 ASSERTL0(it->second.length() > 0, "Empty parameter 'OutputFrequency'.");
94 LibUtilities::Equation equ(m_session->GetInterpreter(), it->second);
95 m_outputFrequency = round(equ.Evaluate());
96 }
97
98 // ConsoleOutput
99 it = pParams.find("ConsoleOutput");
100 if (it == pParams.end())
101 {
102 m_consoleOutput = false;
103 }
104 else
105 {
106 ASSERTL0(it->second.length() > 0, "Empty parameter 'ConsoleOutput'.");
107 ASSERTL0(it->second == "0" || it->second == "1",
108 "Parameter 'ConsoleOutput' can only be '0' or '1'.");
109 m_consoleOutput = boost::lexical_cast<bool>(it->second);
110 }
111}
112
115 const NekDouble &time)
116{
117 v_Update(pFields, time);
118}
119
122 const NekDouble &time)
123{
124 // Check whether output frequency matches
125 if (m_index++ % m_outputFrequency > 0)
126 {
127 return;
128 }
129
130 // Output to file L2 and Linf error for each variable
131 if (m_comm->GetRank() == 0)
132 {
133 m_outFile << time;
134 }
135
136 // Lock equation system pointer
137 auto equationSys = m_equ.lock();
138 ASSERTL0(equationSys, "Weak pointer expired");
139
140 for (size_t i = 0; i < m_numVariables; ++i)
141 {
142 // Evaluate "ExactSolution" function, or zero array
143 Array<OneD, NekDouble> exactsoln(pFields[i]->GetTotPoints(), 0.0);
144 equationSys->EvaluateExactSolution(i, exactsoln, time);
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 if (m_consoleOutput)
154 {
155 std::cout << "L 2 error (variable "
156 << equationSys->GetVariable(i) << ") : " << vL2Error
157 << std::endl;
158 std::cout << "L inf error (variable "
159 << equationSys->GetVariable(i) << ") : " << vLinfError
160 << std::endl;
161 }
162 }
163
164 // Only evaluate H1 for non homogeneous case
166 {
167 NekDouble vH1Error = equationSys->H1Error(i, exactsoln);
168 if (m_comm->GetRank() == 0)
169 {
170 m_outFile << " " << vH1Error;
171
172 if (m_consoleOutput)
173 {
174 std::cout << "H 1 error (variable "
175 << equationSys->GetVariable(i)
176 << ") : " << vH1Error << std::endl;
177 }
178 }
179 }
180 }
181
182 if (m_comm->GetRank() == 0)
183 {
184 m_outFile << std::endl;
185 }
186}
187
190 &pFields,
191 [[maybe_unused]] const NekDouble &time)
192{
193 if (m_comm->GetRank() == 0)
194 {
195 m_outFile.close();
196 }
197}
198
200{
201 return true;
202}
203} // 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
SOLVER_UTILS_EXPORT std::string SetupOutput(const std::string ext, const ParamMap &pParams)
Definition: Filter.h:139
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:93
const std::weak_ptr< EquationSystem > m_equ
Definition: Filter.h:94
std::map< std::string, std::string > ParamMap
Definition: Filter.h:66
std::shared_ptr< SessionReader > SessionReaderSharedPtr
FilterFactory & GetFilterFactory()
double NekDouble