ARTEMIS
ParticleBoundaries_K.H
Go to the documentation of this file.
1 /* Copyright 2021 David Grote
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 #ifndef PARTICLEBOUNDARIES_K_H_
8 #define PARTICLEBOUNDARIES_K_H_
9 
10 #include "ParticleBoundaries.H"
11 
12 #include <AMReX_AmrCore.H>
13 
15 
16  /* \brief Applies the boundary condition on a specific axis
17  * This is called by apply_boundaries.
18  */
20  void
21  apply_boundary (amrex::ParticleReal& x, amrex::Real xmin, amrex::Real xmax,
22  bool& change_sign_ux, bool& particle_lost,
24  amrex::Real refl_probability_xmin, amrex::Real refl_probability_xmax,
25  amrex::RandomEngine const& engine )
26  {
27  if (x < xmin) {
28  if (xmin_bc == ParticleBoundaryType::Open) {
29  particle_lost = true;
30  }
31  else if (xmin_bc == ParticleBoundaryType::Absorbing) {
32  if (refl_probability_xmin == 0 || amrex::Random(engine) > refl_probability_xmin) {
33  particle_lost = true;
34  }
35  else
36  {
37  x = 2*xmin - x;
38  change_sign_ux = true;
39  }
40  }
41  else if (xmin_bc == ParticleBoundaryType::Reflecting) {
42  x = 2*xmin - x;
43  change_sign_ux = true;
44  }
45  }
46  else if (x > xmax) {
47  if (xmax_bc == ParticleBoundaryType::Open) {
48  particle_lost = true;
49  }
50  else if (xmax_bc == ParticleBoundaryType::Absorbing) {
51  if (refl_probability_xmax == 0 || amrex::Random(engine) > refl_probability_xmax) {
52  particle_lost = true;
53  }
54  else
55  {
56  x = 2*xmax - x;
57  change_sign_ux = true;
58  }
59  }
60  else if (xmax_bc == ParticleBoundaryType::Reflecting) {
61  x = 2*xmax - x;
62  change_sign_ux = true;
63  }
64  }
65  }
66 
67  /* \brief Applies absorbing or reflecting boundary condition to the input particles, along all axis.
68  * For reflecting boundaries, the position of the particle is changed appropriately and
69  * the sign of the velocity is changed (depending on the reflect_all_velocities flag).
70  * For absorbing, a flag is set whether the particle has been lost (it is up to the calling
71  * code to take appropriate action to remove any lost particles). Absorbing boundaries can
72  * be given a reflection coefficient for stochastic reflection of particles, this
73  * coefficient is zero by default.
74  * Note that periodic boundaries are handled in AMReX code.
75  *
76  * \param x, xmin, xmax: particle x position, location of x boundary
77  * \param y, ymin, ymax: particle y position, location of y boundary (3D only)
78  * \param z, zmin, zmax: particle z position, location of z boundary
79  * \param ux, uy, uz: particle momenta
80  * \param particle_lost: output, flags whether the particle was lost
81  * \param boundaries: object with boundary condition settings
82  */
84  void
86 #ifndef WARPX_DIM_1D_Z
87  amrex::ParticleReal& x, amrex::Real xmin, amrex::Real xmax,
88 #endif
89 #if (defined WARPX_DIM_3D) || (defined WARPX_DIM_RZ)
90  amrex::ParticleReal& y,
91 #endif
92 #if (defined WARPX_DIM_3D)
93  amrex::Real ymin, amrex::Real ymax,
94 #endif
95  amrex::ParticleReal& z, amrex::Real zmin, amrex::Real zmax,
96  amrex::ParticleReal& ux, amrex::ParticleReal& uy, amrex::ParticleReal& uz,
97  bool& particle_lost,
99  amrex::RandomEngine const& engine)
100  {
101  bool change_sign_ux = false;
102  bool change_sign_uy = false;
103  bool change_sign_uz = false;
104 
105 #ifndef WARPX_DIM_1D_Z
106  apply_boundary(x, xmin, xmax, change_sign_ux, particle_lost,
107  boundaries.xmin_bc, boundaries.xmax_bc,
108  boundaries.reflection_model_xlo(-ux), boundaries.reflection_model_xhi(ux),
109  engine);
110 #endif
111 #ifdef WARPX_DIM_3D
112  apply_boundary(y, ymin, ymax, change_sign_uy, particle_lost,
113  boundaries.ymin_bc, boundaries.ymax_bc,
114  boundaries.reflection_model_ylo(-uy), boundaries.reflection_model_yhi(uy),
115  engine);
116 #endif
117  apply_boundary(z, zmin, zmax, change_sign_uz, particle_lost,
118  boundaries.zmin_bc, boundaries.zmax_bc,
119  boundaries.reflection_model_zlo(-uz), boundaries.reflection_model_zhi(uz),
120  engine);
121 
122  if (boundaries.reflect_all_velocities && (change_sign_ux | change_sign_uy | change_sign_uz)) {
123  change_sign_ux = true;
124  change_sign_uy = true;
125  change_sign_uz = true;
126  }
127 #ifdef WARPX_DIM_RZ
128  // Note that the reflection of the position does "r = 2*rmax - r", but this is only approximate.
129  // The exact calculation requires the position at the start of the step.
130  if (change_sign_ux && change_sign_uy) {
131  ux = -ux;
132  uy = -uy;
133  } else if (change_sign_ux) {
134  // Reflect only ur
135  // Note that y is theta
136  amrex::Real ur = ux*std::cos(y) + uy*std::sin(y);
137  amrex::Real ut = -ux*std::sin(y) + uy*std::cos(y);
138  ur = -ur;
139  ux = ur*std::cos(y) - ut*std::sin(y);
140  uy = ur*std::sin(y) + ut*std::cos(y);
141  }
142 #else
143  if (change_sign_ux) ux = -ux;
144  if (change_sign_uy) uy = -uy;
145 #endif
146  if (change_sign_uz) uz = -uz;
147  }
148 
149 }
150 #endif
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
ParticleBoundaryType
Definition: WarpXAlgorithmSelection.H:161
@ Absorbing
particles crossing domain boundary are removed
@ Reflecting
particles are reflected
@ Open
particles cross domain boundary leave with damped j
Definition: ParticleBoundaries_K.H:14
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void apply_boundaries(amrex::ParticleReal &x, amrex::Real xmin, amrex::Real xmax, amrex::ParticleReal &y, amrex::ParticleReal &z, amrex::Real zmin, amrex::Real zmax, amrex::ParticleReal &ux, amrex::ParticleReal &uy, amrex::ParticleReal &uz, bool &particle_lost, ParticleBoundaries::ParticleBoundariesData const &boundaries, amrex::RandomEngine const &engine)
Definition: ParticleBoundaries_K.H:85
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void apply_boundary(amrex::ParticleReal &x, amrex::Real xmin, amrex::Real xmax, bool &change_sign_ux, bool &particle_lost, ParticleBoundaryType xmin_bc, ParticleBoundaryType xmax_bc, amrex::Real refl_probability_xmin, amrex::Real refl_probability_xmax, amrex::RandomEngine const &engine)
Definition: ParticleBoundaries_K.H:21
def y
Definition: Excitation_Flag_Generator.py:76
def z
Definition: Excitation_Flag_Generator.py:77
Real Random()
Definition: ParticleBoundaries.H:49
bool reflect_all_velocities
Definition: ParticleBoundaries.H:65
amrex::ParserExecutor< 1 > reflection_model_ylo
Definition: ParticleBoundaries.H:60
ParticleBoundaryType ymax_bc
Definition: ParticleBoundaries.H:54
ParticleBoundaryType xmin_bc
Definition: ParticleBoundaries.H:51
amrex::ParserExecutor< 1 > reflection_model_zhi
Definition: ParticleBoundaries.H:63
ParticleBoundaryType xmax_bc
Definition: ParticleBoundaries.H:52
amrex::ParserExecutor< 1 > reflection_model_xhi
Definition: ParticleBoundaries.H:59
ParticleBoundaryType zmax_bc
Definition: ParticleBoundaries.H:56
amrex::ParserExecutor< 1 > reflection_model_zlo
Definition: ParticleBoundaries.H:62
ParticleBoundaryType ymin_bc
Definition: ParticleBoundaries.H:53
amrex::ParserExecutor< 1 > reflection_model_xlo
Definition: ParticleBoundaries.H:58
ParticleBoundaryType zmin_bc
Definition: ParticleBoundaries.H:55
amrex::ParserExecutor< 1 > reflection_model_yhi
Definition: ParticleBoundaries.H:61