OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
DistributionMoments.cpp
Go to the documentation of this file.
1
2// Class DistributionMoments
3// Computes the statistics of particle distributions.
4//
5// Copyright (c) 2021, Christof Metzger-Kraus
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//
19
23#include "Physics/Units.h"
24#include "Utilities/Options.h"
25#include "Utilities/Util.h"
26
27#include "Message/GlobalComm.h"
28#include "Utility/Inform.h"
29
30#include <gsl/gsl_histogram.h>
31
32#include <cstddef>
33#include <cmath>
34#include <functional>
35#include <iterator>
36#include <limits>
37#include <numeric>
38#include <tuple>
39
40extern Inform* gmsg;
41
42const double DistributionMoments::percentileOneSigmaNormalDist_m = std::erf(1 / std::sqrt(2));
43const double DistributionMoments::percentileTwoSigmasNormalDist_m = std::erf(2 / std::sqrt(2));
44const double DistributionMoments::percentileThreeSigmasNormalDist_m = std::erf(3 / std::sqrt(2));
45const double DistributionMoments::percentileFourSigmasNormalDist_m = std::erf(4 / std::sqrt(2));
46
52
54{
55 computeStatistics(bunch.begin(), bunch.end());
56}
57
58void DistributionMoments::compute(const std::vector<OpalParticle>::const_iterator &first,
59 const std::vector<OpalParticle>::const_iterator &last)
60{
61 computeStatistics(first, last);
62}
63
64template<class InputIt>
65void DistributionMoments::computeMeans(const InputIt &first, const InputIt &last)
66{
67 unsigned int localNum = last - first;
68 std::vector<double> localStatistics(10);
69 std::vector<double> maxima(6, std::numeric_limits<double>::lowest());
70 for (InputIt it = first; it != last; ++ it) {
71 OpalParticle const& particle = *it;
72 if (isParticleExcluded(particle)) {
73 -- localNum;
74 continue;
75 }
76 for (unsigned int i = 0; i < 3; ++ i) {
77 maxima[2 * i] = std::max(maxima[2 * i], particle[2 * i]);
78 maxima[2 * i + 1] = std::max(maxima[2 * i + 1], -particle[2 * i]); // calculates the minimum
79 }
80
81 unsigned int l = 0;
82 for (unsigned int i = 0; i < 6; ++ i, ++ l) {
83 localStatistics[l] += particle[i];
84 }
85
86 // l = 6
87 localStatistics[l++] += particle.getTime();
88
89 double gamma = Util::getGamma(particle.getP());
90 double eKin = (gamma - 1.0) * particle.getMass();
91 localStatistics[l++] += eKin;
92 localStatistics[l++] += gamma;
93 }
94 localStatistics.back() = localNum;
95
96 allreduce(localStatistics.data(), localStatistics.size(), std::plus<double>());
97 allreduce(maxima.data(), 6, std::greater<double>());
98 totalNumParticles_m = localStatistics.back();
99
100 double perParticle = 1.0 / totalNumParticles_m;
101 unsigned int l = 0;
102
103 for (; l < 6; ++ l) {
104 centroid_m[l] = localStatistics[l];
105 }
106 for (unsigned int i = 0; i < 3; ++ i) {
107 meanR_m(i) = centroid_m[2 * i] * perParticle;
108 meanP_m(i) = centroid_m[2 * i + 1] * perParticle;
109 maxR_m(i) = maxima[2 * i];
110 minR_m(i) = -maxima[2 * i + 1];
111 }
112
113 meanTime_m = localStatistics[l++] * perParticle;
114
115 meanKineticEnergy_m = localStatistics[l++] * perParticle;
116 meanGamma_m = localStatistics[l++] * perParticle;
117}
118
119/* 2 * Dim centroids + Dim * ( 2 * Dim + 1 ) 2nd moments + 2 * Dim (3rd and 4th order moments)
120 * --> 1st order moments: 0, ..., 2 * Dim - 1
121 * --> 2nd order moments: 2 * Dim, ..., Dim * ( 2 * Dim + 1 )
122 * --> 3rd order moments: Dim * ( 2 * Dim + 1 ) + 1, ..., Dim * ( 2 * Dim + 1 ) + Dim
123 * (only, central <dx^3>, <dy^3> and <dz^3>, interleaved with the 4th order moments)
124 * --> 4th order moments: Dim * ( 2 * Dim + 1 ) + Dim + 1, ..., Dim * ( 2 * Dim + 1 ) + 2 * Dim
125 * (only, central <dx^4>, <dy^4> and <dz^4>)
126 *
127 * For a 6x6 matrix we have each 2nd order moment (except diagonal
128 * entries) twice. We only store the upper half of the matrix.
129 */
130template<class InputIt>
131void DistributionMoments::computeStatistics(const InputIt &first, const InputIt &last)
132{
133 reset();
134
135 computeMeans(first, last);
136
137 double perParticle = 1.0 / totalNumParticles_m;
138 std::vector<double> localStatistics(37);
139 for (InputIt it = first; it != last; ++ it) {
140 OpalParticle const& particle = *it;
141 if (isParticleExcluded(particle)) {
142 continue;
143 }
144 unsigned int l = 6;
145 for (unsigned int i = 0; i < 6; ++ i) {
146 localStatistics[i] += std::pow(particle[i] - centroid_m[i] * perParticle, 2);
147 for (unsigned int j = 0; j <= i; ++ j, ++ l) {
148 localStatistics[l] += particle[i] * particle[j];
149 }
150 }
151
152 localStatistics[l++] += std::pow(particle.getTime() - meanTime_m, 2);
153
154 for (unsigned int i = 0; i < 3; ++ i, l += 2) {
155 double dr = particle[2 * i] - meanR_m(i);
156 double dr2 = dr * dr;
157 localStatistics[l] += dr2 * dr;
158 localStatistics[l + 1] += dr2 * dr2;
159 }
160
161 double eKin = Util::getKineticEnergy(particle.getP(), particle.getMass());
162 localStatistics[l++] += std::pow(eKin - meanKineticEnergy_m, 2);
163 localStatistics[l++] += particle.getCharge();
164 localStatistics[l++] += particle.getMass();
165 }
166
167 allreduce(localStatistics.data(), localStatistics.size(), std::plus<double>());
168
169 fillMembers(localStatistics);
170
171 computePercentiles(first, last);
172}
173
174
175template<class InputIt>
176void DistributionMoments::computePercentiles(const InputIt & first, const InputIt & last) {
178 return;
179 }
180
181 std::vector<gsl_histogram*> histograms(3);
182 // For a normal distribution the number of exchanged data between the cores is minimized
183 // if the number of histogram bins follows the following formula. Since we can't know
184 // how many particles are in each bin for the real distribution we use this formula.
185 unsigned int numBins = 3.5 * std::pow(3, std::log10(totalNumParticles_m));
186
187 Vector_t maxR;
188 for (unsigned int d = 0; d < 3; ++ d) {
189 maxR(d) = 1.0000001 * std::max(maxR_m[d] - meanR_m[d], meanR_m[d] - minR_m[d]);
190 histograms[d] = gsl_histogram_alloc(numBins);
191 gsl_histogram_set_ranges_uniform(histograms[d], 0.0, maxR(d));
192 }
193 for (InputIt it = first; it != last; ++ it) {
194 OpalParticle const& particle = *it;
195 for (unsigned int d = 0; d < 3; ++ d) {
196 gsl_histogram_increment(histograms[d], std::abs(particle[2 * d] - meanR_m[d]));
197 }
198 }
199
200 std::vector<int> localHistogramValues(3 * (numBins + 1)), globalHistogramValues(3 * (numBins + 1));
201 for (unsigned int d = 0; d < 3; ++ d) {
202 int j = 0;
203 size_t accumulated = 0;
204 std::generate(localHistogramValues.begin() + d * (numBins + 1) + 1,
205 localHistogramValues.begin() + (d + 1) * (numBins + 1),
206 [&histograms,&d,&j,&accumulated](){
207 accumulated += gsl_histogram_get(histograms[d], j ++);
208 return accumulated;});
209
210 gsl_histogram_free(histograms[d]);
211 }
212
213 allreduce(localHistogramValues.data(), globalHistogramValues.data(), 3 * (numBins + 1), std::plus<int>());
214
215 int numParticles68 = static_cast<int>(std::floor(totalNumParticles_m * percentileOneSigmaNormalDist_m + 0.5));
216 int numParticles95 = static_cast<int>(std::floor(totalNumParticles_m * percentileTwoSigmasNormalDist_m + 0.5));
217 int numParticles99 = static_cast<int>(std::floor(totalNumParticles_m * percentileThreeSigmasNormalDist_m + 0.5));
218 int numParticles99_99 = static_cast<int>(std::floor(totalNumParticles_m * percentileFourSigmasNormalDist_m + 0.5));
219
220 for (int d = 0; d < 3; d++) {
221 unsigned int localNum = last - first, current = 0;
222 std::vector<Vektor<double, 2>> oneDPhaseSpace(localNum);
223 for (InputIt it = first; it != last; ++ it, ++ current) {
224 OpalParticle const& particle = *it;
225 oneDPhaseSpace[current](0) = particle[2 * d];
226 oneDPhaseSpace[current](1) = particle[2 * d + 1];
227 }
228 std::sort(oneDPhaseSpace.begin(), oneDPhaseSpace.end(),
229 [d, this](Vektor<double, 2>& left, Vektor<double, 2>& right) {
230 return std::abs(left[0] - meanR_m[d]) < std::abs(right[0] - meanR_m[d]);
231 });
232
233 iterator_t endSixtyEight, endNinetyFive, endNinetyNine, endNinetyNine_NinetyNine;
234 endSixtyEight = endNinetyFive = endNinetyNine = endNinetyNine_NinetyNine = oneDPhaseSpace.end();
235
236 std::tie(sixtyEightPercentile_m[d], endSixtyEight) =
237 determinePercentilesDetail(oneDPhaseSpace.begin(),
238 oneDPhaseSpace.end(),
239 globalHistogramValues,
240 localHistogramValues,
241 d, numParticles68);
242
243 std::tie(ninetyFivePercentile_m[d], endNinetyFive) =
244 determinePercentilesDetail(oneDPhaseSpace.begin(),
245 oneDPhaseSpace.end(),
246 globalHistogramValues,
247 localHistogramValues,
248 d, numParticles95);
249
250 std::tie(ninetyNinePercentile_m[d], endNinetyNine) =
251 determinePercentilesDetail(oneDPhaseSpace.begin(),
252 oneDPhaseSpace.end(),
253 globalHistogramValues,
254 localHistogramValues,
255 d, numParticles99);
256
257 std::tie(ninetyNine_NinetyNinePercentile_m[d], endNinetyNine_NinetyNine) =
258 determinePercentilesDetail(oneDPhaseSpace.begin(),
259 oneDPhaseSpace.end(),
260 globalHistogramValues,
261 localHistogramValues,
262 d, numParticles99_99);
263
264 normalizedEps68Percentile_m[d] = computeNormalizedEmittance(oneDPhaseSpace.begin(), endSixtyEight);
265 normalizedEps95Percentile_m[d] = computeNormalizedEmittance(oneDPhaseSpace.begin(), endNinetyFive);
266 normalizedEps99Percentile_m[d] = computeNormalizedEmittance(oneDPhaseSpace.begin(), endNinetyNine);
267 normalizedEps99_99Percentile_m[d] = computeNormalizedEmittance(oneDPhaseSpace.begin(), endNinetyNine_NinetyNine);
268 }
269}
270
300std::pair<double, DistributionMoments::iterator_t>
303 const std::vector<int>& globalAccumulatedHistogram,
304 const std::vector<int>& localAccumulatedHistogram,
305 unsigned int dimension,
306 int numRequiredParticles) const {
307 unsigned int numBins = globalAccumulatedHistogram.size() / 3;
308 double percentile = 0.0;
309 iterator_t endPercentile = end;
310 for (unsigned int i = 1; i < numBins; ++ i) {
311 unsigned int idx = dimension * numBins + i;
312 if (globalAccumulatedHistogram[idx] > numRequiredParticles) {
313 iterator_t beginBin = begin + localAccumulatedHistogram[idx - 1];
314 iterator_t endBin = begin + localAccumulatedHistogram[idx];
315 unsigned int numMissingParticles = numRequiredParticles - globalAccumulatedHistogram[idx - 1];
316 unsigned int shift = 2;
317 while (numMissingParticles == 0) {
318 beginBin = begin + localAccumulatedHistogram[idx - shift];
319 numMissingParticles = numRequiredParticles - globalAccumulatedHistogram[idx - shift];
320 ++ shift;
321 }
322
323 std::vector<unsigned int> numParticlesInBin(Ippl::getNodes() + 1);
324 numParticlesInBin[Ippl::myNode() + 1] = endBin - beginBin;
325 allreduce(&(numParticlesInBin[1]), Ippl::getNodes(), std::plus<unsigned int>());
326 std::partial_sum(numParticlesInBin.begin(), numParticlesInBin.end(), numParticlesInBin.begin());
327
328 std::vector<double> positions(numParticlesInBin.back());
329 std::transform(beginBin, endBin, positions.begin() + numParticlesInBin[Ippl::myNode()],
330 [&dimension, this](Vektor<double, 2> const& particle)
331 { return std::abs(particle[0] - meanR_m[dimension]); });
332 allreduce(&(positions[0]), positions.size(), std::plus<double>());
333 std::sort(positions.begin(), positions.end());
334
335 percentile = (*(positions.begin() + numMissingParticles - 1)
336 + *(positions.begin() + numMissingParticles)) / 2;
337 for (iterator_t it = beginBin; it != endBin; ++ it) {
338 if (std::abs((*it)[0] - meanR_m[dimension]) > percentile) {
339 return std::make_pair(percentile, it);
340 }
341 }
342 endPercentile = endBin;
343 break;
344 }
345 }
346 return std::make_pair(percentile, endPercentile);
347}
348
351 double localStatistics[] = {0.0, 0.0, 0.0, 0.0};
352 localStatistics[0] = end - begin;
353 for (iterator_t it = begin; it < end; ++ it) {
354 const Vektor<double, 2>& rp = *it;
355 localStatistics[1] += rp(0);
356 localStatistics[2] += rp(1);
357 localStatistics[3] += rp(0) * rp(1);
358 }
359 allreduce(&(localStatistics[0]), 4, std::plus<double>());
360
361 double numParticles = localStatistics[0];
362 double perParticle = 1 / localStatistics[0];
363 double meanR = localStatistics[1] * perParticle;
364 double meanP = localStatistics[2] * perParticle;
365 double RP = localStatistics[3] * perParticle;
366
367 localStatistics[0] = 0.0;
368 localStatistics[1] = 0.0;
369 for (iterator_t it = begin; it < end; ++ it) {
370 const Vektor<double, 2>& rp = *it;
371 localStatistics[0] += std::pow(rp(0) - meanR, 2);
372 localStatistics[1] += std::pow(rp(1) - meanP, 2);
373 }
374 allreduce(&(localStatistics[0]), 2, std::plus<double>());
375
376 double stdR = std::sqrt(localStatistics[0] / numParticles);
377 double stdP = std::sqrt(localStatistics[1] / numParticles);
378 double sumRP = RP - meanR * meanP;
379 double squaredEps = std::pow(stdR * stdP, 2) - std::pow(sumRP, 2);
380 double normalizedEps = std::sqrt(std::max(squaredEps, 0.0));
381
382 return normalizedEps;
383}
384
385void DistributionMoments::fillMembers(std::vector<double> const& localMoments) {
386 Vector_t squaredEps, fac, sumRP;
387 double perParticle = 1.0 / totalNumParticles_m;
388
389 unsigned int l = 0;
390 for (; l < 6; l += 2) {
391 stdR_m(l / 2) = std::sqrt(localMoments[l] / totalNumParticles_m);
392 stdP_m(l / 2) = std::sqrt(localMoments[l + 1] / totalNumParticles_m);
393 }
394
395 for (unsigned int i = 0; i < 6; ++ i) {
396 for (unsigned int j = 0; j <= i; ++ j, ++ l) {
397 moments_m(i, j) = localMoments[l] * perParticle;
398 moments_m(j, i) = moments_m(i, j);
399 }
400 }
401
402 stdTime_m = localMoments[l++] * perParticle;
403
404 // halo parameter: kurtosis <dr^4> / <dr^2>^2 of the positions, from the
405 // central moments accumulated in computeStatistics
406 for (unsigned int i = 0; i < 3; ++ i, l += 2) {
407 double variance = localMoments[2 * i] * perParticle;
408 double fourthMoment = localMoments[l + 1] * perParticle;
409
410 halo_m(i) = fourthMoment / std::pow(variance, 2);
412 }
413
414 stdKineticEnergy_m = std::sqrt(localMoments[l++] * perParticle);
415
416 totalCharge_m = localMoments[l++];
417 totalMass_m = localMoments[l++];
418
419 for (unsigned int i = 0; i < 3; ++ i) {
420 sumRP(i) = moments_m(2 * i, 2 * i + 1) - meanR_m(i) * meanP_m(i);
421 stdRP_m(i) = sumRP(i) / (stdR_m(i) * stdP_m(i));
422 squaredEps(i) = std::pow(stdR_m(i) * stdP_m(i), 2) - std::pow(sumRP(i), 2);
423 normalizedEps_m(i) = std::sqrt(std::max(squaredEps(i), 0.0));
424 }
425
426 double betaGamma = std::sqrt(std::pow(meanGamma_m, 2) - 1.0);
428}
429
431{
432 double data[] = {0.0, 0.0};
433 for (OpalParticle const& particle: bunch) {
434 data[0] += Util::getKineticEnergy(particle.getP(), particle.getMass());
435 }
436 data[1] = bunch.getLocalNum();
437 allreduce(data, 2, std::plus<double>());
438
439 meanKineticEnergy_m = data[0] / data[1];
440}
441
443{
444
446 double avgVel[3]={0.0,0.0,0.0};
447
448 //From P in \beta\gamma to get v in m/s: v = (P*c)/\gamma
449 for (OpalParticle const& particle_r: bunch_r) {
450 for(unsigned i = 0; i < 3; i++) {
451 avgVel[i] += ((particle_r.getP()[i] * Physics::c)/
452 (Util::getGamma(particle_r.getP())));
453 }
454 }
455
456 allreduce(avgVel, 3, std::plus<double>());
457
458 const double N = static_cast<double>(bunch_r.getTotalNum());
459 for(unsigned i = 0; i < 3; i++) {
460 avgVel[i]= avgVel[i]/N;
461 }
462
463 double tempAvg = 0.0;
464
465 for (OpalParticle const& particle_r: bunch_r) {
466 for(unsigned i = 0; i < 3; i++) {
467 tempAvg += std::pow((((particle_r.getP()[i] * Physics::c)/
468 (Util::getGamma(particle_r.getP()))) - avgVel[i]),2);
469 }
470 }
471 allreduce(tempAvg, 1, std::plus<double>());
472
473 // Compute the average temperature k_B T in units of kg m^2/s^2, where k_B is
474 // Boltzmann constant, with the rest mass of the bunch's particles (getM() in eV)
475 temperature_m = (1.0/3.0) * Units::eV2kg * bunch_r.getM() * (tempAvg/N);
476
478 (density * std::pow(Physics::q_e,2)));
479
480 computePlasmaParameter(density);
481
482}
483
485{
486 // Plasma parameter: Average number of particles within the Debye sphere
487 plasmaParameter_m = (4.0/3.0) * Physics::pi * std::pow(debyeLength_m,3) * density;
488}
489
490
492{
493 std::fill(std::begin(centroid_m), std::end(centroid_m), 0.0);
494 meanR_m = 0.0;
495 meanP_m = 0.0;
496 stdR_m = 0.0;
497 stdP_m = 0.0;
498 stdRP_m = 0.0;
499 normalizedEps_m = 0.0;
500 geometricEps_m = 0.0;
501 halo_m = 0.0;
502
504 stdKineticEnergy_m = 0.0;
506
507 totalCharge_m = 0.0;
508 totalMass_m = 0.0;
510
519}
520
521
528
530{
531 //FIXME After issue 287 is resolved this shouldn't be necessary anymore
532 return !Options::amr
534 && particle.getId() == 0;
535}
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
Inform * gmsg
Definition Main.cpp:70
void allreduce(const T *input, T *output, int count, Op op)
constexpr double epsilon_0
The permittivity of vacuum in As/Vm.
Definition Physics.h:51
constexpr double q_e
The elementary charge in As.
Definition Physics.h:69
constexpr double c
The velocity of light in m/s.
Definition Physics.h:45
constexpr double pi
The value of.
Definition Physics.h:30
constexpr double eV2kg
Definition Units.h:110
bool computePercentiles
Definition Options.cpp:109
double haloShift
The constant parameter C to shift halo, by < w^4 > / < w^2 > ^2 - C (w=x,y,z)
Definition Options.cpp:105
bool amr
Enable AMR if true.
Definition Options.cpp:97
double getKineticEnergy(Vector_t p, double mass)
Definition Util.h:59
double getGamma(Vector_t p)
Definition Util.h:49
size_t getLocalNum() const
size_t getTotalNum() const
ConstIterator end() const
ConstIterator begin() const
double getM() const
bool isInOPALCyclMode()
Definition OpalData.cpp:272
static OpalData * getInstance()
Definition OpalData.cpp:196
void computeDebyeLength(PartBunchBase< double, 3 > const &, double)
FMatrix< double, 6, 6 > moments_m
static const double percentileFourSigmasNormalDist_m
void computeMeans(const InputIt &, const InputIt &)
void computeMeanKineticEnergy(PartBunchBase< double, 3 > const &)
void compute(const std::vector< OpalParticle >::const_iterator &, const std::vector< OpalParticle >::const_iterator &)
std::vector< Vektor< double, 2 > >::const_iterator iterator_t
void computeStatistics(const InputIt &, const InputIt &)
double computeNormalizedEmittance(const iterator_t &begin, const iterator_t &end) const
static const double percentileThreeSigmasNormalDist_m
void computePercentiles(const InputIt &, const InputIt &)
static const double percentileTwoSigmasNormalDist_m
bool isParticleExcluded(const OpalParticle &) const
Vector_t ninetyNine_NinetyNinePercentile_m
void fillMembers(std::vector< double > const &)
std::pair< double, iterator_t > determinePercentilesDetail(const iterator_t &begin, const iterator_t &end, const std::vector< int > &globalAccumulatedHistogram, const std::vector< int > &localAccumulatedHistogram, unsigned int dimension, int numRequiredParticles) const
static const double percentileOneSigmaNormalDist_m
const Vector_t & getP() const
Get momentum.
double getCharge() const
Get charge in Coulomb.
double getMass() const
Get mass in GeV/c^2.
double getTime() const
Get time.
int64_t getId() const
Get the id of the particle.
A templated representation for matrices.
Definition FMatrix.h:39
static int getNodes()
Definition IpplInfo.cpp:689
static int myNode()
Definition IpplInfo.cpp:710
Vektor< double, 3 > Vector_t
Definition Vektor.h:6