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 const lev, amrex::MFIter const& mfi,
34 
45  void getEnergy ( amrex::ParticleReal const u2, double const mass,
46  double& energy )
47  {
48  using std::sqrt;
49  using namespace amrex::literals;
50 
51  constexpr auto c2 = PhysConst::c * PhysConst::c;
52  energy = mass * u2 / (sqrt(1.0_rt + u2 / c2) + 1.0_rt) / PhysConst::q_e;
53  }
54 
66  void getCollisionEnergy ( amrex::ParticleReal const u2, double const m,
67  double const M, double& gamma, double& energy )
68  {
69  using std::sqrt;
70  using namespace amrex::literals;
71 
72  constexpr auto c2 = PhysConst::c * PhysConst::c;
73 
74  gamma = sqrt(1.0_rt + u2 / c2);
75  energy = (
76  2.0_rt * m * M * u2 / (gamma + 1.0_rt)
77  / (M + m + sqrt(m*m + M*M + 2.0_rt * m * M * gamma))
78  ) / PhysConst::q_e;
79  }
80 
91  void doLorentzTransform ( amrex::ParticleReal& ux, amrex::ParticleReal& uy,
92  amrex::ParticleReal& uz,
93  amrex::ParticleReal const Vx, amrex::ParticleReal const Vy,
94  amrex::ParticleReal const Vz )
95  {
96  // precompute repeatedly used quantities
97  constexpr auto c2 = PhysConst::c * PhysConst::c;
98  const auto V2 = (Vx*Vx + Vy*Vy + Vz*Vz);
99  const auto gamma_V = 1.0_prt / sqrt(1.0_prt - V2 / c2);
100  const auto gamma_u = sqrt(1.0_prt + (ux*ux + uy*uy + uz*uz) / c2);
101 
102  // copy velocity vector values
103  const auto vx = ux;
104  const auto vy = uy;
105  const auto vz = uz;
106 
107  ux = vx * (1.0_prt + (gamma_V - 1.0_prt) * Vx*Vx/V2)
108  + vy * (gamma_V - 1.0_prt) * Vx*Vy/V2
109  + vz * (gamma_V - 1.0_prt) * Vx*Vz/V2
110  - gamma_V * Vx * gamma_u;
111 
112  uy = vy * (1.0_prt + (gamma_V - 1.0_prt) * Vy*Vy/V2)
113  + vx * (gamma_V - 1.0_prt) * Vx*Vy/V2
114  + vz * (gamma_V - 1.0_prt) * Vy*Vz/V2
115  - gamma_V * Vy * gamma_u;
116 
117  uz = vz * (1.0_prt + (gamma_V - 1.0_prt) * Vz*Vz/V2)
118  + vx * (gamma_V - 1.0_prt) * Vx*Vz/V2
119  + vy * (gamma_V - 1.0_prt) * Vy*Vz/V2
120  - gamma_V * Vz * gamma_u;
121  }
122 
133  void getRandomVector ( amrex::ParticleReal& x, amrex::ParticleReal& y,
134  amrex::ParticleReal& z, amrex::RandomEngine const& engine )
135  {
136  using std::sqrt;
137  using std::cos;
138  using std::sin;
139  using namespace amrex::literals;
140 
141  auto const theta = amrex::Random(engine) * 2.0_prt * MathConst::pi;
142  z = 2.0_prt * amrex::Random(engine) - 1.0_prt;
143  auto const xy = sqrt(1_prt - z*z);
144  x = xy * cos(theta);
145  y = xy * sin(theta);
146  }
147 
148 
158  void RandomizeVelocity ( amrex::ParticleReal& ux, amrex::ParticleReal& uy,
159  amrex::ParticleReal& uz,
160  const amrex::ParticleReal vp,
161  amrex::RandomEngine const& engine )
162  {
163  amrex::ParticleReal x, y, z;
164  // generate random unit vector for the new velocity direction
165  getRandomVector(x, y, z, engine);
166 
167  // scale new vector to have the desired magnitude
168  ux = x * vp;
169  uy = y * vp;
170  uz = z * vp;
171  }
172 }
173 
174 #endif // WARPX_PARTICLE_UTILS_H_
static constexpr auto q_e
elementary charge [C]
Definition: constant.H:50
int gamma
Definition: Stencil.py:474
Real Random()
def x
Definition: read_lab_particles.py:26
static constexpr auto c
vacuum speed of light [m/s]
Definition: constant.H:44
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.html.
Definition: ParticleUtils.H:133
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:158
def uz
Definition: read_lab_particles.py:30
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:91
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:66
def z
Definition: read_lab_particles.py:27
#define AMREX_GPU_HOST_DEVICE
#define AMREX_INLINE
Definition: ParticleUtils.cpp:25
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:45
static constexpr amrex::Real pi
ratio of a circle&#39;s circumference to its diameter
Definition: constant.H:23
ParticleBins findParticlesInEachCell(int const lev, MFIter const &mfi, ParticleTileType const &ptile)
Find the particles and count the particles that are in each cell. More specifically this function ret...
Definition: ParticleUtils.cpp:37
def ux
Definition: read_lab_particles.py:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GpuComplex< T > sqrt(const GpuComplex< T > &a_z) noexcept