7#ifndef WARPX_PARTICLE_UTILS_H_
8#define WARPX_PARTICLE_UTILS_H_
54 double const M,
double& gamma,
double& energy )
61 2.0_rt * m * M * u2 / (gamma + 1.0_rt)
62 / (M + m +
sqrt(m*m + M*M + 2.0_rt * m * M * gamma))
84 const auto V2 = (Vx*Vx + Vy*Vy + Vz*Vz);
86 const auto gamma_u = std::sqrt(1.0_prt + (ux*ux + uy*uy + uz*uz)*
PhysConst::inv_c2);
93 ux = vx * (1.0_prt + (gamma_V - 1.0_prt) * Vx*Vx/V2)
94 + vy * (gamma_V - 1.0_prt) * Vx*Vy/V2
95 +
vz * (gamma_V - 1.0_prt) * Vx*Vz/V2
96 - gamma_V * Vx * gamma_u;
98 uy = vy * (1.0_prt + (gamma_V - 1.0_prt) * Vy*Vy/V2)
99 + vx * (gamma_V - 1.0_prt) * Vx*Vy/V2
100 +
vz * (gamma_V - 1.0_prt) * Vy*Vz/V2
101 - gamma_V * Vy * gamma_u;
103 uz =
vz * (1.0_prt + (gamma_V - 1.0_prt) * Vz*Vz/V2)
104 + vx * (gamma_V - 1.0_prt) * Vx*Vz/V2
105 + vy * (gamma_V - 1.0_prt) * Vy*Vz/V2
106 - gamma_V * Vz * gamma_u;
139 ux = ux + gammam1*udotn*Ux_hat - Ux*P0;
140 uy = uy + gammam1*udotn*Uy_hat - Uy*P0;
141 uz = uz + gammam1*udotn*Uz_hat - Uz*P0;
203 auto const xy =
sqrt(1_prt - z*z);
241 const auto *
const xlo = tilebox.
lo();
242 const auto *
const xhi = tilebox.
hi();
244 && (xlo[1] <= point.
y) && (point.
y <= xhi[1]),
245 && (xlo[2] <= point.
z) && (point.
z <= xhi[2]));
258 if (do_cropping[0] && x1 < xmin) {
261 else if (do_cropping[1] && x1 > xmax) {
275 double & x1,
double & z1,
277 if (do_cropping[0] && x1 < xmin) {
278 double const fx = ((x0 - x1) != 0. ? (x0 - xmin)/(x0 - x1) : 0.);
280 z1 = z0 + fx*(z1 - z0);
282 else if (do_cropping[1] && x1 > xmax) {
283 double const fx = ((x0 - x1) != 0. ? (xmax - x0)/(x1 - x0) : 0.);
285 z1 = z0 + fx*(z1 - z0);
298 double & x1,
double & y1,
double & z1,
300 if (do_cropping[0] && x1 < xmin) {
301 double const fx = ((x0 - x1) != 0. ? (x0 - xmin)/(x0 - x1) : 0.);
303 y1 = y0 + fx*(y1 - y0);
304 z1 = z0 + fx*(z1 - z0);
306 else if (do_cropping[1] && x1 > xmax) {
307 double const fx = ((x0 - x1) != 0. ? (xmax - x0)/(x1 - x0) : 0.);
309 y1 = y0 + fx*(y1 - y0);
310 z1 = z0 + fx*(z1 - z0);
325 [[maybe_unused]]
double yp,
326 [[maybe_unused]]
double zp,
335#if defined(WARPX_DIM_3D)
336 xp_grid[0] = (xp - xyzmin.
x)*dinv.
x;
337 xp_grid[1] = (yp - xyzmin.
y)*dinv.
y;
338 xp_grid[2] = (zp - xyzmin.
z)*dinv.
z;
339#elif defined(WARPX_DIM_XZ)
340 xp_grid[0] = (xp - xyzmin.
x)*dinv.
x;
341 xp_grid[1] = (zp - xyzmin.
z)*dinv.
z;
342#elif defined(WARPX_DIM_1D_Z)
343 xp_grid[0] = (zp - xyzmin.
z)*dinv.
z;
344#elif defined(WARPX_DIM_RZ) || defined(WARPX_DIM_RCYLINDER)
346 xp_grid[0] = (rp - xyzmin.
x)*dinv.
x;
347#if defined(WARPX_DIM_RZ)
348 xp_grid[1] = (zp - xyzmin.
z)*dinv.
z;
350#elif defined(WARPX_DIM_RSPHERE)
351 const amrex::Real rp = std::sqrt(xp*xp + yp*yp + zp*zp);
352 xp_grid[0] = (rp - xyzmin.
x)*dinv.
x;
356 for (
int dim = 0; dim < AMREX_SPACEDIM; dim++) {
357 if (do_cropping[dim][0] && xp_grid[dim] <= domain_double[dim][0]) {
360 else if (do_cropping[dim][1] && xp_grid[dim] >= domain_double[dim][1]) {
386 template <
typename T_index>
393 T_index
const cell_start,
394 T_index
const cell_stop,
404 int const numCell = (cell_stop - cell_start);
416 int const max_loop_count = 10;
420 T_index i1 = cell_start;
421 bool correction_failed =
false;
422 bool deltaE_is_zero =
false;
423 while (!deltaE_is_zero && !correction_failed && loop_count <= max_loop_count) {
426 if (i2 == cell_stop) {
431 int const sign = (deltaE < 0.0 ? -1 : 1);
463 const amrex::ParticleReal Ecm = std::sqrt(Etot*Etot - (pxtot*pxtot + pytot*pytot + pztot*pztot)*c2);
465 if (Erel <= 0.0) {
continue; }
473 if (std::abs(deltaEpair) >= std::abs(deltaE)) {
476 deltaE_is_zero =
true;
478 deltaE -= deltaEpair;
492 if (root < 0.0 || a == 0.0) {
continue; }
498 uxp[ indices[i1] ] += ratio1*ux;
499 uyp[ indices[i1] ] += ratio1*uy;
500 uzp[ indices[i1] ] += ratio1*uz;
501 uxp[ indices[i2] ] -= ratio2*ux;
502 uyp[ indices[i2] ] -= ratio2*uy;
503 uzp[ indices[i2] ] -= ratio2*uz;
505 Erel_cumm += Erel - deltaEpair;
507 if (i1 < cell_stop - 2) {
515 if (i1 == cell_stop - 2) {
516 if ((numCell % 2) == 0) { i1 = cell_start + 1; }
517 if ((numCell % 2) != 0) { i1 = cell_start; }
519 if ((numCell % 2) == 0) { i1 = cell_start; }
520 if ((numCell % 2) != 0) { i1 = cell_start + 1; }
527 energy_frac_eff = std::abs(deltaE)/Erel_cumm;
528 if (energy_frac_eff > energy_fraction_max || loop_count > max_loop_count) {
529 correction_failed =
true;
531 }
else if (deltaE < 0.) {
534 fmult_fact = std::max(1._prt, energy_frac_eff/energy_fraction);
540 return correction_failed;
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
#define AMREX_D_TERM(a, b, c)
@ vz
Definition RigidInjectedParticleContainer.H:27
@ alpha
Definition SpeciesPhysicalProperties.H:18
ParticleTile< ParticleType, NArrayReal, NArrayInt, Allocator > ParticleTileType
__host__ __device__ const Real * lo() const &noexcept
__host__ __device__ const Real * hi() const &noexcept
amrex_particle_real ParticleReal
Definition ParticleUtils.cpp:24
AMREX_GPU_HOST_DEVICE AMREX_INLINE void doLorentzTransformWithP(amrex::ParticleReal &px, amrex::ParticleReal &py, amrex::ParticleReal &pz, amrex::ParticleReal mass, amrex::ParticleReal const Ux, amrex::ParticleReal const Uy, amrex::ParticleReal const Uz)
Perform a Lorentz transformation of the given momentum to a frame moving with gamma*velocity (Ux,...
Definition ParticleUtils.H:156
AMREX_GPU_HOST_DEVICE AMREX_INLINE void doLorentzTransformWithU(amrex::ParticleReal &ux, amrex::ParticleReal &uy, amrex::ParticleReal &uz, amrex::ParticleReal const Ux, amrex::ParticleReal const Uy, amrex::ParticleReal const Uz)
Perform a Lorentz transformation of the given velocity to a frame moving with gamma*velocity (Ux,...
Definition ParticleUtils.H:119
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:53
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:76
typename WarpXParticleContainer::ParticleTileType ParticleTileType
Definition ParticleUtils.H:22
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:218
amrex::DenseBins< ParticleTileDataType > findParticlesInEachCell(amrex::Geometry const &geom_lev, amrex::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:35
AMREX_GPU_HOST_DEVICE AMREX_INLINE bool ModifyEnergyPairwise(amrex::ParticleReal *const AMREX_RESTRICT uxp, amrex::ParticleReal *const AMREX_RESTRICT uyp, amrex::ParticleReal *const AMREX_RESTRICT uzp, amrex::ParticleReal *const AMREX_RESTRICT w, T_index const *const AMREX_RESTRICT indices, T_index const cell_start, T_index const cell_stop, amrex::ParticleReal const mass, amrex::ParticleReal const energy_fraction, amrex::ParticleReal const energy_fraction_max, amrex::ParticleReal &deltaE)
Definition ParticleUtils.H:388
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool is_out_of_bounds(double xp, double yp, double zp, const amrex::XDim3 &dinv, const amrex::XDim3 &xyzmin, const amrex::GpuArray< amrex::GpuArray< double, 2 >, 3 > &domain_double, const amrex::GpuArray< amrex::GpuArray< bool, 2 >, 3 > &do_cropping)
Definition ParticleUtils.H:324
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:193
AMREX_GPU_HOST_DEVICE AMREX_INLINE bool containsInclusive(amrex::RealBox const &tilebox, amrex::XDim3 const point)
Definition ParticleUtils.H:240
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void crop_at_boundary(double &x1, double xmin, double xmax, amrex::GpuArray< bool, 2 > const &do_cropping)
Definition ParticleUtils.H:256
typename ParticleTileType::ParticleTileDataType ParticleTileDataType
Definition ParticleUtils.H:23
constexpr auto c
vacuum speed of light [m/s]
Definition constant.H:153
constexpr auto inv_c2
inverse of the square of the vacuum speed of light [s^2/m^2]
Definition constant.H:220
constexpr auto q_e
elementary charge [C]
Definition constant.H:162
constexpr auto pi
ratio of a circle's circumference to its diameter
Definition constant.H:29
__host__ __device__ GpuComplex< T > sqrt(const GpuComplex< T > &a_z) noexcept