forked from ascot4fusion/ascot5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
physlib.h
255 lines (235 loc) · 6.54 KB
/
physlib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* @file physlib.h
* @brief Methods to evaluate elementary physical quantities
*/
#ifndef PHYSLIB_H
#define PHYSLIB_H
#include <math.h>
#include "math.h"
#include "consts.h"
/**
* @brief Evaluate Lorentz factor from velocity norm
*
* \f$ \gamma = \sqrt{\frac{1}{1-v^2/c^2}}\f$
*
* where
*
* - \f$v\f$ is velocity norm [m/s]
*/
#define physlib_gamma_vnorm(v) ( \
sqrt( 1.0 / ( (1.0 - v / CONST_C) * (1.0 + v / CONST_C) ) ) )
/**
* @brief Evaluate Lorentz factor from momentum norm
*
* \f$\gamma = \sqrt{1 + \left(\frac{p}{mc}\right)^2}\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$p\f$ is momentum norm [kg m/s]
*/
#define physlib_gamma_pnorm(m, p) ( \
sqrt(1.0 + ( p * p ) / ( m * m * CONST_C2 ) ) )
/**
* @brief Evaluate Lorentz factor from parallel velocity
*
* \f$\gamma = \sqrt{\frac{1 + (2\mu B/mc^2)}{1 - v_\parallel^2/c^2}}\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$\mu\f$ is magnetic moment [J/T]
* - \f$v_\parallel\f$ is parallel velocity [m/s]
* - \f$B\f$ is magnetic field norm [T]
*/
#define physlib_gamma_vpar(m, mu, vpar, B) ( \
sqrt( ( 1.0 + (2.0 * mu * B) / ( m * CONST_C2 ) ) / \
( (1.0 - vpar / CONST_C) * (1.0 + vpar / CONST_C) ) ) )
/**
* @brief Evaluate Lorentz factor from parallel momentum
*
* \f$\gamma = \sqrt{1 + 2\mu B/mc^2 + (p_\parallel/mc)^2}\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$\mu\f$ is magnetic moment [J/T]
* - \f$p_\parallel\f$ is parallel momentum [kg m/s]
* - \f$B\f$ is magnetic field norm [T]
*/
#define physlib_gamma_ppar(m, mu, ppar, B) ( \
sqrt( 1.0 + 2 * mu * B / ( m * CONST_C2 ) + \
ppar * ppar / ( m * m * CONST_C2 ) ) )
/**
* @brief Evaluate kinetic energy [J] from momentum norm
*
* \f$E_\mathrm{kin}=(\gamma(p) - 1) * m c^2\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$p\f$ is momentum norm [kg m/s]
*/
#define physlib_Ekin_pnorm(m, p) ( \
( physlib_gamma_pnorm(m, p) - 1.0 ) * m * CONST_C2 )
/**
* @brief Evaluate kinetic energy [J] from parallel momentum
*
* \f$E_\mathrm{kin}=(\gamma(m, \mu, p_\parallel, B) - 1) * m c^2\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$p_\parallel\f$ is parallel momentum [kg m/s]
*/
#define physlib_Ekin_ppar(m, mu, ppar, B) ( \
( physlib_gamma_ppar(m, mu, ppar, B) - 1.0 ) * m * CONST_C2 )
/**
* @brief Evaluate velocity norm [m/s] from momentum norm
*
* \f$v = p/\gamma(p)m\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$p\f$ is momentum norm [kg m/s]
*/
#define physlib_vnorm_pnorm(m, p) ( \
p / sqrt(m * m + ( p * p ) / CONST_C2 ) )
/**
* @brief Evaluate momentum norm [kg m/s] from velocity norm
*
* \f$p = \gamma(v)mv\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$v\f$ is velocity norm [m/s]
*/
#define physlib_pnorm_vnorm(m, v) ( \
m * v * physlib_gamma_vnorm(v) )
/**
* @brief Evaluate guiding center parallel momentum [kg m/s] from momentum norm and pitch
*
* \f$p_\parallel = \xi p\f$
*
* where
*
* - \f$p\f$ is momentum norm [kg m/s]
* - \f$\xi\f$ is pitch
*/
#define physlib_gc_ppar(p, xi) ( p * xi )
/**
* @brief Evaluate guiding center magnetic moment [J/T] from momentum norm and pitch
*
* \f$\mu = (1-\xi^2)p/(2mB)\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$p\f$ is momentum norm [kg m/s]
* - \f$\xi\f$ is pitch
* - \f$B\f$ is magnetic field norm [T]
*/
#define physlib_gc_mu(m, p, xi, B) ( \
p * p * ( 1.0 - xi * xi ) / ( 2 * B * m ) )
/**
* @brief Evaluate guiding center momentum norm [kg m/s] from parallel momentum and magnetic moment
*
* \f$p = \sqrt{\gamma(m,\mu,p_\parallel,B)^2 - 1} m c\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$\mu\f$ is magnetic moment [J/T]
* - \f$p_\parallel\f$ is parallel momentum [kg m/s]
* - \f$B\f$ is magnetic field norm [T]
*/
#define physlib_gc_p(m, mu, ppar, B) ( \
m * CONST_C * sqrt( pow( physlib_gamma_ppar(m, mu, ppar, B), 2) - 1 ) )
/**
* @brief Evaluate guiding center pitch from parallel momentum and magnetic moment
*
* \f$\xi = p_\parallel / p(m,\mu,p_\parallel,B) \f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$\mu\f$ is magnetic moment [J/T]
* - \f$p_\parallel\f$ is parallel momentum [kg m/s]
* - \f$B\f$ is magnetic field [T]
*/
#define physlib_gc_xi(m, mu, ppar, B) ( \
ppar / physlib_gc_p(m, mu, ppar, B) )
/**
* @brief Evaluate gyroradius [m] from momentum vector
*
* \f$\rho_g = \frac{\mathbf{p}\cdot\mathbf{B}}{|q|B^2}\f$
*
* where
*
* - \f$q\f$ is charge [C]
* - \f$\mathbf{v}\f$ is momentum vector [kg m/s]
* - \f$\mathbf{B}\f$ is magnetic field vector [T]
*/
#define physlib_gyrolength_p(q, p, B) ( \
math_dot(p, B) / ( fabs(q) * math_dot(B, B) ) )
/**
* @brief Evaluate gyroradius [m] from parallel momentum and magnetic moment
*
* \f$\rho_g = \frac{1}{|q|}\sqrt{\frac{2\gamma(m, \mu, p_\parallel, B) m \mu}
* {B}}\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$q\f$ is charge [C]
* - \f$\mu\f$ is magnetic moment [J/T]
* - \f$p_\parallel\f$ is parallel momentum [kg m/s]
* - \f$B\f$ is magnetic field norm [T]
*/
#define phys_gyrolength_ppar(m, q, mu, ppar, B) ( \
sqrt( 2 * m * mu * \
physlib_gamma_ppar(m, mu, ppar, B) / B ) / fabs(q) )
/**
* @brief Evaluate gyrofrequency [rad/s] from momentum norm
*
* \f$\omega_g = \frac{q B}{\gamma(p) m}\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$q\f$ is charge [C]
* - \f$p\f$ is momentum norm [kg m/s]
* - \f$B\f$ is magnetic field norm [T]
*/
#define phys_gyrofreq_pnorm(m, q, p, B) ( \
fabs(q) * B / ( m * physlib_gamma_pnorm(m, p) ) )
/**
* @brief Evaluate gyrofrequency [rad/s] from parallel momentum and magnetic moment
*
* \f$\omega_g = \frac{q B}{\gamma(m, \mu, p_\parallel, B) m}\f$
*
* where
*
* - \f$m\f$ is mass [kg]
* - \f$q\f$ is charge [C]
* - \f$\mu\f$ is magnetic moment [J/T]
* - \f$p_\parallel\f$ is parallel momentum [kg m/s]
* - \f$B\f$ is magnetic field norm [T]
*/
#define phys_gyrofreq_ppar(m, q, mu, ppar, B) ( \
fabs(q) * B / ( m * physlib_gamma_ppar(m, mu, ppar, B) ) )
/**
* @brief Evaluate toroidal canonical momentum for particle
*
*/
#define phys_ptoroid_fo(q, R, pphi, psi) ( \
R * pphi + q * psi )
/**
* @brief Evaluate toroidal canonical momentum for guiding center
*
*/
#define phys_ptoroid_gc(q, R, ppar, psi, B, Bphi) ( \
ppar * R * (Bphi / B) + q * psi )
#endif