3 #if defined(__GNUG__) && !defined(__clang__)
4 #pragma GCC diagnostic push
5 #pragma GCC diagnostic ignored "-Wnull-dereference"
8 #if defined(__GNUG__) && !defined(__clang__)
9 #pragma GCC diagnostic pop
12 #if defined(__GNUG__) && !defined(__clang__)
13 #pragma GCC diagnostic push
14 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
17 #if defined(__GNUG__) && !defined(__clang__)
18 #pragma GCC diagnostic pop
35 #include <h5pp/h5pp.h>
46 constexpr
static int64_t TWO_TUPLE_SIZE = wmtk::multimesh::utils::TWO_TUPLE_SIZE;
47 constexpr
static int64_t DEFAULT_TUPLES_VALUES = wmtk::multimesh::utils::DEFAULT_TUPLES_VALUES;
49 h5pp::File hdf5_file(filename, h5pp::FileAccess::READONLY);
53 if (hdf5_file.linkExists(
"WMTK/multimesh")) {
54 std::map<std::vector<int64_t>, std::shared_ptr<Mesh>> meshes;
57 const auto dsets = hdf5_file.findGroups(
"mesh_",
"WMTK/multimesh", -1, 1);
58 for (
auto& s : dsets) {
59 const std::string dataset =
"WMTK/multimesh/" + s;
62 hdf5_file.readAttribute<std::vector<int64_t>>(dataset,
"absolute_id");
63 meshes[absolute_id] =
read_mesh(hdf5_file, dataset);
67 for (
auto& p : meshes) {
68 if (p.first.empty())
continue;
70 const std::vector<int64_t>& child_id = p.first;
71 const auto child_index = child_id.back();
73 std::vector<int64_t> parent_id = child_id;
76 auto parent_mesh = meshes.at(parent_id);
77 auto child_mesh = p.second;
79 const PrimitiveType child_primitive_type = child_mesh->top_simplex_type();
82 assert(parent_mesh->m_multi_mesh_manager.m_children.size() == child_index);
84 auto child_to_parent_handle =
86 ->get_attribute_handle<int64_t>(
91 auto parent_to_child_handle =
93 ->get_attribute_handle<int64_t>(
99 child_mesh->m_multi_mesh_manager.m_parent = parent_mesh.get();
100 child_mesh->m_multi_mesh_manager.m_child_id = child_index;
101 child_mesh->m_multi_mesh_manager.map_to_parent_handle = child_to_parent_handle;
103 parent_mesh->m_multi_mesh_manager.m_children.emplace_back();
104 parent_mesh->m_multi_mesh_manager.m_children.back().mesh = child_mesh;
105 parent_mesh->m_multi_mesh_manager.m_children.back().map_handle = parent_to_child_handle;
106 parent_mesh->m_multi_mesh_manager
107 .m_has_child_mesh_in_dimension[child_mesh->top_cell_dimension()] =
true;
108 parent_mesh->assert_capacity_valid();
109 child_mesh->assert_capacity_valid();
113 for (
auto& p : meshes) {
114 assert(p.first == p.second->m_multi_mesh_manager.absolute_id());
126 hdf5_file.readAttribute<
PrimitiveType>(root_dataset,
"top_simplex_type");
128 std::shared_ptr<Mesh> mesh;
130 switch (top_simplex_type) {
138 std::vector<int64_t> capacities =
139 hdf5_file.readAttribute<std::vector<int64_t>>(root_dataset,
"capacities");
142 mesh->set_capacities(capacities);
145 const auto dsets = hdf5_file.findDatasets(
"", root_dataset, -1, 1);
146 for (
auto& s : dsets) {
147 const std::string dataset = root_dataset +
"/" + s;
150 const int64_t stride = hdf5_file.readAttribute<int64_t>(dataset,
"stride");
151 const int64_t dimension = hdf5_file.readAttribute<int64_t>(dataset,
"dimension");
152 const std::string type = hdf5_file.readAttribute<std::string>(dataset,
"type");
153 const std::string name =
154 std::regex_replace(s, std::regex(std::to_string(dimension) +
"/"),
"");
158 if (type ==
"int64_t") {
159 auto v = hdf5_file.readDataset<std::vector<int64_t>>(dataset);
160 const auto default_val = hdf5_file.readAttribute<int64_t>(dataset,
"default_value");
162 set_attribute<int64_t>(default_val, name, pt, stride, v, *mesh);
163 }
else if (type ==
"char") {
164 auto tmp = hdf5_file.readDataset<std::vector<short>>(dataset);
165 const auto default_val = char(hdf5_file.readAttribute<
short>(dataset,
"default_value"));
168 v.reserve(tmp.size());
169 for (
auto val : tmp) v.push_back(
char(val));
172 set_attribute<char>(default_val, name, pt, stride, v, *mesh);
173 }
else if (type ==
"double") {
174 auto v = hdf5_file.readDataset<std::vector<double>>(dataset);
175 const auto default_val = hdf5_file.readAttribute<
double>(dataset,
"default_value");
178 set_attribute<double>(default_val, name, pt, stride, v, *mesh);
179 }
else if (type ==
"rational") {
180 const Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> tmp =
181 hdf5_file.readDataset<
182 Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(dataset);
183 const Eigen::Matrix<char, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> tmp_char =
186 const std::string data = hdf5_file.readAttribute<std::string>(dataset,
"default_value");
190 std::vector<Rational> v;
191 v.reserve(tmp.rows());
192 for (
size_t i = 0; i < tmp.rows(); ++i) {
193 v.emplace_back(tmp_char.row(i));
196 set_attribute<Rational>(default_val, name, pt, stride, v, *mesh);
199 logger().error(
"We currently do not support reading the type \"{}\"", type);
207 template <
typename T>
209 const T& default_val,
210 const std::string& name,
213 const std::vector<T>& v,
223 if (stride != handle_dimension) {
225 "Attribute does not have the expected dimension:\n expected {}\n actual {}",
232 accessor.set_attribute(v);
void set_attribute(const T &default_val, const std::string &name, PrimitiveType pt, int64_t stride, const std::vector< T > &v, Mesh &mesh)
std::shared_ptr< Mesh > read(const std::filesystem::path &filename)
std::shared_ptr< Mesh > read_mesh(h5pp::File &hdf5_file, const std::string &dataset)
attribute::MeshAttributeHandle register_attribute(const std::string &name, PrimitiveType type, int64_t size, bool replace=false, T default_value=T(0))
attribute::MeshAttributeHandle get_attribute_handle(const std::string &name, const PrimitiveType ptype) const
int64_t get_attribute_dimension(const TypedAttributeHandle< T > &handle) const
bool has_attribute(const std::string &name, const PrimitiveType ptype) const
void init_from_binary(const std::string &v)
auto as() const -> const held_handle_type< held_type_from_primitive< T >()> &
Handle that represents attributes for some mesh.
static std::string parent_to_child_map_attribute_name(int64_t index)
static std::string child_to_parent_map_attribute_name()
void log_and_throw_error(const std::string &msg)
spdlog::logger & logger()
Retrieves the current logger.