OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
PartBins.cpp
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#include "Algorithms/PartBins.h"
21#include "Physics/Physics.h"
22#include "Physics/Units.h"
23
24#include "Utility/Inform.h"
25
26#include <cfloat>
27#include <cmath>
28#include <limits>
29#include <vector>
30
31PartBins::PartBins(int bins, int sbins)
32 : gamma_m(1.0),
33 bins_m(bins),
34 sBins_m(sbins),
35 xmin_m(0.0),
36 xmax_m(0.0),
37 hBin_m(0.0),
39 // number of particles in the bins on the local node
40 nBin_m = std::unique_ptr<size_t[]>(new size_t[bins_m]);
41 xbinmin_m = std::unique_ptr<double[]>(new double[bins_m]);
42 xbinmax_m = std::unique_ptr<double[]>(new double[bins_m]);
43
44 // flag whether the bin contain particles or not
45 binsEmitted_m = std::unique_ptr<bool[]>(new bool[bins_m]);
46
47 nDelBin_m = std::unique_ptr<size_t[]>(new size_t[bins_m]);
48
49 for (int i = 0; i < bins_m; i++) {
50 nDelBin_m[i] = nBin_m[i] = 0;
51 xbinmin_m[i] = std::numeric_limits<double>::max();
52 xbinmax_m[i] = -xbinmin_m[i];
53 binsEmitted_m[i] = false;
54 }
55}
56
58 size_t s = 0;
59 size_t sd = 0;
60 size_t gs = 0;
61
62 for (int i = 0; i < getLastemittedBin(); i++) {
63 s += nBin_m[i];
64 sd += nDelBin_m[i];
65 }
66 gs = s - sd;
67 ippl::Comm->reduce(gs, gs, 1, std::plus<size_t>());
68 return gs;
69}
70
72 size_t s = 0;
73 s = nBin_m[b];
74 ippl::Comm->reduce(s, s, 1, std::plus<size_t>());
75 return s;
76}
77
78void PartBins::updatePartInBin_cyc(size_t countLost[]) {
79 for (int ii = 0; ii < nemittedBins_m; ii++) {
80 if (countLost[ii] > 0)
81 nBin_m[ii] -= countLost[ii];
82 }
83}
84
85void PartBins::resetPartInBin_cyc(size_t newPartNum[], int maxbinIndex) {
87 ippl::Comm->reduce(maxbinIndex, maxbinIndex, 1, std::greater<size_t>());
88 nemittedBins_m = maxbinIndex + 1;
89
90 for (int ii = 0; ii < nemittedBins_m; ii++) {
91 nBin_m[ii] = newPartNum[ii]; // only count particles on the local node
92 setBinEmitted(ii); // set true for this bin
93 }
94}
95
97 tmppart_m.clear();
98 isEmitted_m.clear();
99}
100
101bool PartBins::getPart(size_t n, int bin, std::vector<double>& p) {
102 if (tmppart_m[n][6] == bin) {
103 p = tmppart_m[n];
104 return true;
105 } else
106 return false;
107}
108
115
116 double sshift = std::sqrt(1. - (1. / (gamma_m * gamma_m))) * Physics::c * 0.1 * Units::ps2s;
117 std::sort(tmppart_m.begin(), tmppart_m.end(), DescendingLocationSort(2));
118 xmax_m = tmppart_m[0][2];
119 xmin_m = tmppart_m.back()[2];
120
121 for (unsigned int n = 0; n < tmppart_m.size(); n++)
122 tmppart_m[n][2] -= xmax_m + sshift; /* push particles back */
123
124 xmin_m -= xmax_m + 0.0001 * (xmax_m - xmin_m) + sshift; /* lower the limits */
125 xmax_m = -sshift;
126
127 ippl::Comm->reduce(xmax_m, xmax_m, 1, std::greater<double>());
128 ippl::Comm->reduce(xmin_m, xmin_m, 1, std::less<double>());
129
131 // reduce(xmin_m, xmin_m, OpMinAssign());
132 // reduce(xmax_m, xmax_m, OpMaxAssign());
133
134 hBin_m = (std::abs(xmax_m - xmin_m)) / (bins_m);
135 calcHBins();
136 for (int n = 0; n < bins_m; n++)
137 if (nBin_m[n] == 0)
138 setBinEmitted(n);
139}
140
142 for (unsigned int n = 0; n < tmppart_m.size(); n++)
143 tmppart_m[n][6] = getBin(tmppart_m[n][2]);
144 calcExtrema();
145}
146
148 for (unsigned int n = 0; n < tmppart_m.size(); n++) {
149 if (xbinmin_m[(int)tmppart_m[n][6]] >= tmppart_m[n][2])
150 xbinmin_m[(int)tmppart_m[n][6]] = tmppart_m[n][2];
151
152 if (xbinmax_m[(int)tmppart_m[n][6]] <= tmppart_m[n][2])
153 xbinmax_m[(int)tmppart_m[n][6]] = tmppart_m[n][2];
154 }
155}
156
157Inform& PartBins::print(Inform& os) {
158 os << "-----------------------------------------" << endl;
159 os << " CREATE BINNED GAUSS DISTRIBUTION DONE " << endl;
160
161 os << "Bins= " << bins_m << " hBin= " << hBin_m << " Particle vector length "
162 << tmppart_m.size() << endl;
163
164 // for(int i = 0; i < gsl_histogram_bins(h_m); i++)
165 // os << "Bin # " << i << " val " << gsl_histogram_get(h_m, i) << endl;
166 for (int i = 0; i < bins_m; i++) {
167 size_t msum = 0;
168 for (int j = 0; j < sBins_m; j++)
169 msum += gsl_histogram_get(h_m.get(), i * sBins_m + j);
170 os << "Bin # " << i << " val " << msum << endl;
171 }
172
173 if (getLastemittedBin() >= 0)
174 os << "Last emitted bin is " << getLastemittedBin() << endl;
175 else
176 os << "No bin is emitted !" << endl;
177 return os;
178}
179
180int PartBins::getBin(double x) {
185 int b = (int)std::floor(std::abs(xmax_m - x) / hBin_m);
186 nBin_m[b]++;
187 return b;
188}
constexpr double c
The velocity of light in m/s.
Definition Physics.h:45
constexpr double ps2s
Definition Units.h:53
virtual ~PartBins()
Definition PartBins.cpp:96
void calcHBins()
Definition PartBins.cpp:141
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
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:71
void resetPartInBin_cyc(size_t newPartNum[], int binID)
Definition PartBins.cpp:85
std::vector< bool > isEmitted_m
Definition PartBins.h:122
void setBinEmitted(int bin)
Definition PartBins.h:86
void calcExtrema()
Definition PartBins.cpp:147
int getBin(double x)
Definition PartBins.cpp:180
void updatePartInBin_cyc(size_t countLost[])
Definition PartBins.cpp:78
void sortArray()
Definition PartBins.cpp:110
Inform & print(Inform &os)
Definition PartBins.cpp:157
std::unique_ptr< bool[]> binsEmitted_m
Definition PartBins.h:124
double hBin_m
Definition PartBins.h:118
bool getPart(size_t n, int bin, std::vector< double > &p)
Definition PartBins.cpp:101
int nemittedBins_m
Definition PartBins.h:157
size_t getTotalNum()
How many particles are in all the bins.
Definition PartBins.cpp:57
double xmax_m
Definition PartBins.h:111
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