40#include <boost/program_options.hpp>
42#ifdef NEKTAR_USING_VTK
54#pragma GCC visibility push(hidden)
55template <
typename MODTYPE>
56struct ModuleWrap :
public MODTYPE,
public py::trampoline_self_life_support
70 void v_Process([[maybe_unused]] po::variables_map &vm)
override
72 py::gil_scoped_acquire gil;
73 py::function
override = py::get_override(
this,
"Process");
75 if (this->
m_f->m_nParts > 1)
77 if (this->GetModulePriority() ==
eOutput)
79 this->
m_f->m_comm = this->
m_f->m_partComm;
80 if (this->GetModuleName() !=
"OutputInfo")
82 this->RegisterConfig(
"writemultiplefiles");
87 this->
m_f->m_comm = this->
m_f->m_partComm;
91 this->
m_f->m_comm = this->
m_f->m_defComm;
106 py::gil_scoped_acquire gil;
107 py::function
override = py::get_override(
this,
"GetModuleName");
111 return py::cast<std::string>(
override());
119 py::gil_scoped_acquire gil;
120 py::function
override = py::get_override(
this,
"GetModulePriority");
124 return py::cast<ModulePriority>(
override());
128 "GetModulePriority() is a pure virtual function");
143 this->m_config[key] = conf;
150#pragma GCC visibility pop
154 using MODTYPE::v_GetModuleName;
155 using MODTYPE::v_GetModulePriority;
156 using MODTYPE::v_Process;
163 if (m->m_f->m_nParts > 1)
165 if (m->GetModulePriority() ==
eOutput)
167 m->m_f->m_comm = m->m_f->m_partComm;
168 if (m->GetModuleName() !=
"OutputInfo")
170 m->RegisterConfig(
"writemultiplefiles");
175 m->m_f->m_comm = m->m_f->m_partComm;
179 m->m_f->m_comm = m->m_f->m_defComm;
183 m->Process(m->m_f->m_vm);
189 return mod->GetConfigOption(key).as<T>();
215#ifdef NEKTAR_USING_VTK
218 std::shared_ptr<OutputVtk> vtkModule =
219 std::dynamic_pointer_cast<OutputVtk>(mod);
225 throw NekError(
"This module is not the OutputVtk module, cannot get"
229 return vtkModule->GetVtkGrid();
235 throw NekError(
"Nektar++ has not been compiled with VTK support.");
247template <
typename MODTYPE>
256 throw NekError(
"ProcessModule.Create() requires two arguments: "
257 "module name and a Field object.");
262 "Module.Create() requires "
263 "two arguments: module name and a Field object; "
264 "optionally a filename.");
267 std::string modName = py::str(args[0]);
268 ModuleKey modKey = std::make_pair(modType, modName);
274 field = py::cast<FieldSharedPtr>(args[1]);
278 throw NekError(
"Second argument to Create() should be a Field object.");
293 for (
int i = 2; i < py::len(args); ++i)
295 std::string in_fname = py::str(args[i]);
296 mod->RegisterConfig(
"infile", in_fname);
297 mod->AddFile(modName, in_fname);
304 mod->RegisterConfig(
"outfile", py::str(args[2]));
308 for (
auto &kv : kwargs)
310 std::string arg = py::str(kv.first);
314 if (!py::isinstance<py::dict>(kv.second))
316 throw NekError(
"infile should be a dictionary.");
319 py::dict ftype_fname_dict = py::cast<py::dict>(kv.second);
321 for (
auto &kv2 : ftype_fname_dict)
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);
331 std::string val = py::str(kv.second);
332 mod->RegisterConfig(arg, val);
349 std::string
const &value)
351 mod->RegisterConfig(key, value);
354template <
typename MODTYPE>
356 std::string
const &key,
357 std::string
const &defValue,
358 std::string
const &desc,
bool isBool)
360 mod->AddConfigOption(key, defValue, desc, isBool);
367 py::classh<MODTYPE, Module, ModuleWrap<MODTYPE>>(m, modName.c_str())
368 .def(py::init<FieldSharedPtr>())
370 .def(
"AddConfigOption", &ModuleWrap_AddConfigOption<MODTYPE>,
371 py::arg(
"key"), py::arg(
"defValue"), py::arg(
"desc"),
372 py::arg(
"isBool") =
false)
380 .def_static(
"Create", &Module_Create<MODTYPE>);
396 py::classh<Module, ModuleWrap<Module>>(m,
"Module")
397 .def(py::init<FieldSharedPtr>())
405 py::arg(
"value") =
"")
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)
421 .def_static(
"Register", [](
ModuleType const &modType,
422 std::string
const &modName,
425 fac(key, obj, std::string(
ModuleTypeMap[modType]) +
"_" + modName);
Nektar::ErrorUtil::NekError NekError
#define NEKPY_WRAP_ENUM_STRING(MOD, ENUMNAME, MAPNAME)
T Module_GetConfig(std::shared_ptr< Module > mod, const std::string &key)
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)
ModuleSharedPtr Module_Create(py::args args, const py::kwargs &kwargs)
Lightweight wrapper for Module factory creation function.
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)
void Module_GetVtkGrid(std::shared_ptr< Module > mod)
FieldSharedPtr m_f
Field object.
FIELD_UTILS_EXPORT void PrintConfig()
Print out all configuration options for a module.
FIELD_UTILS_EXPORT void SetDefaults()
Sets default configuration options for those which have not been set.
Abstract base class for output modules.
Abstract base class for processing modules.
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
FieldSharedPtr m_f
Field object.
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(py::module &m, std::string modName)