| [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;
 | 
|---|
| [d40189] | 79 |   Vector totalForceAtStep(unsigned int step) const;
 | 
|---|
| [5e99bc] | 80 |   Vector totalMomentumAtStep(unsigned int step) const;
 | 
|---|
| [d40189] | 81 |   Vector totalAbsoluteMomentumAtStep(unsigned int step) const;
 | 
|---|
 | 82 |   Vector totalAbsoluteForceAtStep(unsigned int step) const;
 | 
|---|
| [a33ea5] | 83 | 
 | 
|---|
| [8009ce] | 84 |   size_t getMaxTrajectorySize() const;
 | 
|---|
 | 85 | 
 | 
|---|
| [0477b0] | 86 |   void sortByIds();
 | 
|---|
 | 87 | 
 | 
|---|
| [a33ea5] | 88 | private:
 | 
|---|
 | 89 |   template<class Function>
 | 
|---|
 | 90 |   struct workOnNodePointer {
 | 
|---|
 | 91 |     workOnNodePointer(Function &_f) : f(_f){}
 | 
|---|
 | 92 |     void operator()(atom *atom){
 | 
|---|
| [8f4df1] | 93 |       atom->setPosition(f(atom->getPosition()));
 | 
|---|
| [5a5c47] | 94 |     }
 | 
|---|
| [a33ea5] | 95 |     Function &f;
 | 
|---|
 | 96 |   };
 | 
|---|
| [ddc85b] | 97 | 
 | 
|---|
| [cddda7] | 98 |   template<class T>
 | 
|---|
 | 99 |   struct valueSum {
 | 
|---|
| [6625c3] | 100 |     valueSum(T (AtomInfo::*_f)() const,T startValue) :
 | 
|---|
| [cddda7] | 101 |       f(_f),
 | 
|---|
| [5e99bc] | 102 |       value(startValue)
 | 
|---|
| [cddda7] | 103 |     {}
 | 
|---|
| [f01769] | 104 |     T operator+(const AtomInfo *atom){
 | 
|---|
| [cddda7] | 105 |       return value + (atom->*f)();
 | 
|---|
| [ddc85b] | 106 |     }
 | 
|---|
| [5e99bc] | 107 |     T operator=(T _value){
 | 
|---|
| [cddda7] | 108 |       value = _value;
 | 
|---|
 | 109 |       return value;
 | 
|---|
 | 110 |     }
 | 
|---|
| [6625c3] | 111 |     T (AtomInfo::*f)() const;
 | 
|---|
| [cddda7] | 112 |     T value;
 | 
|---|
 | 113 |   };
 | 
|---|
 | 114 | 
 | 
|---|
| [d40189] | 115 |   template<class T>
 | 
|---|
 | 116 |   struct absValueSum {
 | 
|---|
 | 117 |     absValueSum(T (AtomInfo::*_f)() const,T startValue) :
 | 
|---|
 | 118 |       f(_f),
 | 
|---|
 | 119 |       value(startValue)
 | 
|---|
 | 120 |     {}
 | 
|---|
 | 121 |     T operator+(const AtomInfo *atom){
 | 
|---|
 | 122 |       temp = (atom->*f)();
 | 
|---|
 | 123 |       for (int i=0;i<NDIM;++i)
 | 
|---|
 | 124 |         temp[i] = fabs(temp[i]);
 | 
|---|
 | 125 |       return value + temp;
 | 
|---|
 | 126 |     }
 | 
|---|
 | 127 |     T operator=(T _value){
 | 
|---|
 | 128 |       value = _value;
 | 
|---|
 | 129 |       for (int i=0;i<NDIM;++i)
 | 
|---|
 | 130 |         value[i] = fabs(value[i]);
 | 
|---|
 | 131 |       return value;
 | 
|---|
 | 132 |     }
 | 
|---|
 | 133 |     T (AtomInfo::*f)() const;
 | 
|---|
 | 134 |     T value;
 | 
|---|
 | 135 |     T temp;
 | 
|---|
 | 136 |   };
 | 
|---|
 | 137 | 
 | 
|---|
| [8009ce] | 138 |   template<class T>
 | 
|---|
 | 139 |   struct valueMax {
 | 
|---|
 | 140 |     valueMax(T (AtomInfo::*_f)() const,T startValue) :
 | 
|---|
 | 141 |       f(_f),
 | 
|---|
 | 142 |       value(startValue)
 | 
|---|
 | 143 |     {}
 | 
|---|
| [f01769] | 144 |     T operator+(const AtomInfo *atom){
 | 
|---|
| [8009ce] | 145 |       const T temp = (atom->*f)();
 | 
|---|
 | 146 |       return value < temp ? temp : value;
 | 
|---|
 | 147 |     }
 | 
|---|
 | 148 |     T operator=(T _value){
 | 
|---|
 | 149 |       value = _value;
 | 
|---|
 | 150 |       return value;
 | 
|---|
 | 151 |     }
 | 
|---|
 | 152 |     T (AtomInfo::*f)() const;
 | 
|---|
 | 153 |     T value;
 | 
|---|
 | 154 |   };
 | 
|---|
 | 155 | 
 | 
|---|
| [cddda7] | 156 |   template<class T>
 | 
|---|
 | 157 |   struct stepValueSum {
 | 
|---|
| [6625c3] | 158 |     stepValueSum(unsigned int _step, T (AtomInfo::*_f)(unsigned int) const,T startValue) :
 | 
|---|
| [cddda7] | 159 |       step(_step),
 | 
|---|
 | 160 |       f(_f),
 | 
|---|
| [5e99bc] | 161 |       value(startValue)
 | 
|---|
| [cddda7] | 162 |     {}
 | 
|---|
| [f01769] | 163 |     T operator+(const AtomInfo *atom){
 | 
|---|
| [cddda7] | 164 |       return value + (atom->*f)(step);
 | 
|---|
 | 165 |     }
 | 
|---|
| [5e99bc] | 166 |     T operator=(T _value){
 | 
|---|
| [ddc85b] | 167 |       value = _value;
 | 
|---|
 | 168 |       return value;
 | 
|---|
 | 169 |     }
 | 
|---|
 | 170 |     unsigned int step;
 | 
|---|
| [6625c3] | 171 |     T (AtomInfo::*f)(unsigned int) const;
 | 
|---|
| [cddda7] | 172 |     T value;
 | 
|---|
| [ddc85b] | 173 |   };
 | 
|---|
| [d40189] | 174 | 
 | 
|---|
 | 175 |   template<class T>
 | 
|---|
 | 176 |   struct stepAbsValueSum {
 | 
|---|
 | 177 |     stepAbsValueSum(unsigned int _step, T (AtomInfo::*_f)(unsigned int) const,T startValue) :
 | 
|---|
 | 178 |       step(_step),
 | 
|---|
 | 179 |       f(_f),
 | 
|---|
 | 180 |       value(startValue)
 | 
|---|
 | 181 |     {}
 | 
|---|
 | 182 |     T operator+(const AtomInfo *atom){
 | 
|---|
 | 183 |       temp = (atom->*f)(step);
 | 
|---|
 | 184 |       for (int i=0;i<NDIM;++i)
 | 
|---|
 | 185 |         temp[i] = fabs(temp[i]);
 | 
|---|
 | 186 |       return value + temp;
 | 
|---|
 | 187 |     }
 | 
|---|
 | 188 |     T operator=(T _value){
 | 
|---|
 | 189 |       value = _value;
 | 
|---|
 | 190 |       for (int i=0;i<NDIM;++i)
 | 
|---|
 | 191 |         value[i] = fabs(value[i]);
 | 
|---|
 | 192 |       return value;
 | 
|---|
 | 193 |     }
 | 
|---|
 | 194 |     unsigned int step;
 | 
|---|
 | 195 |     T (AtomInfo::*f)(unsigned int) const;
 | 
|---|
 | 196 |     T value;
 | 
|---|
 | 197 |     T temp;
 | 
|---|
 | 198 |   };
 | 
|---|
| [5a5c47] | 199 | };
 | 
