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
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 */
58void Interpreter_AddConstants(std::shared_ptr<Interpreter> interpreter,
59 py::args args, const py::kwargs &kwargs)
60{
61 // Map that will be passed to C++
62 std::map<std::string, NekDouble> constants;
63
64 // Loop over the keys inside the kwargs dictionary
65 for (auto &it : kwargs)
66 {
67 const std::string &key = py::cast<const std::string>(it.first);
68 const NekDouble &val = py::cast<const NekDouble>(it.second);
69 constants[key] = val;
70 }
71
72 interpreter->AddConstants(constants);
73}
74
75/**
76 * @brief Wrapper for Interpreter::SetParameters.
77 *
78 * boost.python does not know how (by default) to convert from any Python
79 * datatype to a C++ map, so we add a pythonic way to set parameters through
80 * keyword arguments.
81 *
82 * @param args Python function positional arguments.
83 * @param kwargs Python function keyword arguments.
84 *
85 * @return None (null py::object).
86 */
87void Interpreter_SetParameters(std::shared_ptr<Interpreter> interpreter,
88 py::args args, const py::kwargs &kwargs)
89{
90 // Map that will be passed to C++
91 std::map<std::string, NekDouble> parameters;
92
93 // Loop over the keys inside the kwargs dictionary
94 for (auto &it : kwargs)
95 {
96 const std::string &key = py::cast<const std::string>(it.first);
97 const NekDouble &val = py::cast<const NekDouble>(it.second);
98 parameters[key] = val;
99 }
100
101 interpreter->SetParameters(parameters);
102}
103
104/**
105 * @brief Wrapper for Interpreter::GetParameter.
106 *
107 * @param interpreter Interpreter object
108 * @param paramName Name of parameter
109 *
110 * @return Parameter defined by @a paramName.
111 */
112NekDouble Interpreter_GetParameter(std::shared_ptr<Interpreter> interpreter,
113 std::string paramName)
114{
115
116 return interpreter->GetParameter(paramName);
117}
118
119/**
120 * @brief Wrapper for Interpreter::GetConstant.
121 *
122 * @param interpreter Interpreter object
123 * @param constantName Name of constant
124 *
125 * @return Constant defined by @a constantName.
126 */
127NekDouble Interpreter_GetConstant(std::shared_ptr<Interpreter> interpreter,
128 std::string constantName)
129{
130
131 return interpreter->GetConstant(constantName);
132}
133
134/**
135 * @brief Wrapper for Interpreter::Evaluate (only constant or parameter).
136 *
137 * @param interpreter Interpreter object
138 * @param id id of the expression
139 *
140 * @return Value corresponding to @a id
141 */
142NekDouble Interpreter_Evaluate(std::shared_ptr<Interpreter> interpreter,
143 const int id)
144{
145 return interpreter->Evaluate(id);
146}
147
148/**
149 * @brief Wrapper for Interpreter::Evaluate (only constant parameters).
150 *
151 * @param interpreter Interpreter object
152 * @param id id of the expression
153 * @param x x-coordinate within the expression
154 * @param y y-coordinate within the expression
155 * @param z z-coordinate within the expression
156 * @param t value of time within the expression
157 *
158 * @return Value of evaluated expression.
159 */
160NekDouble Interpreter_Evaluate2(std::shared_ptr<Interpreter> interpreter,
161 const int id, const NekDouble x,
162 const NekDouble y, const NekDouble z,
163 const NekDouble t)
164{
165 return interpreter->Evaluate(id, x, y, z, t);
166}
167
168/**
169 * @brief Wrapper for Interpreter::Evaluate (vectorised version of the
170 * evaluation method that will allow the same function to be evaluated many
171 * times).
172 *
173 * @param interpreter Interpreter object
174 * @param id id of the expression
175 * @param x x-coordinates within the expression
176 * @param y y-coordinates within the expression
177 * @param z z-coordinates within the expression
178 * @param t values of time within the expression
179 *
180 * @return Values of evaluated expression.
181 */
183 std::shared_ptr<Interpreter> interpreter, const int id,
188{
189 Array<OneD, NekDouble> tmp(x.size());
190 interpreter->Evaluate(id, x, y, z, t, tmp);
191 return tmp;
192}
193
194/**
195 * @brief Wrapper for Interpreter::Evaluate (zero or more arrays).
196 *
197 * @param expression_id id of the expression
198 * @param points vector containing arrays of values required for the
199 * expression.
200 *
201 * @return Values of evaluated expression.
202 */
204 std::shared_ptr<Interpreter> interpreter, const int expression_id,
205 const std::vector<Array<OneD, const NekDouble>> &points)
206{
207 Array<OneD, NekDouble> tmp(points.size());
208 interpreter->Evaluate(expression_id, points, tmp);
209 return tmp;
210}
211
212void export_Interpreter(py::module &m)
213{
214 py::class_<Interpreter, std::shared_ptr<Interpreter>>(m, "Interpreter")
215
216 .def(py::init<>())
217
218 .def("SetRandomSeed", &Interpreter::SetRandomSeed)
219
220 .def("AddConstants", &Interpreter_AddConstants)
221 .def("AddConstant", &Interpreter::AddConstant)
222 .def("GetConstant", Interpreter_GetConstant)
223
224 .def("SetParameters", &Interpreter_SetParameters)
225 .def("SetParameter", &Interpreter::SetParameter)
226 .def("GetParameter", Interpreter_GetParameter)
227
228 .def("GetTime", &Interpreter::GetTime)
229 .def("DefineFunction", &Interpreter::DefineFunction)
230
231 .def("Evaluate", Interpreter_Evaluate)
232 .def("Evaluate", Interpreter_Evaluate2)
233 .def("Evaluate", Interpreter_Evaluate3)
234 .def("Evaluate", Interpreter_Evaluate4)
235
236 ;
237}
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.
void export_Interpreter(py::module &m)
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.
void Interpreter_SetParameters(std::shared_ptr< Interpreter > interpreter, py::args args, const py::kwargs &kwargs)
Wrapper for Interpreter::SetParameters.
void Interpreter_AddConstants(std::shared_ptr< Interpreter > interpreter, py::args args, const py::kwargs &kwargs)
Wrapper for Interpreter::AddConstants.
std::vector< double > z(NPUPPER)
double NekDouble