source: src/Python/export_numpy.cpp@ 8c9cce

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

Numpy exporters are fully operational.

  • TESTS: added test on working getter and setter.
  • Property mode set to 100644
File size: 4.5 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/def.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<float>();
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_positions() - 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
103void set_ndarray(
104 const np::ndarray &new_positions,
105 boost::function<void (atom &, const Vector &)> &_set_function)
106{
107 unsigned int num_atoms = get_num_atoms();
108
109 // check whether shape of array is correct
110 ASSERT( new_positions.shape()[0] == num_atoms,
111 "pyMoleCuilder::set_positions() - numpy array has unexpected size.");
112
113 unsigned int ia=0;
114 Vector temp;
115 for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection();
116 iter != World::getInstance().endAtomSelection();
117 ++iter) {
118 atom &current = *iter->second;
119 for (unsigned int i=0;i<NDIM;++i)
120 temp[i] = p::extract<double>(new_positions[ia][i]);
121 _set_function(current, temp);
122 ++ia;
123 ASSERT(ia <= num_atoms, "set_positions() - more atoms selected than expected.");
124 }
125}
126
127void set_positions(const np::ndarray &new_positions)
128{
129 static boost::function< void (atom&, const Vector&) > set_vector =
130 boost::bind(&AtomInfo::setPosition, _1, _2);
131 set_ndarray(new_positions, set_vector);
132}
133
134void set_velocities(const np::ndarray &new_positions)
135{
136 static boost::function< void (atom&, const Vector&) > set_vector =
137 boost::bind(&AtomInfo::setAtomicVelocity, _1, _2);
138 set_ndarray(new_positions, set_vector);
139}
140
141void set_forces(const np::ndarray &new_positions)
142{
143 static boost::function< void (atom&, const Vector&) > set_vector =
144 boost::bind(&AtomInfo::setAtomicForce, _1, _2);
145 set_ndarray(new_positions, set_vector);
146}
147
148void export_numpy()
149{
150 p::def("get_positions", get_positions);
151 p::def("get_velocities", get_velocities);
152 p::def("get_forces", get_forces);
153 p::def("set_positions", set_positions, p::args("position"));
154 p::def("set_velocities", set_velocities, p::args("velocity"));
155 p::def("set_forces", set_forces, p::args("force"));
156}
157
Note: See TracBrowser for help on using the repository browser.