ASCOT5
Loading...
Searching...
No Matches
hdf5_histogram.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 "../consts.h"
10#include "../diag/hist.h"
11#include "hdf5_histogram.h"
12
16int hdf5_hist_write(hid_t f, char* path, histogram* hist) {
17 int ordinate_dim = 1;
18 int abscissa_dim = 0;
19 for(int i = 0; i < HIST_ALLDIM; i++) {
20 abscissa_dim += hist->axes[i].n ? 1 : 0;
21 }
22
23 int* abscissa_n_slots = (int*) malloc(abscissa_dim * sizeof(int));
24 double* abscissa_min = (double*) malloc(abscissa_dim * sizeof(double));
25 double* abscissa_max = (double*) malloc(abscissa_dim * sizeof(double));
26 char** abscissa_names = (char**) malloc(abscissa_dim * sizeof(char*));
27 char** abscissa_units = (char**) malloc(abscissa_dim * sizeof(char*));
28
29 int k = 0;
30 for(int i = 0; i < HIST_ALLDIM; i++) {
31 if(!hist->axes[i].n) {
32 continue;
33 }
34 abscissa_min[k] = hist->axes[i].min;
35 abscissa_max[k] = hist->axes[i].max;
36 abscissa_n_slots[k] = hist->axes[i].n;
37
38 switch (hist->axes[i].name)
39 {
40 case R:
41 abscissa_names[k] = "r";
42 abscissa_units[k] = "m";
43 break;
44 case PHI:
45 abscissa_names[k] = "phi";
46 abscissa_units[k] = "deg";
47 abscissa_min[k] *= 180.0/CONST_PI;
48 abscissa_max[k] *= 180.0/CONST_PI;
49 break;
50 case Z:
51 abscissa_names[k] = "z";
52 abscissa_units[k] = "m";
53 break;
54 case RHO:
55 abscissa_names[k] = "rho";
56 abscissa_units[k] = "1";
57 break;
58 case THETA:
59 abscissa_names[k] = "theta";
60 abscissa_units[k] = "deg";
61 break;
62 case PPAR:
63 abscissa_names[k] = "ppar";
64 abscissa_units[k] = "kg*m/s";
65 break;
66 case PPERP:
67 abscissa_names[k] = "pperp";
68 abscissa_units[k] = "kg*m/s";
69 break;
70 case PR:
71 abscissa_names[k] = "pr";
72 abscissa_units[k] = "kg*m/s";
73 break;
74 case PPHI:
75 abscissa_names[k] = "pphi";
76 abscissa_units[k] = "kg*m/s";
77 break;
78 case PZ:
79 abscissa_names[k] = "pz";
80 abscissa_units[k] = "kg*m/s";
81 break;
82 case EKIN:
83 abscissa_names[k] = "ekin";
84 abscissa_units[k] = "eV";
85 abscissa_min[k] /= CONST_E;
86 abscissa_max[k] /= CONST_E;
87 break;
88 case XI:
89 abscissa_names[k] = "pitch";
90 abscissa_units[k] = "1";
91 break;
92 case MU:
93 abscissa_names[k] = "mu";
94 abscissa_units[k] = "T/eV";
95 break;
96 case PTOR:
97 abscissa_names[k] = "ptor";
98 abscissa_units[k] = "kg*m/s";
99 break;
100 case TIME:
101 abscissa_names[k] = "time";
102 abscissa_units[k] = "s";
103 break;
104 case CHARGE:
105 abscissa_names[k] = "charge";
106 abscissa_units[k] = "e";
107 break;
108 }
109 k++;
110 }
111 char* ordinate_names[] = { "distribution" };
112 char* ordinate_units[] = { "s/(m^5*kg^2*deg*e)" };
113
114 /* Create a group for this distribution and write the data in it */
116 f, path, abscissa_dim, ordinate_dim, abscissa_n_slots, abscissa_min,
117 abscissa_max, abscissa_units, abscissa_names, ordinate_units,
118 ordinate_names, hist->bins);
119 free(abscissa_n_slots);
120 free(abscissa_min);
121 free(abscissa_max);
122 free(abscissa_names);
123 free(abscissa_units);
124
125 return retval;
126}
127
160 hid_t f, const char *path, int abscissaDim, int ordinateDim,
161 int *abscissaNslots, double *abscissaMin, double *abscissaMax,
162 char **abscissaUnits, char **abscissaNames,
163 char **ordinateUnits, char **ordinateNames, double *ordinate) {
164
165 char temppath[256]; /* Helper variable */
166
167 /* Create histogram group */
168 hid_t histogram = H5Gcreate2(f, path, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
169 if(histogram < 0) {
170 return 1;
171 }
172
173 /* Dimensions of ordinate data to be written */
174 hsize_t dims[100];
175 dims[0] = ordinateDim;
176 for (int i=0; i<abscissaDim; i++) {
177 dims[i+1] = abscissaNslots[i];
178 }
179
180 /* Write ordinate including its names and units */
181 herr_t err;
182 err = H5LTmake_dataset_double(histogram , "ordinate", abscissaDim+1,
183 dims, ordinate);
184 if(err){
185 H5Gclose(histogram);
186 return err;
187 }
188 for(int i=0; i<ordinateDim; i++) {
189 sprintf(temppath, "name_%02d", i);
190 H5LTset_attribute_string(histogram, "ordinate", temppath,
191 ordinateNames[i]);
192 sprintf(temppath, "unit_%02d", i);
193 H5LTset_attribute_string(histogram, "ordinate", temppath,
194 ordinateUnits[i]);
195 }
196
197
198 /* Write ordinate and abscissa dimensions */
199 hsize_t dimsize = 1;
200 err = H5LTmake_dataset_int(histogram , "ordinate_ndim", 1, &dimsize,
201 &ordinateDim);
202 if(err){
203 H5Gclose(histogram);
204 return 1;
205 }
206 err = H5LTmake_dataset_int(histogram , "abscissa_ndim", 1, &dimsize,
207 &abscissaDim);
208 if(err){
209 H5Gclose(histogram);
210 return 1;
211 }
212
213 /* Write abscissae */
214 for (int i=0; i<abscissaDim; i++) {
215
216 double* abscissavec = (double *) malloc( (dims[i+1]+1)*sizeof(double) );
217 for(int j=0; j<dims[i+1]+1; j++) {
218 abscissavec[j] =
219 abscissaMin[i] + j * ( (abscissaMax[i] - abscissaMin[i])
220 / abscissaNslots[i] );
221 }
222
223 /* Vector */
224 char abscissapath[256];
225 sprintf(abscissapath, "abscissa_vec_%02d",i+1);
226 hsize_t abscissasize[1] = {abscissaNslots[i]+1};
227 err = H5LTmake_dataset_double(histogram , abscissapath, 1, abscissasize,
228 abscissavec);
229 free(abscissavec);
230 if(err){
231 H5Gclose(histogram);
232 return 1;
233 }
234
235 /* Vector length */
236 sprintf(temppath, "abscissa_nbin_%02d", i+1);
237 err = H5LTmake_dataset_int(histogram, temppath, 1, &dimsize,
238 &(abscissaNslots[i]));
239 if(err){
240 H5Gclose(histogram);
241 return 1;
242 }
243
244 /* Name and unit */
245 sprintf(temppath, "name_%02d", i);
246 H5LTset_attribute_string(histogram, abscissapath, temppath,
247 abscissaNames[i]);
248 sprintf(temppath, "unit_%02d", i);
249 H5LTset_attribute_string(histogram, abscissapath, temppath,
250 abscissaUnits[i]);
251
252 }
253
254 H5Gclose(histogram);
255
256 return 0;
257}
Header file containing physical and mathematical constants.
#define CONST_PI
pi
Definition consts.h:11
#define CONST_E
Elementary charge [C].
Definition consts.h:35
int hdf5_histogram_write_uniform_double(hid_t f, const char *path, int abscissaDim, int ordinateDim, int *abscissaNslots, double *abscissaMin, double *abscissaMax, char **abscissaUnits, char **abscissaNames, char **ordinateUnits, char **ordinateNames, double *ordinate)
Write a histogram with uniform grid to HDF5 file.
int hdf5_hist_write(hid_t f, char *path, histogram *hist)
Write a histogram object to HDF5 file.
Header file for hdf5_histogram.c.
Header file for hist.c.
@ R
Definition hist.h:18
@ XI
Definition hist.h:29
@ THETA
Definition hist.h:22
@ PZ
Definition hist.h:27
@ PPHI
Definition hist.h:26
@ RHO
Definition hist.h:21
@ PHI
Definition hist.h:19
@ PPERP
Definition hist.h:24
@ PR
Definition hist.h:25
@ MU
Definition hist.h:30
@ PPAR
Definition hist.h:23
@ Z
Definition hist.h:20
@ EKIN
Definition hist.h:28
@ PTOR
Definition hist.h:31
@ TIME
Definition hist.h:32
@ CHARGE
Definition hist.h:33
real min
Definition hist.h:41
real max
Definition hist.h:42
hist_coordinate name
Definition hist.h:40
size_t n
Definition hist.h:43
Histogram parameters.
Definition hist.h:52
hist_axis axes[HIST_ALLDIM]
Definition hist.h:53
real * bins
Definition hist.h:56