XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
col_vgrids.hpp
Go to the documentation of this file.
1 #ifndef COL_VGRIDS_HPP
2 #define COL_VGRIDS_HPP
3 
4 #include <vector>
5 #include <stdio.h>
6 #include <Kokkos_Core.hpp>
7 #include <Kokkos_DualView.hpp>
8 
9 #include "col_species.hpp"
10 
11 
12 template<class Device>
14  Kokkos::DualView<int*,CLayout,Device> nspecies_in_grid;
15  const int n;
16  const int mb_n_nodes;
17  const int n_species;
18  const int nvr;
19  const int nvz;
20  View<double***,CLayout,HostType> mesh_r;
21  View<double***,CLayout,HostType> mesh_z;
22  View<double***,CLayout,Device> vol;
23  View<double***,CLayout,HostType> vol_h;
24  View<double****,Device> delta_r;
25  View<double****,Device> delta_z;
26  View<double**,Kokkos::LayoutLeft,HostType> vpar_beg_h;
27  View<double**,Kokkos::LayoutLeft,HostType> mesh_dz_h;
28  View<double**,Kokkos::LayoutLeft,HostType> mesh_dr_h;
29 
30  View<double**,Kokkos::LayoutLeft,Device> vpar_beg;
31  View<double**,Kokkos::LayoutLeft,Device> mesh_dz;
32  View<double**,Kokkos::LayoutLeft,Device> mesh_dr;
33 
34  Kokkos::DualView<int**,CLayout,Device> map_grid_to_species;
35 
36  // Count how many species are using each grid
37  inline Kokkos::DualView<int*,CLayout,Device> count_species_in_grid(const CollisionSpecies<Device>& col_spall) const{
38  Kokkos::DualView<int*,CLayout,Device> nspecies_in_grid_tmp("nspecies_in_grid", col_spall.s.h_view.extent(0));
39  // Loop over species and set up mapping
40  for (int isp = 0; isp < col_spall.s.h_view.extent(0); isp++){
41  int gri = col_spall.s.h_view(isp,0).grid_index; // Which grid this species is on
42  nspecies_in_grid_tmp.h_view(gri)++;
43  }
44  return nspecies_in_grid_tmp;
45  }
46 
47  // Count how many grids are used
48  inline int count_grids() const{
49  int ngrids=0;
50  for(int i=0; i<nspecies_in_grid.h_view.size(); i++){
51  if(nspecies_in_grid.h_view(i)>0) ngrids++;
52  }
53  return ngrids;
54  }
55 
57 
60  n(count_grids()),
61  mb_n_nodes(col_spall.pdf_n.h_view.extent(0)), // Use pdf_n dimensions: mb_n_nodes, n_species, nvz, nvr
62  n_species(col_spall.pdf_n.h_view.extent(1)),
63  nvz(col_spall.pdf_n.h_view.extent(2)),
64  nvr(col_spall.pdf_n.h_view.extent(3)),
65  map_grid_to_species("map_grid_to_species",n,n_species), // n_species is the max possible
66  mesh_dr("mesh_dr", mb_n_nodes, n),
67  mesh_dz("mesh_dz", mb_n_nodes, n),
68  vpar_beg("vpar_beg", mb_n_nodes, n),
69  mesh_dr_h("mesh_dr_h", mb_n_nodes, n),
70  mesh_dz_h("mesh_dz_h", mb_n_nodes, n),
71  vpar_beg_h("vpar_beg_h", mb_n_nodes, n),
72  mesh_r("mesh_r", mb_n_nodes, n, nvr),
73  mesh_z("mesh_z", mb_n_nodes, n, nvz),
74  vol("vol",nvr,n, mb_n_nodes),
75  vol_h("vol_h",mb_n_nodes,n, nvr), // Must have nvr fastest index for passing into fortran negative_f_correction routine
76  delta_r("delta_r",n,nvr-1,n_species, mb_n_nodes),
77  delta_z("delta_z",n,nvz-1,n_species, mb_n_nodes)
78  {
79  // Loop over species and set up mapping
80  int isp_in_grid=0;
81  int gri=0;
82  for (int isp = 0; isp < n_species; isp++){
83  // Move to next species or to next grid
84  if(gri!=col_spall.s.h_view(isp,0).grid_index) isp_in_grid=0;
85 
86  gri = col_spall.s.h_view(isp,0).grid_index; // Which grid this species is on
87  map_grid_to_species.h_view(gri,isp_in_grid) = isp;
88  isp_in_grid++;
89  }
90 
91 #ifdef USE_GPU
92  // Copy mapping views to device
93  Kokkos::deep_copy(map_grid_to_species.d_view, map_grid_to_species.h_view);
94  Kokkos::deep_copy(nspecies_in_grid.d_view, nspecies_in_grid.h_view);
95 #endif
96  }
97 
107  KOKKOS_INLINE_FUNCTION double vp_vol_fac(int ivz) const{
108  return (ivz==0 || ivz==(nvz-1)) ? 0.5 : 1.0;
109  }
110 
111  //> Convert the distribution function to the units used
112  // internally by the collision operator
113  // The distribution function set up with f0_module is in normalized velocity space
114  // whereas the collision operator uses SI units.
115  void setup_one(int isp, int mesh_ind, double vth, double dsmu, double dvp, double vp_max) {
116 
117  // vol
118  double pi4bb0vth3_dd = TWOPI*vth*vth*vth*dsmu*dvp;
119 
120  // --- 0th component
121  double smu_n = dsmu/3.0;
122  vol_h(mesh_ind,isp,0) = 0.5*pi4bb0vth3_dd*smu_n;
123  // --- 1st to (last-1)
124  for (int imu = 1; imu < nvr-1; imu++){
125  smu_n = dsmu*imu;
126  vol_h(mesh_ind,isp,imu) = pi4bb0vth3_dd*smu_n;
127  }
128  // --- last component
129  //smu_n = dsmu*((nvr-1)-1.0/3.0);//rh imu or f0_nmu???
130  smu_n = dsmu*(nvr-1);
131  vol_h(mesh_ind,isp,nvr-1) = 0.5*pi4bb0vth3_dd*smu_n;
132 
133  //vth_par = sqrt(sp_t_par(i) * UNIT_CHARGE / mass)
134  //vth_perp = sqrt(sp_t_perp(i) * UNIT_CHARGE / mass)
135 
136  // < Mesh-related quantities>
137  double vpar_beg_l = -vp_max*vth;// ( = dlx)
138  double mesh_dz_l = dvp*vth;// ( = mesh_dz)
139  double mesh_dr_l = dsmu*vth;// ( = mesh_dr)
140 
141  vpar_beg_h(mesh_ind, isp) = vpar_beg_l;
142  mesh_dz_h(mesh_ind, isp) = mesh_dz_l;
143  mesh_dr_h(mesh_ind, isp) = mesh_dr_l;
144 
145  // mesh_r, mesh_z
146  for (int j = 0; j < nvr-1; j++){
147  mesh_r(mesh_ind,isp,j) = mesh_dr_l*j;
148  }
149  mesh_r(mesh_ind,isp,nvr-1) = mesh_dr_l*(nvr-1);
150  for (int j = 0; j < nvz-1; j++){
151  mesh_z(mesh_ind,isp,j) = vpar_beg_l + mesh_dz_l*j;
152  }
153  mesh_z(mesh_ind,isp,nvz-1) = vpar_beg_l + mesh_dz_l*(nvz-1);
154  }
155 
156  KOKKOS_INLINE_FUNCTION double mesh_r_half(int ibatch, int igrid, int j) const{
157  return mesh_dr(ibatch, igrid)*(j+0.5);
158  }
159 
160  KOKKOS_INLINE_FUNCTION double mesh_z_half(int ibatch, int igrid, int j) const{
161  return vpar_beg(ibatch, igrid) + mesh_dz(ibatch, igrid)*(j+0.5);
162  }
163 
164  KOKKOS_INLINE_FUNCTION double local_center_volume(int ibatch, int igrid, int j) const{
165  return mesh_r_half(ibatch,igrid,j)*mesh_dr(ibatch, igrid)*mesh_dz(ibatch, igrid);
166  }
167 
168  void setup_all(const std::vector<Species<Device>>& all_species, const CollisionSpecies<Device>& col_species,
169  const View<int**,HostType>& mesh_nodes, int mesh_batch_ind){
170 
171  for (int mesh_ind = 0; mesh_ind<col_species.s.h_view.extent(1); mesh_ind++){
172  for (int gri = 0; gri < n; gri++){
173  const int FIRST_SPECIES_OF_GRID = 0; // Initialize grid using the first species assigned to the grid
174  int isp = map_grid_to_species.h_view(gri,FIRST_SPECIES_OF_GRID);
175 
176  // Use equilibrium thermal velocity to construct velocity grid
177  int inode = mesh_nodes(mesh_batch_ind,mesh_ind);
178  double vth = 1.0/all_species[col_species.s.h_view(isp,mesh_ind).gid].f0.fg_vth_inv_h(inode); // .get_f0_eq_thermal_velocity_lnode_h(inode);
179  setup_one(isp, mesh_ind, vth, col_species.f.dsmu, col_species.f.dvp, col_species.f.vp_max);
180  }
181  }
182 
183  // Copy to device
184  Kokkos::deep_copy(mesh_dr, mesh_dr_h);
185  Kokkos::deep_copy(mesh_dz, mesh_dz_h);
186  Kokkos::deep_copy(vpar_beg, vpar_beg_h);
187 
188  // Transpose vol
189  // First create a mirror on device of vol_h
190  View<double***,CLayout,Device> vol_tmp("vol_tmp",vol_h.extent(0),vol_h.extent(1),vol_h.extent(2));
191  // Deep copy that
192  Kokkos::deep_copy(vol_tmp, vol_h);
193 
194  // Make a local copy of the View itself for the lambda function to copy to device
195  View<double***,CLayout,Device> lvol = vol;
196 
197  // Naive transpose copy on device - unoptimized, should be small though
198  int n2 = vol_h.extent(1)*nvr;
199  int nvr_loc = nvr;
200  Kokkos::parallel_for("transpose_vol", Kokkos::RangePolicy<ExSpace>( 0, n2*vol_h.extent(0)), KOKKOS_LAMBDA (const int idx){
201  int i0 = idx/n2; // slowest index
202  int rem = idx%n2;
203  int i1 = rem/nvr_loc;
204  int i2 = rem%nvr_loc; // fastest index
205  lvol(i2,i1,i0) = vol_tmp(i0,i1,i2);
206  });
207  Kokkos::fence();
208  }
209 };
210 
211 #endif
View< double **, Kokkos::LayoutLeft, Device > vpar_beg
Definition: col_vgrids.hpp:30
const int n
Number of velocity grids.
Definition: col_vgrids.hpp:15
View< double **, Kokkos::LayoutLeft, HostType > vpar_beg_h
Definition: col_vgrids.hpp:26
Kokkos::DualView< int *, CLayout, Device > nspecies_in_grid
Definition: col_vgrids.hpp:14
KOKKOS_INLINE_FUNCTION double mesh_z_half(int ibatch, int igrid, int j) const
Definition: col_vgrids.hpp:160
View< double ****, Device > delta_z
Definition: col_vgrids.hpp:25
View< double ****, Device > delta_r
Definition: col_vgrids.hpp:24
double dvp
grid spacing in parallel velocity
Definition: vgrid_distribution.hpp:32
const int nvz
grid points in z
Definition: col_vgrids.hpp:19
Kokkos::DualView< CollisionSpeciesScalars **, Device > s
Definition: col_species.hpp:85
Definition: col_vgrids.hpp:13
KOKKOS_INLINE_FUNCTION double vp_vol_fac(int ivz) const
Definition: col_vgrids.hpp:107
const int nvr
grid points in r
Definition: col_vgrids.hpp:18
View< double ***, CLayout, Device > vol
Definition: col_vgrids.hpp:22
View< double ***, CLayout, HostType > mesh_r
Definition: col_vgrids.hpp:20
View< double **, Kokkos::LayoutLeft, Device > mesh_dz
Definition: col_vgrids.hpp:31
const int mb_n_nodes
Batch size.
Definition: col_vgrids.hpp:16
void setup_one(int isp, int mesh_ind, double vth, double dsmu, double dvp, double vp_max)
Definition: col_vgrids.hpp:115
const int n_species
Number of species total.
Definition: col_vgrids.hpp:17
Kokkos::DualView< int *, CLayout, Device > count_species_in_grid(const CollisionSpecies< Device > &col_spall) const
Definition: col_vgrids.hpp:37
double dsmu
grid spacing in mu
Definition: vgrid_distribution.hpp:34
Kokkos::DualView< int **, CLayout, Device > map_grid_to_species
Definition: col_vgrids.hpp:34
View< double ***, CLayout, HostType > mesh_z
Definition: col_vgrids.hpp:21
KOKKOS_INLINE_FUNCTION double mesh_r_half(int ibatch, int igrid, int j) const
Definition: col_vgrids.hpp:156
KOKKOS_INLINE_FUNCTION double local_center_volume(int ibatch, int igrid, int j) const
Definition: col_vgrids.hpp:164
View< double **, Kokkos::LayoutLeft, HostType > mesh_dr_h
Definition: col_vgrids.hpp:28
const VGridDistribution< HostType > f
Definition: col_species.hpp:83
View< double **, Kokkos::LayoutLeft, Device > mesh_dr
Definition: col_vgrids.hpp:32
View< double ***, CLayout, HostType > vol_h
Definition: col_vgrids.hpp:23
Definition: species.hpp:75
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
void setup_all(const std::vector< Species< Device >> &all_species, const CollisionSpecies< Device > &col_species, const View< int **, HostType > &mesh_nodes, int mesh_batch_ind)
Definition: col_vgrids.hpp:168
int count_grids() const
Definition: col_vgrids.hpp:48
Definition: col_species.hpp:81
constexpr double TWOPI
Definition: constants.hpp:9
View< double **, Kokkos::LayoutLeft, HostType > mesh_dz_h
Definition: col_vgrids.hpp:27
double vp_max
max parallel velocity
Definition: vgrid_distribution.hpp:31