XGC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
get_potential_grad.hpp
Go to the documentation of this file.
1 #ifndef GET_POTENTIAL_GRAD
2 #define GET_POTENTIAL_GRAD
3 
4 #include "sml.hpp"
5 #include "grid.hpp"
7 #include "magnetic_field.hpp"
8 #include "my_mirror_view.hpp"
9 #include "gradparx2.hpp"
11 #include "gradient_matrices.hpp"
12 #include "gyro_avg_mat.hpp"
13 #include "task_group.hpp"
14 #include "grid_field.hpp"
15 #include "grid_deriv.hpp"
16 
17 // Fortran pointer retrievals
18 
19 extern "C" rtype* get_psn_pbd0_2_iseg_loc();
20 
21 extern "C" int get_psn_pbd0_2_nseg();
22 
23 // Fortran routines
24 extern "C" void get_field_Ah_cv_ff(Field<VarType::Scalar,PIT_GLOBAL>* psn_Ah_cv_ff);
25 extern "C" void get_pot_epar_em_filter(double* tmp, double* E_para_em);
26 
27 
28 template<class Device>
30  View<double**,CLayout,Device> field;
31 
32  public:
33 
34  AlternatingStorage(const std::string& name, int nnode)
35  : field(NoInit(name), 2, nnode)
36  {}
37 
38  // Functions to access the "alternating" Views
39  View<double*,CLayout, Device> left(int i_plane){
40  int i_left_alt = i_plane%2;
41  return my_subview(field,i_left_alt);
42  }
43 
44  View<double*,CLayout, Device> right(int i_plane){
45  int i_right_alt = (i_plane+1)%2;
46  return my_subview(field,i_right_alt);
47  }
48 };
49 
50 template<class Device, class DeviceOut>
52 
53  int nnode;
54  int nrho;
55  int nphi;
56  int ndim;
57 
58  View<double**,CLayout,Device> input_potential;
62  View<double**,CLayout,Device> potential;
63  View<double***,CLayout, Device> gradient;
64  View<double****,CLayout, Device> gradient_rho;
65  View<double***,CLayout, Device> potential_rho;
66 
67  View<double*,CLayout,Device> scratch;
68 
69  // Additional objects that should go elsewhere eventually, but are here
70  // because they are needed for get_pot_grad and can be reused for the different fields
75 
76  GetPotentialGradTemp(const Simulation<DeviceType>& sml, const Grid<DeviceType>& grid, const DomainDecomposition<DeviceType>& pol_decomp, const MagneticField<DeviceType>& magnetic_field, int n_input_potential_planes, bool gyroavg_requested, const GyroAverageMatrices<HostType>& gyro_avg_matrices_in = GyroAverageMatrices<HostType>())
77  : nnode(grid.nnode),
78  nrho(gyroavg_requested ? gyro_avg_matrices_in.nrho : 0),
79  nphi((PIT_GLOBAL==PhiInterpType::Planes) ? 2 : 1),
81  potential_alt("potential_alt", nnode),
82  gradient_r_alt("gradient_r_alt", nnode),
83  gradient_z_alt("gradient_r_alt", nnode),
84  potential(NoInit("potential"), nphi, nnode),
85  gradient(NoInit("gradient"), ndim, nphi, nnode),
86  potential_rho(NoInit("potential_rho"), nrho+1, nphi, nnode),
87  gradient_rho(NoInit("gradient_rho"), nrho+1, nphi, nnode, ndim),
88  gyro_avg_matrices(gyro_avg_matrices_in.template mirror<DeviceType>()) // Send gyromatrices to GPU
89  {
90  // Use scratch for transpose on device if the fields can't be written directly
91  if(!std::is_same<DeviceOut,Device>()){
92  scratch = View<double*,CLayout,Device>(NoInit("scratch"), gradient_rho.size());
93  input_potential = View<double**,CLayout,Device>(NoInit("input_potential"), n_input_potential_planes, nnode);
94  }
95 
96  GPTLstart("GET_POT_GRAD_MAT_SETUP");
97 #ifdef NO_FORTRAN_MODULES
98  grad_matrices = grid.gradient_matrices_h.template mirror<DeviceType>();
99 #else
100  grad_matrices = GradientMatrices<DeviceType>(true); // Copy in fortran data
101 #endif
102  GPTLstop("GET_POT_GRAD_MAT_SETUP");
103 
104  // This could be set up only once (if it doesnt take up too much device memory)
105  GPTLstart("GET_POT_GRAD_FF_SETUP");
107  GPTLstop("GET_POT_GRAD_FF_SETUP");
108 
109  // Set up for parallel gradient
110  GPTLstart("GET_POT_GRAD_GPTX_SETUP");
111 #ifdef NO_FORTRAN_MODULES
112  if(!sml.is_XGCa) gptx = GradParXTmp(grid, magnetic_field.bt_sign);
113 #else
114  if(!sml.is_XGCa) gptx = GradParXTmp(grid, get_psn_pbd0_2_nseg(), magnetic_field.bt_sign, get_psn_pbd0_2_iseg_loc());
115 #endif
116  GPTLstop("GET_POT_GRAD_GPTX_SETUP");
117  }
118 };
119 
120 template<class Device>
121 struct Field00{
124 
125  View<Field<VarType::Scalar,PhiInterpType::None>*,CLayout, Device> pot_managed; // Use if device != host
126  View<double*,CLayout, Device, Kokkos::MemoryTraits<Kokkos::Unmanaged>> pot;
127  View<double*,CLayout, Device> r;
128  View<double*,CLayout, Device> z;
129 
131  : is_provided(false)
132  {}
133 
134  template<class DeviceIn>
136  : r(NoInit("r"), field_in.f.extent(0)),
137  z(NoInit("z"), field_in.f.extent(0)),
138  is_provided(true),
139  discard_when_basis_is_one(discard_when_basis_is_one_in)
140  {
141  // Create a mirror if Device is different from the input DeviceIn
142  pot_managed = my_mirror_view(field_in.f, Device());
143  mirror_copy(pot_managed, field_in.f);
144  pot = View<double*,CLayout,Device, Kokkos::MemoryTraits<Kokkos::Unmanaged>>((double*)(pot_managed.data()), pot_managed.layout());
145  }
146 
147  void calculate_gradient(const Grid<DeviceType>& grid, const GradientMatrices<DeviceType>& grad_matrices){
148  grid_deriv(grad_matrices, grid,pot,r,z, discard_when_basis_is_one);
149 
150  auto r_tmp = r; // Use these to avoid using member function in kernel
151  auto z_tmp = z; // Use these to avoid using member function in kernel
152 
153  // rh This is general now --->
154  // We compute the electric field --> flip sign
155  // E=-E
156  Kokkos::parallel_for("get_pot_grad_flip_00", Kokkos::RangePolicy<ExSpace>(0,grid.nnode), KOKKOS_LAMBDA( const int i ){
157  r_tmp(i)=-r_tmp(i);
158  z_tmp(i)=-z_tmp(i);
159  });
160  }
161 
162  template<class DeviceOut, PhiInterpType PIT>
163  void set_field(const Grid<DeviceType>& grid, const FieldFollowingCoordinates& ff, View<Field<VarType::Vector2D,PIT>*,CLayout,DeviceOut>& field00_ff_h){
164  if (is_provided){
165  // Copy to two planes if field following is needed
166  int nphi = (PIT==PhiInterpType::Planes) ? 2 : 1;
167  View<double**,CLayout, Device> r_tmp(NoInit("r_tmp"), nphi, r.size());
168  View<double**,CLayout, Device> z_tmp(NoInit("z_tmp"), nphi, z.size());
169 
170  for(int iphi=0; iphi<nphi; iphi++){
171  Kokkos::deep_copy(my_subview(r_tmp, iphi), r);
172  Kokkos::deep_copy(my_subview(z_tmp, iphi), z);
173  }
174 
175  if(PIT==PhiInterpType::Planes){
176  ff.cnvt_grid_real2ff(grid,r_tmp);
177  ff.cnvt_grid_real2ff(grid,z_tmp);
178  }
179 
180  auto field00_ff = my_mirror_view(field00_ff_h, Device());
181  for(int iphi=0; iphi<nphi; iphi++){
182  Kokkos::parallel_for("set_field", Kokkos::RangePolicy<ExSpace>(0,field00_ff_h.extent(0)), KOKKOS_LAMBDA( const int i ){
183 #ifdef XGC1
184  field00_ff(i).V[iphi][0]=r_tmp(iphi,i);
185  field00_ff(i).V[iphi][1]=z_tmp(iphi,i);
186 #else
187  field00_ff(i).E[0]=r_tmp(iphi,i);
188  field00_ff(i).E[1]=z_tmp(iphi,i);
189 #endif
190  });
191  }
192  Kokkos::fence();
193  // Copy if not a mirror
194  mirror_copy(field00_ff_h, field00_ff);
195  }else{
196  Kokkos::parallel_for("set_field", Kokkos::RangePolicy<typename DeviceOut::execution_space>(0,field00_ff_h.extent(0)), KOKKOS_LAMBDA( const int i ){
197 #ifdef XGC1
198  field00_ff_h(i).V[0][0]=0.0;
199  field00_ff_h(i).V[0][1]=0.0;
200  field00_ff_h(i).V[1][0]=0.0;
201  field00_ff_h(i).V[1][1]=0.0;
202 #else
203  field00_ff_h(i).E[0]=0.0;
204  field00_ff_h(i).E[1]=0.0;
205 #endif
206  });
207  }
208  }
209 };
210 
211 template<class Device, class DeviceOut>
212 struct EMParField{
213  bool requested;
214 
215  View<double**,CLayout, Device> field;
216  View<double***,CLayout, Device> field_rho;
217 
219 
221  : requested(false)
222  {}
223 
225  requested = true;
226  field_out = output_field;
227  field_rho = View<double***,CLayout, Device>(NoInit("E_para_em_rho"), field_out.f.extent(1), 2, field_out.f.extent(0));
228  field = View<double**,CLayout, Device>(NoInit("E_para_em"), 2, field_out.f.extent(0));
229  }
230 
231  void calculate(const Grid<DeviceType>& grid, const View<double**,CLayout, Device>& field_para){
232  // Obtain E_para with the same filters that are applied in push_As
233  // This filtered E_para must be used in the equation of motion
234  // for dA_s/dt in case of pullback-mode 4.
235  GPTLstart("GET_POT_EPAR_EM");
236  // tmp_copy gets copied in and modified internally
237  View<double**,CLayout, HostType> tmp_copy(NoInit("tmp"),2,grid.nnode);
238  Kokkos::deep_copy(tmp_copy, field_para);
239 
240  // Output:
241  auto field_h = my_mirror_view(field, HostType());
242 
243  get_pot_epar_em_filter(tmp_copy.data(), field_h.data());
244 
245  // Copy result of get_pot_epar_em_filter back to device
246  mirror_copy(field, field_h);
247  GPTLstop("GET_POT_EPAR_EM");
248  }
249 };
250 
251 template<class DeviceIn, class DeviceOut, VarType VT, PhiInterpType PIT, TorType TT, KinType KT>
253  View<double**,CLayout,DeviceIn, Kokkos::MemoryTraits<Kokkos::Unmanaged>> input_potential;
261 
262  // Constructor if passed an unmanaged view
263  GetPotGradFieldArgs(const View<double**,CLayout,DeviceIn, Kokkos::MemoryTraits<Kokkos::Unmanaged>>& input_potential_in, bool ignore_poloidal_dpot_in=false)
264  : input_potential(input_potential_in),
265  ignore_poloidal_dpot(ignore_poloidal_dpot_in),
266  potential_is_requested(false),
267  gradient_is_requested(false)
268  {}
269 
270  // Constructor if passed a 1D managed view
271  GetPotGradFieldArgs(const View<double*,CLayout,DeviceIn>& input_potential_in, bool ignore_poloidal_dpot_in=false)
272  : input_potential(View<double**,CLayout,DeviceIn,Kokkos::MemoryTraits<Kokkos::Unmanaged>>((double*)(input_potential_in.data()), 1, input_potential_in.extent(0))),
273  ignore_poloidal_dpot(ignore_poloidal_dpot_in),
274  potential_is_requested(false),
275  gradient_is_requested(false)
276  {}
277 
279  potential = potential_in;
280  potential_is_requested = true;
281  }
282 
284  gradient = gradient_in;
285  gradient_is_requested = true;
286  }
287 
289  E_para_em.request(output_field);
290  }
291 };
292 
293 template<class DeviceIn, class DeviceOut, VarType VT, PhiInterpType PIT, TorType TT, KinType KT>
294 void get_field_grad(const Grid<DeviceType>& grid,
295  const DomainDecomposition<DeviceType>& pol_decomp,
299 
300 #endif
VarType
Definition: field.hpp:16
AlternatingStorage(const std::string &name, int nnode)
Definition: get_potential_grad.hpp:34
View< double **, CLayout, Device > input_potential
Definition: get_potential_grad.hpp:58
Definition: field_following_coordinates.hpp:9
static int GPTLstart(const char *name)
Definition: timer_macro.hpp:9
int nrho
Definition: get_potential_grad.hpp:54
View< double *, CLayout, Device > left(int i_plane)
Definition: get_potential_grad.hpp:39
Definition: gradparx2.hpp:7
constexpr VarType vec2d_if_axisym()
View< double *, CLayout, Device > scratch
Definition: get_potential_grad.hpp:67
int ndim
Definition: get_potential_grad.hpp:56
View< Field< VarType::Scalar, PhiInterpType::None > *, CLayout, Device > pot_managed
Definition: get_potential_grad.hpp:125
void mirror_copy(T1 &view_dest, const T2 &view_src)
Definition: my_mirror_view.hpp:122
bool ignore_poloidal_dpot
Definition: get_potential_grad.hpp:256
void request(const GridField< DeviceOut, VarType::Scalar, PIT_GLOBAL, TorType::OnePlane, KinType::GyroKin > &output_field)
Definition: get_potential_grad.hpp:224
void get_pot_epar_em_filter(double *tmp, double *E_para_em)
bool is_provided
Definition: get_potential_grad.hpp:122
Field00()
Definition: get_potential_grad.hpp:130
Definition: sml.hpp:8
static constexpr bool is_XGCa
Equivalent to the preprocessor flag for now.
Definition: sml.hpp:17
Kokkos::Device< HostExSpace, HostMemSpace > HostType
Definition: space_settings.hpp:56
FieldFollowingCoordinates ff
Definition: get_potential_grad.hpp:73
View< double **, CLayout, Device > potential
Definition: get_potential_grad.hpp:62
int nphi
Definition: get_potential_grad.hpp:55
Definition: magnetic_field.hpp:12
Definition: get_potential_grad.hpp:252
int get_psn_pbd0_2_nseg()
void cnvt_grid_real2ff(const Grid< DeviceType > &grid, const View< double **, CLayout, DeviceType > &input, const View< double **, Kokkos::LayoutRight, DeviceType > &output) const
Definition: field_following_coordinates.hpp:103
bool discard_when_basis_is_one
Definition: get_potential_grad.hpp:123
Definition: gradparx2.hpp:43
Definition: get_potential_grad.hpp:121
Kokkos::LayoutRight CLayout
Definition: space_settings.hpp:67
EMParField< DeviceType, DeviceOut > E_para_em
Definition: get_potential_grad.hpp:260
GradientMatrices< DeviceType > grad_matrices
Definition: get_potential_grad.hpp:72
GridField< DeviceOut, VT, PIT, TT, KT > gradient
Definition: get_potential_grad.hpp:254
bool potential_is_requested
Definition: get_potential_grad.hpp:257
Projection< HostType > half_plane_ff
Definition: grid.hpp:289
void get_field_grad(const Grid< DeviceType > &grid, const DomainDecomposition< DeviceType > &pol_decomp, const MagneticField< DeviceType > &magnetic_field, GetPotGradFieldArgs< DeviceIn, DeviceOut, VT, PIT, TT, KT > &args, GetPotentialGradTemp< DeviceType, DeviceOut > &tmp)
Definition: get_potential_grad.cpp:428
Definition: grid_field.hpp:25
View< double *, CLayout, Device, Kokkos::MemoryTraits< Kokkos::Unmanaged > > pot
Definition: get_potential_grad.hpp:126
AlternatingStorage< Device > gradient_z_alt
Definition: get_potential_grad.hpp:61
bool gradient_is_requested
Definition: get_potential_grad.hpp:258
View< double **, CLayout, DeviceIn, Kokkos::MemoryTraits< Kokkos::Unmanaged > > input_potential
Definition: get_potential_grad.hpp:253
void get_field_Ah_cv_ff(Field< VarType::Scalar, PIT_GLOBAL > *psn_Ah_cv_ff)
View< double ***, CLayout, Device > gradient
Definition: get_potential_grad.hpp:63
PhiInterpType
Definition: globals.hpp:89
View< double ***, CLayout, Device > potential_rho
Definition: get_potential_grad.hpp:65
View< double *, CLayout, Device > right(int i_plane)
Definition: get_potential_grad.hpp:44
subroutine grid_deriv(grid, qty, qty_deriv_x, qty_deriv_y, psi_only)
Definition: search.F90:3994
constexpr PhiInterpType PIT_GLOBAL
Definition: globals.hpp:97
AlternatingStorage< Device > gradient_r_alt
Definition: get_potential_grad.hpp:60
double bt_sign
Whether toroidal field is reversed?
Definition: magnetic_field.hpp:19
View< double ***, CLayout, Device > field_rho
Definition: get_potential_grad.hpp:216
Field00< DeviceType > field00
Definition: get_potential_grad.hpp:259
GetPotGradFieldArgs(const View< double *, CLayout, DeviceIn > &input_potential_in, bool ignore_poloidal_dpot_in=false)
Definition: get_potential_grad.hpp:271
EMParField()
Definition: get_potential_grad.hpp:220
void request_potential(const GridField< DeviceOut, VarType::Scalar, PIT, TT, KT > &potential_in)
Definition: get_potential_grad.hpp:278
rtype * get_psn_pbd0_2_iseg_loc()
void request_para_em(const GridField< DeviceOut, VarType::Scalar, PIT_GLOBAL, TorType::OnePlane, KinType::GyroKin > &output_field)
Definition: get_potential_grad.hpp:288
GyroAverageMatrices< DeviceType > gyro_avg_matrices
Definition: get_potential_grad.hpp:71
View< double *, CLayout, Device > z
Definition: get_potential_grad.hpp:128
Field00(const GridField< DeviceIn, VarType::Scalar, PhiInterpType::None, TorType::OnePlane, KinType::DriftKin > &field_in, bool discard_when_basis_is_one_in)
Definition: get_potential_grad.hpp:135
int nnode
Definition: get_potential_grad.hpp:53
Definition: field.hpp:50
Definition: get_potential_grad.hpp:212
View< T *, CLayout, Device > my_mirror_view(const View< T *, CLayout, Device > &view, Device nd)
Definition: my_mirror_view.hpp:14
Kokkos::View< T *, Kokkos::LayoutRight, Device > my_subview(const Kokkos::View< T ****, Kokkos::LayoutRight, Device > &view, int i, int j, int k)
Definition: my_subview.hpp:8
Definition: magnetic_field.F90:1
Kokkos::Device< ExSpace, MemSpace > DeviceType
Definition: space_settings.hpp:47
GridField< DeviceOut, VarType::Scalar, PIT, TT, KT > potential
Definition: get_potential_grad.hpp:255
void calculate(const Grid< DeviceType > &grid, const View< double **, CLayout, Device > &field_para)
Definition: get_potential_grad.hpp:231
Definition: get_potential_grad.hpp:29
AlternatingStorage< Device > potential_alt
Definition: get_potential_grad.hpp:59
void request_gradient(const GridField< DeviceOut, VT, PIT, TT, KT > &gradient_in)
Definition: get_potential_grad.hpp:283
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 calculate_gradient(const Grid< DeviceType > &grid, const GradientMatrices< DeviceType > &grad_matrices)
Definition: get_potential_grad.hpp:147
View< double **, CLayout, Device > field
Definition: get_potential_grad.hpp:215
GridField< DeviceOut, VarType::Scalar, PIT_GLOBAL, TorType::OnePlane, KinType::GyroKin > field_out
Definition: get_potential_grad.hpp:218
int nnode
Number of grid nodes.
Definition: grid.hpp:246
GradParXTmp gptx
Definition: get_potential_grad.hpp:74
View< double *, CLayout, Device > r
Definition: get_potential_grad.hpp:127
View< double **, CLayout, Device > field
Definition: get_potential_grad.hpp:30
void set_field(const Grid< DeviceType > &grid, const FieldFollowingCoordinates &ff, View< Field< VarType::Vector2D, PIT > *, CLayout, DeviceOut > &field00_ff_h)
Definition: get_potential_grad.hpp:163
Kokkos::ViewAllocateWithoutInitializing NoInit
Definition: space_settings.hpp:68
bool requested
Definition: get_potential_grad.hpp:213
GetPotGradFieldArgs(const View< double **, CLayout, DeviceIn, Kokkos::MemoryTraits< Kokkos::Unmanaged >> &input_potential_in, bool ignore_poloidal_dpot_in=false)
Definition: get_potential_grad.hpp:263
View< double ****, CLayout, Device > gradient_rho
Definition: get_potential_grad.hpp:64
static int GPTLstop(const char *name)
Definition: timer_macro.hpp:10
Definition: get_potential_grad.hpp:51