Changeset 923ce2


Ignore:
Timestamp:
May 19, 2021, 7:06:29 PM (4 years ago)
Author:
Frederik Heber <frederik.heber@…>
Branches:
Candidate_v1.7.0, stable
Children:
1e490ec
Parents:
ecc050
git-author:
Frederik Heber <frederik.heber@…> (06/21/20 21:29:52)
git-committer:
Frederik Heber <frederik.heber@…> (05/19/21 19:06:29)
Message:

Added get_masses to python functionality.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Python/export_numpy.cpp

    recc050 r923ce2  
    3737#include<boost/bind.hpp>
    3838#include<boost/function.hpp>
    39 #include<boost/python/def.hpp>
     39#include<boost/python.hpp>
    4040#include<boost/python/numpy.hpp>
    4141
     
    5555{
    5656  p::tuple shape = p::make_tuple(num_atoms, 3);
    57   np::dtype dtype = np::dtype::get_builtin<float>();
     57  np::dtype dtype = np::dtype::get_builtin<double>();
    5858  np::ndarray array = np::zeros(shape, dtype);
    5959  return array;
     
    6363{
    6464  unsigned int num_atoms = get_num_atoms();
    65   std::cout << num_atoms << std::endl;
     65  //std::cout << num_atoms << std::endl;
    6666  np::ndarray positions = allocate_ndarray(num_atoms);
    6767
     
    7474      positions[ia][i] = _get_function(current)[i];
    7575    ++ia;
    76     ASSERT(ia <= num_atoms, "get_positions() - more atoms selected than expected.");
     76    ASSERT(ia <= num_atoms, "get_ndarray() - more atoms selected than expected.");
    7777  }
    7878
     
    101101}
    102102
     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
    103124void set_ndarray(
    104     const np::ndarray &new_positions,
     125    const np::ndarray &_positions,
    105126    boost::function<void (atom &, const Vector &)> &_set_function)
    106127{
     
    108129 
    109130  // check whether shape of array is correct
    110   ASSERT( new_positions.shape()[0] == num_atoms,
    111     "pyMoleCuilder::set_positions() - numpy array has unexpected size.");
     131  ASSERT( _positions.shape(0) == num_atoms,
     132    "pyMoleCuilder::set_ndarray() - numpy array has unexpected size.");
    112133
     134  np::ndarray new_positions = _positions.copy();
    113135  unsigned int ia=0;
    114136  Vector temp;
     
    117139      ++iter) {
    118140    atom &current = *iter->second;
    119     for (unsigned int i=0;i<NDIM;++i)
     141    for (unsigned int i=0;i<NDIM;++i) {
     142      //std::cout << p::extract<char const *>(p::str(new_positions[ia][i])) << std::endl;
    120143      temp[i] = p::extract<double>(new_positions[ia][i]);
     144    }
    121145    _set_function(current, temp);
    122146    ++ia;
    123     ASSERT(ia <= num_atoms, "set_positions() - more atoms selected than expected.");
     147    ASSERT(ia <= num_atoms, "set_ndarray() - more atoms selected than expected.");
    124148  }
    125149}
     
    151175  p::def("get_velocities", get_velocities);
    152176  p::def("get_forces", get_forces);
     177  p::def("get_masses", get_masses);
    153178  p::def("set_positions", set_positions, p::args("position"));
    154179  p::def("set_velocities", set_velocities, p::args("velocity"));
Note: See TracChangeset for help on using the changeset viewer.