XGC1
globals.hpp
Go to the documentation of this file.
1 #ifndef GLOBALS_HPP
2 #define GLOBALS_HPP
3 #include <limits.h>
4 #include <string>
5 #include <cassert>
6 #include "space_settings.hpp"
7 #include "array_deep_copy.hpp"
8 #include "access_add.hpp"
9 #include "simd.hpp"
10 #ifdef USE_MPI
11 #include "my_mpi.hpp"
12 #endif
13 #include "constants.hpp"
14 
15 /* Returns max number of omp threads, or 1 if no omp
16  */
17 inline int get_num_cpu_threads(){
18 #ifdef USE_OMP
19  return omp_get_max_threads();
20 #else
21  return 1;
22 #endif
23 }
24 
25 /* Return true if MPI rank is zero
26  * */
27 inline bool is_rank_zero(){
28 #ifdef USE_MPI
29  return SML_COMM_RANK==0;
30 #else
31  return true;
32 #endif
33 }
34 
35 /* Safely abort and exit the code
36  * */
37 inline void exit_XGC(std::string msg){
38  printf("%s",msg.c_str());
39  fflush(stdout);
40 #ifdef USE_MPI
41  MPI_Abort(SML_COMM_WORLD, 1);
42 #else
43  exit(1);
44 #endif
45 }
46 
47 /* Asserts condition
48  * (Callable from Device)
49  * (C++ has no standardized assert with a message)
50  * */
51 KOKKOS_INLINE_FUNCTION void assert_XGC(bool cond, const char* msg){
52  if(!cond){
53  DEVICE_PRINTF("%s",msg);
54  assert(false);
55  }
56 }
57 
58 /* Check that two integers multiplied together doesn't cause an overflow
59  * */
60 inline bool causes_multiplication_overflow(int a,int b){
61  int c = a*b;
62  if(b==0) return false; // No overflow
63  return !(c/b == a);
64 }
65 
66 /* Check that two integers added together doesn't cause an overflow
67  * */
68 inline bool causes_addition_overflow(int a,int b){
69  if ( ((b > 0) && (a > INT_MAX - b)) // a + b would overflow
70  ||((b < 0) && (a < INT_MIN - b)) ){ // a + b would underflow
71  return true;
72  } else {
73  return false;
74  }
75 }
76 
77 enum class Order{
78  Zero,
79  One,
80  Two
81 };
82 
84  ELECTRON = 0,
85  MAIN_ION = 1
86 };
87 
88 enum KinType{
90  GyroKin=1
91 };
92 
93 /* PhiInterpType specifies whether the field uses two values in order to interpolate in the phi direction
94  * */
95 enum class PhiInterpType{
96  Planes,
97  None
98 };
99 
100 // Eventually, nearly the entire code will be templated on PhiInterpType, which is XGC1 vs XGCa
101 // As an intermediate step, it is convenient to specify the template in one global location
102 #ifdef XGC1
104 #else
106 #endif
107 
108 /* What type of marker weight algorithm to use, reduced delta-f or total-f
109  */
110 enum class MarkerType{
112  FullF,
113  TotalF,
114  None
115 };
116 enum class FAnalyticShape{
117  Maxwellian,
118  SlowingDown,
119  None
120 };
121 enum class WeightEvoEq{
122  Direct,
123  PDE,
124  None
125 };
126 
127 enum class MagneticFieldMode{
130 };
131 #ifdef EXPLICIT_EM
133 #else
135 #endif
136 
137 enum class BFieldSymmetry{
138  Tokamak,
140 };
141 #ifdef STELLARATOR
143 #else
145 #endif
146 
147 /* Whether to use cylindrical limit geometry with periodic boundary function
148  */
149 enum class GeometryType{
150  Toroidal,
152 };
153 #ifdef CYLINDRICAL
155 #else
157 #endif
158 
168 template<GeometryType GT>
169 KOKKOS_INLINE_FUNCTION double geometry_switch(double a, double b);
170 
171 template<>
172 KOKKOS_INLINE_FUNCTION double geometry_switch<GeometryType::Toroidal>(double a, double b){
173  return a;
174 }
175 
176 template<>
177 KOKKOS_INLINE_FUNCTION double geometry_switch<GeometryType::CylindricalLimit>(double a, double b){
178  return b;
179 }
180 
181 
185 template<GeometryType GT>
186 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms();
187 
188 template<>
189 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms<GeometryType::CylindricalLimit>(){
190  return 0;
191 }
192 
193 template<>
194 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms<GeometryType::Toroidal>(){
195  return 1;
196 }
197 
198 enum class PullbackMethod{
200  IdealMHD
201 };
202 
203 /***/
204 
206  PIR = 0,
207  PIZ,
208  PIP,
212  PTL_NPHASE
213 };
214 
216  PIM = 0,
219 #ifdef PTL_G2
220  PIG2,
221 #endif
222  PTL_NCONST
223 };
224 
225 // Divide two integers, then round up
226 KOKKOS_INLINE_FUNCTION int divide_and_round_up(int a, int b){
227  return (a+b-1)/b;
228 }
229 
230 // Positive modulo. i.e. positive_modulo(-1,3) will return 2, whereas (-1)%3 returns -1.
231 KOKKOS_INLINE_FUNCTION unsigned positive_modulo( int value, unsigned m) {
232  int mod = value % (int)m;
233  if (mod < 0) {
234  mod += m;
235  }
236  return mod;
237 }
238 
239 /* Returns the offset of an evenly distributed set of objects into even subsets. If the set can be distributed evenly,
240  * then this operation is simply the local subset times the result of the division. If not, then the first k
241  * subsets will have one extra object, where k is the remainder.
242  *
243  * n_obj is the number of objects that need to be divided
244  * n_subsets is the number of ranks the objects are being divided into
245  * i_subset is the index of this particular subset
246  *
247  */
248 inline long long int offsets_of_even_distribution(long long int n_obj, long long int n_subsets, long long int i_subset){
249  long long int obj_per_subset = n_obj/n_subsets;
250  long long int remainder = n_obj%n_subsets;
251  if (i_subset<remainder){
252  obj_per_subset += 1;
253  return i_subset*obj_per_subset;
254  }else{
255  return i_subset*obj_per_subset + remainder;
256  }
257 }
258 
259 /* Returns the count of an evenly distributed set of objects into even subsets. If the set can be distributed evenly,
260  * then this operation is simply the result of the division. If not, then the first k
261  * subsets will have one extra object, where k is the remainder.
262  *
263  * n_obj is the number of objects that need to be divided
264  * n_subsets is the number of ranks the objects are being divided into
265  * i_subset is the index of this particular subset
266  *
267  */
268 inline long long int counts_of_even_distribution(long long int n_obj, long long int n_subsets, long long int i_subset){
269  long long int obj_per_subset = n_obj/n_subsets;
270  long long int remainder = n_obj%n_subsets;
271  if (i_subset<remainder){
272  obj_per_subset += 1;
273  }
274  return obj_per_subset;
275 }
276 
277 // Converts integer to string and adds leading zeros
278 inline std::string formatted_int2str(int input, int n_digits){
279  std::string string_no_leading = std::to_string(input); // no leading zeros
280 
281  int initial_length = string_no_leading.length();
282  int zeros_to_add = n_digits - std::min(n_digits, initial_length);
283 
284  // Add leading zeros
285  return std::string(zeros_to_add, '0') + string_no_leading;
286 }
287 
288 // is_same_type is equivalent to std::is_same, but can be used in cuda kernels
289 template<class T, class U> struct is_same_type{static constexpr bool val=false;};
290 template<class T > struct is_same_type<T, T>{static constexpr bool val=true;};
291 
292 // Defined and initialized in my_mpi.cpp, reset in sml.tpp
293 extern bool global_debug_flag;
294 extern bool global_perf_barriers_flag;
295 
296 #endif
void exit_XGC(std::string msg)
Definition: globals.hpp:37
PullbackMethod
Definition: globals.hpp:198
ParticlePhase
Definition: globals.hpp:205
@ PIW1
1st weight
Definition: globals.hpp:210
@ PIW2
2nd weight
Definition: globals.hpp:211
@ PIP
phi coordinate
Definition: globals.hpp:208
@ PIRHO
gyroradius
Definition: globals.hpp:209
@ PIR
r coordinate
Definition: globals.hpp:206
@ PTL_NPHASE
Definition: globals.hpp:212
@ PIZ
z coordinate
Definition: globals.hpp:207
constexpr MagneticFieldMode MFM_GLOBAL
Definition: globals.hpp:134
GeometryType
Definition: globals.hpp:149
FAnalyticShape
Definition: globals.hpp:116
int get_num_cpu_threads()
Definition: globals.hpp:17
bool causes_multiplication_overflow(int a, int b)
Definition: globals.hpp:60
bool is_rank_zero()
Definition: globals.hpp:27
SpeciesType
Definition: globals.hpp:83
@ ELECTRON
Definition: globals.hpp:84
@ MAIN_ION
Definition: globals.hpp:85
long long int offsets_of_even_distribution(long long int n_obj, long long int n_subsets, long long int i_subset)
Definition: globals.hpp:248
MagneticFieldMode
Definition: globals.hpp:127
KOKKOS_INLINE_FUNCTION void assert_XGC(bool cond, const char *msg)
Definition: globals.hpp:51
BFieldSymmetry
Definition: globals.hpp:137
KOKKOS_INLINE_FUNCTION int divide_and_round_up(int a, int b)
Definition: globals.hpp:226
constexpr KOKKOS_INLINE_FUNCTION bool use_toroidal_terms()
KOKKOS_INLINE_FUNCTION unsigned positive_modulo(int value, unsigned m)
Definition: globals.hpp:231
WeightEvoEq
Definition: globals.hpp:121
bool causes_addition_overflow(int a, int b)
Definition: globals.hpp:68
long long int counts_of_even_distribution(long long int n_obj, long long int n_subsets, long long int i_subset)
Definition: globals.hpp:268
constexpr BFieldSymmetry BFS_GLOBAL
Definition: globals.hpp:144
constexpr PhiInterpType PIT_GLOBAL
Definition: globals.hpp:103
bool global_perf_barriers_flag
Definition: checkpoint.cpp:12
constexpr GeometryType GEOMETRY
Definition: globals.hpp:156
bool global_debug_flag
Definition: checkpoint.cpp:11
KinType
Definition: globals.hpp:88
@ GyroKin
Definition: globals.hpp:90
@ DriftKin
Definition: globals.hpp:89
ParticleConsts
Definition: globals.hpp:215
@ PTL_NCONST
Definition: globals.hpp:222
@ PIF0
F0.
Definition: globals.hpp:218
@ PIW0
W0.
Definition: globals.hpp:217
@ PIM
Magnetic moment mu.
Definition: globals.hpp:216
MarkerType
Definition: globals.hpp:110
PhiInterpType
Definition: globals.hpp:95
KOKKOS_INLINE_FUNCTION double geometry_switch(double a, double b)
std::string formatted_int2str(int input, int n_digits)
Definition: globals.hpp:278
Order
Definition: globals.hpp:77
int SML_COMM_RANK
Definition: my_mpi.cpp:5
MPI_Comm SML_COMM_WORLD
Definition: my_mpi.cpp:4
#define DEVICE_PRINTF(...)
Definition: space_settings.hpp:86
Definition: globals.hpp:289
static constexpr bool val
Definition: globals.hpp:289