Nektar++
Equation.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: Equation.h
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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Wrapper to AnalyticExpressionEvaluator class.
33 //
34 //
35 ////////////////////////////////////////////////////////////////////////////////
36 #ifndef NEKTAR_LIBUTILITIES_EQUATION_HPP
37 #define NEKTAR_LIBUTILITIES_EQUATION_HPP
38 
39 #include <string>
40 #include <map>
41 #include <stdexcept>
48 #include <boost/algorithm/string/trim.hpp>
49 
50 namespace Nektar
51 {
52  namespace LibUtilities
53  {
54  class Equation
55  {
56 
57  public:
59  m_expr (src.m_expr),
60  m_expr_id (src.m_expr_id),
62  {
63  }
64 
65  LIB_UTILITIES_EXPORT Equation(const SessionReaderSharedPtr& session, const std::string& expr = ""):
66  m_expr (expr),
67  m_expr_id (-1),
68  m_evaluator (session->GetExpressionEvaluator())
69  {
70  boost::algorithm::trim(m_expr);
71 
72  try
73  {
74  if (!m_expr.empty())
75  {
77  }
78  }
79  catch (const std::runtime_error& e)
80  {
81  m_expr_id = -1;
82  std::string msg(std::string("Equation::Equation() fails on expression [") + m_expr + std::string("]\n"));
83  ASSERTL1(false, msg);
84  throw e;
85  return;
86  }
87  catch (const std::string& e)
88  {
89  m_expr_id = -1;
90  std::string msg(std::string("Equation::Equation() fails on expression [") + m_expr + std::string("]\n"));
91  ASSERTL1(false, msg);
92  throw e;
93  return;
94  }
95  }
96 
98  {
99  return *this;
100  }
101 
103  {
104  try
105  {
106  if (m_expr_id != -1)
107  {
109  }
110  }
111  catch (const std::runtime_error& e)
112  {
113  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
114  ASSERTL0(false, msg + std::string("ERROR: ") + e.what());
115  }
116  catch (const std::string& e)
117  {
118  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
119  ASSERTL0(false, msg + std::string("ERROR: ") + e);
120  }
121  return 0;
122  }
123 
125  {
126  try
127  {
128  if (m_expr_id != -1)
129  {
130  return m_evaluator.Evaluate(m_expr_id, x,y,z,t);
131  }
132  }
133  catch (const std::runtime_error& e)
134  {
135  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
136  ASSERTL0(false, msg + std::string("ERROR: ") + e.what());
137  }
138  catch (const std::string& e)
139  {
140  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
141  ASSERTL0(false, msg + std::string("ERROR: ") + e);
142  }
143  return 0;
144  }
145 
150  Array<OneD, NekDouble>& result)
151  {
152  Array<OneD, NekDouble> zero(x.num_elements(), 0.0);
153  Evaluate(x,y,z,zero, result);
154  }
155 
160  const NekDouble t,
161  Array<OneD, NekDouble>& result) const
162  {
163  Array<OneD, NekDouble> time(x.num_elements(), t);
164  Evaluate(x,y,z,time, result);
165  }
166 
167 
173  Array<OneD, NekDouble>& result) const
174  {
175  try
176  {
177  if (m_expr_id != -1)
178  {
179  m_evaluator.Evaluate(m_expr_id, x,y,z,t, result);
180  }
181  }
182  catch (const std::runtime_error& e)
183  {
184  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
185  ASSERTL0(false, msg + std::string("ERROR: ") + e.what());
186  return;
187  }
188  catch (const std::string& e)
189  {
190  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
191  ASSERTL0(false, msg + std::string("ERROR: ") + e);
192  return;
193  }
194  }
195 
196  LIB_UTILITIES_EXPORT void SetParameter(const std::string& name, NekDouble value)
197  {
198  m_evaluator.SetParameter(name, value);
199  }
200 
201  LIB_UTILITIES_EXPORT void SetConstants(const std::map<std::string, NekDouble> &constants)
202  {
203  m_evaluator.AddConstants(constants);
204  }
205 
206  LIB_UTILITIES_EXPORT std::string GetExpression(void) const
207  {
208  return m_expr;
209  }
210 
211  /// Returns time spend on expression evaluation at
212  /// points (it does not include parse/pre-processing time).
214  {
215  return m_evaluator.GetTime();
216  }
217 
218  private:
219  std::string m_expr;
222  };
223  }
224 }
225 
226 #endif //NEKTAR_LIBUTILITIES_EQUATION_HPP
NekDouble GetTime() const
Returns the total time spent in evaluation procedures, seconds.
void AddConstants(std::map< std::string, NekDouble > const &constants)
Constants are evaluated and inserted into the function at the time it is parsed when calling the Defi...
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
void SetParameter(std::string const &name, NekDouble value)
This function behaves in the same way as SetParameters, but it only adds one parameter and it does no...
std::string GetExpression(void) const
Definition: Equation.h:206
void SetParameter(const std::string &name, NekDouble value)
Definition: Equation.h:196
NekDouble Evaluate(const int AnalyticExpression_id)
Evaluation method for expressions depending on parameters only.
Equation & operator=(const Equation &src)
Definition: Equation.h:97
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:50
NekDouble GetTime() const
Returns time spend on expression evaluation at points (it does not include parse/pre-processing time)...
Definition: Equation.h:213
#define LIB_UTILITIES_EXPORT
NekDouble Evaluate(NekDouble x, NekDouble y=0, NekDouble z=0, NekDouble t=0) const
Definition: Equation.h:124
void Evaluate(const Array< OneD, const NekDouble > &x, const Array< OneD, const NekDouble > &y, const Array< OneD, const NekDouble > &z, Array< OneD, NekDouble > &result)
Definition: Equation.h:146
NekDouble Evaluate() const
Definition: Equation.h:102
double NekDouble
This class defines evaluator of analytic (symbolic) mathematical expressions. Expressions are allowed...
Equation(const SessionReaderSharedPtr &session, const std::string &expr="")
Definition: Equation.h:65
int DefineFunction(const std::string &vlist, const std::string &function)
This function allows one to define a function to evaluate. The first argument (vlist) is a list of va...
Equation(const Equation &src)
Definition: Equation.h:58
AnalyticExpressionEvaluator & m_evaluator
Definition: Equation.h:221
void Evaluate(const Array< OneD, const NekDouble > &x, const Array< OneD, const NekDouble > &y, const Array< OneD, const NekDouble > &z, const NekDouble t, Array< OneD, NekDouble > &result) const
Definition: Equation.h:156
void Evaluate(const Array< OneD, const NekDouble > &x, const Array< OneD, const NekDouble > &y, const Array< OneD, const NekDouble > &z, const Array< OneD, const NekDouble > &t, Array< OneD, NekDouble > &result) const
Definition: Equation.h:168
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
Definition: ErrorUtil.hpp:191
void SetConstants(const std::map< std::string, NekDouble > &constants)
Definition: Equation.h:201