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 int* mask, integer size, real* data);
22
32int hdf5_orbit_write(hid_t f, char* path, diag_orb_data* data) {
33 hid_t group = H5Gcreate2(f, path, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
34 if(group < 0) {
35 return 1;
36 }
37
38 int arraylength = data->Nmrk*data->Npnt;
39 int datasize = 0;
40 int* mask = malloc(arraylength*sizeof(int));
41 for(int i=0; i < arraylength; i++) {
42 mask[i] = data->id[i] > 0.1;
43 if(mask[i]) {
44 datasize++;
45 }
46 }
47
48 int dtypef64 = 0;
49 int dtypei32 = 1;
50 int dtypei16 = 2;
51 if(data->record_mode == simulate_mode_fo) {
52
53 hdf5_orbit_writeset(group, "ids", "1", dtypei32, arraylength, 1,
54 mask, datasize, data->id);
55 hdf5_orbit_writeset(group, "mileage", "s", dtypef64, arraylength, 1,
56 mask, datasize, data->mileage);
57 hdf5_orbit_writeset(group, "r", "m", dtypef64, arraylength, 1,
58 mask, datasize, data->r);
59 hdf5_orbit_writeset(group, "phi", "deg", dtypef64, arraylength,
60 180.0/CONST_PI, mask, datasize, data->phi);
61 hdf5_orbit_writeset(group, "z", "m", dtypef64, arraylength, 1,
62 mask, datasize, data->z);
63 hdf5_orbit_writeset(group, "pr", "kg*m/s", dtypef64, arraylength, 1,
64 mask, datasize, data->p_r);
65 hdf5_orbit_writeset(group, "pphi", "kg*m/s", dtypef64, arraylength, 1,
66 mask, datasize, data->p_phi);
67 hdf5_orbit_writeset(group, "pz", "kg*m/s", dtypef64, arraylength, 1,
68 mask, datasize, data->p_z);
69 hdf5_orbit_writeset(group, "weight", "1", dtypef64, arraylength, 1,
70 mask, datasize, data->weight);
71 hdf5_orbit_writeset(group, "charge", "e", dtypei16, arraylength,
72 1.0/CONST_E, mask, datasize, data->charge);
73 hdf5_orbit_writeset(group, "rho", "1", dtypef64, arraylength, 1,
74 mask, datasize, data->rho);
75 hdf5_orbit_writeset(group, "theta", "deg", dtypef64, arraylength,
76 180.0/CONST_PI, mask, datasize, data->theta);
77 hdf5_orbit_writeset(group, "br", "T", dtypef64, arraylength, 1,
78 mask, datasize, data->B_r);
79 hdf5_orbit_writeset(group, "bphi", "T", dtypef64, arraylength, 1,
80 mask, datasize, data->B_phi);
81 hdf5_orbit_writeset(group, "bz", "T", dtypef64, arraylength, 1,
82 mask, datasize, data->B_z);
83 hdf5_orbit_writeset(group, "simmode", "1", dtypei32, arraylength, 1,
84 mask, datasize, data->simmode);
85
86 }
87
88 if(data->record_mode == simulate_mode_gc) {
89 hdf5_orbit_writeset(group, "ids", "1", dtypei32, arraylength, 1,
90 mask, datasize, data->id);
91 hdf5_orbit_writeset(group, "mileage", "s", dtypef64, arraylength, 1,
92 mask, datasize, data->mileage);
93 hdf5_orbit_writeset(group, "r", "m", dtypef64, arraylength, 1,
94 mask, datasize, data->r);
95 hdf5_orbit_writeset(group, "phi", "deg", dtypef64, arraylength,
96 180.0/CONST_PI, mask, datasize, data->phi);
97 hdf5_orbit_writeset(group, "z", "m", dtypef64, arraylength, 1,
98 mask, datasize, data->z);
99 hdf5_orbit_writeset(group, "ppar", "kg*m/s", dtypef64, arraylength, 1,
100 mask, datasize, data->ppar);
101 hdf5_orbit_writeset(group, "mu", "eV/T", dtypef64, arraylength,
102 1.0/CONST_E, mask, datasize, data->mu);
103 hdf5_orbit_writeset(group, "zeta", "rad", dtypef64, arraylength, 1,
104 mask, datasize, data->zeta);
105 hdf5_orbit_writeset(group, "weight", "1", dtypef64, arraylength, 1,
106 mask, datasize, data->weight);
107 hdf5_orbit_writeset(group, "charge", "e", dtypei16, arraylength,
108 1.0/CONST_E, mask, datasize, data->charge);
109 hdf5_orbit_writeset(group, "rho", "1", dtypef64, arraylength, 1,
110 mask, datasize, data->rho);
111 hdf5_orbit_writeset(group, "theta", "deg", dtypef64, arraylength,
112 180.0/CONST_PI, mask, datasize, data->theta);
113 hdf5_orbit_writeset(group, "br", "T", dtypef64, arraylength, 1,
114 mask, datasize, data->B_r);
115 hdf5_orbit_writeset(group, "bphi", "T", dtypef64, arraylength, 1,
116 mask, datasize, data->B_phi);
117 hdf5_orbit_writeset(group, "bz", "T", dtypef64, arraylength, 1,
118 mask, datasize, data->B_z);
119 hdf5_orbit_writeset(group, "simmode", "1", dtypei32, arraylength, 1,
120 mask, datasize, data->simmode);
121 }
122
123 if(data->record_mode == simulate_mode_ml) {
124 hdf5_orbit_writeset(group, "ids", "1", dtypei32, arraylength, 1,
125 mask, datasize, data->id);
126 hdf5_orbit_writeset(group, "mileage", "s", dtypef64, arraylength, 1,
127 mask, datasize, data->mileage);
128 hdf5_orbit_writeset(group, "r", "m", dtypef64, arraylength, 1,
129 mask, datasize, data->r);
130 hdf5_orbit_writeset(group, "phi", "deg", dtypef64, arraylength,
131 180.0/CONST_PI, mask, datasize, data->phi);
132 hdf5_orbit_writeset(group, "z", "m", dtypef64, arraylength, 1,
133 mask, datasize, data->z);
134 hdf5_orbit_writeset(group, "rho", "1", dtypef64, arraylength, 1,
135 mask, datasize, data->rho);
136 hdf5_orbit_writeset(group, "theta", "deg", dtypef64, arraylength,
137 180.0/CONST_PI, mask, datasize, data->theta);
138 hdf5_orbit_writeset(group, "br", "T", dtypef64, arraylength, 1,
139 mask, datasize, data->B_r);
140 hdf5_orbit_writeset(group, "bphi", "T", dtypef64, arraylength, 1,
141 mask, datasize, data->B_phi);
142 hdf5_orbit_writeset(group, "bz", "T", dtypef64, arraylength, 1,
143 mask, datasize, data->B_z);
144 hdf5_orbit_writeset(group, "simmode", "1", dtypei32, arraylength, 1,
145 mask, datasize, data->simmode);
146 }
147
148 if(data->record_mode == simulate_mode_hybrid) {
149 hdf5_orbit_writeset(group, "ids", "1", dtypei32, arraylength, 1,
150 mask, datasize, data->id);
151 hdf5_orbit_writeset(group, "mileage", "s", dtypef64, arraylength, 1,
152 mask, datasize, data->mileage);
153 hdf5_orbit_writeset(group, "r", "m", dtypef64, arraylength, 1,
154 mask, datasize, data->r);
155 hdf5_orbit_writeset(group, "phi", "deg", dtypef64, arraylength,
156 180.0/CONST_PI, mask, datasize, data->phi);
157 hdf5_orbit_writeset(group, "z", "m", dtypef64, arraylength, 1,
158 mask, datasize, data->z);
159 hdf5_orbit_writeset(group, "pr", "kg*m/s", dtypef64, arraylength, 1,
160 mask, datasize, data->p_r);
161 hdf5_orbit_writeset(group, "pphi", "kg*m/s", dtypef64, arraylength, 1,
162 mask, datasize, data->p_phi);
163 hdf5_orbit_writeset(group, "pz", "kg*m/s", dtypef64, arraylength, 1,
164 mask, datasize, data->p_z);
165 hdf5_orbit_writeset(group, "ppar", "kg*m/s", dtypef64, arraylength, 1,
166 mask, datasize, data->ppar);
167 hdf5_orbit_writeset(group, "mu", "eV/T", dtypef64, arraylength,
168 1.0/CONST_E, mask, datasize, data->mu);
169 hdf5_orbit_writeset(group, "zeta", "rad", dtypef64, arraylength, 1,
170 mask, datasize, data->zeta);
171 hdf5_orbit_writeset(group, "weight", "1", dtypef64, arraylength, 1,
172 mask, datasize, data->weight);
173 hdf5_orbit_writeset(group, "charge", "e", dtypei16, arraylength,
174 1.0/CONST_E, mask, datasize, data->charge);
175 hdf5_orbit_writeset(group, "rho", "1", dtypef64, arraylength, 1,
176 mask, datasize, data->rho);
177 hdf5_orbit_writeset(group, "theta", "deg", dtypef64, arraylength,
178 180.0/CONST_PI, mask, datasize, data->theta);
179 hdf5_orbit_writeset(group, "br", "T", dtypef64, arraylength, 1,
180 mask, datasize, data->B_r);
181 hdf5_orbit_writeset(group, "bphi", "T", dtypef64, arraylength, 1,
182 mask, datasize, data->B_phi);
183 hdf5_orbit_writeset(group, "bz", "T", dtypef64, arraylength, 1,
184 mask, datasize, data->B_z);
185 hdf5_orbit_writeset(group, "simmode", "1", dtypei32, arraylength, 1,
186 mask, datasize, data->simmode);
187
188 }
189
190 if(data->mode == DIAG_ORB_POINCARE) {
191 hdf5_orbit_writeset(group, "pncrid", "1", dtypei16, arraylength, 1,
192 mask, datasize, data->pncrid);
193 hdf5_orbit_writeset(group, "pncrdi", "1", dtypei16, arraylength, 1,
194 mask, datasize, data->pncrdi);
195 }
196
197 free(mask);
198 H5Gclose (group);
199
200 return 0;
201}
202
216void hdf5_orbit_writeset(hid_t group, const char* name, const char* unit,
217 int type, int arraylength, real confac,
218 int* mask, integer size, real* orbits) {
219 if(type == 0) {
220 real* data = (real*) malloc(size * sizeof(real));
221 int j = 0;
222 for(int i = 0; i < arraylength; i++) {
223 if(mask[i]) {
224 data[j] = confac*orbits[i];
225 j++;
226 }
227 }
228 hdf5_write_extendible_dataset_double(group, name, size, data);
229 H5LTset_attribute_string(group, name, "unit", unit);
230 free(data);
231 }
232 else if(type == 1) {
233 int* data = (int*) malloc(size * sizeof(int));
234 integer j = 0;
235 for(integer i = 0; i < arraylength; i++) {
236 if(mask[i]) {
237 data[j] = (int)(confac*orbits[i]);
238 j++;
239 }
240 }
241 hdf5_write_extendible_dataset_int(group, name, size, data);
242 H5LTset_attribute_string(group, name, "unit", unit);
243 free(data);
244 }
245 else if(type == 2) {
246 integer* data = (integer*) malloc(size * sizeof(integer));
247 integer j = 0;
248 for(integer i = 0; i < arraylength; i++) {
249 if(mask[i]) {
250 data[j] = (integer)(confac*orbits[i]);
251 j++;
252 }
253 }
254 hdf5_write_extendible_dataset_long(group, name, size, data);
255 H5LTset_attribute_string(group, name, "unit", unit);
256 free(data);
257 }
258}
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, int *mask, integer size, real *data)
Helper function for writing orbit diagnostic datasets.
Definition hdf5_orbit.c:216
int hdf5_orbit_write(hid_t f, char *path, diag_orb_data *data)
Write orbit diagnostics data to a HDF5 file.
Definition hdf5_orbit.c:32
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:33
@ simulate_mode_ml
Definition simulate.h:44
@ simulate_mode_gc
Definition simulate.h:36
@ simulate_mode_hybrid
Definition simulate.h:41
Orbit diagnostics data struct.
Definition diag_orb.h:42
real * mileage
Definition diag_orb.h:44
real * ppar
Definition diag_orb.h:51
real * simmode
Definition diag_orb.h:61
real * B_r
Definition diag_orb.h:58
real * pncrdi
Definition diag_orb.h:63
real * zeta
Definition diag_orb.h:53
real * p_phi
Definition diag_orb.h:49
real * theta
Definition diag_orb.h:57
real * rho
Definition diag_orb.h:56
real * pncrid
Definition diag_orb.h:62
real * weight
Definition diag_orb.h:54
real * B_z
Definition diag_orb.h:60
real * phi
Definition diag_orb.h:46
real * mu
Definition diag_orb.h:52
real * p_z
Definition diag_orb.h:50
int record_mode
Definition diag_orb.h:76
real * charge
Definition diag_orb.h:55
real * id
Definition diag_orb.h:43
real * p_r
Definition diag_orb.h:48
real * B_phi
Definition diag_orb.h:59