XGCa
 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 
14 /* Returns max number of omp threads, or 1 if no omp
15  */
16 inline int get_num_cpu_threads(){
17 #ifdef _OPENMP
18  return omp_get_max_threads();
19 #else
20  return 1;
21 #endif
22 }
23 
24 /* Return true if MPI rank is zero
25  * */
26 inline bool is_rank_zero(){
27 #ifdef USE_MPI
28  return SML_COMM_RANK==0;
29 #else
30  return true;
31 #endif
32 }
33 
34 /* Safely abort and exit the code
35  * */
36 inline void exit_XGC(std::string msg){
37  printf("%s",msg.c_str());
38  fflush(stdout);
39 #ifdef USE_MPI
40  MPI_Abort(SML_COMM_WORLD, 1);
41 #else
42  exit(1);
43 #endif
44 }
45 
46 /* Asserts condition
47  * (Callable from Device)
48  * (C++ has no standardized assert with a message)
49  * */
50 KOKKOS_INLINE_FUNCTION void assert_XGC(bool cond, const char* msg){
51  if(!cond){
52  DEVICE_PRINTF("%s",msg);
53  assert(false);
54  }
55 }
56 
57 /* Check that two integers multiplied together doesn't cause an overflow
58  * */
59 inline bool causes_multiplication_overflow(int a,int b){
60  int c = a*b;
61  if(b==0) return false; // No overflow
62  return !(c/b == a);
63 }
64 
65 /* Check that two integers added together doesn't cause an overflow
66  * */
67 inline bool causes_addition_overflow(int a,int b){
68  if ( ((b > 0) && (a > INT_MAX - b)) // a + b would overflow
69  ||((b < 0) && (a < INT_MIN - b)) ){ // a + b would underflow
70  return true;
71  } else {
72  return false;
73  }
74 }
75 
77  ELECTRON = 0,
79 };
80 
81 enum KinType{
84 };
85 
86 /* PhiInterpType specifies whether the field uses two values in order to interpolate in the phi direction
87  * */
88 enum class PhiInterpType{
89  Planes,
90  None
91 };
92 
93 // Eventually, nearly the entire code will be templated on PhiInterpType, which is XGC1 vs XGCa
94 // As an intermediate step, it is convenient to specify the template in one global location
95 #ifdef XGC1
97 #else
99 #endif
100 
101 /* Whether to use cylindrical limit geometry with periodic boundary function
102  */
103 enum class GeometryType{
104  Toroidal,
106 };
107 #ifdef CYLINDRICAL
109 #else
111 #endif
112 
122 template<GeometryType GT>
123 KOKKOS_INLINE_FUNCTION double geometry_switch(double a, double b);
124 
125 template<>
126 KOKKOS_INLINE_FUNCTION double geometry_switch<GeometryType::Toroidal>(double a, double b){
127  return a;
128 }
129 
130 template<>
131 KOKKOS_INLINE_FUNCTION double geometry_switch<GeometryType::CylindricalLimit>(double a, double b){
132  return b;
133 }
134 
135 
139 template<GeometryType GT>
140 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms();
141 
142 template<>
143 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms<GeometryType::CylindricalLimit>(){
144  return 0;
145 }
146 
147 template<>
148 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms<GeometryType::Toroidal>(){
149  return 1;
150 }
151 
152 /***/
153 
155  PIR = 0,
156  PIZ,
157  PIP,
162 };
163 
165  PIM = 0,
169 };
170 
171 constexpr double UNIT_CHARGE = 1.6022e-19;
172 constexpr double EV_2_J=UNIT_CHARGE;
173 constexpr double J_2_EV=1.0/EV_2_J;
174 constexpr double PROTON_MASS = 1.6720e-27; //< proton mass (MKS)
175 constexpr double PI = 3.1415926535897932; //< pi
176 constexpr double TWOPI = 6.2831853071795862; //< 2 pi
177 
178 // Divide two integers, then round up
179 KOKKOS_INLINE_FUNCTION int divide_and_round_up(int a, int b){
180  return (a+b-1)/b;
181 }
182 
183 // Positive modulo. i.e. positive_modulo(-1,3) will return 2, whereas (-1)%3 returns -1.
184 KOKKOS_INLINE_FUNCTION unsigned positive_modulo( int value, unsigned m) {
185  int mod = value % (int)m;
186  if (mod < 0) {
187  mod += m;
188  }
189  return mod;
190 }
191 
192 // Defined and initialized in my_mpi.cpp, reset in sml.tpp
193 extern bool global_debug_flag;
194 
195 #endif
constexpr double EV_2_J
Conversion rate ev to J.
Definition: globals.hpp:172
Definition: globals.hpp:77
Magnetic moment mu.
Definition: globals.hpp:165
KOKKOS_INLINE_FUNCTION int divide_and_round_up(int a, int b)
Definition: globals.hpp:179
constexpr GeometryType GEOMETRY
Definition: globals.hpp:110
bool is_rank_zero()
Definition: globals.hpp:26
gyroradius
Definition: globals.hpp:158
MPI_Comm SML_COMM_WORLD
Definition: my_mpi.cpp:4
#define DEVICE_PRINTF(...)
Definition: space_settings.hpp:84
Definition: globals.hpp:82
W0.
Definition: globals.hpp:166
bool causes_multiplication_overflow(int a, int b)
Definition: globals.hpp:59
Definition: globals.hpp:161
bool global_debug_flag
Definition: checkpoint.cpp:11
KOKKOS_INLINE_FUNCTION unsigned positive_modulo(int value, unsigned m)
Definition: globals.hpp:184
constexpr double J_2_EV
Conversion rate J to ev.
Definition: globals.hpp:173
PhiInterpType
Definition: globals.hpp:88
r coordinate
Definition: globals.hpp:155
constexpr PhiInterpType PIT_GLOBAL
Definition: globals.hpp:98
Definition: globals.hpp:168
Definition: globals.hpp:83
ParticlePhase
Definition: globals.hpp:154
2nd weight
Definition: globals.hpp:160
int SML_COMM_RANK
Definition: my_mpi.cpp:5
KinType
Definition: globals.hpp:81
constexpr double PROTON_MASS
Definition: globals.hpp:174
GeometryType
Definition: globals.hpp:103
KOKKOS_INLINE_FUNCTION double geometry_switch(double a, double b)
constexpr double TWOPI
Definition: globals.hpp:176
Definition: globals.hpp:78
void exit_XGC(std::string msg)
Definition: globals.hpp:36
F0.
Definition: globals.hpp:167
int get_num_cpu_threads()
Definition: globals.hpp:16
phi coordinate
Definition: globals.hpp:157
KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms()
constexpr double PI
Definition: globals.hpp:175
1st weight
Definition: globals.hpp:159
ParticleConsts
Definition: globals.hpp:164
z coordinate
Definition: globals.hpp:156
SpeciesType
Definition: globals.hpp:76
constexpr double UNIT_CHARGE
Charge of an electron (C)
Definition: globals.hpp:171
bool causes_addition_overflow(int a, int b)
Definition: globals.hpp:67
KOKKOS_INLINE_FUNCTION void assert_XGC(bool cond, const char *msg)
Definition: globals.hpp:50