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 <algorithm>
|
---|
19 | #include <functional>
|
---|
20 | #include <limits>
|
---|
21 | #include <numeric>
|
---|
22 |
|
---|
23 | #include <boost/bind.hpp>
|
---|
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 |
|
---|
43 | template <>
|
---|
44 | struct is_atom<const atom*>{
|
---|
45 | typedef void wrong_type;
|
---|
46 | };
|
---|
47 |
|
---|
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
|
---|
52 | {
|
---|
53 | // when our set carries something besides a atom* this will produce an error
|
---|
54 | typedef typename is_atom<typename container_type::value_type>::wrong_type check_for_atom;
|
---|
55 | public:
|
---|
56 | // typedefs for STL conforming structure
|
---|
57 | typedef iterator_type iterator;
|
---|
58 | typedef const_iterator_type const_iterator;
|
---|
59 |
|
---|
60 | AtomSetMixin() :
|
---|
61 | container_type()
|
---|
62 | {}
|
---|
63 |
|
---|
64 | AtomSetMixin(const container_type& src) :
|
---|
65 | container_type(src)
|
---|
66 | {}
|
---|
67 | virtual ~AtomSetMixin(){}
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * translate all Atoms within this set by a specified amount
|
---|
71 | */
|
---|
72 | void translate(const Vector &translater);
|
---|
73 | void addVelocityAtStep(const Vector velocity, unsigned int step);
|
---|
74 |
|
---|
75 | template<class Function>
|
---|
76 | void transformNodes(Function f);
|
---|
77 | double totalMass() const;
|
---|
78 | double totalTemperatureAtStep(unsigned int step) const;
|
---|
79 | Vector totalMomentumAtStep(unsigned int step) const;
|
---|
80 |
|
---|
81 | size_t getMaxTrajectorySize() const;
|
---|
82 |
|
---|
83 | void sortByIds();
|
---|
84 |
|
---|
85 | private:
|
---|
86 | template<class Function>
|
---|
87 | struct workOnNodePointer {
|
---|
88 | workOnNodePointer(Function &_f) : f(_f){}
|
---|
89 | void operator()(atom *atom){
|
---|
90 | atom->setPosition(f(atom->getPosition()));
|
---|
91 | }
|
---|
92 | Function &f;
|
---|
93 | };
|
---|
94 |
|
---|
95 | template<class T>
|
---|
96 | struct valueSum {
|
---|
97 | valueSum(T (AtomInfo::*_f)() const,T startValue) :
|
---|
98 | f(_f),
|
---|
99 | value(startValue)
|
---|
100 | {}
|
---|
101 | T operator+(const AtomInfo *atom){
|
---|
102 | return value + (atom->*f)();
|
---|
103 | }
|
---|
104 | T operator=(T _value){
|
---|
105 | value = _value;
|
---|
106 | return value;
|
---|
107 | }
|
---|
108 | T (AtomInfo::*f)() const;
|
---|
109 | T value;
|
---|
110 | };
|
---|
111 |
|
---|
112 | template<class T>
|
---|
113 | struct valueMax {
|
---|
114 | valueMax(T (AtomInfo::*_f)() const,T startValue) :
|
---|
115 | f(_f),
|
---|
116 | value(startValue)
|
---|
117 | {}
|
---|
118 | T operator+(const AtomInfo *atom){
|
---|
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 |
|
---|
130 | template<class T>
|
---|
131 | struct stepValueSum {
|
---|
132 | stepValueSum(unsigned int _step, T (AtomInfo::*_f)(unsigned int) const,T startValue) :
|
---|
133 | step(_step),
|
---|
134 | f(_f),
|
---|
135 | value(startValue)
|
---|
136 | {}
|
---|
137 | T operator+(const AtomInfo *atom){
|
---|
138 | return value + (atom->*f)(step);
|
---|
139 | }
|
---|
140 | T operator=(T _value){
|
---|
141 | value = _value;
|
---|
142 | return value;
|
---|
143 | }
|
---|
144 | unsigned int step;
|
---|
145 | T (AtomInfo::*f)(unsigned int) const;
|
---|
146 | T value;
|
---|
147 | };
|
---|
148 | };
|
---|
149 |
|
---|
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){
|
---|
154 | BOOST_FOREACH(AtomInfo *atom,*this){
|
---|
155 | *(atom) += translater;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
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){
|
---|
163 | BOOST_FOREACH(AtomInfo *atom,*this){
|
---|
164 | atom->setAtomicVelocityAtStep(step, atom->getAtomicVelocityAtStep(step)+velocity);
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | template <class container_type,
|
---|
169 | class iterator_type,
|
---|
170 | class const_iterator_type>
|
---|
171 | template<class Function>
|
---|
172 | inline void AtomSetMixin<container_type,iterator_type,const_iterator_type>::transformNodes(Function f){
|
---|
173 | std::for_each(this->begin(),
|
---|
174 | this->end(),
|
---|
175 | AtomSetMixin::workOnNodePointer<Function>(f));
|
---|
176 | }
|
---|
177 |
|
---|
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{
|
---|
182 | return accumulate(this->begin(),this->end(),valueSum<double>(&AtomInfo::getMass,0)).value;
|
---|
183 | }
|
---|
184 |
|
---|
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 |
|
---|
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{
|
---|
197 | return accumulate(this->begin(),this->end(),stepValueSum<double>(step,&AtomInfo::getKineticEnergy,0)).value;
|
---|
198 | }
|
---|
199 |
|
---|
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{
|
---|
204 | return accumulate(this->begin(),this->end(),stepValueSum<Vector>(step,&AtomInfo::getMomentum,Vector())).value;
|
---|
205 | }
|
---|
206 |
|
---|
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 |
|
---|
217 | // allows simpler definition of AtomSets
|
---|
218 | #define ATOMSET(container_type) AtomSetMixin<container_type<atom*> >
|
---|
219 | #define CONSTATOMSET(container_type) AtomSetMixin<container_type<const atom*> >
|
---|
220 |
|
---|
221 | #endif /* ATOMSET_HPP_ */
|
---|