ASCOT5
Loading...
Searching...
No Matches
hdf5_orbit.c
Go to the documentation of this file.
1
5#include <string.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <math.h>
9#include <hdf5.h>
10#include <hdf5_hl.h>
11#include "hdf5_helpers.h"
12#include "../simulate.h"
13#include "../particle.h"
14#include "../ascot5.h"
15#include "../diag/diag_orb.h"
16#include "../consts.h"
17#include "hdf5_orbit.h"
18
19void hdf5_orbit_writeset(hid_t group, const char* name, const char* unit,
20 int type, int arraylength, real confac,
21 integer* mask, integer size, real* data);
22
34int hdf5_orbit_write(hid_t f, char* path, diag_orb_offload_data* data,
35 real* orbits) {
36 hid_t group = H5Gcreate2(f, path, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
37 if(group < 0) {
38 return 1;
39 }
40
41 int arraylength = data->Nmrk*data->Npnt;
42 int datasize = 0;
43 integer* mask = malloc(arraylength*sizeof(real));
44
45 for(integer i=0; i < arraylength; i++) {
46 mask[i] = orbits[i] > 0;
47 if(mask[i]) {
48 datasize++;
49 }
50 }
51
52 int dtypef64 = 0;
53 int dtypei32 = 1;
54 int dtypei16 = 2;
55 if(data->record_mode == simulate_mode_fo) {
56
57 hdf5_orbit_writeset(group, "ids", "1", dtypei32, arraylength, 1,
58 mask, datasize, &orbits[arraylength*0]);
59 hdf5_orbit_writeset(group, "mileage", "s", dtypef64, arraylength, 1,
60 mask, datasize, &orbits[arraylength*1]);
61 hdf5_orbit_writeset(group, "r", "m", dtypef64, arraylength, 1,
62 mask, datasize, &orbits[arraylength*2]);
63 hdf5_orbit_writeset(group, "phi", "deg", dtypef64, arraylength,
64 180.0/CONST_PI,
65 mask, datasize, &orbits[arraylength*3]);
66 hdf5_orbit_writeset(group, "z", "m", dtypef64, arraylength, 1,
67 mask, datasize, &orbits[arraylength*4]);
68 hdf5_orbit_writeset(group, "pr", "kg*m/s", dtypef64, arraylength, 1,
69 mask, datasize, &orbits[arraylength*5]);
70 hdf5_orbit_writeset(group, "pphi", "kg*m/s", dtypef64, arraylength, 1,
71 mask, datasize, &orbits[arraylength*6]);
72 hdf5_orbit_writeset(group, "pz", "kg*m/s", dtypef64, arraylength, 1,
73 mask, datasize, &orbits[arraylength*7]);
74 hdf5_orbit_writeset(group, "weight", "1", dtypef64, arraylength, 1,
75 mask, datasize, &orbits[arraylength*8]);
76 hdf5_orbit_writeset(group, "charge", "e", dtypei16, arraylength,
77 1.0/CONST_E,
78 mask, datasize, &orbits[arraylength*9]);
79 hdf5_orbit_writeset(group, "rho", "1", dtypef64, arraylength, 1,
80 mask, datasize, &orbits[arraylength*10]);
81 hdf5_orbit_writeset(group, "theta", "deg", dtypef64, arraylength,
82 180.0/CONST_PI,
83 mask, datasize, &orbits[arraylength*11]);
84 hdf5_orbit_writeset(group, "br", "T", dtypef64, arraylength, 1,
85 mask, datasize, &orbits[arraylength*12]);
86 hdf5_orbit_writeset(group, "bphi", "T", dtypef64, arraylength, 1,
87 mask, datasize, &orbits[arraylength*13]);
88 hdf5_orbit_writeset(group, "bz", "T", dtypef64, arraylength, 1,
89 mask, datasize, &orbits[arraylength*14]);
90 hdf5_orbit_writeset(group, "simmode", "1", dtypei32, arraylength, 1,
91 mask, datasize, &orbits[arraylength*15]);
92
93 }
94
95 if(data->record_mode == simulate_mode_gc) {
96 hdf5_orbit_writeset(group, "ids", "1", dtypei32, arraylength, 1,
97 mask, datasize, &orbits[arraylength*0]);
98 hdf5_orbit_writeset(group, "mileage", "s", dtypef64, arraylength, 1,
99 mask, datasize, &orbits[arraylength*1]);
100 hdf5_orbit_writeset(group, "r", "m", dtypef64, arraylength, 1,
101 mask, datasize, &orbits[arraylength*2]);
102 hdf5_orbit_writeset(group, "phi", "deg", dtypef64, arraylength,
103 180.0/CONST_PI,
104 mask, datasize, &orbits[arraylength*3]);
105 hdf5_orbit_writeset(group, "z", "m", dtypef64, arraylength, 1,
106 mask, datasize, &orbits[arraylength*4]);
107 hdf5_orbit_writeset(group, "ppar", "kg*m/s", dtypef64, arraylength, 1,
108 mask, datasize, &orbits[arraylength*5]);
109 hdf5_orbit_writeset(group, "mu", "eV/T", dtypef64, arraylength,
110 1.0/CONST_E,
111 mask, datasize, &orbits[arraylength*6]);
112 hdf5_orbit_writeset(group, "zeta", "rad", dtypef64, arraylength, 1,
113 mask, datasize, &orbits[arraylength*7]);
114 hdf5_orbit_writeset(group, "weight", "1", dtypef64, arraylength, 1,
115 mask, datasize, &orbits[arraylength*8]);
116 hdf5_orbit_writeset(group, "charge", "e", dtypei16, arraylength,
117 1.0/CONST_E,
118 mask, datasize, &orbits[arraylength*9]);
119 hdf5_orbit_writeset(group, "rho", "1", dtypef64, arraylength, 1,
120 mask, datasize, &orbits[arraylength*10]);
121 hdf5_orbit_writeset(group, "theta", "deg", dtypef64, arraylength,
122 180.0/CONST_PI,
123 mask, datasize, &orbits[arraylength*11]);
124 hdf5_orbit_writeset(group, "br", "T", dtypef64, arraylength, 1,
125 mask, datasize, &orbits[arraylength*12]);
126 hdf5_orbit_writeset(group, "bphi", "T", dtypef64, arraylength, 1,
127 mask, datasize, &orbits[arraylength*13]);
128 hdf5_orbit_writeset(group, "bz", "T", dtypef64, arraylength, 1,
129 mask, datasize, &orbits[arraylength*14]);
130 hdf5_orbit_writeset(group, "simmode", "1", dtypei32, arraylength, 1,
131 mask, datasize, &orbits[arraylength*15]);
132 }
133
134 if(data->record_mode == simulate_mode_ml) {
135 hdf5_orbit_writeset(group, "ids", "1", dtypei32, arraylength, 1,
136 mask, datasize, &orbits[arraylength*0]);
137 hdf5_orbit_writeset(group, "mileage", "s", dtypef64, arraylength, 1,
138 mask, datasize, &orbits[arraylength*1]);
139 hdf5_orbit_writeset(group, "r", "m", dtypef64, arraylength, 1,
140 mask, datasize, &orbits[arraylength*2]);
141 hdf5_orbit_writeset(group, "phi", "deg", dtypef64, arraylength,
142 180.0/CONST_PI,
143 mask, datasize, &orbits[arraylength*3]);
144 hdf5_orbit_writeset(group, "z", "m", dtypef64, arraylength, 1,
145 mask, datasize, &orbits[arraylength*4]);
146 hdf5_orbit_writeset(group, "rho", "1", dtypef64, arraylength, 1,
147 mask, datasize, &orbits[arraylength*5]);
148 hdf5_orbit_writeset(group, "theta", "deg", dtypef64, arraylength,
149 180.0/CONST_PI,
150 mask, datasize, &orbits[arraylength*6]);
151 hdf5_orbit_writeset(group, "br", "T", dtypef64, arraylength, 1,
152 mask, datasize, &orbits[arraylength*7]);
153 hdf5_orbit_writeset(group, "bphi", "T", dtypef64, arraylength, 1,
154 mask, datasize, &orbits[arraylength*8]);
155 hdf5_orbit_writeset(group, "bz", "T", dtypef64, arraylength, 1,
156 mask, datasize, &orbits[arraylength*9]);
157 hdf5_orbit_writeset(group, "simmode", "1", dtypei32, arraylength, 1,
158 mask, datasize, &orbits[arraylength*10]);
159 }
160
161 if(data->record_mode == simulate_mode_hybrid) {
162 hdf5_orbit_writeset(group, "ids", "1", dtypei32, arraylength, 1,
163 mask, datasize, &orbits[arraylength*0]);
164 hdf5_orbit_writeset(group, "mileage", "s", dtypef64, arraylength, 1,
165 mask, datasize, &orbits[arraylength*1]);
166 hdf5_orbit_writeset(group, "r", "m", dtypef64, arraylength, 1,
167 mask, datasize, &orbits[arraylength*2]);
168 hdf5_orbit_writeset(group, "phi", "deg", dtypef64, arraylength,
169 180.0/CONST_PI,
170 mask, datasize, &orbits[arraylength*3]);
171 hdf5_orbit_writeset(group, "z", "m", dtypef64, arraylength, 1,
172 mask, datasize, &orbits[arraylength*4]);
173 hdf5_orbit_writeset(group, "pr", "kg*m/s", dtypef64, arraylength, 1,
174 mask, datasize, &orbits[arraylength*5]);
175 hdf5_orbit_writeset(group, "pphi", "kg*m/s", dtypef64, arraylength, 1,
176 mask, datasize, &orbits[arraylength*6]);
177 hdf5_orbit_writeset(group, "pz", "kg*m/s", dtypef64, arraylength, 1,
178 mask, datasize, &orbits[arraylength*7]);
179 hdf5_orbit_writeset(group, "ppar", "kg*m/s", dtypef64, arraylength, 1,
180 mask, datasize, &orbits[arraylength*8]);
181 hdf5_orbit_writeset(group, "mu", "eV/T", dtypef64, arraylength,
182 1.0/CONST_E,
183 mask, datasize, &orbits[arraylength*9]);
184 hdf5_orbit_writeset(group, "zeta", "rad", dtypef64, arraylength, 1,
185 mask, datasize, &orbits[arraylength*10]);
186 hdf5_orbit_writeset(group, "weight", "1", dtypef64, arraylength, 1,
187 mask, datasize, &orbits[arraylength*11]);
188 hdf5_orbit_writeset(group, "charge", "e", dtypei16, arraylength,
189 1.0/CONST_E,
190 mask, datasize, &orbits[arraylength*12]);
191 hdf5_orbit_writeset(group, "rho", "1", dtypef64, arraylength, 1,
192 mask, datasize, &orbits[arraylength*13]);
193 hdf5_orbit_writeset(group, "theta", "deg", dtypef64, arraylength,
194 180.0/CONST_PI,
195 mask, datasize, &orbits[arraylength*14]);
196 hdf5_orbit_writeset(group, "br", "T", dtypef64, arraylength, 1,
197 mask, datasize, &orbits[arraylength*15]);
198 hdf5_orbit_writeset(group, "bphi", "T", dtypef64, arraylength, 1,
199 mask, datasize, &orbits[arraylength*16]);
200 hdf5_orbit_writeset(group, "bz", "T", dtypef64, arraylength, 1,
201 mask, datasize, &orbits[arraylength*17]);
202 hdf5_orbit_writeset(group, "simmode", "1", dtypei32, arraylength, 1,
203 mask, datasize, &orbits[arraylength*18]);
204
205 }
206
207 if(data->mode == DIAG_ORB_POINCARE) {
208 hdf5_orbit_writeset(group, "pncrid", "1", dtypei16, arraylength, 1,
209 mask, datasize, &orbits[arraylength * data->Nfld]);
210 hdf5_orbit_writeset(group, "pncrdi", "1", dtypei16, arraylength, 1,
211 mask, datasize, &orbits[arraylength * (data->Nfld+1)]);
212 }
213
214 free(mask);
215 H5Gclose (group);
216
217 return 0;
218}
219
233void hdf5_orbit_writeset(hid_t group, const char* name, const char* unit,
234 int type, int arraylength, real confac,
235 integer* mask, integer size, real* orbits) {
236 if(type == 0) {
237 real* data = (real*) malloc(size * sizeof(real));
238 integer j = 0;
239 for(integer i = 0; i < arraylength; i++) {
240 if(mask[i]) {
241 data[j] = confac*orbits[i];
242 j++;
243 }
244 }
245 hdf5_write_extendible_dataset_double(group, name, size, data);
246 H5LTset_attribute_string(group, name, "unit", unit);
247 free(data);
248 }
249 else if(type == 1) {
250 int* data = (int*) malloc(size * sizeof(int));
251 integer j = 0;
252 for(integer i = 0; i < arraylength; i++) {
253 if(mask[i]) {
254 data[j] = (int)(confac*orbits[i]);
255 j++;
256 }
257 }
258 hdf5_write_extendible_dataset_int(group, name, size, data);
259 H5LTset_attribute_string(group, name, "unit", unit);
260 free(data);
261 }
262 else if(type == 2) {
263 integer* data = (integer*) malloc(size * sizeof(integer));
264 integer j = 0;
265 for(integer i = 0; i < arraylength; i++) {
266 if(mask[i]) {
267 data[j] = (integer)(confac*orbits[i]);
268 j++;
269 }
270 }
271 hdf5_write_extendible_dataset_long(group, name, size, data);
272 H5LTset_attribute_string(group, name, "unit", unit);
273 free(data);
274 }
275}
Main header file for ASCOT5.
double real
Definition ascot5.h:85
long integer
Definition ascot5.h:84
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
Header file for diag_orb.c.
#define DIAG_ORB_POINCARE
Definition diag_orb.h:13
herr_t hdf5_write_extendible_dataset_long(hid_t group, const char *datasetname, int length, long *data)
Create and write to an extendible dataset for long data.
herr_t hdf5_write_extendible_dataset_int(hid_t group, const char *datasetname, int length, int *data)
Create and write to an extendible dataset int data.
herr_t hdf5_write_extendible_dataset_double(hid_t group, const char *datasetname, int length, double *data)
Create and write to an extendible dataset for double data.
Header file for hdf5_helpers.h.
void hdf5_orbit_writeset(hid_t group, const char *name, const char *unit, int type, int arraylength, real confac, integer *mask, integer size, real *data)
Helper function for writing orbit diagnostic datasets.
Definition hdf5_orbit.c:233
int hdf5_orbit_write(hid_t f, char *path, diag_orb_offload_data *data, real *orbits)
Write orbit diagnostics data to a HDF5 file.
Definition hdf5_orbit.c:34
Header file for hdf5_orbit.c.
Header file for math.c.
Header file for particle.c.
Header file for simulate.c.
@ simulate_mode_fo
Definition simulate.h:34
@ simulate_mode_ml
Definition simulate.h:45
@ simulate_mode_gc
Definition simulate.h:37
@ simulate_mode_hybrid
Definition simulate.h:42
Orbit diagnostics offload data struct.
Definition diag_orb.h:35