ASCOT5
Toggle main menu visibility
Loading...
Searching...
No Matches
diag
dist_5D.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_5D.h
"
12
#include "
../particle.h
"
13
17
size_t
dist_5D_index
(
int
i_r,
int
i_phi,
int
i_z,
int
i_ppara,
int
i_pperp,
18
int
i_time,
int
i_q,
size_t
step_6,
size_t
step_5,
19
size_t
step_4,
size_t
step_3,
size_t
step_2,
20
size_t
step_1) {
21
return
(
size_t
)(i_r) * step_6
22
+ (
size_t
)(i_phi) * step_5
23
+ (
size_t
)(i_z) * 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
35
int
dist_5D_init
(
dist_5D_data
* data) {
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_z = (size_t)(data->
n_z
);
42
size_t
n_phi = (size_t)(data->
n_phi
);
43
data->
step_6
= n_q * n_time * n_pperp * n_ppara * n_z * n_phi;
44
data->
step_5
= n_q * n_time * n_pperp * n_ppara * n_z;
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_r
,
sizeof
(
real
));
51
return
data->
histogram
== NULL;
52
}
53
59
void
dist_5D_free
(
dist_5D_data
* data) {
60
free(data->
histogram
);
61
}
62
68
void
dist_5D_offload
(
dist_5D_data
* data) {
69
GPU_MAP_TO_DEVICE(
70
data->
histogram
[0:data->
n_r
*data->
n_phi
*data->
n_z
*data->
n_ppara
*data->
n_pperp
*data->
n_time
*data->
n_q
]
71
)
72
}
73
79
void
dist_5D_onload
(
dist_5D_data
* data) {
80
GPU_UPDATE_FROM_DEVICE(
81
data->
histogram
[0:data->
n_r
*data->
n_phi
*data->
n_z
*data->
n_ppara
*data->
n_pperp
*data->
n_time
*data->
n_q
]
82
)
83
}
84
96
void
dist_5D_update_fo
(
dist_5D_data
* dist,
particle_simd_fo
* p_f,
97
particle_simd_fo
* p_i) {
98
99
#ifdef GPU
100
size_t
index;
101
real
weight;
102
#else
103
size_t
index[
NSIMD
];
104
real
weight[
NSIMD
];
105
int
valid[
NSIMD
] = {0};
106
#endif
107
108
GPU_PARALLEL_LOOP_ALL_LEVELS
109
for
(
int
i = 0; i < p_f->
n_mrk
; i++) {
110
if
(p_f->
running
[i]) {
111
real
i_r = floor((p_f->
r
[i] - dist->
min_r
)
112
/ ((dist->
max_r
- dist->
min_r
)/dist->
n_r
));
113
114
real
phi =
fmod
(p_f->
phi
[i], 2*
CONST_PI
);
115
if
(phi < 0) {
116
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
int
i_z = floor((p_f->
z
[i] - dist->
min_z
)
122
/ ((dist->
max_z
- dist->
min_z
) / dist->
n_z
));
123
124
real
ppara = ( p_f->
p_r
[i] * p_f->
B_r
[i]
125
+ p_f->
p_phi
[i] * p_f->
B_phi
[i]
126
+ p_f->
p_z
[i] * p_f->
B_z
[i])
127
/ sqrt( p_f->
B_r
[i] * p_f->
B_r
[i]
128
+ p_f->
B_phi
[i]* p_f->
B_phi
[i]
129
+ p_f->
B_z
[i] * p_f->
B_z
[i]);
130
int
i_ppara = floor((ppara - dist->
min_ppara
)
131
/ ((dist->
max_ppara
- dist->
min_ppara
) / dist->
n_ppara
));
132
133
real
pperp = sqrt(
134
p_f->
p_r
[i] * p_f->
p_r
[i]
135
+ p_f->
p_phi
[i] * p_f->
p_phi
[i]
136
+ p_f->
p_z
[i] * p_f->
p_z
[i]
137
- ppara * ppara);
138
int
i_pperp = floor((pperp - dist->
min_pperp
)
139
/ ((dist->
max_pperp
- dist->
min_pperp
) / dist->
n_pperp
));
140
141
int
i_time = floor((p_f->
time
[i] - dist->
min_time
)
142
/ ((dist->
max_time
- dist->
min_time
) / dist->
n_time
));
143
144
int
i_q = floor((p_f->
charge
[i]/
CONST_E
- dist->
min_q
)
145
/ ((dist->
max_q
- dist->
min_q
) / dist->
n_q
));
146
147
if
(i_r >= 0 && i_r <= dist->n_r - 1 &&
148
i_phi >= 0 && i_phi <= dist->n_phi - 1 &&
149
i_z >= 0 && i_z <= dist->n_z - 1 &&
150
i_ppara >= 0 && i_ppara <= dist->n_ppara - 1 &&
151
i_pperp >= 0 && i_pperp <= dist->n_pperp - 1 &&
152
i_time >= 0 && i_time <= dist->n_time - 1 &&
153
i_q >= 0 && i_q <= dist->n_q - 1 ) {
154
#ifdef GPU
155
index =
dist_5D_index
(
156
i_r, i_phi, i_z, i_ppara, i_pperp, i_time,
157
i_q, dist->
step_6
, dist->
step_5
, dist->
step_4
,
158
dist->
step_3
, dist->
step_2
, dist->
step_1
);
159
weight = p_f->
weight
[i] * (p_f->
time
[i] - p_i->
time
[i]);
160
GPU_ATOMIC
161
dist->
histogram
[index] += weight;
162
#else
163
index[i] =
dist_5D_index
(
164
i_r, i_phi, i_z, i_ppara, i_pperp, i_time,
165
i_q, dist->
step_6
, dist->
step_5
, dist->
step_4
,
166
dist->
step_3
, dist->
step_2
, dist->
step_1
);
167
weight[i] = p_f->
weight
[i] * (p_f->
time
[i] - p_i->
time
[i]);
168
valid[i] = 1;
169
#endif
170
}
171
}
172
}
173
174
#ifndef GPU
175
for
(
int
i = 0; i < p_f->
n_mrk
; i++) {
176
if
(p_f->
running
[i] && valid[i] == 1) {
177
GPU_ATOMIC
178
dist->
histogram
[index[i]] += weight[i];
179
}
180
}
181
#endif
182
}
183
195
void
dist_5D_update_gc
(
dist_5D_data
* dist,
particle_simd_gc
* p_f,
196
particle_simd_gc
* p_i) {
197
198
GPU_PARALLEL_LOOP_ALL_LEVELS
199
for
(
int
i = 0; i < p_f->
n_mrk
; i++) {
200
if
(p_f->
running
[i]) {
201
int
i_r = floor((p_f->
r
[i] - dist->
min_r
)
202
/ ((dist->
max_r
- dist->
min_r
)/dist->
n_r
));
203
204
real
phi =
fmod
(p_f->
phi
[i], 2*
CONST_PI
);
205
if
(phi < 0) {
206
phi = phi + 2*
CONST_PI
;
207
}
208
int
i_phi = floor((phi - dist->
min_phi
)
209
/ ((dist->
max_phi
- dist->
min_phi
)/dist->
n_phi
));
210
211
int
i_z = floor((p_f->
z
[i] - dist->
min_z
)
212
/ ((dist->
max_z
- dist->
min_z
) / dist->
n_z
));
213
214
int
i_ppara = floor((p_f->
ppar
[i] - dist->
min_ppara
)
215
/ ((dist->
max_ppara
- dist->
min_ppara
) / dist->
n_ppara
));
216
217
real
pperp = sqrt(2 * sqrt( p_f->
B_r
[i] * p_f->
B_r
[i]
218
+ p_f->
B_phi
[i] * p_f->
B_phi
[i]
219
+ p_f->
B_z
[i] * p_f->
B_z
[i] )
220
* p_f->
mu
[i] * p_f->
mass
[i]);
221
int
i_pperp = floor((pperp - dist->
min_pperp
)
222
/ ((dist->
max_pperp
- dist->
min_pperp
) / dist->
n_pperp
));
223
224
int
i_time = floor((p_f->
time
[i] - dist->
min_time
)
225
/ ((dist->
max_time
- dist->
min_time
) / dist->
n_time
));
226
227
int
i_q = floor((p_f->
charge
[i]/
CONST_E
- dist->
min_q
)
228
/ ((dist->
max_q
- dist->
min_q
) / dist->
n_q
));
229
230
if
(i_r >= 0 && i_r <= dist->n_r - 1 &&
231
i_phi >= 0 && i_phi <= dist->n_phi - 1 &&
232
i_z >= 0 && i_z <= dist->n_z - 1 &&
233
i_ppara >= 0 && i_ppara <= dist->n_ppara - 1 &&
234
i_pperp >= 0 && i_pperp <= dist->n_pperp - 1 &&
235
i_time >= 0 && i_time <= dist->n_time - 1 &&
236
i_q >= 0 && i_q <= dist->n_q - 1 ) {
237
real
weight = p_f->
weight
[i] * (p_f->
time
[i] - p_i->
time
[i]);
238
size_t
index =
dist_5D_index
(
239
i_r, i_phi, i_z, i_ppara, i_pperp, i_time,
240
i_q, dist->
step_6
, dist->
step_5
, dist->
step_4
,
241
dist->
step_3
, dist->
step_2
, dist->
step_1
);
242
GPU_ATOMIC
243
dist->
histogram
[index] += weight;
244
}
245
}
246
}
247
}
ascot5.h
Main header file for ASCOT5.
real
double real
Definition
ascot5.h:85
NSIMD
#define NSIMD
Number of particles simulated simultaneously in a particle group operations.
Definition
ascot5.h:91
consts.h
Header file containing physical and mathematical constants.
CONST_PI
#define CONST_PI
pi
Definition
consts.h:11
CONST_E
#define CONST_E
Elementary charge [C].
Definition
consts.h:35
dist_5D_update_gc
void dist_5D_update_gc(dist_5D_data *dist, particle_simd_gc *p_f, particle_simd_gc *p_i)
Update the histogram from guiding center markers.
Definition
dist_5D.c:195
dist_5D_onload
void dist_5D_onload(dist_5D_data *data)
Onload data back to the host.
Definition
dist_5D.c:79
dist_5D_index
size_t dist_5D_index(int i_r, int i_phi, int i_z, 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)
Function for calculating the index in the histogram array.
Definition
dist_5D.c:17
dist_5D_update_fo
void dist_5D_update_fo(dist_5D_data *dist, particle_simd_fo *p_f, particle_simd_fo *p_i)
Update the histogram from full-orbit particles.
Definition
dist_5D.c:96
dist_5D_free
void dist_5D_free(dist_5D_data *data)
Free allocated resources.
Definition
dist_5D.c:59
dist_5D_init
int dist_5D_init(dist_5D_data *data)
Initializes distribution from offload data.
Definition
dist_5D.c:35
dist_5D_offload
void dist_5D_offload(dist_5D_data *data)
Offload data to the accelerator.
Definition
dist_5D.c:68
dist_5D.h
Header file for dist_5D.c.
fmod
real fmod(real x, real y)
Compute the modulus of two real numbers.
Definition
math.c:22
math.h
Header file for math.c.
particle.h
Header file for particle.c.
physlib.h
Methods to evaluate elementary physical quantities.
dist_5D_data
Histogram parameters.
Definition
dist_5D.h:15
dist_5D_data::n_z
int n_z
Definition
dist_5D.h:24
dist_5D_data::min_time
real min_time
Definition
dist_5D.h:37
dist_5D_data::max_r
real max_r
Definition
dist_5D.h:18
dist_5D_data::max_pperp
real max_pperp
Definition
dist_5D.h:34
dist_5D_data::step_5
size_t step_5
Definition
dist_5D.h:48
dist_5D_data::min_phi
real min_phi
Definition
dist_5D.h:21
dist_5D_data::step_3
size_t step_3
Definition
dist_5D.h:46
dist_5D_data::max_phi
real max_phi
Definition
dist_5D.h:22
dist_5D_data::min_ppara
real min_ppara
Definition
dist_5D.h:29
dist_5D_data::step_1
size_t step_1
Definition
dist_5D.h:44
dist_5D_data::n_q
int n_q
Definition
dist_5D.h:40
dist_5D_data::max_z
real max_z
Definition
dist_5D.h:26
dist_5D_data::n_phi
int n_phi
Definition
dist_5D.h:20
dist_5D_data::max_time
real max_time
Definition
dist_5D.h:38
dist_5D_data::n_time
int n_time
Definition
dist_5D.h:36
dist_5D_data::step_2
size_t step_2
Definition
dist_5D.h:45
dist_5D_data::n_ppara
int n_ppara
Definition
dist_5D.h:28
dist_5D_data::step_6
size_t step_6
Definition
dist_5D.h:49
dist_5D_data::n_pperp
int n_pperp
Definition
dist_5D.h:32
dist_5D_data::min_z
real min_z
Definition
dist_5D.h:25
dist_5D_data::n_r
int n_r
Definition
dist_5D.h:16
dist_5D_data::max_ppara
real max_ppara
Definition
dist_5D.h:30
dist_5D_data::min_r
real min_r
Definition
dist_5D.h:17
dist_5D_data::max_q
real max_q
Definition
dist_5D.h:42
dist_5D_data::histogram
real * histogram
Definition
dist_5D.h:51
dist_5D_data::min_pperp
real min_pperp
Definition
dist_5D.h:33
dist_5D_data::step_4
size_t step_4
Definition
dist_5D.h:47
dist_5D_data::min_q
real min_q
Definition
dist_5D.h:41
particle_simd_fo
Struct representing NSIMD particle markers.
Definition
particle.h:210
particle_simd_fo::p_phi
real * p_phi
Definition
particle.h:216
particle_simd_fo::time
real * time
Definition
particle.h:220
particle_simd_fo::charge
real * charge
Definition
particle.h:219
particle_simd_fo::r
real * r
Definition
particle.h:212
particle_simd_fo::n_mrk
size_t n_mrk
Definition
particle.h:256
particle_simd_fo::weight
real * weight
Definition
particle.h:241
particle_simd_fo::B_phi
real * B_phi
Definition
particle.h:226
particle_simd_fo::p_z
real * p_z
Definition
particle.h:217
particle_simd_fo::B_z
real * B_z
Definition
particle.h:227
particle_simd_fo::B_r
real * B_r
Definition
particle.h:225
particle_simd_fo::p_r
real * p_r
Definition
particle.h:215
particle_simd_fo::running
integer * running
Definition
particle.h:252
particle_simd_fo::phi
real * phi
Definition
particle.h:213
particle_simd_fo::z
real * z
Definition
particle.h:214
particle_simd_gc
Struct representing NSIMD guiding center markers.
Definition
particle.h:275
particle_simd_gc::weight
real * weight
Definition
particle.h:307
particle_simd_gc::z
real * z
Definition
particle.h:279
particle_simd_gc::running
integer * running
Definition
particle.h:320
particle_simd_gc::ppar
real * ppar
Definition
particle.h:280
particle_simd_gc::mu
real * mu
Definition
particle.h:281
particle_simd_gc::n_mrk
size_t n_mrk
Definition
particle.h:324
particle_simd_gc::mass
real * mass
Definition
particle.h:283
particle_simd_gc::phi
real * phi
Definition
particle.h:278
particle_simd_gc::charge
real * charge
Definition
particle.h:284
particle_simd_gc::r
real * r
Definition
particle.h:277
particle_simd_gc::B_phi
real * B_phi
Definition
particle.h:290
particle_simd_gc::time
real * time
Definition
particle.h:285
particle_simd_gc::B_z
real * B_z
Definition
particle.h:292
particle_simd_gc::B_r
real * B_r
Definition
particle.h:288
Generated on
for ASCOT5 by
1.18.0