37#include <boost/program_options.hpp>
38#include <boost/python/raw_function.hpp>
40#ifdef NEKTAR_USING_VTK
52template <
class MODTYPE>
53struct ModuleWrap :
public MODTYPE,
public py::wrapper<MODTYPE>
67 void v_Process([[maybe_unused]] po::variables_map &vm)
override
69 this->get_override(
"Process")();
74 py::override f = this->get_override(
"GetModuleName");
75 return py::call<std::string>(f.ptr());
80 py::override f = this->get_override(
"GetModulePriority");
81 return py::call<ModulePriority>(f.ptr());
96 this->m_config[key] = conf;
108 if (m->m_f->m_nParts > 1)
110 if (m->GetModulePriority() ==
eOutput)
112 m->m_f->m_comm = m->m_f->m_partComm;
113 if (m->GetModuleName() !=
"OutputInfo")
115 m->RegisterConfig(
"writemultiplefiles");
120 m->m_f->m_comm = m->m_f->m_partComm;
124 m->m_f->m_comm = m->m_f->m_defComm;
128 m->Process(m->m_f->m_vm);
134 return mod->GetConfigOption(key).as<T>();
160#ifdef NEKTAR_USING_VTK
163 std::shared_ptr<OutputVtk> vtkModule =
164 std::dynamic_pointer_cast<OutputVtk>(mod);
170 throw NekError(
"This module is not the OutputVtk module, cannot get"
174 return vtkModule->GetVtkGrid();
180 throw NekError(
"Nektar++ has not been compiled with VTK support.");
192template <
typename MODTYPE>
201 throw NekError(
"ProcessModule.Create() requires two arguments: "
202 "module name and a Field object.");
207 "Module.Create() requires "
208 "two arguments: module name and a Field object; "
209 "optionally a filename.");
212 std::string modName = py::extract<std::string>(args[0]);
213 ModuleKey modKey = std::make_pair(modType, modName);
215 if (!py::extract<FieldSharedPtr>(args[1]).check())
217 throw NekError(
"Second argument to Create() should be a Field object.");
228 for (
int i = 2; i < py::len(args); ++i)
230 std::string in_fname = py::extract<std::string>(args[i]);
231 mod->RegisterConfig(
"infile", in_fname);
232 mod->AddFile(modName, in_fname);
239 mod->RegisterConfig(
"outfile", py::extract<std::string>(args[2]));
243 py::list items = kwargs.items();
245 for (
int i = 0; i < py::len(items); ++i)
247 std::string arg = py::extract<std::string>(items[i][0]);
251 py::extract<py::dict> dict_check(items[i][1]);
253 if (!dict_check.check())
255 throw NekError(
"infile should be a dictionary.");
258 py::dict ftype_fname_dict = py::extract<py::dict>(items[i][1]);
259 py::list ft_fn_items = ftype_fname_dict.items();
260 for (
int i = 0; i < py::len(ft_fn_items); ++i)
263 py::extract<std::string>(ft_fn_items[i][0]);
264 std::string f_name = py::extract<std::string>(
265 ft_fn_items[i][1].attr(
"__str__")());
266 mod->RegisterConfig(arg, f_name);
267 mod->AddFile(f_type, f_name);
273 py::extract<std::string>(items[i][1].attr(
"__str__")());
274 mod->RegisterConfig(arg, val);
291 std::string
const &value)
293 mod->RegisterConfig(key, value);
296template <
typename MODTYPE>
298 std::string
const &key,
299 std::string
const &defValue,
300 std::string
const &desc,
bool isBool)
302 mod->AddConfigOption(key, defValue, desc, isBool);
310 py::incref(obj.ptr());
315 py::decref(
m_obj.ptr());
321 return py::extract<ModuleSharedPtr>(inst);
328#if PY_MAJOR_VERSION == 2
376 helper, std::placeholders::_1));
384#if PY_MAJOR_VERSION == 2
393 py::import(
"__main__").attr(modkey.c_str()) = capsule;
404 py::objects::class_value_wrapper<
405 std::shared_ptr<MODTYPE>,
406 py::objects::make_ptr_instance<
407 MODTYPE, py::objects::pointer_holder<std::shared_ptr<MODTYPE>,
416 py::class_<ModuleWrap<MODTYPE>, std::shared_ptr<ModuleWrap<MODTYPE>>,
417 py::bases<Module>, boost::noncopyable>(
418 modName.c_str(), py::init<FieldSharedPtr>())
420 .def(
"AddConfigOption", ModuleWrap_AddConfigOption<MODTYPE>,
421 (py::arg(
"key"), py::arg(
"defValue"), py::arg(
"desc"),
422 py::arg(
"isBool") =
false))
430 .def(
"Create", py::raw_function(Module_Create<MODTYPE>))
431 .staticmethod(
"Create");
442#ifdef NEKTAR_USING_VTK
448 py::implicitly_convertible<std::shared_ptr<ModuleWrap<Module>>,
449 std::shared_ptr<Module>>();
450 py::implicitly_convertible<std::shared_ptr<ModuleWrap<InputModule>>,
451 std::shared_ptr<Module>>();
452 py::implicitly_convertible<std::shared_ptr<ModuleWrap<OutputModule>>,
453 std::shared_ptr<Module>>();
454 py::implicitly_convertible<std::shared_ptr<ModuleWrap<ProcessModule>>,
455 std::shared_ptr<Module>>();
462 py::class_<ModuleWrap<Module>, std::shared_ptr<ModuleWrap<Module>>,
463 boost::noncopyable>(
"Module", py::init<FieldSharedPtr>())
471 (py::arg(
"key"), py::arg(
"value") =
""))
472 .def(
"PrintConfig", &Module::PrintConfig)
473 .def(
"SetDefaults", &Module::SetDefaults)
474 .def(
"GetStringConfig", Module_GetConfig<std::string>)
475 .def(
"GetFloatConfig", Module_GetConfig<double>)
476 .def(
"GetIntConfig", Module_GetConfig<int>)
477 .def(
"GetBoolConfig", Module_GetConfig<bool>)
478 .def(
"AddConfigOption", ModuleWrap_AddConfigOption<Module>,
479 (py::arg(
"key"), py::arg(
"defValue"), py::arg(
"desc"),
480 py::arg(
"isBool") =
false))
482 py::return_value_policy<py::return_by_value>())
489 .staticmethod(
"Register");
Nektar::ErrorUtil::NekError NekError
#define NEKPY_WRAP_ENUM_STRING(ENUMNAME, MAPNAME)
void ModuleCapsuleDestructor(PyObject *ptr)
T Module_GetConfig(std::shared_ptr< Module > mod, const std::string &key)
ModuleSharedPtr Module_Create(py::tuple args, py::dict kwargs)
Lightweight wrapper for Module factory creation function.
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 ModuleWrap_AddConfigOption(std::shared_ptr< ModuleWrap< MODTYPE > > mod, std::string const &key, std::string const &defValue, std::string const &desc, bool isBool)
void Module_Register(ModuleType const &modType, std::string const &modName, py::object &obj)
Lightweight wrapper for the Module factory RegisterCreatorFunction, to support the ability for Python...
void Module_GetVtkGrid(std::shared_ptr< Module > mod)
#define VTK_PYTHON_CONVERSION(type)
ModuleRegisterHelper(py::object obj)
ModuleSharedPtr create(FieldSharedPtr field)
Abstract base class for output modules.
Abstract base class for processing modules.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
std::shared_ptr< Field > FieldSharedPtr
std::pair< ModuleType, std::string > ModuleKey
const std::string ModuleTypeMap[]
std::shared_ptr< Module > ModuleSharedPtr
ModuleFactory & GetModuleFactory()
Module wrapper to handle virtual function calls in Module and its subclasses as defined by the templa...
void AddConfigOption(std::string key, std::string def, std::string desc, bool isBool)
Defines a configuration option for this module.
std::string v_GetModuleName() override
ModulePriority v_GetModulePriority() override
void v_Process(po::variables_map &vm) override
Concrete implementation of the Module::Process function.
ModuleWrap(FieldSharedPtr field)
Constructor, which is identical to FieldUtils::Module.
Represents a command-line configuration option.
PythonModuleClass(std::string modName)