packages.biu: add hdf5

This commit is contained in:
2024-08-22 20:57:44 +08:00
parent 89b04882c1
commit 0d75fc01d4
7 changed files with 62 additions and 4 deletions

View File

@@ -17,12 +17,13 @@ find_package(Boost REQUIRED COMPONENTS headers iostreams filesystem)
find_package(range-v3 REQUIRED)
find_path(NAMEOF_INCLUDE_DIR nameof.hpp REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(HighFive REQUIRED)
add_library(biu src/common.cpp)
add_library(biu src/common.cpp src/hdf5.cpp)
target_include_directories(biu PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> ${NAMEOF_INCLUDE_DIR} ${ZPP_BITS_INCLUDE_DIR})
target_link_libraries(biu PUBLIC magic_enum::magic_enum fmt::fmt Boost::headers Boost::iostreams Boost::filesystem
range-v3::range-v3 Eigen3::Eigen)
range-v3::range-v3 Eigen3::Eigen HighFive_HighFive)
set_property(TARGET biu PROPERTY CXX_STANDARD 23 CXX_STANDARD_REQUIRED ON)
install(TARGETS biu EXPORT biuTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}

View File

@@ -5,3 +5,4 @@ find_package(Boost REQUIRED COMPONENTS headers iostreams)
find_package(Eigen3 REQUIRED)
find_package(range-v3 REQUIRED)
find_path(NAMEOF_INCLUDE_DIR nameof.hpp REQUIRED)
find_package(HighFive REQUIRED)

View File

@@ -1,11 +1,11 @@
{
stdenv, cmake, lib,
magic-enum, fmt, boost, eigen, range-v3, nameof, zpp-bits
magic-enum, fmt, boost, eigen, range-v3, nameof, zpp-bits, highfive
}: stdenv.mkDerivation rec
{
name = "biu";
src = ./.;
buildInputs = [ magic-enum fmt boost range-v3 nameof zpp-bits eigen ];
buildInputs = [ magic-enum fmt boost range-v3 nameof zpp-bits eigen highfive ];
propagatedBuildInputs = buildInputs;
nativeBuildInputs = [ cmake ];
doCheck = true;

View File

@@ -6,6 +6,7 @@
# include <biu/string.tpp>
# include <biu/format.tpp>
# include <biu/eigen.tpp>
# include <biu/hdf5.tpp>
// # include <biu/logger.tpp>
// # include <biu/smartref.tpp>

View File

@@ -0,0 +1,28 @@
# pragma once
# include <highfive/H5File.hpp>
namespace biu
{
namespace hdf5
{
struct PhonopyComplex { double r, i; };
namespace detail_
{
HighFive::CompoundType create_phonopy_complex();
}
class Hdf5file
{
public:
Hdf5file(std::string filename, bool readonly = false);
template <typename T> Hdf5file& read(std::string name, T& object);
template <typename T> T read(std::string name);
template <typename T> Hdf5file& write(std::string name, const T& object);
protected:
HighFive::File File_;
};
}
using hdf5::Hdf5file, hdf5::PhonopyComplex;
}
HIGHFIVE_REGISTER_TYPE(biu::hdf5::PhonopyComplex, biu::hdf5::detail_::create_phonopy_complex)

View File

@@ -0,0 +1,12 @@
# pragma once
# include <biu/hdf5.hpp>
namespace biu::hdf5
{
template <typename T> Hdf5file& Hdf5file::read(std::string name, T& object)
{ object = File_.getDataSet(name).read<std::remove_cvref_t<decltype(object)>>(); return *this; }
template <typename T> T Hdf5file::read(std::string name)
{ std::remove_cvref_t<T> object; read(name, object); return object; }
template <typename T> Hdf5file& Hdf5file::write(std::string name, const T& object)
{ File_.createDataSet(name, object); return *this; }
}

15
packages/biu/src/hdf5.cpp Normal file
View File

@@ -0,0 +1,15 @@
# include <biu/hdf5.hpp>
namespace biu::hdf5
{
Hdf5file::Hdf5file(std::string filename, bool readonly)
: File_
(
filename,
readonly ? HighFive::File::ReadOnly
: HighFive::File::ReadWrite | HighFive::File::Create | HighFive::File::Truncate
)
{}
HighFive::CompoundType detail_::create_phonopy_complex()
{ return {{ "r", HighFive::AtomicType<double>{}}, {"i", HighFive::AtomicType<double>{}}}; }
}