Debian Codegraph @

src/read_data_flash_hdf5_utils.c

raw
1 /*
2 * This subroutine performs the calls to the HDF5 library for the FLASH
3 * tracer particles data read
4 *
5 * Easier to do it this way and link with c than to try to link against
6 * the Fortran interface (in the latter case the modules must
7 * have been compiled with the *exact* compiler used to compile splash
8 * which is a real pain).
9 *
10 */
11 #include <stdlib.h>
12 #include <assert.h>
13 #include <string.h>
14 #include <hdf5.h>
15 #include "hdf5_helper_utils.h"
16
17 void read_flash_data_fromc(int *icol,int *np,double temparr[*np], int id[*np]);
18
19 void read_flash_hdf5_header(char *filename, float *time, int *npart, int *ncol, int *ierr)
20 {
21 hid_t file_id;
22 hid_t dataset_id;
23 hid_t dataspace_id;
24 herr_t status;
25 herr_t HDF5_error = -1;
26
27 //printf(" opening %s \n",filename);
28 file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT);
29 if (file_id == HDF5_error)
30 { printf("ERROR opening %s \n",filename); *ierr = 1; return; }
31
32 // READ NPART AND NCOL from file
33 // Also, why is this #if even neccessary....!
34
35 #if H5_VERSION_GE(1,8,0)
36 dataset_id = H5Dopen2(file_id,"tracer particles",H5P_DEFAULT);
37 #else
38 dataset_id = H5Dopen(file_id,"tracer particles");
39 #endif
40
41 if (dataset_id == HDF5_error)
42 { printf("ERROR opening tracer particle data set \n"); *ierr = 2; return; }
43
44 dataspace_id = H5Dget_space(dataset_id);
45
46 // get dimensional information from dataspace
47 hsize_t HDFxdims[4], HDFmaxdims[4];
48 int rank = H5Sget_simple_extent_dims(dataspace_id, HDFxdims, HDFmaxdims);
49 if (rank > 4) { printf("RANK of dataset exceeds array bounds \n"); *ierr = 3; return; }
50
51 // from the dimensional info, calculate the size of the buffer.
52 *npart = HDFxdims[0];
53 *ncol = HDFxdims[1];
54 //printf(" number of particles %i \n",HDFxdims[0]);
55 //printf(" number of columns = %i \n",HDFxdims[1]);
56
57 status = H5Sclose(dataspace_id);
58 if (status == HDF5_error) { printf("ERROR closing dataspace \n"); *ierr = 4; }
59
60 status = H5Dclose(dataset_id);
61 if (status == HDF5_error) { printf("ERROR closing dataset \n"); *ierr = 4; }
62
63 /*
64 * read the time from the file - this is
65 * contained in a pointlessly complicated
66 * compound structure that we have to replicate here
67 *
68 */
69
70
71
72 #if H5_VERSION_GE(1,8,0)
73 dataset_id = H5Dopen2(file_id,"real scalars",H5P_DEFAULT);
74 #else
75 dataset_id = H5Dopen(file_id,"real scalars");
76 #endif
77
78 if (dataset_id == HDF5_error)
79 { printf("ERROR opening real scalars data set for time \n"); *ierr = 5; return; }
80
81 dataspace_id = H5Dget_space(dataset_id);
82 rank = H5Sget_simple_extent_dims(dataspace_id, HDFxdims, HDFmaxdims);
83 if (rank > 4) { printf("RANK of dataset exceeds array bounds \n"); *ierr = 3; return; }
84
85 int lenheader = HDFxdims[0];
86 //printf(" header contains %i items \n",lenheader);
87
88 typedef struct h_t {
89 char name[80];
90 double value;
91 } h_t;
92 h_t header[lenheader];
93
94 hid_t strtype = H5Tcopy(H5T_C_S1);
95 status = H5Tset_size (strtype, 80);
96
97 hid_t compound_id = H5Tcreate(H5T_COMPOUND, sizeof(h_t));
98 H5Tinsert(compound_id,"name",HOFFSET(h_t, name), strtype);
99 H5Tinsert(compound_id,"value",HOFFSET(h_t, value), H5T_NATIVE_DOUBLE);
100
101 status = H5Dread(dataset_id,compound_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,header);
102 if (status == HDF5_error) { printf("ERROR reading header \n"); *ierr = 6; }
103 *time = header[0].value;
104
105 status = H5Sclose(dataspace_id);
106 if (status == HDF5_error) { printf("ERROR closing dataspace \n"); *ierr = 4; }
107
108 status = H5Dclose(dataset_id);
109 if (status == HDF5_error) { printf("ERROR closing dataset \n"); *ierr = 4; }
110
111 // Additionally check if an SPH_density data set is present
112 if (checkfordataset(file_id,"SPH_density")==1) {
113 printf(" file contains SPH-calculated densities \n");
114 *ncol += 1;
115 } else {
116 printf(" file does not contain SPH_density data set \n");
117 }
118
119 status = H5Fclose( file_id );
120 if (status == HDF5_error) { printf("ERROR closing file \n"); *ierr = 7; }
121
122 }
123
124 void read_flash_hdf5_data(char *filename, int *npart, int *ncol, int *isrequired, int *ierr)
125 {
126 hid_t file_id;
127 hid_t dataset_id, SPHdataset_id;
128 hid_t dataspace_id;
129 hid_t memspace_id;
130 herr_t status;
131 herr_t HDF5_error = -1;
132
133 //printf(" re-opening %s \n",filename);
134 file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT);
135 if (file_id == HDF5_error)
136 { printf("ERROR re-opening %s \n",filename); *ierr = 1; return; }
137
138 // re-open tracer particle dataspace
139
140 #if H5_VERSION_GE(1,8,0)
141 dataset_id = H5Dopen2(file_id,"tracer particles",H5P_DEFAULT);
142 #else
143 dataset_id = H5Dopen(file_id,"tracer particles");
144 #endif
145
146 if (dataset_id == HDF5_error)
147 { printf("ERROR opening tracer particle data set \n"); *ierr = 2; return; }
148
149 dataspace_id = H5Dget_space(dataset_id);
150
151 // Additionally check if an SPH_density data set is present
152
153 int ncolloop;
154 int gotSPHdata = checkfordataset(file_id,"SPH_density");
155 if (gotSPHdata==0) {
156 //printf(" file does not contain SPH_density data set \n");
157 ncolloop = *ncol;
158 } else {
159 //printf(" file contains SPH-calculated densities \n");
160 ncolloop = *ncol - 1;
161 }
162
163 // make a temporary space to put each column as we read it
164 hsize_t nparth[1];
165 nparth[0] = *npart;
166 memspace_id = H5Screate_simple(1,nparth,NULL);
167
168 // dynamically allocate a temporary double array to store one column
169 double* temp = 0;
170 temp = malloc(*npart*sizeof(double));
171
172 // read particle information into one column
173 hsize_t offset[2], count[2];
174
175 int i;
176 count[0] = *npart;
177 count[1] = 1;
178 /*
179 * read particle IDs first so we can sort into ID order
180 */
181 offset[0] = 0;
182 offset[1] = 5; // ID in column 5: should check this but this is for the future
183 status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET, offset, NULL, count, NULL);
184 if (status == HDF5_error) { printf("ERROR creating hyperslab \n"); *ierr = 4; }
185 if (!H5Sselect_valid(dataspace_id)) { printf("ERROR selecting hyperslab \n"); *ierr = 5; }
186 // read ID
187 H5Dread(dataset_id,H5T_NATIVE_DOUBLE,memspace_id,dataspace_id,H5P_DEFAULT,temp);
188 int* tempid = 0;
189 tempid = malloc(*npart*sizeof(int));
190
191 // convert temp (double) into integer array
192 for (i=0;i<*npart;i++) {
193 tempid[i] = (int)temp[i];
194 }
195
196 /*
197 * start loop from 1 because first array is a useless "tag" array that
198 * we don't need to read.
199 *
200 */
201 for (i=1;i<ncolloop;i++) {
202 if (i != 5 && isrequired[i-1]==1) { // skip column 5 which is the particle ID, above
203 offset[0] = 0;
204 offset[1] = i;
205 //printf(" getting column %i offset %i \n",i,offset[0]);
206 //printf(" getting column %i count %i \n",i,count[0]);
207
208 status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET, offset, NULL, count, NULL);
209 if (status == HDF5_error) { printf("ERROR creating hyperslab \n"); *ierr = 4; }
210
211 if (!H5Sselect_valid(dataspace_id)) { printf("ERROR selecting hyperslab \n"); *ierr = 5; }
212 //printf(" getting column %i count %i \n",i,count[0]);
213
214 H5Dread(dataset_id,H5T_NATIVE_DOUBLE,memspace_id,dataspace_id,H5P_DEFAULT,temp);
215
216 // call Fortran back, sending values in temp array to fill into the main splash dat array
217
218 read_flash_data_fromc(&i,&*npart,temp,tempid);
219 }
220 }
221
222 status = H5Sclose(dataspace_id);
223 if (status == HDF5_error) { printf("ERROR closing dataspace \n"); *ierr = 4; }
224
225 status = H5Dclose(dataset_id);
226 if (status == HDF5_error) { printf("ERROR closing dataset \n"); *ierr = 4; }
227
228 // Additionally read SPH_density data set if it is present
229 if (gotSPHdata==1) {
230
231 #if H5_VERSION_GE(1,8,0)
232 dataset_id = H5Dopen2(file_id,"tracer particles",H5P_DEFAULT);
233 #else
234 dataset_id = H5Dopen(file_id,"tracer particles");
235 #endif
236
237 if (dataset_id == HDF5_error)
238 { printf("ERROR opening tracer particle data set \n"); *ierr = 2; return; }
239
240 #if H5_VERSION_GE(1,8,0)
241 SPHdataset_id = H5Dopen2(file_id,"SPH_density",H5P_DEFAULT);
242 #else
243 SPHdataset_id = H5Dopen(file_id,"SPH_density");
244 #endif
245
246 printf(" reading SPH_density data set \n");
247
248 status = H5Dread(SPHdataset_id,H5T_NATIVE_DOUBLE,H5S_ALL,H5S_ALL,H5P_DEFAULT,temp);
249 if (status == HDF5_error) { printf("ERROR reading SPH_density dataspace \n"); *ierr = 3; }
250
251 // call Fortran back, sending values in temp array to fill into the main splash dat array
252 read_flash_data_fromc(&ncolloop,&*npart,temp,tempid);
253
254 status = H5Dclose(SPHdataset_id);
255 if (status == HDF5_error) { printf("ERROR closing SPH_density dataset \n"); *ierr = 4; }
256 }
257
258 // deallocate memory
259 free(tempid);
260 free(temp);
261
262 status = H5Fclose( file_id );
263 if (status == HDF5_error) { printf("ERROR closing file \n"); *ierr = 7; }
264
265 }
266
267 void write_tracer_particle_density(char *filename, int *npart, double *dens, int *ierr)
268 {
269 hid_t file_id;
270 hid_t dataset_id;
271 hid_t memspace_id;
272 herr_t status;
273 herr_t HDF5_error = -1;
274
275 printf(" opening %s for write \n",filename);
276 file_id = H5Fopen(filename,H5F_ACC_RDWR,H5P_DEFAULT);
277 if (file_id == HDF5_error)
278 { printf("ERROR opening %s for write \n",filename); *ierr = 1; return; }
279
280 if (checkfordataset(file_id,"SPH_density")==1) {
281 // SPH data set already exists, we are going to overwrite it
282
283 #if H5_VERSION_GE(1,8,0)
284 dataset_id = H5Dopen2(file_id,"SPH_density",H5P_DEFAULT);
285 #else
286 dataset_id = H5Dopen(file_id,"SPH_density");
287 #endif
288 if (dataset_id == HDF5_error)
289 { printf("ERROR opening SPH_density data set for overwrite \n"); *ierr = 2; return; }
290
291 } else {
292 // create new dataspace for tracer particle density
293 hsize_t nparth[1];
294 nparth[0] = *npart;
295 memspace_id = H5Screate_simple(1,nparth,NULL);
296
297 #if H5_VERSION_GE(1,8,0)
298 dataset_id = H5Dcreate2(file_id,"SPH_density",H5T_NATIVE_DOUBLE,memspace_id,H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
299 #else
300 dataset_id = H5Dcreate(file_id,"SPH_density",H5T_NATIVE_DOUBLE,memspace_id,H5P_DEFAULT);
301 #endif
302
303 if (dataset_id == HDF5_error)
304 { printf("ERROR creating SPH_density data set \n"); *ierr = 2; return; }
305 }
306
307 // write dataset to file
308 status = H5Dwrite(dataset_id,H5T_NATIVE_DOUBLE,H5S_ALL,H5S_ALL,H5P_DEFAULT,dens);
309 if (status == HDF5_error) { printf("ERROR writing to file \n"); *ierr = 4; }
310
311 // clean up
312
313 status = H5Dclose(dataset_id);
314 if (status == HDF5_error) { printf("ERROR closing dataset \n"); *ierr = 6; }
315
316 status = H5Fclose( file_id );
317 if (status == HDF5_error) { printf("ERROR closing file \n"); *ierr = 7; }
318
319 }