BELFEM 0.9.0
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// Save Strings
142//------------------------------------------------------------------------------
143
147 void
148 save_data( const string & aLabel,
149 const string & aValue );
150
151// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
152
153 void
154 save_data( const string & aLabel,
155 const char * aValue );
156
157//------------------------------------------------------------------------------
158// Load Strings
159//------------------------------------------------------------------------------
160
161 void
162 load_data( const string & aLabel,
163 string & aValue );
164
165//------------------------------------------------------------------------------
166// Save Scalars
167//------------------------------------------------------------------------------
168
172 void
173 save_data( const string & aLabel,
174 const sint & aValue );
175
176// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
177
181 void
182 save_data( const string & aLabel,
183 const uint & aValue );
184
185// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
186
190 void
191 save_data( const string & aLabel,
192 const luint & aValue );
193
194// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
195
202 void
203 save_data( const string & aLabel,
204 const lluint & aValue );
205
206// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
207
211 void
212 save_data( const string & aLabel,
213 const real & aValue );
214
215// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
216
220 void
221 save_data( const string & aLabel,
222 const bool & aValue );
223
224//------------------------------------------------------------------------------
225// Load Scalars
226//------------------------------------------------------------------------------
227
228 void
229 load_data( const string & aLabel,
230 bool & aValue );
231
232// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
233
234 void
235 load_data( const string & aLabel,
236 sint & aValue );
237
238// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
239
240 void
241 load_data( const string & aLabel,
242 uint & aValue );
243
244// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
245
246 void
247 load_data( const string & aLabel,
248 luint & aValue );
249
250// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
251
252 void
253 load_data( const string & aLabel,
254 lluint & aValue );
255
256// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
257
258 void
259 load_data( const string & aLabel,
260 real & aValue );
261
262//------------------------------------------------------------------------------
263// Cells
264//------------------------------------------------------------------------------
265
266 template< typename T >
267 void
268 save_data( const string & aLabel,
269 const Cell< T > & aCell )
270 {
272 mActiveGroup,
273 aLabel,
274 aCell.data(),
275 aCell.size(),
276 mStatus );
277 }
278
279// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
280
281 template< typename T >
282 void
283 load_data( const string & aLabel,
284 Cell< T > & aCell )
285 {
286 hsize_t tSize = hdf5::get_array_size( mActiveGroup, aLabel );
287
288 aCell.set_size( tSize );
289
291 mActiveGroup,
292 aLabel,
293 aCell.data(),
294 aCell.size(),
295 mStatus );
296 }
297
298//------------------------------------------------------------------------------
299// Vectors
300//------------------------------------------------------------------------------
301
302 template< typename T >
303 void
304 save_data( const string & aLabel,
305 const Vector< T > & aVector )
306 {
307 // call interface
309 mActiveGroup,
310 aLabel,
311 aVector,
312 mStatus );
313 }
314
315// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
316
317 template< typename T >
318 void
319 load_data( const string & aLabel,
320 Vector< T > & aVector )
321 {
323 mActiveGroup,
324 aLabel,
325 aVector,
326 mStatus );
327 }
328
329//------------------------------------------------------------------------------
330// Matrices
331//------------------------------------------------------------------------------
332
333 template< typename T >
334 void
335 save_data( const string & aLabel,
336 const Matrix< T > & aMatrix,
337 const bool aTranspose = false )
338 {
339 // call interface
341 mActiveGroup,
342 aLabel,
343 aMatrix,
344 mStatus,
345 aTranspose );
346 }
347
348// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
349
350 template< typename T >
351 void
352 load_data( const string & aLabel,
353 Matrix< T > & aMatrix,
354 const bool aTranspose = false )
355 {
356 // call interface
358 mActiveGroup,
359 aLabel,
360 aMatrix,
361 mStatus,
362 aTranspose );
363 }
364
365//------------------------------------------------------------------------------
366// Raw Arrays
367//------------------------------------------------------------------------------
368
369 template< typename T >
370 void
372 const string & aLabel,
373 const T * aArray,
374 const index_t aMemorySize )
375 {
377 mActiveGroup,
378 aLabel,
379 aArray,
380 aMemorySize,
381 mStatus );
382 }
383
384 template< typename T >
385 void
387 const string & aLabel,
388 T * aArray,
389 const index_t aMemorySize ) // <-- must be known a priori
390 {
392 mActiveGroup,
393 aLabel,
394 aArray,
395 aMemorySize,
396 mStatus );
397 }
398
399//------------------------------------------------------------------------------
400 };
401
402//------------------------------------------------------------------------------
403// Cells of strings
404//------------------------------------------------------------------------------
405
406 template<>
407 inline void
408 HDF5::save_data( const string & aLabel,
409 const Cell< string > & aCell )
410 {
411#ifdef BELFEM_HDF5
412 hdf5::save_strings_to_file( mActiveGroup, aLabel, aCell, mStatus );
413#endif
414 }
415
416 template<>
417 inline void
419 const string & aLabel,
420 Cell< string > & aCell )
421 {
422#ifdef BELFEM_HDF5
423 hdf5::load_strings_from_file( mActiveGroup, aLabel, aCell, mStatus );
424#endif
425 }
426
427//------------------------------------------------------------------------------
428} /* namespace belfem */
429
430//------------------------------------------------------------------------------
431#endif //BELFEM_CL_HDF5_HPP
void set_size(const size_t aSize)
Definition cl_Cell.hpp:189
size_t size() const
return the size of the Cell
Definition cl_Cell.hpp:181
T * data()
Definition cl_Cell.hpp:120
void save_data(const string &aLabel, const string &aValue)
save a string into the active group
Definition cl_HDF5.cpp:330
void load_data(const string &aLabel, Cell< T > &aCell)
Definition cl_HDF5.hpp:283
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:371
hid_t active_group() const
Definition cl_HDF5.cpp:538
hid_t create_group(const string &aLabel)
create a group and make it the active group
Definition cl_HDF5.cpp:158
void close_tree()
Definition cl_HDF5.cpp:302
void load_data(const string &aLabel, T *aArray, const index_t aMemorySize)
Definition cl_HDF5.hpp:386
void close()
close file
Definition cl_HDF5.cpp:132
void close_active_group()
Definition cl_HDF5.cpp:265
void load_data(const string &aLabel, Vector< T > &aVector)
Definition cl_HDF5.hpp:319
void load_data(const string &aLabel, string &aValue)
Definition cl_HDF5.cpp:358
void load_data(const string &aLabel, Matrix< T > &aMatrix, const bool aTranspose=false)
Definition cl_HDF5.hpp:352
void save_data(const string &aLabel, const Vector< T > &aVector)
Definition cl_HDF5.hpp:304
const string & tree() const
Definition cl_HDF5.cpp:546
hid_t select_group(const string &aLabel)
open an existing group and make it the active group
Definition cl_HDF5.cpp:221
void save_data(const string &aLabel, const Matrix< T > &aMatrix, const bool aTranspose=false)
Definition cl_HDF5.hpp:335
void save_data(const string &aLabel, const Cell< T > &aCell)
Definition cl_HDF5.hpp:268
herr_t & status()
Definition cl_HDF5.cpp:147
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:549
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:394
void save_strings_to_file(hid_t &aLoc, const std::string &aLabel, const Cell< string > &aCell, herr_t &aStatus)
Definition hdf5_tools.hpp:971
void load_strings_from_file(hid_t &aLoc, const std::string &aLabel, Cell< string > &aCell, herr_t &aStatus)
Definition hdf5_tools.hpp:1023
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:841
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:723
hsize_t get_array_size(hid_t &aFileID, const std::string &aLabel)
Definition hdf5_tools.cpp:264
void load_vector_from_file(hid_t &aLoc, const std::string &aLabel, Vector< T > &aVector, herr_t &aStatus)
Definition hdf5_tools.hpp:634
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:472
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