WarpX
VelocityCoincidenceThinning.H
Go to the documentation of this file.
1 /* Copyright 2024 The WarpX Community
2  *
3  * This file is part of WarpX.
4  *
5  * Authors: Roelof Groenewald (TAE Technologies)
6  *
7  * License: BSD-3-Clause-LBNL
8  */
9 #ifndef WARPX_VELOCITY_COINCIDENCE_THINNING_H_
10 #define WARPX_VELOCITY_COINCIDENCE_THINNING_H_
11 
13 #include "Resampling.H"
15 #include "Utils/ParticleUtils.H"
16 
24 public:
25 
30 
36  VelocityCoincidenceThinning (const std::string& species_name);
37 
38  enum struct VelocityGridType {
39  Spherical = 0,
40  Cartesian = 1
41  };
42 
50  void operator() (WarpXParIter& pti, int lev, WarpXParticleContainer* pc) const final;
51 
67  struct HeapSort {
68 
70  void swap(int &a, int &b) const
71  {
72  const auto temp = b;
73  b = a;
74  a = temp;
75  }
76 
78  void operator() (int index_array[], const int bin_array[], const int start, const int n) const
79  {
80  // sort index_array into a max heap structure
81  for (int i = 1; i < n; i++)
82  {
83  auto j = i;
84  // move child through heap if it is bigger than its parent
85  while (j > 0 && bin_array[index_array[j+start]] > bin_array[index_array[(j - 1)/2 + start]]) {
86  // swap child and parent until branch is properly ordered
87  swap(index_array[j+start], index_array[(j - 1)/2 + start]);
88  j = (j - 1) / 2;
89  }
90  }
91 
92  for (int i = n - 1; i > 0; i--)
93  {
94  // swap value of first (now the largest value) to the new end point
95  swap(index_array[start], index_array[i+start]);
96 
97  // remake the max heap
98  int j = 0, index;
99  while (j < i) {
100  index = 2 * j + 1;
101 
102  // if left child is smaller than right child, point index variable to right child
103  if (index + 1 < i && bin_array[index_array[index+start]] < bin_array[index_array[index+1+start]]) {
104  index++;
105  }
106  // if parent is smaller than child, swap parent with child having higher value
107  if (index < i && bin_array[index_array[j+start]] < bin_array[index_array[index+start]]) {
108  swap(index_array[j+start], index_array[index+start]);
109  }
110  j = index;
111  }
112  }
113  }
114  };
115 
121 
123  void labelOnSphericalVelocityGrid (const amrex::ParticleReal ux[],
124  const amrex::ParticleReal uy[],
125  const amrex::ParticleReal uz[],
126  const unsigned int indices[],
127  int bin_array[], int index_array[],
128  const int cell_start, const int cell_stop ) const
129  {
130  for (int i = cell_start; i < cell_stop; ++i)
131  {
132  // get polar components of the velocity vector
133  auto u_mag = std::sqrt(
134  ux[indices[i]]*ux[indices[i]] +
135  uy[indices[i]]*uy[indices[i]] +
136  uz[indices[i]]*uz[indices[i]]
137  );
138  auto u_theta = std::atan2(uy[indices[i]], ux[indices[i]]) + MathConst::pi;
139  auto u_phi = std::acos(uz[indices[i]]/u_mag);
140 
141  const int ii = static_cast<int>(u_theta / dutheta);
142  const int jj = static_cast<int>(u_phi / duphi);
143  const int kk = static_cast<int>(u_mag / dur);
144 
145  bin_array[i] = ii + jj * n1 + kk * n1 * n2;
146  index_array[i] = i;
147  }
148  }
149 
151  void labelOnCartesianVelocityGrid (const amrex::ParticleReal ux[],
152  const amrex::ParticleReal uy[],
153  const amrex::ParticleReal uz[],
154  const unsigned int indices[],
155  int bin_array[], int index_array[],
156  const int cell_start, const int cell_stop ) const
157  {
158  for (int i = cell_start; i < cell_stop; ++i)
159  {
160  const int ii = static_cast<int>((ux[indices[i]] - ux_min) / dux);
161  const int jj = static_cast<int>((uy[indices[i]] - uy_min) / duy);
162  const int kk = static_cast<int>((uz[indices[i]] - uz_min) / duz);
163 
164  bin_array[i] = ii + jj * n1 + kk * n1 * n2;
165  index_array[i] = i;
166  }
167  }
168 
170  void operator() (const amrex::ParticleReal ux[], const amrex::ParticleReal uy[],
171  const amrex::ParticleReal uz[], const unsigned int indices[],
172  int bin_array[], int index_array[],
173  const int cell_start, const int cell_stop) const
174  {
177  ux, uy, uz, indices, bin_array, index_array, cell_start,
178  cell_stop
179  );
180  }
183  ux, uy, uz, indices, bin_array, index_array, cell_start,
184  cell_stop
185  );
186  }
187  }
188 
190  int n1, n2;
191  amrex::ParticleReal dur, dutheta, duphi;
192  amrex::ParticleReal dux, duy, duz;
193  amrex::ParticleReal ux_min, uy_min, uz_min, ux_max, uy_max;
194  };
195 
196 private:
198 
199  int m_min_ppc = 1;
201  amrex::ParticleReal m_delta_ur;
203 };
204 #endif // WARPX_VELOCITY_COINCIDENCE_THINNING_H_
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
This class implements a particle merging scheme wherein particles are clustered in phase space and pa...
Definition: VelocityCoincidenceThinning.H:23
VelocityGridType m_velocity_grid_type
Definition: VelocityCoincidenceThinning.H:197
amrex::ParticleReal m_delta_ur
Definition: VelocityCoincidenceThinning.H:201
VelocityCoincidenceThinning()=default
Default constructor of the VelocityCoincidenceThinning class.
amrex::Vector< amrex::ParticleReal > m_delta_u
Definition: VelocityCoincidenceThinning.H:202
int m_nphi
Definition: VelocityCoincidenceThinning.H:200
int m_min_ppc
Definition: VelocityCoincidenceThinning.H:199
VelocityGridType
Definition: VelocityCoincidenceThinning.H:38
int m_ntheta
Definition: VelocityCoincidenceThinning.H:200
void operator()(WarpXParIter &pti, int lev, WarpXParticleContainer *pc) const final
A method that performs merging for the considered species.
Definition: VelocityCoincidenceThinning.cpp:56
Definition: WarpXParticleContainer.H:53
Definition: WarpXParticleContainer.H:111
i
Definition: check_interp_points_and_weights.py:174
ii
Definition: check_interp_points_and_weights.py:148
jj
Definition: check_interp_points_and_weights.py:160
index
Definition: run_automated.py:328
An empty base class from which specific resampling algorithms are derived.
Definition: Resampling.H:23
This merging routine requires functionality to sort a GPU vector based on another GPU vector's values...
Definition: VelocityCoincidenceThinning.H:67
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(int index_array[], const int bin_array[], const int start, const int n) const
Definition: VelocityCoincidenceThinning.H:78
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void swap(int &a, int &b) const
Definition: VelocityCoincidenceThinning.H:70
Struct used to assign velocity space bin numbers to a given set of particles.
Definition: VelocityCoincidenceThinning.H:120
amrex::ParticleReal ux_min
Definition: VelocityCoincidenceThinning.H:193
amrex::ParticleReal uz_min
Definition: VelocityCoincidenceThinning.H:193
amrex::ParticleReal uy_min
Definition: VelocityCoincidenceThinning.H:193
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void labelOnSphericalVelocityGrid(const amrex::ParticleReal ux[], const amrex::ParticleReal uy[], const amrex::ParticleReal uz[], const unsigned int indices[], int bin_array[], int index_array[], const int cell_start, const int cell_stop) const
Definition: VelocityCoincidenceThinning.H:123
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(const amrex::ParticleReal ux[], const amrex::ParticleReal uy[], const amrex::ParticleReal uz[], const unsigned int indices[], int bin_array[], int index_array[], const int cell_start, const int cell_stop) const
Definition: VelocityCoincidenceThinning.H:170
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void labelOnCartesianVelocityGrid(const amrex::ParticleReal ux[], const amrex::ParticleReal uy[], const amrex::ParticleReal uz[], const unsigned int indices[], int bin_array[], int index_array[], const int cell_start, const int cell_stop) const
Definition: VelocityCoincidenceThinning.H:151
amrex::ParticleReal dutheta
Definition: VelocityCoincidenceThinning.H:191
amrex::ParticleReal duphi
Definition: VelocityCoincidenceThinning.H:191
amrex::ParticleReal duy
Definition: VelocityCoincidenceThinning.H:192
amrex::ParticleReal duz
Definition: VelocityCoincidenceThinning.H:192
amrex::ParticleReal dur
Definition: VelocityCoincidenceThinning.H:191
amrex::ParticleReal uy_max
Definition: VelocityCoincidenceThinning.H:193
int n2
Definition: VelocityCoincidenceThinning.H:190
amrex::ParticleReal dux
Definition: VelocityCoincidenceThinning.H:192
VelocityGridType velocity_grid_type
Definition: VelocityCoincidenceThinning.H:189
amrex::ParticleReal ux_max
Definition: VelocityCoincidenceThinning.H:193
int n1
Definition: VelocityCoincidenceThinning.H:190