XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
102  PIR = 0,
103  PIZ,
104  PIP,
109 };
110 
112  PIM = 0,
116 };
117 
118 constexpr double UNIT_CHARGE = 1.6022e-19;
119 constexpr double EV_2_J=UNIT_CHARGE;
120 constexpr double J_2_EV=1.0/EV_2_J;
121 constexpr double PROTON_MASS = 1.6720e-27; //< proton mass (MKS)
122 constexpr double PI = 3.1415926535897932; //< pi
123 constexpr double TWOPI = 6.2831853071795862; //< 2 pi
124 
125 // Divide two integers, then round up
126 KOKKOS_INLINE_FUNCTION int divide_and_round_up(int a, int b){
127  return (a+b-1)/b;
128 }
129 
130 // Positive modulo. i.e. positive_modulo(-1,3) will return 2, whereas (-1)%3 returns -1.
131 KOKKOS_INLINE_FUNCTION unsigned positive_modulo( int value, unsigned m) {
132  int mod = value % (int)m;
133  if (mod < 0) {
134  mod += m;
135  }
136  return mod;
137 }
138 
139 #endif
constexpr double EV_2_J
Conversion rate ev to J.
Definition: globals.hpp:119
Definition: globals.hpp:77
Magnetic moment mu.
Definition: globals.hpp:112
KOKKOS_INLINE_FUNCTION int divide_and_round_up(int a, int b)
Definition: globals.hpp:126
bool is_rank_zero()
Definition: globals.hpp:26
gyroradius
Definition: globals.hpp:105
MPI_Comm SML_COMM_WORLD
Definition: my_mpi.cpp:4
#define DEVICE_PRINTF(...)
Definition: space_settings.hpp:77
Definition: globals.hpp:82
W0.
Definition: globals.hpp:113
bool causes_multiplication_overflow(int a, int b)
Definition: globals.hpp:59
Definition: globals.hpp:108
KOKKOS_INLINE_FUNCTION unsigned positive_modulo(int value, unsigned m)
Definition: globals.hpp:131
constexpr double J_2_EV
Conversion rate J to ev.
Definition: globals.hpp:120
PhiInterpType
Definition: globals.hpp:88
r coordinate
Definition: globals.hpp:102
constexpr PhiInterpType PIT_GLOBAL
Definition: globals.hpp:98
Definition: globals.hpp:115
Definition: globals.hpp:83
ParticlePhase
Definition: globals.hpp:101
2nd weight
Definition: globals.hpp:107
int SML_COMM_RANK
Definition: my_mpi.cpp:5
KinType
Definition: globals.hpp:81
constexpr double PROTON_MASS
Definition: globals.hpp:121
constexpr double TWOPI
Definition: globals.hpp:123
Definition: globals.hpp:78
void exit_XGC(std::string msg)
Definition: globals.hpp:36
F0.
Definition: globals.hpp:114
int get_num_cpu_threads()
Definition: globals.hpp:16
phi coordinate
Definition: globals.hpp:104
constexpr double PI
Definition: globals.hpp:122
1st weight
Definition: globals.hpp:106
ParticleConsts
Definition: globals.hpp:111
z coordinate
Definition: globals.hpp:103
SpeciesType
Definition: globals.hpp:76
constexpr double UNIT_CHARGE
Charge of an electron (C)
Definition: globals.hpp:118
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