opm-simulators
HDF5Serializer.hpp
1 /*
2  This file is part of the Open Porous Media project (OPM).
3 
4  OPM is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 2 of the License, or
7  (at your option) any later version.
8 
9  OPM is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with OPM. If not, see <http://www.gnu.org/licenses/>.
16 
17  Consult the COPYING file in the top-level source directory of this
18  module for the precise wording of the license and the list of
19  copyright holders.
20 */
21 #ifndef HDF5_SERIALIZER_HPP
22 #define HDF5_SERIALIZER_HPP
23 
24 #include <opm/common/utility/Serializer.hpp>
25 
26 #include <opm/simulators/utils/HDF5File.hpp>
27 #include <opm/simulators/utils/ParallelCommunication.hpp>
28 #include <opm/simulators/utils/SerializationPackers.hpp>
29 
30 #include <cstddef>
31 #include <limits>
32 #include <string>
33 
34 namespace Opm {
35 
37 class HDF5Serializer : public Serializer<Serialization::MemPacker> {
38 public:
39  HDF5Serializer(const std::string& fileName,
40  HDF5File::OpenMode mode,
41  Parallel::Communication comm)
42  : Serializer<Serialization::MemPacker>(m_packer_priv)
43  , m_h5file(fileName, mode, comm)
44  {}
45 
52  template<class T>
53  void write(T& data,
54  const std::string& group,
55  const std::string& dset,
57  {
58  try {
59  this->pack(data);
60  } catch (...) {
61  m_packSize = std::numeric_limits<std::size_t>::max();
62  throw;
63  }
64 
65  m_h5file.write(group, dset, m_buffer, mode);
66  }
67 
75  void writeHeader(const std::string& simulator_name,
76  const std::string& module_version,
77  const std::string& time_stamp,
78  const std::string& case_name,
79  const std::string& params,
80  int num_procs);
81 
88  template<class T>
89  void read(T& data,
90  const std::string& group,
91  const std::string& dset,
93  {
94  m_h5file.read(group, dset, m_buffer, mode);
95  this->unpack(data);
96  }
97 
99  int lastReportStep() const;
100 
102  std::vector<int> reportSteps() const;
103 
104 private:
105  const Serialization::MemPacker m_packer_priv{};
106  HDF5File m_h5file;
107 };
108 
109 }
110 
111 #endif // HDF5_SERIALIZER_HPP
void write(const std::string &group, const std::string &dset, const std::vector< char > &buffer, DataSetMode mode=DataSetMode::PROCESS_SPLIT) const
Write a char buffer to a specified location in file.
Definition: HDF5File.cpp:99
int lastReportStep() const
Returns the last report step stored in file.
Definition: HDF5Serializer.cpp:46
One separate data set for each parallel process.
OpenMode
Enumeration of file opening modes.
Definition: HDF5File.hpp:37
void read(const std::string &group, const std::string &dset, std::vector< char > &buffer, DataSetMode mode=DataSetMode::PROCESS_SPLIT) const
Read a char buffer from a specified location in file.
Definition: HDF5File.cpp:154
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
Class for (de-)serializing using HDF5.
Definition: HDF5Serializer.hpp:37
void write(T &data, const std::string &group, const std::string &dset, HDF5File::DataSetMode mode=HDF5File::DataSetMode::PROCESS_SPLIT)
Serialize and write data to restart file.
Definition: HDF5Serializer.hpp:53
void read(T &data, const std::string &group, const std::string &dset, HDF5File::DataSetMode mode=HDF5File::DataSetMode::PROCESS_SPLIT)
Read data and deserialize from restart file.
Definition: HDF5Serializer.hpp:89
std::vector< int > reportSteps() const
Returns a list of report steps stored in restart file.
Definition: HDF5Serializer.cpp:58
DataSetMode
Enumeration of dataset modes.
Definition: HDF5File.hpp:44
void writeHeader(const std::string &simulator_name, const std::string &module_version, const std::string &time_stamp, const std::string &case_name, const std::string &params, int num_procs)
Writes a header to the file.
Definition: HDF5Serializer.cpp:29