| [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 | 
 | 
|---|
| [795c0f] | 41 | template <>
 | 
|---|
 | 42 | struct is_atom<const atom*>{
 | 
|---|
 | 43 |   typedef void wrong_type;
 | 
|---|
 | 44 | };
 | 
|---|
 | 45 | 
 | 
|---|
| [3738f0] | 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
 | 
|---|
| [5a5c47] | 50 | {
 | 
|---|
 | 51 |   // when our set carries something besides a atom* this will produce an error
 | 
|---|
| [3738f0] | 52 |   typedef typename is_atom<typename container_type::value_type>::wrong_type check_for_atom;
 | 
|---|
| [5a5c47] | 53 | public:
 | 
|---|
 | 54 |   // typedefs for STL conforming structure
 | 
|---|
| [3738f0] | 55 |   typedef iterator_type iterator;
 | 
|---|
 | 56 |   typedef const_iterator_type const_iterator;
 | 
|---|
| [5a5c47] | 57 | 
 | 
|---|
 | 58 |   AtomSetMixin() :
 | 
|---|
| [3738f0] | 59 |     container_type()
 | 
|---|
| [5a5c47] | 60 |   {}
 | 
|---|
 | 61 | 
 | 
|---|
| [3738f0] | 62 |   AtomSetMixin(const container_type& src) :
 | 
|---|
 | 63 |     container_type(src)
 | 
|---|
| [5a5c47] | 64 |   {}
 | 
|---|
 | 65 |   virtual ~AtomSetMixin(){}
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 |   /**
 | 
|---|
 | 68 |    * translate all Atoms within this set by a specified amount
 | 
|---|
 | 69 |    */
 | 
|---|
| [a33ea5] | 70 |   void translate(const Vector &translater);
 | 
|---|
| [5ac690] | 71 |   void addVelocityAtStep(const Vector velocity, unsigned int step);
 | 
|---|
| [a33ea5] | 72 | 
 | 
|---|
 | 73 |   template<class Function>
 | 
|---|
 | 74 |   void transformNodes(Function f);
 | 
|---|
| [cddda7] | 75 |   double totalMass() const;
 | 
|---|
| [5e99bc] | 76 |   double totalTemperatureAtStep(unsigned int step) const;
 | 
|---|
 | 77 |   Vector totalMomentumAtStep(unsigned int step) const;
 | 
|---|
| [a33ea5] | 78 | 
 | 
|---|
| [8009ce] | 79 |   size_t getMaxTrajectorySize() const;
 | 
|---|
 | 80 | 
 | 
|---|
| [a33ea5] | 81 | private:
 | 
|---|
 | 82 |   template<class Function>
 | 
|---|
 | 83 |   struct workOnNodePointer {
 | 
|---|
 | 84 |     workOnNodePointer(Function &_f) : f(_f){}
 | 
|---|
 | 85 |     void operator()(atom *atom){
 | 
|---|
| [8f4df1] | 86 |       atom->setPosition(f(atom->getPosition()));
 | 
|---|
| [5a5c47] | 87 |     }
 | 
|---|
| [a33ea5] | 88 |     Function &f;
 | 
|---|
 | 89 |   };
 | 
|---|
| [ddc85b] | 90 | 
 | 
|---|
| [cddda7] | 91 |   template<class T>
 | 
|---|
 | 92 |   struct valueSum {
 | 
|---|
| [6625c3] | 93 |     valueSum(T (AtomInfo::*_f)() const,T startValue) :
 | 
|---|
| [cddda7] | 94 |       f(_f),
 | 
|---|
| [5e99bc] | 95 |       value(startValue)
 | 
|---|
| [cddda7] | 96 |     {}
 | 
|---|
| [f01769] | 97 |     T operator+(const AtomInfo *atom){
 | 
|---|
| [cddda7] | 98 |       return value + (atom->*f)();
 | 
|---|
| [ddc85b] | 99 |     }
 | 
|---|
| [5e99bc] | 100 |     T operator=(T _value){
 | 
|---|
| [cddda7] | 101 |       value = _value;
 | 
|---|
 | 102 |       return value;
 | 
|---|
 | 103 |     }
 | 
|---|
| [6625c3] | 104 |     T (AtomInfo::*f)() const;
 | 
|---|
| [cddda7] | 105 |     T value;
 | 
|---|
 | 106 |   };
 | 
|---|
 | 107 | 
 | 
|---|
| [8009ce] | 108 |   template<class T>
 | 
|---|
 | 109 |   struct valueMax {
 | 
|---|
 | 110 |     valueMax(T (AtomInfo::*_f)() const,T startValue) :
 | 
|---|
 | 111 |       f(_f),
 | 
|---|
 | 112 |       value(startValue)
 | 
|---|
 | 113 |     {}
 | 
|---|
| [f01769] | 114 |     T operator+(const AtomInfo *atom){
 | 
|---|
| [8009ce] | 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 | 
 | 
|---|
| [cddda7] | 126 |   template<class T>
 | 
|---|
 | 127 |   struct stepValueSum {
 | 
|---|
| [6625c3] | 128 |     stepValueSum(unsigned int _step, T (AtomInfo::*_f)(unsigned int) const,T startValue) :
 | 
|---|
| [cddda7] | 129 |       step(_step),
 | 
|---|
 | 130 |       f(_f),
 | 
|---|
| [5e99bc] | 131 |       value(startValue)
 | 
|---|
| [cddda7] | 132 |     {}
 | 
|---|
| [f01769] | 133 |     T operator+(const AtomInfo *atom){
 | 
|---|
| [cddda7] | 134 |       return value + (atom->*f)(step);
 | 
|---|
 | 135 |     }
 | 
|---|
| [5e99bc] | 136 |     T operator=(T _value){
 | 
|---|
| [ddc85b] | 137 |       value = _value;
 | 
|---|
 | 138 |       return value;
 | 
|---|
 | 139 |     }
 | 
|---|
 | 140 |     unsigned int step;
 | 
|---|
| [6625c3] | 141 |     T (AtomInfo::*f)(unsigned int) const;
 | 
|---|
| [cddda7] | 142 |     T value;
 | 
|---|
| [ddc85b] | 143 |   };
 | 
|---|
| [5a5c47] | 144 | };
 | 
|---|
 | 145 | 
 | 
|---|
| [3738f0] | 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){
 | 
|---|
| [6625c3] | 150 |   BOOST_FOREACH(AtomInfo *atom,*this){
 | 
|---|
| [8f4df1] | 151 |     *(atom) += translater;
 | 
|---|
| [a33ea5] | 152 |   }
 | 
|---|
 | 153 | }
 | 
|---|
 | 154 | 
 | 
|---|
| [3738f0] | 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){
 | 
|---|
| [6625c3] | 159 |   BOOST_FOREACH(AtomInfo *atom,*this){
 | 
|---|
| [056e70] | 160 |     atom->setAtomicVelocityAtStep(step, atom->getAtomicVelocityAtStep(step)+velocity);
 | 
|---|
| [5ac690] | 161 |   }
 | 
|---|
 | 162 | }
 | 
|---|
 | 163 | 
 | 
|---|
| [3738f0] | 164 | template <class container_type,
 | 
|---|
 | 165 |           class iterator_type,
 | 
|---|
 | 166 |           class const_iterator_type>
 | 
|---|
| [a33ea5] | 167 | template<class Function>
 | 
|---|
| [3738f0] | 168 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::transformNodes(Function f){
 | 
|---|
| [a33ea5] | 169 |   std::for_each(this->begin(),
 | 
|---|
 | 170 |                 this->end(),
 | 
|---|
 | 171 |                 AtomSetMixin::workOnNodePointer<Function>(f));
 | 
|---|
 | 172 | }
 | 
|---|
 | 173 | 
 | 
|---|
| [3738f0] | 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{
 | 
|---|
| [6625c3] | 178 |   return accumulate(this->begin(),this->end(),valueSum<double>(&AtomInfo::getMass,0)).value;
 | 
|---|
| [5e99bc] | 179 | }
 | 
|---|
 | 180 | 
 | 
|---|
| [8009ce] | 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 | 
 | 
|---|
| [3738f0] | 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{
 | 
|---|
| [6625c3] | 193 |   return accumulate(this->begin(),this->end(),stepValueSum<double>(step,&AtomInfo::getKineticEnergy,0)).value;
 | 
|---|
| [cddda7] | 194 | }
 | 
|---|
 | 195 | 
 | 
|---|
| [3738f0] | 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{
 | 
|---|
| [6625c3] | 200 |   return accumulate(this->begin(),this->end(),stepValueSum<Vector>(step,&AtomInfo::getMomentum,Vector())).value;
 | 
|---|
| [ddc85b] | 201 | }
 | 
|---|
 | 202 | 
 | 
|---|
| [5a5c47] | 203 | // allows simpler definition of AtomSets
 | 
|---|
 | 204 | #define ATOMSET(container_type) AtomSetMixin<container_type<atom*> >
 | 
|---|
| [795c0f] | 205 | #define CONSTATOMSET(container_type) AtomSetMixin<container_type<const atom*> >
 | 
|---|
| [5a5c47] | 206 | 
 | 
|---|
 | 207 | #endif /* ATOMSET_HPP_ */
 | 
|---|