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
38
39using namespace Nektar;
40using namespace Nektar::LibUtilities;
41
42/**
43 * @brief Helper class for holding a reference to a Python function to act as a
44 * wrapper for TimeIntegrationScheme::FunctorType1.
45 *
46 * This wrapper is used for TimeIntegrationSchemeOperators::DefineOdeRhs and
47 * TimeIntegrationSchemeOperators::DefineProjection.
48 */
49#pragma GCC visibility push(hidden)
51{
52 /// Default constructor
53 CallbackHolderT1(py::object cb) : m_cb(cb)
54 {
55 }
56
57 /**
58 * @brief C++ callback function to invoke Python function stored in #m_cb.
59 */
62 const NekDouble time)
63 {
64 py::object ret = m_cb(in, time);
65
66 py::list outList = py::cast<py::list>(ret);
67
68 for (std::size_t i = 0; i < py::len(outList); ++i)
69 {
70 out[i] = py::cast<Array<OneD, NekDouble>>(outList[i]);
71 }
72 }
73
74private:
75 /// Callback defined in Python code.
76 py::object m_cb;
77};
78#pragma GCC visibility pop
79
80/**
81 * @brief Helper class for holding a reference to a Python function to act as a
82 * wrapper for TimeIntegrationScheme::FunctorType1.
83 *
84 * This wrapper is used for TimeIntegrationSchemeOperators::DefineImplicitSolve.
85 */
86#pragma GCC visibility push(hidden)
88{
89 /// Default constructor
90 CallbackHolderT2(py::object cb) : m_cb(cb)
91 {
92 }
93
94 /**
95 * @brief C++ callback function to invoke Python function stored in #m_cb.
96 */
99 const NekDouble time, const NekDouble lambda)
100 {
101 py::object ret = m_cb(in, time, lambda);
102
103 py::list outList = py::cast<py::list>(ret);
104
105 for (std::size_t i = 0; i < py::len(outList); ++i)
106 {
107 out[i] = py::cast<Array<OneD, NekDouble>>(outList[i]);
108 }
109 }
110
111private:
112 /// Callback defined in Python code.
113 py::object m_cb;
114};
115#pragma GCC visibility pop
116
118 TimeIntegrationSchemeOperatorsSharedPtr op, py::object callback)
119{
120 CallbackHolderT1 *cb = new CallbackHolderT1(callback);
121
122 op->DefineOdeRhs(&CallbackHolderT1::call, cb);
123}
124
126 TimeIntegrationSchemeOperatorsSharedPtr op, py::object callback)
127{
128 CallbackHolderT1 *cb = new CallbackHolderT1(callback);
129
130 op->DefineProjection(&CallbackHolderT1::call, cb);
131}
132
134 TimeIntegrationSchemeOperatorsSharedPtr op, py::object callback)
135{
136 CallbackHolderT2 *cb = new CallbackHolderT2(callback);
137
138 op->DefineImplicitSolve(&CallbackHolderT2::call, cb);
139}
140
142{
144 std::shared_ptr<TimeIntegrationSchemeOperators>>(
145 m, "TimeIntegrationSchemeOperators")
147 .def("DefineProjection",
149 .def("DefineImplicitSolve",
151}
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(py::module &m)
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.