Nektar++
Loading...
Searching...
No Matches
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" << " " + varName + "_Linf";
74
76 {
77 m_outFile << " " + varName + "_H1";
78 }
79 }
80
81 m_outFile << std::endl;
82 }
83
84 // OutputFrequency
85 auto it = pParams.find("OutputFrequency");
86 if (it == pParams.end())
87 {
89 }
90 else
91 {
92 ASSERTL0(it->second.length() > 0, "Empty parameter 'OutputFrequency'.");
93 LibUtilities::Equation equ(m_session->GetInterpreter(), it->second);
94 m_outputFrequency = round(equ.Evaluate());
95 }
96
97 // ConsoleOutput
98 it = pParams.find("ConsoleOutput");
99 if (it == pParams.end())
100 {
101 m_consoleOutput = false;
102 }
103 else
104 {
105 ASSERTL0(it->second.length() > 0, "Empty parameter 'ConsoleOutput'.");
106 ASSERTL0(it->second == "0" || it->second == "1",
107 "Parameter 'ConsoleOutput' can only be '0' or '1'.");
108 m_consoleOutput = boost::lexical_cast<bool>(it->second);
109 }
110}
111
114 const NekDouble &time)
115{
116 v_Update(pFields, time);
117}
118
121 const NekDouble &time)
122{
123 // Check whether output frequency matches
124 if (m_index++ % m_outputFrequency > 0)
125 {
126 return;
127 }
128
129 // Output to file L2 and Linf error for each variable
130 if (m_comm->GetRank() == 0)
131 {
132 m_outFile << time;
133 }
134
135 // Lock equation system pointer
136 auto equationSys = m_equ.lock();
137 ASSERTL0(equationSys, "Weak pointer expired");
138
139 for (size_t i = 0; i < m_numVariables; ++i)
140 {
141 // Evaluate "ExactSolution" function, or zero array
142 Array<OneD, NekDouble> exactsoln(pFields[i]->GetTotPoints(), 0.0);
143 equationSys->EvaluateExactSolution(i, exactsoln, time);
144
145 NekDouble vL2Error = equationSys->L2Error(i, exactsoln);
146 NekDouble vLinfError = equationSys->LinfError(i, exactsoln);
147
148 if (m_comm->GetRank() == 0)
149 {
150 m_outFile << " " << vL2Error << " " << vLinfError;
151
152 if (m_consoleOutput)
153 {
154 std::cout << "L 2 error (variable "
155 << equationSys->GetVariable(i) << ") : " << vL2Error
156 << std::endl;
157 std::cout << "L inf error (variable "
158 << equationSys->GetVariable(i) << ") : " << vLinfError
159 << std::endl;
160 }
161 }
162
163 // Only evaluate H1 for non homogeneous case
165 {
166 NekDouble vH1Error = equationSys->H1Error(i, exactsoln);
167 if (m_comm->GetRank() == 0)
168 {
169 m_outFile << " " << vH1Error;
170
171 if (m_consoleOutput)
172 {
173 std::cout << "H 1 error (variable "
174 << equationSys->GetVariable(i)
175 << ") : " << vH1Error << std::endl;
176 }
177 }
178 }
179 }
180
181 if (m_comm->GetRank() == 0)
182 {
183 m_outFile << std::endl;
184 }
185}
186
189 &pFields,
190 [[maybe_unused]] const NekDouble &time)
191{
192 if (m_comm->GetRank() == 0)
193 {
194 m_outFile.close();
195 }
196}
197
199{
200 return true;
201}
202} // namespace Nektar::SolverUtils
#define ASSERTL0(condition, msg)
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)
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()