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 <class Set>
|
---|
42 | class AtomSetMixin : public Set
|
---|
43 | {
|
---|
44 | // when our set carries something besides a atom* this will produce an error
|
---|
45 | typedef typename is_atom<typename Set::value_type>::wrong_type check_for_atom;
|
---|
46 | public:
|
---|
47 | // typedefs for STL conforming structure
|
---|
48 | typedef typename Set::iterator iterator;
|
---|
49 | typedef typename Set::const_iterator const_iterator;
|
---|
50 |
|
---|
51 | AtomSetMixin() :
|
---|
52 | Set()
|
---|
53 | {}
|
---|
54 |
|
---|
55 | AtomSetMixin(const Set& src) :
|
---|
56 | Set(src)
|
---|
57 | {}
|
---|
58 | virtual ~AtomSetMixin(){}
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * translate all Atoms within this set by a specified amount
|
---|
62 | */
|
---|
63 | void translate(const Vector &translater);
|
---|
64 | void addVelocityAtStep(const Vector velocity, unsigned int step);
|
---|
65 |
|
---|
66 | template<class Function>
|
---|
67 | void transformNodes(Function f);
|
---|
68 | double totalMass() const;
|
---|
69 | double totalTemperatureAtStep(unsigned int step) const;
|
---|
70 | Vector totalMomentumAtStep(unsigned int step) const;
|
---|
71 |
|
---|
72 | private:
|
---|
73 | template<class Function>
|
---|
74 | struct workOnNodePointer {
|
---|
75 | workOnNodePointer(Function &_f) : f(_f){}
|
---|
76 | void operator()(atom *atom){
|
---|
77 | atom->setPosition(f(atom->getPosition()));
|
---|
78 | }
|
---|
79 | Function &f;
|
---|
80 | };
|
---|
81 |
|
---|
82 | template<class T>
|
---|
83 | struct valueSum {
|
---|
84 | valueSum(T (atom::*_f)() const,T startValue) :
|
---|
85 | f(_f),
|
---|
86 | value(startValue)
|
---|
87 | {}
|
---|
88 | T operator+(atom *atom){
|
---|
89 | return value + (atom->*f)();
|
---|
90 | }
|
---|
91 | T operator=(T _value){
|
---|
92 | value = _value;
|
---|
93 | return value;
|
---|
94 | }
|
---|
95 | T (atom::*f)() const;
|
---|
96 | T value;
|
---|
97 | };
|
---|
98 |
|
---|
99 | template<class T>
|
---|
100 | struct stepValueSum {
|
---|
101 | stepValueSum(unsigned int _step, T (atom::*_f)(unsigned int) const,T startValue) :
|
---|
102 | step(_step),
|
---|
103 | f(_f),
|
---|
104 | value(startValue)
|
---|
105 | {}
|
---|
106 | T operator+(atom *atom){
|
---|
107 | return value + (atom->*f)(step);
|
---|
108 | }
|
---|
109 | T operator=(T _value){
|
---|
110 | value = _value;
|
---|
111 | return value;
|
---|
112 | }
|
---|
113 | unsigned int step;
|
---|
114 | T (atom::*f)(unsigned int) const;
|
---|
115 | T value;
|
---|
116 | };
|
---|
117 | };
|
---|
118 |
|
---|
119 | template<class Set>
|
---|
120 | inline void AtomSetMixin<Set>::translate(const Vector &translater){
|
---|
121 | BOOST_FOREACH(atom *atom,*this){
|
---|
122 | *(atom) += translater;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | template<class Set>
|
---|
127 | inline void AtomSetMixin<Set>::addVelocityAtStep(const Vector velocity, unsigned int step){
|
---|
128 | BOOST_FOREACH(atom *atom,*this){
|
---|
129 | atom->Trajectory.U.at(step) += velocity;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | template<class Set>
|
---|
134 | template<class Function>
|
---|
135 | inline void AtomSetMixin<Set>::transformNodes(Function f){
|
---|
136 | std::for_each(this->begin(),
|
---|
137 | this->end(),
|
---|
138 | AtomSetMixin::workOnNodePointer<Function>(f));
|
---|
139 | }
|
---|
140 |
|
---|
141 | template<class Set>
|
---|
142 | inline double AtomSetMixin<Set>::totalMass() const{
|
---|
143 | return accumulate(this->begin(),this->end(),valueSum<double>(&atom::getMass,0)).value;
|
---|
144 | }
|
---|
145 |
|
---|
146 | template<class Set>
|
---|
147 | inline double AtomSetMixin<Set>::totalTemperatureAtStep(unsigned int step) const{
|
---|
148 | return accumulate(this->begin(),this->end(),stepValueSum<double>(step,&atom::getKineticEnergy,0)).value;
|
---|
149 | }
|
---|
150 |
|
---|
151 | template<class Set>
|
---|
152 | inline Vector AtomSetMixin<Set>::totalMomentumAtStep(unsigned int step) const{
|
---|
153 | return accumulate(this->begin(),this->end(),stepValueSum<Vector>(step,&atom::getMomentum,Vector())).value;
|
---|
154 | }
|
---|
155 |
|
---|
156 | // allows simpler definition of AtomSets
|
---|
157 | #define ATOMSET(container_type) AtomSetMixin<container_type<atom*> >
|
---|
158 |
|
---|
159 | #endif /* ATOMSET_HPP_ */
|
---|