BELFEM 0.9.3
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
cl_HDF5.hpp
Go to the documentation of this file.
1/*
2 * BELFEM -- The Berkeley Lab Finite Element Framework
3 * Copyright (c) 2026, The Regents of the University of California,
4 * through Lawrence Berkeley National Laboratory (subject to receipt of any required
5 * approvals from the U.S. Dept. of Energy). All rights reserved.
6 *
7 * Developers: Christian Messe, Gregory Giard
8 *
9 * See the top-level LICENSE file for the complete license and disclaimer.
10 */
11
12#ifndef BELFEM_CL_HDF5_HPP
13#define BELFEM_CL_HDF5_HPP
14
15
16#include "typedefs.hpp"
17#include "filetools.hpp"
18
19#include "hdf5_tools.hpp"
20
21#include "cl_Vector.hpp"
22#include "cl_Matrix.hpp"
23#include "cl_Cell.hpp"
24
25//------------------------------------------------------------------------------
26
27namespace belfem
28{
29
30//------------------------------------------------------------------------------
31
38 class HDF5
39 {
40 // path to data file
41 string mPath;
42
43 // pointer to file
44 hid_t mFile;
45
46 // pointer to active group
47 hid_t mActiveGroup = -1;
48
49 string mActiveGroupLabel = "";
50
51 // error status of file
52 herr_t mStatus = 0;
53
54 // flag telling if file is open
55 bool mFileIsOpen = false;
56
57 Cell< string > mTreeLabels ;
58 Cell< hid_t > mTree ;
59
60//------------------------------------------------------------------------------
61 public:
62//------------------------------------------------------------------------------
63
67 HDF5( const string & aPath,
68 const enum FileMode aMode=FileMode::NEW,
69 const bool aParallelMode=false );
70
71//------------------------------------------------------------------------------
72
76 ~HDF5();
77
78//------------------------------------------------------------------------------
79
83 void
84 close();
85
86//------------------------------------------------------------------------------
87
88 herr_t &
89 status();
90
91//------------------------------------------------------------------------------
92// GROUP FUNCTIONS
93//------------------------------------------------------------------------------
94
98 hid_t
99 create_group( const string & aLabel );
100
101//------------------------------------------------------------------------------
102
108 hid_t
109 create_group( const string & aLabel,
110 const hid_t aParent );
111
112//------------------------------------------------------------------------------
113
117 hid_t
118 select_group( const string & aLabel );
119
120//------------------------------------------------------------------------------
121
122 void
124
125//------------------------------------------------------------------------------
126
127 void
128 close_tree();
129
130//------------------------------------------------------------------------------
131
132 hid_t
133 active_group() const;
134
135//------------------------------------------------------------------------------
136
137 const string &
138 tree() const ;
139
140//------------------------------------------------------------------------------
141// Handle census
142//------------------------------------------------------------------------------
143
153 index_t
154 number_of_open_objects() const ;
155
156//------------------------------------------------------------------------------
157
164 static index_t
166
167//------------------------------------------------------------------------------
168// Save Strings
169//------------------------------------------------------------------------------
170
174 void
175 save_data( const string & aLabel,
176 const string & aValue );
177
178// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
179
180 void
181 save_data( const string & aLabel,
182 const char * aValue );
183
184//------------------------------------------------------------------------------
185// Load Strings
186//------------------------------------------------------------------------------
187
188 void
189 load_data( const string & aLabel,
190 string & aValue );
191
192//------------------------------------------------------------------------------
193// Save Scalars
194//------------------------------------------------------------------------------
195
199 void
200 save_data( const string & aLabel,
201 const sint & aValue );
202
203// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
204
208 void
209 save_data( const string & aLabel,
210 const uint & aValue );
211
212// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
213
217 void
218 save_data( const string & aLabel,
219 const luint & aValue );
220
221// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
222
229 void
230 save_data( const string & aLabel,
231 const lluint & aValue );
232
233// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
234
238 void
239 save_data( const string & aLabel,
240 const real & aValue );
241
242// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
243
247 void
248 save_data( const string & aLabel,
249 const bool & aValue );
250
251//------------------------------------------------------------------------------
252// Load Scalars
253//------------------------------------------------------------------------------
254
255 void
256 load_data( const string & aLabel,
257 bool & aValue );
258
259// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
260
261 void
262 load_data( const string & aLabel,
263 sint & aValue );
264
265// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
266
267 void
268 load_data( const string & aLabel,
269 uint & aValue );
270
271// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
272
273 void
274 load_data( const string & aLabel,
275 luint & aValue );
276
277// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
278
279 void
280 load_data( const string & aLabel,
281 lluint & aValue );
282
283// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
284
285 void
286 load_data( const string & aLabel,
287 real & aValue );
288
289//------------------------------------------------------------------------------
290// Cells
291//------------------------------------------------------------------------------
292
293 template< typename T >
294 void
295 save_data( const string & aLabel,
296 const Cell< T > & aCell )
297 {
299 mActiveGroup,
300 aLabel,
301 aCell.data(),
302 aCell.size(),
303 mStatus );
304 }
305
306// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
307
308 template< typename T >
309 void
310 load_data( const string & aLabel,
311 Cell< T > & aCell )
312 {
313 hsize_t tSize = hdf5::get_array_size( mActiveGroup, aLabel );
314
315 aCell.set_size( tSize );
316
318 mActiveGroup,
319 aLabel,
320 aCell.data(),
321 aCell.size(),
322 mStatus );
323 }
324
325//------------------------------------------------------------------------------
326// Vectors
327//------------------------------------------------------------------------------
328
329 template< typename T >
330 void
331 save_data( const string & aLabel,
332 const Vector< T > & aVector )
333 {
334 // call interface
336 mActiveGroup,
337 aLabel,
338 aVector,
339 mStatus );
340 }
341
342// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
343
344 template< typename T >
345 void
346 load_data( const string & aLabel,
347 Vector< T > & aVector )
348 {
350 mActiveGroup,
351 aLabel,
352 aVector,
353 mStatus );
354 }
355
356//------------------------------------------------------------------------------
357// Matrices
358//------------------------------------------------------------------------------
359
360 template< typename T >
361 void
362 save_data( const string & aLabel,
363 const Matrix< T > & aMatrix,
364 const bool aTranspose = false )
365 {
366 // call interface
368 mActiveGroup,
369 aLabel,
370 aMatrix,
371 mStatus,
372 aTranspose );
373 }
374
375// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
376
377 template< typename T >
378 void
379 load_data( const string & aLabel,
380 Matrix< T > & aMatrix,
381 const bool aTranspose = false )
382 {
383 // call interface
385 mActiveGroup,
386 aLabel,
387 aMatrix,
388 mStatus,
389 aTranspose );
390 }
391
392//------------------------------------------------------------------------------
393// Raw Arrays
394//------------------------------------------------------------------------------
395
396 template< typename T >
397 void
399 const string & aLabel,
400 const T * aArray,
401 const index_t aMemorySize )
402 {
404 mActiveGroup,
405 aLabel,
406 aArray,
407 aMemorySize,
408 mStatus );
409 }
410
411 template< typename T >
412 void
414 const string & aLabel,
415 T * aArray,
416 const index_t aMemorySize ) // <-- must be known a priori
417 {
419 mActiveGroup,
420 aLabel,
421 aArray,
422 aMemorySize,
423 mStatus );
424 }
425
426//------------------------------------------------------------------------------
427 };
428
429//------------------------------------------------------------------------------
430// Cells of strings
431//------------------------------------------------------------------------------
432
433 template<>
434 inline void
435 HDF5::save_data( const string & aLabel,
436 const Cell< string > & aCell )
437 {
438#ifdef BELFEM_HDF5
439 hdf5::save_strings_to_file( mActiveGroup, aLabel, aCell, mStatus );
440#endif
441 }
442
443 template<>
444 inline void
446 const string & aLabel,
447 Cell< string > & aCell )
448 {
449#ifdef BELFEM_HDF5
450 hdf5::load_strings_from_file( mActiveGroup, aLabel, aCell, mStatus );
451#endif
452 }
453
454//------------------------------------------------------------------------------
455} /* namespace belfem */
456
457//------------------------------------------------------------------------------
458#endif //BELFEM_CL_HDF5_HPP
void set_size(const size_t aSize)
New elements are value-initialized ( zero for arithmetic T, nullptr for pointers ).
Definition cl_Cell.hpp:199
size_t size() const
Definition cl_Cell.hpp:189
T * data()
Returns a borrowed pointer into contiguous storage, possibly nullptr when the Cell is empty ( test si...
Definition cl_Cell.hpp:129
void save_data(const string &aLabel, const string &aValue)
save a string into the active group
Definition cl_HDF5.cpp:313
void load_data(const string &aLabel, Cell< T > &aCell)
Definition cl_HDF5.hpp:310
HDF5(const string &aPath, const enum FileMode aMode=FileMode::NEW, const bool aParallelMode=false)
constructor
Definition cl_HDF5.cpp:22
void save_data(const string &aLabel, const T *aArray, const index_t aMemorySize)
Definition cl_HDF5.hpp:398
hid_t active_group() const
Definition cl_HDF5.cpp:507
hid_t create_group(const string &aLabel)
create a group and make it the active group
Definition cl_HDF5.cpp:148
void close_tree()
Definition cl_HDF5.cpp:287
void load_data(const string &aLabel, T *aArray, const index_t aMemorySize)
Definition cl_HDF5.hpp:413
void close()
close file
Definition cl_HDF5.cpp:122
void close_active_group()
Definition cl_HDF5.cpp:252
void load_data(const string &aLabel, Vector< T > &aVector)
Definition cl_HDF5.hpp:346
index_t number_of_open_objects() const
Number of objects open in this file other than the file itself: datasets, groups, named datatypes and...
Definition cl_HDF5.cpp:523
void load_data(const string &aLabel, string &aValue)
Definition cl_HDF5.cpp:339
void load_data(const string &aLabel, Matrix< T > &aMatrix, const bool aTranspose=false)
Definition cl_HDF5.hpp:379
void save_data(const string &aLabel, const Vector< T > &aVector)
Definition cl_HDF5.hpp:331
const string & tree() const
Definition cl_HDF5.cpp:515
hid_t select_group(const string &aLabel)
open an existing group and make it the active group
Definition cl_HDF5.cpp:211
void save_data(const string &aLabel, const Matrix< T > &aMatrix, const bool aTranspose=false)
Definition cl_HDF5.hpp:362
void save_data(const string &aLabel, const Cell< T > &aCell)
Definition cl_HDF5.hpp:295
herr_t & status()
Definition cl_HDF5.cpp:137
static index_t number_of_open_datatypes()
Number of datatype identifiers open in the whole library: those of every open file plus the transient...
Definition cl_HDF5.cpp:543
Dense column-major matrix.
Definition cl_BZ_Matrix.hpp:28
Column vector.
Definition cl_BZ_Vector.hpp:41
void save_vector_to_file(hid_t &aLoc, const std::string &aLabel, const Vector< T > &aVector, herr_t &aStatus)
Definition hdf5_tools.hpp:553
void save_array_to_file(hid_t &aLoc, const std::string &aLabel, const T *aData, const hsize_t &aLength, herr_t &aStatus)
Definition hdf5_tools.hpp:415
void save_strings_to_file(hid_t &aLoc, const std::string &aLabel, const Cell< string > &aCell, herr_t &aStatus)
Definition hdf5_tools.hpp:910
void load_strings_from_file(hid_t &aLoc, const std::string &aLabel, Cell< string > &aCell, herr_t &aStatus)
Definition hdf5_tools.hpp:956
void load_matrix_from_file(hid_t &aLoc, const std::string &aLabel, Matrix< T > &aMatrix, herr_t &aStatus, const bool aTranspose=false)
Definition hdf5_tools.hpp:808
void save_matrix_to_file(hid_t &aLoc, const std::string &aLabel, const Matrix< T > &aMatrix, herr_t &aStatus, const bool aTranspose=false)
Definition hdf5_tools.hpp:703
hsize_t get_array_size(hid_t &aFileID, const std::string &aLabel)
Definition hdf5_tools.cpp:223
void load_vector_from_file(hid_t &aLoc, const std::string &aLabel, Vector< T > &aVector, herr_t &aStatus)
Definition hdf5_tools.hpp:626
void load_array_from_file(hid_t &aLoc, const std::string &aLabel, T *aData, const hsize_t aLength, herr_t &aStatus)
Definition hdf5_tools.hpp:484
USER GUIDES:
Definition cl_Capacitor.cpp:16
int hsize_t
Definition hdf5_types.hpp:22
int hid_t
Definition hdf5_types.hpp:20
long long unsigned int lluint
Definition typedefs.hpp:34
long unsigned int luint
Definition typedefs.hpp:33
unsigned int uint
Definition typedefs.hpp:30
FileMode
Definition filetools.hpp:27
@ NEW
Definition filetools.hpp:28
uint32_t index_t
Definition typedefs.hpp:52
int sint
Definition typedefs.hpp:28
int herr_t
Definition hdf5_types.hpp:21
double real
Definition typedefs.hpp:36