1#ifndef UTOPIA_DATAIO_GRAPH_UTILS_HH
2#define UTOPIA_DATAIO_GRAPH_UTILS_HH
6#include <boost/graph/adjacency_list.hpp>
7#include <boost/graph/adjacency_matrix.hpp>
8#include <boost/graph/graph_traits.hpp>
9#include <boost/graph/properties.hpp>
10#include <boost/hana/all_of.hpp>
11#include <boost/hana/ext/std/tuple.hpp>
12#include <boost/hana/for_each.hpp>
16#include "../core/logging.hh"
17#include "../core/type_traits.hh"
18#include "../core/graph/iterator.hh"
22using namespace std::literals::string_literals;
31namespace GraphUtilsHelper{
47template <
typename Graph>
48std::pair<std::shared_ptr<HDFDataset>,
49 std::shared_ptr<HDFDataset>>
57 spdlog::get(
"data_io")->info(
"Saving graph with {} vertices and {} edges "
67 dset_vertices->add_attribute(
"coords_mode__vertex_idx",
"trivial");
69 dset_edges->add_attribute(
"dim_name__0",
"label");
70 dset_edges->add_attribute(
"coords_mode__label",
"values");
72 std::vector<std::string>{
"source",
"target"});
73 dset_edges->add_attribute(
"dim_name__1",
"edge_idx");
74 dset_edges->add_attribute(
"coords_mode__edge_idx",
"trivial");
84 return std::apply([](
auto,
auto,
auto...
tail) {
85 return std::make_tuple(
tail...);},
t);
90template <
typename CoordT>
93 if constexpr (Utils::is_string_v<CoordT>) {
94 return std::vector<std::string>{};
97 return std::vector<CoordT>{};
142 "Error, 'entity_kind' has to be either 'vertex' or 'edge'");
193 "Error, the name of dimension 0 has to be s string");
201 constexpr auto num_adaptors = std::tuple_size_v<std::decay_t<
211 using CoordT = std::tuple_element_t<0, FirstTupElementT>;
229 std::tuple_element_t<0,
232 "Error, coordinate types do not match! Check that all "
233 "coordinates are of the same type");
289template <
typename Graph>
290std::shared_ptr<HDFGroup>
293 const std::string& name)
298 grp->add_attribute(
"content",
"graph");
300 grp->add_attribute(
"is_directed", boost::is_directed(
g));
301 grp->add_attribute(
"allows_parallel", boost::allows_parallel_edges(
g));
306 grp->add_attribute(
"edge_container_is_transposed",
true);
311 "keep_dim", std::vector<std::string>{
"vertex_idx",
"edge_idx"}
314 spdlog::get(
"data_io")->info(
"Opened graph group '{}'.", name);
330template <
typename Graph>
339 auto [
v,
v_end] = boost::vertices(
g);
341 return boost::get(boost::vertex_index_t(),
g,
vd);
346 auto [
e,
e_end] = boost::edges(
g);
348 return boost::get(boost::vertex_index_t(),
g, boost::source(
ed,
g));
352 return boost::get(boost::vertex_index_t(),
g, boost::target(
ed,
g));
355 spdlog::get(
"data_io")->debug(
"Graph saved.");
372template <
typename Graph,
typename PropertyMap>
375 const std::shared_ptr<HDFGroup>&
grp,
383 auto [
v,
v_end] = boost::vertices(
g);
389 auto [
e,
e_end] = boost::edges(
g);
398 spdlog::get(
"data_io")->debug(
"Graph saved.");
451 const std::shared_ptr<HDFGroup>&
nw_grp,
452 const std::string&
label,
459 std::tuple_element_t<0, std::decay_t<Adaptors>>>...>,
460 "Error, the first entry of each entry of 'adaptor_tuple' has to be "
461 "string like, to name the adaptor");
466 "Has to be either 'IterateOver::vertices' or 'IterateOver::edges'"
473 auto log = spdlog::get(
"data_io");
485 auto it_pair = GraphUtils::iterator_pair<IterateOver::vertices>(
g);
497 log->debug(
"Graph vertex properties saved with label '{}'.",
label);
506 auto it_pair = GraphUtils::iterator_pair<IterateOver::edges>(
g);
518 log->debug(
"Graph edge properties saved with label '{}'.",
label);
540template <
typename Graph,
typename...
Adaptors>
543 const std::shared_ptr<HDFGroup>&
nw_grp,
544 const std::string&
label,
571template <
typename Graph,
typename...
Adaptors>
574 const std::shared_ptr<HDFGroup>&
nw_grp,
575 const std::string&
label,
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
IterateOver
Over which graph entity to iterate.
Definition iterator.hh:19
@ vertices
Iterate over vertices.
@ edges
Iterate over edges.
void save_edge_properties(Graph &&g, const std::shared_ptr< HDFGroup > &nw_grp, const std::string &label, const std::tuple< Adaptors... > &adaptor_tuple)
Definition graph_utils.hh:573
std::shared_ptr< HDFGroup > create_graph_group(const Graph &g, const std::shared_ptr< HDFGroup > &parent_grp, const std::string &name)
Definition graph_utils.hh:291
void save_graph(const Graph &g, const std::shared_ptr< HDFGroup > &grp)
Write function for a boost::Graph.
Definition graph_utils.hh:332
void save_vertex_properties(Graph &&g, const std::shared_ptr< HDFGroup > &nw_grp, const std::string &label, const std::tuple< Adaptors... > &adaptor_tuple)
Definition graph_utils.hh:542
void save_graph_entity_properties(const Graph &g, const std::shared_ptr< HDFGroup > &nw_grp, const std::string &label, const std::tuple< Adaptors... > &adaptor_tuple)
Definition graph_utils.hh:450
This is the central file of the HDF5 dataIO module of Utopia and provides a class for writing to,...
This file provides a class for creating and managing groups in a HDF5 file, which then can create oth...
auto generate_write_function(const Graph &g, NWGroup &&nw_grp, std::string label, ItPair &&it_pair, std::size_t num_entities)
Definition graph_utils.hh:134
EntityKind
Kinds of Entities to get properties from.
Definition graph_utils.hh:102
constexpr std::tuple< Tail... > tuple_tail(const std::tuple< First, Second, Tail... > &t)
Builds new tuple containing all elements but the first two.
Definition graph_utils.hh:81
std::pair< std::shared_ptr< HDFDataset >, std::shared_ptr< HDFDataset > > setup_graph_containers(const Graph &g, const std::shared_ptr< HDFGroup > &grp)
Definition graph_utils.hh:50
auto coords_container()
Definition graph_utils.hh:91
constexpr bool is_callable_v
Shorthand for is_callable<T>::value.
Definition type_traits.hh:734
constexpr bool is_string_v
Shorthand for 'is_string<T>::value'.
Definition type_traits.hh:140
Check if a type T is a string-like type, i.e. std::basic_string, const char*, char*,...
Definition type_traits.hh:105