Nektar++
Classes | Functions
Python/Module.cpp File Reference
#include <FieldUtils/Module.h>
#include <LibUtilities/Python/BasicUtils/NekFactory.hpp>
#include <LibUtilities/Python/BasicUtils/SharedArray.hpp>
#include <LibUtilities/Python/NekPyConfig.hpp>
#include <boost/program_options.hpp>

Go to the source code of this file.

Classes

struct  ModuleWrap< MODTYPE >
 Module wrapper to handle virtual function calls in Module and its subclasses as defined by the template parameter. More...
 
struct  ModulePublic< MODTYPE >
 
struct  ModuleTypeProxy< MODTYPE >
 
struct  ModuleTypeProxy< InputModule >
 
struct  ModuleTypeProxy< ProcessModule >
 
struct  ModuleTypeProxy< OutputModule >
 
struct  PythonModuleClass< MODTYPE >
 

Functions

void Module_Process (ModuleSharedPtr m)
 
template<typename T >
Module_GetConfig (std::shared_ptr< Module > mod, const std::string &key)
 
void Module_GetVtkGrid (std::shared_ptr< Module > mod)
 
template<typename MODTYPE >
ModuleSharedPtr Module_Create (py::args args, const py::kwargs &kwargs)
 Lightweight wrapper for Module factory creation function. More...
 
void Module_RegisterConfig (std::shared_ptr< Module > mod, std::string const &key, std::string const &value)
 Lightweight wrapper for FieldUtils::Module::RegisterConfig. More...
 
template<typename MODTYPE >
void ModuleWrap_AddConfigOption (std::shared_ptr< ModuleWrap< MODTYPE > > mod, std::string const &key, std::string const &defValue, std::string const &desc, bool isBool)
 
void export_Module (py::module &m)
 

Function Documentation

◆ export_Module()

void export_Module ( py::module &  m)

Definition at line 384 of file Python/Module.cpp.

385{
387
388 // Export ModuleType enum.
390
391 // Wrapper for the Module class. Note that since Module contains a pure
392 // virtual function, we need the ModuleWrap helper class to handle this for
393 // us. In the lightweight wrappers above, we therefore need to ensure we're
394 // passing std::shared_ptr<Module> as the first argument, otherwise they
395 // won't accept objects constructed from Python.
396 py::classh<Module, ModuleWrap<Module>>(m, "Module")
397 .def(py::init<FieldSharedPtr>())
398
399 // Process function for this module.
400 .def("Process", &Module_Process)
401 .def("Run", &Module_Process)
402
403 // Configuration options.
404 .def("RegisterConfig", Module_RegisterConfig, py::arg("key"),
405 py::arg("value") = "")
406 .def("PrintConfig", &Module::PrintConfig)
407 .def("SetDefaults", &Module::SetDefaults)
408 .def("GetStringConfig", Module_GetConfig<std::string>)
409 .def("GetFloatConfig", Module_GetConfig<double>)
410 .def("GetIntConfig", Module_GetConfig<int>)
411 .def("GetBoolConfig", Module_GetConfig<bool>)
412 .def("AddConfigOption", &ModuleWrap_AddConfigOption<Module>,
413 py::arg("key"), py::arg("defValue"), py::arg("desc"),
414 py::arg("isBool") = false)
415 .def("GetVtkGrid", &Module_GetVtkGrid)
416
417 // Allow direct access to field object through a property.
418 .def_readwrite("field", &ModuleWrap<Module>::m_f)
419
420 // Factory functions.
421 .def_static("Register", [](ModuleType const &modType,
422 std::string const &modName,
423 py::object &obj) {
424 ModuleKey key(modType, modName);
425 fac(key, obj, std::string(ModuleTypeMap[modType]) + "_" + modName);
426 });
427
428 PythonModuleClass<InputModule>(m, "InputModule");
429 PythonModuleClass<ProcessModule>(m, "ProcessModule");
430 PythonModuleClass<OutputModule>(m, "OutputModule");
431}
#define NEKPY_WRAP_ENUM_STRING(MOD, ENUMNAME, MAPNAME)
Definition: NekPyConfig.hpp:62
void Module_RegisterConfig(std::shared_ptr< Module > mod, std::string const &key, std::string const &value)
Lightweight wrapper for FieldUtils::Module::RegisterConfig.
void Module_Process(ModuleSharedPtr m)
void Module_GetVtkGrid(std::shared_ptr< Module > mod)
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:180
const std::string ModuleTypeMap[]
Definition: Module.h:72
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:47
Module wrapper to handle virtual function calls in Module and its subclasses as defined by the templa...

References Nektar::FieldUtils::GetModuleFactory(), Module_GetVtkGrid(), Module_Process(), Module_RegisterConfig(), Nektar::FieldUtils::ModuleTypeMap, and NEKPY_WRAP_ENUM_STRING.

Referenced by PYBIND11_MODULE().

◆ Module_Create()

template<typename MODTYPE >
ModuleSharedPtr Module_Create ( py::args  args,
const py::kwargs &  kwargs 
)

Lightweight wrapper for Module factory creation function.

