Nektar++
BasicUtils/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 
45 Equation::Equation(InterpreterSharedPtr evaluator, const std::string &expr,
46  const std::string &vlist)
47  : m_vlist(vlist), m_expr(expr), m_expr_id(-1), m_evaluator(evaluator)
48 {
49  boost::algorithm::trim(m_expr);
50  boost::algorithm::trim(m_vlist);
51 
52  if (m_vlist.empty())
53  {
54  m_vlist = "x y z t";
55  }
56 
57  try
58  {
59  if (!m_expr.empty())
60  {
61  m_expr_id = m_evaluator->DefineFunction(m_vlist, m_expr);
62  }
63  }
64  catch (const std::runtime_error &e)
65  {
66  m_expr_id = -1;
67  std::string msg(
68  std::string("Equation::Equation() fails on expression [") + m_expr +
69  std::string("]\n"));
71  throw e;
72  return;
73  }
74  catch (const std::string &e)
75  {
76  m_expr_id = -1;
77  std::string msg(
78  std::string("Equation::Equation() fails on expression [") + m_expr +
79  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(
107  std::string("Equation::Evaluate fails on expression [") + m_expr +
108  std::string("]\n"));
109  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e.what());
110  }
111  catch (const std::string &e)
112  {
113  std::string msg(
114  std::string("Equation::Evaluate fails on expression [") + m_expr +
115  std::string("]\n"));
116  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e);
117  }
118  return 0;
119 }
120 
122  NekDouble t) const
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 =
134  "Equation::Evaluate fails on expression [" + m_expr + "]\n";
135  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e.what());
136  }
137  catch (const std::string &e)
138  {
139  std::string msg =
140  "Equation::Evaluate fails on expression [" + m_expr + "]\n";
141  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e);
142  }
143 
144  return 0;
145 }
146 
150  Array<OneD, NekDouble> &result) const
151 {
152  Array<OneD, NekDouble> zero(x.size(), 0.0);
153  Evaluate(x, y, z, zero, result);
154 }
155 
159  const NekDouble t, Array<OneD, NekDouble> &result) const
160 {
161  Array<OneD, NekDouble> time(x.size(), t);
162  Evaluate(x, y, z, time, result);
163 }
164 
169  Array<OneD, NekDouble> &result) const
170 {
171  std::vector<Array<OneD, const NekDouble>> points;
172  points.push_back(x);
173  points.push_back(y);
174  points.push_back(z);
175  points.push_back(t);
176  Evaluate(points, result);
177 }
178 
179 void Equation::Evaluate(const std::vector<Array<OneD, const NekDouble>> points,
180  Array<OneD, NekDouble> &result) const
181 {
182  try
183  {
184  if (m_expr_id != -1)
185  {
186  m_evaluator->Evaluate(m_expr_id, points, result);
187  }
188  }
189  catch (const std::runtime_error &e)
190  {
191  std::string msg =
192  "Equation::Evaluate fails on expression [" + m_expr + "]\n";
193  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e.what());
194  return;
195  }
196  catch (const std::string &e)
197  {
198  std::string msg =
199  "Equation::Evaluate fails on expression [" + m_expr + "]\n";
200  NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e);
201  return;
202  }
203 }
204 
205 void Equation::SetParameter(const std::string &name, NekDouble value)
206 {
207  m_evaluator->SetParameter(name, value);
208 }
209 
210 void Equation::SetConstants(const std::map<std::string, NekDouble> &constants)
211 {
212  m_evaluator->AddConstants(constants);
213 }
214 
215 std::string Equation::GetExpression(void) const
216 {
217  return m_expr;
218 }
219 
220 std::string Equation::GetVlist(void) const
221 {
222  return m_vlist;
223 }
224 
225 /// Returns time spend on expression evaluation at
226 /// points (it does not include parse/pre-processing time).
228 {
229  return m_evaluator->GetTime();
230 }
231 
232 } // namespace LibUtilities
233 } // namespace Nektar
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
InterpreterSharedPtr m_evaluator
Definition: Equation.h:126
NekDouble GetTime() const
Returns time spend on expression evaluation at points (it does not include parse/pre-processing time)...
Equation(const Equation &)=default
Equation & operator=(const Equation &src)
std::string GetExpression(void) const
void SetConstants(const std::map< std::string, NekDouble > &constants)
void SetParameter(const std::string &name, NekDouble value)
std::shared_ptr< Interpreter > InterpreterSharedPtr
Definition: Interpreter.h:324
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble