| [5a5c47] | 1 | /* | 
|---|
|  | 2 | * AtomSet.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Jul 30, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef ATOMSET_HPP_ | 
|---|
|  | 9 | #define ATOMSET_HPP_ | 
|---|
|  | 10 |  | 
|---|
| [56f73b] | 11 | // include config.h | 
|---|
|  | 12 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 13 | #include <config.h> | 
|---|
|  | 14 | #endif | 
|---|
|  | 15 |  | 
|---|
|  | 16 |  | 
|---|
| [5a5c47] | 17 |  | 
|---|
| [0477b0] | 18 | #include <algorithm> | 
|---|
| [5a5c47] | 19 | #include <functional> | 
|---|
| [0477b0] | 20 | #include <limits> | 
|---|
| [ddc85b] | 21 | #include <numeric> | 
|---|
| [0477b0] | 22 |  | 
|---|
|  | 23 | #include <boost/bind.hpp> | 
|---|
| [5a5c47] | 24 | #include <boost/foreach.hpp> | 
|---|
|  | 25 |  | 
|---|
|  | 26 | /** | 
|---|
|  | 27 | * A simple mixin to give any STL conforming structure fast Vector abilities | 
|---|
|  | 28 | * | 
|---|
|  | 29 | * TODO: make this work for maps | 
|---|
|  | 30 | */ | 
|---|
|  | 31 |  | 
|---|
|  | 32 | #include "atom.hpp" | 
|---|
|  | 33 |  | 
|---|
|  | 34 | // this tests, whether we actually have a Vector | 
|---|
|  | 35 | template <class V> | 
|---|
|  | 36 | struct is_atom{}; | 
|---|
|  | 37 |  | 
|---|
|  | 38 | template <> | 
|---|
|  | 39 | struct is_atom<atom*>{ | 
|---|
|  | 40 | typedef void wrong_type; | 
|---|
|  | 41 | }; | 
|---|
|  | 42 |  | 
|---|
| [795c0f] | 43 | template <> | 
|---|
|  | 44 | struct is_atom<const atom*>{ | 
|---|
|  | 45 | typedef void wrong_type; | 
|---|
|  | 46 | }; | 
|---|
|  | 47 |  | 
|---|
| [3738f0] | 48 | template <class container_type, | 
|---|
|  | 49 | class iterator_type = typename container_type::iterator, | 
|---|
|  | 50 | class const_iterator_type = typename container_type::const_iterator> | 
|---|
|  | 51 | class AtomSetMixin : public container_type | 
|---|
| [5a5c47] | 52 | { | 
|---|
|  | 53 | // when our set carries something besides a atom* this will produce an error | 
|---|
| [3738f0] | 54 | typedef typename is_atom<typename container_type::value_type>::wrong_type check_for_atom; | 
|---|
| [5a5c47] | 55 | public: | 
|---|
|  | 56 | // typedefs for STL conforming structure | 
|---|
| [3738f0] | 57 | typedef iterator_type iterator; | 
|---|
|  | 58 | typedef const_iterator_type const_iterator; | 
|---|
| [5a5c47] | 59 |  | 
|---|
|  | 60 | AtomSetMixin() : | 
|---|
| [3738f0] | 61 | container_type() | 
|---|
| [5a5c47] | 62 | {} | 
|---|
|  | 63 |  | 
|---|
| [3738f0] | 64 | AtomSetMixin(const container_type& src) : | 
|---|
|  | 65 | container_type(src) | 
|---|
| [5a5c47] | 66 | {} | 
|---|
|  | 67 | virtual ~AtomSetMixin(){} | 
|---|
|  | 68 |  | 
|---|
|  | 69 | /** | 
|---|
|  | 70 | * translate all Atoms within this set by a specified amount | 
|---|
|  | 71 | */ | 
|---|
| [a33ea5] | 72 | void translate(const Vector &translater); | 
|---|
| [5ac690] | 73 | void addVelocityAtStep(const Vector velocity, unsigned int step); | 
|---|
| [a33ea5] | 74 |  | 
|---|
|  | 75 | template<class Function> | 
|---|
|  | 76 | void transformNodes(Function f); | 
|---|
| [cddda7] | 77 | double totalMass() const; | 
|---|
| [5e99bc] | 78 | double totalTemperatureAtStep(unsigned int step) const; | 
|---|
|  | 79 | Vector totalMomentumAtStep(unsigned int step) const; | 
|---|
| [a33ea5] | 80 |  | 
|---|
| [8009ce] | 81 | size_t getMaxTrajectorySize() const; | 
|---|
|  | 82 |  | 
|---|
| [0477b0] | 83 | void sortByIds(); | 
|---|
|  | 84 |  | 
|---|
| [a33ea5] | 85 | private: | 
|---|
|  | 86 | template<class Function> | 
|---|
|  | 87 | struct workOnNodePointer { | 
|---|
|  | 88 | workOnNodePointer(Function &_f) : f(_f){} | 
|---|
|  | 89 | void operator()(atom *atom){ | 
|---|
| [8f4df1] | 90 | atom->setPosition(f(atom->getPosition())); | 
|---|
| [5a5c47] | 91 | } | 
|---|
| [a33ea5] | 92 | Function &f; | 
|---|
|  | 93 | }; | 
|---|
| [ddc85b] | 94 |  | 
|---|
| [cddda7] | 95 | template<class T> | 
|---|
|  | 96 | struct valueSum { | 
|---|
| [6625c3] | 97 | valueSum(T (AtomInfo::*_f)() const,T startValue) : | 
|---|
| [cddda7] | 98 | f(_f), | 
|---|
| [5e99bc] | 99 | value(startValue) | 
|---|
| [cddda7] | 100 | {} | 
|---|
| [f01769] | 101 | T operator+(const AtomInfo *atom){ | 
|---|
| [cddda7] | 102 | return value + (atom->*f)(); | 
|---|
| [ddc85b] | 103 | } | 
|---|
| [5e99bc] | 104 | T operator=(T _value){ | 
|---|
| [cddda7] | 105 | value = _value; | 
|---|
|  | 106 | return value; | 
|---|
|  | 107 | } | 
|---|
| [6625c3] | 108 | T (AtomInfo::*f)() const; | 
|---|
| [cddda7] | 109 | T value; | 
|---|
|  | 110 | }; | 
|---|
|  | 111 |  | 
|---|
| [8009ce] | 112 | template<class T> | 
|---|
|  | 113 | struct valueMax { | 
|---|
|  | 114 | valueMax(T (AtomInfo::*_f)() const,T startValue) : | 
|---|
|  | 115 | f(_f), | 
|---|
|  | 116 | value(startValue) | 
|---|
|  | 117 | {} | 
|---|
| [f01769] | 118 | T operator+(const AtomInfo *atom){ | 
|---|
| [8009ce] | 119 | const T temp = (atom->*f)(); | 
|---|
|  | 120 | return value < temp ? temp : value; | 
|---|
|  | 121 | } | 
|---|
|  | 122 | T operator=(T _value){ | 
|---|
|  | 123 | value = _value; | 
|---|
|  | 124 | return value; | 
|---|
|  | 125 | } | 
|---|
|  | 126 | T (AtomInfo::*f)() const; | 
|---|
|  | 127 | T value; | 
|---|
|  | 128 | }; | 
|---|
|  | 129 |  | 
|---|
| [cddda7] | 130 | template<class T> | 
|---|
|  | 131 | struct stepValueSum { | 
|---|
| [6625c3] | 132 | stepValueSum(unsigned int _step, T (AtomInfo::*_f)(unsigned int) const,T startValue) : | 
|---|
| [cddda7] | 133 | step(_step), | 
|---|
|  | 134 | f(_f), | 
|---|
| [5e99bc] | 135 | value(startValue) | 
|---|
| [cddda7] | 136 | {} | 
|---|
| [f01769] | 137 | T operator+(const AtomInfo *atom){ | 
|---|
| [cddda7] | 138 | return value + (atom->*f)(step); | 
|---|
|  | 139 | } | 
|---|
| [5e99bc] | 140 | T operator=(T _value){ | 
|---|
| [ddc85b] | 141 | value = _value; | 
|---|
|  | 142 | return value; | 
|---|
|  | 143 | } | 
|---|
|  | 144 | unsigned int step; | 
|---|
| [6625c3] | 145 | T (AtomInfo::*f)(unsigned int) const; | 
|---|
| [cddda7] | 146 | T value; | 
|---|
| [ddc85b] | 147 | }; | 
|---|
| [5a5c47] | 148 | }; | 
|---|
|  | 149 |  | 
|---|
| [3738f0] | 150 | template <class container_type, | 
|---|
|  | 151 | class iterator_type, | 
|---|
|  | 152 | class const_iterator_type> | 
|---|
|  | 153 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::translate(const Vector &translater){ | 
|---|
| [6625c3] | 154 | BOOST_FOREACH(AtomInfo *atom,*this){ | 
|---|
| [8f4df1] | 155 | *(atom) += translater; | 
|---|
| [a33ea5] | 156 | } | 
|---|
|  | 157 | } | 
|---|
|  | 158 |  | 
|---|
| [3738f0] | 159 | template <class container_type, | 
|---|
|  | 160 | class iterator_type, | 
|---|
|  | 161 | class const_iterator_type> | 
|---|
|  | 162 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::addVelocityAtStep(const Vector velocity, unsigned int step){ | 
|---|
| [6625c3] | 163 | BOOST_FOREACH(AtomInfo *atom,*this){ | 
|---|
| [056e70] | 164 | atom->setAtomicVelocityAtStep(step, atom->getAtomicVelocityAtStep(step)+velocity); | 
|---|
| [5ac690] | 165 | } | 
|---|
|  | 166 | } | 
|---|
|  | 167 |  | 
|---|
| [3738f0] | 168 | template <class container_type, | 
|---|
|  | 169 | class iterator_type, | 
|---|
|  | 170 | class const_iterator_type> | 
|---|
| [a33ea5] | 171 | template<class Function> | 
|---|
| [3738f0] | 172 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::transformNodes(Function f){ | 
|---|
| [a33ea5] | 173 | std::for_each(this->begin(), | 
|---|
|  | 174 | this->end(), | 
|---|
|  | 175 | AtomSetMixin::workOnNodePointer<Function>(f)); | 
|---|
|  | 176 | } | 
|---|
|  | 177 |  | 
|---|
| [3738f0] | 178 | template <class container_type, | 
|---|
|  | 179 | class iterator_type, | 
|---|
|  | 180 | class const_iterator_type> | 
|---|
|  | 181 | inline double AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalMass() const{ | 
|---|
| [6625c3] | 182 | return accumulate(this->begin(),this->end(),valueSum<double>(&AtomInfo::getMass,0)).value; | 
|---|
| [5e99bc] | 183 | } | 
|---|
|  | 184 |  | 
|---|
| [8009ce] | 185 | template <class container_type, | 
|---|
|  | 186 | class iterator_type, | 
|---|
|  | 187 | class const_iterator_type> | 
|---|
|  | 188 | inline size_t AtomSetMixin<container_type,iterator_type,const_iterator_type>::getMaxTrajectorySize() const | 
|---|
|  | 189 | { | 
|---|
|  | 190 | return accumulate(this->begin(),this->end(),valueMax<size_t>(&AtomInfo::getTrajectorySize,(size_t)1)).value; | 
|---|
|  | 191 | } | 
|---|
|  | 192 |  | 
|---|
| [3738f0] | 193 | template <class container_type, | 
|---|
|  | 194 | class iterator_type, | 
|---|
|  | 195 | class const_iterator_type> | 
|---|
|  | 196 | inline double AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalTemperatureAtStep(unsigned int step) const{ | 
|---|
| [6625c3] | 197 | return accumulate(this->begin(),this->end(),stepValueSum<double>(step,&AtomInfo::getKineticEnergy,0)).value; | 
|---|
| [cddda7] | 198 | } | 
|---|
|  | 199 |  | 
|---|
| [3738f0] | 200 | template <class container_type, | 
|---|
|  | 201 | class iterator_type, | 
|---|
|  | 202 | class const_iterator_type> | 
|---|
|  | 203 | inline Vector AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalMomentumAtStep(unsigned int step) const{ | 
|---|
| [6625c3] | 204 | return accumulate(this->begin(),this->end(),stepValueSum<Vector>(step,&AtomInfo::getMomentum,Vector())).value; | 
|---|
| [ddc85b] | 205 | } | 
|---|
|  | 206 |  | 
|---|
| [0477b0] | 207 | template <class container_type, | 
|---|
|  | 208 | class iterator_type, | 
|---|
|  | 209 | class const_iterator_type> | 
|---|
|  | 210 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::sortByIds(){ | 
|---|
|  | 211 | std::sort(this->begin(), this->end(), | 
|---|
|  | 212 | boost::bind(std::less<atomId_t>(), | 
|---|
|  | 213 | boost::bind(&atom::getId, _1), | 
|---|
|  | 214 | boost::bind(&atom::getId, _2))); | 
|---|
|  | 215 | } | 
|---|
|  | 216 |  | 
|---|
| [5a5c47] | 217 | // allows simpler definition of AtomSets | 
|---|
|  | 218 | #define ATOMSET(container_type) AtomSetMixin<container_type<atom*> > | 
|---|
| [795c0f] | 219 | #define CONSTATOMSET(container_type) AtomSetMixin<container_type<const atom*> > | 
|---|
| [5a5c47] | 220 |  | 
|---|
|  | 221 | #endif /* ATOMSET_HPP_ */ | 
|---|