| 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::doubleVec() >( | 
|---|
| 113 | "getBoundingBox", | 
|---|
| 114 | MoleCuilder::detail::module_getBoundingBox, | 
|---|
| 115 | "returns the cuboid bounding box of the current domain." | 
|---|
| 116 | ); | 
|---|
| 117 | boost::python::def< double() >( | 
|---|
| 118 | "getDomainVolume", | 
|---|
| 119 | MoleCuilder::detail::module_getDomainVolume, | 
|---|
| 120 | "returns the volume of the simulation domain." | 
|---|
| 121 | ); | 
|---|
| 122 | boost::python::def< MoleCuilder::detail::elementVec() >( | 
|---|
| 123 | "getSelectedAtomElements", | 
|---|
| 124 | MoleCuilder::detail::module_getSelectedAtomElements, | 
|---|
| 125 | "returns the element numbers of all currently selected atoms." | 
|---|
| 126 | ); | 
|---|
| 127 | boost::python::def< MoleCuilder::detail::atomPositionsVec() >( | 
|---|
| 128 | "getSelectedAtomPositions", | 
|---|
| 129 | MoleCuilder::detail::module_getSelectedAtomPositions, | 
|---|
| 130 | "returns the positions of all currently selected atoms." | 
|---|
| 131 | ); | 
|---|
| 132 | boost::python::def< MoleCuilder::detail::atomIdVec() >( | 
|---|
| 133 | "getSelectedAtomIds", | 
|---|
| 134 | MoleCuilder::detail::module_getSelectedAtomIds, | 
|---|
| 135 | "returns the ids of all currently selected atoms." | 
|---|
| 136 | ); | 
|---|
| 137 | boost::python::def< double() >( | 
|---|
| 138 | "getSelectedMolarMass", | 
|---|
| 139 | MoleCuilder::detail::module_getSelectedMolarMass, | 
|---|
| 140 | "returns the molar mass of all selected atoms." | 
|---|
| 141 | ); | 
|---|
| 142 |  | 
|---|
| 143 | // STL Vectors: | 
|---|
| 144 | // unsignedIntVec | 
|---|
| 145 | boost::python::class_< std::vector< atomId_t > >("PythonType_unsignedIntVec") | 
|---|
| 146 | .def("__len__", &std::vector< unsigned int >::size) | 
|---|
| 147 | .def("clear", &std::vector< unsigned int >::clear) | 
|---|
| 148 | .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::add, | 
|---|
| 149 | boost::python::with_custodian_and_ward<1, 2>()) // let container keep value | 
|---|
| 150 | .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::get, | 
|---|
| 151 | boost::python::return_value_policy<boost::python::copy_non_const_reference>()) | 
|---|
| 152 | .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::set, | 
|---|
| 153 | boost::python::with_custodian_and_ward<1,2>()) // to let container keep value | 
|---|
| 154 | .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::del) | 
|---|
| 155 | .def("__iter__", boost::python::iterator< std::vector< unsigned int > >()) | 
|---|
| 156 | .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::toStringRepr) | 
|---|
| 157 | ; | 
|---|
| 158 | // doubleVec | 
|---|
| 159 | boost::python::class_< std::vector< double > >("PythonType_doubleVec") | 
|---|
| 160 | .def("__len__", &std::vector< double >::size) | 
|---|
| 161 | .def("clear", &std::vector< double >::clear) | 
|---|
| 162 | .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::add, | 
|---|
| 163 | boost::python::with_custodian_and_ward<1, 2>()) // let container keep value | 
|---|
| 164 | .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::get, | 
|---|
| 165 | boost::python::return_value_policy<boost::python::copy_non_const_reference>()) | 
|---|
| 166 | .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::set, | 
|---|
| 167 | boost::python::with_custodian_and_ward<1,2>()) // to let container keep value | 
|---|
| 168 | .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::del) | 
|---|
| 169 | .def("__iter__", boost::python::iterator< std::vector< double > >()) | 
|---|
| 170 | .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::toStringRepr) | 
|---|
| 171 | ; | 
|---|
| 172 | // positions | 
|---|
| 173 | boost::python::class_< std::vector< std::vector< double > > >("PythonType_positions") | 
|---|
| 174 | .def("__len__", &std::vector< std::vector< double > >::size) | 
|---|
| 175 | .def("clear", &std::vector< std::vector< double > >::clear) | 
|---|
| 176 | .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::add, | 
|---|
| 177 | boost::python::with_custodian_and_ward<1, 2>()) // let container keep value | 
|---|
| 178 | .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::get, | 
|---|
| 179 | boost::python::return_value_policy<boost::python::copy_non_const_reference>()) | 
|---|
| 180 | .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::set, | 
|---|
| 181 | boost::python::with_custodian_and_ward<1,2>()) // to let container keep value | 
|---|
| 182 | .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::del) | 
|---|
| 183 | .def("__iter__", boost::python::iterator< std::vector< std::vector< double > > >()) | 
|---|
| 184 | .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::toStringRepr) | 
|---|
| 185 | ; | 
|---|
| 186 |  | 
|---|
| 187 | // access to all Actions | 
|---|
| 188 | #define export_print(z,n,list) \ | 
|---|
| 189 | BOOST_PP_CAT(export_, BOOST_PP_SEQ_ELEM(n, list))(); | 
|---|
| 190 | #define BOOST_PP_LOCAL_MACRO(n) export_print(~, n, GLOBALLISTOFPYTHONACTIONS) | 
|---|
| 191 | #define BOOST_PP_LOCAL_LIMITS  (0, BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(GLOBALLISTOFPYTHONACTIONS))) | 
|---|
| 192 | #include BOOST_PP_LOCAL_ITERATE() | 
|---|
| 193 | #undef instance_print | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 |  | 
|---|
| 197 | #endif /* PYTHONSCRIPTING_IMPL_HPP_ */ | 
|---|