Nektar++
Python/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: Python wrapper for Equation.
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
38 
40 
41 #include <boost/python/raw_function.hpp>
42 
43 using namespace Nektar;
44 using namespace Nektar::LibUtilities;
45 
46 /**
47  * @brief Wrapper for Equation::SetConstants.
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  */
58 py::object Equation_SetConstants(py::tuple args, py::dict kwargs)
59 {
60  // To extract the 'self' object
61  Equation &equation = py::extract<Equation &>(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  equation.SetConstants(constants);
79 
80  // Return nothing.
81  return py::object();
82 }
83 
84 /**
85  * @brief Construct an equation object from an expression string @p expr and a
86  * list of variables @p vlist.
87  *
88  * @param evaluator Interpreter object
89  * @param expr String contaning the expression
90  * @param vlist String contining the list of variables in @p expr.
91  *
92  * @return An #Equation object.
93  */
94 std::shared_ptr<Equation> ConstructEquation(
95  std::shared_ptr<Interpreter> evaluator, std::string expr, std::string vlist)
96 {
97  return std::make_shared<Equation>(evaluator, expr, vlist);
98 }
99 
100 /**
101  * @brief Wrapper for Equation::Evaluate (overloaded for no parameters).
102  *
103  * @param equation Equation object
104  *
105  * @return Value of @p equation.
106  */
107 NekDouble Equation_Evaluate1(std::shared_ptr<Equation> equation)
108 {
109  return equation->Evaluate();
110 }
111 
112 /**
113  * @brief Wrapper for Equation::Evaluate (overloaded for constant parameters).
114  *
115  * @param equation Equation object from Python
116  * @param x x-coordinate within the expression
117  * @param y y-coordinate within the expression
118  * @param z z-coordinate within the expression
119  * @param t value of time within the expression
120  *
121  * @return Value of evaluated expression.
122  */
123 NekDouble Equation_Evaluate2(std::shared_ptr<Equation> equation,
124  const NekDouble x, const NekDouble y = 0,
125  const NekDouble z = 0, const NekDouble t = 0)
126 {
127  return equation->Evaluate(x, y, z, t);
128 }
129 
130 /**
131  * @brief Wrapper for Equation::Evaluate (overloaded for Array parameters).
132  *
133  * @param equation Equation object from Python
134  * @param x x-coordinates within the expression
135  * @param y y-coordinates within the expression
136  * @param z z-coordinates within the expression
137  *
138  * @return Array containing values of evaluated expression.
139  */
140 Array<OneD, NekDouble> Equation_Evaluate3(std::shared_ptr<Equation> equation,
144 {
145  Array<OneD, NekDouble> tmp(x.size());
146  equation->Evaluate(x, y, z, tmp);
147  return tmp;
148 }
149 
150 /**
151  * @brief Wrapper for Equation::Evaluate (overloaded for Array parameters +
152  * constant time).
153  *
154  * @param equation Equation object from Python
155  * @param x x-coordinates within the expression
156  * @param y y-coordinates within the expression
157  * @param z z-coordinates within the expression
158  * @param t Value of time
159  *
160  * @return Array containing values of evaluated expression.
161  */
162 Array<OneD, NekDouble> Equation_Evaluate4(std::shared_ptr<Equation> equation,
166  const NekDouble t)
167 {
168  Array<OneD, NekDouble> tmp(x.size());
169  equation->Evaluate(x, y, z, t, tmp);
170  return tmp;
171 }
172 
173 /**
174  * @brief Wrapper for Equation::Evaluate (overloaded for Array parameters +
175  * Array time).
176  *
177  * @param equation Equation object from Python
178  * @param x x-coordinates within the expression
179  * @param y y-coordinates within the expression
180  * @param z z-coordinates within the expression
181  * @param t Time values within the expression.
182  *
183  * @return Array containing values of evaluated expression.
184  */
185 Array<OneD, NekDouble> Equation_Evaluate5(std::shared_ptr<Equation> equation,
190 {
191  Array<OneD, NekDouble> tmp(x.size());
192  equation->Evaluate(x, y, z, t, tmp);
193  return tmp;
194 }
195 
197 {
198  py::class_<Equation, std::shared_ptr<Equation>, boost::noncopyable>(
199  "Equation", py::no_init)
200 
201  .def("__init__", py::make_constructor(ConstructEquation))
202 
203  .def("Evaluate", Equation_Evaluate1)
204  .def("Evaluate", Equation_Evaluate2)
205  .def("Evaluate", Equation_Evaluate3)
206  .def("Evaluate", Equation_Evaluate4)
207  .def("Evaluate", Equation_Evaluate5)
208 
209  .def("SetParameter", &Equation::SetParameter)
210  .def("SetConstants", py::raw_function(Equation_SetConstants))
211  .def("GetExpression", &Equation::GetExpression)
212  .def("GetVlist", &Equation::GetVlist)
213 
214  .def("GetTime", &Equation::GetTime);
215 }
std::shared_ptr< Equation > ConstructEquation(std::shared_ptr< Interpreter > evaluator, std::string expr, std::string vlist)
Construct an equation object from an expression string expr and a list of variables vlist.
py::object Equation_SetConstants(py::tuple args, py::dict kwargs)
Wrapper for Equation::SetConstants.
Array< OneD, NekDouble > Equation_Evaluate4(std::shared_ptr< Equation > equation, const Array< OneD, const NekDouble > &x, const Array< OneD, const NekDouble > &y, const Array< OneD, const NekDouble > &z, const NekDouble t)
Wrapper for Equation::Evaluate (overloaded for Array parameters + constant time).
NekDouble Equation_Evaluate2(std::shared_ptr< Equation > equation, const NekDouble x, const NekDouble y=0, const NekDouble z=0, const NekDouble t=0)
Wrapper for Equation::Evaluate (overloaded for constant parameters).
void export_Equation()
NekDouble Equation_Evaluate1(std::shared_ptr< Equation > equation)
Wrapper for Equation::Evaluate (overloaded for no parameters).
Array< OneD, NekDouble > Equation_Evaluate3(std::shared_ptr< Equation > equation, const Array< OneD, const NekDouble > &x, const Array< OneD, const NekDouble > &y, const Array< OneD, const NekDouble > &z)
Wrapper for Equation::Evaluate (overloaded for Array parameters).
Array< OneD, NekDouble > Equation_Evaluate5(std::shared_ptr< Equation > equation, 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 Equation::Evaluate (overloaded for Array parameters + Array time).
NekDouble GetTime() const
Returns time spend on expression evaluation at points (it does not include parse/pre-processing time)...
std::string GetExpression(void) const
void SetConstants(const std::map< std::string, NekDouble > &constants)
void SetParameter(const std::string &name, NekDouble value)
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble