KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
hdf5_file.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // license: HDF5Application/license.txt
9 //
10 // Main author: Michael Andre, https://github.com/msandre
11 // Suneth Warnakulasuriya
12 //
13 
14 #pragma once
15 
16 // System includes
17 #include <vector>
18 #include <string>
19 
20 // External includes
21 
22 // Project includes
23 #include "containers/array_1d.h"
25 #include "includes/define.h"
27 
28 // Application includes
30 
31 namespace Kratos
32 {
33 
34 namespace HDF5
35 {
38 
41 
43 struct WriteInfo
44 {
45  std::size_t StartIndex = -1;
46  std::size_t BlockSize = -1;
47  std::size_t TotalSize = -1;
48 };
49 
51 
56 class KRATOS_API(HDF5_APPLICATION) File
57 {
58 private:
61 
62  enum class DataTransferMode
63  {
64  Independent,
65  Collective
66  };
67 
69 
70 public:
73 
74  template <class T>
76 
77  template <class T>
79 
82 
86 
87  explicit File(
88  const DataCommunicator& rDataCommunicator,
89  Parameters Settings);
90 
91  File(const File& rOther) = delete;
92 
93  File(File&& rOther);
94 
95  virtual ~File();
96 
97  File& operator=(const File& rOther) = delete;
98 
99  File& operator=(File&& rOther);
100 
104 
113  bool HasPath(const std::string& rPath) const;
114 
121  bool IsGroup(const std::string& rPath) const;
122 
129  bool IsDataSet(const std::string& rPath) const;
130 
138  bool HasAttribute(
139  const std::string& rObjectPath,
140  const std::string& rName) const;
141 
151  template<class TDataType>
152  bool HasAttributeType(
153  const std::string& rObjectPath,
154  const std::string& rName) const;
155 
163  std::vector<hsize_t> GetAttributeDimensions(
164  const std::string& rObjectPath,
165  const std::string& rName) const;
166 
173  void DeleteAttribute(
174  const std::string& rObjectPath,
175  const std::string& rName);
176 
183  std::vector<std::string> GetAttributeNames(const std::string& rObjectPath) const;
184 
190  void CreateGroup(const std::string& rPath);
191 
198  std::vector<std::string> GetLinkNames(const std::string& rGroupPath) const;
199 
206  std::vector<std::string> GetGroupNames(const std::string& rGroupPath) const;
207 
214  std::vector<std::string> GetDataSetNames(const std::string& rGroupPath) const;
215 
225  void AddPath(const std::string& rPath);
226 
235  template<class TDataType>
236  void WriteAttribute(
237  const std::string& rObjectPath,
238  const std::string& rName,
239  const TDataType& rValue);
240 
247  void WriteAttribute(
248  const std::string& rObjectPath,
249  const Parameters Attributes);
250 
262  template<class TDataType>
263  void WriteDataSet(
264  const std::string& rPath,
265  const TDataType& rData,
266  WriteInfo& rInfo);
267 
279  template<class TDataType>
280  void WriteDataSetIndependent(
281  const std::string& rPath,
282  const TDataType& rData,
283  WriteInfo& rInfo);
284 
291  std::vector<unsigned> GetDataDimensions(const std::string& rPath) const;
292 
300  bool HasIntDataType(const std::string& rPath) const;
301 
309  bool HasFloatDataType(const std::string& rPath) const;
310 
319  template<class TDataType>
320  bool HasDataType(const std::string& rPath) const;
321 
323  void Flush();
324 
332  void Close();
333 
334  unsigned GetFileSize() const;
335 
336  std::string GetFileName() const;
337 
338  int GetEchoLevel() const;
339 
340  void SetEchoLevel(int Level);
341 
342  const DataCommunicator& GetDataCommunicator() const;
343 
345  unsigned GetPID() const;
346 
348  unsigned GetTotalProcesses() const;
349 
358  template<class TDataType>
359  void ReadAttribute(
360  const std::string& rObjectPath,
361  const std::string& rName,
362  TDataType& rValue) const;
363 
370  Parameters ReadAttribute(const std::string& rObjectPath) const;
371 
385  template<class TDataType>
386  void ReadDataSet(
387  const std::string& rPath,
388  TDataType& rData,
389  const unsigned StartIndex,
390  const unsigned BlockSize) const;
391 
403  template<class TDataType>
404  void ReadDataSetIndependent(
405  const std::string& rPath,
406  TDataType& rData,
407  const unsigned StartIndex,
408  const unsigned BlockSize) const;
409 
410  unsigned GetOpenObjectsCount() const;
412 
413 private:
416 
417  DataCommunicator const * mpDataCommunicator;
418 
419  std::string mFileName;
420 
421  hid_t mFileId = -1; // Default invalid file id.
422 
423  int mEchoLevel = 0;
424 
426 
429 
430  void SetFileDriver(
431  const std::string& rDriver,
432  hid_t FileAccessPropertyListId) const;
433 
434  hid_t GetFileId() const;
435 
436  hid_t OpenExistingDataSet(const std::string& rPath);
437 
438  template<class TDataType, DataTransferMode TDataTransferMode>
439  void WriteDataSetImpl(
440  const std::string& rPath,
441  const TDataType& rData,
442  WriteInfo& rInfo);
443 
444  template<class TDataType, DataTransferMode TDataTransferMode>
445  void ReadDataSetImpl(
446  const std::string& rPath,
447  TDataType& rData,
448  const unsigned StartIndex,
449  const unsigned BlockSize) const;
450 
452 };
453 
455 
456 namespace Internals
457 {
459 
463 bool IsPath(const std::string& rPath);
464 
466 std::vector<std::string> Split(const std::string& rPath, char Delimiter);
467 
468 std::vector<hsize_t> GetDataDimensions(const Vector<int>& rData);
469 std::vector<hsize_t> GetDataDimensions(const Vector<double>& rData);
470 std::vector<hsize_t> GetDataDimensions(const Vector<array_1d<double, 3>>& rData);
471 std::vector<hsize_t> GetDataDimensions(const Matrix<int>& rData);
472 std::vector<hsize_t> GetDataDimensions(const Matrix<double>& rData);
473 std::vector<hsize_t> GetDataDimensions(const File& rFile, const std::string& rPath);
474 } // namespace Internals.
475 
477 } // namespace HDF5.
478 } // namespace Kratos.
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
Serial (do-nothing) version of a wrapper class for MPI communication.
Definition: data_communicator.h:318
A base class for reading and writing an HDF5 file.
Definition: hdf5_file.h:57
File & operator=(const File &rOther)=delete
File(const File &rOther)=delete
KRATOS_CLASS_POINTER_DEFINITION(File)
Pointer definition.
Definition: amatrix_interface.h:41
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
def Flush(a)
Definition: DEM_procedures.py:13
bool IsPath(const std::string &rPath)
Check if string is a valid path.
Definition: hdf5_file.cpp:66
std::vector< hsize_t > GetDataDimensions(const Vector< array_1d< double, 3 >> &rData)
std::vector< std::string > Split(const std::string &rPath, char Delimiter)
Return vector of non-empty substrings separated by a delimiter.
void SetEchoLevel(const int EchoLevel)
Definition: add_custom_io_to_python.cpp:344
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
def CreateGroup(file_or_group, name, overwrite_previous=True)
Definition: hdf5_io_tools.py:24
Stores information about a dataset written to HDF5.
Definition: hdf5_file.h:44
std::size_t TotalSize
Definition: hdf5_file.h:47
std::size_t BlockSize
Definition: hdf5_file.h:46
std::size_t StartIndex
Definition: hdf5_file.h:45