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