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 
35 #include <boost/core/ignore_unused.hpp>
36 
38 
39 namespace Nektar
40 {
41 namespace SolverUtils
42 {
43 std::string FilterError::className =
45 
47  const std::weak_ptr<EquationSystem> &pEquation,
48  const ParamMap &pParams)
49  : Filter(pSession, pEquation)
50 {
51  std::string outName;
52 
53  // OutputFile
54  auto it = pParams.find("OutputFile");
55  if (it == pParams.end())
56  {
57  outName = m_session->GetSessionName();
58  }
59  else
60  {
61  ASSERTL0(it->second.length() > 0, "Empty parameter 'OutputFile'.");
62  outName = it->second;
63  }
64  outName += ".err";
65 
66  // Lock equation system pointer
67  auto equationSys = m_equ.lock();
68  ASSERTL0(equationSys, "Weak pointer expired");
69 
70  m_numVariables = equationSys->GetNvariables();
71 
72  m_comm = pSession->GetComm();
73  if (m_comm->GetRank() == 0)
74  {
75  m_outFile.open(outName);
76  ASSERTL0(m_outFile.good(), "Unable to open: '" + outName + "'");
77  m_outFile.setf(std::ios::scientific, std::ios::floatfield);
78 
79  m_outFile << "Time";
80  for (size_t i = 0; i < m_numVariables; ++i)
81  {
82  std::string varName = equationSys->GetVariable(i);
83  m_outFile << " " + varName + "_L2"
84  << " " + varName + "_Linf";
85  }
86 
87  m_outFile << std::endl;
88  }
89 
90  // OutputFrequency
91  it = pParams.find("OutputFrequency");
92  if (it == pParams.end())
93  {
95  }
96  else
97  {
98  ASSERTL0(it->second.length() > 0, "Empty parameter 'OutputFrequency'.");
99  LibUtilities::Equation equ(m_session->GetInterpreter(), it->second);
100  m_outputFrequency = round(equ.Evaluate());
101  }
102 }
103 
106  const NekDouble &time)
107 {
108  v_Update(pFields, time);
109 }
110 
113  const NekDouble &time)
114 {
115  boost::ignore_unused(pFields);
116 
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  Array<OneD, NekDouble> exactsoln(equationSys->GetTotPoints(), 0.0);
135 
136  // Evaluate "ExactSolution" function, or zero array
137  equationSys->EvaluateExactSolution(i, exactsoln, time);
138 
139  NekDouble vL2Error = equationSys->L2Error(i, exactsoln);
140  NekDouble vLinfError = equationSys->LinfError(i, exactsoln);
141 
142  if (m_comm->GetRank() == 0)
143  {
144  m_outFile << " " << vL2Error << " " << vLinfError;
145  }
146  }
147 
148  if (m_comm->GetRank() == 0)
149  {
150  m_outFile << std::endl;
151  }
152 }
153 
156  const NekDouble &time)
157 {
158  boost::ignore_unused(pFields, time);
159 
160  if (m_comm->GetRank() == 0)
161  {
162  m_outFile.close();
163  }
164 }
165 
167 {
168  return true;
169 }
170 } // namespace SolverUtils
171 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
NekDouble Evaluate() const
Definition: Equation.cpp:95
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:200
virtual 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:46
LibUtilities::CommSharedPtr m_comm
Definition: FilterError.h:86
virtual bool v_IsTimeDependent() final
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:50
static std::string className
Name of the class.
Definition: FilterError.h:61
virtual void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) final
virtual void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) final
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:86
const std::weak_ptr< EquationSystem > m_equ
Definition: Filter.h:87
std::map< std::string, std::string > ParamMap
Definition: Filter.h:68
std::shared_ptr< SessionReader > SessionReaderSharedPtr
FilterFactory & GetFilterFactory()
Definition: Filter.cpp:41
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
double NekDouble