ASCOT5
Loading...
Searching...
No Matches
dist_rho5D.c
Go to the documentation of this file.
1
5#include <stdio.h>
6#include <stdlib.h>
7#include <math.h>
8#include "../ascot5.h"
9#include "../consts.h"
10#include "../physlib.h"
11#include "dist_rho5D.h"
12#include "../particle.h"
13
17size_t dist_rho5D_index(int i_rho, int i_theta, int i_phi, int i_ppara,
18 int i_pperp, int i_time, int i_q, size_t step_6,
19 size_t step_5, size_t step_4, size_t step_3,
20 size_t step_2, size_t step_1) {
21 return (size_t)(i_rho) * step_6
22 + (size_t)(i_theta) * step_5
23 + (size_t)(i_phi) * step_4
24 + (size_t)(i_ppara) * step_3
25 + (size_t)(i_pperp) * step_2
26 + (size_t)(i_time) * step_1
27 + (size_t)(i_q);
28}
29
36 offload_data->n_rho = 0;
37 offload_data->min_rho = 0;
38 offload_data->max_rho = 0;
39 offload_data->n_theta = 0;
40 offload_data->min_theta = 0;
41 offload_data->max_theta = 0;
42 offload_data->n_phi = 0;
43 offload_data->min_phi = 0;
44 offload_data->max_phi = 0;
45 offload_data->n_ppara = 0;
46 offload_data->min_ppara = 0;
47 offload_data->max_ppara = 0;
48 offload_data->n_pperp = 0;
49 offload_data->min_pperp = 0;
50 offload_data->max_pperp = 0;
51}
52
61 dist_rho5D_offload_data* offload_data,
62 real* offload_array) {
63 dist_data->n_rho = offload_data->n_rho;
64 dist_data->min_rho = offload_data->min_rho;
65 dist_data->max_rho = offload_data->max_rho;
66
67 dist_data->n_theta = offload_data->n_theta;
68 dist_data->min_theta = offload_data->min_theta;
69 dist_data->max_theta = offload_data->max_theta;
70
71 dist_data->n_phi = offload_data->n_phi;
72 dist_data->min_phi = offload_data->min_phi;
73 dist_data->max_phi = offload_data->max_phi;
74
75 dist_data->n_ppara = offload_data->n_ppara;
76 dist_data->min_ppara = offload_data->min_ppara;
77 dist_data->max_ppara = offload_data->max_ppara;
78
79 dist_data->n_pperp = offload_data->n_pperp;
80 dist_data->min_pperp = offload_data->min_pperp;
81 dist_data->max_pperp = offload_data->max_pperp;
82
83 dist_data->n_time = offload_data->n_time;
84 dist_data->min_time = offload_data->min_time;
85 dist_data->max_time = offload_data->max_time;
86
87 dist_data->n_q = offload_data->n_q;
88 dist_data->min_q = offload_data->min_q;
89 dist_data->max_q = offload_data->max_q;
90
91 size_t n_q = (size_t)(dist_data->n_q);
92 size_t n_time = (size_t)(dist_data->n_time);
93 size_t n_pperp = (size_t)(dist_data->n_pperp);
94 size_t n_ppara = (size_t)(dist_data->n_ppara);
95 size_t n_phi = (size_t)(dist_data->n_phi);
96 size_t n_theta = (size_t)(dist_data->n_theta);
97 dist_data->step_6 = n_q * n_time * n_pperp * n_ppara * n_phi * n_theta;
98 dist_data->step_5 = n_q * n_time * n_pperp * n_ppara * n_phi;
99 dist_data->step_4 = n_q * n_time * n_pperp * n_ppara;
100 dist_data->step_3 = n_q * n_time * n_pperp;
101 dist_data->step_2 = n_q * n_time;
102 dist_data->step_1 = n_q;
103
104 dist_data->histogram = &offload_array[0];
105}
106
119 particle_simd_fo* p_i) {
120
121 GPU_PARALLEL_LOOP_ALL_LEVELS
122 for(int i = 0; i < p_f->n_mrk; i++) {
123 if(p_f->running[i]) {
124
125 int i_rho = floor((p_f->rho[i] - dist->min_rho)
126 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
127
128 real phi = fmod(p_f->phi[i], 2*CONST_PI);
129 if(phi < 0) {
130 phi += 2*CONST_PI;
131 }
132 int i_phi = floor((phi - dist->min_phi)
133 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
134
135 real theta = fmod(p_f->theta[i], 2*CONST_PI);
136 if(theta < 0) {
137 theta += 2*CONST_PI;
138 }
139 int i_theta = floor((theta - dist->min_theta)
140 / ( (dist->max_theta - dist->min_theta)
141 / dist->n_theta) );
142
143 real ppara = ( p_f->p_r[i] * p_f->B_r[i]
144 + p_f->p_phi[i] * p_f->B_phi[i]
145 + p_f->p_z[i] * p_f->B_z[i])
146 / sqrt( p_f->B_r[i] * p_f->B_r[i]
147 + p_f->B_phi[i]* p_f->B_phi[i]
148 + p_f->B_z[i] * p_f->B_z[i]);
149 int i_ppara = floor((ppara - dist->min_ppara)
150 / ((dist->max_ppara - dist->min_ppara)
151 / dist->n_ppara));
152
153 real pperp = sqrt(
154 p_f->p_r[i] * p_f->p_r[i]
155 + p_f->p_phi[i] * p_f->p_phi[i]
156 + p_f->p_z[i] * p_f->p_z[i]
157 - ppara * ppara);
158 int i_pperp = floor((pperp - dist->min_pperp)
159 / ((dist->max_pperp - dist->min_pperp)
160 / dist->n_pperp));
161
162 int i_time = floor((p_f->time[i] - dist->min_time)
163 / ((dist->max_time - dist->min_time) / dist->n_time));
164
165 int i_q = floor((p_f->charge[i]/CONST_E - dist->min_q)
166 / ((dist->max_q - dist->min_q) / dist->n_q));
167
168 if(i_rho >= 0 && i_rho <= dist->n_rho - 1 &&
169 i_phi >= 0 && i_phi <= dist->n_phi - 1 &&
170 i_theta >= 0 && i_theta <= dist->n_theta - 1 &&
171 i_ppara >= 0 && i_ppara <= dist->n_ppara - 1 &&
172 i_pperp >= 0 && i_pperp <= dist->n_pperp - 1 &&
173 i_time >= 0 && i_time <= dist->n_time - 1 &&
174 i_q >= 0 && i_q <= dist->n_q - 1 ) {
175 real weight = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
176 size_t index = dist_rho5D_index(
177 i_rho, i_theta, i_phi, i_ppara, i_pperp,
178 i_time, i_q, dist->step_6, dist->step_5, dist->step_4,
179 dist->step_3, dist->step_2, dist->step_1);
180 GPU_ATOMIC
181 dist->histogram[index] += weight;
182 }
183 }
184 }
185}
186
199 particle_simd_gc* p_i) {
200 real phi[NSIMD];
201 real theta[NSIMD];
202 real pperp[NSIMD];
203
204 int i_rho[NSIMD];
205 int i_phi[NSIMD];
206 int i_theta[NSIMD];
207 int i_ppara[NSIMD];
208 int i_pperp[NSIMD];
209 int i_time[NSIMD];
210 int i_q[NSIMD];
211
212 int ok[NSIMD];
213 real weight[NSIMD];
214
215 #pragma omp simd
216 for(int i = 0; i < NSIMD; i++) {
217 if(p_f->running[i]) {
218
219 i_rho[i] = floor((p_f->rho[i] - dist->min_rho)
220 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
221
222 phi[i] = fmod(p_f->phi[i], 2*CONST_PI);
223 if(phi[i] < 0) {
224 phi[i] = phi[i] + 2*CONST_PI;
225 }
226 i_phi[i] = floor((phi[i] - dist->min_phi)
227 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
228
229 theta[i] = fmod(p_f->theta[i], 2*CONST_PI);
230 if(theta[i] < 0) {
231 theta[i] = theta[i] + 2*CONST_PI;
232 }
233 i_theta[i] = floor((theta[i] - dist->min_theta)
234 / ((dist->max_theta - dist->min_theta)
235 / dist->n_theta));
236
237 i_ppara[i] = floor((p_f->ppar[i] - dist->min_ppara)
238 / ((dist->max_ppara - dist->min_ppara) / dist->n_ppara));
239
240 pperp[i] = sqrt(2 * sqrt( p_f->B_r[i] * p_f->B_r[i]
241 + p_f->B_phi[i] * p_f->B_phi[i]
242 + p_f->B_z[i] * p_f->B_z[i] )
243 * p_f->mu[i] * p_f->mass[i]);
244 i_pperp[i] = floor((pperp[i] - dist->min_pperp)
245 / ((dist->max_pperp - dist->min_pperp)
246 / dist->n_pperp));
247
248 i_time[i] = floor((p_f->time[i] - dist->min_time)
249 / ((dist->max_time - dist->min_time) / dist->n_time));
250
251 i_q[i] = floor((p_f->charge[i]/CONST_E - dist->min_q)
252 / ((dist->max_q - dist->min_q) / dist->n_q));
253
254 if(i_rho[i] >= 0 && i_rho[i] <= dist->n_rho - 1 &&
255 i_phi[i] >= 0 && i_phi[i] <= dist->n_phi - 1 &&
256 i_theta[i] >= 0 && i_theta[i] <= dist->n_theta - 1 &&
257 i_ppara[i] >= 0 && i_ppara[i] <= dist->n_ppara - 1 &&
258 i_pperp[i] >= 0 && i_pperp[i] <= dist->n_pperp - 1 &&
259 i_time[i] >= 0 && i_time[i] <= dist->n_time - 1 &&
260 i_q[i] >= 0 && i_q[i] <= dist->n_q - 1 ) {
261 ok[i] = 1;
262 weight[i] = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
263 }
264 else {
265 ok[i] = 0;
266 }
267 }
268 }
269
270 for(int i = 0; i < NSIMD; i++) {
271 if(p_f->running[i] && ok[i]) {
272 size_t index = dist_rho5D_index(
273 i_rho[i], i_theta[i], i_phi[i], i_ppara[i], i_pperp[i],
274 i_time[i], i_q[i], dist->step_6, dist->step_5, dist->step_4,
275 dist->step_3, dist->step_2, dist->step_1);
276 #pragma omp atomic
277 dist->histogram[index] += weight[i];
278 }
279 }
280}
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
Header file containing physical and mathematical constants.
#define CONST_PI
pi
Definition consts.h:11
#define CONST_E
Elementary charge [C]
Definition consts.h:32
void dist_rho5D_free_offload(dist_rho5D_offload_data *offload_data)
Frees the offload data.
Definition dist_rho5D.c:35
void dist_rho5D_update_gc(dist_rho5D_data *dist, particle_simd_gc *p_f, particle_simd_gc *p_i)
Update the histogram from guiding center markers.
Definition dist_rho5D.c:198
void dist_rho5D_init(dist_rho5D_data *dist_data, dist_rho5D_offload_data *offload_data, real *offload_array)
Initializes distribution from offload data.
Definition dist_rho5D.c:60
size_t dist_rho5D_index(int i_rho, int i_theta, int i_phi, int i_ppara, int i_pperp, int i_time, int i_q, size_t step_6, size_t step_5, size_t step_4, size_t step_3, size_t step_2, size_t step_1)
Internal function calculating the index in the histogram array.
Definition dist_rho5D.c:17
void dist_rho5D_update_fo(dist_rho5D_data *dist, particle_simd_fo *p_f, particle_simd_fo *p_i)
Update the histogram from full-orbit particles.
Definition dist_rho5D.c:118
Header file for dist_rho5D.c.
real fmod(real x, real y)
Compute the modulus of two real numbers.
Definition math.c:22
Header file for math.c.
Header file for particle.c.
Methods to evaluate elementary physical quantities.
Histogram parameters on target.
Definition dist_rho5D.h:48
Histogram parameters that will be offloaded to target.
Definition dist_rho5D.h:15
Struct representing NSIMD particle markers.
Definition particle.h:210
integer * running
Definition particle.h:252
Struct representing NSIMD guiding center markers.
Definition particle.h:275