Hipace
ParticleUtil.H
Go to the documentation of this file.
1 /* Copyright 2020-2021
2  *
3  * This file is part of HiPACE++.
4  *
5  * Authors: Andrew Myers, MaxThevenet, Remi Lehe, Severin Diederichs
6  * Weiqun Zhang
7  * License: BSD-3-Clause-LBNL
8  */
9 #ifndef HIPACE_ParticleUtil_H_
10 #define HIPACE_ParticleUtil_H_
11 
12 #include <AMReX_Gpu.H>
13 #include <AMReX_REAL.H>
14 #include <AMReX_IntVect.H>
15 #include <AMReX_RealVect.H>
16 #include <AMReX_Random.H>
17 
19 namespace ParticleUtil
20 {
21  using namespace amrex::literals;
22 
34  void get_position_unit_cell (amrex::Real* r, const amrex::IntVect& nppc, int i_part, const amrex::RandomEngine& engine, const amrex::GpuArray<int, 3> rand)
35  {
36  int nx = nppc[0];
37  int ny = nppc[1];
38  int nz = nppc[2];
39 
40  int ix_part = i_part/(ny * nz);
41  int iy_part = (i_part % (ny * nz)) % ny;
42  int iz_part = (i_part % (ny * nz)) / ny;
43 
44  r[0] = rand[0] ? amrex::Random(engine) : (0.5_rt + ix_part)/nx;
45  r[1] = rand[1] ? amrex::Random(engine) : (0.5_rt + iy_part)/ny;
46  r[2] = rand[2] ? amrex::Random(engine) : (0.5_rt + iz_part)/nz;
47  }
48 
50  void get_position_unit_cell (amrex::Real* r, const amrex::IntVect& nppc, int i_part)
51  {
52  int nx = nppc[0];
53  int ny = nppc[1];
54  int nz = nppc[2];
55 
56  int ix_part = i_part/(ny * nz);
57  int iy_part = (i_part % (ny * nz)) % ny;
58  int iz_part = (i_part % (ny * nz)) / ny;
59 
60  r[0] = (0.5_rt + ix_part)/nx;
61  r[1] = (0.5_rt + iy_part)/ny;
62  r[2] = (0.5_rt + iz_part)/nz;
63  }
64 
66  void get_position_unit_cell_fine (amrex::Real* r, bool& do_init, int i_part,
67  const amrex::Array<int, 2>& nppc_coarse,
68  const amrex::Array<int, 2>& nppc_fine,
69  const int n_fine_transition_cells,
70  const int i_fine_transition_cells)
71  {
72  if (i_fine_transition_cells == 0) {
73  int nx = nppc_coarse[0];
74  int ny = nppc_coarse[1];
75  if (i_part < nx * ny) {
76  do_init = true;
77  int ix_part = i_part % nx;
78  int iy_part = i_part / nx;
79  r[0] = (0.5_rt + ix_part)/nx;
80  r[1] = (0.5_rt + iy_part)/ny;
81  } else {
82  do_init = false;
83  }
84  } else {
85  do_init = true;
86  int nx_fine = nppc_fine[0];
87  int ny_fine = nppc_fine[1];
88  int ix_part_fine = i_part % nx_fine;
89  int iy_part_fine = i_part / nx_fine;
90 
91  int nx_coarse = nppc_coarse[0];
92  int ny_coarse = nppc_coarse[1];
93  int ix_part_coarse = (ix_part_fine * nx_coarse) / nx_fine;
94  int iy_part_coarse = (iy_part_fine * ny_coarse) / ny_fine;
95 
96  amrex::Real s = amrex::Real(i_fine_transition_cells) / (n_fine_transition_cells + 1);
97  s = 1.5_rt*s - 0.5_rt*s*s*s;
98 
99  r[0] = ((0.5_rt + ix_part_coarse) / nx_coarse) * (1._rt - s) +
100  ((0.5_rt + ix_part_fine) / nx_fine) * s;
101  r[1] = ((0.5_rt + iy_part_coarse) / ny_coarse) * (1._rt - s) +
102  ((0.5_rt + iy_part_fine) / ny_fine) * s;
103  }
104  }
105 
113  void get_gaussian_random_momentum (amrex::Real* u, const amrex::RealVect u_mean,
114  const amrex::RealVect u_std,
115  const amrex::RandomEngine& engine)
116  {
117  amrex::Real ux_th = amrex::RandomNormal(0.0, u_std[0], engine);
118  amrex::Real uy_th = amrex::RandomNormal(0.0, u_std[1], engine);
119  amrex::Real uz_th = amrex::RandomNormal(0.0, u_std[2], engine);
120 
121  u[0] = u_mean[0] + ux_th;
122  u[1] = u_mean[1] + uy_th;
123  u[2] = u_mean[2] + uz_th;
124  }
125 }
126 
127 #endif
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
Basic helper functions that can be used for both plasma and beam species.
Definition: ParticleUtil.H:20
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void get_position_unit_cell(amrex::Real *r, const amrex::IntVect &nppc, int i_part, const amrex::RandomEngine &engine, const amrex::GpuArray< int, 3 > rand)
Definition: ParticleUtil.H:34
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void get_gaussian_random_momentum(amrex::Real *u, const amrex::RealVect u_mean, const amrex::RealVect u_std, const amrex::RandomEngine &engine)
Definition: ParticleUtil.H:113
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void get_position_unit_cell_fine(amrex::Real *r, bool &do_init, int i_part, const amrex::Array< int, 2 > &nppc_coarse, const amrex::Array< int, 2 > &nppc_fine, const int n_fine_transition_cells, const int i_fine_transition_cells)
Definition: ParticleUtil.H:66
Real Random()
Real RandomNormal(Real mean, Real stddev)
std::array< T, N > Array