| [7b1824] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2019 Frederik Heber. 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 |  * export_numpy.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Mar 23, 2019
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 36 | 
 | 
|---|
| [8c9cce] | 37 | #include<boost/bind.hpp>
 | 
|---|
 | 38 | #include<boost/function.hpp>
 | 
|---|
| [923ce2] | 39 | #include<boost/python.hpp>
 | 
|---|
| [7b1824] | 40 | #include<boost/python/numpy.hpp>
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 43 | 
 | 
|---|
| [8c9cce] | 44 | #include "World.hpp"
 | 
|---|
 | 45 | 
 | 
|---|
| [7b1824] | 46 | namespace p = boost::python;
 | 
|---|
 | 47 | namespace np = boost::python::numpy;
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | unsigned int get_num_atoms()
 | 
|---|
 | 50 | {
 | 
|---|
| [8c9cce] | 51 |   return World::getInstance().countSelectedAtoms();
 | 
|---|
| [7b1824] | 52 | }
 | 
|---|
 | 53 | 
 | 
|---|
| [8c9cce] | 54 | np::ndarray allocate_ndarray(const unsigned int num_atoms)
 | 
|---|
| [7b1824] | 55 | {
 | 
|---|
 | 56 |   p::tuple shape = p::make_tuple(num_atoms, 3);
 | 
|---|
| [923ce2] | 57 |   np::dtype dtype = np::dtype::get_builtin<double>();
 | 
|---|
| [8c9cce] | 58 |   np::ndarray array = np::zeros(shape, dtype);
 | 
|---|
 | 59 |   return array;
 | 
|---|
 | 60 | }
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 | np::ndarray get_ndarray(boost::function<const Vector &(const atom &)> &_get_function)
 | 
|---|
 | 63 | {
 | 
|---|
 | 64 |   unsigned int num_atoms = get_num_atoms();
 | 
|---|
| [923ce2] | 65 |   //std::cout << num_atoms << std::endl;
 | 
|---|
| [8c9cce] | 66 |   np::ndarray positions = allocate_ndarray(num_atoms);
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 |   unsigned int ia=0;
 | 
|---|
 | 69 |   for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
 | 70 |       iter != World::getInstance().endAtomSelection();
 | 
|---|
 | 71 |       ++iter) {
 | 
|---|
 | 72 |     const atom & current = *iter->second;
 | 
|---|
 | 73 |     for (unsigned int i=0;i<NDIM;++i)
 | 
|---|
 | 74 |       positions[ia][i] = _get_function(current)[i];
 | 
|---|
 | 75 |     ++ia;
 | 
|---|
| [923ce2] | 76 |     ASSERT(ia <= num_atoms, "get_ndarray() - more atoms selected than expected.");
 | 
|---|
| [8c9cce] | 77 |   }
 | 
|---|
 | 78 | 
 | 
|---|
| [7b1824] | 79 |   return positions;
 | 
|---|
 | 80 | }
 | 
|---|
 | 81 | 
 | 
|---|
| [8c9cce] | 82 | np::ndarray get_positions()
 | 
|---|
 | 83 | {
 | 
|---|
 | 84 |   static boost::function< const Vector&(const atom&) > get_vector =
 | 
|---|
 | 85 |       boost::bind(&AtomInfo::getPosition, _1);
 | 
|---|
 | 86 |   return get_ndarray(get_vector);
 | 
|---|
 | 87 | }
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 | np::ndarray get_velocities()
 | 
|---|
 | 90 | {
 | 
|---|
 | 91 |   static boost::function< const Vector&(const atom&) > get_vector =
 | 
|---|
 | 92 |       boost::bind(&AtomInfo::getAtomicVelocity, _1);
 | 
|---|
 | 93 |   return get_ndarray(get_vector);
 | 
|---|
 | 94 | }
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | np::ndarray get_forces()
 | 
|---|
 | 97 | {
 | 
|---|
 | 98 |   static boost::function< const Vector&(const atom&) > get_vector =
 | 
|---|
 | 99 |       boost::bind(&AtomInfo::getAtomicForce, _1);
 | 
|---|
 | 100 |   return get_ndarray(get_vector);
 | 
|---|
 | 101 | }
 | 
|---|
 | 102 | 
 | 
|---|
| [923ce2] | 103 | np::ndarray get_masses()
 | 
|---|
 | 104 | {
 | 
|---|
 | 105 |   unsigned int num_atoms = get_num_atoms();
 | 
|---|
 | 106 |   //std::cout << num_atoms << std::endl;
 | 
|---|
 | 107 |   p::tuple shape = p::make_tuple(num_atoms);
 | 
|---|
 | 108 |   np::dtype dtype = np::dtype::get_builtin<double>();
 | 
|---|
 | 109 |   np::ndarray masses = np::zeros(shape, dtype);
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 |   unsigned int ia=0;
 | 
|---|
 | 112 |   for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
 | 113 |       iter != World::getInstance().endAtomSelection();
 | 
|---|
 | 114 |       ++iter) {
 | 
|---|
 | 115 |     const atom & current = *iter->second;
 | 
|---|
 | 116 |     masses[ia] = current.getMass();
 | 
|---|
 | 117 |     ++ia;
 | 
|---|
 | 118 |     ASSERT(ia <= num_atoms, "get_masses() - more atoms selected than expected.");
 | 
|---|
 | 119 |   }
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 |   return masses;
 | 
