| [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> | 
|---|
|  | 21 |  | 
|---|
| [628577] | 22 | #include "CodePatterns/toString.hpp" | 
|---|
|  | 23 |  | 
|---|
| [cc6e5c] | 24 | // all "getter" functions | 
|---|
|  | 25 | #include "modules.hpp" | 
|---|
|  | 26 |  | 
|---|
| [14de8e1] | 27 | //!> define all present actions | 
|---|
|  | 28 | #include "GlobalListOfActions.hpp" | 
|---|
|  | 29 |  | 
|---|
|  | 30 | //!> python wrapping for all of these actions | 
|---|
|  | 31 | #include "AllActionPython.hpp" | 
|---|
|  | 32 |  | 
|---|
|  | 33 | namespace MoleCuilder { | 
|---|
|  | 34 |  | 
|---|
|  | 35 | namespace PythonTypes { | 
|---|
|  | 36 |  | 
|---|
|  | 37 | inline void IndexError(){ | 
|---|
|  | 38 | PyErr_SetString(PyExc_IndexError, "Index out of range"); | 
|---|
|  | 39 | boost::python::throw_error_already_set(); | 
|---|
|  | 40 | } | 
|---|
|  | 41 |  | 
|---|
|  | 42 | template<class T> | 
|---|
|  | 43 | struct vec_item{ | 
|---|
|  | 44 | typedef typename T::value_type V; | 
|---|
|  | 45 | static V& get(T& x, int i){ | 
|---|
|  | 46 | static V nothing; | 
|---|
|  | 47 | if(i < 0) i += x.size(); | 
|---|
|  | 48 | if(i >= 0 && i < int(x.size())) return x[i]; | 
|---|
|  | 49 | IndexError(); | 
|---|
|  | 50 | return nothing; | 
|---|
|  | 51 | } | 
|---|
|  | 52 | static void set(T& x, int i, V const& v){ | 
|---|
|  | 53 | if(i < 0) i += x.size(); | 
|---|
|  | 54 | if(i >= 0 && i < int(x.size())) x[i] = v; | 
|---|
|  | 55 | else IndexError(); | 
|---|
|  | 56 | } | 
|---|
|  | 57 | static void del(T& x, int i){ | 
|---|
|  | 58 | if(i < 0) i += x.size(); | 
|---|
|  | 59 | if(i >= 0 && i < int(x.size())) x.erase(x.begin() + i); | 
|---|
|  | 60 | else IndexError(); | 
|---|
|  | 61 | } | 
|---|
|  | 62 | static void add(T& x, V const& v){ | 
|---|
|  | 63 | x.push_back(v); | 
|---|
|  | 64 | } | 
|---|
|  | 65 | }; | 
|---|
|  | 66 |  | 
|---|
|  | 67 |  | 
|---|
|  | 68 | } /* namespace PythonTypes */ | 
|---|
| [cc6e5c] | 69 |  | 
|---|
| [14de8e1] | 70 | } /* namespace MoleCuilder */ | 
|---|
|  | 71 |  | 
|---|
| [cc6e5c] | 72 |  | 
|---|
| [14de8e1] | 73 | BOOST_PYTHON_MODULE(pyMoleCuilder) | 
|---|
|  | 74 | { | 
|---|
| [cc6e5c] | 75 | // from this moment on, we need to be sure to deeinitialize in the correct or | 
|---|
|  | 76 | // this is handled by the cleanup function | 
|---|
|  | 77 | atexit(MoleCuilder::detail::module_exit); | 
|---|
|  | 78 |  | 
|---|
| [14de8e1] | 79 | // set the docstring of the current module scope | 
|---|
|  | 80 | 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."; | 
|---|
|  | 81 |  | 
|---|
| [cc6e5c] | 82 | boost::python::def( | 
|---|
|  | 83 | "reinit", | 
|---|
|  | 84 | MoleCuilder::detail::module_reinit, | 
|---|
|  | 85 | "Reinitializes the internal state of the python module as if it had been freshly imported,saves all input files beforehand." | 
|---|
|  | 86 | ); | 
|---|
| [415ddd] | 87 | boost::python::def( | 
|---|
|  | 88 | "wait", | 
|---|
|  | 89 | MoleCuilder::detail::module_wait, | 
|---|
|  | 90 | "Waits until all currently queued actions have been processed." | 
|---|
|  | 91 | ); | 
|---|
| [cc6e5c] | 92 | boost::python::def< MoleCuilder::detail::doubleVec() >( | 
|---|
|  | 93 | "getBoundingBox", | 
|---|
|  | 94 | MoleCuilder::detail::module_getBoundingBox, | 
|---|
|  | 95 | "returns the cuboid bounding box of the current domain." | 
|---|
|  | 96 | ); | 
|---|
|  | 97 | boost::python::def< double() >( | 
|---|
|  | 98 | "getDomainVolume", | 
|---|
|  | 99 | MoleCuilder::detail::module_getDomainVolume, | 
|---|
|  | 100 | "returns the volume of the simulation domain." | 
|---|
|  | 101 | ); | 
|---|
| [ea30e6] | 102 | boost::python::def< MoleCuilder::detail::elementVec() >( | 
|---|
|  | 103 | "getSelectedAtomElements", | 
|---|
|  | 104 | MoleCuilder::detail::module_getSelectedAtomElements, | 
|---|
|  | 105 | "returns the element numbers of all currently selected atoms." | 
|---|
|  | 106 | ); | 
|---|
| [e70818] | 107 | boost::python::def< MoleCuilder::detail::atomPositionsVec() >( | 
|---|
|  | 108 | "getSelectedAtomPositions", | 
|---|
|  | 109 | MoleCuilder::detail::module_getSelectedAtomPositions, | 
|---|
|  | 110 | "returns the positions of all currently selected atoms." | 
|---|
|  | 111 | ); | 
|---|
| [ea30e6] | 112 | boost::python::def< MoleCuilder::detail::atomIdVec() >( | 
|---|
|  | 113 | "getSelectedAtomIds", | 
|---|
|  | 114 | MoleCuilder::detail::module_getSelectedAtomIds, | 
|---|
|  | 115 | "returns the ids of all currently selected atoms." | 
|---|
|  | 116 | ); | 
|---|
| [cc6e5c] | 117 | boost::python::def< double() >( | 
|---|
|  | 118 | "getSelectedMolarMass", | 
|---|
|  | 119 | MoleCuilder::detail::module_getSelectedMolarMass, | 
|---|
|  | 120 | "returns the molar mass of all selected atoms." | 
|---|
|  | 121 | ); | 
|---|
|  | 122 |  | 
|---|
| [14de8e1] | 123 | // STL Vectors: | 
|---|
| [ea30e6] | 124 | // unsignedIntVec | 
|---|
|  | 125 | boost::python::class_< std::vector< atomId_t > >("PythonType_unsignedIntVec") | 
|---|
|  | 126 | .def("__len__", &std::vector< unsigned int >::size) | 
|---|
|  | 127 | .def("clear", &std::vector< unsigned int >::clear) | 
|---|
|  | 128 | .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::add, | 
|---|
|  | 129 | boost::python::with_custodian_and_ward<1, 2>()) // let container keep value | 
|---|
|  | 130 | .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::get, | 
|---|
|  | 131 | boost::python::return_value_policy<boost::python::copy_non_const_reference>()) | 
|---|
|  | 132 | .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::set, | 
|---|
|  | 133 | boost::python::with_custodian_and_ward<1,2>()) // to let container keep value | 
|---|
|  | 134 | .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::del) | 
|---|
|  | 135 | .def("__iter__", boost::python::iterator< std::vector< unsigned int > >()) | 
|---|
|  | 136 | ; | 
|---|
| [14de8e1] | 137 | // doubleVec | 
|---|
|  | 138 | boost::python::class_< std::vector< double > >("PythonType_doubleVec") | 
|---|
|  | 139 | .def("__len__", &std::vector< double >::size) | 
|---|
|  | 140 | .def("clear", &std::vector< double >::clear) | 
|---|
|  | 141 | .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::add, | 
|---|
|  | 142 | boost::python::with_custodian_and_ward<1, 2>()) // let container keep value | 
|---|
|  | 143 | .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::get, | 
|---|
|  | 144 | boost::python::return_value_policy<boost::python::copy_non_const_reference>()) | 
|---|
|  | 145 | .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::set, | 
|---|
|  | 146 | boost::python::with_custodian_and_ward<1,2>()) // to let container keep value | 
|---|
|  | 147 | .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::del) | 
|---|
|  | 148 | .def("__iter__", boost::python::iterator< std::vector< double > >()) | 
|---|
|  | 149 | ; | 
|---|
| [e70818] | 150 | // positions | 
|---|
|  | 151 | boost::python::class_< std::vector< std::vector< double > > >("PythonType_positions") | 
|---|
|  | 152 | .def("__len__", &std::vector< std::vector< double > >::size) | 
|---|
|  | 153 | .def("clear", &std::vector< std::vector< double > >::clear) | 
|---|
|  | 154 | .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::add, | 
|---|
|  | 155 | boost::python::with_custodian_and_ward<1, 2>()) // let container keep value | 
|---|
|  | 156 | .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::get, | 
|---|
|  | 157 | boost::python::return_value_policy<boost::python::copy_non_const_reference>()) | 
|---|
|  | 158 | .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::set, | 
|---|
|  | 159 | boost::python::with_custodian_and_ward<1,2>()) // to let container keep value | 
|---|
|  | 160 | .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::del) | 
|---|
|  | 161 | .def("__iter__", boost::python::iterator< std::vector< std::vector< double > > >()) | 
|---|
|  | 162 | ; | 
|---|
| [14de8e1] | 163 |  | 
|---|
| [cc6e5c] | 164 | // access to all Actions | 
|---|
| [14de8e1] | 165 | #define export_print(z,n,list) \ | 
|---|
|  | 166 | BOOST_PP_CAT(export_, BOOST_PP_SEQ_ELEM(n, list))(); | 
|---|
| [975b83] | 167 | #define BOOST_PP_LOCAL_MACRO(n) export_print(~, n, GLOBALLISTOFPYTHONACTIONS) | 
|---|
|  | 168 | #define BOOST_PP_LOCAL_LIMITS  (0, BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(GLOBALLISTOFPYTHONACTIONS))) | 
|---|
| [14de8e1] | 169 | #include BOOST_PP_LOCAL_ITERATE() | 
|---|
|  | 170 | #undef instance_print | 
|---|
|  | 171 | } | 
|---|
|  | 172 |  | 
|---|
|  | 173 |  | 
|---|
| [06993e] | 174 | #endif /* PYTHONSCRIPTING_IMPL_HPP_ */ | 
|---|