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
37 size_t n_q = (size_t)(data->n_q);
38 size_t n_time = (size_t)(data->n_time);
39 size_t n_pperp = (size_t)(data->n_pperp);
40 size_t n_ppara = (size_t)(data->n_ppara);
41 size_t n_phi = (size_t)(data->n_phi);
42 size_t n_theta = (size_t)(data->n_theta);
43 data->step_6 = n_q * n_time * n_pperp * n_ppara * n_phi * n_theta;
44 data->step_5 = n_q * n_time * n_pperp * n_ppara * n_phi;
45 data->step_4 = n_q * n_time * n_pperp * n_ppara;
46 data->step_3 = n_q * n_time * n_pperp;
47 data->step_2 = n_q * n_time;
48 data->step_1 = n_q;
49
50 data->histogram = calloc(data->step_6 * (size_t)data->n_rho, sizeof(real));
51 return data->histogram == NULL;
52}
53
60 free(data->histogram);
61}
62
69 GPU_MAP_TO_DEVICE(
70 data->histogram[0:data->n_rho*data->n_theta*data->n_phi*data->n_ppara*data->n_pperp*data->n_time*data->n_q]
71 )
72}
73
86 particle_simd_fo* p_i) {
87
88#ifdef GPU
89 size_t index;
90 real weight;
91#else
92 size_t index[NSIMD];
93 real weight[NSIMD];
94#endif
95
96 GPU_PARALLEL_LOOP_ALL_LEVELS
97 for(int i = 0; i < p_f->n_mrk; i++) {
98 if(p_f->running[i]) {
99
100 int i_rho = floor((p_f->rho[i] - dist->min_rho)
101 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
102
103 real phi = fmod(p_f->phi[i], 2*CONST_PI);
104 if(phi < 0) {
105 phi += 2*CONST_PI;
106 }
107 int i_phi = floor((phi - dist->min_phi)
108 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
109
110 real theta = fmod(p_f->theta[i], 2*CONST_PI);
111 if(theta < 0) {
112 theta += 2*CONST_PI;
113 }
114 int i_theta = floor((theta - dist->min_theta)
115 / ( (dist->max_theta - dist->min_theta)
116 / dist->n_theta) );
117
118 real ppara = ( p_f->p_r[i] * p_f->B_r[i]
119 + p_f->p_phi[i] * p_f->B_phi[i]
120 + p_f->p_z[i] * p_f->B_z[i])
121 / sqrt( p_f->B_r[i] * p_f->B_r[i]
122 + p_f->B_phi[i]* p_f->B_phi[i]
123 + p_f->B_z[i] * p_f->B_z[i]);
124 int i_ppara = floor((ppara - dist->min_ppara)
125 / ((dist->max_ppara - dist->min_ppara)
126 / dist->n_ppara));
127
128 real pperp = sqrt(
129 p_f->p_r[i] * p_f->p_r[i]
130 + p_f->p_phi[i] * p_f->p_phi[i]
131 + p_f->p_z[i] * p_f->p_z[i]
132 - ppara * ppara);
133 int i_pperp = floor((pperp - dist->min_pperp)
134 / ((dist->max_pperp - dist->min_pperp)
135 / dist->n_pperp));
136
137 int i_time = floor((p_f->time[i] - dist->min_time)
138 / ((dist->max_time - dist->min_time) / dist->n_time));
139
140 int i_q = floor((p_f->charge[i]/CONST_E - dist->min_q)
141 / ((dist->max_q - dist->min_q) / dist->n_q));
142
143 if(i_rho >= 0 && i_rho <= dist->n_rho - 1 &&
144 i_phi >= 0 && i_phi <= dist->n_phi - 1 &&
145 i_theta >= 0 && i_theta <= dist->n_theta - 1 &&
146 i_ppara >= 0 && i_ppara <= dist->n_ppara - 1 &&
147 i_pperp >= 0 && i_pperp <= dist->n_pperp - 1 &&
148 i_time >= 0 && i_time <= dist->n_time - 1 &&
149 i_q >= 0 && i_q <= dist->n_q - 1 ) {
150#ifdef GPU
151 index = dist_rho5D_index(
152 i_rho, i_theta, i_phi, i_ppara, i_pperp,
153 i_time, i_q, dist->step_6, dist->step_5, dist->step_4,
154 dist->step_3, dist->step_2, dist->step_1);
155 weight = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
156 GPU_ATOMIC
157 dist->histogram[index] += weight;
158#else
159 index[i] = dist_rho5D_index(
160 i_rho, i_theta, i_phi, i_ppara, i_pperp,
161 i_time, i_q, dist->step_6, dist->step_5, dist->step_4,
162 dist->step_3, dist->step_2, dist->step_1);
163 weight[i] = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
164#endif
165 }
166 }
167 }
168#ifndef GPU
169 for(int i = 0; i < p_f->n_mrk; i++) {
170 if(p_f->running[i] && index[i] >= 0 &&
171 index[i] < dist->step_6 * dist->n_rho) {
172 GPU_ATOMIC
173 dist->histogram[index[i]] += weight[i];
174 }
175 }
176#endif
177}
178
191 particle_simd_gc* p_i) {
192 real phi[NSIMD];
193 real theta[NSIMD];
194 real pperp[NSIMD];
195
196 int i_rho[NSIMD];
197 int i_phi[NSIMD];
198 int i_theta[NSIMD];
199 int i_ppara[NSIMD];
200 int i_pperp[NSIMD];
201 int i_time[NSIMD];
202 int i_q[NSIMD];
203
204 int ok[NSIMD];
205 real weight[NSIMD];
206
207 #pragma omp simd
208 for(int i = 0; i < NSIMD; i++) {
209 if(p_f->running[i]) {
210
211 i_rho[i] = floor((p_f->rho[i] - dist->min_rho)
212 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
213
214 phi[i] = fmod(p_f->phi[i], 2*CONST_PI);
215 if(phi[i] < 0) {
216 phi[i] = phi[i] + 2*CONST_PI;
217 }
218 i_phi[i] = floor((phi[i] - dist->min_phi)
219 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
220
221 theta[i] = fmod(p_f->theta[i], 2*CONST_PI);
222 if(theta[i] < 0) {
223 theta[i] = theta[i] + 2*CONST_PI;
224 }
225 i_theta[i] = floor((theta[i] - dist->min_theta)
226 / ((dist->max_theta - dist->min_theta)
227 / dist->n_theta));
228
229 i_ppara[i] = floor((p_f->ppar[i] - dist->min_ppara)
230 / ((dist->max_ppara - dist->min_ppara) / dist->n_ppara));
231
232 pperp[i] = sqrt(2 * sqrt( p_f->B_r[i] * p_f->B_r[i]
233 + p_f->B_phi[i] * p_f->B_phi[i]
234 + p_f->B_z[i] * p_f->B_z[i] )
235 * p_f->mu[i] * p_f->mass[i]);
236 i_pperp[i] = floor((pperp[i] - dist->min_pperp)
237 / ((dist->max_pperp - dist->min_pperp)
238 / dist->n_pperp));
239
240 i_time[i] = floor((p_f->time[i] - dist->min_time)
241 / ((dist->max_time - dist->min_time) / dist->n_time));
242
243 i_q[i] = floor((p_f->charge[i]/CONST_E - dist->min_q)
244 / ((dist->max_q - dist->min_q) / dist->n_q));
245
246 if(i_rho[i] >= 0 && i_rho[i] <= dist->n_rho - 1 &&
247 i_phi[i] >= 0 && i_phi[i] <= dist->n_phi - 1 &&
248 i_theta[i] >= 0 && i_theta[i] <= dist->n_theta - 1 &&
249 i_ppara[i] >= 0 && i_ppara[i] <= dist->n_ppara - 1 &&
250 i_pperp[i] >= 0 && i_pperp[i] <= dist->n_pperp - 1 &&
251 i_time[i] >= 0 && i_time[i] <= dist->n_time - 1 &&
252 i_q[i] >= 0 && i_q[i] <= dist->n_q - 1 ) {
253 ok[i] = 1;
254 weight[i] = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
255 }
256 else {
257 ok[i] = 0;
258 }
259 }
260 }
261
262 for(int i = 0; i < NSIMD; i++) {
263 if(p_f->running[i] && ok[i]) {
264 size_t index = dist_rho5D_index(
265 i_rho[i], i_theta[i], i_phi[i], i_ppara[i], i_pperp[i],
266 i_time[i], i_q[i], dist->step_6, dist->step_5, dist->step_4,
267 dist->step_3, dist->step_2, dist->step_1);
268 #pragma omp atomic
269 dist->histogram[index] += weight[i];
270 }
271 }
272}
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
int dist_rho5D_init(dist_rho5D_data *data)
Initializes distribution data.
Definition dist_rho5D.c:35
void dist_rho5D_free(dist_rho5D_data *data)
Free the allocated resources.
Definition dist_rho5D.c:59
void dist_rho5D_offload(dist_rho5D_data *data)
Offload data to the accelerator.
Definition dist_rho5D.c:68
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:190
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:85
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.
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