Changeset 8c9cce for src/Python/export_numpy.cpp
- Timestamp:
- May 19, 2021, 7:06:29 PM (4 years ago)
- Branches:
- Candidate_v1.7.0, stable
- Children:
- ecc050
- Parents:
- 7ca7d5d
- git-author:
- Frederik Heber <frederik.heber@…> (04/23/19 01:11:50)
- git-committer:
- Frederik Heber <frederik.heber@…> (05/19/21 19:06:29)
- File:
-
- 1 edited
-
src/Python/export_numpy.cpp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Python/export_numpy.cpp
r7ca7d5d r8c9cce 35 35 //#include "CodePatterns/MemDebug.hpp" 36 36 37 #include<boost/bind.hpp> 38 #include<boost/function.hpp> 37 39 #include<boost/python/def.hpp> 38 39 40 #include<boost/python/numpy.hpp> 40 41 41 42 #include "CodePatterns/Assert.hpp" 43 44 #include "World.hpp" 42 45 43 46 namespace p = boost::python; … … 46 49 unsigned int get_num_atoms() 47 50 { 48 return 0; 51 return World::getInstance().countSelectedAtoms(); 52 } 53 54 np::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 62 np::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; 49 80 } 50 81 51 82 np::ndarray get_positions() 52 83 { 53 unsigned int num_atoms = get_num_atoms(); 54 p::tuple shape = p::make_tuple(num_atoms, 3); 55 np::dtype dtype = np::dtype::get_builtin<float>(); 56 np::ndarray positions = np::zeros(shape, dtype); 57 return positions; 84 static boost::function< const Vector&(const atom&) > get_vector = 85 boost::bind(&AtomInfo::getPosition, _1); 86 return get_ndarray(get_vector); 58 87 } 59 88 60 void set_positions(const np::ndarray &new_positions) 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 103 void set_ndarray( 104 const np::ndarray &new_positions, 105 boost::function<void (atom &, const Vector &)> &_set_function) 61 106 { 62 107 unsigned int num_atoms = get_num_atoms(); … … 65 110 ASSERT( new_positions.shape()[0] == num_atoms, 66 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 ¤t = *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 127 void 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 134 void 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 141 void 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); 67 146 } 68 147 69 148 void export_numpy() 70 149 { 71 p::def("get_position", get_positions); 72 p::def("set_position", set_positions, p::args("position")); 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")); 73 156 } 74 157
Note:
See TracChangeset
for help on using the changeset viewer.
