Ignore:
Timestamp:
May 19, 2021, 7:06:29 PM (4 years ago)
Author:
Frederik Heber <frederik.heber@…>
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)
Message:

Numpy exporters are fully operational.

  • TESTS: added test on working getter and setter.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Python/export_numpy.cpp

    r7ca7d5d r8c9cce  
    3535//#include "CodePatterns/MemDebug.hpp"
    3636
     37#include<boost/bind.hpp>
     38#include<boost/function.hpp>
    3739#include<boost/python/def.hpp>
    38 
    3940#include<boost/python/numpy.hpp>
    4041
    4142#include "CodePatterns/Assert.hpp"
     43
     44#include "World.hpp"
    4245
    4346namespace p = boost::python;
     
    4649unsigned int get_num_atoms()
    4750{
    48   return 0;
     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;
    4980}
    5081
    5182np::ndarray get_positions()
    5283{
    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);
    5887}
    5988
    60 void set_positions(const np::ndarray &new_positions)
     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)
    61106{
    62107  unsigned int num_atoms = get_num_atoms();
     
    65110  ASSERT( new_positions.shape()[0] == num_atoms,
    66111    "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);
    67146}
    68147
    69148void export_numpy()
    70149{
    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"));
    73156}
    74157
Note: See TracChangeset for help on using the changeset viewer.