ASCOT5
Loading...
Searching...
No Matches
dist_6D.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_6D.h"
12#include "../gctransform.h"
13
17size_t dist_6D_index(int i_r, int i_phi, int i_z, 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, size_t step_3,
20 size_t step_2, size_t step_1) {
21 return (size_t)(i_r) * step_7
22 + (size_t)(i_phi) * step_6
23 + (size_t)(i_z) * 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_z = (size_t)(data->n_z);
42 size_t n_phi = (size_t)(data->n_phi);
43 data->step_7 = n_q * n_time * n_pz * n_pphi * n_pr * n_z * n_phi;
44 data->step_6 = n_q * n_time * n_pz * n_pphi * n_pr * n_z;
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_pz * data->n_z * data->n_phi * data->n_r,
53 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_r*data->n_phi*data->n_z*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_r = floor((p_f->r[i] - dist->min_r)
94 / ((dist->max_r - dist->min_r)/dist->n_r));
95
96 real phi = fmod(p_f->phi[i], 2*CONST_PI);
97 if(phi < 0) {
98 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 int i_z = floor((p_f->z[i] - dist->min_z)
104 / ((dist->max_z - dist->min_z) / dist->n_z));
105
106 int i_pr = floor((p_f->p_r[i] - dist->min_pr)
107 / ((dist->max_pr - dist->min_pr) / dist->n_pr));
108
109 int i_pphi = floor((p_f->p_phi[i] - dist->min_pphi)
110 / ((dist->max_pphi - dist->min_pphi) / dist->n_pphi));
111
112 int i_pz = floor((p_f->p_z[i] - dist->min_pz)
113 / ((dist->max_pz - dist->min_pz) / dist->n_pz));
114
115 int i_time = floor((p_f->time[i] - dist->min_time)
116 / ((dist->max_time - dist->min_time) / dist->n_time));
117
118 int i_q = floor((p_f->charge[i]/CONST_E - dist->min_q)
119 / ((dist->max_q - dist->min_q) / dist->n_q));
120
121 if(i_r >= 0 && i_r <= dist->n_r - 1 &&
122 i_phi >= 0 && i_phi <= dist->n_phi - 1 &&
123 i_z >= 0 && i_z <= dist->n_z - 1 &&
124 i_pr >= 0 && i_pr <= dist->n_pr - 1 &&
125 i_pphi >= 0 && i_pphi <= dist->n_pphi - 1 &&
126 i_pz >= 0 && i_pz <= dist->n_pz - 1 &&
127 i_time >= 0 && i_time <= dist->n_time - 1 &&
128 i_q >= 0 && i_q <= dist->n_q - 1 ) {
129 real weight = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
130 size_t index = dist_6D_index(
131 i_r, i_phi, i_z, i_pr, i_pphi, i_pz,
132 i_time, i_q, dist->step_7, dist->step_6, dist->step_5,
133 dist->step_4, dist->step_3, dist->step_2, dist->step_1);
134 GPU_ATOMIC
135 dist->histogram[index] += weight;
136 }
137 }
138 }
139}
140
153 particle_simd_gc* p_i) {
154 real phi[NSIMD];
155
156 int i_r[NSIMD];
157 int i_phi[NSIMD];
158 int i_z[NSIMD];
159 int i_pr[NSIMD];
160 int i_pphi[NSIMD];
161 int i_pz[NSIMD];
162 int i_time[NSIMD];
163 int i_q[NSIMD];
164
165 int ok[NSIMD];
166 real weight[NSIMD];
167
168 #pragma omp simd
169 for(int i = 0; i < NSIMD; i++) {
170 if(p_f->running[i]) {
171
172 real pr, pphi, pz;
173 real B_dB[12] = {
174 p_f->B_r[i], p_f->B_r_dr[i], p_f->B_r_dphi[i], p_f->B_r_dz[i],
175 p_f->B_phi[i], p_f->B_phi_dr[i], p_f->B_phi_dphi[i],
176 p_f->B_phi_dz[i],
177 p_f->B_z[i], p_f->B_z_dr[i], p_f->B_z_dphi[i], p_f->B_z_dz[i]};
178 gctransform_pparmuzeta2prpphipz(p_f->mass[i], p_f->charge[i], B_dB,
179 p_f->phi[i], p_f->ppar[i],
180 p_f->mu[i], p_f->zeta[i],
181 &pr, &pphi, &pz);
182
183 i_r[i] = floor((p_f->r[i] - dist->min_r)
184 / ((dist->max_r - dist->min_r)/dist->n_r));
185
186 phi[i] = fmod(p_f->phi[i], 2*CONST_PI);
187 if(phi[i] < 0) {
188 phi[i] = phi[i] + 2*CONST_PI;
189 }
190 i_phi[i] = floor((phi[i] - dist->min_phi)
191 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
192
193 i_z[i] = floor((p_f->z[i] - dist->min_z)
194 / ((dist->max_z - dist->min_z) / dist->n_z));
195
196 i_pr[i] = floor((pr - dist->min_pr)
197 / ((dist->max_pr - dist->min_pr) / dist->n_pr));
198
199 i_pphi[i] = floor((pphi - dist->min_pphi)
200 / ((dist->max_pphi - dist->min_pphi) / dist->n_pphi));
201
202 i_pz[i] = floor((pz - dist->min_pz)
203 / ((dist->max_pz - dist->min_pz) / dist->n_pz));
204
205 i_time[i] = floor((p_f->time[i] - dist->min_time)
206 / ((dist->max_time - dist->min_time) / dist->n_time));
207
208 i_q[i] = floor((p_f->charge[i]/CONST_E - dist->min_q)
209 / ((dist->max_q - dist->min_q) / dist->n_q));
210
211 if(i_r[i] >= 0 && i_r[i] <= dist->n_r - 1 &&
212 i_phi[i] >= 0 && i_phi[i] <= dist->n_phi - 1 &&
213 i_z[i] >= 0 && i_z[i] <= dist->n_z - 1 &&
214 i_pr[i] >= 0 && i_pr[i] <= dist->n_pr - 1 &&
215 i_pphi[i] >= 0 && i_pphi[i] <= dist->n_pphi - 1 &&
216 i_pz[i] >= 0 && i_pz[i] <= dist->n_pz - 1 &&
217 i_time[i] >= 0 && i_time[i] <= dist->n_time - 1 &&
218 i_q[i] >= 0 && i_q[i] <= dist->n_q - 1 ) {
219 ok[i] = 1;
220 weight[i] = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
221 }
222 else {
223 ok[i] = 0;
224 }
225 }
226 }
227
228 for(int i = 0; i < NSIMD; i++) {
229 if(p_f->running[i] && ok[i]) {
230 size_t index = dist_6D_index(
231 i_r[i], i_phi[i], i_z[i], i_pr[i], i_pphi[i], i_pz[i],
232 i_time[i], i_q[i], dist->step_7, dist->step_6, dist->step_5,
233 dist->step_4, dist->step_3, dist->step_2, dist->step_1);
234 #pragma omp atomic
235 dist->histogram[index] += weight[i];
236 }
237 }
238}
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_6D_update_fo(dist_6D_data *dist, particle_simd_fo *p_f, particle_simd_fo *p_i)
Update the histogram from full-orbit particles.
Definition dist_6D.c:86
void dist_6D_update_gc(dist_6D_data *dist, particle_simd_gc *p_f, particle_simd_gc *p_i)
Update the histogram from guiding-center particles.
Definition dist_6D.c:152
int dist_6D_init(dist_6D_data *data)
Initializes distribution data.
Definition dist_6D.c:34
size_t dist_6D_index(int i_r, int i_phi, int i_z, 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_6D.c:17
void dist_6D_free(dist_6D_data *data)
Free allocated resources.
Definition dist_6D.c:60
void dist_6D_offload(dist_6D_data *data)
Offload data to the accelerator.
Definition dist_6D.c:69
Header file for dist_6D.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_6D.h:15
real max_phi
Definition dist_6D.h:22
real min_phi
Definition dist_6D.h:21
size_t step_7
Definition dist_6D.h:54
real * histogram
Definition dist_6D.h:56
size_t step_3
Definition dist_6D.h:50
real min_pz
Definition dist_6D.h:37
real max_pz
Definition dist_6D.h:38
real min_time
Definition dist_6D.h:41
size_t step_5
Definition dist_6D.h:52
real min_q
Definition dist_6D.h:45
size_t step_4
Definition dist_6D.h:51
real max_pphi
Definition dist_6D.h:34
real max_time
Definition dist_6D.h:42
real min_pphi
Definition dist_6D.h:33
real min_pr
Definition dist_6D.h:29
real max_pr
Definition dist_6D.h:30
size_t step_6
Definition dist_6D.h:53
size_t step_1
Definition dist_6D.h:48
real max_z
Definition dist_6D.h:26
real min_r
Definition dist_6D.h:17
real max_r
Definition dist_6D.h:18
real max_q
Definition dist_6D.h:46
size_t step_2
Definition dist_6D.h:49
real min_z
Definition dist_6D.h:25
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