WarpX
ParticleUtils.H
Go to the documentation of this file.
1 /* Copyright 2019-2020 Neil Zaim, Yinjian Zhao
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 #ifndef WARPX_PARTICLE_UTILS_H_
8 #define WARPX_PARTICLE_UTILS_H_
9 
11 #include "Utils/WarpXConst.H"
12 
13 #include <AMReX_DenseBins.H>
14 #include <AMReX_Particles.H>
15 
16 #include <AMReX_BaseFwd.H>
17 
18 namespace ParticleUtils {
19 
32  findParticlesInEachCell (int lev,
33  amrex::MFIter const & mfi,
35 
46  void getEnergy ( amrex::ParticleReal const u2, double const mass,
47  double& energy )
48  {
49  using std::sqrt;
50  using namespace amrex::literals;
51 
52  constexpr auto c2 = PhysConst::c * PhysConst::c;
53  energy = mass * u2 / (sqrt(1.0_rt + u2 / c2) + 1.0_rt) / PhysConst::q_e;
54  }
55 
68  void getCollisionEnergy ( amrex::ParticleReal const u2, double const m,
69  double const M, double& gamma, double& energy )
70  {
71  using std::sqrt;
72  using namespace amrex::literals;
73 
74  constexpr auto c2 = PhysConst::c * PhysConst::c;
75 
76  gamma = sqrt(1.0_rt + u2 / c2);
77  energy = (
78  2.0_rt * m * M * u2 / (gamma + 1.0_rt)
79  / (M + m + sqrt(m*m + M*M + 2.0_rt * m * M * gamma))
80  ) / PhysConst::q_e;
81  }
82 
93  void doLorentzTransform ( amrex::ParticleReal& ux, amrex::ParticleReal& uy,
94  amrex::ParticleReal& uz,
95  amrex::ParticleReal const Vx, amrex::ParticleReal const Vy,
96  amrex::ParticleReal const Vz )
97  {
98  using namespace amrex::literals;
99 
100  // precompute repeatedly used quantities
101  constexpr auto c2 = PhysConst::c * PhysConst::c;
102  const auto V2 = (Vx*Vx + Vy*Vy + Vz*Vz);
103  const auto gamma_V = 1.0_prt / std::sqrt(1.0_prt - V2 / c2);
104  const auto gamma_u = std::sqrt(1.0_prt + (ux*ux + uy*uy + uz*uz) / c2);
105 
106  // copy velocity vector values
107  const auto vx = ux;
108  const auto vy = uy;
109  const auto vz = uz;
110 
111  ux = vx * (1.0_prt + (gamma_V - 1.0_prt) * Vx*Vx/V2)
112  + vy * (gamma_V - 1.0_prt) * Vx*Vy/V2
113  + vz * (gamma_V - 1.0_prt) * Vx*Vz/V2
114  - gamma_V * Vx * gamma_u;
115 
116  uy = vy * (1.0_prt + (gamma_V - 1.0_prt) * Vy*Vy/V2)
117  + vx * (gamma_V - 1.0_prt) * Vx*Vy/V2
118  + vz * (gamma_V - 1.0_prt) * Vy*Vz/V2
119  - gamma_V * Vy * gamma_u;
120 
121  uz = vz * (1.0_prt + (gamma_V - 1.0_prt) * Vz*Vz/V2)
122  + vx * (gamma_V - 1.0_prt) * Vx*Vz/V2
123  + vy * (gamma_V - 1.0_prt) * Vy*Vz/V2
124  - gamma_V * Vz * gamma_u;
125  }
126 
137  void getRandomVector ( amrex::ParticleReal& x, amrex::ParticleReal& y,
138  amrex::ParticleReal& z, amrex::RandomEngine const& engine )
139  {
140  using std::sqrt;
141  using std::cos;
142  using std::sin;
143  using namespace amrex::literals;
144 
145  auto const theta = amrex::Random(engine) * 2.0_prt * MathConst::pi;
146  z = 2.0_prt * amrex::Random(engine) - 1.0_prt;
147  auto const xy = sqrt(1_prt - z*z);
148  x = xy * cos(theta);
149  y = xy * sin(theta);
150  }
151 
152 
162  void RandomizeVelocity ( amrex::ParticleReal& ux, amrex::ParticleReal& uy,
163  amrex::ParticleReal& uz,
164  const amrex::ParticleReal vp,
165  amrex::RandomEngine const& engine )
166  {
167  amrex::ParticleReal x, y, z;
168  // generate random unit vector for the new velocity direction
169  getRandomVector(x, y, z, engine);
170 
171  // scale new vector to have the desired magnitude
172  ux = x * vp;
173  uy = y * vp;
174  uz = z * vp;
175  }
176 
177  /* \brief Determines whether the point is within the tilebox, inclusive of the boundaries.
178  * Note that this routine is needed since tilebox.contains excludes the boundaries.
179  * \param[in] tilebox The tilebox being checked
180  * \param[in] point The point being checked
181  * \result true if the point with within the boundary, otherwise false
182  */
184  bool containsInclusive (amrex::RealBox const& tilebox, amrex::XDim3 const point) {
185  const auto *const xlo = tilebox.lo();
186  const auto *const xhi = tilebox.hi();
187  return AMREX_D_TERM((xlo[0] <= point.x) && (point.x <= xhi[0]),
188  && (xlo[1] <= point.y) && (point.y <= xhi[1]),
189  && (xlo[2] <= point.z) && (point.z <= xhi[2]));
190  }
191 
192 }
193 
194 #endif // WARPX_PARTICLE_UTILS_H_
#define AMREX_INLINE
#define AMREX_GPU_HOST_DEVICE
#define AMREX_D_TERM(a, b, c)
AMREX_GPU_HOST_DEVICE const Real * hi() const &noexcept
AMREX_GPU_HOST_DEVICE const Real * lo() const &noexcept
Definition: ParticleUtils.cpp:26
ParticleBins findParticlesInEachCell(int lev, MFIter const &mfi, ParticleTileType &ptile)
Find the particles and count the particles that are in each cell. More specifically this function ret...
Definition: ParticleUtils.cpp:40
AMREX_GPU_HOST_DEVICE AMREX_INLINE void getCollisionEnergy(amrex::ParticleReal const u2, double const m, double const M, double &gamma, double &energy)
Return (relativistic) collision energy assuming the target (with mass M) is stationary and the projec...
Definition: ParticleUtils.H:68
AMREX_GPU_HOST_DEVICE AMREX_INLINE void doLorentzTransform(amrex::ParticleReal &ux, amrex::ParticleReal &uy, amrex::ParticleReal &uz, amrex::ParticleReal const Vx, amrex::ParticleReal const Vy, amrex::ParticleReal const Vz)
Perform a Lorentz transformation of the given velocity to a frame moving with velocity (Vx,...
Definition: ParticleUtils.H:93
AMREX_GPU_HOST_DEVICE AMREX_INLINE void getEnergy(amrex::ParticleReal const u2, double const mass, double &energy)
Return (relativistic) particle energy given velocity and mass. Note the use of double since this calc...
Definition: ParticleUtils.H:46
AMREX_GPU_HOST_DEVICE AMREX_INLINE void RandomizeVelocity(amrex::ParticleReal &ux, amrex::ParticleReal &uy, amrex::ParticleReal &uz, const amrex::ParticleReal vp, amrex::RandomEngine const &engine)
Function to perform scattering of a particle that results in a random velocity vector with given magn...
Definition: ParticleUtils.H:162
AMREX_GPU_HOST_DEVICE AMREX_INLINE void getRandomVector(amrex::ParticleReal &x, amrex::ParticleReal &y, amrex::ParticleReal &z, amrex::RandomEngine const &engine)
Generate random unit vector in 3 dimensions https://mathworld.wolfram.com/SpherePointPicking....
Definition: ParticleUtils.H:137
AMREX_GPU_HOST_DEVICE AMREX_INLINE bool containsInclusive(amrex::RealBox const &tilebox, amrex::XDim3 const point)
Definition: ParticleUtils.H:184
static constexpr auto c
vacuum speed of light [m/s]
Definition: constant.H:44
static constexpr auto q_e
elementary charge [C]
Definition: constant.H:50
Real Random()
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GpuComplex< T > sqrt(const GpuComplex< T > &a_z) noexcept
int gamma
boosted frame
Definition: stencil.py:431