Nektar++
TimeIntegrationSchemeOperators.cpp
Go to the documentation of this file.
1//////////////////////////////////////////////////////////////////////////////
2//
3// File: TimeIntegrationSchemeOperators.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 TimeIntegrationScheme.
32//
33///////////////////////////////////////////////////////////////////////////////
34
36
38
39using namespace Nektar;
40using namespace Nektar::LibUtilities;
41
42// Converts a OneD array of ExpLists to a Python list.
43inline py::list ArrayOneDToPyList(
44 const Array<OneD, const Array<OneD, NekDouble>> &in)
45{
46 py::list ret;
47
48 for (int i = 0; i < in.size(); ++i)
49 {
50 ret.append(py::object(in[i]));
51 }
52
53 return ret;
54}
55
56/**
57 * @brief Helper class for holding a reference to a Python function to act as a
58 * wrapper for TimeIntegrationScheme::FunctorType1.
59 *
60 * This wrapper is used for TimeIntegrationSchemeOperators::DefineOdeRhs and
61 * TimeIntegrationSchemeOperators::DefineProjection.
62 */
64{
65 /// Default constructor
66 CallbackHolderT1(py::object cb) : m_cb(cb)
67 {
68 }
69
70 /**
71 * @brief C++ callback function to invoke Python function stored in #m_cb.
72 */
75 const NekDouble time)
76 {
77 py::object ret = m_cb(in, time);
78
79 py::list outList = py::extract<py::list>(ret);
80
81 for (std::size_t i = 0; i < py::len(outList); ++i)
82 {
83 out[i] = py::extract<Array<OneD, NekDouble>>(outList[i]);
84 }
85 }
86
87private:
88 /// Callback defined in Python code.
89 py::object m_cb;
90};
91
92/**
93 * @brief Helper class for holding a reference to a Python function to act as a
94 * wrapper for TimeIntegrationScheme::FunctorType1.
95 *
96 * This wrapper is used for TimeIntegrationSchemeOperators::DefineImplicitSolve.
97 */
99{
100 /// Default constructor
101 CallbackHolderT2(py::object cb) : m_cb(cb)
102 {
103 }
104
105 /**
106 * @brief C++ callback function to invoke Python function stored in #m_cb.
107 */
110 const NekDouble time, const NekDouble lambda)
111 {
112 py::object ret = m_cb(in, time, lambda);
113
114 py::list outList = py::extract<py::list>(ret);
115
116 for (std::size_t i = 0; i < py::len(outList); ++i)
117 {
118 out[i] = py::extract<Array<OneD, NekDouble>>(outList[i]);
119 }
120 }
121
122private:
123 /// Callback defined in Python code.
124 py::object m_cb;
125};
126
128 TimeIntegrationSchemeOperatorsSharedPtr op, py::object callback)
129{
130 CallbackHolderT1 *cb = new CallbackHolderT1(callback);
131
132 op->DefineOdeRhs(&CallbackHolderT1::call, cb);
133}
134
136 TimeIntegrationSchemeOperatorsSharedPtr op, py::object callback)
137{
138 CallbackHolderT1 *cb = new CallbackHolderT1(callback);
139
140 op->DefineProjection(&CallbackHolderT1::call, cb);
141}
142
144 TimeIntegrationSchemeOperatorsSharedPtr op, py::object callback)
145{
146 CallbackHolderT2 *cb = new CallbackHolderT2(callback);
147
148 op->DefineImplicitSolve(&CallbackHolderT2::call, cb);
149}
150
152{
154 std::shared_ptr<TimeIntegrationSchemeOperators>,
155 boost::noncopyable>("TimeIntegrationSchemeOperators",
156 py::init<>())
158 .def("DefineProjection",
160 .def("DefineImplicitSolve",
162}
py::list ArrayOneDToPyList(const Array< OneD, const Array< OneD, NekDouble > > &in)
void TimeIntegrationSchemeOperators_DefineOdeRhs(TimeIntegrationSchemeOperatorsSharedPtr op, py::object callback)
void TimeIntegrationSchemeOperators_DefineImplicitSolve(TimeIntegrationSchemeOperatorsSharedPtr op, py::object callback)
void TimeIntegrationSchemeOperators_DefineProjection(TimeIntegrationSchemeOperatorsSharedPtr op, py::object callback)
void export_TimeIntegrationSchemeOperators()
Binds a set of functions for use by time integration schemes.
std::shared_ptr< TimeIntegrationSchemeOperators > TimeIntegrationSchemeOperatorsSharedPtr
double NekDouble
Helper class for holding a reference to a Python function to act as a wrapper for TimeIntegrationSche...
void call(TimeIntegrationSchemeOperators::InArrayType &in, TimeIntegrationSchemeOperators::OutArrayType &out, const NekDouble time)
C++ callback function to invoke Python function stored in m_cb.
CallbackHolderT1(py::object cb)
Default constructor.
py::object m_cb
Callback defined in Python code.
Helper class for holding a reference to a Python function to act as a wrapper for TimeIntegrationSche...
void call(TimeIntegrationSchemeOperators::InArrayType &in, TimeIntegrationSchemeOperators::OutArrayType &out, const NekDouble time, const NekDouble lambda)
C++ callback function to invoke Python function stored in m_cb.
py::object m_cb
Callback defined in Python code.
CallbackHolderT2(py::object cb)
Default constructor.