diff --git a/cpp/hdf5_tools.cpp b/cpp/hdf5_tools.cpp index 27f47668f8735bff4e811f94fd69656d1e67a6fc..99c696d3cedcbbc66eec3869df8d8500671ec136 100644 --- a/cpp/hdf5_tools.cpp +++ b/cpp/hdf5_tools.cpp @@ -274,6 +274,44 @@ std::string hdf5_tools::read_string( } } +template <class partsize_t> +int hdf5_tools::write_particle_ID_pairs_with_single_rank( + const std::vector<partsize_t> v, + const hid_t group, + const std::string dset_name) +{ + TIMEZONE("hdf5_tools::write_particle_ID_pairs_with_single_rank"); + // the vector contains pair information, so its size must be a multiple of 2 + assert((v.size() % 2) == 0); + // file space creation + hid_t fspace; + hsize_t dims[2]; + dims[0] = v.size()/2; + dims[1] = 2; + fspace = H5Screate_simple(2, dims, NULL); + // create dataset + hsize_t dset_id = H5Dcreate( + group, + dset_name.c_str(), + hdf5_tools::hdf5_type_id<partsize_t>(), + fspace, + H5P_DEFAULT, + H5P_DEFAULT, + H5P_DEFAULT); + // write data + H5Dwrite( + dset_id, + hdf5_tools::hdf5_type_id<partsize_t>(), + H5S_ALL, + H5S_ALL, + H5P_DEFAULT, + &v.front()); + // clean up + H5Dclose(dset_id); + H5Sclose(fspace); + return EXIT_SUCCESS; +} + template std::vector<int> hdf5_tools::read_vector<int>( const hid_t, diff --git a/cpp/hdf5_tools.hpp b/cpp/hdf5_tools.hpp index 82c004869f253c36105d636cf6f91a962166b93f..d3ea1226aa62197ef2ee442f9e66afd0aba9cc86 100644 --- a/cpp/hdf5_tools.hpp +++ b/cpp/hdf5_tools.hpp @@ -33,6 +33,65 @@ namespace hdf5_tools { + // see https://support.hdfgroup.org/HDF5/doc/H5.user/Datatypes.html + template <typename data_type> hid_t hdf5_type_id(); + template <> hid_t hdf5_type_id<char>() + { + return H5T_NATIVE_CHAR; + } + template <> hid_t hdf5_type_id<signed char>() + { + return H5T_NATIVE_SCHAR; + } + template <> hid_t hdf5_type_id<unsigned char>() + { + return H5T_NATIVE_UCHAR; + } + template <> hid_t hdf5_type_id<short>() + { + return H5T_NATIVE_SHORT; + } + template <> hid_t hdf5_type_id<unsigned short>() + { + return H5T_NATIVE_USHORT; + } + template <> hid_t hdf5_type_id<int>() + { + return H5T_NATIVE_INT; + } + template <> hid_t hdf5_type_id<unsigned>() + { + return H5T_NATIVE_UINT; + } + template <> hid_t hdf5_type_id<long>() + { + return H5T_NATIVE_LONG; + } + template <> hid_t hdf5_type_id<unsigned long>() + { + return H5T_NATIVE_ULONG; + } + template <> hid_t hdf5_type_id<long long>() + { + return H5T_NATIVE_LLONG; + } + template <> hid_t hdf5_type_id<unsigned long long>() + { + return H5T_NATIVE_ULLONG; + } + template <> hid_t hdf5_type_id<float>() + { + return H5T_NATIVE_FLOAT; + } + template <> hid_t hdf5_type_id<double>() + { + return H5T_NATIVE_DOUBLE; + } + template <> hid_t hdf5_type_id<long double>() + { + return H5T_NATIVE_LDOUBLE; + } + int grow_single_dataset( hid_t dset, int tincrement); @@ -90,6 +149,12 @@ namespace hdf5_tools const hid_t group, const std::string dset_name, const number value); + + template <class partsize_t> + int write_particle_ID_pairs_with_single_rank( + const std::vector<partsize_t> v, + const hid_t group, + const std::string dset_name); } #endif//HDF5_TOOLS_HPP