Wildmeshing Toolkit
HDF5Writer.cpp
Go to the documentation of this file.
1 #if defined(__GNUG__) && !defined(__clang__)
2 #pragma GCC diagnostic push
3 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
4 #endif
5 #include <string>
6 #if defined(__GNUG__) && !defined(__clang__)
7 #pragma GCC diagnostic pop
8 #endif
9 #include <fmt/format.h>
10 #include <fmt/ranges.h>
11 #include "HDF5Writer.hpp"
12 
13 #include <wmtk/utils/Rational.hpp>
14 
15 #include <h5pp/h5pp.h>
16 
17 #include <sstream>
18 
19 namespace wmtk {
20 namespace {
21 template <typename T>
22 std::string get_type()
23 {
24  return "";
25 }
26 
27 template <>
28 std::string get_type<int64_t>()
29 {
30  return "int64_t";
31 }
32 
33 template <>
34 std::string get_type<double>()
35 {
36  return "double";
37 }
38 
39 template <>
40 std::string get_type<short>()
41 {
42  return "char";
43 }
44 
45 template <>
46 std::string get_type<std::string>()
47 {
48  return "rational";
49 }
50 } // namespace
51 
52 HDF5Writer::HDF5Writer(const std::filesystem::path& filename)
53 {
54  m_hdf5_file = std::make_shared<h5pp::File>(filename, h5pp::FileAccess::REPLACE);
55 }
56 
58  const std::string& name,
59  const int64_t type,
60  const int64_t stride,
61  const std::vector<int64_t>& val,
62  const int64_t default_val)
63 {
64  write_internal(name, type, stride, val, default_val);
65 }
66 
68  const std::string& name,
69  const int64_t type,
70  const int64_t stride,
71  const std::vector<double>& val,
72  const double default_val)
73 {
74  write_internal(name, type, stride, val, default_val);
75 }
76 
78  const std::string& name,
79  const int64_t type,
80  const int64_t stride,
81  const std::vector<char>& val,
82  const char default_val)
83 {
84  std::vector<short> tmp;
85  tmp.reserve(val.size());
86  for (const auto& v : val) tmp.push_back(v);
87 
88  write_internal(name, type, stride, tmp, short(default_val));
89 }
90 
91 
93  const std::string& name,
94  const int64_t type,
95  const int64_t stride,
96  const std::vector<Rational>& val,
97  const Rational& default_val)
98 {
99  std::vector<std::string> tmp;
100  tmp.reserve(val.size());
101  int64_t max_size = -1;
102  for (const auto& v : val) {
103  tmp.emplace_back(v.serialize());
104  max_size = std::max(max_size, int64_t(tmp.back().size()));
105  }
106 
107  Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> data(tmp.size(), max_size);
108  data.setConstant(int(' '));
109  for (int64_t i = 0; i < data.rows(); ++i) {
110  for (int64_t j = 0; j < tmp[i].size(); ++j) {
111  data(i, j) = int(tmp[i][j]);
112  }
113  }
114 
115  write_internal(name, type, stride, data, default_val.to_binary());
116 }
117 
118 void HDF5Writer::write_capacities(const std::vector<int64_t>& capacities)
119 {
120  m_hdf5_file->writeAttribute(capacities, dataset_path(), "capacities");
121 }
122 
123 template <typename Data, typename T>
125  const std::string& name,
126  const int64_t type,
127  const int64_t stride,
128  const Data& val,
129  const T& default_val)
130 {
131  std::stringstream ss;
132  ss << dataset_path() << "/" << type << "/" << name;
133 
134  m_hdf5_file->writeDataset(val, ss.str());
135  m_hdf5_file->writeAttribute(stride, ss.str(), "stride");
136  m_hdf5_file->writeAttribute(default_val, ss.str(), "default_value");
137  m_hdf5_file->writeAttribute(type, ss.str(), "dimension");
138  m_hdf5_file->writeAttribute(get_type<T>(), ss.str(), "type");
139 }
140 
142 {
143  m_hdf5_file->writeAttribute(type, dataset_path(), "top_simplex_type");
144 }
145 
146 void HDF5Writer::write_absolute_id(const std::vector<int64_t>& id)
147 {
148  if (id.empty() || m_mm_level == 0) {
149  m_name = "";
150  } else {
151  m_name = fmt::format("mesh_{}", fmt::join(id, ""));
152  }
153 
154  ++m_mm_level;
155 
156  m_hdf5_file->createGroup(dataset_path());
157 
158  if (!id.empty()) m_hdf5_file->writeAttribute(id, dataset_path(), "absolute_id");
159 }
160 
161 std::string HDF5Writer::dataset_path() const
162 {
163  std::string res = "WMTK";
164 
165  if (!m_name.empty()) res += "/multimesh/" + m_name;
166 
167  return res;
168 }
169 
170 
171 } // namespace wmtk
std::string dataset_path() const
Definition: HDF5Writer.cpp:161
std::string m_name
Definition: HDF5Writer.hpp:54
HDF5Writer(const std::filesystem::path &filename)
Definition: HDF5Writer.cpp:52
std::shared_ptr< h5pp::File > m_hdf5_file
Definition: HDF5Writer.hpp:53
void write_internal(const std::string &name, const int64_t type, const int64_t stride, const Data &val, const T &default_val)
Definition: HDF5Writer.cpp:124
void write_capacities(const std::vector< int64_t > &capacities) override
Definition: HDF5Writer.cpp:118
void write_absolute_id(const std::vector< int64_t > &id) override
Definition: HDF5Writer.cpp:146
void write_top_simplex_type(const PrimitiveType type) override
Definition: HDF5Writer.cpp:141
bool write(const int) override
Definition: HDF5Writer.hpp:20
int64_t m_mm_level
Definition: MeshWriter.hpp:51
std::string to_binary() const
Definition: Rational.cpp:350
Definition: Accessor.hpp:6