XGC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 _OPENMP
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 
78  ELECTRON = 0,
80 };
81 
82 enum KinType{
85 };
86 
87 /* PhiInterpType specifies whether the field uses two values in order to interpolate in the phi direction
88  * */
89 enum class PhiInterpType{
90  Planes,
91  None
92 };
93 
94 // Eventually, nearly the entire code will be templated on PhiInterpType, which is XGC1 vs XGCa
95 // As an intermediate step, it is convenient to specify the template in one global location
96 #ifdef XGC1
98 #else
100 #endif
101 
102 /* Whether to use cylindrical limit geometry with periodic boundary function
103  */
104 enum class GeometryType{
105  Toroidal,
107 };
108 #ifdef CYLINDRICAL
110 #else
112 #endif
113 
123 template<GeometryType GT>
124 KOKKOS_INLINE_FUNCTION double geometry_switch(double a, double b);
125 
126 template<>
127 KOKKOS_INLINE_FUNCTION double geometry_switch<GeometryType::Toroidal>(double a, double b){
128  return a;
129 }
130 
131 template<>
132 KOKKOS_INLINE_FUNCTION double geometry_switch<GeometryType::CylindricalLimit>(double a, double b){
133  return b;
134 }
135 
136 
140 template<GeometryType GT>
141 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms();
142 
143 template<>
144 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms<GeometryType::CylindricalLimit>(){
145  return 0;
146 }
147 
148 template<>
149 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms<GeometryType::Toroidal>(){
150  return 1;
151 }
152 
153 /***/
154 
156  PIR = 0,
157  PIZ,
158  PIP,
163 };
164 
166  PIM = 0,
170 };
171 
172 // Divide two integers, then round up
173 KOKKOS_INLINE_FUNCTION int divide_and_round_up(int a, int b){
174  return (a+b-1)/b;
175 }
176 
177 // Positive modulo. i.e. positive_modulo(-1,3) will return 2, whereas (-1)%3 returns -1.
178 KOKKOS_INLINE_FUNCTION unsigned positive_modulo( int value, unsigned m) {
179  int mod = value % (int)m;
180  if (mod < 0) {
181  mod += m;
182  }
183  return mod;
184 }
185 
186 // Defined and initialized in my_mpi.cpp, reset in sml.tpp
187 extern bool global_debug_flag;
188 
189 #endif
Definition: globals.hpp:78
Magnetic moment mu.
Definition: globals.hpp:166
KOKKOS_INLINE_FUNCTION int divide_and_round_up(int a, int b)
Definition: globals.hpp:173
constexpr GeometryType GEOMETRY
Definition: globals.hpp:111
bool is_rank_zero()
Definition: globals.hpp:27
gyroradius
Definition: globals.hpp:159
MPI_Comm SML_COMM_WORLD
Definition: my_mpi.cpp:4
#define DEVICE_PRINTF(...)
Definition: space_settings.hpp:85
Definition: globals.hpp:83
W0.
Definition: globals.hpp:167
bool causes_multiplication_overflow(int a, int b)
Definition: globals.hpp:60
Definition: globals.hpp:162
bool global_debug_flag
Definition: checkpoint.cpp:11
KOKKOS_INLINE_FUNCTION unsigned positive_modulo(int value, unsigned m)
Definition: globals.hpp:178
PhiInterpType
Definition: globals.hpp:89
r coordinate
Definition: globals.hpp:156
constexpr PhiInterpType PIT_GLOBAL
Definition: globals.hpp:97
Definition: globals.hpp:169
Definition: globals.hpp:84
ParticlePhase
Definition: globals.hpp:155
2nd weight
Definition: globals.hpp:161
int SML_COMM_RANK
Definition: my_mpi.cpp:5
KinType
Definition: globals.hpp:82
GeometryType
Definition: globals.hpp:104
KOKKOS_INLINE_FUNCTION double geometry_switch(double a, double b)
Definition: globals.hpp:79
void exit_XGC(std::string msg)
Definition: globals.hpp:37
F0.
Definition: globals.hpp:168
int get_num_cpu_threads()
Definition: globals.hpp:17
phi coordinate
Definition: globals.hpp:158
KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms()
1st weight
Definition: globals.hpp:160
ParticleConsts
Definition: globals.hpp:165
z coordinate
Definition: globals.hpp:157
SpeciesType
Definition: globals.hpp:77
bool causes_addition_overflow(int a, int b)
Definition: globals.hpp:68
KOKKOS_INLINE_FUNCTION void assert_XGC(bool cond, const char *msg)
Definition: globals.hpp:51