Nektar++
Loading...
Searching...
No Matches
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
41{
42
43/*
44 * This is a Null construtor for Equation with a trivial expression.
45 */
47{
48 // Create Null expression and vlist
49 m_expr = "0.0";
50 m_vlist = "x y z t";
51 boost::algorithm::trim(m_expr);
52 boost::algorithm::trim(m_vlist);
53
54 // Evaluate expression
56 m_expr_id = m_evaluator->DefineFunction(m_vlist, m_expr);
57}
58
59Equation::Equation(InterpreterSharedPtr evaluator, const std::string &expr,
60 const std::string &vlist)
61 : m_vlist(vlist), m_expr(expr), m_expr_id(-1), m_evaluator(evaluator)
62{
63 boost::algorithm::trim(m_expr);
64 boost::algorithm::trim(m_vlist);
65
66 if (m_vlist.empty())
67 {
68 m_vlist = "x y z t";
69 }
70
71 try
72 {
73 if (!m_expr.empty())
74 {
75 m_expr_id = m_evaluator->DefineFunction(m_vlist, m_expr);
76 }
77 }
78 catch (const std::runtime_error &e)
79 {
80 m_expr_id = -1;
81 std::string msg(
82 std::string("Equation::Equation() fails on expression [") + m_expr +
83 std::string("]\n"));
85 throw e;
86 return;
87 }
88 catch (const std::string &e)
89 {
90 m_expr_id = -1;
91 std::string msg(
92 std::string("Equation::Equation() fails on expression [") + m_expr +
93 std::string("]\n"));
95 throw e;
96 return;
97 }
98}
99
101{
102 m_vlist = src.m_vlist;
103 m_expr = src.m_expr;
104 m_expr_id = src.m_expr_id;
106 return *this;
107}
108
110{
111 try
112 {
113 if (m_expr_id != -1)
114 {
115 return m_evaluator->Evaluate(m_expr_id);
116 }
117 }
118 catch (const std::runtime_error &e)
119 {
120 std::string msg(
121 std::string("Equation::Evaluate fails on expression [") + m_expr +
122 std::string("]\n"));
123 NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e.what());
124 }
125 catch (const std::string &e)
126 {
127 std::string msg(
128 std::string("Equation::Evaluate fails on expression [") + m_expr +
129 std::string("]\n"));
130 NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e);
131 }
132 return 0;
133}
134
136 NekDouble t) const
137{
138 try
139 {
140 if (m_expr_id != -1)
141 {
142 return m_evaluator->Evaluate(m_expr_id, x, y, z, t);
143 }
144 }
145 catch (const std::runtime_error &e)
146 {
147 std::string msg =
148 "Equation::Evaluate fails on expression [" + m_expr + "]\n";
149 NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e.what());
150 }
151 catch (const std::string &e)
152 {
153 std::string msg =
154 "Equation::Evaluate fails on expression [" + m_expr + "]\n";
155 NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e);
156 }
157
158 return 0;
159}
160
161NekDouble Equation::Evaluate(std::vector<NekDouble> point) const
162{
163 try
164 {
165 if (m_expr_id != -1)
166 {
167 return m_evaluator->EvaluateAtPoint(m_expr_id, point);
168 }
169 }
170 catch (const std::runtime_error &e)
171 {
172 std::string msg =
173 "Equation::Evaluate fails on expression [" + m_expr + "]\n";
174 NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e.what());
175 }
176 catch (const std::string &e)
177 {
178 std::string msg =
179 "Equation::Evaluate fails on expression [" + m_expr + "]\n";
180 NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e);
181 }
182
183 return 0;
184}
185
189 Array<OneD, NekDouble> &result) const
190{
191 Array<OneD, NekDouble> zero(x.size(), 0.0);
192 Evaluate(x, y, z, zero, result);
193}
194
198 const NekDouble t, Array<OneD, NekDouble> &result) const
199{
200 Array<OneD, NekDouble> time(x.size(), t);
201 Evaluate(x, y, z, time, result);
202}
203
208 Array<OneD, NekDouble> &result) const
209{
210 std::vector<Array<OneD, const NekDouble>> points;
211 points.push_back(x);
212 points.push_back(y);
213 points.push_back(z);
214 points.push_back(t);
215 Evaluate(points, result);
216}
217
219 Array<OneD, NekDouble> &result) const
220{
221 try
222 {
223 if (m_expr_id != -1)
224 {
225 m_evaluator->Evaluate(m_expr_id, points, result);
226 }
227 }
228 catch (const std::runtime_error &e)
229 {
230 std::string msg =
231 "Equation::Evaluate fails on expression [" + m_expr + "]\n";
232 NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e.what());
233 return;
234 }
235 catch (const std::string &e)
236 {
237 std::string msg =
238 "Equation::Evaluate fails on expression [" + m_expr + "]\n";
239 NEKERROR(ErrorUtil::efatal, msg + std::string("ERROR: ") + e);
240 return;
241 }
242}
243
244void Equation::SetParameter(const std::string &name, NekDouble value)
245{
246 m_evaluator->SetParameter(name, value);
247}
248
249void Equation::SetConstants(const std::map<std::string, NekDouble> &constants)
250{
251 m_evaluator->AddConstants(constants);
252}
253
254std::string Equation::GetExpression(void) const
255{
256 return m_expr;
257}
258
259std::string Equation::GetVlist(void) const
260{
261 return m_vlist;
262}
263
264/// Returns time spend on expression evaluation at
265/// points (it does not include parse/pre-processing time).
267{
268 return m_evaluator->GetTime();
269}
270
271} // namespace Nektar::LibUtilities
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
InterpreterSharedPtr m_evaluator
Definition Equation.h:128
NekDouble GetTime() const
Returns time spend on expression evaluation at points (it does not include parse/pre-processing time)...
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)
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
std::shared_ptr< Interpreter > InterpreterSharedPtr