source: src/Python/PythonScripting_impl.hpp@ 6b7765

Candidate_v1.7.0 stable
Last change on this file since 6b7765 was 7df863, checked in by Frederik Heber <frederik.heber@…>, 5 years ago

Added repr to all boost python vector instances.

  • Property mode set to 100644
File size: 7.6 KB
Line 
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
22#include "CodePatterns/toString.hpp"
23
24// all "getter" functions
25#include "modules.hpp"
26
27//!> define all present actions
28#include "GlobalListOfActions.hpp"
29
30//!> python wrapping for all of these actions
31#include "AllActionPython.hpp"
32
33namespace MoleCuilder {
34
35namespace PythonTypes {
36
37inline void IndexError(){
38 PyErr_SetString(PyExc_IndexError, "Index out of range");
39 boost::python::throw_error_already_set();
40}
41
42template<class T>
43struct 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 static std::string toStringRepr(T& x){
66 return toString(x);
67 }
68};
69
70
71} /* namespace PythonTypes */
72
73} /* namespace MoleCuilder */
74
75
76BOOST_PYTHON_MODULE(pyMoleCuilder)
77{
78 // from this moment on, we need to be sure to deeinitialize in the correct or
79 // this is handled by the cleanup function
80 atexit(MoleCuilder::detail::module_exit);
81
82 // set the docstring of the current module scope
83 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.";
84
85 boost::python::def(
86 "reinit",
87 MoleCuilder::detail::module_reinit,
88 "Reinitializes the internal state of the python module as if it had been freshly imported,saves all input files beforehand."
89 );
90 boost::python::def(
91 "wait",
92 MoleCuilder::detail::module_wait,
93 "Waits until all currently queued actions have been processed."
94 );
95 boost::python::def< std::string() >(
96 "getGraph6String",
97 MoleCuilder::detail::module_getGraph6String,
98 "returns chemical graph of the current selected set of atoms as graph6 representation."
99 );
100 boost::python::def< std::string() >(
101 "getElementListAsString",
102 MoleCuilder::detail::module_getElementListAsString,
103 "returns list of elements over the nodes of the graph of the current selected set of atoms."
104 );
105 boost::python::def< MoleCuilder::detail::doubleVec() >(
106 "getBoundingBox",
107 MoleCuilder::detail::module_getBoundingBox,
108 "returns the cuboid bounding box of the current domain."
109 );
110 boost::python::def< double() >(
111 "getDomainVolume",
112 MoleCuilder::detail::module_getDomainVolume,
113 "returns the volume of the simulation domain."
114 );
115 boost::python::def< MoleCuilder::detail::elementVec() >(
116 "getSelectedAtomElements",
117 MoleCuilder::detail::module_getSelectedAtomElements,
118 "returns the element numbers of all currently selected atoms."
119 );
120 boost::python::def< MoleCuilder::detail::atomPositionsVec() >(
121 "getSelectedAtomPositions",
122 MoleCuilder::detail::module_getSelectedAtomPositions,
123 "returns the positions of all currently selected atoms."
124 );
125 boost::python::def< MoleCuilder::detail::atomIdVec() >(
126 "getSelectedAtomIds",
127 MoleCuilder::detail::module_getSelectedAtomIds,
128 "returns the ids of all currently selected atoms."
129 );
130 boost::python::def< double() >(
131 "getSelectedMolarMass",
132 MoleCuilder::detail::module_getSelectedMolarMass,
133 "returns the molar mass of all selected atoms."
134 );
135
136 // STL Vectors:
137 // unsignedIntVec
138 boost::python::class_< std::vector< atomId_t > >("PythonType_unsignedIntVec")
139 .def("__len__", &std::vector< unsigned int >::size)
140 .def("clear", &std::vector< unsigned int >::clear)
141 .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::add,
142 boost::python::with_custodian_and_ward<1, 2>()) // let container keep value
143 .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::get,
144 boost::python::return_value_policy<boost::python::copy_non_const_reference>())
145 .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::set,
146 boost::python::with_custodian_and_ward<1,2>()) // to let container keep value
147 .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::del)
148 .def("__iter__", boost::python::iterator< std::vector< unsigned int > >())
149 .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< unsigned int > >::toStringRepr)
150 ;
151 // doubleVec
152 boost::python::class_< std::vector< double > >("PythonType_doubleVec")
153 .def("__len__", &std::vector< double >::size)
154 .def("clear", &std::vector< double >::clear)
155 .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::add,
156 boost::python::with_custodian_and_ward<1, 2>()) // let container keep value
157 .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::get,
158 boost::python::return_value_policy<boost::python::copy_non_const_reference>())
159 .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::set,
160 boost::python::with_custodian_and_ward<1,2>()) // to let container keep value
161 .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::del)
162 .def("__iter__", boost::python::iterator< std::vector< double > >())
163 .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::toStringRepr)
164 ;
165 // positions
166 boost::python::class_< std::vector< std::vector< double > > >("PythonType_positions")
167 .def("__len__", &std::vector< std::vector< double > >::size)
168 .def("clear", &std::vector< std::vector< double > >::clear)
169 .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::add,
170 boost::python::with_custodian_and_ward<1, 2>()) // let container keep value
171 .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::get,
172 boost::python::return_value_policy<boost::python::copy_non_const_reference>())
173 .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::set,
174 boost::python::with_custodian_and_ward<1,2>()) // to let container keep value
175 .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::del)
176 .def("__iter__", boost::python::iterator< std::vector< std::vector< double > > >())
177 .def("__repr__", &MoleCuilder::PythonTypes::vec_item< std::vector< std::vector< double > > >::toStringRepr)
178 ;
179
180 // access to all Actions
181#define export_print(z,n,list) \
182 BOOST_PP_CAT(export_, BOOST_PP_SEQ_ELEM(n, list))();
183#define BOOST_PP_LOCAL_MACRO(n) export_print(~, n, GLOBALLISTOFPYTHONACTIONS)
184#define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(GLOBALLISTOFPYTHONACTIONS)))
185#include BOOST_PP_LOCAL_ITERATE()
186#undef instance_print
187}
188
189
190#endif /* PYTHONSCRIPTING_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.