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 
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  const auto tts = m1*g1s*m2*g2s/(inv_c2*p1sm*p1sm) + T_R(1.0);
115  const auto tts2 = tts*tts;
116  T_R s = n1*n2/n12 * dt*lnLmd*q1*q1*q2*q2 /
117  ( T_R(4.0) * MathConst::pi * PhysConst::ep0 * PhysConst::ep0 *
118  m1*g1*m2*g2/(inv_c2*inv_c2) ) * gc*p1sm/mass_g * tts2;
119 
120  // Compute s'
121  const auto cbrt_n1 = std::cbrt(n1);
122  const auto cbrt_n2 = std::cbrt(n2);
123  const auto coeff = static_cast<T_R>(
124  std::pow(4.0*MathConst::pi/3.0,1.0/3.0));
125  T_R const vrel = mass_g*p1sm/(m1*g1s*m2*g2s*gc);
126  T_R const sp = coeff * n1*n2/n12 * dt * vrel * (m1+m2) /
127  amrex::max( m1*cbrt_n1*cbrt_n1,
128  m2*cbrt_n2*cbrt_n2);
129 
130  // Determine s
131  s = amrex::min(s,sp);
132 
133  // Get random numbers
134  T_R r = amrex::Random(engine);
135 
136  // Compute scattering angle
137  T_R cosXs;
138  T_R sinXs;
139  if ( s <= T_R(0.1) )
140  {
141  while ( true )
142  {
143  cosXs = T_R(1.0) + s * std::log(r);
144  // Avoid the bug when r is too small such that cosXs < -1
145  if ( cosXs >= T_R(-1.0) ) { break; }
146  r = amrex::Random(engine);
147  }
148  }
149  else if ( s > T_R(0.1) && s <= T_R(3.0) )
150  {
151  T_R const Ainv = static_cast<T_R>(
152  0.0056958 + 0.9560202*s - 0.508139*s*s +
153  0.47913906*s*s*s - 0.12788975*s*s*s*s + 0.02389567*s*s*s*s*s);
154  cosXs = Ainv * std::log( std::exp(T_R(-1.0)/Ainv) +
155  T_R(2.0) * r * std::sinh(T_R(1.0)/Ainv) );
156  }
157  else if ( s > T_R(3.0) && s <= T_R(6.0) )
158  {
159  T_R const A = T_R(3.0) * std::exp(-s);
160  cosXs = T_R(1.0)/A * std::log( std::exp(-A) +
161  T_R(2.0) * r * std::sinh(A) );
162  }
163  else
164  {
165  cosXs = T_R(2.0) * r - T_R(1.0);
166  }
167  sinXs = std::sqrt(T_R(1.0) - cosXs*cosXs);
168 
169  // Get random azimuthal angle
170  T_R const phis = amrex::Random(engine) * T_R(2.0) * MathConst::pi;
171  T_R const cosphis = std::cos(phis);
172  T_R const sinphis = std::sin(phis);
173 
174  // Compute post-collision momenta pfs in COM
175  T_R p1fsx;
176  T_R p1fsy;
177  T_R p1fsz;
178  // p1sp is the p1s perpendicular
179  T_R p1sp = std::sqrt( p1sx*p1sx + p1sy*p1sy );
180  // Make sure p1sp is not almost zero
181  if ( p1sp > std::numeric_limits<T_R>::min() )
182  {
183  p1fsx = ( p1sx*p1sz/p1sp ) * sinXs*cosphis +
184  ( p1sy*p1sm/p1sp ) * sinXs*sinphis +
185  ( p1sx ) * cosXs;
186  p1fsy = ( p1sy*p1sz/p1sp ) * sinXs*cosphis +
187  (-p1sx*p1sm/p1sp ) * sinXs*sinphis +
188  ( p1sy ) * cosXs;
189  p1fsz = (-p1sp ) * sinXs*cosphis +
190  ( T_R(0.0) ) * sinXs*sinphis +
191  ( p1sz ) * cosXs;
192  // Note a negative sign is different from
193  // Eq. (12) in Perez's paper,
194  // but they are the same due to the random nature of phis.
195  }
196  else
197  {
198  // If the previous p1sp is almost zero
199  // x->y y->z z->x
200  // This set is equivalent to the one in Nanbu's paper
201  p1sp = std::sqrt( p1sy*p1sy + p1sz*p1sz );
202  p1fsy = ( p1sy*p1sx/p1sp ) * sinXs*cosphis +
203  ( p1sz*p1sm/p1sp ) * sinXs*sinphis +
204  ( p1sy ) * cosXs;
205  p1fsz = ( p1sz*p1sx/p1sp ) * sinXs*cosphis +
206  (-p1sy*p1sm/p1sp ) * sinXs*sinphis +
207  ( p1sz ) * cosXs;
208  p1fsx = (-p1sp ) * sinXs*cosphis +
209  ( T_R(0.0) ) * sinXs*sinphis +
210  ( p1sx ) * cosXs;
211  }
212 
213  T_R const p2fsx = -p1fsx;
214  T_R const p2fsy = -p1fsy;
215  T_R const p2fsz = -p1fsz;
216 
217  // Transform from COM to lab frame
218  T_R p1fx; T_R p2fx;
219  T_R p1fy; T_R p2fy;
220  T_R p1fz; T_R p2fz;
221  if ( vcms > std::numeric_limits<T_R>::min() )
222  {
223  T_R const vcDp1fs = vcx*p1fsx + vcy*p1fsy + vcz*p1fsz;
224  T_R const vcDp2fs = vcx*p2fsx + vcy*p2fsy + vcz*p2fsz;
225  T_R const factor = (gc-T_R(1.0))/vcms;
226  T_R const factor1 = factor*vcDp1fs + m1*g1s*gc;
227  T_R const factor2 = factor*vcDp2fs + m2*g2s*gc;
228  p1fx = p1fsx + vcx * factor1;
229  p1fy = p1fsy + vcy * factor1;
230  p1fz = p1fsz + vcz * factor1;
231  p2fx = p2fsx + vcx * factor2;
232  p2fy = p2fsy + vcy * factor2;
233  p2fz = p2fsz + vcz * factor2;
234  }
235  else // If vcms = 0, don't do Lorentz-transform.
236  {
237  p1fx = p1fsx;
238  p1fy = p1fsy;
239  p1fz = p1fsz;
240  p2fx = p2fsx;
241  p2fy = p2fsy;
242  p2fz = p2fsz;
243  }
244 
245  // Rejection method
246  r = amrex::Random(engine);
247  if ( w2 > r*amrex::max(w1, w2) )
248  {
249  u1x = p1fx / m1;
250  u1y = p1fy / m1;
251  u1z = p1fz / m1;
252 #ifndef AMREX_USE_DPCPP
253  AMREX_ASSERT(!std::isnan(u1x+u1y+u1z+u2x+u2y+u2z));
254  AMREX_ASSERT(!std::isinf(u1x+u1y+u1z+u2x+u2y+u2z));
255 #endif
256  }
257  r = amrex::Random(engine);
258  if ( w1 > r*amrex::max(w1, w2) )
259  {
260  u2x = p2fx / m2;
261  u2y = p2fy / m2;
262  u2z = p2fz / m2;
263 #ifndef AMREX_USE_DPCPP
264  AMREX_ASSERT(!std::isnan(u1x+u1y+u1z+u2x+u2y+u2z));
265  AMREX_ASSERT(!std::isinf(u1x+u1y+u1z+u2x+u2y+u2z));
266 #endif
267  }
268 
269 }
270 
271 #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