| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * pyMoleCuilder.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Sep 21, 2011
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include <boost/python.hpp>
 | 
|---|
| 36 | #include <boost/python/module.hpp>
 | 
|---|
| 37 | #include <boost/python/args.hpp>
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 40 | #include "CodePatterns/toString.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | //!> define all present actions
 | 
|---|
| 43 | #include "GlobalListOfActions.hpp"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | //!> python wrapping for all of these actions
 | 
|---|
| 46 | #include "AllActionPython.hpp"
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #include "cleanUp.hpp"
 | 
|---|
| 49 | #include "Actions/ActionHistory.hpp"
 | 
|---|
| 50 | #include "cleanUp.hpp"
 | 
|---|
| 51 | 
 | 
|---|
| 52 | namespace MoleCuilder {
 | 
|---|
| 53 | 
 | 
|---|
| 54 | namespace detail {
 | 
|---|
| 55 | 
 | 
|---|
| 56 | void module_exit()
 | 
|---|
| 57 | {
 | 
|---|
| 58 |   // save everything
 | 
|---|
| 59 |   std::cout << "Saving." << std::endl;
 | 
|---|
| 60 |   saveAll();
 | 
|---|
| 61 |   // purge everything
 | 
|---|
| 62 |   std::cout << "Cleaning memory." << std::endl;
 | 
|---|
| 63 |   cleanUp();
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | void module_reinit()
 | 
|---|
| 67 | {
 | 
|---|
| 68 |   // save everything
 | 
|---|
| 69 |   std::cout << "Saving." << std::endl;
 | 
|---|
| 70 |   saveAll();
 | 
|---|
| 71 |   // purge everything
 | 
|---|
| 72 |   std::cout << "Cleaning static instances from memory." << std::endl;
 | 
|---|
| 73 |   purgeStaticInstances();
 | 
|---|
| 74 |   // need to init the history before any action is created
 | 
|---|
| 75 |   std::cout << "Reinitializing." << std::endl;
 | 
|---|
| 76 |   MoleCuilder::ActionHistory::init();
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | } /* namespace detail */
 | 
|---|
| 80 | 
 | 
|---|
| 81 | namespace PythonTypes {
 | 
|---|
| 82 | 
 | 
|---|
| 83 | inline void IndexError(){
 | 
|---|
| 84 |     PyErr_SetString(PyExc_IndexError, "Index out of range");
 | 
|---|
| 85 |     boost::python::throw_error_already_set();
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | template<class T>
 | 
|---|
| 89 | struct vec_item{
 | 
|---|
| 90 |     typedef typename T::value_type V;
 | 
|---|
| 91 |     static V& get(T& x, int i){
 | 
|---|
| 92 |         static V nothing;
 | 
|---|
| 93 |         if(i < 0) i += x.size();
 | 
|---|
| 94 |         if(i >= 0 && i < int(x.size())) return x[i];
 | 
|---|
| 95 |         IndexError();
 | 
|---|
| 96 |         return nothing;
 | 
|---|
| 97 |     }
 | 
|---|
| 98 |     static void set(T& x, int i, V const& v){
 | 
|---|
| 99 |         if(i < 0) i += x.size();
 | 
|---|
| 100 |         if(i >= 0 && i < int(x.size())) x[i] = v;
 | 
|---|
| 101 |         else IndexError();
 | 
|---|
| 102 |     }
 | 
|---|
| 103 |     static void del(T& x, int i){
 | 
|---|
| 104 |         if(i < 0) i += x.size();
 | 
|---|
| 105 |         if(i >= 0 && i < int(x.size())) x.erase(x.begin() + i);
 | 
|---|
| 106 |         else IndexError();
 | 
|---|
| 107 |     }
 | 
|---|
| 108 |     static void add(T& x, V const& v){
 | 
|---|
| 109 |         x.push_back(v);
 | 
|---|
| 110 |     }
 | 
|---|
| 111 | };
 | 
|---|
| 112 | 
 | 
|---|
| 113 | 
 | 
|---|
| 114 | } /* namespace PythonTypes */
 | 
|---|
| 115 | } /* namespace MoleCuilder */
 | 
|---|
| 116 | 
 | 
|---|
| 117 | BOOST_PYTHON_MODULE(pyMoleCuilder)
 | 
|---|
| 118 | {
 | 
|---|
| 119 |   // need to init the history before any action is created
 | 
|---|
| 120 |   MoleCuilder::ActionHistory::init();
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   // from this moment on, we need to be sure to deeinitialize in the correct order
 | 
|---|
| 123 |   // this is handled by the cleanup function
 | 
|---|
| 124 |   atexit(MoleCuilder::detail::module_exit);
 | 
|---|
| 125 | 
 | 
|---|
| 126 |   // set the docstring of the current module scope
 | 
|---|
| 127 |   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.";
 | 
|---|
| 128 | 
 | 
|---|
| 129 |   boost::python::def("reinit", MoleCuilder::detail::module_reinit, "Reinitializes the internal state of the python module as if it had been freshly imported, saves all input files beforehand.");
 | 
|---|
| 130 | 
 | 
|---|
| 131 |   // STL Vectors:
 | 
|---|
| 132 |   // doubleVec
 | 
|---|
| 133 |   boost::python::class_< std::vector< double > >("PythonType_doubleVec")
 | 
|---|
| 134 |       .def("__len__", &std::vector< double >::size)
 | 
|---|
| 135 |       .def("clear", &std::vector< double >::clear)
 | 
|---|
| 136 |       .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::add,
 | 
|---|
| 137 |             boost::python::with_custodian_and_ward<1, 2>()) // let container keep value
 | 
|---|
| 138 |       .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::get,
 | 
|---|
| 139 |            boost::python::return_value_policy<boost::python::copy_non_const_reference>())
 | 
|---|
| 140 |       .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::set,
 | 
|---|
| 141 |            boost::python::with_custodian_and_ward<1,2>()) // to let container keep value
 | 
|---|
| 142 |       .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::del)
 | 
|---|
| 143 |       .def("__iter__", boost::python::iterator< std::vector< double > >())
 | 
|---|
| 144 |   ;
 | 
|---|
| 145 | 
 | 
|---|
| 146 | 
 | 
|---|
| 147 | #define export_print(z,n,list) \
 | 
|---|
| 148 |         BOOST_PP_CAT(export_, BOOST_PP_SEQ_ELEM(n, list))();
 | 
|---|
| 149 | #define BOOST_PP_LOCAL_MACRO(n) export_print(~, n, GLOBALLISTOFACTIONS)
 | 
|---|
| 150 | #define BOOST_PP_LOCAL_LIMITS  (0, BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(GLOBALLISTOFACTIONS)))
 | 
|---|
| 151 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 152 | #undef instance_print  
 | 
|---|
| 153 | }
 | 
|---|