Nektar++
Python/Filter.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: Filter.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 Filter.
32//
33///////////////////////////////////////////////////////////////////////////////
34
37
39
40using namespace Nektar;
41using namespace Nektar::SolverUtils;
42
43/**
44 * @brief Filter wrapper to handle virtual function calls in @c Filter and its
45 * subclasses.
46 */
47struct FilterWrap : public Filter, public py::wrapper<Filter>
48{
49 /**
50 * @brief Constructor, which is identical to Filter.
51 *
52 * @param field Input field.
53 */
55 std::shared_ptr<EquationSystem> eqn)
56 : Filter(session, eqn), py::wrapper<Filter>()
57 {
58 }
59
62 {
63 py::list expLists;
64
65 for (int i = 0; i < pFields.size(); ++i)
66 {
67 expLists.append(py::object(pFields[i]));
68 }
69
70 return expLists;
71 }
72
75 const NekDouble &time) override
76 {
77 this->get_override("Initialise")(ArrayOneDToPyList(pFields), time);
78 }
79
82 const NekDouble &time) override
83 {
84 this->get_override("Update")(ArrayOneDToPyList(pFields), time);
85 }
86
89 const NekDouble &time) override
90 {
91 this->get_override("Finalise")(ArrayOneDToPyList(pFields), time);
92 }
93
94 bool v_IsTimeDependent() override
95 {
96 return this->get_override("IsTimeDependent")();
97 }
98};
99
100/**
101 * @brief Lightweight wrapper for Filter factory creation function.
102 */
103FilterSharedPtr Filter_Create(py::tuple args, py::dict kwargs)
104{
106
107 if (py::len(args) != 3)
108 {
109 throw NekError("Filter.Create() requires three arguments: "
110 "filter name, a SessionReader object and an "
111 "EquationSystem object.");
112 }
113
114 std::string filterName = py::extract<std::string>(args[0]);
115
116 if (!py::extract<LibUtilities::SessionReaderSharedPtr>(args[1]).check())
117 {
118 throw NekError("Second argument to Create() should be a SessionReader "
119 "object.");
120 }
121
122 if (!py::extract<EquationSystemSharedPtr>(args[2]).check())
123 {
124 throw NekError("Second argument to Create() should be a EquationSystem "
125 "object.");
126 }
127
129 py::extract<LibUtilities::SessionReaderSharedPtr>(args[1]);
130 EquationSystemSharedPtr eqn = py::extract<EquationSystemSharedPtr>(args[2]);
131
132 // Process keyword arguments.
133 py::list items = kwargs.items();
134 Filter::ParamMap params;
135
136 for (int i = 0; i < py::len(items); ++i)
137 {
138 std::string arg = py::extract<std::string>(items[i][0]);
139 std::string val =
140 py::extract<std::string>(items[i][1].attr("__str__")());
141 params[arg] = val;
142 }
143
144 std::shared_ptr<Filter> filter =
145 GetFilterFactory().CreateInstance(filterName, session, eqn, params);
146
147 return filter;
148}
149
150void Filter_Initialise(std::shared_ptr<Filter> filter,
152 NekDouble time)
153{
154 filter->Initialise(expLists, time);
155}
156
157void Filter_Update(std::shared_ptr<Filter> filter,
159 NekDouble time)
160{
161 filter->Update(expLists, time);
162}
163
164void Filter_Finalise(std::shared_ptr<Filter> filter,
166 NekDouble time)
167{
168 filter->Finalise(expLists, time);
169}
170
171bool Filter_IsTimeDependent(std::shared_ptr<Filter> filter)
172{
173 return filter->IsTimeDependent();
174}
175
177{
179
180 // Define FilterWrap to be implicitly convertible to a Filter, since it
181 // seems that doesn't sometimes get picked up.
182 py::implicitly_convertible<std::shared_ptr<FilterWrap>,
183 std::shared_ptr<Filter>>();
184
185 // Wrapper for the Filter class. Note that since Filter contains a pure
186 // virtual function, we need the FilterWrap helper class to handle this for
187 // us. In the lightweight wrappers above, we therefore need to ensure we're
188 // passing std::shared_ptr<Filter> as the first argument, otherwise they
189 // won't accept objects constructed from Python.
190 py::class_<FilterWrap, std::shared_ptr<FilterWrap>, boost::noncopyable>(
191 "Filter", py::init<LibUtilities::SessionReaderSharedPtr,
193
194 .def("Initialise", &Filter_Initialise)
195 .def("Update", &Filter_Update)
196 .def("Finalise", &Filter_Finalise)
197 .def("IsTimeDependent", &Filter_IsTimeDependent)
198
199 // Factory functions.
200 .def("Create", py::raw_function(Filter_Create))
201 .staticmethod("Create")
202 .def("Register", [](std::string const &filterName,
203 py::object &obj) { fac(filterName, obj); })
204 .staticmethod("Register");
205
207}
Nektar::ErrorUtil::NekError NekError
FilterSharedPtr Filter_Create(py::tuple args, py::dict kwargs)
Lightweight wrapper for Filter factory creation function.
bool Filter_IsTimeDependent(std::shared_ptr< Filter > filter)
void export_Filter()
void Filter_Finalise(std::shared_ptr< Filter > filter, Array< OneD, MultiRegions::ExpListSharedPtr > expLists, NekDouble time)
void Filter_Initialise(std::shared_ptr< Filter > filter, Array< OneD, MultiRegions::ExpListSharedPtr > expLists, NekDouble time)
void Filter_Update(std::shared_ptr< Filter > filter, Array< OneD, MultiRegions::ExpListSharedPtr > expLists, NekDouble time)
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
std::map< std::string, std::string > ParamMap
Definition: Filter.h:65
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Filter > FilterSharedPtr
A shared pointer to a Driver object.
Definition: Filter.h:51
std::shared_ptr< EquationSystem > EquationSystemSharedPtr
A shared pointer to an EquationSystem object.
FilterFactory & GetFilterFactory()
double NekDouble
Filter wrapper to handle virtual function calls in Filter and its subclasses.
FilterWrap(LibUtilities::SessionReaderSharedPtr session, std::shared_ptr< EquationSystem > eqn)
Constructor, which is identical to Filter.
void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
py::list ArrayOneDToPyList(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields)
void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
bool v_IsTimeDependent() override
A helper class that for factory-based classes allows std::shared_ptr<T> as something that boost::pyth...