HDF5 (TRIQS/h5 Integration)
clair/c2py integrates with the TRIQS/h5 library, enabling serialization and deserialization of C++ classes to HDF5 files from Python.
If your C++ class implements HDF5 support via TRIQS/h5 (the h5_write / h5_read friend functions), clair automatically makes the wrapped objects usable with the TRIQS/h5 HDFArchive.
C++ Example:
// HDF5 serialization
#include <c2py/c2py.hpp>
#include <h5/h5.hpp>
#include <vector>
namespace mylib {
class matrix {
std::vector<std::vector<double>> data_;
public:
matrix(size_t rows, size_t cols) : data_(rows, std::vector<double>(cols, 0.0)) {}
// HDF5 write
friend void h5_write(h5::group g, std::string const &name, matrix const &m) { h5::write(g, name, m.data_); }
// HDF5 read
friend void h5_read(h5::group g, std::string const &name, matrix &m) { h5::read(g, name, m.data_); }
};
} // namespace mylib
Python Usage:
The wrapped type can be stored in and retrieved from a TRIQS/h5 HDFArchive:
from h5 import HDFArchive
from hdf5 import Matrix # module 'hdf5', class mylib::matrix -> Matrix
m = Matrix(3, 3)
# Write to an HDF5 archive
with HDFArchive("data.h5", "w") as ar:
ar["matrix"] = m
# Read it back
with HDFArchive("data.h5", "r") as ar:
m2 = ar["matrix"]
Notes:
The HDF5 interface relies on the TRIQS/h5 C++ library for the serialization logic (the
h5_write/h5_readfriends shown above).For each class with HDF5 support, clair generates a
__write_hdf5__method and registers the type with theh5.formatsregistry, so it works transparently with the TRIQS/h5HDFArchive.