| [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 |  | 
|---|
|  | 18 | #include <functional> | 
|---|
| [ddc85b] | 19 | #include <numeric> | 
|---|
| [5a5c47] | 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 |  | 
|---|
| [3738f0] | 41 | template <class container_type, | 
|---|
|  | 42 | class iterator_type = typename container_type::iterator, | 
|---|
|  | 43 | class const_iterator_type = typename container_type::const_iterator> | 
|---|
|  | 44 | class AtomSetMixin : public container_type | 
|---|
| [5a5c47] | 45 | { | 
|---|
|  | 46 | // when our set carries something besides a atom* this will produce an error | 
|---|
| [3738f0] | 47 | typedef typename is_atom<typename container_type::value_type>::wrong_type check_for_atom; | 
|---|
| [5a5c47] | 48 | public: | 
|---|
|  | 49 | // typedefs for STL conforming structure | 
|---|
| [3738f0] | 50 | typedef iterator_type iterator; | 
|---|
|  | 51 | typedef const_iterator_type const_iterator; | 
|---|
| [5a5c47] | 52 |  | 
|---|
|  | 53 | AtomSetMixin() : | 
|---|
| [3738f0] | 54 | container_type() | 
|---|
| [5a5c47] | 55 | {} | 
|---|
|  | 56 |  | 
|---|
| [3738f0] | 57 | AtomSetMixin(const container_type& src) : | 
|---|
|  | 58 | container_type(src) | 
|---|
| [5a5c47] | 59 | {} | 
|---|
|  | 60 | virtual ~AtomSetMixin(){} | 
|---|
|  | 61 |  | 
|---|
|  | 62 | /** | 
|---|
|  | 63 | * translate all Atoms within this set by a specified amount | 
|---|
|  | 64 | */ | 
|---|
| [a33ea5] | 65 | void translate(const Vector &translater); | 
|---|
| [5ac690] | 66 | void addVelocityAtStep(const Vector velocity, unsigned int step); | 
|---|
| [a33ea5] | 67 |  | 
|---|
|  | 68 | template<class Function> | 
|---|
|  | 69 | void transformNodes(Function f); | 
|---|
| [cddda7] | 70 | double totalMass() const; | 
|---|
| [5e99bc] | 71 | double totalTemperatureAtStep(unsigned int step) const; | 
|---|
|  | 72 | Vector totalMomentumAtStep(unsigned int step) const; | 
|---|
| [a33ea5] | 73 |  | 
|---|
| [8009ce] | 74 | size_t getMaxTrajectorySize() const; | 
|---|
|  | 75 |  | 
|---|
| [a33ea5] | 76 | private: | 
|---|
|  | 77 | template<class Function> | 
|---|
|  | 78 | struct workOnNodePointer { | 
|---|
|  | 79 | workOnNodePointer(Function &_f) : f(_f){} | 
|---|
|  | 80 | void operator()(atom *atom){ | 
|---|
| [8f4df1] | 81 | atom->setPosition(f(atom->getPosition())); | 
|---|
| [5a5c47] | 82 | } | 
|---|
| [a33ea5] | 83 | Function &f; | 
|---|
|  | 84 | }; | 
|---|
| [ddc85b] | 85 |  | 
|---|
| [cddda7] | 86 | template<class T> | 
|---|
|  | 87 | struct valueSum { | 
|---|
| [6625c3] | 88 | valueSum(T (AtomInfo::*_f)() const,T startValue) : | 
|---|
| [cddda7] | 89 | f(_f), | 
|---|
| [5e99bc] | 90 | value(startValue) | 
|---|
| [cddda7] | 91 | {} | 
|---|
| [6625c3] | 92 | T operator+(AtomInfo *atom){ | 
|---|
| [cddda7] | 93 | return value + (atom->*f)(); | 
|---|
| [ddc85b] | 94 | } | 
|---|
| [5e99bc] | 95 | T operator=(T _value){ | 
|---|
| [cddda7] | 96 | value = _value; | 
|---|
|  | 97 | return value; | 
|---|
|  | 98 | } | 
|---|
| [6625c3] | 99 | T (AtomInfo::*f)() const; | 
|---|
| [cddda7] | 100 | T value; | 
|---|
|  | 101 | }; | 
|---|
|  | 102 |  | 
|---|
| [8009ce] | 103 | template<class T> | 
|---|
|  | 104 | struct valueMax { | 
|---|
|  | 105 | valueMax(T (AtomInfo::*_f)() const,T startValue) : | 
|---|
|  | 106 | f(_f), | 
|---|
|  | 107 | value(startValue) | 
|---|
|  | 108 | {} | 
|---|
|  | 109 | T operator+(AtomInfo *atom){ | 
|---|
|  | 110 | const T temp = (atom->*f)(); | 
|---|
|  | 111 | return value < temp ? temp : value; | 
|---|
|  | 112 | } | 
|---|
|  | 113 | T operator=(T _value){ | 
|---|
|  | 114 | value = _value; | 
|---|
|  | 115 | return value; | 
|---|
|  | 116 | } | 
|---|
|  | 117 | T (AtomInfo::*f)() const; | 
|---|
|  | 118 | T value; | 
|---|
|  | 119 | }; | 
|---|
|  | 120 |  | 
|---|
| [cddda7] | 121 | template<class T> | 
|---|
|  | 122 | struct stepValueSum { | 
|---|
| [6625c3] | 123 | stepValueSum(unsigned int _step, T (AtomInfo::*_f)(unsigned int) const,T startValue) : | 
|---|
| [cddda7] | 124 | step(_step), | 
|---|
|  | 125 | f(_f), | 
|---|
| [5e99bc] | 126 | value(startValue) | 
|---|
| [cddda7] | 127 | {} | 
|---|
| [6625c3] | 128 | T operator+(AtomInfo *atom){ | 
|---|
| [cddda7] | 129 | return value + (atom->*f)(step); | 
|---|
|  | 130 | } | 
|---|
| [5e99bc] | 131 | T operator=(T _value){ | 
|---|
| [ddc85b] | 132 | value = _value; | 
|---|
|  | 133 | return value; | 
|---|
|  | 134 | } | 
|---|
|  | 135 | unsigned int step; | 
|---|
| [6625c3] | 136 | T (AtomInfo::*f)(unsigned int) const; | 
|---|
| [cddda7] | 137 | T value; | 
|---|
| [ddc85b] | 138 | }; | 
|---|
| [5a5c47] | 139 | }; | 
|---|
|  | 140 |  | 
|---|
| [3738f0] | 141 | template <class container_type, | 
|---|
|  | 142 | class iterator_type, | 
|---|
|  | 143 | class const_iterator_type> | 
|---|
|  | 144 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::translate(const Vector &translater){ | 
|---|
| [6625c3] | 145 | BOOST_FOREACH(AtomInfo *atom,*this){ | 
|---|
| [8f4df1] | 146 | *(atom) += translater; | 
|---|
| [a33ea5] | 147 | } | 
|---|
|  | 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>::addVelocityAtStep(const Vector velocity, unsigned int step){ | 
|---|
| [6625c3] | 154 | BOOST_FOREACH(AtomInfo *atom,*this){ | 
|---|
| [056e70] | 155 | atom->setAtomicVelocityAtStep(step, atom->getAtomicVelocityAtStep(step)+velocity); | 
|---|
| [5ac690] | 156 | } | 
|---|
|  | 157 | } | 
|---|
|  | 158 |  | 
|---|
| [3738f0] | 159 | template <class container_type, | 
|---|
|  | 160 | class iterator_type, | 
|---|
|  | 161 | class const_iterator_type> | 
|---|
| [a33ea5] | 162 | template<class Function> | 
|---|
| [3738f0] | 163 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::transformNodes(Function f){ | 
|---|
| [a33ea5] | 164 | std::for_each(this->begin(), | 
|---|
|  | 165 | this->end(), | 
|---|
|  | 166 | AtomSetMixin::workOnNodePointer<Function>(f)); | 
|---|
|  | 167 | } | 
|---|
|  | 168 |  | 
|---|
| [3738f0] | 169 | template <class container_type, | 
|---|
|  | 170 | class iterator_type, | 
|---|
|  | 171 | class const_iterator_type> | 
|---|
|  | 172 | inline double AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalMass() const{ | 
|---|
| [6625c3] | 173 | return accumulate(this->begin(),this->end(),valueSum<double>(&AtomInfo::getMass,0)).value; | 
|---|
| [5e99bc] | 174 | } | 
|---|
|  | 175 |  | 
|---|
| [8009ce] | 176 | template <class container_type, | 
|---|
|  | 177 | class iterator_type, | 
|---|
|  | 178 | class const_iterator_type> | 
|---|
|  | 179 | inline size_t AtomSetMixin<container_type,iterator_type,const_iterator_type>::getMaxTrajectorySize() const | 
|---|
|  | 180 | { | 
|---|
|  | 181 | return accumulate(this->begin(),this->end(),valueMax<size_t>(&AtomInfo::getTrajectorySize,(size_t)1)).value; | 
|---|
|  | 182 | } | 
|---|
|  | 183 |  | 
|---|
| [3738f0] | 184 | template <class container_type, | 
|---|
|  | 185 | class iterator_type, | 
|---|
|  | 186 | class const_iterator_type> | 
|---|
|  | 187 | inline double AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalTemperatureAtStep(unsigned int step) const{ | 
|---|
| [6625c3] | 188 | return accumulate(this->begin(),this->end(),stepValueSum<double>(step,&AtomInfo::getKineticEnergy,0)).value; | 
|---|
| [cddda7] | 189 | } | 
|---|
|  | 190 |  | 
|---|
| [3738f0] | 191 | template <class container_type, | 
|---|
|  | 192 | class iterator_type, | 
|---|
|  | 193 | class const_iterator_type> | 
|---|
|  | 194 | inline Vector AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalMomentumAtStep(unsigned int step) const{ | 
|---|
| [6625c3] | 195 | return accumulate(this->begin(),this->end(),stepValueSum<Vector>(step,&AtomInfo::getMomentum,Vector())).value; | 
|---|
| [ddc85b] | 196 | } | 
|---|
|  | 197 |  | 
|---|
| [5a5c47] | 198 | // allows simpler definition of AtomSets | 
|---|
|  | 199 | #define ATOMSET(container_type) AtomSetMixin<container_type<atom*> > | 
|---|
|  | 200 |  | 
|---|
|  | 201 | #endif /* ATOMSET_HPP_ */ | 
|---|