HDF5 (TRIQS/h5 Integration)

clair/c2py provides seamless integration with the TRIQS/h5 library, enabling automatic serialization and deserialization of C++ classes to HDF5 files from Python.

If your C++ class implements HDF5 support via TRIQS/h5, clair will automatically expose corresponding Python methods for saving and loading objects.

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:

from mymodule import Matrix
import h5py

m = Matrix(3, 3)
# Save to HDF5
with h5py.File('data.h5', 'w') as f:
    m.save_to_hdf5(f, 'matrix')

# Load from HDF5
with h5py.File('data.h5', 'r') as f:
    m2 = Matrix.load_from_hdf5(f, 'matrix')

Notes:

  • The HDF5 interface relies on the TRIQS/h5 C++ library for serialization logic.

  • Python bindings automatically mirror the C++ API, making it easy to persist and restore objects.

  • You can use any HDF5-compatible Python library (e.g., h5py) for file management.