source: src/Python/export_numpy.cpp@ 802a9d

Candidate_v1.7.1 stable
Last change on this file since 802a9d was 802a9d, checked in by Frederik Heber <frederik.heber@…>, 7 days ago

Adds scripts to generate a bond table.

  • one python script for the computation and a bash script to call it. With this split, we can easily parallelize the computations to make use of JobMarket's multiple workers.
  • Property mode set to 100644
File size: 5.9 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#include "CodePatterns/Log.hpp"
44
45#include "Geometry/GeometryRegistry.hpp"
46#include "World.hpp"
47
48namespace p = boost::python;
49namespace np = boost::python::numpy;
50
51unsigned int get_num_atoms()
52{
53 return World::getInstance().countSelectedAtoms();
54}
55
56np::ndarray allocate_ndarray(const unsigned int num_atoms)
57{
58 p::tuple shape = p::make_tuple(num_atoms, 3);
59 np::dtype dtype = np::dtype::get_builtin<double>();
60 np::ndarray array = np::zeros(shape, dtype);
61 return array;
62}
63
64np::ndarray get_ndarray(boost::function<const Vector &(const atom &)> &_get_function)
65{
66 unsigned int num_atoms = get_num_atoms();
67 //std::cout << num_atoms << std::endl;
68 np::ndarray positions = allocate_ndarray(num_atoms);
69
70 unsigned int ia=0;
71 for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
72 iter != World::getInstance().endAtomSelection();
73 ++iter) {
74 const atom & current = *iter->second;
75 for (unsigned int i=0;i<NDIM;++i)
76 positions[ia][i] = _get_function(current)[i];
77 ++ia;
78 ASSERT(ia <= num_atoms, "get_ndarray() - more atoms selected than expected.");
79 }
80
81 return positions;
82}
83
84np::ndarray get_geometryobject(const std::string &name)
85{
86 LOG(1, "Getting GeometryObject of name " << name);
87 const GeometryObject *object = GeometryRegistry::getInstance().getByName(name);
88 p::tuple shape = p::make_tuple(3);
89 np::dtype dtype = np::dtype::get_builtin<double>();
90 np::ndarray distance = np::zeros(shape, dtype);
91 if (object != NULL) {
92 for (unsigned int i=0;i<NDIM;++i)
93 distance[i] = object->getVector()[i];
94 }
95 return distance;
96}
97
98np::ndarray get_positions()
99{
100 static boost::function< const Vector&(const atom&) > get_vector =
101 boost::bind(&AtomInfo::getPosition, _1);
102 return get_ndarray(get_vector);
103}
104
105np::ndarray get_velocities()
106{
107 static boost::function< const Vector&(const atom&) > get_vector =
108 boost::bind(&AtomInfo::getAtomicVelocity, _1);
109 return get_ndarray(get_vector);
110}
111
112np::ndarray get_forces()
113{
114 static boost::function< const Vector&(const atom&) > get_vector =
115 boost::bind(&AtomInfo::getAtomicForce, _1);
116 return get_ndarray(get_vector);
117}
118
119np::ndarray get_masses()
120{
121 unsigned int num_atoms = get_num_atoms();
122 //std::cout << num_atoms << std::endl;
123 p::tuple shape = p::make_tuple(num_atoms);
124 np::dtype dtype = np::dtype::get_builtin<double>();
125 np::ndarray masses = np::zeros(shape, dtype);
126
127 unsigned int ia=0;
128 for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
129 iter != World::getInstance().endAtomSelection();
130 ++iter) {
131 const atom & current = *iter->second;
132 masses[ia] = current.getMass();
133 ++ia;
134 ASSERT(ia <= num_atoms, "get_masses() - more atoms selected than expected.");
135 }
136
137 return masses;
138}
139
140void set_ndarray(
141 const np::ndarray &_positions,
142 boost::function<void (atom &, const Vector &)> &_set_function)
143{
144#ifndef NDEBUG
145 unsigned int num_atoms = get_num_atoms();
146#endif
147 // check whether shape of array is correct
148 ASSERT( _positions.shape(0) == num_atoms,
149 "pyMoleCuilder::set_ndarray() - numpy array has unexpected size.");
150
151 np::ndarray new_positions = _positions.copy();
152 unsigned int ia=0;
153 Vector temp;
154 for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection();
155 iter != World::getInstance().endAtomSelection();
156 ++iter) {
157 atom &current = *iter->second;
158 for (unsigned int i=0;i<NDIM;++i) {
159 //std::cout << p::extract<char const *>(p::str(new_positions[ia][i])) << std::endl;
160 temp[i] = p::extract<double>(new_positions[ia][i]);
161 }
162 _set_function(current, temp);
163 ++ia;
164 ASSERT(ia <= num_atoms, "set_ndarray() - more atoms selected than expected.");
165 }
166}
167
168void set_positions(const np::ndarray &new_positions)
169{
170 static boost::function< void (atom&, const Vector&) > set_vector =
171 boost::bind(&AtomInfo::setPosition, _1, _2);
172 set_ndarray(new_positions, set_vector);
173}
174
175void set_velocities(const np::ndarray &new_positions)
176{
177 static boost::function< void (atom&, const Vector&) > set_vector =
178 boost::bind(&AtomInfo::setAtomicVelocity, _1, _2);
179 set_ndarray(new_positions, set_vector);
180}
181
182void set_forces(const np::ndarray &new_positions)
183{
184 static boost::function< void (atom&, const Vector&) > set_vector =
185 boost::bind(&AtomInfo::setAtomicForce, _1, _2);
186 set_ndarray(new_positions, set_vector);
187}
188
189void export_numpy()
190{
191 p::def("get_geometryobject", get_geometryobject, p::args("name"));
192 p::def("get_positions", get_positions);
193 p::def("get_velocities", get_velocities);
194 p::def("get_forces", get_forces);
195 p::def("get_masses", get_masses);
196 p::def("set_positions", set_positions, p::args("position"));
197 p::def("set_velocities", set_velocities, p::args("velocity"));
198 p::def("set_forces", set_forces, p::args("force"));
199}
200
Note: See TracBrowser for help on using the repository browser.