Peano
Loading...
Searching...
No Matches
PeanoHDF5PatchFileWriter.cpp
Go to the documentation of this file.
2
3#include <fstream>
4
5#include "../../../mpi/Rank.h"
6
7
9 "tarch::plotter::"
10 "griddata::"
11 "blockstructured::"
12 "PeanoHDF5PatchFileWriter"
13);
14
15
17 = "# \n"
18 "# Peano HDF5 patch file \n"
19 "# Version 0.1 \n"
20 "# \n";
21
22
24 int dimensions, const std::string& filename, [[maybe_unused]] bool append, [[maybe_unused]] bool compress
25):
26 _dimensions(dimensions),
27 _compress(compress) {
30
31 clear();
32
33 if (filename.rfind(".hdf5") != std::string::npos) {
35 "PeanoHDF5PatchFileWriter()",
36 "filename should not end with .h5 as routine adds extension automatically. Chosen filename=" << filename
37 );
38 }
39
40#ifdef UseHDF5
41 if (append) {
42 _file = H5Fopen((filename + ".hdf5").c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
43 } else {
44 _file = H5Fcreate((filename + ".hdf5").c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
45 }
46
47 if (_file < 0) {
49 "PeanoHDF5PatchFileWriter()",
50 "failed to obtain file handle for HDF5 file " << filename << ". Wanted to append data=" << append
51 );
52 _isOpen = false;
53 } else {
54 _isOpen = true;
55
56 //
57 // Create the dataset counter attribute on the root level
58 //
59 if (!append) {
60 {
61 logDebug("PeanoHDF5PatchFileWriter(...)", "create Number of datasets attribute");
62
63 /*
64 * Create scalar attribute.
65 */
66 hid_t dataSetCounterDataSpace = H5Screate(H5S_SCALAR);
67 hid_t dataSetCounterAttribute = H5Acreate(
68 _file, "Number of datasets", H5T_NATIVE_INT, dataSetCounterDataSpace, H5P_DEFAULT, H5P_DEFAULT
69 );
70
71 /*
72 * Write scalar attribute.
73 */
74 int i = 0;
75 H5Awrite(dataSetCounterAttribute, H5T_NATIVE_INT, &i);
76
77 H5Aclose(dataSetCounterAttribute);
78 H5Sclose(dataSetCounterDataSpace);
79 }
80
81 {
82 logDebug("PeanoHDF5PatchFileWriter(...)", "create Number of cells per axis attribute");
83
87 hid_t numberOfCellsPerAxisDataSpace = H5Screate(H5S_SCALAR);
88 hid_t numberOfCellsPerAxisAttribute = H5Acreate(
89 _file, "Number of cells per axis", H5T_NATIVE_INT, numberOfCellsPerAxisDataSpace, H5P_DEFAULT, H5P_DEFAULT
90 );
91
95 assertion(false);
96 // H5Awrite(numberOfCellsPerAxisAttribute, H5T_NATIVE_INT, &_numberOfCellsPerAxis);
97
98 H5Aclose(numberOfCellsPerAxisAttribute);
99 H5Sclose(numberOfCellsPerAxisDataSpace);
100 }
101
102 {
103 logDebug("PeanoHDF5PatchFileWriter(...)", "create Dimensions attribute");
104
108 hid_t dimensionsDataSpace = H5Screate(H5S_SCALAR);
109 hid_t dimensionsAttribute = H5Acreate(
110 _file, "Dimensions", H5T_NATIVE_INT, dimensionsDataSpace, H5P_DEFAULT, H5P_DEFAULT
111 );
112
116 H5Awrite(dimensionsAttribute, H5T_NATIVE_INT, &_dimensions);
117
118 H5Aclose(dimensionsAttribute);
119 H5Sclose(dimensionsDataSpace);
120 }
121 }
122
123 logDebug("PeanoHDF5PatchFileWriter(...)", "Increase Number of datasets counter");
124
125 //
126 // Increase the dataset counter and thus init _numberOfActiveDataset
127 //
128 hid_t attribute = H5Aopen_by_name(_file, "/", "Number of datasets", H5P_DEFAULT, H5P_DEFAULT);
129 H5Aread(attribute, H5T_NATIVE_INT, &_numberOfActiveDataset);
131 H5Awrite(attribute, H5T_NATIVE_INT, &_numberOfActiveDataset);
132 H5Aclose(attribute);
134
135 logDebug("PeanoHDF5PatchFileWriter(...)", "create dataset and its subgroups");
136
137 //
138 // Create active dataset group
139 //
140 hid_t newGroup;
141 newGroup = H5Gcreate(_file, getNameOfCurrentDataset().c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
142 H5Gclose(newGroup);
143
144 //
145 // Create active dataset group
146 //
147 newGroup = H5Gcreate(
148 _file, (getNameOfCurrentDataset() + "/vertexdata").c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT
149 );
150 H5Gclose(newGroup);
151
152 newGroup = H5Gcreate(
153 _file, (getNameOfCurrentDataset() + "/vertexdata/data").c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT
154 );
155 H5Gclose(newGroup);
156
157 newGroup = H5Gcreate(
158 _file, (getNameOfCurrentDataset() + "/vertexdata/metadata").c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT
159 );
160 H5Gclose(newGroup);
161
162 newGroup = H5Gcreate(
163 _file, (getNameOfCurrentDataset() + "/vertexdata/mapping").c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT
164 );
165 H5Gclose(newGroup);
166
167 newGroup = H5Gcreate(
168 _file, (getNameOfCurrentDataset() + "/vertexdata/numberofunknowns").c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT
169 );
170 H5Gclose(newGroup);
171
172 newGroup = H5Gcreate(
173 _file, (getNameOfCurrentDataset() + "/celldata").c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT
174 );
175 H5Gclose(newGroup);
176
177 newGroup = H5Gcreate(
178 _file, (getNameOfCurrentDataset() + "/celldata/data").c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT
179 );
180 H5Gclose(newGroup);
181
182 newGroup = H5Gcreate(
183 _file, (getNameOfCurrentDataset() + "/celldata/metadata").c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT
184 );
185 H5Gclose(newGroup);
186
187 newGroup = H5Gcreate(
188 _file, (getNameOfCurrentDataset() + "/celldata/mapping").c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT
189 );
190 H5Gclose(newGroup);
191
192 newGroup = H5Gcreate(
193 _file, (getNameOfCurrentDataset() + "/celldata/numberofunknowns").c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT
194 );
195 H5Gclose(newGroup);
196
197 logDebug("PeanoHDF5PatchFileWriter(...)", "HDF5 file structure set up");
198 }
199#else
200 logError(
201 "PeanoHDF5PatchFileWriter()", "tried to use Peano's HDF5 writer though code has been compiled without --with-hdf5 (autotools)"
202 );
203 _isOpen = false;
204#endif
205}
206
207
209 return "/dataset-" + std::to_string(_numberOfActiveDataset);
210}
211
212
214#ifdef UseHDF5
215 if (_file >= 0) {
216 herr_t status = H5Fclose(_file);
217
218 if (status < 0) {
219 logError("PeanoHDF5PatchFileWriter()", "failed to close HDF5 file");
220 }
221 }
222#endif
223
224 _isOpen = false;
225}
226
227
230 const std::string& identifier, int unknownsPerAxis, int recordsPerCell, const std::string& description
231 ) {
232 assertion(_isOpen);
233 return new CellDataWriter(identifier, unknownsPerAxis, recordsPerCell, description, "", nullptr, *this);
234}
235
236
239 const std::string& identifier,
240 int unknownsPerAxis,
241 int recordsPerCell,
242 const std::string& description,
243 const std::string& metaData
244 ) {
245 assertion(_isOpen);
246 return new CellDataWriter(identifier, unknownsPerAxis, recordsPerCell, description, metaData, nullptr, *this);
247}
248
249
252 const std::string& identifier,
253 int unknownsPerAxis,
254 int recordsPerCell,
255 const std::string& description,
256 const std::string& metaData,
257 double* mapping
258 ) {
259 assertion(_isOpen);
260 return new CellDataWriter(identifier, unknownsPerAxis, recordsPerCell, description, metaData, mapping, *this);
261}
262
263
266 const std::string& identifier, int unknownsPerAxis, int recordsPerVertex, const std::string& description
267 ) {
268 assertion(_isOpen);
269 return new VertexDataWriter(identifier, unknownsPerAxis, recordsPerVertex, description, "", nullptr, *this);
270}
271
272
275 const std::string& identifier,
276 int unknownsPerAxis,
277 int recordsPerVertex,
278 const std::string& description,
279 const std::string& metaData
280 ) {
281 assertion(_isOpen);
282 return new VertexDataWriter(identifier, unknownsPerAxis, recordsPerVertex, description, metaData, nullptr, *this);
283}
284
285
288 const std::string& identifier,
289 int unknownsPerAxis,
290 int recordsPerVertex,
291 const std::string& description,
292 const std::string& metaData,
293 double* mapping
294 ) {
295 assertion(_isOpen);
296 return new VertexDataWriter(identifier, unknownsPerAxis, recordsPerVertex, description, metaData, mapping, *this);
297}
298
299
302) {
303 assertion(_isOpen);
304 assertionEquals(_dimensions, 2);
305
306 _geometryData.push_back(offset(0));
307 _geometryData.push_back(offset(1));
308 _geometryData.push_back(size(0));
309 _geometryData.push_back(size(1));
310
311 return -1;
312}
313
314
317) {
318 assertion(_isOpen);
319 assertionEquals(_dimensions, 3);
320
321 _geometryData.push_back(offset(0));
322 _geometryData.push_back(offset(1));
323 _geometryData.push_back(offset(2));
324 _geometryData.push_back(size(0));
325 _geometryData.push_back(size(1));
326 _geometryData.push_back(size(2));
327
328 return -1;
329}
330
331
332#ifdef UseHDF5
333hid_t tarch::plotter::griddata::blockstructured::PeanoHDF5PatchFileWriter::createDataTableProperties(
334 const int lineWidth, int rowCount
335) const {
336 constexpr int MinNumberOfRowsForCompressionToPayOff = 64;
337 if (_compress && rowCount > MinNumberOfRowsForCompressionToPayOff) {
338 logDebug("createDataTableProperties(int)", "insert properties of tables which triggers zlib compression");
339
340 //
341 // Dataset is chunked for compression and we use zlib/deflate compression
342 // with level 6
343 //
344 hsize_t cdims[] = {static_cast<hsize_t>(lineWidth), MinNumberOfRowsForCompressionToPayOff};
345 hid_t propertyList = H5Pcreate(H5P_DATASET_CREATE);
346 H5Pset_chunk(propertyList, 2, cdims);
347 H5Pset_deflate(propertyList, 6);
348 return propertyList;
349 } else {
350 return H5P_DEFAULT;
351 }
352}
353#endif
354
355
357 assertion(_isOpen);
358
359#ifdef UseHDF5
360 logDebug("writeToFile(string)", "write dataset identifier");
361
362 hid_t metaDataAttribute = H5Screate(H5S_SCALAR);
363 hid_t metaDataType = H5Tcopy(H5T_C_S1);
364
365 // H5Tset_size(metaDataType, filenamePrefix.size());
366 H5Tset_strpad(metaDataType, H5T_STR_NULLTERM);
367 hid_t attribute = H5Acreate2(
368 _file, (getNameOfCurrentDataset() + "/identifier").c_str(), metaDataType, metaDataAttribute, H5P_DEFAULT, H5P_DEFAULT
369 );
370
371 /*
372 * Write string attribute.
373 */
374 // H5Awrite(attribute, metaDataType, filenamePrefix.c_str());
375
376 /*
377 * Close attribute and file dataspaces, and datatype.
378 */
379 H5Aclose(attribute);
380 H5Sclose(metaDataAttribute);
381
382
383 //
384 // Create the data space with unlimited dimensions.
385 logDebug("writeToFile(string)", "write geometry table");
386
387 hsize_t geometryTableDimensions[] = {2 * static_cast<hsize_t>(_dimensions), _geometryData.size() / _dimensions / 2};
388
389 //
390 // Set up handles/tables
391 //
392 hid_t geometryTable = H5Screate_simple(2, geometryTableDimensions, NULL);
393 hid_t geometryDataset = H5Dcreate(
394 _file,
395 (getNameOfCurrentDataset() + "/geometry").c_str(),
396 H5T_NATIVE_DOUBLE,
397 geometryTable,
398 H5P_DEFAULT,
399 createDataTableProperties(2 * static_cast<hsize_t>(_dimensions), _geometryData.size() / _dimensions / 2),
400 H5P_DEFAULT
401 );
402
403 //
404 // Write data
405 //
406 H5Dwrite(geometryDataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, _geometryData.data());
407
408 //
409 // Close/release handles
410 //
411 hid_t result = H5Dclose(geometryDataset);
412 H5Sclose(geometryTable);
413
414 logDebug("writeToFile(string)", "all mandatory data written to HDF5 file");
415
416 return result >= 0;
417#else
418 return true;
419#endif
420}
421
422
424
425
427 _vertexCounter = 0;
428 _cellCounter = 0;
429}
430
431
433 /*
434 _out << "begin meta-data" << std::endl
435 << metaData << std::endl
436 << "end meta-data" << std::endl << std::endl;
437 */
438 (void)metaData;
439}
#define assertionEquals(lhs, rhs)
#define assertion(expr)
#define logError(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:464
#define logDebug(methodName, logMacroMessageStream)
Definition Log.h:50
#define logWarning(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:440
Log Device.
Definition Log.h:516
virtual int plotPatch(const tarch::la::Vector< 2, double > &offset, const tarch::la::Vector< 2, double > &size) override
virtual CellDataWriter * createCellDataWriter(const std::string &identifier, int unknownsPerAxis, int recordsPerCell, const std::string &description) override
Caller has to destroy this instance manually.
virtual VertexDataWriter * createVertexDataWriter(const std::string &identifier, int unknownsPerAxis, int recordsPerVertex, const std::string &description) override
Caller has to destroy this instance manually.
PeanoHDF5PatchFileWriter(int dimension, const std::string &filename, bool append, bool compress)
Simple vector class.
Definition Vector.h:150