XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
basic_physics.hpp
Go to the documentation of this file.
1 #ifndef BASIC_PHYSICS_HPP
2 #define BASIC_PHYSICS_HPP
3 #include "space_settings.hpp"
4 #include "constants.hpp"
5 #include "simd.hpp"
6 
7 // Kinetic energy
8 // E = (1/2) m v^2
9 KOKKOS_INLINE_FUNCTION double kinetic_energy(double mass, double v){
10  return 0.5*mass*v*v;
11 }
12 
13 // Return equilibrium distribution function
14 // f = (n / sqrt(T^3)) * exp(-E/T)
15 // T and E are EV units for XGC normalization-particle.
16 KOKKOS_INLINE_FUNCTION double maxwellian_dist(double den, double temp, double energy){
17  return den/(temp*sqrt(temp))*exp(-min(EXP_LIM,energy/temp));
18 }
19 
20 // Maxwellian distribution of 1D velocity part
21 // sqrt(m/TWOPI e) is missing when temp and energy are in eV unit.
22 // sqrt(m/TWOPI) is missing when temp and energy are in Joule.
23 // (1.0 - exp(-maxe)) is correction factor since the energy is finite
24 KOKKOS_INLINE_FUNCTION double maxwellian_dist_1d_v(double temp, double energy, double maxe){
25  return 1.0/sqrt(temp)*exp(-min(EXP_LIM,energy/temp)) / (1.0-exp(-maxe));
26 }
27 
28 // Maxwellian distribution of 2D velocity part
29 // m/(TWOPI e) is missing when temp and energy are in eV unit.
30 // m/TWOPI is missing when temp and energy are in Joule.
31 // (1.0 - exp(-maxe)) is correction factor since the energy is finite
32 KOKKOS_INLINE_FUNCTION double maxwellian_dist_2d_v(double temp, double energy, double maxe){
33  return 1.0/temp*exp(-min(EXP_LIM,energy/temp)) / (1.0-exp(-maxe));
34 }
35 
36 // Return adiabatic exponential factor
37 // Should be the same as get_ad of f0module.F90
38 // When the modified adiabatic function is used, the adiabatic response becomes
39 // linear function in (-a,a) --> exp_ad(x) = 1+x
40 // exponential decay to zero in x<-a --> exp_ad(x)=exp(-|x|+a)*(1-a)
41 // This satisfies continuous condition
42 // mirror symmetry (0,1) --> x-> -x, y-> 2-y
43 // for (x>a), exp_ad(x)=2 - exp(-|x|+a)*(1-a)
44 KOKKOS_INLINE_FUNCTION double exp_ad(double x){
45 #ifdef USE_LINEAR_ADIABATIC_RESPONSE
46  constexpr bool use_linear = true;
47 #else
48  constexpr bool use_linear = false;
49 #endif
50 
51 #ifdef LINEAR_ADIABATIC_RESPONSE_BD
52  constexpr double lim=LINEAR_ADIABATIC_RESPONSE_BD;
53 #else
54  constexpr double lim=0.7;
55 #endif
56  if constexpr(use_linear){
57  double tmp = exp(-abs(x)+lim)*(1-lim);
58  double tmp2 = (x<-lim ? tmp : 1.0+x);
59  return (x>lim ? 2.0-tmp : tmp2);
60  } else {
61  return exp(x);
62  }
63 }
64 
65 // gyro_radius
66 KOKKOS_INLINE_FUNCTION double gyro_radius(double B, double mu, double c2_2m){
67  return sqrt(mu/(B*c2_2m));
68 }
69 
70 /* Returns thermal velocity
71  * @param[in] mass species mass
72  * @param[in] temp_ev reference temperature
73  * return thermal velocity
74  */
75 KOKKOS_INLINE_FUNCTION double thermal_velocity(double mass, double temp_ev){
76  return sqrt((temp_ev*EV_2_J)/mass);
77 }
78 
79 /* Returns velocity normalized by thermal velocity
80  * @param[in] mass species mass
81  * @param[in] temp_ev reference temperature
82  * @param[in] v input velocity
83  * return normalized velocity
84  */
85 KOKKOS_INLINE_FUNCTION double normalize_to_vth(double mass, double temp_ev, double v){
86  return v*sqrt(mass/(temp_ev*EV_2_J));
87 }
88 
89 /* Returns normalized parallel velocity
90  * @param[in] c_m species charge over mass
91  * @param[in] mass species mass
92  * @param[in] B magnetic field magnitude
93  * @param[in] temp_ev reference temperature
94  * @param[in] rho gyroradius
95  * return normalized parallel velocity
96  */
97 KOKKOS_INLINE_FUNCTION double normalized_v_para(double c_m, double mass, double B, double temp_ev, double rho){
98  return c_m*rho*B*sqrt(mass/(temp_ev*EV_2_J));
99 }
100 
101 /* Returns normalized sqrt(mu)
102  * @param[in] B magnetic field magnitude
103  * @param[in] temp_ev particle/reference temperature
104  * @param[out] mu mu
105  * return normalized sqrt(mu)
106 */
107 KOKKOS_INLINE_FUNCTION double normalized_sqrt_mu(double B, double temp_ev, double mu){
108  return sqrt(mu*2.0*B/(temp_ev*EV_2_J));
109 }
110 
111 // Convert from {rho, mu} to {energy, pitch}
112 KOKKOS_INLINE_FUNCTION void rho_mu_to_en_pitch(double B, double c_m, double c2_2m, double mass, double rho, double mu, double& en, double& pitch){
113  en = mu*B + c2_2m*(rho*rho*B*B);
114  double v_para = c_m*rho*B;
115  double v_perp2 = 2.0*mu*B/mass;
116  pitch=v_para/sqrt(v_para*v_para + v_perp2);
117 }
118 
119 // Convert from {energy, pitch} to {rho, mu}
120 KOKKOS_INLINE_FUNCTION void en_pitch_to_rho_mu(double B, double c_m, double mass, double en, double pitch, double& rho, double& mu){
121  rho = pitch*sqrt(2.0*en/mass)/(B*c_m);
122  mu = max(0.0,(1.0-pitch*pitch)*en/B);
123 }
124 
125 KOKKOS_INLINE_FUNCTION void rz_2_psitheta(double dpsidr, double dpsidz, double x_r, double x_z, double& x_rad, double& x_theta){
126  x_rad = dpsidr * x_r + dpsidz * x_z;
127  x_theta = -dpsidz * x_r + dpsidr * x_z;
128 }
129 
130 KOKKOS_INLINE_FUNCTION void rz_2_psitheta(const Simd<double>& dpsidr, const Simd<double>& dpsidz, SimdVector2D& x){
131  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++){
132  double x_rad, x_theta;
133  rz_2_psitheta(dpsidr[i_simd], dpsidz[i_simd], x.r[i_simd], x.z[i_simd], x_rad, x_theta);
134  x.r[i_simd] = x_rad;
135  x.z[i_simd] = x_theta;
136  }
137 }
138 
139 KOKKOS_INLINE_FUNCTION void psitheta_2_rz(double dpsidr, double dpsidz, double x_rad, double x_theta, double& x_r, double& x_z){
140  x_r = dpsidr * x_rad - dpsidz * x_theta;
141  x_z = dpsidz * x_rad + dpsidr * x_theta;
142 }
143 
144 #endif
KOKKOS_INLINE_FUNCTION double normalized_v_para(double c_m, double mass, double B, double temp_ev, double rho)
Definition: basic_physics.hpp:97
constexpr double EV_2_J
Conversion rate ev to J.
Definition: constants.hpp:5
KOKKOS_INLINE_FUNCTION double thermal_velocity(double mass, double temp_ev)
Definition: basic_physics.hpp:75
real(kind=8) function gyro_radius(x, mu, isp)
Definition: poisson_extra.F90:74
KOKKOS_INLINE_FUNCTION void en_pitch_to_rho_mu(double B, double c_m, double mass, double en, double pitch, double &rho, double &mu)
Definition: basic_physics.hpp:120
KOKKOS_INLINE_FUNCTION void rz_2_psitheta(double dpsidr, double dpsidz, double x_r, double x_z, double &x_rad, double &x_theta)
Definition: basic_physics.hpp:125
KOKKOS_INLINE_FUNCTION double maxwellian_dist(double den, double temp, double energy)
Definition: basic_physics.hpp:16
KOKKOS_INLINE_FUNCTION double kinetic_energy(double mass, double v)
Definition: basic_physics.hpp:9
KOKKOS_INLINE_FUNCTION void rho_mu_to_en_pitch(double B, double c_m, double c2_2m, double mass, double rho, double mu, double &en, double &pitch)
Definition: basic_physics.hpp:112
Simd< double > z
Definition: simd.hpp:141
KOKKOS_INLINE_FUNCTION double maxwellian_dist_1d_v(double temp, double energy, double maxe)
Definition: basic_physics.hpp:24
KOKKOS_INLINE_FUNCTION double normalize_to_vth(double mass, double temp_ev, double v)
Definition: basic_physics.hpp:85
KOKKOS_INLINE_FUNCTION void psitheta_2_rz(double dpsidr, double dpsidz, double x_rad, double x_theta, double &x_r, double &x_z)
Definition: basic_physics.hpp:139
KOKKOS_INLINE_FUNCTION double normalized_sqrt_mu(double B, double temp_ev, double mu)
Definition: basic_physics.hpp:107
Simd< double > r
Definition: simd.hpp:140
Definition: simd.hpp:139
KOKKOS_INLINE_FUNCTION double maxwellian_dist_2d_v(double temp, double energy, double maxe)
Definition: basic_physics.hpp:32
KOKKOS_INLINE_FUNCTION double exp_ad(double x)
Definition: basic_physics.hpp:44
const double EXP_LIM
Limit of exponents.
Definition: constants.hpp:14