ASCOT5
Loading...
Searching...
No Matches
hdf5_state.c
Go to the documentation of this file.
1
5#include <string.h>
6#include <stdlib.h>
7#include <hdf5.h>
8#include <hdf5_hl.h>
9#include "../ascot5.h"
10#include "../consts.h"
11#include "../physlib.h"
12#include "../particle.h"
13#include "hdf5_helpers.h"
14#include "hdf5_state.h"
15#ifdef TRAP_FPE
16#include <fenv.h>
17#endif
36int hdf5_state_write(hid_t f, char* run, char* state, integer n,
37 particle_state* p) {
38
39 char path[256];
40 sprintf(path, "%s%s", run, state);
41
42 hid_t state_group = H5Gcreate2(f, path, H5P_DEFAULT, H5P_DEFAULT,
43 H5P_DEFAULT);
44 if(state_group < 0) {
45 return 1;
46 }
47
48 real* data = (real*) malloc(n * sizeof(real));
49
50 /* Particle coordinates */
51 integer i;
52 for(i = 0; i < n; i++) {
53 data[i] = p[i].rprt;
54 }
55 hdf5_write_extendible_dataset_double(state_group, "rprt", n, data);
56 H5LTset_attribute_string(state_group, "rprt", "unit", "m");
57
58 for(i = 0; i < n; i++) {
59 data[i] = p[i].phiprt*(180/CONST_PI);
60 }
61 hdf5_write_extendible_dataset_double(state_group, "phiprt", n, data);
62 H5LTset_attribute_string(state_group, "phiprt", "unit", "deg");
63
64 for(i = 0; i < n; i++) {
65 data[i] = p[i].zprt;
66 }
67 hdf5_write_extendible_dataset_double(state_group, "zprt", n, data);
68 H5LTset_attribute_string(state_group, "zprt", "unit", "m");
69
70 for(i = 0; i < n; i++) {
71 data[i] = p[i].p_r;
72 }
73 hdf5_write_extendible_dataset_double(state_group, "prprt", n, data);
74 H5LTset_attribute_string(state_group, "prprt", "unit", "kg*m/s");
75
76 for(i = 0; i < n; i++) {
77 data[i] = p[i].p_phi;
78 }
79 hdf5_write_extendible_dataset_double(state_group, "pphiprt", n, data);
80 H5LTset_attribute_string(state_group, "pphiprt", "unit", "kg*m/s");
81
82 for(i = 0; i < n; i++) {
83 data[i] = p[i].p_z;
84 }
85 hdf5_write_extendible_dataset_double(state_group, "pzprt", n, data);
86 H5LTset_attribute_string(state_group, "pzprt", "unit", "kg*m/s");
87
88 /* Guiding center coordinates */
89 for(i = 0; i < n; i++) {
90 data[i] = p[i].r;
91 }
92 hdf5_write_extendible_dataset_double(state_group, "r", n, data);
93 H5LTset_attribute_string(state_group, "r", "unit", "m");
94
95 for(i = 0; i < n; i++) {
96 data[i] = p[i].phi*(180/CONST_PI);
97 }
98 hdf5_write_extendible_dataset_double(state_group, "phi", n, data);
99 H5LTset_attribute_string(state_group, "phi", "unit", "deg");
100
101 for(i = 0; i < n; i++) {
102 data[i] = p[i].z;
103 }
104 hdf5_write_extendible_dataset_double(state_group, "z", n, data);
105 H5LTset_attribute_string(state_group, "z", "unit", "m");
106
107 for(i = 0; i < n; i++) {
108 data[i] = p[i].ppar;
109 }
110 hdf5_write_extendible_dataset_double(state_group, "ppar", n, data);
111 H5LTset_attribute_string(state_group, "ppar", "unit", "kg*m/s");
112
113 for(i = 0; i < n; i++) {
114 data[i] = p[i].mu/CONST_E;
115 }
116 hdf5_write_extendible_dataset_double(state_group, "mu", n, data);
117 H5LTset_attribute_string(state_group, "mu", "unit", "eV/T");
118
119 for(i = 0; i < n; i++) {
120 data[i] = p[i].zeta;
121 }
122 hdf5_write_extendible_dataset_double(state_group, "zeta", n, data);
123 H5LTset_attribute_string(state_group, "zeta", "unit", "rad");
124
125 /* Common */
126 for(i = 0; i < n; i++) {
127 data[i] = p[i].weight;
128 }
129 hdf5_write_extendible_dataset_double(state_group, "weight", n, data);
130 H5LTset_attribute_string(state_group, "weight", "unit", "markers/s");
131
132 for(i = 0; i < n; i++) {
133 data[i] = p[i].time;
134 }
135 hdf5_write_extendible_dataset_double(state_group, "time", n, data);
136 H5LTset_attribute_string(state_group, "time", "unit", "s");
137
138 for(i = 0; i < n; i++) {
139 data[i] = p[i].mileage;
140 }
141 hdf5_write_extendible_dataset_double(state_group, "mileage", n, data);
142 H5LTset_attribute_string(state_group, "mileage", "unit", "s");
143
144 for(i = 0; i < n; i++) {
145 data[i] = p[i].cputime;
146 }
147 hdf5_write_extendible_dataset_double(state_group, "cputime", n, data);
148 H5LTset_attribute_string(state_group, "cputime", "unit", "s");
149
150 for(i = 0; i < n; i++) {
151 data[i] = p[i].rho;
152 }
153 hdf5_write_extendible_dataset_double(state_group, "rho", n, data);
154 H5LTset_attribute_string(state_group, "rho", "unit", "1");
155
156 for(i = 0; i < n; i++) {
157 data[i] = p[i].theta*(180/CONST_PI);
158 }
159 hdf5_write_extendible_dataset_double(state_group, "theta", n, data);
160 H5LTset_attribute_string(state_group, "theta", "unit", "deg");
161
162 for(i = 0; i < n; i++) {
163 data[i] = p[i].mass/CONST_U;
164 }
165 hdf5_write_extendible_dataset_double(state_group, "mass", n, data);
166 H5LTset_attribute_string(state_group, "mass", "unit", "amu");
167
168 /* Magnetic field */
169 for(i = 0; i < n; i++) {
170 data[i] = p[i].B_r;
171 }
172 hdf5_write_extendible_dataset_double(state_group, "br", n, data);
173 H5LTset_attribute_string(state_group, "br", "unit", "T");
174
175 for(i = 0; i < n; i++) {
176 data[i] = p[i].B_phi;
177 }
178 hdf5_write_extendible_dataset_double(state_group, "bphi", n, data);
179 H5LTset_attribute_string(state_group, "bphi", "unit", "T");
180
181 for(i = 0; i < n; i++) {
182 data[i] = p[i].B_z;
183 }
184 hdf5_write_extendible_dataset_double(state_group, "bz", n, data);
185 H5LTset_attribute_string(state_group, "bz", "unit", "T");
186
187 free(data);
188
189 /* Integer quantities */
190 integer* intdata = (integer*) malloc(n * sizeof(integer));
191
192 for(i = 0; i < n; i++) {
193 intdata[i] = p[i].id;
194 }
195 hdf5_write_extendible_dataset_long(state_group, "ids", n, intdata);
196 H5LTset_attribute_string(state_group, "ids", "unit", "1");
197
198 for(i = 0; i < n; i++) {
199 intdata[i] = p[i].endcond;
200 }
201 hdf5_write_extendible_dataset_long(state_group, "endcond", n, intdata);
202 H5LTset_attribute_string(state_group, "endcond", "unit", "1");
203
204 for(i = 0; i < n; i++) {
205 intdata[i] = p[i].walltile;
206 }
207 hdf5_write_extendible_dataset_long(state_group, "walltile", n, intdata);
208 H5LTset_attribute_string(state_group, "walltile", "unit", "1");
209
210 free(intdata);
211
212 int* intdata32 = (int*) malloc(n * sizeof(int));
213#ifdef TRAP_FPE
214 /* If there are errors in generating the markers, the data may be corrupt. We should ignore
215 * floating point exceptions here. */
216 fedisableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
217#endif
218 for(i = 0; i < n; i++) {
219 intdata32[i] = (int)round(p[i].charge/CONST_E);
220 }
221#ifdef TRAP_FPE
222 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
223#endif
224 hdf5_write_extendible_dataset_int(state_group, "charge", n, intdata32);
225 H5LTset_attribute_string(state_group, "charge", "unit", "e");
226
227 for(i = 0; i < n; i++) {
228 intdata32[i] = p[i].anum;
229 }
230 hdf5_write_extendible_dataset_int(state_group, "anum", n, intdata32);
231 H5LTset_attribute_string(state_group, "anum", "unit", "1");
232
233 for(i = 0; i < n; i++) {
234 intdata32[i] = p[i].znum;
235 }
236 hdf5_write_extendible_dataset_int(state_group, "znum", n, intdata32);
237 H5LTset_attribute_string(state_group, "znum", "unit", "1");
238
239 /* Error data */
240 int d1, d2;// Dummies
241 for(i = 0; i < n; i++) {
242 error_parse(p[i].err, &(intdata32[i]), &d1, &d2);
243 }
244 hdf5_write_extendible_dataset_int(state_group, "errormsg", n, intdata32);
245 H5LTset_attribute_string(state_group, "errormsg", "unit", "1");
246
247 for(i = 0; i < n; i++) {
248 error_parse(p[i].err, &d1, &(intdata32[i]), &d2);
249 }
250 hdf5_write_extendible_dataset_int(state_group, "errorline", n, intdata32);
251 H5LTset_attribute_string(state_group, "errorline", "unit", "1");
252
253 for(i = 0; i < n; i++) {
254 error_parse(p[i].err, &d1, &d2, &(intdata32[i]));
255 }
256 hdf5_write_extendible_dataset_int(state_group, "errormod", n, intdata32);
257 H5LTset_attribute_string(state_group, "errormod", "unit", "1");
258
259 free(intdata32);
260
261 H5Gclose(state_group);
262
263 return 0;
264}
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_U
Atomic mass unit in kilograms [kg]
Definition consts.h:29
#define CONST_PI
pi
Definition consts.h:11
#define CONST_E
Elementary charge [C]
Definition consts.h:32
void error_parse(a5err err, int *msg, int *line, int *file)
Retrieve stored data from the error flag.
Definition error.c:44
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.
int hdf5_state_write(hid_t f, char *run, char *state, integer n, particle_state *p)
Writes marker state to an ASCOT5 HDF5 file.
Definition hdf5_state.c:36
Header file for hdf5_state.c.
Header file for particle.c.
Methods to evaluate elementary physical quantities.
General representation of a marker.
Definition particle.h:40
integer id
Definition particle.h:63
integer walltile
Definition particle.h:65
integer endcond
Definition particle.h:64