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->n_time * data->n_pr * data->n_pphi
52 * data->n_pr * data->n_phi * data->n_theta
53 * data->n_rho, sizeof(real) );
54 return data->histogram == NULL;
55}
56
61 free(data->histogram);
62}
63
70 GPU_MAP_TO_DEVICE(
71 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]
72 )
73}
74
87 particle_simd_fo* p_i) {
88
89 GPU_PARALLEL_LOOP_ALL_LEVELS
90 for(int i = 0; i < p_f->n_mrk; i++) {
91 if(p_f->running[i]) {
92
93 int i_rho = floor((p_f->rho[i] - dist->min_rho)
94 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
95
96 real phi = fmod(p_f->phi[i], 2*CONST_PI);
97 if(phi < 0) {
98 phi = phi + 2*CONST_PI;
99 }
100 int i_phi = floor((phi - dist->min_phi)
101 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
102
103 real theta = fmod(p_f->theta[i], 2*CONST_PI);
104 if(theta < 0) {
105 theta += 2*CONST_PI;
106 }
107 int i_theta = floor((theta - dist->min_theta)
108 / ((dist->max_theta - dist->min_theta)
109 / dist->n_theta));
110
111 int i_pr = floor((p_f->p_r[i] - dist->min_pr)
112 / ((dist->max_pr - dist->min_pr) / dist->n_pr));
113
114 int i_pphi = floor((p_f->p_phi[i] - dist->min_pphi)
115 / ((dist->max_pphi - dist->min_pphi)
116 / dist->n_pphi));
117
118 int i_pz = floor((p_f->p_z[i] - dist->min_pz)
119 / ((dist->max_pz - dist->min_pz) / dist->n_pz));
120
121 int i_time = floor((p_f->time[i] - dist->min_time)
122 / ((dist->max_time - dist->min_time) / dist->n_time));
123
124 int i_q = floor((p_f->charge[i]/CONST_E - dist->min_q)
125 / ((dist->max_q - dist->min_q) / dist->n_q));
126
127 if(i_rho >= 0 && i_rho <= dist->n_rho - 1 &&
128 i_theta >=0 && i_theta <= dist->n_theta -1 &&
129 i_phi >=0 && i_phi <= dist->n_phi - 1 &&
130 i_pr >= 0 && i_pr <= dist->n_pr - 1 &&
131 i_pphi >= 0 && i_pphi <= dist->n_pphi - 1 &&
132 i_pz >= 0 && i_pz <= dist->n_pz - 1 &&
133 i_time >= 0 && i_time <= dist->n_time - 1 &&
134 i_q >= 0 && i_q <= dist->n_q - 1 ) {
135 real weight = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
136 size_t index = dist_rho6D_index(
137 i_rho, i_theta, i_phi, i_pr, i_pphi, i_pz,
138 i_time, i_q, dist->step_7, dist->step_6, dist->step_5,
139 dist->step_4, dist->step_3, dist->step_2, dist->step_1);
140
141 GPU_ATOMIC
142 dist->histogram[index] += weight;
143 }
144 }
145 }
146}
147
160 particle_simd_gc* p_i) {
161 real phi[NSIMD];
162 real theta[NSIMD];
163
164 int i_rho[NSIMD];
165 int i_theta[NSIMD];
166 int i_phi[NSIMD];
167 int i_pr[NSIMD];
168 int i_pphi[NSIMD];
169 int i_pz[NSIMD];
170 int i_time[NSIMD];
171 int i_q[NSIMD];
172
173 int ok[NSIMD];
174 real weight[NSIMD];
175
176 #pragma omp simd
177 for(int i = 0; i < NSIMD; i++) {
178 if(p_f->running[i]) {
179
180 real pr, pphi, pz;
181 real B_dB[12] = {p_f->B_r[i],
182 p_f->B_r_dr[i],
183 p_f->B_r_dphi[i],
184 p_f->B_r_dz[i],
185 p_f->B_phi[i],
186 p_f->B_phi_dr[i],
187 p_f->B_phi_dphi[i],
188 p_f->B_phi_dz[i],
189 p_f->B_z[i],
190 p_f->B_z_dr[i],
191 p_f->B_z_dphi[i],
192 p_f->B_z_dz[i]};
193 gctransform_pparmuzeta2prpphipz(p_f->mass[i], p_f->charge[i], B_dB,
194 p_f->phi[i], p_f->ppar[i],
195 p_f->mu[i], p_f->zeta[i],
196 &pr, &pphi, &pz);
197
198 i_rho[i] = floor((p_f->rho[i] - dist->min_rho)
199 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
200
201 phi[i] = fmod(p_f->phi[i], 2*CONST_PI);
202 if(phi[i] < 0) {
203 phi[i] = phi[i] + 2*CONST_PI;
204 }
205 i_phi[i] = floor((phi[i] - dist->min_phi)
206 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
207
208 theta[i] = fmod(p_f->theta[i], 2*CONST_PI);
209 if(theta[i] < 0) {
210 theta[i] = theta[i] + 2*CONST_PI;
211 }
212 i_theta[i] = floor((theta[i] - dist->min_theta)
213 / ((dist->max_theta - dist->min_theta)
214 / dist->n_theta));
215
216 i_pr[i] = floor((pr - dist->min_pr)
217 / ((dist->max_pr - dist->min_pr) / dist->n_pr));
218
219 i_pphi[i] = floor((pphi - dist->min_pphi)
220 / ((dist->max_pphi - dist->min_pphi)
221 / dist->n_pphi));
222
223 i_pz[i] = floor((pz - dist->min_pz)
224 / ((dist->max_pz - dist->min_pz) / dist->n_pz));
225
226 i_time[i] = floor((p_f->time[i] - dist->min_time)
227 / ((dist->max_time - dist->min_time) / dist->n_time));
228
229 i_q[i] = floor((p_f->charge[i]/CONST_E - dist->min_q)
230 / ((dist->max_q - dist->min_q) / dist->n_q));
231
232 if(i_rho[i] >= 0 && i_rho[i] <= dist->n_rho - 1 &&
233 i_theta[i] >= 0 && i_theta[i] <= dist->n_theta -1 &&
234 i_phi[i] >= 0 && i_phi[i] <= dist->n_phi - 1 &&
235 i_pr[i] >= 0 && i_pr[i] <= dist->n_pr - 1 &&
236 i_pphi[i] >= 0 && i_pphi[i] <= dist->n_pphi - 1 &&
237 i_pz[i] >= 0 && i_pz[i] <= dist->n_pz - 1 &&
238 i_time[i] >= 0 && i_time[i] <= dist->n_time - 1 &&
239 i_q[i] >= 0 && i_q[i] <= dist->n_q - 1 ) {
240 ok[i] = 1;
241 weight[i] = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
242 }
243 else {
244 ok[i] = 0;
245 }
246 }
247 }
248
249 for(int i = 0; i < NSIMD; i++) {
250 if(p_f->running[i] && ok[i]) {
251 size_t index = dist_rho6D_index(
252 i_rho[i], i_theta[i], i_phi[i], i_pr[i], i_pphi[i], i_pz[i],
253 i_time[i], i_q[i], dist->step_7, dist->step_6, dist->step_5,
254 dist->step_4, dist->step_3, dist->step_2, dist->step_1);
255 #pragma omp atomic
256 dist->histogram[index] += weight[i];
257 }
258 }
259}
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
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:159
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:60
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:86
void dist_rho6D_offload(dist_rho6D_data *data)
Offload data to the accelerator.
Definition dist_rho6D.c:69
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