Page MenuHomec4science

lm_python_bindings.hh
No OneTemporary

File Metadata

Created
Mon, Jul 8, 11:12

lm_python_bindings.hh

#include "action_interface.hh"
#include "compute_list.hh"
#include "domain_multiscale.hh"
#include "dumper_list.hh"
#include "factory_multiscale.hh"
#include "filter_interface.hh"
#include "filter_list.hh"
#include "geometry_list.hh"
#include "geometry_manager.hh"
#include "lib_continuum.hh"
#include "lib_dd.hh"
#include "lib_dumper.hh"
#include "lib_filter.hh"
#include "lib_geometry.hh"
#include "lib_md.hh"
#include "lib_stimulation.hh"
#include "lm_common.hh"
#include "lm_communicator.hh"
#include "lm_type_caster.hh"
#include "stimulation_interface.hh"
#include "stimulation_list.hh"
/* -------------------------------------------------------------------------- */
#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
__BEGIN_LIBMULTISCALE__
template <typename Class>
inline void declare_domain(py::module &m, const char *class_name) {
py::class_<Class, DomainInterface, std::shared_ptr<Class>>(
m, class_name, py::multiple_inheritance())
.def(py::init<LMID, CommGroup &>())
.def("init", [](Class &self) {
self.checkAllKeywordsAreParsed();
self.init();
});
}
template <typename Class>
inline void declare_geometry(py::module &m, const char *class_name) {
py::class_<Class, Geometry, std::shared_ptr<Class>>(
m, class_name, py::multiple_inheritance())
.def(py::init<UInt, LMID>())
.def("init", [](Class &self) {
self.checkAllKeywordsAreParsed();
self.init();
});
}
template <typename Class>
inline void declare_stimulation(py::module &m, const char *class_name) {
py::class_<Class, StimulationInterface, std::shared_ptr<Class>>(
m, class_name, py::multiple_inheritance())
.def(py::init<LMID>())
.def("init", [](Class &self) {
self.checkAllKeywordsAreParsed();
self.init();
});
}
template <typename Class>
inline void declare_compute(py::module &m, const char *class_name) {
py::class_<Class, ComputeInterface, std::shared_ptr<Class>>(
m, class_name, py::multiple_inheritance())
.def(py::init<LMID>())
.def("init", [](Class &self) {
self.checkAllKeywordsAreParsed();
self.init();
});
}
template <typename Class>
inline void declare_filter(py::module &m, const char *class_name) {
py::class_<Class, FilterInterface, std::shared_ptr<Class>>(
m, class_name, py::multiple_inheritance())
.def(py::init<LMID>())
.def("init", [](Class &self) {
self.checkAllKeywordsAreParsed();
self.init();
});
}
template <typename Class>
inline void declare_dumper(py::module &m, const char *class_name) {
py::class_<Class, DumperInterface, std::shared_ptr<Class>>(
m, class_name, py::multiple_inheritance())
.def(py::init<LMID>())
.def("init", [](Class &self) {
self.checkAllKeywordsAreParsed();
self.init();
});
}
inline std::string clean_class_name(const std::string &arg) {
std::string res = arg;
res.erase(std::remove(res.begin(), res.end(), '<'), res.end());
res.erase(std::remove(res.begin(), res.end(), '>'), res.end());
return res;
}
inline void makeBindings(py::module &m) {
py::enum_<IntegrationSchemeStage>(m, "IntegrationSchemeStage")
.value("NONE_STEP", NONE_STEP)
.value("PRE_DUMP", PRE_DUMP)
.value("PRE_STEP1", PRE_STEP1)
.value("PRE_STEP2", PRE_STEP2)
.value("PRE_STEP3", PRE_STEP3)
.value("PRE_STEP4", PRE_STEP4)
.value("PRE_STEP5", PRE_STEP5)
.value("PRE_STEP6", PRE_STEP6)
.value("PRE_STEP7", PRE_STEP7)
.value("PRE_FATAL", PRE_FATAL)
.value("ALL_STEP", ALL_STEP)
.export_values();
py::enum_<CouplingStage>(m, "CouplingStage")
.value("COUPLING_STEP1", COUPLING_STEP1)
.value("COUPLING_STEP2", COUPLING_STEP2)
.value("COUPLING_STEP3", COUPLING_STEP3)
.value("COUPLING_STEP4", COUPLING_STEP4)
.value("COUPLING_STEP5", COUPLING_STEP5)
.value("COUPLING_STEP6", COUPLING_STEP6)
.value("COUPLING_STEP7", COUPLING_STEP7)
.export_values();
py::enum_<DOFType>(m, "DOFType")
.value("dt_local_master", dt_local_master)
.value("dt_local_slave", dt_local_slave)
.value("dt_local", dt_local)
.value("dt_ghost", dt_ghost)
.value("dt_all", dt_all)
.export_values();
py::enum_<FieldType>(m, "FieldType")
.value("position0", _position0)
.value("position", _position)
.value("displacement", _displacement)
.value("velocity", _velocity)
.value("force", _force)
.value("stress", _stress)
.value("temperature", _temperature)
.value("grouprank", _grouprank)
.value("strain", _strain)
.value("epot", _epot)
.value("applied_force", _applied_force)
.value("mass", _mass)
.value("tag", _tag)
.value("id", _id)
.value("proc", _proc)
.value("charge", _charge)
.value("burgers", _burgers)
.value("normal", _normal)
.export_values();
py::class_<DomainMultiScale>(m, "DomainMultiScale")
.def_static("getManager", &DomainMultiScale::getManager,
py::return_value_policy::reference)
.def("build", &DomainMultiScale::build)
.def("getObject", &DomainMultiScale::getObject,
py::return_value_policy::reference)
//.def("getTimeStep", &DomainMultiScale::getTimeStep)
// .def("performStep1", &DomainMultiScale::performStep1)
// .def("performStep2", &DomainMultiScale::performStep2)
// .def("performStep3", &DomainMultiScale::performStep3)
.def("coupling", &DomainMultiScale::coupling)
.def("destroy", &DomainMultiScale::destroy)
.def("addObject", py::overload_cast<std::shared_ptr<DomainInterface>>(
&DomainMultiScale::addObject));
py::class_<ActionManager>(m, "ActionManager")
.def_static("getManager", py::overload_cast<>(&ActionManager::getManager),
py::return_value_policy::reference)
.def_static("getManager",
py::overload_cast<UInt>(&ActionManager::getManager),
py::return_value_policy::reference)
.def("action", &ActionManager::action)
.def("destroy", &ActionManager::destroy)
.def("addObject", py::overload_cast<std::shared_ptr<ActionInterface>>(
&ActionManager::addObject));
py::class_<FilterManager>(m, "FilterManager")
.def_static("getManager", py::overload_cast<>(&FilterManager::getManager),
py::return_value_policy::reference)
.def_static("getManager",
py::overload_cast<UInt>(&FilterManager::getManager),
py::return_value_policy::reference)
.def("action", &FilterManager::action)
.def("destroy", &FilterManager::destroy)
.def("addObject", py::overload_cast<std::shared_ptr<FilterInterface>>(
&FilterManager::addObject));
py::class_<CommGroup>(m, "CommGroup")
.def(py::init<const LMID &, int, UInt>());
py::class_<Communicator>(m, "Communicator")
.def("getCommunicator", &Communicator::getCommunicator,
py::return_value_policy::reference)
.def("getNumberFreeProcs", &Communicator::getNumberFreeProcs)
.def("addGroup", &Communicator::addGroup)
.def("getGroup", &Communicator::getGroup);
m.attr("prologue") = py::module::import("prologue");
py::class_<GeometryManager>(m, "GeometryManager")
.def_static("getManager", &GeometryManager::getManager,
py::return_value_policy::reference)
.def("addGeometry", &GeometryManager::addGeometry)
.def("destroy", &GeometryManager::destroy);
py::class_<Parsable, std::shared_ptr<Parsable>>(m, "Parsable")
.def("getPossibleKeywords", &Parsable::getPossibleKeywords)
.def("getParam", &Parsable::getParam<py::object>)
.def("setParam",
[](Parsable &self, const std::string &keyword, py::object &val) {
self.setParam<py::object>(keyword, val);
})
.def_property_readonly("params", [m](Parsable &self) {
return m.attr("prologue").attr("LMParameters")(self);
});
py::class_<DomainInterface, Parsable, std::shared_ptr<DomainInterface>>(
m, "DomainInterface", py::multiple_inheritance())
// .def("performStep1", &DomainInterface::performStep1)
// .def("performStep2", &DomainInterface::performStep2)
// .def("performStep3", &DomainInterface::performStep3)
// .def("performStep4", &DomainInterface::performStep4)
// .def("performStep5", &DomainInterface::performStep5)
;
py::class_<Geometry, Parsable, std::shared_ptr<Geometry>>(
m, "Geometry", py::multiple_inheritance());
py::class_<ActionInterface, Parsable, std::shared_ptr<ActionInterface>>
action_interface(m, "ActionInterface", py::multiple_inheritance());
action_interface.def("action", &ActionInterface::action)
.def("getID", [](ActionInterface &self) { return self.getID(); })
.def("getOutputs",
[](ActionInterface &self) {
auto &outputs = self.getOutputs();
py::dict ret;
for (auto &&kv : outputs) {
ret.attr("__setitem__")
.
operator()<py::return_value_policy::reference>(kv.first,
kv.second);
}
return ret;
})
.def("getInputs",
[](ActionInterface &self) {
auto &inputs = self.getInputs();
py::dict ret;
for (auto &&kv : inputs) {
ret.attr("__setitem__")
.
operator()<py::return_value_policy::reference>(kv.first,
kv.second);
}
return ret;
})
.def("createOutput", &ActionInterface::createOutput)
.def("getOutput", &ActionInterface::getOutput, py::arg("name") = "",
py::return_value_policy::reference)
.def_property_readonly("inputs", [m](ActionInterface &self) {
return m.attr("prologue").attr("LMInputs")(self);
});
#define PYTHON_DOMAIN(n, data, obj) \
action_interface.def("setInput", \
[m](ActionInterface &self, const std::string &name, \
BOOST_PP_TUPLE_ELEM(3, 0, obj) & input) { \
try { \
self.connect(name, input.getOutput()); \
} catch (...) { \
self.connect(name, make_argument(input)); \
} \
});
BOOST_PP_SEQ_FOR_EACH(PYTHON_DOMAIN, f, LIST_ATOM_MODEL);
BOOST_PP_SEQ_FOR_EACH(PYTHON_DOMAIN, f, LIST_DD_MODEL);
BOOST_PP_SEQ_FOR_EACH(PYTHON_DOMAIN, f, LIST_CONTINUUM_MODEL);
#undef PYTHON_DOMAIN
#define PYTHON_COMPUTE(n, data, obj) \
action_interface.def("setInput", \
[m](ActionInterface &self, const std::string &name, \
BOOST_PP_TUPLE_ELEM(3, 0, obj) & input) { \
try { \
self.connect(name, input.getOutput()); \
} catch (...) { \
self.connect(name, make_argument(input)); \
} \
});
BOOST_PP_SEQ_FOR_EACH(PYTHON_COMPUTE, f, LIST_COMPUTE);
BOOST_PP_SEQ_FOR_EACH(PYTHON_COMPUTE, f, LIST_FILTER);
#undef PYTHON_COMPUTE
py::class_<StimulationInterface, ActionInterface,
std::shared_ptr<StimulationInterface>>(m, "StimulationInterface",
py::multiple_inheritance())
.def("stimulate", &StimulationInterface::stimulate);
py::class_<FilterInterface, ActionInterface,
std::shared_ptr<FilterInterface>>(m, "FilterInterface",
py::multiple_inheritance())
.def("build", &FilterInterface::build);
py::class_<DumperInterface, ActionInterface,
std::shared_ptr<DumperInterface>>(m, "DumperInterface",
py::multiple_inheritance())
.def("dump", &DumperInterface::dump);
py::class_<ComputeInterface, FilterInterface,
std::shared_ptr<ComputeInterface>>(m, "ComputeInterface",
py::multiple_inheritance())
.def("compute", &ComputeInterface::compute)
.def("getArray", &ComputeInterface::getArray, py::arg("name") = "",
py::return_value_policy::reference)
.def("getCastedOutput",
[](ComputeInterface &self, std::string name, bool eval) {
return self.getCastedOutput<ContainerArray<Real>>(name, eval);
},
py::arg("name"), py::arg("eval") = true);
py::class_<ContainerArray<Real>>(m, "ContainerArrayReal",
py::multiple_inheritance())
//.def_readwrite("name_computed", &ContainerArray<Real>::name_computed)
.def("getDim", &ContainerArray<Real>::getDim)
.def("size", &ContainerArray<Real>::size)
.def("to_array", &ContainerArray<Real>::to_array);
py::class_<ArgumentContainer, std::shared_ptr<ArgumentContainer>>(
m, "ArgumentContainer")
.def("make_argument", [](ArgumentContainer &arg,
ComputeInterface &c) { arg = make_argument(c); })
.def("make_argument", [](ArgumentContainer &arg, py::array c) {
auto cont = std::make_shared<ContainerArray<Real>>("temp");
cont->setCommGroup(Communicator::getCommunicator().getGroup("all"));
*cont = py::cast<ContainerArray<Real>::EigenArray>(c);
arg = make_argument(std::move(cont));
});
#define PYTHON_DOMAIN(n, data, obj) \
{ \
using _class = BOOST_PP_TUPLE_ELEM(3, 0, obj); \
std::string domain_name = \
clean_class_name(BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(3, 0, obj))); \
declare_domain<_class>(m, domain_name.c_str()); \
}
BOOST_PP_SEQ_FOR_EACH(PYTHON_DOMAIN, f, LIST_ATOM_MODEL);
BOOST_PP_SEQ_FOR_EACH(PYTHON_DOMAIN, f, LIST_DD_MODEL);
BOOST_PP_SEQ_FOR_EACH(PYTHON_DOMAIN, f, LIST_CONTINUUM_MODEL);
#undef PYTHON_DOMAIN
#define PYTHON_GEOMETRY(n, data, obj) \
{ \
using _class = BOOST_PP_TUPLE_ELEM(2, 0, obj); \
std::string geometry_name = \
clean_class_name(BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(2, 0, obj))); \
\
declare_geometry<_class>(m, geometry_name.c_str()); \
}
BOOST_PP_SEQ_FOR_EACH(PYTHON_GEOMETRY, f, LIST_GEOMETRY);
#undef PYTHON_GEOMETRY
#define PYTHON_STIMULATION(n, data, obj) \
{ \
using _class = BOOST_PP_TUPLE_ELEM(2, 0, obj); \
std::string stimulation_name = \
clean_class_name(BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(2, 0, obj))); \
\
declare_stimulation<_class>(m, stimulation_name.c_str()); \
}
BOOST_PP_SEQ_FOR_EACH(PYTHON_STIMULATION, f, LIST_STIMULATION);
#undef PYTHON_STIMULATION
#define PYTHON_COMPUTE(n, data, obj) \
{ \
using _class = BOOST_PP_TUPLE_ELEM(2, 0, obj); \
std::string compute_name = \
clean_class_name(BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(2, 0, obj))); \
\
declare_compute<_class>(m, compute_name.c_str()); \
}
BOOST_PP_SEQ_FOR_EACH(PYTHON_COMPUTE, f, LIST_COMPUTE);
#undef PYTHON_COMPUTE
#define PYTHON_FILTER(n, data, obj) \
{ \
using _class = BOOST_PP_TUPLE_ELEM(2, 0, obj); \
std::string filter_name = \
clean_class_name(BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(2, 0, obj))); \
\
declare_filter<_class>(m, filter_name.c_str()); \
}
BOOST_PP_SEQ_FOR_EACH(PYTHON_FILTER, f, LIST_FILTER);
#undef PYTHON_FILTER
#define PYTHON_DUMPER(n, data, obj) \
{ \
using _class = BOOST_PP_TUPLE_ELEM(2, 0, obj); \
std::string dumper_name = \
clean_class_name(BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(2, 0, obj))); \
\
declare_dumper<_class>(m, dumper_name.c_str()); \
}
BOOST_PP_SEQ_FOR_EACH(PYTHON_DUMPER, f, LIST_DUMPER);
#undef PYTHON_DUMPER
declare_compute<ComputeCompatibility>(m, "ComputeCompatibility");
}
__END_LIBMULTISCALE__

Event Timeline