WarpX
UpdateMomentumPerezElastic.H
Go to the documentation of this file.
1 /* Copyright 2019 Yinjian Zhao
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 #ifndef WARPX_PARTICLES_COLLISION_UPDATE_MOMENTUM_PEREZ_ELASTIC_H_
8 #define WARPX_PARTICLES_COLLISION_UPDATE_MOMENTUM_PEREZ_ELASTIC_H_
9 
10 #include "Utils/WarpXConst.H"
11 
12 #include <AMReX_Math.H>
13 #include <AMReX_Random.H>
14 
15 #include <cmath> // isnan() isinf()
16 #include <limits> // numeric_limits<float>::min()
17 
18 /* \brief Update particle velocities according to
19  * F. Perez et al., Phys.Plasmas.19.083104 (2012),
20  * which is based on Nanbu's method, PhysRevE.55.4642 (1997).
21  * @param[in] LmdD is max(Debye length, minimal interparticle distance).
22  * @param[in] L is the Coulomb log. A fixed L will be used if L > 0,
23  * otherwise L will be calculated based on the algorithm.
24  * To see if there are nan or inf updated velocities,
25  * compile with USE_ASSERTION=TRUE.
26  *
27  * Updates and corrections to the original publication are documented in
28  * https://github.com/ECP-WarpX/WarpX/issues/429
29  * https://github.com/ECP-WarpX/WarpX/files/3799803/main.pdf
30  */
31 
32 template <typename T_PR, typename T_R>
35  T_PR& u1x, T_PR& u1y, T_PR& u1z, T_PR& u2x, T_PR& u2y, T_PR& u2z,
36  T_PR const n1, T_PR const n2, T_PR const n12,
37  T_PR const q1, T_PR const m1, T_PR const w1,
38  T_PR const q2, T_PR const m2, T_PR const w2,
39  T_R const dt, T_PR const L, T_PR const lmdD,
40  amrex::RandomEngine const& engine)
41 {
42 
43  T_PR const diffx = amrex::Math::abs(u1x-u2x);
44  T_PR const diffy = amrex::Math::abs(u1y-u2y);
45  T_PR const diffz = amrex::Math::abs(u1z-u2z);
46  T_PR const diffm = std::sqrt(diffx*diffx+diffy*diffy+diffz*diffz);
47  T_PR const summm = std::sqrt(u1x*u1x+u1y*u1y+u1z*u1z) + std::sqrt(u2x*u2x+u2y*u2y+u2z*u2z);
48  // If g = u1 - u2 = 0, do not collide.
49  // Or if the relative difference is less than 1.0e-10.
50  if ( diffm < std::numeric_limits<T_PR>::min() || diffm/summm < 1.0e-10 ) { return; }
51 
52  T_PR constexpr inv_c2 = T_PR(1.0) / ( PhysConst::c * PhysConst::c );
53 
54  // Compute Lorentz factor gamma
55  T_PR const g1 = std::sqrt( T_PR(1.0) + (u1x*u1x+u1y*u1y+u1z*u1z)*inv_c2 );
56  T_PR const g2 = std::sqrt( T_PR(1.0) + (u2x*u2x+u2y*u2y+u2z*u2z)*inv_c2 );
57 
58  // Compute momenta
59  T_PR const p1x = u1x * m1;
60  T_PR const p1y = u1y * m1;
61  T_PR const p1z = u1z * m1;
62  T_PR const p2x = u2x * m2;
63  T_PR const p2y = u2y * m2;
64  T_PR const p2z = u2z * m2;
65 
66  // Compute center-of-mass (COM) velocity and gamma
67  T_PR const mass_g = m1 * g1 + m2 * g2;
68  T_PR const vcx = (p1x+p2x) / mass_g;
69  T_PR const vcy = (p1y+p2y) / mass_g;
70  T_PR const vcz = (p1z+p2z) / mass_g;
71  T_PR const vcms = vcx*vcx + vcy*vcy + vcz*vcz;
72  T_PR const gc = T_PR(1.0) / std::sqrt( T_PR(1.0) - vcms*inv_c2 );
73 
74  // Compute vc dot v1 and v2
75  T_PR const vcDv1 = (vcx*u1x + vcy*u1y + vcz*u1z) / g1;
76  T_PR const vcDv2 = (vcx*u2x + vcy*u2y + vcz*u2z) / g2;
77 
78  // Compute p1 star
79  T_PR p1sx;
80  T_PR p1sy;
81  T_PR p1sz;
82  if ( vcms > std::numeric_limits<T_PR>::min() )
83  {
84  /* lorentz_transform_factor = ( (gc-1.0)/vcms*vcDv1 - gc )*m1*g1;
85  * Rewrite to avoid loss of precision from subtracting similar
86  * numbers when gc is close to 1 */
87  T_PR const lorentz_transform_factor =
88  ( (gc*gc*vcms*inv_c2/(T_PR(1.0) + gc))/vcms*vcDv1 - gc )*m1*g1;
89  p1sx = p1x + vcx*lorentz_transform_factor;
90  p1sy = p1y + vcy*lorentz_transform_factor;
91  p1sz = p1z + vcz*lorentz_transform_factor;
92  }
93  else // If vcms = 0, don't do Lorentz-transform.
94  {
95  p1sx = p1x;
96  p1sy = p1y;
97  p1sz = p1z;
98  }
99  T_PR const p1sm = std::sqrt( p1sx*p1sx + p1sy*p1sy + p1sz*p1sz );
100 
101  // Compute gamma star
102  T_PR const g1s = ( T_PR(1.0) - vcDv1*inv_c2 )*gc*g1;
103  T_PR const g2s = ( T_PR(1.0) - vcDv2*inv_c2 )*gc*g2;
104 
105  // Compute the Coulomb log lnLmd
106  T_PR lnLmd;
107  if ( L > T_PR(0.0) ) { lnLmd = L; }
108  else
109  {
110  // Compute b0 according to eq (22) from Perez et al., Phys.Plasmas.19.083104 (2012)
111  // Note: there is a typo in the equation, the last square is incorrect!
112  // See the SMILEI documentation: https://smileipic.github.io/Smilei/Understand/collisions.html
113  // and https://github.com/ECP-WarpX/WarpX/files/3799803/main.pdf from GitHub #429
114  T_PR const b0 = amrex::Math::abs(q1*q2) * inv_c2 /
115  (T_PR(4.0)*MathConst::pi*PhysConst::ep0) * gc/mass_g *
116  ( m1*g1s*m2*g2s/(p1sm*p1sm*inv_c2) + T_PR(1.0) );
117 
118  // Compute the minimal impact parameter
119  constexpr T_PR hbar_pi = static_cast<T_PR>(PhysConst::hbar*MathConst::pi);
120  const T_PR bmin = amrex::max(hbar_pi/p1sm, b0);
121 
122  // Compute the Coulomb log lnLmd
123  lnLmd = amrex::max( T_PR(2.0),
124  T_PR(0.5)*std::log(T_PR(1.0)+lmdD*lmdD/(bmin*bmin)) );
125  }
126 
127  // Compute s
128  const auto tts = m1*g1s*m2*g2s/(inv_c2*p1sm*p1sm) + T_PR(1.0);
129  const auto tts2 = tts*tts;
130  T_PR s = n1*n2/n12 * dt*lnLmd*q1*q1*q2*q2 /
131  ( T_PR(4.0) * MathConst::pi * PhysConst::ep0 * PhysConst::ep0 *
132  m1*g1*m2*g2/(inv_c2*inv_c2) ) * gc*p1sm/mass_g * tts2;
133 
134  // Compute s'
135  const auto cbrt_n1 = std::cbrt(n1);
136  const auto cbrt_n2 = std::cbrt(n2);
137  const auto coeff = static_cast<T_PR>(
138  std::pow(4.0*MathConst::pi/3.0,1.0/3.0));
139  T_PR const vrel = mass_g*p1sm/(m1*g1s*m2*g2s*gc);
140  T_PR const sp = coeff * n1*n2/n12 * dt * vrel * (m1+m2) /
141  amrex::max( m1*cbrt_n1*cbrt_n1,
142  m2*cbrt_n2*cbrt_n2);
143 
144  // Determine s
145  s = amrex::min(s,sp);
146 
147  // Get random numbers
148  T_PR r = amrex::Random(engine);
149 
150  // Compute scattering angle
151  T_PR cosXs;
152  T_PR sinXs;
153  if ( s <= T_PR(0.1) )
154  {
155  while ( true )
156  {
157  cosXs = T_PR(1.0) + s * std::log(r);
158  // Avoid the bug when r is too small such that cosXs < -1
159  if ( cosXs >= T_PR(-1.0) ) { break; }
160  r = amrex::Random(engine);
161  }
162  }
163  else if ( s > T_PR(0.1) && s <= T_PR(3.0) )
164  {
165  T_PR const Ainv = static_cast<T_PR>(
166  0.0056958 + 0.9560202*s - 0.508139*s*s +
167  0.47913906*s*s*s - 0.12788975*s*s*s*s + 0.02389567*s*s*s*s*s);
168  cosXs = Ainv * std::log( std::exp(T_PR(-1.0)/Ainv) +
169  T_PR(2.0) * r * std::sinh(T_PR(1.0)/Ainv) );
170  }
171  else if ( s > T_PR(3.0) && s <= T_PR(6.0) )
172  {
173  T_PR const A = T_PR(3.0) * std::exp(-s);
174  cosXs = T_PR(1.0)/A * std::log( std::exp(-A) +
175  T_PR(2.0) * r * std::sinh(A) );
176  }
177  else
178  {
179  cosXs = T_PR(2.0) * r - T_PR(1.0);
180  }
181  sinXs = std::sqrt(T_PR(1.0) - cosXs*cosXs);
182 
183  // Get random azimuthal angle
184  T_PR const phis = amrex::Random(engine) * T_PR(2.0) * MathConst::pi;
185  T_PR const cosphis = std::cos(phis);
186  T_PR const sinphis = std::sin(phis);
187 
188  // Compute post-collision momenta pfs in COM
189  T_PR p1fsx;
190  T_PR p1fsy;
191  T_PR p1fsz;
192  // p1sp is the p1s perpendicular
193  T_PR p1sp = std::sqrt( p1sx*p1sx + p1sy*p1sy );
194  // Make sure p1sp is not almost zero
195  if ( p1sp > std::numeric_limits<T_PR>::min() )
196  {
197  p1fsx = ( p1sx*p1sz/p1sp ) * sinXs*cosphis +
198  ( p1sy*p1sm/p1sp ) * sinXs*sinphis +
199  ( p1sx ) * cosXs;
200  p1fsy = ( p1sy*p1sz/p1sp ) * sinXs*cosphis +
201  (-p1sx*p1sm/p1sp ) * sinXs*sinphis +
202  ( p1sy ) * cosXs;
203  p1fsz = (-p1sp ) * sinXs*cosphis +
204  ( T_PR(0.0) ) * sinXs*sinphis +
205  ( p1sz ) * cosXs;
206  // Note a negative sign is different from
207  // Eq. (12) in Perez's paper,
208  // but they are the same due to the random nature of phis.
209  }
210  else
211  {
212  // If the previous p1sp is almost zero
213  // x->y y->z z->x
214  // This set is equivalent to the one in Nanbu's paper
215  p1sp = std::sqrt( p1sy*p1sy + p1sz*p1sz );
216  p1fsy = ( p1sy*p1sx/p1sp ) * sinXs*cosphis +
217  ( p1sz*p1sm/p1sp ) * sinXs*sinphis +
218  ( p1sy ) * cosXs;
219  p1fsz = ( p1sz*p1sx/p1sp ) * sinXs*cosphis +
220  (-p1sy*p1sm/p1sp ) * sinXs*sinphis +
221  ( p1sz ) * cosXs;
222  p1fsx = (-p1sp ) * sinXs*cosphis +
223  ( T_PR(0.0) ) * sinXs*sinphis +
224  ( p1sx ) * cosXs;
225  }
226 
227  T_PR const p2fsx = -p1fsx;
228  T_PR const p2fsy = -p1fsy;
229  T_PR const p2fsz = -p1fsz;
230 
231  // Transform from COM to lab frame
232  T_PR p1fx; T_PR p2fx;
233  T_PR p1fy; T_PR p2fy;
234  T_PR p1fz; T_PR p2fz;
235  if ( vcms > std::numeric_limits<T_PR>::min() )
236  {
237  T_PR const vcDp1fs = vcx*p1fsx + vcy*p1fsy + vcz*p1fsz;
238  T_PR const vcDp2fs = vcx*p2fsx + vcy*p2fsy + vcz*p2fsz;
239  /* factor = (gc-1.0)/vcms; Rewrite to avoid subtraction losing precision when gc is close to 1 */
240  T_PR const factor = gc*gc*inv_c2/(gc+T_PR(1.0));
241  T_PR const factor1 = factor*vcDp1fs + m1*g1s*gc;
242  T_PR const factor2 = factor*vcDp2fs + m2*g2s*gc;
243  p1fx = p1fsx + vcx * factor1;
244  p1fy = p1fsy + vcy * factor1;
245  p1fz = p1fsz + vcz * factor1;
246  p2fx = p2fsx + vcx * factor2;
247  p2fy = p2fsy + vcy * factor2;
248  p2fz = p2fsz + vcz * factor2;
249  }
250  else // If vcms = 0, don't do Lorentz-transform.
251  {
252  p1fx = p1fsx;
253  p1fy = p1fsy;
254  p1fz = p1fsz;
255  p2fx = p2fsx;
256  p2fy = p2fsy;
257  p2fz = p2fsz;
258  }
259 
260  // Rejection method
261  r = amrex::Random(engine);
262  if ( w2 > r*amrex::max(w1, w2) )
263  {
264  u1x = p1fx / m1;
265  u1y = p1fy / m1;
266  u1z = p1fz / m1;
267 #ifndef AMREX_USE_DPCPP
268  AMREX_ASSERT(!std::isnan(u1x+u1y+u1z+u2x+u2y+u2z));
269  AMREX_ASSERT(!std::isinf(u1x+u1y+u1z+u2x+u2y+u2z));
270 #endif
271  }
272  r = amrex::Random(engine);
273  if ( w1 > r*amrex::max(w1, w2) )
274  {
275  u2x = p2fx / m2;
276  u2y = p2fy / m2;
277  u2z = p2fz / m2;
278 #ifndef AMREX_USE_DPCPP
279  AMREX_ASSERT(!std::isnan(u1x+u1y+u1z+u2x+u2y+u2z));
280  AMREX_ASSERT(!std::isinf(u1x+u1y+u1z+u2x+u2y+u2z));
281 #endif
282  }
283 
284 }
285 
286 #endif // WARPX_PARTICLES_COLLISION_UPDATE_MOMENTUM_PEREZ_ELASTIC_H_
#define AMREX_ASSERT(EX)
#define AMREX_INLINE
#define AMREX_GPU_HOST_DEVICE
AMREX_GPU_HOST_DEVICE AMREX_INLINE void UpdateMomentumPerezElastic(T_PR &u1x, T_PR &u1y, T_PR &u1z, T_PR &u2x, T_PR &u2y, T_PR &u2z, T_PR const n1, T_PR const n2, T_PR const n12, T_PR const q1, T_PR const m1, T_PR const w1, T_PR const q2, T_PR const m2, T_PR const w2, T_R const dt, T_PR const L, T_PR const lmdD, amrex::RandomEngine const &engine)
Definition: UpdateMomentumPerezElastic.H:34
static constexpr auto c
vacuum speed of light [m/s]
Definition: constant.H:44
static constexpr auto ep0
vacuum permittivity: dielectric permittivity of vacuum [F/m]
Definition: constant.H:46
static constexpr auto hbar
reduced Planck Constant = h / tau [J*s]
Definition: constant.H:59
Real Random()
AMREX_GPU_HOST_DEVICE constexpr AMREX_FORCE_INLINE const T & max(const T &a, const T &b) noexcept
AMREX_GPU_HOST_DEVICE constexpr AMREX_FORCE_INLINE const T & min(const T &a, const T &b) noexcept
s
Definition: plot_results.py:104
float dt
Definition: stencil.py:440
sp
Definition: stencil.py:25