WarpX
InjectorPosition.H
Go to the documentation of this file.
1 /* Copyright 2019 Axel Huebl, David Grote, Maxence Thevenet
2  * Weiqun Zhang
3  *
4  * This file is part of WarpX.
5  *
6  * License: BSD-3-Clause-LBNL
7  */
8 #ifndef INJECTOR_POSITION_H_
9 #define INJECTOR_POSITION_H_
10 
11 #include <AMReX_Gpu.H>
12 #include <AMReX_Dim3.H>
13 #include <AMReX_Utility.H>
14 
15 // struct whose getPositionUnitBox returns x, y and z for a particle with
16 // random distribution inside a unit cell.
18 {
19  AMREX_GPU_HOST_DEVICE
20  amrex::XDim3
21  getPositionUnitBox (int /*i_part*/, int /*ref_fac*/,
22  amrex::RandomEngine const& engine) const noexcept
23  {
24  return amrex::XDim3{amrex::Random(engine), amrex::Random(engine), amrex::Random(engine)};
25  }
26 };
27 
28 // struct whose getPositionUnitBox returns x, y and z for a particle with
29 // regular distribution inside a unit cell.
31 {
32  InjectorPositionRegular (amrex::Dim3 const& a_ppc) noexcept : ppc(a_ppc) {}
33 
34  // i_part: particle number within the cell, required to evenly space
35  // particles within the cell.
36  // ref_fac: the number of particles evenly-spaced within a cell
37  // is a_ppc*(ref_fac**AMREX_SPACEDIM).
38  AMREX_GPU_HOST_DEVICE
39  amrex::XDim3
40  getPositionUnitBox (int const i_part, int const ref_fac,
41  amrex::RandomEngine const&) const noexcept
42  {
43  using namespace amrex;
44 
45  int const nx = ref_fac*ppc.x;
46  int const ny = ref_fac*ppc.y;
47 #if (defined WARPX_DIM_3D) || (defined WARPX_DIM_RZ)
48  int const nz = ref_fac*ppc.z;
49 #else
50  int const nz = 1;
51 #endif
52  int const ix_part = i_part / (ny*nz); // written this way backward compatibility
53  int const iz_part = (i_part-ix_part*(ny*nz)) / ny;
54  int const iy_part = (i_part-ix_part*(ny*nz)) - ny*iz_part;
55  return XDim3{
56  (0.5_rt + ix_part) / nx,
57  (0.5_rt + iy_part) / ny,
58  (0.5_rt + iz_part) / nz
59  };
60  }
61 private:
62  amrex::Dim3 ppc;
63 };
64 
65 // Base struct for position injector.
66 // InjectorPosition contains a union (called Object) that holds any one
67 // instance of:
68 // - InjectorPositionRandom : to generate random distribution;
69 // - InjectorPositionRegular: to generate regular distribution.
70 // The choice is made at runtime, depending in the constructor called.
71 // This mimics virtual functions.
73 {
74  // This constructor stores a InjectorPositionRandom in union object.
76  amrex::Real a_xmin, amrex::Real a_xmax,
77  amrex::Real a_ymin, amrex::Real a_ymax,
78  amrex::Real a_zmin, amrex::Real a_zmax)
79  : type(Type::random),
80  object(t),
81  xmin(a_xmin), xmax(a_xmax),
82  ymin(a_ymin), ymax(a_ymax),
83  zmin(a_zmin), zmax(a_zmax)
84  { }
85 
86  // This constructor stores a InjectorPositionRegular in union object.
88  amrex::Real a_xmin, amrex::Real a_xmax,
89  amrex::Real a_ymin, amrex::Real a_ymax,
90  amrex::Real a_zmin, amrex::Real a_zmax,
91  amrex::Dim3 const& a_ppc)
92  : type(Type::regular),
93  object(t, a_ppc),
94  xmin(a_xmin), xmax(a_xmax),
95  ymin(a_ymin), ymax(a_ymax),
96  zmin(a_zmin), zmax(a_zmax)
97  { }
98 
99  // Explicitly prevent the compiler from generating copy constructors
100  // and copy assignment operators.
101  InjectorPosition (InjectorPosition const&) = delete;
102  InjectorPosition (InjectorPosition&&) = delete;
103  void operator= (InjectorPosition const&) = delete;
104  void operator= (InjectorPosition &&) = delete;
105 
106  // call getPositionUnitBox from the object stored in the union
107  // (the union is called Object, and the instance is called object).
108  AMREX_GPU_HOST_DEVICE
109  amrex::XDim3
110  getPositionUnitBox (int const i_part, int const ref_fac,
111  amrex::RandomEngine const& engine) const noexcept
112  {
113  switch (type)
114  {
115  case Type::regular:
116  {
117  return object.regular.getPositionUnitBox(i_part, ref_fac, engine);
118  }
119  default:
120  {
121  return object.random.getPositionUnitBox(i_part, ref_fac, engine);
122  }
123  };
124  }
125 
126  // bool: whether position specified is within bounds.
127  AMREX_GPU_HOST_DEVICE
128  bool
129  insideBounds (amrex::Real x, amrex::Real y, amrex::Real z) const noexcept
130  {
131  return (x < xmax and x >= xmin and
132  y < ymax and y >= ymin and
133  z < zmax and z >= zmin);
134  }
135 
136  // bool: whether the region defined by lo and hi overaps with the plasma region
137  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
138  bool
139  overlapsWith (const amrex::XDim3& lo, const amrex::XDim3& hi) const noexcept
140  {
141  return ! ( (xmin > hi.x) || (xmax < lo.x)
142  || (ymin > hi.y) || (ymax < lo.y)
143  || (zmin > hi.z) || (zmax < lo.z) );
144  }
145 
146 private:
147  enum struct Type { random, regular };
149 
150  // An instance of union Object constructs and stores any one of
151  // the objects declared (random or regular).
152  union Object {
153  Object (InjectorPositionRandom*) noexcept : random() {}
154  Object (InjectorPositionRegular*, amrex::Dim3 const& a_ppc) noexcept
155  : regular(a_ppc) {}
158  };
160 
161  amrex::Real xmin, xmax;
162  amrex::Real ymin, ymax;
163  amrex::Real zmin, zmax;
164 };
165 
166 #endif
InjectorPositionRegular regular
Definition: InjectorPosition.H:157
def x
Definition: read_lab_particles.py:25
InjectorPosition(InjectorPositionRandom *t, amrex::Real a_xmin, amrex::Real a_xmax, amrex::Real a_ymin, amrex::Real a_ymax, amrex::Real a_zmin, amrex::Real a_zmax)
Definition: InjectorPosition.H:75
Object(InjectorPositionRandom *) noexcept
Definition: InjectorPosition.H:153
InjectorPosition(InjectorPositionRegular *t, amrex::Real a_xmin, amrex::Real a_xmax, amrex::Real a_ymin, amrex::Real a_ymax, amrex::Real a_zmin, amrex::Real a_zmax, amrex::Dim3 const &a_ppc)
Definition: InjectorPosition.H:87
Definition: InjectorPosition.H:152
AMREX_GPU_HOST_DEVICE amrex::XDim3 getPositionUnitBox(int, int, amrex::RandomEngine const &engine) const noexcept
Definition: InjectorPosition.H:21
amrex::Real ymin
Definition: InjectorPosition.H:162
def z
Definition: read_lab_particles.py:26
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool overlapsWith(const amrex::XDim3 &lo, const amrex::XDim3 &hi) const noexcept
Definition: InjectorPosition.H:139
amrex::Real xmin
Definition: InjectorPosition.H:161
Type type
Definition: InjectorPosition.H:148
InjectorPositionRegular(amrex::Dim3 const &a_ppc) noexcept
Definition: InjectorPosition.H:32
AMREX_GPU_HOST_DEVICE bool insideBounds(amrex::Real x, amrex::Real y, amrex::Real z) const noexcept
Definition: InjectorPosition.H:129
Definition: InjectorPosition.H:17
InjectorPositionRandom random
Definition: InjectorPosition.H:156
type
Definition: run_alltests_1node.py:67
amrex::Real zmin
Definition: InjectorPosition.H:163
Type
Definition: InjectorPosition.H:147
Object object
Definition: InjectorPosition.H:159
AMREX_GPU_HOST_DEVICE amrex::XDim3 getPositionUnitBox(int const i_part, int const ref_fac, amrex::RandomEngine const &) const noexcept
Definition: InjectorPosition.H:40
Definition: InjectorPosition.H:30
Object(InjectorPositionRegular *, amrex::Dim3 const &a_ppc) noexcept
Definition: InjectorPosition.H:154
Definition: InjectorPosition.H:72
AMREX_GPU_HOST_DEVICE amrex::XDim3 getPositionUnitBox(int const i_part, int const ref_fac, amrex::RandomEngine const &engine) const noexcept
Definition: InjectorPosition.H:110
Definition: PML.H:52
amrex::Dim3 ppc
Definition: InjectorPosition.H:62