OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
DistributionMoments.h
Go to the documentation of this file.
1//
2// Class DistributionMoments
3// Computes the statistics of particle distributions.
4//
5// Copyright (c) 2025, 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#ifndef DISTRIBUTIONMOMENTS_H
19#define DISTRIBUTIONMOMENTS_H
20
21#include "Algorithms/Matrix.h"
22#include "Ippl.h"
24#include "Physics/Physics.h"
25#include "Physics/Units.h"
26
27#include <Kokkos_Core.hpp>
28#include <vector>
29
30template <typename T, unsigned Dim = 3>
31using Vector_t = ippl::Vector<T, Dim>;
32
33typedef typename std::pair<Vector_t<double, 3>, Vector_t<double, 3>> VectorPair_t;
34
35#include "Algorithms/Matrix.h"
36
37class OpalParticle;
38
40public:
42
47 void setContainerState(std::weak_ptr<BunchStateHandler::ContainerState> containerState) {
48 containerState_m = std::move(containerState);
49 }
50
56 void setEnergyReferenceMass(double referenceMassGeV, bool rescaleToReference = true) {
57 referenceMassGeV_m = referenceMassGeV;
58 rescaleToReference_m = rescaleToReference;
59 }
62 void compute(
63 const std::vector<OpalParticle>::const_iterator&,
64 const std::vector<OpalParticle>::const_iterator&);
65 void computeMoments(
66 ippl::ParticleAttrib<Vector_t<double, 3>>::view_type Rview,
67 ippl::ParticleAttrib<Vector_t<double, 3>>::view_type Pview,
68 ippl::ParticleAttrib<double>::view_type Mview, size_t Np, size_t Nlocal);
70 ippl::ParticleAttrib<Vector_t<double, 3>>::view_type Rview, size_t Nlcoal);
73 ippl::ParticleAttrib<Vector_t<double, 3>>::view_type Pview, size_t Np, size_t Nlocal,
74 double density);
75 void computePlasmaParameter(double);
76
88
97
98 double getMeanTime() const;
99 double getStdTime() const;
100 double getMeanGamma() const;
101 double getMeanGammaZ() const;
102 double getMeanKineticEnergy() const;
103 double getTemperature() const;
104 double getDebyeLength() const;
105 double getPlasmaParameter() const;
106 double getStdKineticEnergy() const;
107 double getDx() const;
108 double getDDx() const;
109 double getDy() const;
110 double getDDy() const;
114 double getTotalCharge() const;
115 double getTotalMass() const;
116 double getTotalNumParticles() const;
117
118 void computeMeans(
119 ippl::ParticleAttrib<Vector_t<double, 3>>::view_type Rview,
120 ippl::ParticleAttrib<Vector_t<double, 3>>::view_type Pview,
121 ippl::ParticleAttrib<double>::view_type Mview, size_t Np, size_t Nlocal);
122
123private:
124 bool isParticleExcluded(const OpalParticle&) const;
125
126 // template <class InputIt>
127 // void computeMeans(const InputIt&, const InputIt&);
128 // template <class InputIt>
129 // void computeStatistics(const InputIt&, const InputIt&);
130 template <class InputIt>
131 void computePercentiles(const InputIt&, const InputIt&);
132 using iterator_t = std::vector<Vector_t<double, 2>>::const_iterator;
133 std::pair<double, iterator_t> determinePercentilesDetail(
134 const iterator_t& begin, const iterator_t& end,
135 const std::vector<int>& globalAccumulatedHistogram,
136 const std::vector<int>& localAccumulatedHistogram, unsigned int dimension,
137 int numRequiredParticles) const;
138 double computeNormalizedEmittance(const iterator_t& begin, const iterator_t& end) const;
139
140 void fillMembers(std::vector<double>&);
141
142 void reset();
143
145
146 std::weak_ptr<BunchStateHandler::ContainerState> containerState_m;
147
166
168 double stdTime_m;
176
177 // If enabled, compute kinetic energy moments with referenceMassGeV_m instead of Mview(k).
179 double referenceMassGeV_m = 0.0;
180
185
189
194};
195
197
201
203
207
211
215
217
219
221
223
224inline double DistributionMoments::getMeanTime() const { return meanTime_m; }
225
226inline double DistributionMoments::getStdTime() const { return stdTime_m; }
227
228inline double DistributionMoments::getMeanGamma() const { return meanGamma_m; }
229
230inline double DistributionMoments::getMeanGammaZ() const { return meanGammaZ_m; }
231
233
234// Compute and return the value of temperature in K
238inline double DistributionMoments::getDebyeLength() const { return debyeLength_m; }
240
242
243inline double DistributionMoments::getDx() const { return moments_m(0, 5); }
244
245inline double DistributionMoments::getDDx() const { return moments_m(1, 5); }
246
247inline double DistributionMoments::getDy() const { return moments_m(2, 5); }
248
249inline double DistributionMoments::getDDy() const { return moments_m(3, 5); }
250
252
254
256
257inline double DistributionMoments::getTotalCharge() const { return totalCharge_m; }
258
259inline double DistributionMoments::getTotalMass() const { return totalMass_m; }
260
262
266
270
274
278
282
286
290
294
296 Vector_t<double, 3> maxDistance;
297 for (unsigned int i = 0; i < 3; ++i) {
298 maxDistance[i] = std::max(std::abs(maxR_m[i]), std::abs(minR_m[i]));
299 }
300 return maxDistance;
301}
302
303// ---------------------------------------------------------------------------
304// Custom Kokkos-reducible types for parallel_reduce.
305// All used with Kokkos::Sum (which calls operator+= and reduction_identity::sum()).
306// See
307// https://kokkos.org/kokkos-core-wiki/ProgrammingGuide/Custom-Reductions-Built-In-Reducers-with-Custom-Scalar-Types.html
308// ---------------------------------------------------------------------------
309
310// Element-wise sum of N doubles.
311template <int N>
312struct SumArray {
313 double data[N];
314
315 KOKKOS_INLINE_FUNCTION
317 for (int i = 0; i < N; ++i)
318 data[i] = 0.0;
319 }
320
321 KOKKOS_INLINE_FUNCTION
323 for (int i = 0; i < N; ++i)
324 data[i] += src.data[i];
325 return *this;
326 }
327};
328
329// Element-wise maximum of N doubles.
330template <int N>
331struct MaxArray {
332 double data[N];
333
334 KOKKOS_INLINE_FUNCTION
336 for (int i = 0; i < N; ++i)
337 data[i] = Kokkos::reduction_identity<double>::max();
338 }
339
340 KOKKOS_INLINE_FUNCTION
342 for (int i = 0; i < N; ++i)
343 if (src.data[i] > data[i]) data[i] = src.data[i];
344 return *this;
345 }
346};
347
348// Element-wise minimum of N doubles.
349template <int N>
350struct MinArray {
351 double data[N];
352
353 KOKKOS_INLINE_FUNCTION
355 for (int i = 0; i < N; ++i)
356 data[i] = Kokkos::reduction_identity<double>::min();
357 }
358
359 KOKKOS_INLINE_FUNCTION
361 for (int i = 0; i < N; ++i)
362 if (src.data[i] < data[i]) data[i] = src.data[i];
363 return *this;
364 }
365};
366
367// Element-wise sum of a 6x6 matrix of doubles.
369 double data[6][6];
370
371 KOKKOS_INLINE_FUNCTION
373 for (int i = 0; i < 6; ++i)
374 for (int j = 0; j < 6; ++j)
375 data[i][j] = 0.0;
376 }
377
378 KOKKOS_INLINE_FUNCTION
380 for (int i = 0; i < 6; ++i)
381 for (int j = 0; j < 6; ++j)
382 data[i][j] += src.data[i][j];
383 return *this;
384 }
385};
386
387namespace Kokkos {
388 template <int N>
389 struct reduction_identity<SumArray<N>> {
390 KOKKOS_FORCEINLINE_FUNCTION static SumArray<N> sum() { return SumArray<N>(); }
391 };
392 template <int N>
393 struct reduction_identity<MaxArray<N>> {
394 KOKKOS_FORCEINLINE_FUNCTION static MaxArray<N> sum() { return MaxArray<N>(); }
395 };
396 template <int N>
397 struct reduction_identity<MinArray<N>> {
398 KOKKOS_FORCEINLINE_FUNCTION static MinArray<N> sum() { return MinArray<N>(); }
399 };
400 template <>
401 struct reduction_identity<SumMatrix6x6> {
402 KOKKOS_FORCEINLINE_FUNCTION static SumMatrix6x6 sum() { return SumMatrix6x6(); }
403 };
404} // namespace Kokkos
405
406// ---------------------------------------------------------------------------
407
408#endif
std::pair< Vector_t< double, 3 >, Vector_t< double, 3 > > VectorPair_t
ippl::Vector< T, Dim > Vector_t
typename ippl::detail::ViewType< ippl::Vector< double, Dim >, 1 >::view_type view_type
void fillMembers(std::vector< double > &)
matrix6x6_t getMoments6x6() const
Vector_t< double, 3 > sixtyEightPercentile_m
double getMeanKineticEnergy() const
Vector_t< double, 3 > get68Percentile() const
Vector_t< double, 3 > getMinPosition() const
Vector_t< double, 3 > getMeanMomentum() const
Vector_t< double, 3 > ninetyFivePercentile_m
void computeDebyeLength(ippl::ParticleAttrib< Vector_t< double, 3 > >::view_type Pview, size_t Np, size_t Nlocal, double density)
Vector_t< double, 3 > getStandardDeviationPosition() const
Vector_t< double, 3 > getNormalizedEmittance() const
Vector_t< double, 3 > get99_99Percentile() const
bool getRescaleEnergyToReference() const
std::vector< Vector_t< double, 2 > >::const_iterator iterator_t
double getTotalCharge() const
Vector_t< double, 3 > minR_m
static const double percentileFourSigmasNormalDist_m
double getTemperature() const
void computeMoments(ippl::ParticleAttrib< Vector_t< double, 3 > >::view_type Rview, ippl::ParticleAttrib< Vector_t< double, 3 > >::view_type Pview, ippl::ParticleAttrib< double >::view_type Mview, size_t Np, size_t Nlocal)
double getDebyeLength() const
void computeMeans(ippl::ParticleAttrib< Vector_t< double, 3 > >::view_type Rview, ippl::ParticleAttrib< Vector_t< double, 3 > >::view_type Pview, ippl::ParticleAttrib< double >::view_type Mview, size_t Np, size_t Nlocal)
void computeMinMaxPosition(ippl::ParticleAttrib< Vector_t< double, 3 > >::view_type Rview, size_t Nlcoal)
Vector_t< double, 3 > getHalo() const
Vector_t< double, 3 > ninetyNinePercentile_m
void compute(const std::vector< OpalParticle >::const_iterator &, const std::vector< OpalParticle >::const_iterator &)
double getStdKineticEnergy() const
Vector_t< double, 3 > getNormalizedEmittance99Percentile() const
Vector_t< double, 3 > getStandardDeviationMomentum() const
Vector_t< double, 3 > getMeanPosition() const
Vector_t< double, 3 > getStandardDeviationRP() const
Vector_t< double, 3 > ninetyNine_NinetyNinePercentile_m
double getPlasmaParameter() const
Vector_t< double, 3 > normalizedEps_m
Vector_t< double, 3 > normalizedEps68Percentile_m
Vector_t< double, 3 > meanP_m
Vector_t< double, 3 > getNormalizedEmittance99_99Percentile() const
double computeNormalizedEmittance(const iterator_t &begin, const iterator_t &end) const
Vector_t< double, 3 > halo_m
double getTotalNumParticles() const
Vector_t< double, 3 > stdP_m
Vector_t< double, 3 > normalizedEps99Percentile_m
Vector_t< double, 3 > getMaxPosition() const
Vector_t< double, 3 > stdR_m
static const double percentileThreeSigmasNormalDist_m
double getEnergyReferenceMassGeV() const
Vector_t< double, 3 > getNormalizedEmittance95Percentile() const
Vector_t< double, 3 > getGeometricEmittance() const
Vector_t< double, 3 > meanR_m
void computePercentiles(const InputIt &, const InputIt &)
static const double percentileTwoSigmasNormalDist_m
Vector_t< double, 3 > get95Percentile() const
Vector_t< double, 6 > getCentroid() const
Vector_t< double, 3 > getNormalizedEmittance68Percentile() const
Vector_t< double, 3 > normalizedEps99_99Percentile_m
bool isParticleExcluded(const OpalParticle &) const
std::weak_ptr< BunchStateHandler::ContainerState > containerState_m
void setContainerState(std::weak_ptr< BunchStateHandler::ContainerState > containerState)
Vector_t< double, 3 > maxR_m
Vector_t< double, 3 > stdRP_m
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
Vector_t< double, 3 > geometricEps_m
static const double percentileOneSigmaNormalDist_m
Vector_t< double, 3 > get99Percentile() const
void setEnergyReferenceMass(double referenceMassGeV, bool rescaleToReference=true)
Vector_t< double, 6 > getMeans() const
Vector_t< double, 6 > means_m
Vector_t< double, 6 > centroid_m
Vector_t< double, 3 > getMaxR() const
Vector_t< double, 3 > normalizedEps95Percentile_m
constexpr double kB
Boltzman's constant in eV/K.
Definition Physics.h:75
constexpr double c
The velocity of light in m/s.
Definition Physics.h:60
constexpr double eV2kg
Definition Units.h:110
static KOKKOS_FORCEINLINE_FUNCTION MaxArray< N > sum()
static KOKKOS_FORCEINLINE_FUNCTION MinArray< N > sum()
static KOKKOS_FORCEINLINE_FUNCTION SumArray< N > sum()
static KOKKOS_FORCEINLINE_FUNCTION SumMatrix6x6 sum()
KOKKOS_INLINE_FUNCTION MaxArray()
KOKKOS_INLINE_FUNCTION MaxArray & operator+=(const MaxArray &src)
KOKKOS_INLINE_FUNCTION MinArray()
KOKKOS_INLINE_FUNCTION MinArray & operator+=(const MinArray &src)
KOKKOS_INLINE_FUNCTION SumArray()
KOKKOS_INLINE_FUNCTION SumArray & operator+=(const SumArray &src)
KOKKOS_INLINE_FUNCTION SumMatrix6x6()
KOKKOS_INLINE_FUNCTION SumMatrix6x6 & operator+=(const SumMatrix6x6 &src)