| [14de8e1] | 1 | /*
 | 
|---|
 | 2 |  * PythonScripting.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Apr 13, 2012
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [06993e] | 8 | #ifndef PYTHONSCRIPTING_IMPL_HPP_
 | 
|---|
 | 9 | #define PYTHONSCRIPTING_IMPL_HPP_
 | 
|---|
| [14de8e1] | 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>
 | 
|---|
| [7b1824] | 21 | #include <boost/python/numpy.hpp>
 | 
|---|
| [14de8e1] | 22 | 
 | 
|---|
| [628577] | 23 | #include "CodePatterns/toString.hpp"
 | 
|---|
 | 24 | 
 | 
|---|
| [cc6e5c] | 25 | // all "getter" functions
 | 
|---|
 | 26 | #include "modules.hpp"
 | 
|---|
 | 27 | 
 | 
|---|
| [14de8e1] | 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 |     }
 | 
|---|
| [7df863] | 66 |     static std::string toStringRepr(T& x){
 | 
|---|
 | 67 |         return toString(x);
 | 
|---|
 | 68 |     }
 | 
|---|
| [14de8e1] | 69 | };
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | } /* namespace PythonTypes */
 | 
|---|
| [cc6e5c] | 73 | 
 | 
|---|
| [14de8e1] | 74 | } /* namespace MoleCuilder */
 | 
|---|
 | 75 | 
 | 
|---|
| [7b1824] | 76 | void export_numpy();
 | 
|---|
| [cc6e5c] | 77 | 
 | 
|---|
| [14de8e1] | 78 | BOOST_PYTHON_MODULE(pyMoleCuilder)
 | 
|---|
 | 79 | {
 | 
|---|
| [cc6e5c] | 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 | 
 | 
|---|
| [7b1824] | 84 |   // initialize numpy C-API
 | 
|---|
 | 85 |   boost::python::numpy::initialize();
 | 
|---|
 | 86 | 
 | 
|---|
| [14de8e1] | 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 | 
 | 
|---|
| [7b1824] | 90 |   export_numpy();
 | 
|---|
 | 91 | 
 | 
|---|
| [cc6e5c] | 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 |   );
 | 
|---|
| [ecc050] | 97 |   boost::python::def< bool() >(
 | 
|---|
| [415ddd] | 98 |       "wait",
 | 
|---|
 | 99 |       MoleCuilder::detail::module_wait,
 | 
|---|
 | 100 |       "Waits until all currently queued actions have been processed."
 | 
|---|
 | 101 |   );
 | 
|---|
| [5061d9] | 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 |   );
 | 
|---|
| [cc6e5c] | 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 |   );
 | 
|---|
| [ea30e6] | 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 |   );
 | 
|---|
| [e70818] | 127 |   boost::python::def< MoleCuilder::detail::atomPositionsVec() >(
 | 
|---|
 | 128 |       "getSelectedAtomPositions",
 | 
|---|
 | 129 |       MoleCuilder::detail::module_getSelectedAtomPositions,
 | 
|---|
 | 130 |       "returns the positions of all currently selected atoms."
 | 
|---|
 | 131 |   );
 | 
|---|
| [ea30e6] | 132 |   boost::python::def< MoleCuilder::detail::atomIdVec() >(
 | 
|---|
 | 133 |       "getSelectedAtomIds",
 | 
|---|
 | 134 |       MoleCuilder::detail::module_getSelectedAtomIds,
 | 
|---|
 | 135 |       "returns the ids of all currently selected atoms."
 | 
|---|
 | 136 |   );
 | 
|---|
| [cc6e5c] | 137 |   boost::python::def< double() >(
 | 
|---|
 | 138 |       "getSelectedMolarMass",
 | 
|---|
 | 139 |       MoleCuilder::detail::module_getSelectedMolarMass,
 | 
|---|
 | 140 |       "returns the molar mass of all selected atoms."
 | 
|---|
 | 141 |   );
 | 
|---|
 | 142 | 
 | 
|---|
| [14de8e1] | 143 |   // STL Vectors:
 | 
|---|
| [ea30e6] | 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 > >())
 | 
|---|
| [7df863] | 156 |       .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::toStringRepr)
 | 
|---|
| [ea30e6] | 157 |   ;
 | 
|---|
| [14de8e1] | 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 > >())
 | 
|---|
| [7df863] | 170 |       .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::toStringRepr)
 | 
|---|
| [14de8e1] | 171 |   ;
 | 
|---|
| [e70818] | 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 > > >())
 | 
|---|
| [7df863] | 184 |       .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::toStringRepr)
 | 
|---|
| [e70818] | 185 |   ;
 | 
|---|
| [14de8e1] | 186 | 
 | 
|---|
| [cc6e5c] | 187 |   // access to all Actions
 | 
|---|
| [14de8e1] | 188 | #define export_print(z,n,list) \
 | 
|---|
 | 189 |   BOOST_PP_CAT(export_, BOOST_PP_SEQ_ELEM(n, list))();
 | 
|---|
| [975b83] | 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)))
 | 
|---|
| [14de8e1] | 192 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
 | 193 | #undef instance_print
 | 
|---|
 | 194 | }
 | 
|---|
 | 195 | 
 | 
|---|
 | 196 | 
 | 
|---|
| [06993e] | 197 | #endif /* PYTHONSCRIPTING_IMPL_HPP_ */
 | 
|---|