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 #include <AMReX_Random.H>
12 #include <AMReX_Math.H>
13 
14 #include <cmath> // isnan() isinf()
15 #include <limits> // numeric_limits<float>::min()
16 
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 
28 template <typename T_R>
29 AMREX_GPU_HOST_DEVICE AMREX_INLINE
31  T_R& u1x, T_R& u1y, T_R& u1z, T_R& u2x, T_R& u2y, T_R& u2z,
32  T_R const n1, T_R const n2, T_R const n12,
33  T_R const q1, T_R const m1, T_R const w1,
34  T_R const q2, T_R const m2, T_R const w2,
35  T_R const dt, T_R const L, T_R const lmdD,
36  amrex::RandomEngine const& engine)
37 {
38 
39  // If g = u1 - u2 = 0, do not collide.
40  if ( amrex::Math::abs(u1x-u2x) < std::numeric_limits<T_R>::min() &&
41  amrex::Math::abs(u1y-u2y) < std::numeric_limits<T_R>::min() &&
42  amrex::Math::abs(u1z-u2z) < std::numeric_limits<T_R>::min() )
43  { return; }
44 
45  T_R constexpr inv_c2 = T_R(1.0) / ( PhysConst::c * PhysConst::c );
46 
47  // Compute Lorentz factor gamma
48  T_R const g1 = std::sqrt( T_R(1.0) + (u1x*u1x+u1y*u1y+u1z*u1z)*inv_c2 );
49  T_R const g2 = std::sqrt( T_R(1.0) + (u2x*u2x+u2y*u2y+u2z*u2z)*inv_c2 );
50 
51  // Compute momenta
52  T_R const p1x = u1x * m1;
53  T_R const p1y = u1y * m1;
54  T_R const p1z = u1z * m1;
55  T_R const p2x = u2x * m2;
56  T_R const p2y = u2y * m2;
57  T_R const p2z = u2z * m2;
58 
59  // Compute center-of-mass (COM) velocity and gamma
60  T_R const mass_g = m1 * g1 + m2 * g2;
61  T_R const vcx = (p1x+p2x) / mass_g;
62  T_R const vcy = (p1y+p2y) / mass_g;
63  T_R const vcz = (p1z+p2z) / mass_g;
64  T_R const vcms = vcx*vcx + vcy*vcy + vcz*vcz;
65  T_R const gc = T_R(1.0) / std::sqrt( T_R(1.0) - vcms*inv_c2 );
66 
67  // Compute vc dot v1 and v2
68  T_R const vcDv1 = (vcx*u1x + vcy*u1y + vcz*u1z) / g1;
69  T_R const vcDv2 = (vcx*u2x + vcy*u2y + vcz*u2z) / g2;
70 
71  // Compute p1 star
72  T_R p1sx;
73  T_R p1sy;
74  T_R p1sz;
75  if ( vcms > std::numeric_limits<T_R>::min() )
76  {
77  T_R const lorentz_tansform_factor =
78  ( (gc-T_R(1.0))/vcms*vcDv1 - gc )*m1*g1;
79  p1sx = p1x + vcx*lorentz_tansform_factor;
80  p1sy = p1y + vcy*lorentz_tansform_factor;
81  p1sz = p1z + vcz*lorentz_tansform_factor;
82  }
83  else // If vcms = 0, don't do Lorentz-transform.
84  {
85  p1sx = p1x;
86  p1sy = p1y;
87  p1sz = p1z;
88  }
89  T_R const p1sm = std::sqrt( p1sx*p1sx + p1sy*p1sy + p1sz*p1sz );
90 
91  // Compute gamma star
92  T_R const g1s = ( T_R(1.0) - vcDv1*inv_c2 )*gc*g1;
93  T_R const g2s = ( T_R(1.0) - vcDv2*inv_c2 )*gc*g2;
94 
95  // Compute the Coulomb log lnLmd
96  T_R lnLmd;
97  if ( L > T_R(0.0) ) { lnLmd = L; }
98  else
99  {
100  // Compute b0
101  T_R const b0 = amrex::Math::abs(q1*q2) * inv_c2 /
102  (T_R(4.0)*MathConst::pi*PhysConst::ep0) * gc/mass_g *
103  ( m1*g1s*m2*g2s/(p1sm*p1sm*inv_c2) + T_R(1.0) );
104 
105  // Compute the minimal impact parameter
106  T_R bmin = amrex::max(PhysConst::hbar*MathConst::pi/p1sm,b0);
107 
108  // Compute the Coulomb log lnLmd
109  lnLmd = amrex::max( T_R(2.0),
110  T_R(0.5)*std::log(T_R(1.0)+lmdD*lmdD/(bmin*bmin)) );
111  }
112 
113  // Compute s
114  T_R s = n1*n2/n12 * dt*lnLmd*q1*q1*q2*q2 /
115  ( T_R(4.0) * MathConst::pi * PhysConst::ep0 * PhysConst::ep0 *
116  m1*g1*m2*g2/(inv_c2*inv_c2) ) * gc*p1sm/mass_g *
117  std::pow(m1*g1s*m2*g2s/(inv_c2*p1sm*p1sm) + T_R(1.0), 2.0);
118 
119  // Compute s'
120  T_R const vrel = mass_g*p1sm/(m1*g1s*m2*g2s*gc);
121  T_R const sp = std::pow(T_R(4.0)*MathConst::pi/T_R(3.0),T_R(1.0/3.0)) *
122  n1*n2/n12 * dt * vrel * (m1+m2) /
123  amrex::max( m1*std::pow(n1,T_R(2.0/3.0)),
124  m2*std::pow(n2,T_R(2.0/3.0)) );
125 
126  // Determine s
127  s = amrex::min(s,sp);
128 
129  // Get random numbers
130  T_R r = amrex::Random(engine);
131 
132  // Compute scattering angle
133  T_R cosXs;
134  T_R sinXs;
135  if ( s <= T_R(0.1) )
136  {
137  while ( true )
138  {
139  cosXs = T_R(1.0) + s * std::log(r);
140  // Avoid the bug when r is too small such that cosXs < -1
141  if ( cosXs >= T_R(-1.0) ) { break; }
142  r = amrex::Random(engine);
143  }
144  }
145  else if ( s > T_R(0.1) && s <= T_R(3.0) )
146  {
147  T_R const Ainv = 0.0056958 + 0.9560202*s - 0.508139*s*s +
148  0.47913906*s*s*s - 0.12788975*s*s*s*s + 0.02389567*s*s*s*s*s;
149  cosXs = Ainv * std::log( std::exp(T_R(-1.0)/Ainv) +
150  T_R(2.0) * r * std::sinh(T_R(1.0)/Ainv) );
151  }
152  else if ( s > T_R(3.0) && s <= T_R(6.0) )
153  {
154  T_R const A = T_R(3.0) * std::exp(-s);
155  cosXs = T_R(1.0)/A * std::log( std::exp(-A) +
156  T_R(2.0) * r * std::sinh(A) );
157  }
158  else
159  {
160  cosXs = T_R(2.0) * r - T_R(1.0);
161  }
162  sinXs = std::sqrt(T_R(1.0) - cosXs*cosXs);
163 
164  // Get random azimuthal angle
165  T_R const phis = amrex::Random(engine) * T_R(2.0) * MathConst::pi;
166  T_R const cosphis = std::cos(phis);
167  T_R const sinphis = std::sin(phis);
168 
169  // Compute post-collision momenta pfs in COM
170  T_R p1fsx;
171  T_R p1fsy;
172  T_R p1fsz;
173  // p1sp is the p1s perpendicular
174  T_R p1sp = std::sqrt( p1sx*p1sx + p1sy*p1sy );
175  // Make sure p1sp is not almost zero
176  if ( p1sp > std::numeric_limits<T_R>::min() )
177  {
178  p1fsx = ( p1sx*p1sz/p1sp ) * sinXs*cosphis +
179  ( p1sy*p1sm/p1sp ) * sinXs*sinphis +
180  ( p1sx ) * cosXs;
181  p1fsy = ( p1sy*p1sz/p1sp ) * sinXs*cosphis +
182  (-p1sx*p1sm/p1sp ) * sinXs*sinphis +
183  ( p1sy ) * cosXs;
184  p1fsz = (-p1sp ) * sinXs*cosphis +
185  ( T_R(0.0) ) * sinXs*sinphis +
186  ( p1sz ) * cosXs;
187  // Note a negative sign is different from
188  // Eq. (12) in Perez's paper,
189  // but they are the same due to the random nature of phis.
190  }
191  else
192  {
193  // If the previous p1sp is almost zero
194  // x->y y->z z->x
195  // This set is equivalent to the one in Nanbu's paper
196  p1sp = std::sqrt( p1sy*p1sy + p1sz*p1sz );
197  p1fsy = ( p1sy*p1sx/p1sp ) * sinXs*cosphis +
198  ( p1sz*p1sm/p1sp ) * sinXs*sinphis +
199  ( p1sy ) * cosXs;
200  p1fsz = ( p1sz*p1sx/p1sp ) * sinXs*cosphis +
201  (-p1sy*p1sm/p1sp ) * sinXs*sinphis +
202  ( p1sz ) * cosXs;
203  p1fsx = (-p1sp ) * sinXs*cosphis +
204  ( T_R(0.0) ) * sinXs*sinphis +
205  ( p1sx ) * cosXs;
206  }
207 
208  T_R const p2fsx = -p1fsx;
209  T_R const p2fsy = -p1fsy;
210  T_R const p2fsz = -p1fsz;
211 
212  // Transform from COM to lab frame
213  T_R p1fx; T_R p2fx;
214  T_R p1fy; T_R p2fy;
215  T_R p1fz; T_R p2fz;
216  if ( vcms > std::numeric_limits<T_R>::min() )
217  {
218  T_R const vcDp1fs = vcx*p1fsx + vcy*p1fsy + vcz*p1fsz;
219  T_R const vcDp2fs = vcx*p2fsx + vcy*p2fsy + vcz*p2fsz;
220  T_R const factor = (gc-T_R(1.0))/vcms;
221  T_R const factor1 = factor*vcDp1fs + m1*g1s*gc;
222  T_R const factor2 = factor*vcDp2fs + m2*g2s*gc;
223  p1fx = p1fsx + vcx * factor1;
224  p1fy = p1fsy + vcy * factor1;
225  p1fz = p1fsz + vcz * factor1;
226  p2fx = p2fsx + vcx * factor2;
227  p2fy = p2fsy + vcy * factor2;
228  p2fz = p2fsz + vcz * factor2;
229  }
230  else // If vcms = 0, don't do Lorentz-transform.
231  {
232  p1fx = p1fsx;
233  p1fy = p1fsy;
234  p1fz = p1fsz;
235  p2fx = p2fsx;
236  p2fy = p2fsy;
237  p2fz = p2fsz;
238  }
239 
240  // Rejection method
241  r = amrex::Random(engine);
242  if ( w2 > r*amrex::max(w1, w2) )
243  {
244  u1x = p1fx / m1;
245  u1y = p1fy / m1;
246  u1z = p1fz / m1;
247 #ifndef AMREX_USE_DPCPP
248  AMREX_ASSERT(!std::isnan(u1x+u1y+u1z+u2x+u2y+u2z));
249  AMREX_ASSERT(!std::isinf(u1x+u1y+u1z+u2x+u2y+u2z));
250 #endif
251  }
252  r = amrex::Random(engine);
253  if ( w1 > r*amrex::max(w1, w2) )
254  {
255  u2x = p2fx / m2;
256  u2y = p2fy / m2;
257  u2z = p2fz / m2;
258 #ifndef AMREX_USE_DPCPP
259  AMREX_ASSERT(!std::isnan(u1x+u1y+u1z+u2x+u2y+u2z));
260  AMREX_ASSERT(!std::isinf(u1x+u1y+u1z+u2x+u2y+u2z));
261 #endif
262  }
263 
264 }
265 
266 #endif // WARPX_PARTICLES_COLLISION_UPDATE_MOMENTUM_PEREZ_ELASTIC_H_
AMREX_GPU_HOST_DEVICE AMREX_INLINE void UpdateMomentumPerezElastic(T_R &u1x, T_R &u1y, T_R &u1z, T_R &u2x, T_R &u2y, T_R &u2z, T_R const n1, T_R const n2, T_R const n12, T_R const q1, T_R const m1, T_R const w1, T_R const q2, T_R const m2, T_R const w2, T_R const dt, T_R const L, T_R const lmdD, amrex::RandomEngine const &engine)
Definition: UpdateMomentumPerezElastic.H:30
s
Definition: plot_results.py:103