|---|
 | 200 | 
 | 
|---|
| [3738f0] | 201 | template <class container_type,
 | 
|---|
 | 202 |           class iterator_type,
 | 
|---|
 | 203 |           class const_iterator_type>
 | 
|---|
 | 204 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::translate(const Vector &translater){
 | 
|---|
| [6625c3] | 205 |   BOOST_FOREACH(AtomInfo *atom,*this){
 | 
|---|
| [8f4df1] | 206 |     *(atom) += translater;
 | 
|---|
| [a33ea5] | 207 |   }
 | 
|---|
 | 208 | }
 | 
|---|
 | 209 | 
 | 
|---|
| [3738f0] | 210 | template <class container_type,
 | 
|---|
 | 211 |           class iterator_type,
 | 
|---|
 | 212 |           class const_iterator_type>
 | 
|---|
 | 213 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::addVelocityAtStep(const Vector velocity, unsigned int step){
 | 
|---|
| [6625c3] | 214 |   BOOST_FOREACH(AtomInfo *atom,*this){
 | 
|---|
| [056e70] | 215 |     atom->setAtomicVelocityAtStep(step, atom->getAtomicVelocityAtStep(step)+velocity);
 | 
|---|
| [5ac690] | 216 |   }
 | 
|---|
 | 217 | }
 | 
|---|
 | 218 | 
 | 
|---|
| [3738f0] | 219 | template <class container_type,
 | 
|---|
 | 220 |           class iterator_type,
 | 
|---|
 | 221 |           class const_iterator_type>
 | 
|---|
| [a33ea5] | 222 | template<class Function>
 | 
|---|
| [3738f0] | 223 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::transformNodes(Function f){
 | 
|---|
| [a33ea5] | 224 |   std::for_each(this->begin(),
 | 
|---|
 | 225 |                 this->end(),
 | 
|---|
 | 226 |                 AtomSetMixin::workOnNodePointer<Function>(f));
 | 
|---|
 | 227 | }
 | 
|---|
 | 228 | 
 | 
|---|
| [3738f0] | 229 | template <class container_type,
 | 
