OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
PartBins.h
Go to the documentation of this file.
1//
2// Class PartBins
3// Defines a structure to hold energy bins and their associated data
4//
5// Copyright (c) 2007-2020, Paul Scherrer Institut, Villigen PSI, Switzerland
6// All rights reserved
7//
8// This file is part of OPAL.
9//
10// OPAL is free software: you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// You should have received a copy of the GNU General Public License
16// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17//
18
19#ifndef OPAL_Bins_HH
20#define OPAL_Bins_HH
21
22#include <gsl/gsl_histogram.h>
23
24#include <functional>
25#include <memory>
26#include <vector>
27
28class Inform;
29
30class PartBins {
31
41public:
42
43
44 PartBins(int bins, int sbins);
45
46 virtual ~PartBins();
47
48
50 void fill(std::vector<double> &p) {
51 tmppart_m.push_back(p);
52 isEmitted_m.push_back(false);
53 }
54
56 size_t getNp() {
57 // size_t a = tmppart_m.size();
58 // reduce(a, a, OpAddAssign());
59 // return a;
60 return tmppart_m.size();
61 }
62
64 void setPartNum(int bin, long long num) {nBin_m[bin] = num;}
65
67 bool doEmission() {return getNp() > 0;}
68
69 bool isEmitted(int n, int /*bin*/) {
70 return isEmitted_m[n]; //(isEmitted_m[n][0]==1) && (isEmitted_m[n][1] == bin);
71 }
72
74 bool getPart(size_t n, int bin, std::vector<double> &p);
75
79 void sortArray();
80
81 private:
83 void calcHBins();
84 void calcExtrema();
86 void setBinEmitted(int bin) {binsEmitted_m[bin] = true;}
87
88 public:
90 void resetPartInBin_cyc(size_t newPartNum[], int binID);
92 void updatePartInBin_cyc(size_t countLost[]);
93
94 void setGamma(double gamma) { gamma_m = gamma;}
95 double getGamma() {return gamma_m;}
96
97protected:
98
99 double gamma_m;
104 int getBin(double x);
105
108
110 double xmin_m;
111 double xmax_m;
112
114 std::unique_ptr<double[]> xbinmin_m;
115 std::unique_ptr<double[]> xbinmax_m;
116
118 double hBin_m;
119
121 std::vector< std::vector<double> > tmppart_m;
122 std::vector< bool > isEmitted_m;
124 std::unique_ptr<bool[]> binsEmitted_m;
125
126public:
127
128 Inform &print(Inform &os);
129
130 int getSBins() { return sBins_m; };
131
133 virtual int getNBins() {return gsl_histogram_bins(h_m.get()) / sBins_m; }
134
137
139 void resetBins() {
140 h_m.reset(nullptr);
141 }
142
143 virtual bool weHaveBins() {
144 return h_m != nullptr;
145 }
146
148 size_t getTotalNum();
149
151 size_t getTotalNumPerBin(int b);
152
153
154protected:
155
158
160 std::unique_ptr<size_t[]> nBin_m;
161
163 std::unique_ptr<size_t[]> nDelBin_m;
164
165 std::unique_ptr<gsl_histogram> h_m;
166
167};
168
170 return p.print(os);
171}
172
173
175public:
176 AscendingLocationSort(int direction = 0): direction_m(direction)
177 {;}
178
179 bool operator()(const std::vector<double>& first_part, const std::vector<double>& second_part) {
180 return first_part[direction_m] < second_part[direction_m];
181 }
182private:
184};
185
187public:
188 DescendingLocationSort(int direction = 0): direction_m(direction)
189 {;}
190
191 bool operator()(const std::vector<double>& first_part, const std::vector<double>& second_part) {
192 return first_part[direction_m] > second_part[direction_m];
193 }
194private:
196};
197
198#endif // OPAL_Bins_HH
Inform & operator<<(Inform &os, PartBins &p)
Definition: PartBins.h:169
virtual ~PartBins()
Definition: PartBins.cpp:100
void calcHBins()
Definition: PartBins.cpp:143
std::vector< std::vector< double > > tmppart_m
Definition: PartBins.h:121
int sBins_m
Definition: PartBins.h:107
std::unique_ptr< double[]> xbinmax_m
Definition: PartBins.h:115
std::unique_ptr< double[]> xbinmin_m
Definition: PartBins.h:114
double xmin_m
Definition: PartBins.h:110
double gamma_m
Definition: PartBins.h:99
size_t getNp()
get the number of particles in the temporary particle structure used for binning
Definition: PartBins.h:56
bool doEmission()
Is true if we still have particles to emit.
Definition: PartBins.h:67
PartBins(int bins, int sbins)
Definition: PartBins.cpp:31
std::unique_ptr< size_t[]> nDelBin_m
Definition: PartBins.h:163
size_t getTotalNumPerBin(int b)
How many particles are in the bin b.
Definition: PartBins.cpp:73
void setGamma(double gamma)
Definition: PartBins.h:94
bool isEmitted(int n, int)
Definition: PartBins.h:69
void resetPartInBin_cyc(size_t newPartNum[], int binID)
Definition: PartBins.cpp:89
std::vector< bool > isEmitted_m
Definition: PartBins.h:122
virtual int getNBins()
Definition: PartBins.h:133
void setPartNum(int bin, long long num)
Definition: PartBins.h:64
void setBinEmitted(int bin)
Definition: PartBins.h:86
void fill(std::vector< double > &p)
Add a particle to the temporary container.
Definition: PartBins.h:50
void calcExtrema()
Definition: PartBins.cpp:150
int getBin(double x)
Definition: PartBins.cpp:183
void updatePartInBin_cyc(size_t countLost[])
Definition: PartBins.cpp:81
void sortArray()
Definition: PartBins.cpp:116
Inform & print(Inform &os)
Definition: PartBins.cpp:160
std::unique_ptr< bool[]> binsEmitted_m
Definition: PartBins.h:124
double hBin_m
Definition: PartBins.h:118
double getGamma()
Definition: PartBins.h:95
bool getPart(size_t n, int bin, std::vector< double > &p)
Definition: PartBins.cpp:106
int nemittedBins_m
Definition: PartBins.h:157
size_t getTotalNum()
How many particles are in all the bins.
Definition: PartBins.cpp:59
virtual bool weHaveBins()
Definition: PartBins.h:143
double xmax_m
Definition: PartBins.h:111
void resetBins()
If the bunch object rebins we need to call resetBins()
Definition: PartBins.h:139
int getLastemittedBin()
Definition: PartBins.h:136
int bins_m
Definition: PartBins.h:106
std::unique_ptr< size_t[]> nBin_m
Definition: PartBins.h:160
std::unique_ptr< gsl_histogram > h_m
Definition: PartBins.h:165
int getSBins()
Definition: PartBins.h:130
bool operator()(const std::vector< double > &first_part, const std::vector< double > &second_part)
Definition: PartBins.h:179
AscendingLocationSort(int direction=0)
Definition: PartBins.h:176
bool operator()(const std::vector< double > &first_part, const std::vector< double > &second_part)
Definition: PartBins.h:191
DescendingLocationSort(int direction=0)
Definition: PartBins.h:188
Definition: Inform.h:42