Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  ASSERTL0(false, msg + std::string("ERROR: ") + e.what());
84  return;
85  }
86  catch (const std::string& e)
87  {
88  m_expr_id = -1;
89  std::string msg(std::string("Equation::Equation() fails on expression [") + m_expr + std::string("]\n"));
90  ASSERTL0(false, msg + std::string("ERROR: ") + e);
91  return;
92  }
93  }
94 
96  {
97  return *this;
98  }
99 
101  {
102  try
103  {
104  if (m_expr_id != -1)
105  {
107  }
108  }
109  catch (const std::runtime_error& e)
110  {
111  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
112  ASSERTL0(false, msg + std::string("ERROR: ") + e.what());
113  }
114  catch (const std::string& e)
115  {
116  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
117  ASSERTL0(false, msg + std::string("ERROR: ") + e);
118  }
119  return 0;
120  }
121 
123  {
124  try
125  {
126  if (m_expr_id != -1)
127  {
128  return m_evaluator.Evaluate(m_expr_id, x,y,z,t);
129  }
130  }
131  catch (const std::runtime_error& e)
132  {
133  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
134  ASSERTL0(false, msg + std::string("ERROR: ") + e.what());
135  }
136  catch (const std::string& e)
137  {
138  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
139  ASSERTL0(false, msg + std::string("ERROR: ") + e);
140  }
141  return 0;
142  }
143 
145  const Array<OneD, const NekDouble>& x,
146  const Array<OneD, const NekDouble>& y,
147  const Array<OneD, const NekDouble>& z,
148  Array<OneD, NekDouble>& result)
149  {
150  Array<OneD, NekDouble> zero(x.num_elements(), 0.0);
151  Evaluate(x,y,z,zero, result);
152  }
153 
155  const Array<OneD, const NekDouble>& x,
156  const Array<OneD, const NekDouble>& y,
157  const Array<OneD, const NekDouble>& z,
158  const NekDouble t,
159  Array<OneD, NekDouble>& result) const
160  {
161  Array<OneD, NekDouble> time(x.num_elements(), t);
162  Evaluate(x,y,z,time, result);
163  }
164 
165 
167  const Array<OneD, const NekDouble>& x,
168  const Array<OneD, const NekDouble>& y,
169  const Array<OneD, const NekDouble>& z,
170  const Array<OneD, const NekDouble>& t,
171  Array<OneD, NekDouble>& result) const
172  {
173  try
174  {
175  if (m_expr_id != -1)
176  {
177  m_evaluator.Evaluate(m_expr_id, x,y,z,t, result);
178  }
179  }
180  catch (const std::runtime_error& e)
181  {
182  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
183  ASSERTL0(false, msg + std::string("ERROR: ") + e.what());
184  return;
185  }
186  catch (const std::string& e)
187  {
188  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
189  ASSERTL0(false, msg + std::string("ERROR: ") + e);
190  return;
191  }
192  }
193 
194  LIB_UTILITIES_EXPORT void SetParameter(const std::string& name, NekDouble value)
195  {
196  m_evaluator.SetParameter(name, value);
197  }
198 
199  LIB_UTILITIES_EXPORT void SetConstants(const std::map<std::string, NekDouble> &constants)
200  {
201  m_evaluator.AddConstants(constants);
202  }
203 
204  LIB_UTILITIES_EXPORT std::string GetExpression(void) const
205  {
206  return m_expr;
207  }
208 
209  /// Returns time spend on expression evaluation at
210  /// points (it does not include parse/pre-processing time).
212  {
213  return m_evaluator.GetTime();
214  }
215 
216  private:
217  std::string m_expr;
220  };
221  }
222 }
223 
224 #endif //NEKTAR_LIBUTILITIES_EQUATION_HPP