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
39
41
42using namespace Nektar;
43using namespace Nektar::SolverUtils;
44
45/**
46 * @brief Filter wrapper to handle virtual function calls in @c Filter and its
47 * subclasses.
48 */
49#pragma GCC visibility push(hidden)
50struct FilterWrap : public Filter, public py::trampoline_self_life_support
51{
52 /**
53 * @brief Constructor, which is identical to Filter.
54 *
55 * @param field Input field.
56 */
58 std::shared_ptr<EquationSystem> eqn)
59 : Filter(session, eqn)
60 {
61 }
62
65 const NekDouble &time) override
66 {
67 PYBIND11_OVERRIDE_PURE_NAME(void, Filter, "Initialise", v_Initialise,
68 pFields, time);
69 }
70
73 const NekDouble &time) override
74 {
75 PYBIND11_OVERRIDE_PURE_NAME(void, Filter, "Update", v_Update, pFields,
76 time);
77 }
78
81 const NekDouble &time) override
82 {
83 PYBIND11_OVERRIDE_PURE_NAME(void, Filter, "Finalise", v_Finalise,
84 pFields, time);
85 }
86
87 bool v_IsTimeDependent() override
88 {
89 PYBIND11_OVERRIDE_PURE_NAME(bool, Filter, "IsTimeDependent",
91 }
92};
93#pragma GCC visibility pop
94
95struct FilterPublic : public Filter
96{
97 using Filter::v_Finalise;
98 using Filter::v_Initialise;
99 using Filter::v_IsTimeDependent;
100 using Filter::v_Update;
101};
102
103/**
104 * @brief Lightweight wrapper for Filter factory creation function.
105 */
106FilterSharedPtr Filter_Create(py::args args, const py::kwargs &kwargs)
107{
109
110 if (py::len(args) != 3)
111 {
112 throw NekError("Filter.Create() requires three arguments: "
113 "filter name, a SessionReader object and an "
114 "EquationSystem object.");
115 }
116
117 std::string filterName = py::cast<std::string>(args[0]);
118
121
122 try
123 {
124 session = py::cast<LibUtilities::SessionReaderSharedPtr>(args[1]);
125 }
126 catch (...)
127 {
128 throw NekError("Second argument to Create() should be a SessionReader "
129 "object.");
130 }
131
132 try
133 {
134 eqsys = py::cast<EquationSystemSharedPtr>(args[2]);
135 }
136 catch (...)
137 {
138 throw NekError("Second argument to Create() should be a EquationSystem "
139 "object.");
140 }
141
142 // Process keyword arguments.
143 Filter::ParamMap params;
144
145 for (auto &kwarg : kwargs)
146 {
147 std::string arg = py::cast<std::string>(kwarg.first);
148 std::string val = py::cast<std::string>(py::str(kwarg.second));
149 params[arg] = val;
150 }
151
152 std::shared_ptr<Filter> filter =
153 GetFilterFactory().CreateInstance(filterName, session, eqsys, params);
154
155 return filter;
156}
157
158void export_Filter(py::module &m)
159{
161
162 // Wrapper for the Filter class. Note that since Filter contains a pure
163 // virtual function, we need the FilterWrap helper class to handle this for
164 // us. In the lightweight wrappers above, we therefore need to ensure we're
165 // passing std::shared_ptr<Filter> as the first argument, otherwise they
166 // won't accept objects constructed from Python.
167 py::classh<Filter, FilterWrap>(m, "Filter")
170
171 .def("Initialise", &FilterPublic::v_Initialise)
172 .def("Update", &FilterPublic::v_Update)
173 .def("Finalise", &FilterPublic::v_Finalise)
174 .def("IsTimeDependent", &FilterPublic::v_IsTimeDependent)
175
176 // Factory functions.
177 .def_static("Create", &Filter_Create)
178
179 .def_static("Register", [](std::string &filterName, py::object &obj) {
180 fac(filterName, obj, filterName);
181 });
182}
Nektar::ErrorUtil::NekError NekError
void export_Filter(py::module &m)
FilterSharedPtr Filter_Create(py::args args, const py::kwargs &kwargs)
Lightweight wrapper for Filter factory creation function.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
virtual void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)=0
virtual void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)=0
virtual bool v_IsTimeDependent()=0
std::map< std::string, std::string > ParamMap
Definition: Filter.h:66
virtual void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)=0
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Filter > FilterSharedPtr
A shared pointer to a Driver object.
Definition: Filter.h:52
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
void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
bool v_IsTimeDependent() override