Nektar++
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Private Attributes | Friends | List of all members
Nektar::SolverUtils::FilterPython Class Reference

#include <FilterPython.h>

Inheritance diagram for Nektar::SolverUtils::FilterPython:
[legend]

Public Member Functions

SOLVER_UTILS_EXPORT FilterPython (const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< EquationSystem > &pEquation, const ParamMap &pParams)
 
SOLVER_UTILS_EXPORT ~FilterPython () override
 
- Public Member Functions inherited from Nektar::SolverUtils::Filter
SOLVER_UTILS_EXPORT Filter (const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< EquationSystem > &pEquation)
 
virtual SOLVER_UTILS_EXPORT ~Filter ()
 
SOLVER_UTILS_EXPORT void Initialise (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
 
SOLVER_UTILS_EXPORT void Update (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
 
SOLVER_UTILS_EXPORT void Finalise (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
 
SOLVER_UTILS_EXPORT bool IsTimeDependent ()
 

Static Public Member Functions

static FilterSharedPtr create (const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< EquationSystem > &pEquation, const std::map< std::string, std::string > &pParams)
 Creates an instance of this class. More...
 

Static Public Attributes

static std::string className
 Name of the class. More...
 

Protected Member Functions

void v_Initialise (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
 
void v_Update (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
 
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 void v_Finalise (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)=0
 
virtual bool v_IsTimeDependent ()=0
 

Private Attributes

LibUtilities::FieldIOSharedPtr m_fld
 
py::object m_global
 
std::shared_ptr< Filterm_pyFilter = nullptr
 

Friends

class MemoryManager< FilterPython >
 

Additional Inherited Members

- Public Types inherited from Nektar::SolverUtils::Filter
typedef std::map< std::string, std::string > ParamMap
 
- Protected Attributes inherited from Nektar::SolverUtils::Filter
LibUtilities::SessionReaderSharedPtr m_session
 
const std::weak_ptr< EquationSystemm_equ
 

Detailed Description

Definition at line 43 of file FilterPython.h.

Constructor & Destructor Documentation

◆ FilterPython()

Nektar::SolverUtils::FilterPython::FilterPython ( const LibUtilities::SessionReaderSharedPtr pSession,
const std::shared_ptr< EquationSystem > &  pEquation,
const ParamMap pParams 
)

Definition at line 139 of file FilterPython.cpp.

142 : Filter(pSession, pEquation)
143{
144 auto it = pParams.find("PythonFile");
145 ASSERTL0(it != pParams.end(), "Empty parameter 'PythonFile'.");
146 std::string pythonFile = it->second;
147
148 Py_Initialize();
149 std::string pythonCode;
150
151 // Set the global namespace.
152 m_global = py::import("__main__").attr("__dict__");
153
154 try
155 {
156 std::ifstream t(pythonFile);
157 std::string str((std::istreambuf_iterator<char>(t)),
158 std::istreambuf_iterator<char>());
159 pythonCode = str;
160 }
161 catch (...)
162 {
163 ASSERTL0(false, "Error reading Python file: " + pythonFile);
164 }
165
166 // Print out the loaded session file if we're running in verbose mode.
167 if (pSession->DefinesCmdLineArgument("verbose"))
168 {
169 std::cout << "-----------------" << std::endl;
170 std::cout << "BEGIN PYTHON CODE" << std::endl;
171 std::cout << "-----------------" << std::endl;
172 std::cout << pythonCode << std::endl;
173 std::cout << "-----------------" << std::endl;
174 std::cout << "END PYTHON CODE" << std::endl;
175 std::cout << "-----------------" << std::endl;
176 }
177
178 try
179 {
180 // Import NekPy libraries. This ensures we have all of our boost python
181 // wrappers. I guess this could also be done by the calling script...
182 auto nekpy = py::import("NekPy");
183 auto multireg = py::import("NekPy.MultiRegions");
184
185 // Eval the python code. We can then grab functions etc from the global
186 // namespace.
187 py::exec(pythonCode.c_str(), m_global, m_global);
188 }
189 catch (py::error_already_set const &)
190 {
191 // Parse and output the exception
192 ASSERTL0(false,
193 "Failed to load Python code: " + parse_python_exception());
194 }
195
196 // Give the option as well to just create a filter that's defined via
197 // Python.
198 it = pParams.find("FilterName");
199 if (it != pParams.end())
200 {
201 std::string filterName = it->second;
202
203 // Check filter factory to make sure we have this class.
204 ASSERTL0(GetFilterFactory().ModuleExists(filterName),
205 "Unable to locate filter named '" + filterName + "'");
206
207 try
208 {
209 m_pyFilter = GetFilterFactory().CreateInstance(filterName, pSession,
210 pEquation, pParams);
211 }
212 catch (py::error_already_set const &)
213 {
214 // Parse and output the exception
215 ASSERTL0(false,
216 "Failed to load Python code: " + parse_python_exception());
217 }
218 }
219}
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
SOLVER_UTILS_EXPORT Filter(const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< EquationSystem > &pEquation)
std::shared_ptr< Filter > m_pyFilter
Definition: FilterPython.h:83
std::string parse_python_exception()
Temporarily stolen from boost examples.
FilterFactory & GetFilterFactory()

References ASSERTL0, Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::CreateInstance(), Nektar::SolverUtils::GetFilterFactory(), m_global, m_pyFilter, and Nektar::SolverUtils::parse_python_exception().

◆ ~FilterPython()

Nektar::SolverUtils::FilterPython::~FilterPython ( )
override

Definition at line 221 of file FilterPython.cpp.

222{
223}

Member Function Documentation

◆ create()

static FilterSharedPtr Nektar::SolverUtils::FilterPython::create ( const LibUtilities::SessionReaderSharedPtr pSession,
const std::shared_ptr< EquationSystem > &  pEquation,
const std::map< std::string, std::string > &  pParams 
)
inlinestatic

Creates an instance of this class.

Definition at line 49 of file FilterPython.h.

53 {
55 pSession, pEquation, pParams);
56 return p;
57 }
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
std::shared_ptr< Filter > FilterSharedPtr
A shared pointer to a Driver object.
Definition: Filter.h:51

References Nektar::MemoryManager< DataType >::AllocateSharedPtr(), and CellMLToNektar.cellml_metadata::p.

◆ v_Finalise()

void Nektar::SolverUtils::FilterPython::v_Finalise ( const Array< OneD, const MultiRegions::ExpListSharedPtr > &  pFields,
const NekDouble time 
)
overrideprotectedvirtual

Implements Nektar::SolverUtils::Filter.

Definition at line 269 of file FilterPython.cpp.

272{
273 try
274 {
275 if (m_pyFilter)
276 {
277 m_pyFilter->Finalise(pFields, time);
278 }
279 else
280 {
281 m_global["filter_finalise"](ArrayOneDToPyList(pFields), time);
282 }
283 }
284 catch (py::error_already_set const &)
285 {
286 std::cout << "Error in Python: " << parse_python_exception()
287 << std::endl;
288 }
289}
py::list ArrayOneDToPyList(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields)

References Nektar::SolverUtils::ArrayOneDToPyList(), m_global, m_pyFilter, and Nektar::SolverUtils::parse_python_exception().

◆ v_Initialise()

void Nektar::SolverUtils::FilterPython::v_Initialise ( const Array< OneD, const MultiRegions::ExpListSharedPtr > &  pFields,
const NekDouble time 
)
overrideprotectedvirtual

Implements Nektar::SolverUtils::Filter.

Definition at line 225 of file FilterPython.cpp.

228{
229 try
230 {
231 if (m_pyFilter)
232 {
233 m_pyFilter->Initialise(pFields, time);
234 }
235 else
236 {
237 m_global["filter_initialise"](ArrayOneDToPyList(pFields), time);
238 }
239 }
240 catch (py::error_already_set const &)
241 {
242 std::cout << "Error in Python: " << parse_python_exception()
243 << std::endl;
244 }
245}

References Nektar::SolverUtils::ArrayOneDToPyList(), m_global, m_pyFilter, and Nektar::SolverUtils::parse_python_exception().

◆ v_IsTimeDependent()

bool Nektar::SolverUtils::FilterPython::v_IsTimeDependent ( )
overrideprotectedvirtual

Implements Nektar::SolverUtils::Filter.

Definition at line 291 of file FilterPython.cpp.

292{
293 if (m_pyFilter)
294 {
295 return m_pyFilter->IsTimeDependent();
296 }
297
298 return true;
299}

References m_pyFilter.

◆ v_Update()

void Nektar::SolverUtils::FilterPython::v_Update ( const Array< OneD, const MultiRegions::ExpListSharedPtr > &  pFields,
const NekDouble time 
)
overrideprotectedvirtual

Implements Nektar::SolverUtils::Filter.

Definition at line 247 of file FilterPython.cpp.

250{
251 try
252 {
253 if (m_pyFilter)
254 {
255 m_pyFilter->Update(pFields, time);
256 }
257 else
258 {
259 m_global["filter_update"](ArrayOneDToPyList(pFields), time);
260 }
261 }
262 catch (py::error_already_set const &)
263 {
264 std::cout << "Error in Python: " << parse_python_exception()
265 << std::endl;
266 }
267}

References Nektar::SolverUtils::ArrayOneDToPyList(), m_global, m_pyFilter, and Nektar::SolverUtils::parse_python_exception().

Friends And Related Function Documentation

◆ MemoryManager< FilterPython >

friend class MemoryManager< FilterPython >
friend

Definition at line 1 of file FilterPython.h.

Member Data Documentation

◆ className

std::string Nektar::SolverUtils::FilterPython::className
static
Initial value:
=
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
static FilterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< EquationSystem > &pEquation, const std::map< std::string, std::string > &pParams)
Creates an instance of this class.
Definition: FilterPython.h:49

Name of the class.

Definition at line 60 of file FilterPython.h.

◆ m_fld

LibUtilities::FieldIOSharedPtr Nektar::SolverUtils::FilterPython::m_fld
private

Definition at line 81 of file FilterPython.h.

◆ m_global

py::object Nektar::SolverUtils::FilterPython::m_global
private

Definition at line 82 of file FilterPython.h.

Referenced by FilterPython(), v_Finalise(), v_Initialise(), and v_Update().

◆ m_pyFilter

std::shared_ptr<Filter> Nektar::SolverUtils::FilterPython::m_pyFilter = nullptr
private

Definition at line 83 of file FilterPython.h.

Referenced by FilterPython(), v_Finalise(), v_Initialise(), v_IsTimeDependent(), and v_Update().