30#include <gsl/gsl_histogram.h>
59 const std::vector<OpalParticle>::const_iterator &last)
64template<
class InputIt>
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) {
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]);
82 for (
unsigned int i = 0; i < 6; ++ i, ++ l) {
83 localStatistics[l] += particle[i];
87 localStatistics[l++] += particle.
getTime();
90 double eKin = (gamma - 1.0) * particle.
getMass();
91 localStatistics[l++] += eKin;
92 localStatistics[l++] += gamma;
94 localStatistics.back() = localNum;
96 allreduce(localStatistics.data(), localStatistics.size(), std::plus<double>());
97 allreduce(maxima.data(), 6, std::greater<double>());
103 for (; l < 6; ++ l) {
106 for (
unsigned int i = 0; i < 3; ++ i) {
109 maxR_m(i) = maxima[2 * i];
110 minR_m(i) = -maxima[2 * i + 1];
113 meanTime_m = localStatistics[l++] * perParticle;
130template<
class InputIt>
138 std::vector<double> localStatistics(37);
139 for (InputIt it = first; it != last; ++ it) {
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];
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;
163 localStatistics[l++] += particle.
getCharge();
164 localStatistics[l++] += particle.
getMass();
167 allreduce(localStatistics.data(), localStatistics.size(), std::plus<double>());
175template<
class InputIt>
181 std::vector<gsl_histogram*> histograms(3);
188 for (
unsigned int d = 0; d < 3; ++ d) {
190 histograms[d] = gsl_histogram_alloc(numBins);
191 gsl_histogram_set_ranges_uniform(histograms[d], 0.0, maxR(d));
193 for (InputIt it = first; it != last; ++ it) {
195 for (
unsigned int d = 0; d < 3; ++ d) {
196 gsl_histogram_increment(histograms[d], std::abs(particle[2 * d] -
meanR_m[d]));
200 std::vector<int> localHistogramValues(3 * (numBins + 1)), globalHistogramValues(3 * (numBins + 1));
201 for (
unsigned int d = 0; d < 3; ++ d) {
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;});
210 gsl_histogram_free(histograms[d]);
213 allreduce(localHistogramValues.data(), globalHistogramValues.data(), 3 * (numBins + 1), std::plus<int>());
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) {
225 oneDPhaseSpace[current](0) = particle[2 * d];
226 oneDPhaseSpace[current](1) = particle[2 * d + 1];
228 std::sort(oneDPhaseSpace.begin(), oneDPhaseSpace.end(),
230 return std::abs(left[0] - meanR_m[d]) < std::abs(right[0] - meanR_m[d]);
233 iterator_t endSixtyEight, endNinetyFive, endNinetyNine, endNinetyNine_NinetyNine;
234 endSixtyEight = endNinetyFive = endNinetyNine = endNinetyNine_NinetyNine = oneDPhaseSpace.end();
238 oneDPhaseSpace.end(),
239 globalHistogramValues,
240 localHistogramValues,
245 oneDPhaseSpace.end(),
246 globalHistogramValues,
247 localHistogramValues,
252 oneDPhaseSpace.end(),
253 globalHistogramValues,
254 localHistogramValues,
259 oneDPhaseSpace.end(),
260 globalHistogramValues,
261 localHistogramValues,
262 d, numParticles99_99);
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;
310 for (
unsigned int i = 1; i < numBins; ++ i) {
311 unsigned int idx = dimension * numBins + i;
312 if (globalAccumulatedHistogram[idx] > numRequiredParticles) {
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];
323 std::vector<unsigned int> numParticlesInBin(
Ippl::getNodes() + 1);
324 numParticlesInBin[
Ippl::myNode() + 1] = endBin - beginBin;
326 std::partial_sum(numParticlesInBin.begin(), numParticlesInBin.end(), numParticlesInBin.begin());
328 std::vector<double> positions(numParticlesInBin.back());
329 std::transform(beginBin, endBin, positions.begin() + numParticlesInBin[
Ippl::myNode()],
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());
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);
342 endPercentile = endBin;
346 return std::make_pair(percentile, endPercentile);
351 double localStatistics[] = {0.0, 0.0, 0.0, 0.0};
355 localStatistics[1] += rp(0);
356 localStatistics[2] += rp(1);
357 localStatistics[3] += rp(0) * rp(1);
359 allreduce(&(localStatistics[0]), 4, std::plus<double>());
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;
367 localStatistics[0] = 0.0;
368 localStatistics[1] = 0.0;
371 localStatistics[0] += std::pow(rp(0) - meanR, 2);
372 localStatistics[1] += std::pow(rp(1) - meanP, 2);
374 allreduce(&(localStatistics[0]), 2, std::plus<double>());
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));
382 return normalizedEps;
390 for (; l < 6; l += 2) {
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;
402 stdTime_m = localMoments[l++] * perParticle;
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;
410 halo_m(i) = fourthMoment / std::pow(variance, 2);
419 for (
unsigned int i = 0; i < 3; ++ i) {
422 squaredEps(i) = std::pow(
stdR_m(i) *
stdP_m(i), 2) - std::pow(sumRP(i), 2);
426 double betaGamma = std::sqrt(std::pow(
meanGamma_m, 2) - 1.0);
432 double data[] = {0.0, 0.0};
446 double avgVel[3]={0.0,0.0,0.0};
450 for(
unsigned i = 0; i < 3; i++) {
451 avgVel[i] += ((particle_r.getP()[i] *
Physics::c)/
456 allreduce(avgVel, 3, std::plus<double>());
458 const double N =
static_cast<double>(bunch_r.
getTotalNum());
459 for(
unsigned i = 0; i < 3; i++) {
460 avgVel[i]= avgVel[i]/N;
463 double tempAvg = 0.0;
466 for(
unsigned i = 0; i < 3; i++) {
467 tempAvg += std::pow((((particle_r.getP()[i] *
Physics::c)/
471 allreduce(tempAvg, 1, std::plus<double>());
534 && particle.
getId() == 0;
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
void allreduce(const T *input, T *output, int count, Op op)
constexpr double epsilon_0
The permittivity of vacuum in As/Vm.
constexpr double q_e
The elementary charge in As.
constexpr double c
The velocity of light in m/s.
constexpr double pi
The value of.
double haloShift
The constant parameter C to shift halo, by < w^4 > / < w^2 > ^2 - C (w=x,y,z)
bool amr
Enable AMR if true.
double getKineticEnergy(Vector_t p, double mass)
double getGamma(Vector_t p)
size_t getLocalNum() const
size_t getTotalNum() const
ConstIterator end() const
ConstIterator begin() const
static OpalData * getInstance()
double meanKineticEnergy_m
Vector_t ninetyNinePercentile_m
void computeDebyeLength(PartBunchBase< double, 3 > const &, double)
void computePlasmaParameter(double)
FMatrix< double, 6, 6 > moments_m
static const double percentileFourSigmasNormalDist_m
void computeMeans(const InputIt &, const InputIt &)
Vector_t normalizedEps99_99Percentile_m
void computeMeanKineticEnergy(PartBunchBase< double, 3 > const &)
unsigned int totalNumParticles_m
void compute(const std::vector< OpalParticle >::const_iterator &, const std::vector< OpalParticle >::const_iterator &)
Vector_t normalizedEps68Percentile_m
Vector_t normalizedEps95Percentile_m
std::vector< Vektor< double, 2 > >::const_iterator iterator_t
Vector_t ninetyFivePercentile_m
void computeStatistics(const InputIt &, const InputIt &)
double computeNormalizedEmittance(const iterator_t &begin, const iterator_t &end) const
Vector_t normalizedEps99Percentile_m
static const double percentileThreeSigmasNormalDist_m
void computePercentiles(const InputIt &, const InputIt &)
static const double percentileTwoSigmasNormalDist_m
bool isParticleExcluded(const OpalParticle &) const
Vector_t sixtyEightPercentile_m
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
void resetPlasmaParameters()
double stdKineticEnergy_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.
Vektor< double, 3 > Vector_t