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
42
43using namespace Nektar;
44using 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 */
58void Equation_SetConstants(std::shared_ptr<Equation> equation, py::args args,
59 const py::kwargs &kwargs)
60{
61 // Map that will be passed to C++.
62 std::map<std::string, NekDouble> constants;
63
64 for (auto &it : kwargs)
65 {
66 const std::string &key = py::cast<const std::string>(it.first);
67 const NekDouble &val = py::cast<const NekDouble>(it.second);
68 constants[key] = val;
69 }
70
71 equation->SetConstants(constants);
72}
73
74/**
75 * @brief Construct an equation object from an expression string @p expr and a
76 * list of variables @p vlist.
77 *
78 * @param evaluator Interpreter object
79 * @param expr String contaning the expression
80 * @param vlist String contining the list of variables in @p expr.
81 *
82 * @return An #Equation object.
83 */
84std::shared_ptr<Equation> ConstructEquation(std::shared_ptr<Interpreter> interp,
85 std::string expr, std::string vlist)
86{
87 return std::make_shared<Equation>(interp, expr, vlist);
88}
89
90/**
91 * @brief Wrapper for Equation::Evaluate (overloaded for no parameters).
92 *
93 * @param equation Equation object
94 *
95 * @return Value of @p equation.
96 */
97NekDouble Equation_Evaluate1(std::shared_ptr<Equation> equation)
98{
99 return equation->Evaluate();
100}
101
102/**
103 * @brief Wrapper for Equation::Evaluate (overloaded for constant parameters).
104 *
105 * @param equation Equation object from Python
106 * @param x x-coordinate within the expression
107 * @param y y-coordinate within the expression
108 * @param z z-coordinate within the expression
109 * @param t value of time within the expression
110 *
111 * @return Value of evaluated expression.
112 */
113NekDouble Equation_Evaluate2(std::shared_ptr<Equation> equation,
114 const NekDouble x, const NekDouble y = 0,
115 const NekDouble z = 0, const NekDouble t = 0)
116{
117 return equation->Evaluate(x, y, z, t);
118}
119
120/**
121 * @brief Wrapper for Equation::Evaluate (overloaded for Array parameters).
122 *
123 * @param equation Equation object from Python
124 * @param x x-coordinates within the expression
125 * @param y y-coordinates within the expression
126 * @param z z-coordinates within the expression
127 *
128 * @return Array containing values of evaluated expression.
129 */
130Array<OneD, NekDouble> Equation_Evaluate3(std::shared_ptr<Equation> equation,
134{
135 Array<OneD, NekDouble> tmp(x.size());
136 equation->Evaluate(x, y, z, tmp);
137 return tmp;
138}
139
140/**
141 * @brief Wrapper for Equation::Evaluate (overloaded for Array parameters +
142 * constant time).
143 *
144 * @param equation Equation object from Python
145 * @param x x-coordinates within the expression
146 * @param y y-coordinates within the expression
147 * @param z z-coordinates within the expression
148 * @param t Value of time
149 *
150 * @return Array containing values of evaluated expression.
151 */
152Array<OneD, NekDouble> Equation_Evaluate4(std::shared_ptr<Equation> equation,
156 const NekDouble t)
157{
158 Array<OneD, NekDouble> tmp(x.size());
159 equation->Evaluate(x, y, z, t, tmp);
160 return tmp;
161}
162
163/**
164 * @brief Wrapper for Equation::Evaluate (overloaded for Array parameters +
165 * Array time).
166 *
167 * @param equation Equation object from Python
168 * @param x x-coordinates within the expression
169 * @param y y-coordinates within the expression
170 * @param z z-coordinates within the expression
171 * @param t Time values within the expression.
172 *
173 * @return Array containing values of evaluated expression.
174 */
175Array<OneD, NekDouble> Equation_Evaluate5(std::shared_ptr<Equation> equation,
180{
181 Array<OneD, NekDouble> tmp(x.size());
182 equation->Evaluate(x, y, z, t, tmp);
183 return tmp;
184}
185
186void export_Equation(py::module &m)
187{
188 py::class_<Equation, std::shared_ptr<Equation>>(m, "Equation")
189
190 .def(py::init(&ConstructEquation))
191
192 .def("Evaluate", Equation_Evaluate1)
193 .def("Evaluate", Equation_Evaluate2)
194 .def("Evaluate", Equation_Evaluate3)
195 .def("Evaluate", Equation_Evaluate4)
196 .def("Evaluate", Equation_Evaluate5)
197
198 .def("SetParameter", &Equation::SetParameter)
199 .def("SetConstants", &Equation_SetConstants)
200 .def("GetExpression", &Equation::GetExpression)
201 .def("GetVlist", &Equation::GetVlist)
202
203 .def("GetTime", &Equation::GetTime);
204}
void Equation_SetConstants(std::shared_ptr< Equation > equation, py::args args, const py::kwargs &kwargs)
Wrapper for Equation::SetConstants.
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 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).
NekDouble Equation_Evaluate1(std::shared_ptr< Equation > equation)
Wrapper for Equation::Evaluate (overloaded for no parameters).
std::shared_ptr< Equation > ConstructEquation(std::shared_ptr< Interpreter > interp, std::string expr, std::string vlist)
Construct an equation object from an expression string expr and a list of variables vlist.
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).
void export_Equation(py::module &m)
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).
std::vector< double > z(NPUPPER)
double NekDouble