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