OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
AmrParticleLevelCounter.h
Go to the documentation of this file.
1//
2// Class AmrParticleLevelCounter
3// Helper class in order to keep track of particles
4// per level. It allows to iterate faster through
5// particles at a certain level.
6// The class is built on the STL map container where
7// the key represents the level and the value is the
8// the number of particles at that level.
9//
10// Copyright (c) 2016 - 2020, Matthias Frey Paul Scherrer Institut, Villigen PSI, Switzerland
11// All rights reserved
12//
13// Implemented as part of the PhD thesis
14// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
15//
16// This file is part of OPAL.
17//
18// OPAL is free software: you can redistribute it and/or modify
19// it under the terms of the GNU General Public License as published by
20// the Free Software Foundation, either version 3 of the License, or
21// (at your option) any later version.
22//
23// You should have received a copy of the GNU General Public License
24// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
25//
26#ifndef AMR_PARTICLE_LEVEL_COUNTER_H
27#define AMR_PARTICLE_LEVEL_COUNTER_H
28
29#include <map>
30#include <numeric>
31#include <functional>
32#include <iterator>
33
34template <
35 class Key,
36 class T,
37 class Compare = std::less<Key>,
38 class Allocator = std::allocator<std::pair<const Key, T> >
40{
41
42public:
44 typedef typename std::map<Key, T>::size_type size_type;
46 typedef typename std::map<Key, T>::const_iterator const_iterator;
47
48public:
49
51
57 void increment(const Key& level, T nTimes = T(1)) { count_m[level] += nTimes; }
58
64 void decrement(const Key& level, T nTimes = T(1)) { increment(level, -nTimes); }
65
66 T& operator[](T level) { return count_m[level]; }
67
68 const T& operator[](T level) const { return count_m[level]; }
69
70 size_type size() const { return count_m.size(); }
71
72 bool empty() const { return count_m.empty(); }
73
74 iterator begin() { return count_m.begin(); }
75 const_iterator begin() const { return count_m.begin(); }
76
77 iterator end() { return count_m.end(); }
78 const_iterator end() const { return count_m.end(); }
79
80
86 T begin(T level) const {
87 auto end = count_m.begin();
88
89 // make sure to stay within container
90 T size = count_m.size();
91 std::advance(end, (level > size) ? size : level);
92
93 return std::accumulate(count_m.begin(), end, 0,
94 [](T sum, const value_type& value_pair) {
95 return sum + value_pair.second;
96 });
97 }
98
99
105 T end(T level) const { return begin(level + 1); }
106
107
113 void remove(T num, T begin) {
114 int inum = int(num);
115 while ( inum > -1 ) {
116 T level = which(begin + inum);
117 --count_m[level];
118 --inum;
119 }
120 }
121
122
128 return begin( count_m.size() );
129 }
130
131
135 T getLocalNumUpToLevel(T level) const {
136 PAssert_GE(level, T(0));
137 T sum = 0;
138 for (T i = 0; i <= level; ++i) {
139 sum += end(i) - begin(i);
140 }
141 return sum;
142 }
143
144
148 T getLocalNumAtLevel(T level) const {
149 PAssert_GE(level, T(0));
150 return end(level) - begin(level);
151 }
152
153private:
159 T which(T idx) {
160 T level = 0;
161
162 while ( idx >= end(level) && level < size() )
163 ++level;
164
165 return level;
166 }
167
168
169private:
174 std::map<Key, T> count_m;
175};
176
177#endif
T * value_type(const SliceIterator< T > &)
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1111
#define PAssert_GE(a, b)
Definition: PAssert.h:109
std::string::iterator iterator
Definition: MSLang.h:16
const_iterator begin() const
void increment(const Key &level, T nTimes=T(1))
const T & operator[](T level) const
std::map< Key, T >::size_type size_type
std::map< Key, T >::value_type value_type
void decrement(const Key &level, T nTimes=T(1))
std::map< Key, T >::iterator iterator
const_iterator end() const
std::map< Key, T >::const_iterator const_iterator