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
36 size_t n_q = (size_t)(data->n_q);
37 size_t n_time = (size_t)(data->n_time);
38 size_t n_pz = (size_t)(data->n_pz);
39 size_t n_pphi = (size_t)(data->n_pphi);
40 size_t n_pr = (size_t)(data->n_pr);
41 size_t n_phi = (size_t)(data->n_phi);
42 size_t n_theta = (size_t)(data->n_theta);
43 data->step_7 = n_q * n_time * n_pz * n_pphi * n_pr * n_phi * n_theta;
44 data->step_6 = n_q * n_time * n_pz * n_pphi * n_pr * n_phi;
45 data->step_5 = n_q * n_time * n_pz * n_pphi * n_pr;
46 data->step_4 = n_q * n_time * n_pz * n_pphi;
47 data->step_3 = n_q * n_time * n_pz;
48 data->step_2 = n_q * n_time;
49 data->step_1 = n_q;
50
51 data->histogram = calloc(data->step_7 * (size_t)data->n_rho, sizeof(real));
52 return data->histogram == NULL;
53}
54
59 free(data->histogram);
60}
61
68 GPU_MAP_TO_DEVICE(
69 data->histogram[0:data->n_rho*data->n_theta*data->n_phi*data->n_pr*data->n_pphi*data->n_pz*data->n_time*data->n_q]
70 )
71}
72
79 GPU_UPDATE_FROM_DEVICE(
80 data->histogram[0:data->n_rho*data->n_theta*data->n_phi*data->n_pr*data->n_pphi*data->n_pz*data->n_time*data->n_q]
81 )
82}
83
96 particle_simd_fo* p_i) {
97
98#ifdef GPU
99 size_t index;
100 real weight;
101#else
102 size_t index[NSIMD];
103 real weight[NSIMD];
104 int valid[NSIMD] = {0};
105#endif
106
107 GPU_PARALLEL_LOOP_ALL_LEVELS
108 for(int i = 0; i < p_f->n_mrk; i++) {
109 if(p_f->running[i]) {
110
111 int i_rho = floor((p_f->rho[i] - dist->min_rho)
112 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
113
114 real phi = fmod(p_f->phi[i], 2*CONST_PI);
115 if(phi < 0) {
116 phi = phi + 2*CONST_PI;
117 }
118 int i_phi = floor((phi - dist->min_phi)
119 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
120
121 real theta = fmod(p_f->theta[i], 2*CONST_PI);
122 if(theta < 0) {
123 theta += 2*CONST_PI;
124 }
125 int i_theta = floor((theta - dist->min_theta)
126 / ((dist->max_theta - dist->min_theta)
127 / dist->n_theta));
128
129 int i_pr = floor((p_f->p_r[i] - dist->min_pr)
130 / ((dist->max_pr - dist->min_pr) / dist->n_pr));
131
132 int i_pphi = floor((p_f->p_phi[i] - dist->min_pphi)
133 / ((dist->max_pphi - dist->min_pphi)
134 / dist->n_pphi));
135
136 int i_pz = floor((p_f->p_z[i] - dist->min_pz)
137 / ((dist->max_pz - dist->min_pz) / dist->n_pz));
138
139 int i_time = floor((p_f->time[i] - dist->min_time)
140 / ((dist->max_time - dist->min_time) / dist->n_time));
141
142 int i_q = floor((p_f->charge[i]/CONST_E - dist->min_q)
143 / ((dist->max_q - dist->min_q) / dist->n_q));
144
145 if(i_rho >= 0 && i_rho <= dist->n_rho - 1 &&
146 i_theta >=0 && i_theta <= dist->n_theta -1 &&
147 i_phi >=0 && i_phi <= dist->n_phi - 1 &&
148 i_pr >= 0 && i_pr <= dist->n_pr - 1 &&
149 i_pphi >= 0 && i_pphi <= dist->n_pphi - 1 &&
150 i_pz >= 0 && i_pz <= dist->n_pz - 1 &&
151 i_time >= 0 && i_time <= dist->n_time - 1 &&
152 i_q >= 0 && i_q <= dist->n_q - 1 ) {
153#ifdef GPU
154 index = dist_rho6D_index(
155 i_rho, i_theta, i_phi, i_pr, i_pphi, i_pz,
156 i_time, i_q, dist->step_7, dist->step_6, dist->step_5,
157 dist->step_4, dist->step_3, dist->step_2, dist->step_1);
158 weight = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
159 GPU_ATOMIC
160 dist->histogram[index] += weight;
161#else
162 index[i] = dist_rho6D_index(
163 i_rho, i_theta, i_phi, i_pr, i_pphi, i_pz,
164 i_time, i_q, dist->step_7, dist->step_6, dist->step_5,
165 dist->step_4, dist->step_3, dist->step_2, dist->step_1);
166 weight[i] = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
167 valid[i] = 1;
168#endif
169 }
170 }
171 }
172#ifndef GPU
173 for(int i = 0; i < p_f->n_mrk; i++) {
174 if(p_f->running[i] && valid[i] == 1) {
175 GPU_ATOMIC
176 dist->histogram[index[i]] += weight[i];
177 }
178 }
179#endif
180}
181
194 particle_simd_gc* p_i) {
195
196 GPU_PARALLEL_LOOP_ALL_LEVELS
197 for(int i = 0; i < p_f->n_mrk; i++) {
198 if(p_f->running[i]) {
199
200 real pr, pphi, pz;
201 real B_dB[12] = {p_f->B_r[i],
202 p_f->B_r_dr[i],
203 p_f->B_r_dphi[i],
204 p_f->B_r_dz[i],
205 p_f->B_phi[i],
206 p_f->B_phi_dr[i],
207 p_f->B_phi_dphi[i],
208 p_f->B_phi_dz[i],
209 p_f->B_z[i],
210 p_f->B_z_dr[i],
211 p_f->B_z_dphi[i],
212 p_f->B_z_dz[i]};
213 gctransform_pparmuzeta2prpphipz(p_f->mass[i], p_f->charge[i], B_dB,
214 p_f->phi[i], p_f->ppar[i],
215 p_f->mu[i], p_f->zeta[i],
216 &pr, &pphi, &pz);
217
218 int i_rho = floor((p_f->rho[i] - dist->min_rho)
219 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
220
221 real phi = fmod(p_f->phi[i], 2*CONST_PI);
222 if(phi < 0) {
223 phi = phi + 2*CONST_PI;
224 }
225 int i_phi = floor((phi - dist->min_phi)
226 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
227
228 real theta = fmod(p_f->theta[i], 2*CONST_PI);
229 if(theta < 0) {
230 theta = theta + 2*CONST_PI;
231 }
232 int i_theta = floor((theta - dist->min_theta)
233 / ((dist->max_theta - dist->min_theta)
234 / dist->n_theta));
235
236 int i_pr = floor((pr - dist->min_pr)
237 / ((dist->max_pr - dist->min_pr) / dist->n_pr));
238
239 int i_pphi = floor((pphi - dist->min_pphi)
240 / ((dist->max_pphi - dist->min_pphi)
241 / dist->n_pphi));
242
243 int i_pz = floor((pz - dist->min_pz)
244 / ((dist->max_pz - dist->min_pz) / dist->n_pz));
245
246 int i_time = floor((p_f->time[i] - dist->min_time)
247 / ((dist->max_time - dist->min_time) / dist->n_time));
248
249 int i_q = floor((p_f->charge[i]/CONST_E - dist->min_q)
250 / ((dist->max_q - dist->min_q) / dist->n_q));
251
252 if(i_rho >= 0 && i_rho <= dist->n_rho - 1 &&
253 i_theta >= 0 && i_theta <= dist->n_theta -1 &&
254 i_phi >= 0 && i_phi <= dist->n_phi - 1 &&
255 i_pr >= 0 && i_pr <= dist->n_pr - 1 &&
256 i_pphi >= 0 && i_pphi <= dist->n_pphi - 1 &&
257 i_pz >= 0 && i_pz <= dist->n_pz - 1 &&
258 i_time >= 0 && i_time <= dist->n_time - 1 &&
259 i_q >= 0 && i_q <= dist->n_q - 1 ) {
260 real weight = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
261 size_t index = dist_rho6D_index(
262 i_rho, i_theta, i_phi, i_pr, i_pphi, i_pz,
263 i_time, i_q, dist->step_7, dist->step_6, dist->step_5,
264 dist->step_4, dist->step_3, dist->step_2, dist->step_1);
265 GPU_ATOMIC
266 dist->histogram[index] += weight;
267 }
268 }
269 }
270}
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:35
void dist_rho6D_onload(dist_rho6D_data *data)
Onload data back to the host.
Definition dist_rho6D.c:78
int dist_rho6D_init(dist_rho6D_data *data)
Initializes distribution data.
Definition dist_rho6D.c:34
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:193
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(dist_rho6D_data *data)
Free allocated resources.
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:95
void dist_rho6D_offload(dist_rho6D_data *data)
Offload data to the accelerator.
Definition dist_rho6D.c:67
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: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
integer * running
Definition particle.h:320
real * B_phi_dphi
Definition particle.h:299