|---|
 | 230 |           class iterator_type,
 | 
|---|
 | 231 |           class const_iterator_type>
 | 
|---|
 | 232 | inline double AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalMass() const{
 | 
|---|
| [6625c3] | 233 |   return accumulate(this->begin(),this->end(),valueSum<double>(&AtomInfo::getMass,0)).value;
 | 
|---|
| [5e99bc] | 234 | }
 | 
|---|
 | 235 | 
 | 
|---|
| [8009ce] | 236 | template <class container_type,
 | 
|---|
 | 237 |           class iterator_type,
 | 
|---|
 | 238 |           class const_iterator_type>
 | 
|---|
 | 239 | inline size_t AtomSetMixin<container_type,iterator_type,const_iterator_type>::getMaxTrajectorySize() const
 | 
|---|
 | 240 | {
 | 
|---|
 | 241 |   return accumulate(this->begin(),this->end(),valueMax<size_t>(&AtomInfo::getTrajectorySize,(size_t)1)).value;
 | 
|---|
 | 242 | }
 | 
|---|
 | 243 | 
 | 
|---|
| [3738f0] | 244 | template <class container_type,
 | 
|---|
 | 245 |           class iterator_type,
 | 
|---|
 | 246 |           class const_iterator_type>
 | 
|---|
 | 247 | inline double AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalTemperatureAtStep(unsigned int step) const{
 | 
|---|
| [6625c3] | 248 |   return accumulate(this->begin(),this->end(),stepValueSum<double>(step,&AtomInfo::getKineticEnergy,0)).value;
 | 
|---|
| [cddda7] | 249 | }
 | 
|---|
 | 250 | 
 | 
|---|
| [3738f0] | 251 | template <class container_type,
 | 
|---|
 | 252 |           class iterator_type,
 | 
|---|
 | 253 |           class const_iterator_type>
 | 
|---|
 | 254 | inline Vector AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalMomentumAtStep(unsigned int step) const{
 | 
|---|
| [6625c3] | 255 |   return accumulate(this->begin(),this->end(),stepValueSum<Vector>(step,&AtomInfo::getMomentum,Vector())).value;
 | 
|---|
| [ddc85b] | 256 | }
 | 
|---|
 | 257 | 
 | 
|---|
| [d40189] | 258 | template <class container_type,
 | 
|---|
 | 259 |           class iterator_type,
 | 
|---|
 | 260 |           class const_iterator_type>
 | 
|---|
 | 261 | inline Vector AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalAbsoluteMomentumAtStep(unsigned int step) const{
 | 
|---|
 | 262 |   return accumulate(this->begin(),this->end(),stepAbsValueSum<Vector>(step,&AtomInfo::getMomentum,Vector())).value;
 | 
|---|
 | 263 | }
 | 
|---|
 | 264 | 
 | 
|---|
 | 265 | template <class container_type,
 | 
|---|
 | 266 |           class iterator_type,
 | 
|---|
 | 267 |           class const_iterator_type>
 | 
|---|
 | 268 | inline Vector AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalForceAtStep(unsigned int step) const{
 | 
|---|
 | 269 |   return accumulate(this->begin(),this->end(),stepValueSum<Vector>(step,&AtomInfo::getAcceleration,Vector())).value;
 | 
|---|
 | 270 | }
 | 
|---|
 | 271 | 
 | 
|---|
 | 272 | template <class container_type,
 | 
|---|
 | 273 |           class iterator_type,
 | 
|---|
 | 274 |           class const_iterator_type>
 | 
|---|
 | 275 | inline Vector AtomSetMixin<container_type,iterator_type,const_iterator_type>::totalAbsoluteForceAtStep(unsigned int step) const{
 | 
|---|
 | 276 |   return accumulate(this->begin(),this->end(),stepAbsValueSum<Vector>(step,&AtomInfo::getAcceleration,Vector())).value;
 | 
|---|
 | 277 | }
 | 
|---|
 | 278 | 
 | 
|---|
| [0477b0] | 279 | template <class container_type,
 | 
|---|
 | 280 |           class iterator_type,
 | 
|---|
 | 281 |           class const_iterator_type>
 | 
|---|
 | 282 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::sortByIds(){
 | 
|---|
 | 283 |   std::sort(this->begin(), this->end(),
 | 
|---|
 | 284 |       boost::bind(std::less<atomId_t>(),
 | 
|---|
 | 285 |           boost::bind(&atom::getId, _1),
 | 
|---|
 | 286 |           boost::bind(&atom::getId, _2)));
 | 
|---|
 | 287 | }
 | 
|---|
 | 288 | 
 | 
|---|
| [5a5c47] | 289 | // allows simpler definition of AtomSets
 | 
|---|
 | 290 | #define ATOMSET(container_type) AtomSetMixin<container_type<atom*> >
 | 
|---|
| [795c0f] | 291 | #define CONSTATOMSET(container_type) AtomSetMixin<container_type<const atom*> >
 | 
|---|
| [5a5c47] | 292 | 
 | 
|---|
 | 293 | #endif /* ATOMSET_HPP_ */
 | 
|---|