source: src/Python/export_numpy.cpp@ 641550

Candidate_v1.7.0 stable
Last change on this file since 641550 was 923ce2, checked in by Frederik Heber <frederik.heber@…>, 4 years ago

Added get_masses to python functionality.

  • Property mode set to 100644
File size: 5.3 KB
Line 
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
37#include<boost/bind.hpp>
38#include<boost/function.hpp>
39#include<boost/python.hpp>
40#include<boost/python/numpy.hpp>
41
42#include "CodePatterns/Assert.hpp"
43
44#include "World.hpp"
45
46namespace p = boost::python;
47namespace np = boost::python::numpy;
48
49unsigned int get_num_atoms()
50{
51 return World::getInstance().countSelectedAtoms();
52}
53
54np::ndarray allocate_ndarray(const unsigned int num_atoms)
55{
56 p::tuple shape = p::make_tuple(num_atoms, 3);
57 np::dtype dtype = np::dtype::get_builtin<double>();
58 np::ndarray array = np::zeros(shape, dtype);
59 return array;
60}
61
62np::ndarray get_ndarray(boost::function<const Vector &(const atom &)> &_get_function)
63{
64 unsigned int num_atoms = get_num_atoms();
65 //std::cout << num_atoms << std::endl;
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;
76 ASSERT(ia <= num_atoms, "get_ndarray() - more atoms selected than expected.");
77 }
78
79 return positions;
80}
81
82np::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
89np::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
96np::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
103np::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
124void set_ndarray(
125 const np::ndarray &_positions,
126 boost::function<void (atom &, const Vector &)> &_set_function)
127{
128 unsigned int num_atoms = get_num_atoms();
129
130 // check whether shape of array is correct
131 ASSERT( _positions.shape(0) == num_atoms,
132 "pyMoleCuilder::set_ndarray() - numpy array has unexpected size.");
133
134 np::ndarray new_positions = _positions.copy();
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 &current = *iter->second;
141 for (unsigned int i=0;i<NDIM;++i) {
142 //std::cout << p::extract<char const *>(p::str(new_positions[ia][i])) << std::endl;
143 temp[i] = p::extract<double>(new_positions[ia][i]);
144 }
145 _set_function(current, temp);
146 ++ia;
147 ASSERT(ia <= num_atoms, "set_ndarray() - more atoms selected than expected.");
148 }
149}
150
151void 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
158void 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
165void 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);
170}
171
172void export_numpy()
173{
174 p::def("get_positions", get_positions);
175 p::def("get_velocities", get_velocities);
176 p::def("get_forces", get_forces);
177 p::def("get_masses", get_masses);
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"));
181}
182
Note: See TracBrowser for help on using the repository browser.