Nektar++
Equation.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: Equation.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: Wrapper to Interpreter class
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
37 
38 #include <boost/algorithm/string/trim.hpp>
39 
40 namespace Nektar
41 {
42 namespace LibUtilities
43 {
44 
46  const std::string& expr,
47  const std::string& vlist):
48  m_vlist (vlist),
49  m_expr (expr),
50  m_expr_id (-1),
51  m_evaluator (evaluator)
52 {
53  boost::algorithm::trim(m_expr);
54  boost::algorithm::trim(m_vlist);
55 
56  if (m_vlist.empty())
57  {
58  m_vlist = "x y z t";
59  }
60 
61  try
62  {
63  if (!m_expr.empty())
64  {
65  m_expr_id = m_evaluator->DefineFunction(m_vlist, m_expr);
66  }
67  }
68  catch (const std::runtime_error& e)
69  {
70  m_expr_id = -1;
71  std::string msg(std::string("Equation::Equation() fails on expression [") + m_expr + std::string("]\n"));
73  throw e;
74  return;
75  }
76  catch (const std::string& e)
77  {
78  m_expr_id = -1;
79  std::string msg(std::string("Equation::Equation() fails on expression [") + m_expr + std::string("]\n"));
81  throw e;
82  return;
83  }
84 }
85 
87 {
88  m_vlist = src.m_vlist;
89  m_expr = src.m_expr;
90  m_expr_id = src.m_expr_id;
92  return *this;
93 }
94 
96 {
97  try
98  {
99  if (m_expr_id != -1)
100  {
101  return m_evaluator->Evaluate(m_expr_id);
102  }
103  }
104  catch (const std::runtime_error& e)
105  {
106  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
107  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e.what());
108  }
109  catch (const std::string& e)
110  {
111  std::string msg(std::string("Equation::Evaluate fails on expression [") + m_expr + std::string("]\n"));
112  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e);
113  }
114  return 0;
115 }
116 
118  NekDouble x, NekDouble y, NekDouble z, NekDouble t) const
119 {
120  try
121  {
122  if (m_expr_id != -1)
123  {
124  return m_evaluator->Evaluate(m_expr_id, x,y,z,t);
125  }
126  }
127  catch (const std::runtime_error& e)
128  {
129  std::string msg =
130  "Equation::Evaluate fails on expression [" + m_expr + "]\n";
131  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e.what());
132  }
133  catch (const std::string& e)
134  {
135  std::string msg =
136  "Equation::Evaluate fails on expression [" + m_expr + "]\n";
137  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e);
138  }
139 
140  return 0;
141 }
142 
147  Array<OneD, NekDouble>& result) const
148 {
149  Array<OneD, NekDouble> zero(x.num_elements(), 0.0);
150  Evaluate(x,y,z,zero, result);
151 }
152 
157  const NekDouble t,
158  Array<OneD, NekDouble>& result) const
159 {
160  Array<OneD, NekDouble> time(x.num_elements(), t);
161  Evaluate(x,y,z,time, result);
162 }
163 
164 
170  Array<OneD, NekDouble>& result) const
171 {
172  std::vector<Array<OneD, const NekDouble> > points;
173  points.push_back(x);
174  points.push_back(y);
175  points.push_back(z);
176  points.push_back(t);
177  Evaluate(points, result);
178 }
179 
181  const std::vector<Array<OneD, const NekDouble> > points,
182  Array<OneD, NekDouble>& result) const
183 {
184  try
185  {
186  if (m_expr_id != -1)
187  {
188  m_evaluator->Evaluate(m_expr_id, points, result);
189  }
190  }
191  catch (const std::runtime_error& e)
192  {
193  std::string msg =
194  "Equation::Evaluate fails on expression [" + m_expr + "]\n";
195  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e.what());
196  return;
197  }
198  catch (const std::string& e)
199  {
200  std::string msg =
201  "Equation::Evaluate fails on expression [" + m_expr + "]\n";
202  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e);
203  return;
204  }
205 }
206 
207 void Equation::SetParameter(const std::string& name, NekDouble value)
208 {
209  m_evaluator->SetParameter(name, value);
210 }
211 
212 void Equation::SetConstants(const std::map<std::string, NekDouble> &constants)
213 {
214  m_evaluator->AddConstants(constants);
215 }
216 
217 std::string Equation::GetExpression(void) const
218 {
219  return m_expr;
220 }
221 
222 std::string Equation::GetVlist(void) const
223 {
224  return m_vlist;
225 }
226 
227 /// Returns time spend on expression evaluation at
228 /// points (it does not include parse/pre-processing time).
230 {
231  return m_evaluator->GetTime();
232 }
233 
234 }
235 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209
std::string GetExpression(void) const
Definition: Equation.cpp:217
InterpreterSharedPtr m_evaluator
Definition: Equation.h:128
void SetParameter(const std::string &name, NekDouble value)
Definition: Equation.cpp:207
NekDouble GetTime() const
Returns time spend on expression evaluation at points (it does not include parse/pre-processing time)...
Definition: Equation.cpp:229
Equation(const Equation &)=default
Equation & operator=(const Equation &src)
Definition: Equation.cpp:86
std::shared_ptr< Interpreter > InterpreterSharedPtr
Definition: Interpreter.h:325
NekDouble Evaluate() const
Definition: Equation.cpp:95
double NekDouble
std::string GetVlist(void) const
Definition: Equation.cpp:222
void SetConstants(const std::map< std::string, NekDouble > &constants)
Definition: Equation.cpp:212