| 1 | /*
|
|---|
| 2 | * PythonScripting.hpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Apr 13, 2012
|
|---|
| 5 | * Author: heber
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #ifndef PYTHONSCRIPTING_IMPL_HPP_
|
|---|
| 9 | #define PYTHONSCRIPTING_IMPL_HPP_
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 | // include config.h
|
|---|
| 13 | #ifdef HAVE_CONFIG_H
|
|---|
| 14 | #include <config.h>
|
|---|
| 15 | #endif
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | #include <boost/python.hpp>
|
|---|
| 19 | #include <boost/python/module.hpp>
|
|---|
| 20 | #include <boost/python/args.hpp>
|
|---|
| 21 | #include <boost/python/numpy.hpp>
|
|---|
| 22 |
|
|---|
| 23 | #include "CodePatterns/toString.hpp"
|
|---|
| 24 |
|
|---|
| 25 | // all "getter" functions
|
|---|
| 26 | #include "modules.hpp"
|
|---|
| 27 |
|
|---|
| 28 | //!> define all present actions
|
|---|
| 29 | #include "GlobalListOfActions.hpp"
|
|---|
| 30 |
|
|---|
| 31 | //!> python wrapping for all of these actions
|
|---|
| 32 | #include "AllActionPython.hpp"
|
|---|
| 33 |
|
|---|
| 34 | namespace MoleCuilder {
|
|---|
| 35 |
|
|---|
| 36 | namespace PythonTypes {
|
|---|
| 37 |
|
|---|
| 38 | inline void IndexError(){
|
|---|
| 39 | PyErr_SetString(PyExc_IndexError, "Index out of range");
|
|---|
| 40 | boost::python::throw_error_already_set();
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | template<class T>
|
|---|
| 44 | struct vec_item{
|
|---|
| 45 | typedef typename T::value_type V;
|
|---|
| 46 | static V& get(T& x, int i){
|
|---|
| 47 | static V nothing;
|
|---|
| 48 | if(i < 0) i += x.size();
|
|---|
| 49 | if(i >= 0 && i < int(x.size())) return x[i];
|
|---|
| 50 | IndexError();
|
|---|
| 51 | return nothing;
|
|---|
| 52 | }
|
|---|
| 53 | static void set(T& x, int i, V const& v){
|
|---|
| 54 | if(i < 0) i += x.size();
|
|---|
| 55 | if(i >= 0 && i < int(x.size())) x[i] = v;
|
|---|
| 56 | else IndexError();
|
|---|
| 57 | }
|
|---|
| 58 | static void del(T& x, int i){
|
|---|
| 59 | if(i < 0) i += x.size();
|
|---|
| 60 | if(i >= 0 && i < int(x.size())) x.erase(x.begin() + i);
|
|---|
| 61 | else IndexError();
|
|---|
| 62 | }
|
|---|
| 63 | static void add(T& x, V const& v){
|
|---|
| 64 | x.push_back(v);
|
|---|
| 65 | }
|
|---|
| 66 | static std::string toStringRepr(T& x){
|
|---|
| 67 | return toString(x);
|
|---|
| 68 | }
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | } /* namespace PythonTypes */
|
|---|
| 73 |
|
|---|
| 74 | } /* namespace MoleCuilder */
|
|---|
| 75 |
|
|---|
| 76 | void export_numpy();
|
|---|
| 77 |
|
|---|
| 78 | BOOST_PYTHON_MODULE(pyMoleCuilder)
|
|---|
| 79 | {
|
|---|
| 80 | // from this moment on, we need to be sure to deeinitialize in the correct or
|
|---|
| 81 | // this is handled by the cleanup function
|
|---|
| 82 | atexit(MoleCuilder::detail::module_exit);
|
|---|
| 83 |
|
|---|
| 84 | // initialize numpy C-API
|
|---|
| 85 | boost::python::numpy::initialize();
|
|---|
| 86 |
|
|---|
| 87 | // set the docstring of the current module scope
|
|---|
| 88 | boost::python::scope().attr("__doc__") = "pyMolecuilder are the python bindings to all Actions of the program suite MoleCuilder.\n\nMoleCuilder is a program to build molecular (dynamics) worlds, allowing you indefinite manipulation, control and analysis over the atoms and molecules within a simulation domain.";
|
|---|
| 89 |
|
|---|
| 90 | export_numpy();
|
|---|
| 91 |
|
|---|
| 92 | boost::python::def(
|
|---|
| 93 | "reinit",
|
|---|
| 94 | MoleCuilder::detail::module_reinit,
|
|---|
| 95 | "Reinitializes the internal state of the python module as if it had been freshly imported,saves all input files beforehand."
|
|---|
| 96 | );
|
|---|
| 97 | boost::python::def< bool() >(
|
|---|
| 98 | "wait",
|
|---|
| 99 | MoleCuilder::detail::module_wait,
|
|---|
| 100 | "Waits until all currently queued actions have been processed."
|
|---|
| 101 | );
|
|---|
| 102 | boost::python::def< std::string() >(
|
|---|
| 103 | "getGraph6String",
|
|---|
| 104 | MoleCuilder::detail::module_getGraph6String,
|
|---|
| 105 | "returns chemical graph of the current selected set of atoms as graph6 representation."
|
|---|
| 106 | );
|
|---|
| 107 | boost::python::def< std::string() >(
|
|---|
| 108 | "getElementListAsString",
|
|---|
| 109 | MoleCuilder::detail::module_getElementListAsString,
|
|---|
| 110 | "returns list of elements over the nodes of the graph of the current selected set of atoms."
|
|---|
| 111 | );
|
|---|
| 112 | boost::python::def< MoleCuilder::detail::stringVec() >(
|
|---|
| 113 | "getAllGraph6Strings",
|
|---|
| 114 | MoleCuilder::detail::module_getAllGraph6Strings,
|
|---|
| 115 | "returns chemical graphs of the current selected set of atoms as graph6 representation with all non-hydrogen atom permutations."
|
|---|
| 116 | );
|
|---|
| 117 | boost::python::def< MoleCuilder::detail::stringVec() >(
|
|---|
| 118 | "getAllElementListAsStrings",
|
|---|
| 119 | MoleCuilder::detail::module_getAllElementListAsStrings,
|
|---|
| 120 | "returns lists of elements over the nodes of the graph of the current selected set of atoms with all non-hydrogen atom permutations."
|
|---|
| 121 | );
|
|---|
| 122 | boost::python::def< MoleCuilder::detail::doubleVec() >(
|
|---|
| 123 | "getBoundingBox",
|
|---|
| 124 | MoleCuilder::detail::module_getBoundingBox,
|
|---|
| 125 | "returns the cuboid bounding box of the current domain."
|
|---|
| 126 | );
|
|---|
| 127 | boost::python::def< double() >(
|
|---|
| 128 | "getDomainVolume",
|
|---|
| 129 | MoleCuilder::detail::module_getDomainVolume,
|
|---|
| 130 | "returns the volume of the simulation domain."
|
|---|
| 131 | );
|
|---|
| 132 | boost::python::def< MoleCuilder::detail::elementVec() >(
|
|---|
| 133 | "getSelectedAtomElements",
|
|---|
| 134 | MoleCuilder::detail::module_getSelectedAtomElements,
|
|---|
| 135 | "returns the element numbers of all currently selected atoms."
|
|---|
| 136 | );
|
|---|
| 137 | boost::python::def< MoleCuilder::detail::atomPositionsVec() >(
|
|---|
| 138 | "getSelectedAtomPositions",
|
|---|
| 139 | MoleCuilder::detail::module_getSelectedAtomPositions,
|
|---|
| 140 | "returns the positions of all currently selected atoms."
|
|---|
| 141 | );
|
|---|
| 142 | boost::python::def< MoleCuilder::detail::atomIdVec() >(
|
|---|
| 143 | "getSelectedAtomIds",
|
|---|
| 144 | MoleCuilder::detail::module_getSelectedAtomIds,
|
|---|
| 145 | "returns the ids of all currently selected atoms."
|
|---|
| 146 | );
|
|---|
| 147 | boost::python::def< double() >(
|
|---|
| 148 | "getSelectedMolarMass",
|
|---|
| 149 | MoleCuilder::detail::module_getSelectedMolarMass,
|
|---|
| 150 | "returns the molar mass of all selected atoms."
|
|---|
| 151 | );
|
|---|
| 152 |
|
|---|
| 153 | // STL Vectors:
|
|---|
| 154 | // unsignedIntVec
|
|---|
| 155 | boost::python::class_< std::vector< atomId_t > >("PythonType_unsignedIntVec")
|
|---|
| 156 | .def("__len__", &std::vector< unsigned int >::size)
|
|---|
| 157 | .def("clear", &std::vector< unsigned int >::clear)
|
|---|
| 158 | .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::add,
|
|---|
| 159 | boost::python::with_custodian_and_ward<1, 2>()) // let container keep value
|
|---|
| 160 | .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::get,
|
|---|
| 161 | boost::python::return_value_policy<boost::python::copy_non_const_reference>())
|
|---|
| 162 | .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::set,
|
|---|
| 163 | boost::python::with_custodian_and_ward<1,2>()) // to let container keep value
|
|---|
| 164 | .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::del)
|
|---|
| 165 | .def("__iter__", boost::python::iterator< std::vector< unsigned int > >())
|
|---|
| 166 | .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::toStringRepr)
|
|---|
| 167 | ;
|
|---|
| 168 | // doubleVec
|
|---|
| 169 | boost::python::class_< std::vector< double > >("PythonType_doubleVec")
|
|---|
| 170 | .def("__len__", &std::vector< double >::size)
|
|---|
| 171 | .def("clear", &std::vector< double >::clear)
|
|---|
| 172 | .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::add,
|
|---|
| 173 | boost::python::with_custodian_and_ward<1, 2>()) // let container keep value
|
|---|
| 174 | .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::get,
|
|---|
| 175 | boost::python::return_value_policy<boost::python::copy_non_const_reference>())
|
|---|
| 176 | .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::set,
|
|---|
| 177 | boost::python::with_custodian_and_ward<1,2>()) // to let container keep value
|
|---|
| 178 | .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::del)
|
|---|
| 179 | .def("__iter__", boost::python::iterator< std::vector< double > >())
|
|---|
| 180 | .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::toStringRepr)
|
|---|
| 181 | ;
|
|---|
| 182 | // positions
|
|---|
| 183 | boost::python::class_< std::vector< std::vector< double > > >("PythonType_positions")
|
|---|
| 184 | .def("__len__", &std::vector< std::vector< double > >::size)
|
|---|
| 185 | .def("clear", &std::vector< std::vector< double > >::clear)
|
|---|
| 186 | .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::add,
|
|---|
| 187 | boost::python::with_custodian_and_ward<1, 2>()) // let container keep value
|
|---|
| 188 | .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::get,
|
|---|
| 189 | boost::python::return_value_policy<boost::python::copy_non_const_reference>())
|
|---|
| 190 | .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::set,
|
|---|
| 191 | boost::python::with_custodian_and_ward<1,2>()) // to let container keep value
|
|---|
| 192 | .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::del)
|
|---|
| 193 | .def("__iter__", boost::python::iterator< std::vector< std::vector< double > > >())
|
|---|
| 194 | .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::toStringRepr)
|
|---|
| 195 | ;
|
|---|
| 196 | // positions
|
|---|
| 197 | boost::python::class_< std::vector< std::string > >("PythonType_stringVec")
|
|---|
| 198 | .def("__len__", &std::vector< std::string >::size)
|
|---|
| 199 | .def("clear", &std::vector< std::string >::clear)
|
|---|
| 200 | .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< std::string > >::add,
|
|---|
| 201 | boost::python::with_custodian_and_ward<1, 2>()) // let container keep value
|
|---|
| 202 | .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::string > >::get,
|
|---|
| 203 | boost::python::return_value_policy<boost::python::copy_non_const_reference>())
|
|---|
| 204 | .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::string > >::set,
|
|---|
| 205 | boost::python::with_custodian_and_ward<1,2>()) // to let container keep value
|
|---|
| 206 | .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::string > >::del)
|
|---|
| 207 | .def("__iter__", boost::python::iterator< std::vector< std::string > >())
|
|---|
| 208 | .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::string > >::toStringRepr)
|
|---|
| 209 | ;
|
|---|
| 210 |
|
|---|
| 211 | // access to all Actions
|
|---|
| 212 | #define export_print(z,n,list) \
|
|---|
| 213 | BOOST_PP_CAT(export_, BOOST_PP_SEQ_ELEM(n, list))();
|
|---|
| 214 | #define BOOST_PP_LOCAL_MACRO(n) export_print(~, n, GLOBALLISTOFPYTHONACTIONS)
|
|---|
| 215 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(GLOBALLISTOFPYTHONACTIONS)))
|
|---|
| 216 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 217 | #undef instance_print
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 | #endif /* PYTHONSCRIPTING_IMPL_HPP_ */
|
|---|