[693a80] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 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/>.
|
---|
[693a80] | 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 |
|
---|
[949953] | 35 | #include <boost/python.hpp>
|
---|
[693a80] | 36 | #include <boost/python/module.hpp>
|
---|
| 37 | #include <boost/python/args.hpp>
|
---|
| 38 |
|
---|
| 39 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 40 |
|
---|
[949953] | 41 | //!> define all present actions
|
---|
| 42 | #include "GlobalListOfActions.hpp"
|
---|
| 43 |
|
---|
| 44 | //!> python wrapping for all of these actions
|
---|
| 45 | #include "AllActionPython.hpp"
|
---|
| 46 |
|
---|
| 47 | #include "cleanUp.hpp"
|
---|
| 48 | #include "Actions/ActionHistory.hpp"
|
---|
[567357] | 49 | #include "cleanUp.hpp"
|
---|
| 50 |
|
---|
| 51 | namespace MoleCuilder {
|
---|
| 52 |
|
---|
| 53 | namespace detail {
|
---|
| 54 |
|
---|
| 55 | void module_exit()
|
---|
| 56 | {
|
---|
| 57 | // save everything
|
---|
| 58 | std::cout << "Saving." << std::endl;
|
---|
| 59 | saveAll();
|
---|
| 60 | // purge everything
|
---|
| 61 | std::cout << "Cleaning memory." << std::endl;
|
---|
| 62 | cleanUp();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void module_reinit()
|
---|
| 66 | {
|
---|
| 67 | // save everything
|
---|
| 68 | std::cout << "Saving." << std::endl;
|
---|
| 69 | saveAll();
|
---|
| 70 | // purge everything
|
---|
| 71 | std::cout << "Cleaning static instances from memory." << std::endl;
|
---|
| 72 | purgeStaticInstances();
|
---|
| 73 | // need to init the history before any action is created
|
---|
| 74 | std::cout << "Reinitializing." << std::endl;
|
---|
| 75 | MoleCuilder::ActionHistory::init();
|
---|
| 76 | }
|
---|
[949953] | 77 |
|
---|
[567357] | 78 | } /* namespace detail */
|
---|
[693a80] | 79 |
|
---|
[f89b45] | 80 | namespace PythonTypes {
|
---|
| 81 |
|
---|
| 82 | inline void IndexError(){
|
---|
| 83 | PyErr_SetString(PyExc_IndexError, "Index out of range");
|
---|
| 84 | boost::python::throw_error_already_set();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | template<class T>
|
---|
| 88 | struct vec_item{
|
---|
| 89 | typedef typename T::value_type V;
|
---|
| 90 | static V& get(T& x, int i){
|
---|
| 91 | static V nothing;
|
---|
| 92 | if(i < 0) i += x.size();
|
---|
| 93 | if(i >= 0 && i < int(x.size())) return x[i];
|
---|
| 94 | IndexError();
|
---|
| 95 | return nothing;
|
---|
| 96 | }
|
---|
| 97 | static void set(T& x, int i, V const& v){
|
---|
| 98 | if(i < 0) i += x.size();
|
---|
| 99 | if(i >= 0 && i < int(x.size())) x[i] = v;
|
---|
| 100 | else IndexError();
|
---|
| 101 | }
|
---|
| 102 | static void del(T& x, int i){
|
---|
| 103 | if(i < 0) i += x.size();
|
---|
| 104 | if(i >= 0 && i < int(x.size())) x.erase(x.begin() + i);
|
---|
| 105 | else IndexError();
|
---|
| 106 | }
|
---|
| 107 | static void add(T& x, V const& v){
|
---|
| 108 | x.push_back(v);
|
---|
| 109 | }
|
---|
| 110 | };
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | } /* namespace PythonTypes */
|
---|
| 114 | } /* namespace MoleCuilder */
|
---|
| 115 |
|
---|
[693a80] | 116 | BOOST_PYTHON_MODULE(pyMoleCuilder)
|
---|
| 117 | {
|
---|
[949953] | 118 | // need to init the history before any action is created
|
---|
| 119 | MoleCuilder::ActionHistory::init();
|
---|
| 120 |
|
---|
| 121 | // from this moment on, we need to be sure to deeinitialize in the correct order
|
---|
| 122 | // this is handled by the cleanup function
|
---|
[567357] | 123 | atexit(MoleCuilder::detail::module_exit);
|
---|
[949953] | 124 |
|
---|
| 125 | // set the docstring of the current module scope
|
---|
| 126 | 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.";
|
---|
| 127 |
|
---|
[567357] | 128 | 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.");
|
---|
[f89b45] | 129 |
|
---|
| 130 | // STL Vectors:
|
---|
| 131 | // doubleVec
|
---|
| 132 | boost::python::class_< std::vector< double > >("PythonType_doubleVec")
|
---|
| 133 | .def("__len__", &std::vector< double >::size)
|
---|
| 134 | .def("clear", &std::vector< double >::clear)
|
---|
| 135 | .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::add,
|
---|
| 136 | boost::python::with_custodian_and_ward<1, 2>()) // let container keep value
|
---|
| 137 | .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::get,
|
---|
| 138 | boost::python::return_value_policy<boost::python::copy_non_const_reference>())
|
---|
| 139 | .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::set,
|
---|
| 140 | boost::python::with_custodian_and_ward<1,2>()) // to let container keep value
|
---|
| 141 | .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::del)
|
---|
| 142 | .def("__iter__", boost::python::iterator< std::vector< double > >())
|
---|
| 143 | ;
|
---|
| 144 |
|
---|
| 145 |
|
---|
[949953] | 146 | #define export_print(z,n,list) \
|
---|
| 147 | BOOST_PP_CAT(export_, BOOST_PP_SEQ_ELEM(n, list))();
|
---|
| 148 | #define BOOST_PP_LOCAL_MACRO(n) export_print(~, n, GLOBALLISTOFACTIONS)
|
---|
| 149 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(GLOBALLISTOFACTIONS)))
|
---|
| 150 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 151 | #undef instance_print
|
---|
[693a80] | 152 | }
|
---|