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