|---|
 | 122 | }
 | 
|---|
 | 123 | 
 | 
|---|
| [8c9cce] | 124 | void set_ndarray(
 | 
|---|
| [923ce2] | 125 |     const np::ndarray &_positions,
 | 
|---|
| [8c9cce] | 126 |     boost::function<void (atom &, const Vector &)> &_set_function)
 | 
|---|
| [7b1824] | 127 | {
 | 
|---|
 | 128 |   unsigned int num_atoms = get_num_atoms();
 | 
|---|
 | 129 |   
 | 
|---|
 | 130 |   // check whether shape of array is correct 
 | 
|---|
| [923ce2] | 131 |   ASSERT( _positions.shape(0) == num_atoms,
 | 
|---|
 | 132 |     "pyMoleCuilder::set_ndarray() - numpy array has unexpected size.");
 | 
|---|
| [8c9cce] | 133 | 
 | 
|---|
| [923ce2] | 134 |   np::ndarray new_positions = _positions.copy();
 | 
|---|
| [8c9cce] | 135 |   unsigned int ia=0;
 | 
|---|
 | 136 |   Vector temp;
 | 
|---|
 | 137 |   for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
 | 138 |       iter != World::getInstance().endAtomSelection();
 | 
|---|
 | 139 |       ++iter) {
 | 
|---|
 | 140 |     atom ¤t = *iter->second;
 | 
|---|
| [923ce2] | 141 |     for (unsigned int i=0;i<NDIM;++i) {
 | 
|---|
 | 142 |       //std::cout << p::extract<char const *>(p::str(new_positions[ia][i])) << std::endl;
 | 
|---|
| [8c9cce] | 143 |       temp[i] = p::extract<double>(new_positions[ia][i]);
 | 
|---|
| [923ce2] | 144 |     }
 | 
|---|
| [8c9cce] | 145 |     _set_function(current, temp);
 | 
|---|
 | 146 |     ++ia;
 | 
|---|
| [923ce2] | 147 |     ASSERT(ia <= num_atoms, "set_ndarray() - more atoms selected than expected.");
 | 
|---|
| [8c9cce] | 148 |   }
 | 
|---|
 | 149 | }
 | 
|---|
 | 150 | 
 | 
|---|
 | 151 | void set_positions(const np::ndarray &new_positions)
 | 
|---|
 | 152 | {
 | 
|---|
 | 153 |   static boost::function< void (atom&, const Vector&) > set_vector =
 | 
|---|
 | 154 |       boost::bind(&AtomInfo::setPosition, _1, _2);
 | 
|---|
 | 155 |   set_ndarray(new_positions, set_vector);
 | 
|---|
 | 156 | }
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | void set_velocities(const np::ndarray &new_positions)
 | 
|---|
 | 159 | {
 | 
|---|
 | 160 |   static boost::function< void (atom&, const Vector&) > set_vector =
 | 
|---|
 | 161 |       boost::bind(&AtomInfo::setAtomicVelocity, _1, _2);
 | 
|---|
 | 162 |   set_ndarray(new_positions, set_vector);
 | 
|---|
 | 163 | }
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 | void set_forces(const np::ndarray &new_positions)
 | 
|---|
 | 166 | {
 | 
|---|
 | 167 |   static boost::function< void (atom&, const Vector&) > set_vector =
 | 
|---|
 | 168 |       boost::bind(&AtomInfo::setAtomicForce, _1, _2);
 | 
|---|
 | 169 |   set_ndarray(new_positions, set_vector);
 | 
|---|
| [7b1824] | 170 | }
 | 
|---|
 | 171 | 
 | 
|---|
 | 172 | void export_numpy()
 | 
|---|
 | 173 | {
 | 
|---|
| [8c9cce] | 174 |   p::def("get_positions", get_positions);
 | 
|---|
 | 175 |   p::def("get_velocities", get_velocities);
 | 
|---|
 | 176 |   p::def("get_forces", get_forces);
 | 
|---|
| [923ce2] | 177 |   p::def("get_masses", get_masses);
 | 
|---|
| [8c9cce] | 178 |   p::def("set_positions", set_positions, p::args("position"));
 | 
|---|
 | 179 |   p::def("set_velocities", set_velocities, p::args("velocity"));
 | 
|---|
 | 180 |   p::def("set_forces", set_forces, p::args("force"));
 | 
|---|
| [7b1824] | 181 | }
 | 
|---|
 | 182 | 
 | 
|---|