XGC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vgrid_distribution.hpp
Go to the documentation of this file.
1 #ifndef VGRID_DISTRIBUTION_HPP
2 #define VGRID_DISTRIBUTION_HPP
3 
4 #include "space_settings.hpp"
5 #include "NamelistReader.hpp"
6 #include "velocity_grid.hpp"
7 #include "view_arithmetic.hpp"
9 #include "maxwellian.hpp"
10 #include "uniform_range.hpp"
11 
12 template<class Device>
14  public:
15 
16  using exec_space = typename Device::execution_space;
17 
18  View<double****,CLayout, Device> f;
19 
20  // Create a 1D unmanaged view looking at the same address
21  View<double*,CLayout, Device, Kokkos::MemoryTraits<Kokkos::Unmanaged>> f_1D;
22 
23  // Make f private when possible - ALS
24  //public:
25 
26  double vp_max;
27  double dvp;
28  double smu_max;
29  double dsmu;
31 
33 
34  // Analytical distribution for testing
35  VGridDistribution(const VelocityGrid& vgrid, const DomainDecomposition<DeviceType>& pol_decomp, const std::vector<Maxwellian>& maxwellians)
36  : f("f", maxwellians.size(), vgrid.nvr, pol_decomp.nnodes, vgrid.nvz),
37  f_1D(f.data(), f.size()),
38  vp_max(vgrid.vp_max),
39  dvp(vgrid.dvp),
40  smu_max(vgrid.smu_max),
41  dsmu(vgrid.dsmu),
43  {
44  for (int i=0; i<f.extent(0); i++){
45  for (int imu=0; imu<f.extent(1); imu++){
46  double vperp = imu*dsmu;
47  for (int inode=0; inode<f.extent(2); inode++){
48  for (int ivp=0; ivp<f.extent(3); ivp++){
49  double vpara = ivp*dvp - vp_max;
50  f(i,imu,inode,ivp) = maxwellians[i].get_f(vpara, vperp);
51  }
52  }
53  }
54  }
55  }
56 
57  VGridDistribution(int nsp, const VelocityGrid& vgrid, const DomainDecomposition<DeviceType>& pol_decomp)
58  : f("f", nsp, vgrid.nvr, pol_decomp.nnodes, vgrid.nvz),
59  f_1D(f.data(), f.size()),
60  vp_max(vgrid.vp_max),
61  dvp(vgrid.dvp),
62  smu_max(vgrid.smu_max),
63  dsmu(vgrid.dsmu),
65  {}
66 
67  // () operator: behave as if accessing the View directly
68  KOKKOS_INLINE_FUNCTION double& operator()(int isp, int ivr, int inode, int ivz) const { return f(isp, ivr, inode, ivz);}
69 
70  // [] operator: access the data as if it has been squashed to 1 dimension
71  KOKKOS_INLINE_FUNCTION double& operator[](int i) const { return f_1D(i);}
72 
73  double* data() const {return f.data();}
74 
80  template<typename F>
81  inline void for_all_elements(const std::string label, F lambda_func) const {
82  Kokkos::parallel_for(label, Kokkos::RangePolicy<exec_space>(0, f.size()), lambda_func);
83  }
84 
90  template<typename F>
91  inline void for_each_element(const std::string label, F lambda_func) const {
92  Kokkos::parallel_for(label, Kokkos::MDRangePolicy<Kokkos::Rank<4>, exec_space>({0, 0, 0, 0}, {f.extent(0), f.extent(1), f.extent(2), f.extent(3)}), lambda_func);
93  }
94 
95 
96  // Scatter value onto vgrid distribution
97  KOKKOS_INLINE_FUNCTION void scatter(int i_node, const VGridWeights& wt, double value) const{
98  constexpr int ISP_ZERO = 0;
99  Kokkos::atomic_add(&(f(ISP_ZERO, wt.i_vr+0,i_node,wt.i_vz+0)),value*wt.w_00);
100  Kokkos::atomic_add(&(f(ISP_ZERO, wt.i_vr+0,i_node,wt.i_vz+1)),value*wt.w_01);
101  Kokkos::atomic_add(&(f(ISP_ZERO, wt.i_vr+1,i_node,wt.i_vz+0)),value*wt.w_10);
102  Kokkos::atomic_add(&(f(ISP_ZERO, wt.i_vr+1,i_node,wt.i_vz+1)),value*wt.w_11);
103  }
104 
105  // Temporary scatter for views not yet converted to VGridDistribution
106  KOKKOS_INLINE_FUNCTION static void scatter(const View<double***,CLayout,Device>& view, int i_node, const VGridWeights& wt, double value){
107  Kokkos::atomic_add(&(view(wt.i_vr+0,i_node,wt.i_vz+0)),value*wt.w_00);
108  Kokkos::atomic_add(&(view(wt.i_vr+0,i_node,wt.i_vz+1)),value*wt.w_01);
109  Kokkos::atomic_add(&(view(wt.i_vr+1,i_node,wt.i_vz+0)),value*wt.w_10);
110  Kokkos::atomic_add(&(view(wt.i_vr+1,i_node,wt.i_vz+1)),value*wt.w_11);
111  }
112 
113  // Temporary scatter for views not yet converted to VGridDistribution
114  KOKKOS_INLINE_FUNCTION static void scatter(const View<double****,CLayout,Device>& view, int i_node, const VGridWeights& wt, double value){
115  constexpr int ISP_ZERO = 0;
116  Kokkos::atomic_add(&(view(ISP_ZERO,wt.i_vr+0,i_node,wt.i_vz+0)),value*wt.w_00);
117  Kokkos::atomic_add(&(view(ISP_ZERO,wt.i_vr+0,i_node,wt.i_vz+1)),value*wt.w_01);
118  Kokkos::atomic_add(&(view(ISP_ZERO,wt.i_vr+1,i_node,wt.i_vz+0)),value*wt.w_10);
119  Kokkos::atomic_add(&(view(ISP_ZERO,wt.i_vr+1,i_node,wt.i_vz+1)),value*wt.w_11);
120  }
121 
122  // Gather value from vgrid distribution
123  KOKKOS_INLINE_FUNCTION double gather(int i_node, const VGridWeights& wt) const{
124  constexpr int ISP_ZERO = 0;
125  return ( f(ISP_ZERO,wt.i_vr+0,i_node,wt.i_vz+0)*wt.w_00 +
126  f(ISP_ZERO,wt.i_vr+0,i_node,wt.i_vz+1)*wt.w_01 +
127  f(ISP_ZERO,wt.i_vr+1,i_node,wt.i_vz+0)*wt.w_10 +
128  f(ISP_ZERO,wt.i_vr+1,i_node,wt.i_vz+1)*wt.w_11 );
129  }
130 
131  // Temporary gather for views not yet converted to VGridDistribution
132  template<class T>
133  KOKKOS_INLINE_FUNCTION static double gather(const T& view, int i_node, const VGridWeights& wt){
134  return ( view(wt.i_vr+0,i_node,wt.i_vz+0)*wt.w_00 +
135  view(wt.i_vr+0,i_node,wt.i_vz+1)*wt.w_01 +
136  view(wt.i_vr+1,i_node,wt.i_vz+0)*wt.w_10 +
137  view(wt.i_vr+1,i_node,wt.i_vz+1)*wt.w_11 );
138  }
139 
140  // Temporary gather for views not yet converted to VGridDistribution
141  // Divides by normalization view
142  template<class T>
143  KOKKOS_INLINE_FUNCTION static double normed_gather(const T& view, int i_node, const VGridWeights& wt, const T& norm_view){
144  return (view(wt.i_vr+0,i_node,wt.i_vz+0)*wt.w_00/norm_view(wt.i_vr+0,i_node,wt.i_vz+0) +
145  view(wt.i_vr+0,i_node,wt.i_vz+1)*wt.w_01/norm_view(wt.i_vr+0,i_node,wt.i_vz+1) +
146  view(wt.i_vr+1,i_node,wt.i_vz+0)*wt.w_10/norm_view(wt.i_vr+1,i_node,wt.i_vz+0) +
147  view(wt.i_vr+1,i_node,wt.i_vz+1)*wt.w_11/norm_view(wt.i_vr+1,i_node,wt.i_vz+1));
148  }
149 
150  // Access dimensions
151  KOKKOS_INLINE_FUNCTION int n_species() const{
152  return f.extent(0);
153  }
154 
155  KOKKOS_INLINE_FUNCTION int n_vr() const {
156  return f.extent(1);
157  }
158 
159  KOKKOS_INLINE_FUNCTION int n_nodes() const{
160  return f.extent(2);
161  }
162 
163  KOKKOS_INLINE_FUNCTION int n_vz() const{
164  return f.extent(3);
165  }
166 
167  KOKKOS_INLINE_FUNCTION int size() const{
168  return f.size();
169  }
170 
171  // Miscellaneous
172  inline double get_smu_n(int imu) const{
173  return (imu>0 ? dsmu*imu : dsmu*inv_mu0_factor);
174  }
175 
176  // Returns factor to handle velocity space volume on edge of velocity grid
177  KOKKOS_INLINE_FUNCTION double mu_vol_fac(int ivr) const{
178  return (ivr==0 || ivr==n_vr()-1) ? 0.5 : 1.0;
179  }
180 
182  return UniformRange(n_vr(), 0.0, smu_max);
183  }
184 
186  return UniformRange(n_vz(), -vp_max, vp_max);
187  }
188 };
189 
190 #endif
KOKKOS_INLINE_FUNCTION int n_vr() const
Definition: vgrid_distribution.hpp:155
double * data() const
Definition: vgrid_distribution.hpp:73
KOKKOS_INLINE_FUNCTION int size() const
Definition: vgrid_distribution.hpp:167
UniformRange vz_range() const
Definition: vgrid_distribution.hpp:185
double w_00
Definition: vgrid_weights.hpp:10
Definition: velocity_grid.hpp:8
double get_smu_n(int imu) const
Definition: vgrid_distribution.hpp:172
UniformRange vr_range() const
Definition: vgrid_distribution.hpp:181
VGridDistribution(const VelocityGrid &vgrid, const DomainDecomposition< DeviceType > &pol_decomp, const std::vector< Maxwellian > &maxwellians)
Definition: vgrid_distribution.hpp:35
KOKKOS_INLINE_FUNCTION double gather(int i_node, const VGridWeights &wt) const
Definition: vgrid_distribution.hpp:123
double smu_max
max mu
Definition: vgrid_distribution.hpp:28
static KOKKOS_INLINE_FUNCTION double gather(const T &view, int i_node, const VGridWeights &wt)
Definition: vgrid_distribution.hpp:133
double dvp
grid spacing in parallel velocity
Definition: vgrid_distribution.hpp:27
double w_01
Definition: vgrid_weights.hpp:11
View< double ****, CLayout, Device > f
Definition: vgrid_distribution.hpp:18
KOKKOS_INLINE_FUNCTION int n_species() const
Definition: vgrid_distribution.hpp:151
KOKKOS_INLINE_FUNCTION double & operator()(int isp, int ivr, int inode, int ivz) const
Definition: vgrid_distribution.hpp:68
static KOKKOS_INLINE_FUNCTION void scatter(const View< double ***, CLayout, Device > &view, int i_node, const VGridWeights &wt, double value)
Definition: vgrid_distribution.hpp:106
double inv_mu0_factor
Definition: vgrid_distribution.hpp:30
double w_11
Definition: vgrid_weights.hpp:13
VGridDistribution()
Definition: vgrid_distribution.hpp:32
KOKKOS_INLINE_FUNCTION int n_vz() const
Definition: vgrid_distribution.hpp:163
View< double *, CLayout, Device, Kokkos::MemoryTraits< Kokkos::Unmanaged > > f_1D
Definition: vgrid_distribution.hpp:21
VGridDistribution(int nsp, const VelocityGrid &vgrid, const DomainDecomposition< DeviceType > &pol_decomp)
Definition: vgrid_distribution.hpp:57
static KOKKOS_INLINE_FUNCTION void scatter(const View< double ****, CLayout, Device > &view, int i_node, const VGridWeights &wt, double value)
Definition: vgrid_distribution.hpp:114
typename HostType::execution_space exec_space
Definition: vgrid_distribution.hpp:16
Definition: vgrid_distribution.hpp:13
int i_vr
Definition: vgrid_weights.hpp:8
int i_vz
Definition: vgrid_weights.hpp:9
double dsmu
grid spacing in mu
Definition: vgrid_distribution.hpp:29
void for_each_element(const std::string label, F lambda_func) const
Definition: vgrid_distribution.hpp:91
Definition: vgrid_weights.hpp:7
Definition: uniform_range.hpp:6
void parallel_for(const std::string name, int n_ptl, Function func, Option option, HostAoSoA aosoa_h, DeviceAoSoA aosoa_d)
Definition: streamed_parallel_for.hpp:252
KOKKOS_INLINE_FUNCTION int n_nodes() const
Definition: vgrid_distribution.hpp:159
static KOKKOS_INLINE_FUNCTION double normed_gather(const T &view, int i_node, const VGridWeights &wt, const T &norm_view)
Definition: vgrid_distribution.hpp:143
void for_all_elements(const std::string label, F lambda_func) const
Definition: vgrid_distribution.hpp:81
KOKKOS_INLINE_FUNCTION double & operator[](int i) const
Definition: vgrid_distribution.hpp:71
double w_10
Definition: vgrid_weights.hpp:12
KOKKOS_INLINE_FUNCTION double mu_vol_fac(int ivr) const
Definition: vgrid_distribution.hpp:177
double vp_max
max parallel velocity
Definition: vgrid_distribution.hpp:26
KOKKOS_INLINE_FUNCTION void scatter(int i_node, const VGridWeights &wt, double value) const
Definition: vgrid_distribution.hpp:97