Nektar++
Python/Interpreter/Interpreter.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: Interpreter.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: Python wrapper for Interpreter.
32//
33///////////////////////////////////////////////////////////////////////////////
34
36
39
41#include <boost/python/raw_function.hpp>
42
43using namespace Nektar;
44using namespace Nektar::LibUtilities;
45
46/**
47 * @brief Wrapper for Interpreter::AddConstants.
48 *
49 * boost.python does not know how (by default) to convert from any Python
50 * datatype to a C++ map, so we add a pythonic way to set parameters through
51 * keyword arguments.
52 *
53 * @param args Python function positional arguments.
54 * @param kwargs Python function keyword arguments.
55 *
56 * @return None (null py::object).
57 */
58py::object Interpreter_AddConstants(py::tuple args, py::dict kwargs)
59{
60 // To extract the 'self' object
61 Interpreter &interpreter = py::extract<Interpreter &>(args[0]);
62
63 // Map that will be passed to C++
64 std::map<std::string, NekDouble> constants;
65
66 // Loop over the keys inside the kwargs dictionary
67 py::list keys = kwargs.keys();
68 for (int i = 0; i < py::len(keys); ++i)
69 {
70 py::object arg = kwargs[keys[i]];
71 if (arg)
72 {
73 constants[py::extract<std::string>(keys[i])] =
74 py::extract<NekDouble>(arg);
75 }
76 }
77
78 interpreter.AddConstants(constants);
79 return py::object();
80}
81
82/**
83 * @brief Wrapper for Interpreter::SetParameters.
84 *
85 * boost.python does not know how (by default) to convert from any Python
86 * datatype to a C++ map, so we add a pythonic way to set parameters through
87 * keyword arguments.
88 *
89 * @param args Python function positional arguments.
90 * @param kwargs Python function keyword arguments.
91 *
92 * @return None (null py::object).
93 */
94py::object Interpreter_SetParameters(py::tuple args, py::dict kwargs)
95{
96
97 // To extract the 'self' object
98 Interpreter &interpreter = py::extract<Interpreter &>(args[0]);
99
100 // Map that will be passed to C++
101 std::map<std::string, NekDouble> parameters;
102
103 // Loop over the keys inside the kwargs dictionary
104 py::list keys = kwargs.keys();
105 for (int i = 0; i < py::len(keys); ++i)
106 {
107 py::object arg = kwargs[keys[i]];
108 if (arg)
109 {
110 parameters[py::extract<std::string>(keys[i])] =
111 py::extract<NekDouble>(arg);
112 }
113 }
114 interpreter.SetParameters(parameters);
115 return py::object();
116}
117
118/**
119 * @brief Wrapper for Interpreter::GetParameter.
120 *
121 * @param interpreter Interpreter object
122 * @param paramName Name of parameter
123 *
124 * @return Parameter defined by @a paramName.
125 */
126NekDouble Interpreter_GetParameter(std::shared_ptr<Interpreter> interpreter,
127 std::string paramName)
128{
129
130 return interpreter->GetParameter(paramName);
131}
132
133/**
134 * @brief Wrapper for Interpreter::GetConstant.
135 *
136 * @param interpreter Interpreter object
137 * @param constantName Name of constant
138 *
139 * @return Constant defined by @a constantName.
140 */
141NekDouble Interpreter_GetConstant(std::shared_ptr<Interpreter> interpreter,
142 std::string constantName)
143{
144
145 return interpreter->GetConstant(constantName);
146}
147
148/**
149 * @brief Wrapper for Interpreter::Evaluate (only constant or parameter).
150 *
151 * @param interpreter Interpreter object
152 * @param id id of the expression
153 *
154 * @return Value corresponding to @a id
155 */
156NekDouble Interpreter_Evaluate(std::shared_ptr<Interpreter> interpreter,
157 const int id)
158{
159 return interpreter->Evaluate(id);
160}
161
162/**
163 * @brief Wrapper for Interpreter::Evaluate (only constant parameters).
164 *
165 * @param interpreter Interpreter object
166 * @param id id of the expression
167 * @param x x-coordinate within the expression
168 * @param y y-coordinate within the expression
169 * @param z z-coordinate within the expression
170 * @param t value of time within the expression
171 *
172 * @return Value of evaluated expression.
173 */
174NekDouble Interpreter_Evaluate2(std::shared_ptr<Interpreter> interpreter,
175 const int id, const NekDouble x,
176 const NekDouble y, const NekDouble z,
177 const NekDouble t)
178{
179 return interpreter->Evaluate(id, x, y, z, t);
180}
181
182/**
183 * @brief Wrapper for Interpreter::Evaluate (vectorised version of the
184 * evaluation method that will allow the same function to be evaluated many
185 * times).
186 *
187 * @param interpreter Interpreter object
188 * @param id id of the expression
189 * @param x x-coordinates within the expression
190 * @param y y-coordinates within the expression
191 * @param z z-coordinates within the expression
192 * @param t values of time within the expression
193 *
194 * @return Values of evaluated expression.
195 */
197 std::shared_ptr<Interpreter> interpreter, const int id,
202{
203 Array<OneD, NekDouble> tmp(x.size());
204 interpreter->Evaluate(id, x, y, z, t, tmp);
205 return tmp;
206}
207
208/**
209 * @brief Wrapper for Interpreter::Evaluate (zero or more arrays).
210 *
211 * @param expression_id id of the expression
212 * @param points vector containing arrays of values required for the
213 * expression.
214 *
215 * @return Values of evaluated expression.
216 */
218 std::shared_ptr<Interpreter> interpreter, const int expression_id,
219 const std::vector<Array<OneD, const NekDouble>> &points)
220{
221 Array<OneD, NekDouble> tmp(points.size());
222 interpreter->Evaluate(expression_id, points, tmp);
223 return tmp;
224}
225
227{
228 py::class_<Interpreter, std::shared_ptr<Interpreter>, boost::noncopyable>(
229 "Interpreter", py::init<>())
230
231 .def("SetRandomSeed", &Interpreter::SetRandomSeed)
232
233 .def("AddConstants", py::raw_function(Interpreter_AddConstants))
234 .def("AddConstant", &Interpreter::AddConstant)
235 .def("GetConstant", Interpreter_GetConstant)
236
237 .def("SetParameters", py::raw_function(Interpreter_SetParameters))
238 .def("SetParameter", &Interpreter::SetParameter)
239 .def("GetParameter", Interpreter_GetParameter)
240
241 .def("GetTime", &Interpreter::GetTime)
242 .def("DefineFunction", &Interpreter::DefineFunction)
243
244 .def("Evaluate", Interpreter_Evaluate)
245 .def("Evaluate", Interpreter_Evaluate2)
246 .def("Evaluate", Interpreter_Evaluate3)
247 .def("Evaluate", Interpreter_Evaluate4)
248
249 ;
250}
NekDouble Interpreter_Evaluate2(std::shared_ptr< Interpreter > interpreter, const int id, const NekDouble x, const NekDouble y, const NekDouble z, const NekDouble t)
Wrapper for Interpreter::Evaluate (only constant parameters).
Array< OneD, NekDouble > Interpreter_Evaluate4(std::shared_ptr< Interpreter > interpreter, const int expression_id, const std::vector< Array< OneD, const NekDouble > > &points)
Wrapper for Interpreter::Evaluate (zero or more arrays).
NekDouble Interpreter_Evaluate(std::shared_ptr< Interpreter > interpreter, const int id)
Wrapper for Interpreter::Evaluate (only constant or parameter).
NekDouble Interpreter_GetParameter(std::shared_ptr< Interpreter > interpreter, std::string paramName)
Wrapper for Interpreter::GetParameter.
py::object Interpreter_SetParameters(py::tuple args, py::dict kwargs)
Wrapper for Interpreter::SetParameters.
Array< OneD, NekDouble > Interpreter_Evaluate3(std::shared_ptr< Interpreter > interpreter, const int id, const Array< OneD, const NekDouble > &x, const Array< OneD, const NekDouble > &y, const Array< OneD, const NekDouble > &z, const Array< OneD, const NekDouble > &t)
Wrapper for Interpreter::Evaluate (vectorised version of the evaluation method that will allow the sa...
NekDouble Interpreter_GetConstant(std::shared_ptr< Interpreter > interpreter, std::string constantName)
Wrapper for Interpreter::GetConstant.
py::object Interpreter_AddConstants(py::tuple args, py::dict kwargs)
Wrapper for Interpreter::AddConstants.
Interpreter class for the evaluation of mathematical expressions.
Definition: Interpreter.h:76
void AddConstants(std::map< std::string, NekDouble > const &constants)
Set constants to be evaluated.
void SetParameters(std::map< std::string, NekDouble > const &params)
Set parameter values.
std::vector< double > z(NPUPPER)
double NekDouble