ASCOT5
Loading...
Searching...
No Matches
dist_rho6D.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 "../gctransform.h"
12#include "dist_rho6D.h"
13
17size_t dist_rho6D_index(int i_rho, int i_theta, int i_phi, int i_pr, int i_pphi,
18 int i_pz, int i_time, int i_q, size_t step_7,
19 size_t step_6, size_t step_5, size_t step_4,
20 size_t step_3, size_t step_2, size_t step_1) {
21 return (size_t)(i_rho) * step_7
22 + (size_t)(i_theta) * step_6
23 + (size_t)(i_phi) * step_5
24 + (size_t)(i_pr) * step_4
25 + (size_t)(i_pphi) * step_3
26 + (size_t)(i_pz) * step_2
27 + (size_t)(i_time) * step_1
28 + (size_t)(i_q);
29}
30
35 offload_data->n_rho = 0;
36 offload_data->min_rho = 0;
37 offload_data->max_rho = 0;
38 offload_data->n_theta = 0;
39 offload_data->min_theta = 0;
40 offload_data->max_theta = 0;
41 offload_data->n_phi = 0;
42 offload_data->min_phi = 0;
43 offload_data->max_phi = 0;
44 offload_data->n_pr = 0;
45 offload_data->min_pr = 0;
46 offload_data->max_pr = 0;
47 offload_data->n_pphi = 0;
48 offload_data->min_pphi = 0;
49 offload_data->max_pphi = 0;
50 offload_data->n_pz = 0;
51 offload_data->min_pz = 0;
52 offload_data->max_pz = 0;
53}
54
59 dist_rho6D_offload_data* offload_data,
60 real* offload_array) {
61 dist_data->n_rho = offload_data->n_rho;
62 dist_data->min_rho = offload_data->min_rho;
63 dist_data->max_rho = offload_data->max_rho;
64
65 dist_data->n_theta = offload_data->n_theta;
66 dist_data->min_theta = offload_data->min_theta;
67 dist_data->max_theta = offload_data->max_theta;
68
69 dist_data->n_phi = offload_data->n_phi;
70 dist_data->min_phi = offload_data->min_phi;
71 dist_data->max_phi = offload_data->max_phi;
72
73 dist_data->n_pr = offload_data->n_pr;
74 dist_data->min_pr = offload_data->min_pr;
75 dist_data->max_pr = offload_data->max_pr;
76
77 dist_data->n_pphi = offload_data->n_pphi;
78 dist_data->min_pphi = offload_data->min_pphi;
79 dist_data->max_pphi = offload_data->max_pphi;
80
81 dist_data->n_pz = offload_data->n_pz;
82 dist_data->min_pz = offload_data->min_pz;
83 dist_data->max_pz = offload_data->max_pz;
84
85 dist_data->n_time = offload_data->n_time;
86 dist_data->min_time = offload_data->min_time;
87 dist_data->max_time = offload_data->max_time;
88
89 dist_data->n_q = offload_data->n_q;
90 dist_data->min_q = offload_data->min_q;
91 dist_data->max_q = offload_data->max_q;
92
93 size_t n_q = (size_t)(dist_data->n_q);
94 size_t n_time = (size_t)(dist_data->n_time);
95 size_t n_pz = (size_t)(dist_data->n_pz);
96 size_t n_pphi = (size_t)(dist_data->n_pphi);
97 size_t n_pr = (size_t)(dist_data->n_pr);
98 size_t n_phi = (size_t)(dist_data->n_phi);
99 size_t n_theta = (size_t)(dist_data->n_theta);
100 dist_data->step_7 = n_q * n_time * n_pz * n_pphi * n_pr * n_phi * n_theta;
101 dist_data->step_6 = n_q * n_time * n_pz * n_pphi * n_pr * n_phi;
102 dist_data->step_5 = n_q * n_time * n_pz * n_pphi * n_pr;
103 dist_data->step_4 = n_q * n_time * n_pz * n_pphi;
104 dist_data->step_3 = n_q * n_time * n_pz;
105 dist_data->step_2 = n_q * n_time;
106 dist_data->step_1 = n_q;
107
108 dist_data->histogram = &offload_array[0];
109}
110
123 particle_simd_fo* p_i) {
124
125 GPU_PARALLEL_LOOP_ALL_LEVELS
126 for(int i = 0; i < p_f->n_mrk; i++) {
127 if(p_f->running[i]) {
128
129 int i_rho = floor((p_f->rho[i] - dist->min_rho)
130 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
131
132 real phi = fmod(p_f->phi[i], 2*CONST_PI);
133 if(phi < 0) {
134 phi = phi + 2*CONST_PI;
135 }
136 int i_phi = floor((phi - dist->min_phi)
137 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
138
139 real theta = fmod(p_f->theta[i], 2*CONST_PI);
140 if(theta < 0) {
141 theta += 2*CONST_PI;
142 }
143 int i_theta = floor((theta - dist->min_theta)
144 / ((dist->max_theta - dist->min_theta)
145 / dist->n_theta));
146
147 int i_pr = floor((p_f->p_r[i] - dist->min_pr)
148 / ((dist->max_pr - dist->min_pr) / dist->n_pr));
149
150 int i_pphi = floor((p_f->p_phi[i] - dist->min_pphi)
151 / ((dist->max_pphi - dist->min_pphi)
152 / dist->n_pphi));
153
154 int i_pz = floor((p_f->p_z[i] - dist->min_pz)
155 / ((dist->max_pz - dist->min_pz) / dist->n_pz));
156
157 int i_time = floor((p_f->time[i] - dist->min_time)
158 / ((dist->max_time - dist->min_time) / dist->n_time));
159
160 int i_q = floor((p_f->charge[i]/CONST_E - dist->min_q)
161 / ((dist->max_q - dist->min_q) / dist->n_q));
162
163 if(i_rho >= 0 && i_rho <= dist->n_rho - 1 &&
164 i_theta >=0 && i_theta <= dist->n_theta -1 &&
165 i_phi >=0 && i_phi <= dist->n_phi - 1 &&
166 i_pr >= 0 && i_pr <= dist->n_pr - 1 &&
167 i_pphi >= 0 && i_pphi <= dist->n_pphi - 1 &&
168 i_pz >= 0 && i_pz <= dist->n_pz - 1 &&
169 i_time >= 0 && i_time <= dist->n_time - 1 &&
170 i_q >= 0 && i_q <= dist->n_q - 1 ) {
171 real weight = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
172 size_t index = dist_rho6D_index(
173 i_rho, i_theta, i_phi, i_pr, i_pphi, i_pz,
174 i_time, i_q, dist->step_7, dist->step_6, dist->step_5,
175 dist->step_4, dist->step_3, dist->step_2, dist->step_1);
176
177 GPU_ATOMIC
178 dist->histogram[index] += weight;
179 }
180 }
181 }
182}
183
196 particle_simd_gc* p_i) {
197 real phi[NSIMD];
198 real theta[NSIMD];
199
200 int i_rho[NSIMD];
201 int i_theta[NSIMD];
202 int i_phi[NSIMD];
203 int i_pr[NSIMD];
204 int i_pphi[NSIMD];
205 int i_pz[NSIMD];
206 int i_time[NSIMD];
207 int i_q[NSIMD];
208
209 int ok[NSIMD];
210 real weight[NSIMD];
211
212 #pragma omp simd
213 for(int i = 0; i < NSIMD; i++) {
214 if(p_f->running[i]) {
215
216 real pr, pphi, pz;
217 real B_dB[12] = {p_f->B_r[i],
218 p_f->B_r_dr[i],
219 p_f->B_r_dphi[i],
220 p_f->B_r_dz[i],
221 p_f->B_phi[i],
222 p_f->B_phi_dr[i],
223 p_f->B_phi_dphi[i],
224 p_f->B_phi_dz[i],
225 p_f->B_z[i],
226 p_f->B_z_dr[i],
227 p_f->B_z_dphi[i],
228 p_f->B_z_dz[i]};
229 gctransform_pparmuzeta2prpphipz(p_f->mass[i], p_f->charge[i], B_dB,
230 p_f->phi[i], p_f->ppar[i],
231 p_f->mu[i], p_f->zeta[i],
232 &pr, &pphi, &pz);
233
234 i_rho[i] = floor((p_f->rho[i] - dist->min_rho)
235 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
236
237 phi[i] = fmod(p_f->phi[i], 2*CONST_PI);
238 if(phi[i] < 0) {
239 phi[i] = phi[i] + 2*CONST_PI;
240 }
241 i_phi[i] = floor((phi[i] - dist->min_phi)
242 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
243
244 theta[i] = fmod(p_f->theta[i], 2*CONST_PI);
245 if(theta[i] < 0) {
246 theta[i] = theta[i] + 2*CONST_PI;
247 }
248 i_theta[i] = floor((theta[i] - dist->min_theta)
249 / ((dist->max_theta - dist->min_theta)
250 / dist->n_theta));
251
252 i_pr[i] = floor((pr - dist->min_pr)
253 / ((dist->max_pr - dist->min_pr) / dist->n_pr));
254
255 i_pphi[i] = floor((pphi - dist->min_pphi)
256 / ((dist->max_pphi - dist->min_pphi)
257 / dist->n_pphi));
258
259 i_pz[i] = floor((pz - dist->min_pz)
260 / ((dist->max_pz - dist->min_pz) / dist->n_pz));
261
262 i_time[i] = floor((p_f->time[i] - dist->min_time)
263 / ((dist->max_time - dist->min_time) / dist->n_time));
264
265 i_q[i] = floor((p_f->charge[i]/CONST_E - dist->min_q)
266 / ((dist->max_q - dist->min_q) / dist->n_q));
267
268 if(i_rho[i] >= 0 && i_rho[i] <= dist->n_rho - 1 &&
269 i_theta[i] >= 0 && i_theta[i] <= dist->n_theta -1 &&
270 i_phi[i] >= 0 && i_phi[i] <= dist->n_phi - 1 &&
271 i_pr[i] >= 0 && i_pr[i] <= dist->n_pr - 1 &&
272 i_pphi[i] >= 0 && i_pphi[i] <= dist->n_pphi - 1 &&
273 i_pz[i] >= 0 && i_pz[i] <= dist->n_pz - 1 &&
274 i_time[i] >= 0 && i_time[i] <= dist->n_time - 1 &&
275 i_q[i] >= 0 && i_q[i] <= dist->n_q - 1 ) {
276 ok[i] = 1;
277 weight[i] = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
278 }
279 else {
280 ok[i] = 0;
281 }
282 }
283 }
284
285 for(int i = 0; i < NSIMD; i++) {
286 if(p_f->running[i] && ok[i]) {
287 size_t index = dist_rho6D_index(
288 i_rho[i], i_theta[i], i_phi[i], i_pr[i], i_pphi[i], i_pz[i],
289 i_time[i], i_q[i], dist->step_7, dist->step_6, dist->step_5,
290 dist->step_4, dist->step_3, dist->step_2, dist->step_1);
291 #pragma omp atomic
292 dist->histogram[index] += weight[i];
293 }
294 }
295}
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_rho6D_update_gc(dist_rho6D_data *dist, particle_simd_gc *p_f, particle_simd_gc *p_i)
Update the histogram from guiding-center particles.
Definition dist_rho6D.c:195
size_t dist_rho6D_index(int i_rho, int i_theta, int i_phi, int i_pr, int i_pphi, int i_pz, int i_time, int i_q, size_t step_7, 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_rho6D.c:17
void dist_rho6D_free_offload(dist_rho6D_offload_data *offload_data)
Frees the offload data.
Definition dist_rho6D.c:34
void dist_rho6D_init(dist_rho6D_data *dist_data, dist_rho6D_offload_data *offload_data, real *offload_array)
Initializes distribution from offload data.
Definition dist_rho6D.c:58
void dist_rho6D_update_fo(dist_rho6D_data *dist, particle_simd_fo *p_f, particle_simd_fo *p_i)
Update the histogram from full-orbit particles.
Definition dist_rho6D.c:122
Header file for dist_rho6D.c.
void gctransform_pparmuzeta2prpphipz(real mass, real charge, real *B_dB, real phi, real ppar, real mu, real zeta, real *pr, real *pphi, real *pz)
Transform particle ppar, mu, and zeta to momentum vector.
Header file for gctransform.c.
real fmod(real x, real y)
Compute the modulus of two real numbers.
Definition math.c:22
Header file for math.c.
Methods to evaluate elementary physical quantities.
Histogram parameters on target.
Definition dist_rho6D.h:52
Histogram parameters that will be offloaded to target.
Definition dist_rho6D.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