Parameters
modTypeModule type (input/process/output).
modNameModule name (typically filename extension).
fieldField that will be passed between modules.
Template Parameters
MODTYPESubclass of Module (e.g #InputModule, #OutputModule)

Definition at line 248 of file Python/Module.cpp.

249{
251
253
254 if (modType == eProcessModule && py::len(args) != 2)
255 {
256 throw NekError("ProcessModule.Create() requires two arguments: "
257 "module name and a Field object.");
258 }
259 else if (modType != eProcessModule && py::len(args) < 2)
260 {
261 throw NekError(ModuleTypeMap[modType] +
262 "Module.Create() requires "
263 "two arguments: module name and a Field object; "
264 "optionally a filename.");
265 }
266
267 std::string modName = py::str(args[0]);
268 ModuleKey modKey = std::make_pair(modType, modName);
269
271
272 try
273 {
274 field = py::cast<FieldSharedPtr>(args[1]);
275 }
276 catch (...)
277 {
278 throw NekError("Second argument to Create() should be a Field object.");
279 }
280
281 if (!GetModuleFactory().ModuleExists(modKey))
282 {
283 throw ErrorUtil::NekError("Module '" + modName + "' does not exist.");
284 }
285
287
288 if (modType == eInputModule)
289 {
290 // For input modules we can try to interpret the remaining arguments as
291 // input files. Assume that the file's type is identical to the module
292 // name.
293 for (int i = 2; i < py::len(args); ++i)
294 {
295 std::string in_fname = py::str(args[i]);
296 mod->RegisterConfig("infile", in_fname);
297 mod->AddFile(modName, in_fname);
298 }
299 }
300 else if (modType == eOutputModule && py::len(args) >= 3)
301 {
302 // For output modules we can try to interpret the remaining argument as
303 // an output file.
304 mod->RegisterConfig("outfile", py::str(args[2]));
305 }
306
307 // Process keyword arguments.
308 for (auto &kv : kwargs)
309 {
310 std::string arg = py::str(kv.first);
311
312 if (arg == "infile" && modKey.first == eInputModule)
313 {
314 if (!py::isinstance<py::dict>(kv.second))
315 {
316 throw NekError("infile should be a dictionary.");
317 }
318
319 py::dict ftype_fname_dict = py::cast<py::dict>(kv.second);
320
321 for (auto &kv2 : ftype_fname_dict)
322 {
323 std::string f_type = py::str(kv2.first);
324 std::string f_name = py::str(kv2.second);
325 mod->RegisterConfig(arg, f_name);
326 mod->AddFile(f_type, f_name);
327 }
328 }
329 else
330 {
331 std::string val = py::str(kv.second);
332 mod->RegisterConfig(arg, val);
333 }
334 }
335
336 mod->SetDefaults();
337
338 return mod;
339}
Nektar::ErrorUtil::NekError NekError
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:1026
std::shared_ptr< Module > ModuleSharedPtr
Definition: Module.h:329

References Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::CreateInstance(), Nektar::FieldUtils::eInputModule, Nektar::FieldUtils::eOutputModule, Nektar::FieldUtils::eProcessModule, FilterPython_Function::field, Nektar::FieldUtils::GetModuleFactory(), and Nektar::FieldUtils::ModuleTypeMap.

◆ Module_GetConfig()

template<typename T >
T Module_GetConfig ( std::shared_ptr< Module mod,
const std::string &  key 
)

Definition at line 187 of file Python/Module.cpp.

188{
189 return mod->GetConfigOption(key).as<T>();
190}

◆ Module_GetVtkGrid()

void Module_GetVtkGrid ( std::shared_ptr< Module mod)

Definition at line 232 of file Python/Module.cpp.

233{
235 throw NekError("Nektar++ has not been compiled with VTK support.");
236}

Referenced by export_Module().

◆ Module_Process()

void Module_Process ( ModuleSharedPtr  m)

Definition at line 161 of file Python/Module.cpp.

162{
163 if (m->m_f->m_nParts > 1)
164 {
165 if (m->GetModulePriority() == eOutput)
166 {
167 m->m_f->m_comm = m->m_f->m_partComm;
168 if (m->GetModuleName() != "OutputInfo")
169 {
170 m->RegisterConfig("writemultiplefiles");
171 }
172 }
173 else if (m->GetModulePriority() == eCreateGraph)
174 {
175 m->m_f->m_comm = m->m_f->m_partComm;
176 }
177 else
178 {
179 m->m_f->m_comm = m->m_f->m_defComm;
180 }
181 }
182 m->SetDefaults();
183 m->Process(m->m_f->m_vm);
184}

References Nektar::FieldUtils::eCreateGraph, and Nektar::FieldUtils::eOutput.

Referenced by export_Module().

◆ Module_RegisterConfig()

void Module_RegisterConfig ( std::shared_ptr< Module mod,
std::string const &  key,
std::string const &  value 
)

Lightweight wrapper for FieldUtils::Module::RegisterConfig.

Parameters
modModule to call
keyConfiguration key.
valueOptional value (some configuration options are boolean).

Definition at line 348 of file Python/Module.cpp.

350{
351 mod->RegisterConfig(key, value);
352}

Referenced by export_Module().

◆ ModuleWrap_AddConfigOption()

template<typename MODTYPE >
void ModuleWrap_AddConfigOption ( std::shared_ptr< ModuleWrap< MODTYPE > >  mod,
std::string const &  key,
std::string const &  defValue,
std::string const &  desc,
bool  isBool 
)

Definition at line 355 of file Python/Module.cpp.

359{
360 mod->AddConfigOption(key, defValue, desc, isBool);
361}