ASCOT5
Loading...
Searching...
No Matches
mccc_gc_euler.c
Go to the documentation of this file.
1
5#include <math.h>
6#include "../../ascot5.h"
7#include "../../consts.h"
8#include "../../math.h"
9#include "../../physlib.h"
10#include "../../error.h"
11#include "../../particle.h"
12#include "../../B_field.h"
13#include "../../plasma.h"
14#include "../../random.h"
15#include "mccc_coefs.h"
16#include "mccc.h"
17
30 plasma_data* pdata, mccc_data* mdata, real* rnd) {
31
32 /* Get plasma information before going to the SIMD loop */
33 int n_species = plasma_get_n_species(pdata);
34 const real* qb = plasma_get_species_charge(pdata);
35 const real* mb = plasma_get_species_mass(pdata);
36
37 #pragma omp simd
38 for(int i = 0; i < NSIMD; i++) {
39 if(p->running[i]) {
40 a5err errflag = 0;
41
42 /* Initial (R,z) position and magnetic field are needed for later */
43 real Brpz[3] = {p->B_r[i], p->B_phi[i], p->B_z[i]};
44 real Bnorm = math_norm(Brpz);
45 real Bxyz[3];
46 math_vec_rpz2xyz(Brpz, Bxyz, p->phi[i]);
47 real R0 = p->r[i];
48 real z0 = p->z[i];
49
50 /* Move guiding center to (x, y, z, vnorm, xi) coordinates */
51 real vin, pin, vflow, gamma, ppar_flow, xiin, Xin_xyz[3];
52 Xin_xyz[0] = p->r[i] * cos(p->phi[i]);
53 Xin_xyz[1] = p->r[i] * sin(p->phi[i]);
54 Xin_xyz[2] = p->z[i];
55 if(!errflag) {
56 errflag = plasma_eval_flow(
57 &vflow, p->rho[i], p->r[i], p->phi[i], p->z[i], p->time[i],
58 pdata);
59 }
60 gamma = physlib_gamma_ppar(p->mass[i], p->mu[i], p->ppar[i], Bnorm);
61 ppar_flow = p->ppar[i] - gamma * vflow * p->mass[i];
62 pin = physlib_gc_p(p->mass[i], p->mu[i], ppar_flow, Bnorm);
63 xiin = physlib_gc_xi(p->mass[i], p->mu[i], ppar_flow, Bnorm);
64 vin = physlib_vnorm_pnorm(p->mass[i], pin);
65
66 /* Evaluate plasma density and temperature */
68 if(!errflag) {
69 errflag = plasma_eval_densandtemp(nb, Tb, p->rho[i],
70 p->r[i], p->phi[i], p->z[i],
71 p->time[i], pdata);
72 }
73
74 /* Coulomb logarithm */
75 real clogab[MAX_SPECIES];
76 mccc_coefs_clog(clogab, p->mass[i], p->charge[i], vin,
77 n_species, mb, qb, nb, Tb);
78
79 /* Evaluate collision coefficients and sum them for each *
80 * species */
81 real gyrofreq = phys_gyrofreq_pnorm(p->mass[i], p->charge[i],
82 pin, Bnorm);
83 real K = 0, Dpara = 0, nu = 0, DX = 0;
84 for(int j = 0; j < n_species; j++) {
85 real vb = sqrt( 2 * Tb[j] / mb[j] );
86 real x = vin / vb;
87 real mufun[3];
88 mccc_coefs_mufun(mufun, x, mdata); // eq. 2.83 PhD Hirvijoki
89
90 real Qb = mccc_coefs_Q(p->mass[i], p->charge[i], mb[j],
91 qb[j], nb[j], vb, clogab[j],
92 mufun[0]);
93 real Dparab = mccc_coefs_Dpara(p->mass[i], p->charge[i], vin,
94 qb[j], nb[j], vb, clogab[j],
95 mufun[0]);
96 real Dperpb = mccc_coefs_Dperp(p->mass[i], p->charge[i], vin,
97 qb[j], nb[j], vb, clogab[j],
98 mufun[1]);
99 real dDparab = mccc_coefs_dDpara(p->mass[i], p->charge[i], vin,
100 qb[j], nb[j], vb, clogab[j],
101 mufun[0], mufun[2]);
102
103 K += mccc_coefs_K(vin, Dparab, dDparab, Qb);
104 Dpara += Dparab;
105 nu += mccc_coefs_nu(vin, Dperpb); // eq.41
106 DX += mccc_coefs_DX(xiin, Dparab, Dperpb, gyrofreq);
107 }
108
109 /* Evaluate collisions */
110 real sdt = sqrt(h[i]);
111 real dW[5];
112 dW[0]=sdt*rnd[0*NSIMD + i]; // For X_1
113 dW[1]=sdt*rnd[1*NSIMD + i]; // For X_2
114 dW[2]=sdt*rnd[2*NSIMD + i]; // For X_3
115 dW[3]=sdt*rnd[3*NSIMD + i]; // For v
116 dW[4]=sdt*rnd[4*NSIMD + i]; // For xi
117
118 real bhat[3];
119 math_unit(Bxyz, bhat);
120
121 real k1 = sqrt(2*DX);
122 real k2 = math_dot(bhat, dW);
123
124 real vout, xiout, Xout_xyz[3];
125 Xout_xyz[0] = Xin_xyz[0] + k1 * ( dW[0] - k2 * bhat[0] );
126 Xout_xyz[1] = Xin_xyz[1] + k1 * ( dW[1] - k2 * bhat[1] );
127 Xout_xyz[2] = Xin_xyz[2] + k1 * ( dW[2] - k2 * bhat[2] );
128 vout = vin + K*h[i] + sqrt( 2 * Dpara ) * dW[3];
129 xiout = xiin - xiin*nu*h[i] + sqrt(( 1 - xiin*xiin ) * nu) * dW[4];
130
131 /* Enforce boundary conditions */
132 real cutoff = MCCC_CUTOFF * sqrt( Tb[0] / p->mass[i] );
133 if(vout < cutoff){
134 vout = 2 * cutoff - vout;
135 }
136
137 if(fabs(xiout) > 1){
138 xiout = ( (xiout > 0) - (xiout < 0) )
139 * ( 2 - fabs( xiout ) );
140 }
141
142 /* Remove energy or pitch change or spatial diffusion from the *
143 * results if that is requested */
144 if(!mdata->include_energy) {
145 vout = vin;
146 }
147 if(!mdata->include_pitch) {
148 xiout = xiin;
149 }
150 if(!mdata->include_gcdiff) {
151 Xout_xyz[0] = Xin_xyz[0];
152 Xout_xyz[1] = Xin_xyz[1];
153 Xout_xyz[2] = Xin_xyz[2];
154 }
155 real pout = physlib_pnorm_vnorm(p->mass[i], vout);
156
157 /* Back to cylindrical coordinates */
158 real Xout_rpz[3];
159 math_xyz2rpz(Xout_xyz, Xout_rpz);
160
161 /* Evaluate magnetic field (and gradient) and rho at new position */
162 real B_dB[15], psi[1], rho[2];
163 if(!errflag) {
164 errflag = B_field_eval_B_dB(B_dB, Xout_rpz[0], Xout_rpz[1],
165 Xout_rpz[2], p->time[i] + h[i],
166 Bdata);
167 }
168 if(!errflag) {
169 errflag = B_field_eval_psi(psi, Xout_rpz[0], Xout_rpz[1],
170 Xout_rpz[2], p->time[i] + h[i],
171 Bdata);
172 }
173 if(!errflag) {
174 errflag = B_field_eval_rho(rho, psi[0], Bdata);
175 }
176
177 if(!errflag) {
178 /* Update marker coordinates at the new position */
179 p->B_r[i] = B_dB[0];
180 p->B_r_dr[i] = B_dB[1];
181 p->B_r_dphi[i] = B_dB[2];
182 p->B_r_dz[i] = B_dB[3];
183
184 p->B_phi[i] = B_dB[4];
185 p->B_phi_dr[i] = B_dB[5];
186 p->B_phi_dphi[i] = B_dB[6];
187 p->B_phi_dz[i] = B_dB[7];
188
189 p->B_z[i] = B_dB[8];
190 p->B_z_dr[i] = B_dB[9];
191 p->B_z_dphi[i] = B_dB[10];
192 p->B_z_dz[i] = B_dB[11];
193
194 p->rho[i] = rho[0];
195
196 Bnorm = math_normc(B_dB[0], B_dB[4], B_dB[8]);
197
198 p->r[i] = Xout_rpz[0];
199 p->z[i] = Xout_rpz[2];
200
201 /*Since we use xiout (in "flow frame") here, we will get the mu
202 in the "flow frame". If we assume that the flow frame has the
203 same perpendicular velocity, mu remains unchanged under the
204 co-ordinate transformation and thus this mu is then the same as
205 the mu in the lab frame.*/
206 p->mu[i] = physlib_gc_mu(p->mass[i], pout, xiout, Bnorm);
207 gamma = physlib_gamma_pnorm(p->mass[i], pout);
208
209 /* difference in ppar between the lab frame and "flow frame" */
210 real dppar = gamma * p->mass[i] * vflow;
211
212 /* p->ppar is in the lab frame; xiout and pout are in the
213 "flow frame" */
214 p->ppar[i] = physlib_gc_ppar(pout, xiout) + dppar;
215
216 /* Evaluate phi and theta angles so that they are cumulative */
217 real axisrz[2];
218 errflag = B_field_get_axis_rz(axisrz, Bdata, p->phi[i]);
219 p->theta[i] += atan2( (R0-axisrz[0]) * (p->z[i]-axisrz[1])
220 - (z0-axisrz[1]) * (p->r[i]-axisrz[0]),
221 (R0-axisrz[0]) * (p->r[i]-axisrz[0])
222 + (z0-axisrz[1]) * (p->z[i]-axisrz[1]) );
223 p->phi[i] += atan2( Xin_xyz[0] * Xout_xyz[1]
224 - Xin_xyz[1] * Xout_xyz[0],
225 Xin_xyz[0] * Xout_xyz[0]
226 + Xin_xyz[1] * Xout_xyz[1] );
227 }
228
229 /* Error handling */
230 if(errflag) {
231 p->err[i] = errflag;
232 p->running[i] = 0;
233 }
234 }
235 }
236}
a5err B_field_eval_rho(real rho[2], real psi, B_field_data *Bdata)
Evaluate normalized poloidal flux rho and its psi derivative.
Definition B_field.c:228
a5err B_field_eval_psi(real *psi, real r, real phi, real z, real t, B_field_data *Bdata)
Evaluate poloidal flux psi.
Definition B_field.c:102
a5err B_field_eval_B_dB(real B_dB[15], real r, real phi, real z, real t, B_field_data *Bdata)
Evaluate magnetic field and its derivatives.
Definition B_field.c:449
a5err B_field_get_axis_rz(real rz[2], B_field_data *Bdata, real phi)
Return magnetic axis Rz-coordinates.
Definition B_field.c:501
Header file for B_field.c.
Main header file for ASCOT5.
double real
Definition ascot5.h:85
#define NSIMD
Number of particles simulated simultaneously in a particle group operations.
Definition ascot5.h:91
#define MAX_SPECIES
Maximum number of plasma species.
Definition ascot5.h:95
Header file containing physical and mathematical constants.
Error module for ASCOT5.
unsigned long int a5err
Simulation error flag.
Definition error.h:17
Header file for math.c.
#define math_dot(a, b)
Calculate dot product a[3] dot b[3].
Definition math.h:31
#define math_unit(a, b)
Calculate unit vector b from a 3D vector a.
Definition math.h:73
#define math_xyz2rpz(xyz, rpz)
Convert cartesian coordinates xyz to cylindrical coordinates rpz.
Definition math.h:77
#define math_vec_rpz2xyz(vrpz, vxyz, phi)
Transform vector from cylindrical to cartesian basis: vrpz -> vxyz, phi is the toroidal angle in radi...
Definition math.h:86
#define math_normc(a1, a2, a3)
Calculate norm of 3D vector from its components a1, a2, a3.
Definition math.h:70
#define math_norm(a)
Calculate norm of 3D vector a.
Definition math.h:67
Header file for mccc package.
#define MCCC_CUTOFF
Defines minimum energy boundary condition.
Definition mccc.h:22
Routines to evaluate coefficients needed to evaluate collisions.
#define mccc_coefs_Dpara(ma, qa, va, qb, nb, vb, clogab, mu0)
Evaluate non-relativistic parallel diffusion coefficient [m^2/s^3].
Definition mccc_coefs.h:103
#define mccc_coefs_dDpara(ma, qa, va, qb, nb, vb, clogab, mu0, dmu0)
Evaluate derivative of non-relativistic parallel diffusion coefficient [m/s^2].
Definition mccc_coefs.h:126
#define mccc_coefs_K(va, Dpara, dDpara, Q)
Evaluate guiding center drag coefficient [m/s^2].
Definition mccc_coefs.h:167
static void mccc_coefs_mufun(real mufun[3], real x, mccc_data *mdata)
Evaluate special functions needed by collision coefficients.
Definition mccc_coefs.h:275
#define mccc_coefs_Dperp(ma, qa, va, qb, nb, vb, clogab, mu1)
Evaluate non-relativistic perpendicular diffusion coefficient [m^2/s^3].
Definition mccc_coefs.h:150
#define mccc_coefs_nu(va, Dperp)
Evaluate pitch collision frequency [1/s].
Definition mccc_coefs.h:180
#define mccc_coefs_DX(xi, Dpara, Dperp, gyrofreq)
Evaluate spatial diffusion coefficient [m^2/s].
Definition mccc_coefs.h:195
static DECLARE_TARGET_END void mccc_coefs_clog(real *clogab, real ma, real qa, real va, int nspec, const real *mb, const real *qb, const real *nb, const real *Tb)
Evaluate Coulomb logarithm.
Definition mccc_coefs.h:228
#define mccc_coefs_Q(ma, qa, mb, qb, nb, vb, clogab, mu0)
Evaluate non-relativistic drag coefficient [m/s^2].
Definition mccc_coefs.h:43
void mccc_gc_euler(particle_simd_gc *p, real *h, B_field_data *Bdata, plasma_data *pdata, mccc_data *mdata, real *rnd)
Integrate collisions for one time-step.
Header file for particle.c.
Methods to evaluate elementary physical quantities.
#define physlib_gamma_pnorm(m, p)
Evaluate Lorentz factor from momentum norm.
Definition physlib.h:46
#define physlib_gc_xi(m, mu, ppar, B)
Evaluate guiding center pitch from parallel momentum and magnetic moment.
Definition physlib.h:214
#define physlib_pnorm_vnorm(m, v)
Evaluate momentum norm [kg m/s] from velocity norm.
Definition physlib.h:154
#define physlib_gamma_ppar(m, mu, ppar, B)
Evaluate Lorentz factor from parallel momentum.
Definition physlib.h:77
#define physlib_vnorm_pnorm(m, p)
Evaluate velocity norm [m/s] from momentum norm.
Definition physlib.h:141
#define phys_gyrofreq_pnorm(m, q, p, B)
Evaluate gyrofrequency [rad/s] from momentum norm.
Definition physlib.h:261
#define physlib_gc_ppar(p, xi)
Evaluate guiding center parallel momentum [kg m/s] from momentum norm and pitch.
Definition physlib.h:167
#define physlib_gc_p(m, mu, ppar, B)
Evaluate guiding center momentum norm [kg m/s] from parallel momentum and magnetic moment.
Definition physlib.h:198
#define physlib_gc_mu(m, p, xi, B)
Evaluate guiding center magnetic moment [J/T] from momentum norm and pitch.
Definition physlib.h:182
const real * plasma_get_species_mass(plasma_data *pls_data)
Get mass of all plasma species.
Definition plasma.c:314
int plasma_get_n_species(plasma_data *pls_data)
Get the number of plasma species.
Definition plasma.c:284
a5err plasma_eval_flow(real *vflow, real rho, real r, real phi, real z, real t, plasma_data *pls_data)
Evalate plasma flow along the field lines.
Definition plasma.c:236
const real * plasma_get_species_charge(plasma_data *pls_data)
Get charge of all plasma species.
Definition plasma.c:344
a5err plasma_eval_densandtemp(real *dens, real *temp, real rho, real r, real phi, real z, real t, plasma_data *pls_data)
Evaluate plasma density and temperature for all species.
Definition plasma.c:186
Header file for plasma.c.
Header file for random.c.
Magnetic field simulation data.
Definition B_field.h:41
Parameters and data required to evaluate Coulomb collisions.
Definition mccc.h:27
int include_pitch
Definition mccc.h:30
int include_gcdiff
Definition mccc.h:31
int include_energy
Definition mccc.h:29
Struct representing NSIMD guiding center markers.
Definition particle.h:275
Plasma simulation data.
Definition plasma.h:32