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->n_time * data->n_pperp * data->n_ppara
51 * data->n_phi * data->n_theta * data->n_rho,
52 sizeof(real) );
53 return data->histogram == NULL;
54}
55
62 free(data->histogram);
63}
64
71 GPU_MAP_TO_DEVICE(
72 data->histogram[0:data->n_rho*data->n_theta*data->n_phi*data->n_ppara*data->n_pperp*data->n_time*data->n_q]
73 )
74}
75
88 particle_simd_fo* p_i) {
89
90 GPU_PARALLEL_LOOP_ALL_LEVELS
91 for(int i = 0; i < p_f->n_mrk; i++) {
92 if(p_f->running[i]) {
93
94 int i_rho = floor((p_f->rho[i] - dist->min_rho)
95 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
96
97 real phi = fmod(p_f->phi[i], 2*CONST_PI);
98 if(phi < 0) {
99 phi += 2*CONST_PI;
100 }
101 int i_phi = floor((phi - dist->min_phi)
102 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
103
104 real theta = fmod(p_f->theta[i], 2*CONST_PI);
105 if(theta < 0) {
106 theta += 2*CONST_PI;
107 }
108 int i_theta = floor((theta - dist->min_theta)
109 / ( (dist->max_theta - dist->min_theta)
110 / dist->n_theta) );
111
112 real ppara = ( p_f->p_r[i] * p_f->B_r[i]
113 + p_f->p_phi[i] * p_f->B_phi[i]
114 + p_f->p_z[i] * p_f->B_z[i])
115 / sqrt( p_f->B_r[i] * p_f->B_r[i]
116 + p_f->B_phi[i]* p_f->B_phi[i]
117 + p_f->B_z[i] * p_f->B_z[i]);
118 int i_ppara = floor((ppara - dist->min_ppara)
119 / ((dist->max_ppara - dist->min_ppara)
120 / dist->n_ppara));
121
122 real pperp = sqrt(
123 p_f->p_r[i] * p_f->p_r[i]
124 + p_f->p_phi[i] * p_f->p_phi[i]
125 + p_f->p_z[i] * p_f->p_z[i]
126 - ppara * ppara);
127 int i_pperp = floor((pperp - dist->min_pperp)
128 / ((dist->max_pperp - dist->min_pperp)
129 / dist->n_pperp));
130
131 int i_time = floor((p_f->time[i] - dist->min_time)
132 / ((dist->max_time - dist->min_time) / dist->n_time));
133
134 int i_q = floor((p_f->charge[i]/CONST_E - dist->min_q)
135 / ((dist->max_q - dist->min_q) / dist->n_q));
136
137 if(i_rho >= 0 && i_rho <= dist->n_rho - 1 &&
138 i_phi >= 0 && i_phi <= dist->n_phi - 1 &&
139 i_theta >= 0 && i_theta <= dist->n_theta - 1 &&
140 i_ppara >= 0 && i_ppara <= dist->n_ppara - 1 &&
141 i_pperp >= 0 && i_pperp <= dist->n_pperp - 1 &&
142 i_time >= 0 && i_time <= dist->n_time - 1 &&
143 i_q >= 0 && i_q <= dist->n_q - 1 ) {
144 real weight = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
145 size_t index = dist_rho5D_index(
146 i_rho, i_theta, i_phi, i_ppara, i_pperp,
147 i_time, i_q, dist->step_6, dist->step_5, dist->step_4,
148 dist->step_3, dist->step_2, dist->step_1);
149 GPU_ATOMIC
150 dist->histogram[index] += weight;
151 }
152 }
153 }
154}
155
168 particle_simd_gc* p_i) {
169 real phi[NSIMD];
170 real theta[NSIMD];
171 real pperp[NSIMD];
172
173 int i_rho[NSIMD];
174 int i_phi[NSIMD];
175 int i_theta[NSIMD];
176 int i_ppara[NSIMD];
177 int i_pperp[NSIMD];
178 int i_time[NSIMD];
179 int i_q[NSIMD];
180
181 int ok[NSIMD];
182 real weight[NSIMD];
183
184 #pragma omp simd
185 for(int i = 0; i < NSIMD; i++) {
186 if(p_f->running[i]) {
187
188 i_rho[i] = floor((p_f->rho[i] - dist->min_rho)
189 / ((dist->max_rho - dist->min_rho)/dist->n_rho));
190
191 phi[i] = fmod(p_f->phi[i], 2*CONST_PI);
192 if(phi[i] < 0) {
193 phi[i] = phi[i] + 2*CONST_PI;
194 }
195 i_phi[i] = floor((phi[i] - dist->min_phi)
196 / ((dist->max_phi - dist->min_phi)/dist->n_phi));
197
198 theta[i] = fmod(p_f->theta[i], 2*CONST_PI);
199 if(theta[i] < 0) {
200 theta[i] = theta[i] + 2*CONST_PI;
201 }
202 i_theta[i] = floor((theta[i] - dist->min_theta)
203 / ((dist->max_theta - dist->min_theta)
204 / dist->n_theta));
205
206 i_ppara[i] = floor((p_f->ppar[i] - dist->min_ppara)
207 / ((dist->max_ppara - dist->min_ppara) / dist->n_ppara));
208
209 pperp[i] = sqrt(2 * sqrt( p_f->B_r[i] * p_f->B_r[i]
210 + p_f->B_phi[i] * p_f->B_phi[i]
211 + p_f->B_z[i] * p_f->B_z[i] )
212 * p_f->mu[i] * p_f->mass[i]);
213 i_pperp[i] = floor((pperp[i] - dist->min_pperp)
214 / ((dist->max_pperp - dist->min_pperp)
215 / dist->n_pperp));
216
217 i_time[i] = floor((p_f->time[i] - dist->min_time)
218 / ((dist->max_time - dist->min_time) / dist->n_time));
219
220 i_q[i] = floor((p_f->charge[i]/CONST_E - dist->min_q)
221 / ((dist->max_q - dist->min_q) / dist->n_q));
222
223 if(i_rho[i] >= 0 && i_rho[i] <= dist->n_rho - 1 &&
224 i_phi[i] >= 0 && i_phi[i] <= dist->n_phi - 1 &&
225 i_theta[i] >= 0 && i_theta[i] <= dist->n_theta - 1 &&
226 i_ppara[i] >= 0 && i_ppara[i] <= dist->n_ppara - 1 &&
227 i_pperp[i] >= 0 && i_pperp[i] <= dist->n_pperp - 1 &&
228 i_time[i] >= 0 && i_time[i] <= dist->n_time - 1 &&
229 i_q[i] >= 0 && i_q[i] <= dist->n_q - 1 ) {
230 ok[i] = 1;
231 weight[i] = p_f->weight[i] * (p_f->time[i] - p_i->time[i]);
232 }
233 else {
234 ok[i] = 0;
235 }
236 }
237 }
238
239 for(int i = 0; i < NSIMD; i++) {
240 if(p_f->running[i] && ok[i]) {
241 size_t index = dist_rho5D_index(
242 i_rho[i], i_theta[i], i_phi[i], i_ppara[i], i_pperp[i],
243 i_time[i], i_q[i], dist->step_6, dist->step_5, dist->step_4,
244 dist->step_3, dist->step_2, dist->step_1);
245 #pragma omp atomic
246 dist->histogram[index] += weight[i];
247 }
248 }
249}
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_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:61
void dist_rho5D_offload(dist_rho5D_data *data)
Offload data to the accelerator.
Definition dist_rho5D.c:70
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:167
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:87
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