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 |
|
---|
12 | #include <functional>
|
---|
13 | #include <numeric>
|
---|
14 | #include <algorithm>
|
---|
15 | #include <boost/foreach.hpp>
|
---|
16 | #include <limits>
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * A simple mixin to give any STL conforming structure fast Vector abilities
|
---|
20 | *
|
---|
21 | * TODO: make this work for maps
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "atom.hpp"
|
---|
25 |
|
---|
26 | // this tests, whether we actually have a Vector
|
---|
27 | template <class V>
|
---|
28 | struct is_atom{};
|
---|
29 |
|
---|
30 | template <>
|
---|
31 | struct is_atom<atom*>{
|
---|
32 | typedef void wrong_type;
|
---|
33 | };
|
---|
34 |
|
---|
35 | template <class Set>
|
---|
36 | class AtomSetMixin : public Set
|
---|
37 | {
|
---|
38 | // when our set carries something besides a atom* this will produce an error
|
---|
39 | typedef typename is_atom<typename Set::value_type>::wrong_type check_for_atom;
|
---|
40 | public:
|
---|
41 | // typedefs for STL conforming structure
|
---|
42 | typedef typename Set::iterator iterator;
|
---|
43 | typedef typename Set::const_iterator const_iterator;
|
---|
44 |
|
---|
45 | AtomSetMixin() :
|
---|
46 | Set()
|
---|
47 | {}
|
---|
48 |
|
---|
49 | AtomSetMixin(const Set& src) :
|
---|
50 | Set(src)
|
---|
51 | {}
|
---|
52 | virtual ~AtomSetMixin(){}
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * translate all Atoms within this set by a specified amount
|
---|
56 | */
|
---|
57 | void translate(const Vector &translater);
|
---|
58 |
|
---|
59 | template<class Function>
|
---|
60 | void transformNodes(Function f);
|
---|
61 | double totalTemperatureAtStep(unsigned int i) const;
|
---|
62 | double totalMass() const;
|
---|
63 |
|
---|
64 | private:
|
---|
65 | template<class Function>
|
---|
66 | struct workOnNodePointer {
|
---|
67 | workOnNodePointer(Function &_f) : f(_f){}
|
---|
68 | void operator()(atom *atom){
|
---|
69 | atom->setPosition(f(atom->getPosition()));
|
---|
70 | }
|
---|
71 | Function &f;
|
---|
72 | };
|
---|
73 |
|
---|
74 | template<class T>
|
---|
75 | struct valueSum {
|
---|
76 | valueSum(T (atom::*_f)() const) :
|
---|
77 | f(_f),
|
---|
78 | value(0)
|
---|
79 | {}
|
---|
80 | T operator+(atom *atom){
|
---|
81 | return value + (atom->*f)();
|
---|
82 | }
|
---|
83 | T operator=(double _value){
|
---|
84 | value = _value;
|
---|
85 | return value;
|
---|
86 | }
|
---|
87 | T (atom::*f)() const;
|
---|
88 | T value;
|
---|
89 | };
|
---|
90 |
|
---|
91 | template<class T>
|
---|
92 | struct stepValueSum {
|
---|
93 | stepValueSum(unsigned int _step, T (atom::*_f)(unsigned int) const) :
|
---|
94 | step(_step),
|
---|
95 | f(_f),
|
---|
96 | value(0)
|
---|
97 | {}
|
---|
98 | T operator+(atom *atom){
|
---|
99 | return value + (atom->*f)(step);
|
---|
100 | }
|
---|
101 | T operator=(double _value){
|
---|
102 | value = _value;
|
---|
103 | return value;
|
---|
104 | }
|
---|
105 | unsigned int step;
|
---|
106 | T (atom::*f)(unsigned int) const;
|
---|
107 | T value;
|
---|
108 | };
|
---|
109 | };
|
---|
110 |
|
---|
111 | template<class Set>
|
---|
112 | inline void AtomSetMixin<Set>::translate(const Vector &translater){
|
---|
113 | BOOST_FOREACH(atom *atom,*this){
|
---|
114 | *(atom) += translater;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | template<class Set>
|
---|
119 | template<class Function>
|
---|
120 | inline void AtomSetMixin<Set>::transformNodes(Function f){
|
---|
121 | std::for_each(this->begin(),
|
---|
122 | this->end(),
|
---|
123 | AtomSetMixin::workOnNodePointer<Function>(f));
|
---|
124 | }
|
---|
125 |
|
---|
126 | template<class Set>
|
---|
127 | inline double AtomSetMixin<Set>::totalTemperatureAtStep(unsigned int step) const{
|
---|
128 | return accumulate(this->begin(),this->end(),stepValueSum<double>(step,&atom::getKineticEnergy)).value;
|
---|
129 | }
|
---|
130 |
|
---|
131 | template<class Set>
|
---|
132 | inline double AtomSetMixin<Set>::totalMass() const{
|
---|
133 | return accumulate(this->begin(),this->end(),valueSum<double>(&atom::getMass)).value;
|
---|
134 | }
|
---|
135 |
|
---|
136 | // allows simpler definition of AtomSets
|
---|
137 | #define ATOMSET(container_type) AtomSetMixin<container_type<atom*> >
|
---|
138 |
|
---|
139 | #endif /* ATOMSET_HPP_ */
|
---|