diff --git a/dune/fufem/hdf5/CMakeLists.txt b/dune/fufem/hdf5/CMakeLists.txt index 31f8a6b04ca18936d55d19c9c8a7e31b1caee760..5d9c05173a323c83595395bfb8aebbdea83bc061 100644 --- a/dune/fufem/hdf5/CMakeLists.txt +++ b/dune/fufem/hdf5/CMakeLists.txt @@ -1,9 +1,9 @@ install(FILES + attributes.hh + file.hh frombuffer.hh - hdf5-attributes.hh - hdf5-sequence-io.hh - hdf5-singleton-writer.hh - hdf5file.hh - hdf5typetraits.hh + sequenceio.hh + singletonwriter.hh tobuffer.hh + typetraits.hh DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dune/fufem/hdf5) diff --git a/dune/fufem/hdf5/hdf5-attributes.hh b/dune/fufem/hdf5/attributes.hh similarity index 87% rename from dune/fufem/hdf5/hdf5-attributes.hh rename to dune/fufem/hdf5/attributes.hh index 31ed0395413177cb72d0b6de54a736587fed4bca..35fc6647eb5654e8e66317387d80fef4d3c04a3c 100644 --- a/dune/fufem/hdf5/hdf5-attributes.hh +++ b/dune/fufem/hdf5/attributes.hh @@ -1,15 +1,16 @@ -#ifndef DUNE_FUFEM_HDF5_HDF5_ATTRIBUTES_HH -#define DUNE_FUFEM_HDF5_HDF5_ATTRIBUTES_HH +#ifndef DUNE_FUFEM_HDF5_ATTRIBUTES_HH +#define DUNE_FUFEM_HDF5_ATTRIBUTES_HH #include <string> #include <hdf5.h> -#include "hdf5file.hh" +#include "file.hh" -class HDF5Attribute { +namespace HDF5 { +class Attribute { public: - HDF5Attribute(HDF5Grouplike &grouplike, std::string key) + Attribute(Grouplike &grouplike, std::string key) : grouplike_(grouplike), key_(key) {} bool exists() { return H5Aexists(grouplike_.c_obj(), key_.c_str()); } @@ -56,7 +57,8 @@ public: } private: - HDF5Grouplike &grouplike_; + Grouplike &grouplike_; std::string key_; }; +} #endif diff --git a/dune/fufem/hdf5/hdf5file.hh b/dune/fufem/hdf5/file.hh similarity index 84% rename from dune/fufem/hdf5/hdf5file.hh rename to dune/fufem/hdf5/file.hh index 4936fe4001972afe7b595a3388682ab4b08df3ed..1e9d25080f6536be2b4e83ff2fc3aa1488cf973c 100644 --- a/dune/fufem/hdf5/hdf5file.hh +++ b/dune/fufem/hdf5/file.hh @@ -1,12 +1,15 @@ -#ifndef DUNE_FUFEM_HDF5_HDF5FILE_HH -#define DUNE_FUFEM_HDF5_HDF5FILE_HH +#ifndef DUNE_FUFEM_HDF5_FILE_HH +#define DUNE_FUFEM_HDF5_FILE_HH #include <sys/stat.h> #include <string> #include <hdf5.h> -class HDF5Grouplike { +#include <dune/common/exceptions.hh> + +namespace HDF5 { +class Grouplike { public: bool hasDataset(std::string name) { return H5Lexists(c_obj(), name.c_str(), H5P_DEFAULT); @@ -25,9 +28,9 @@ public: virtual hid_t c_obj() = 0; }; -class HDF5File : public HDF5Grouplike { +class File : public Grouplike { public: - HDF5File(std::string filename, bool swmr = false) { + File(std::string filename, bool swmr = false) { struct stat buffer; /* Use the latest HDF5 format. @@ -60,13 +63,11 @@ public: void flush() { H5Fflush(file_, H5F_SCOPE_LOCAL); } - ~HDF5File() { H5Fclose(file_); } + ~File() { H5Fclose(file_); } hid_t c_obj() override { return file_; } - bool swmrEnabled() { - return swmrEnabled_; - } + bool swmrEnabled() { return swmrEnabled_; } void swmrEnable() { if (swmrEnabled_) return; @@ -86,21 +87,22 @@ private: bool swmrEnabled_ = false; }; -class HDF5Group : public HDF5Grouplike { +class Group : public Grouplike { public: - HDF5Group(HDF5Grouplike &parent, std::string groupname) : parent_(parent) { + Group(Grouplike &parent, std::string groupname) : parent_(parent) { group_ = H5Lexists(parent_.c_obj(), groupname.c_str(), H5P_DEFAULT) ? H5Gopen(parent_.c_obj(), groupname.c_str(), H5P_DEFAULT) : H5Gcreate(parent_.c_obj(), groupname.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } - ~HDF5Group() { H5Gclose(group_); } + ~Group() { H5Gclose(group_); } hid_t c_obj() override { return group_; } private: - HDF5Grouplike &parent_; + Grouplike &parent_; hid_t group_; }; +} #endif diff --git a/dune/fufem/hdf5/hdf5-sequence-io.hh b/dune/fufem/hdf5/sequenceio.hh similarity index 84% rename from dune/fufem/hdf5/hdf5-sequence-io.hh rename to dune/fufem/hdf5/sequenceio.hh index 9768d14fee268d0aeae2539157c4d44b2163fc8d..ff033b25f01312b2b20e5cd4ec43eaf3d6855778 100644 --- a/dune/fufem/hdf5/hdf5-sequence-io.hh +++ b/dune/fufem/hdf5/sequenceio.hh @@ -1,5 +1,5 @@ -#ifndef DUNE_FUFEM_HDF5_HDF5_SEQUENCE_IO_HH -#define DUNE_FUFEM_HDF5_HDF5_SEQUENCE_IO_HH +#ifndef DUNE_FUFEM_HDF5_SEQUENCEIO_HH +#define DUNE_FUFEM_HDF5_SEQUENCEIO_HH #include <numeric> @@ -10,19 +10,20 @@ #include <dune/istl/bvector.hh> #include <dune/istl/matrix.hh> -#include "hdf5file.hh" -#include "hdf5typetraits.hh" +#include "file.hh" #include "frombuffer.hh" #include "tobuffer.hh" +#include "typetraits.hh" +namespace HDF5 { template <int spatialDimensions, typename ctype = double, typename T = hsize_t> -class HDF5SequenceIO { +class SequenceIO { int static const dimensions = 1 + spatialDimensions; public: template <typename... Args> - HDF5SequenceIO(HDF5Grouplike &file, std::string datasetname, Args... args) - : file_(file), capacity_{ { T(args)... } } { + SequenceIO(Grouplike &file, std::string datasetname, Args... args) + : file_(file), capacity_{{T(args)...}} { static_assert(sizeof...(args) == spatialDimensions, "wrong number of arguments"); @@ -43,14 +44,14 @@ public: H5Pset_chunk(plist, dimensions, chunk_dims.data()); dset_ = file_.createDataset(datasetname, file_space, plist, - HDF5TypeTraits<ctype>::getType()); + TypeTraits<ctype>::getType()); H5Pclose(plist); H5Sclose(file_space); } } - ~HDF5SequenceIO() { H5Dclose(dset_); } + ~SequenceIO() { H5Dclose(dset_); } void add(size_t timeStep, std::vector<ctype> const &buffer) { if (buffer.size() != entrySize()) @@ -71,7 +72,7 @@ public: if (buffer.size() != entrySize()) DUNE_THROW(Dune::Exception, "buffer size incorrect"); - H5DOappend(dset_, H5P_DEFAULT, skip, 1, HDF5TypeTraits<ctype>::getType(), + H5DOappend(dset_, H5P_DEFAULT, skip, 1, TypeTraits<ctype>::getType(), buffer.data()); } #endif @@ -93,7 +94,7 @@ public: count.data(), NULL); hid_t mem_space = H5Screate_simple(dimensions, count.data(), NULL); - H5Dread(dset_, HDF5TypeTraits<ctype>::getType(), mem_space, file_space, + H5Dread(dset_, TypeTraits<ctype>::getType(), mem_space, file_space, H5P_DEFAULT, buffer.data()); H5Sclose(mem_space); @@ -147,7 +148,7 @@ private: } void checkDatatype() { - if (!H5Tequal(H5Dget_type(dset_), HDF5TypeTraits<ctype>::getType())) + if (!H5Tequal(H5Dget_type(dset_), TypeTraits<ctype>::getType())) DUNE_THROW(Dune::Exception, "unexpected data type"); } @@ -165,15 +166,15 @@ private: return ret; } - HDF5Grouplike &file_; + Grouplike &file_; std::array<T, spatialDimensions> const capacity_; hid_t dset_; }; template <int spatialDimensions, typename ctype, typename T, class Data> -void addEntry(HDF5SequenceIO<spatialDimensions, ctype, T> &writer, - size_t timeStep, Data const &data) { +void addEntry(SequenceIO<spatialDimensions, ctype, T> &writer, size_t timeStep, + Data const &data) { std::vector<ctype> buffer; toBuffer(data, buffer); writer.add(timeStep, buffer); @@ -181,7 +182,7 @@ void addEntry(HDF5SequenceIO<spatialDimensions, ctype, T> &writer, #if H5_VERSION_GE(1, 10, 0) template <int spatialDimensions, typename ctype, typename T, class Data> -void appendEntry(HDF5SequenceIO<spatialDimensions, ctype, T> &writer, +void appendEntry(SequenceIO<spatialDimensions, ctype, T> &writer, Data const &data) { std::vector<ctype> buffer; toBuffer(data, buffer); @@ -190,11 +191,12 @@ void appendEntry(HDF5SequenceIO<spatialDimensions, ctype, T> &writer, #endif template <int spatialDimensions, typename ctype, typename T, class Data> -void readEntry(HDF5SequenceIO<spatialDimensions, ctype, T> &reader, - size_t timeStep, Data &data) { +void readEntry(SequenceIO<spatialDimensions, ctype, T> &reader, size_t timeStep, + Data &data) { std::vector<ctype> buffer; std::array<T, spatialDimensions> dimensions; reader.read(timeStep, buffer, dimensions); fromBuffer(buffer, dimensions, data); } +} #endif diff --git a/dune/fufem/hdf5/hdf5-singleton-writer.hh b/dune/fufem/hdf5/singletonwriter.hh similarity index 80% rename from dune/fufem/hdf5/hdf5-singleton-writer.hh rename to dune/fufem/hdf5/singletonwriter.hh index e861741e69475a1cb9d22a33a330a5c6a471abdd..fcd3571a8b39958932773a1454b5ca5edd7f65c2 100644 --- a/dune/fufem/hdf5/hdf5-singleton-writer.hh +++ b/dune/fufem/hdf5/singletonwriter.hh @@ -1,5 +1,5 @@ -#ifndef DUNE_FUFEM_HDF5_HDF5_SINGLETON_WRITER_HH -#define DUNE_FUFEM_HDF5_HDF5_SINGLETON_WRITER_HH +#ifndef DUNE_FUFEM_HDF5_SINGLETONWRITER_HH +#define DUNE_FUFEM_HDF5_SINGLETONWRITER_HH #include <numeric> @@ -9,20 +9,20 @@ #include <dune/istl/bvector.hh> #include <dune/istl/matrix.hh> -#include "hdf5file.hh" -#include "hdf5typetraits.hh" +#include "file.hh" #include "tobuffer.hh" +#include "typetraits.hh" +namespace HDF5 { template <int spatialDimensions, typename ctype = double, typename T = hsize_t> -class HDF5SingletonWriter { +class SingletonWriter { private: int static const dimensions = spatialDimensions; public: template <typename... Args> - HDF5SingletonWriter(HDF5Grouplike &file, std::string datasetname, - Args... args) - : file_(file), capacity_{ { T(args)... } } { + SingletonWriter(Grouplike &file, std::string datasetname, Args... args) + : file_(file), capacity_{{T(args)...}} { static_assert(sizeof...(args) == spatialDimensions, "wrong number of arguments"); @@ -40,14 +40,14 @@ public: hid_t plist = H5Pcreate(H5P_DATASET_CREATE); H5Pset_layout(plist, H5D_CONTIGUOUS); dset_ = file_.createDataset(datasetname, file_space, plist, - HDF5TypeTraits<ctype>::getType()); + TypeTraits<ctype>::getType()); H5Pclose(plist); H5Sclose(file_space); } } - ~HDF5SingletonWriter() { H5Dclose(dset_); } + ~SingletonWriter() { H5Dclose(dset_); } void set(std::vector<ctype> const &buffer) { if (buffer.size() != entrySize()) @@ -60,7 +60,7 @@ public: count.data(), NULL); hid_t mem_space = H5Screate_simple(dimensions, count.data(), NULL); - H5Dwrite(dset_, HDF5TypeTraits<ctype>::getType(), mem_space, file_space, + H5Dwrite(dset_, TypeTraits<ctype>::getType(), mem_space, file_space, H5P_DEFAULT, buffer.data()); H5Sclose(mem_space); H5Sclose(file_space); @@ -95,11 +95,10 @@ private: } void checkDatatype() { - if (!H5Tequal(H5Dget_type(dset_), HDF5TypeTraits<ctype>::getType())) + if (!H5Tequal(H5Dget_type(dset_), TypeTraits<ctype>::getType())) DUNE_THROW(Dune::Exception, "unexpected data type"); } - std::array<T, dimensions> minExtent() { std::array<T, dimensions> ret; std::fill(ret.begin(), ret.end(), 0); @@ -108,17 +107,18 @@ private: std::array<T, dimensions> maxExtent() { return capacity_; } - HDF5Grouplike &file_; + Grouplike &file_; std::array<T, spatialDimensions> const capacity_; hid_t dset_; }; template <int spatialDimensions, typename ctype, typename T, class Data> -void setEntry(HDF5SingletonWriter<spatialDimensions, ctype, T> &writer, +void setEntry(SingletonWriter<spatialDimensions, ctype, T> &writer, Data const &data) { std::vector<ctype> buffer; toBuffer(data, buffer); writer.set(buffer); } +} #endif diff --git a/dune/fufem/hdf5/hdf5typetraits.hh b/dune/fufem/hdf5/typetraits.hh similarity index 53% rename from dune/fufem/hdf5/hdf5typetraits.hh rename to dune/fufem/hdf5/typetraits.hh index 29ddb798391f3b80bd12168d9431963c3b9d969a..73b9b48f34393f104b7534f41d9a33a2fe1ef686 100644 --- a/dune/fufem/hdf5/hdf5typetraits.hh +++ b/dune/fufem/hdf5/typetraits.hh @@ -1,57 +1,74 @@ -#ifndef DUNE_FUFEM_HDF5_HDF5TYPETRAITS_HH -#define DUNE_FUFEM_HDF5_HDF5TYPETRAITS_HH +#ifndef DUNE_FUFEM_HDF5_TYPETRAITS_HH +#define DUNE_FUFEM_HDF5_TYPETRAITS_HH -template <typename ctype> struct HDF5TypeTraits { +namespace HDF5 { +template <typename ctype> +struct TypeTraits { static hid_t getType() { DUNE_THROW(Dune::Exception, "Not a recognised type."); } }; -template <> struct HDF5TypeTraits<float> { +template <> +struct TypeTraits<float> { static hid_t getType() { return H5T_NATIVE_FLOAT; } }; -template <> struct HDF5TypeTraits<double> { +template <> +struct TypeTraits<double> { static hid_t getType() { return H5T_NATIVE_DOUBLE; } }; -template <> struct HDF5TypeTraits<long double> { +template <> +struct TypeTraits<long double> { static hid_t getType() { return H5T_NATIVE_LDOUBLE; } }; -template <> struct HDF5TypeTraits<char> { +template <> +struct TypeTraits<char> { static hid_t getType() { return H5T_NATIVE_CHAR; } }; -template <> struct HDF5TypeTraits<signed char> { +template <> +struct TypeTraits<signed char> { static hid_t getType() { return H5T_NATIVE_SCHAR; } }; -template <> struct HDF5TypeTraits<unsigned char> { +template <> +struct TypeTraits<unsigned char> { static hid_t getType() { return H5T_NATIVE_UCHAR; } }; -template <> struct HDF5TypeTraits<short> { +template <> +struct TypeTraits<short> { static hid_t getType() { return H5T_NATIVE_SHORT; } }; -template <> struct HDF5TypeTraits<unsigned short> { +template <> +struct TypeTraits<unsigned short> { static hid_t getType() { return H5T_NATIVE_USHORT; } }; -template <> struct HDF5TypeTraits<int> { +template <> +struct TypeTraits<int> { static hid_t getType() { return H5T_NATIVE_INT; } }; -template <> struct HDF5TypeTraits<unsigned int> { +template <> +struct TypeTraits<unsigned int> { static hid_t getType() { return H5T_NATIVE_UINT; } }; -template <> struct HDF5TypeTraits<long> { +template <> +struct TypeTraits<long> { static hid_t getType() { return H5T_NATIVE_LONG; } }; -template <> struct HDF5TypeTraits<unsigned long> { +template <> +struct TypeTraits<unsigned long> { static hid_t getType() { return H5T_NATIVE_ULONG; } }; -template <> struct HDF5TypeTraits<long long> { +template <> +struct TypeTraits<long long> { static hid_t getType() { return H5T_NATIVE_LLONG; } }; -template <> struct HDF5TypeTraits<unsigned long long> { +template <> +struct TypeTraits<unsigned long long> { static hid_t getType() { return H5T_NATIVE_ULLONG; } }; +} #endif diff --git a/dune/fufem/test/test-hdf5.cc b/dune/fufem/test/test-hdf5.cc index 4dccb53f75b063df8b34e906f2aa822ac50ed4a1..626a321aaed9e0691fd416cf2393e129d1755809 100644 --- a/dune/fufem/test/test-hdf5.cc +++ b/dune/fufem/test/test-hdf5.cc @@ -7,9 +7,9 @@ #include <dune/istl/bvector.hh> -#include <dune/fufem/hdf5/hdf5file.hh> -#include <dune/fufem/hdf5/hdf5-sequence-io.hh> -#include <dune/fufem/hdf5/hdf5-singleton-writer.hh> +#include <dune/fufem/hdf5/file.hh> +#include <dune/fufem/hdf5/sequenceio.hh> +#include <dune/fufem/hdf5/singletonwriter.hh> void deleteIfNecessary(std::string filename) { struct stat buffer; @@ -19,16 +19,14 @@ void deleteIfNecessary(std::string filename) { } } - - template <class Vector> void checkAddSequence(Vector const &written0, Vector const &written1) { using ft = typename Vector::block_type::field_type; std::string const filename("out-sequence.h5"); { deleteIfNecessary(filename); - HDF5File file(filename.c_str()); - HDF5SequenceIO<2, ft> h5writer(file, "data", 2, 3); + HDF5::File file(filename.c_str()); + HDF5::SequenceIO<2, ft> h5writer(file, "data", 2, 3); addEntry(h5writer, 1, written1); addEntry(h5writer, 0, written0); } @@ -39,8 +37,8 @@ void checkAddSequence(Vector const &written0, Vector const &written1) { Vector read0; Vector read1; { - HDF5File file(filename.c_str()); - HDF5SequenceIO<2, ft> h5reader(file, "data", 2, 3); + HDF5::File file(filename.c_str()); + HDF5::SequenceIO<2, ft> h5reader(file, "data", 2, 3); readEntry(h5reader, 0, read0); readEntry(h5reader, 1, read1); } @@ -61,8 +59,8 @@ void checkAppendSequence(Vector const &written0, Vector const &written1) { std::string const filename("out-sequence.h5"); { deleteIfNecessary(filename); - HDF5File file(filename.c_str()); - HDF5SequenceIO<2, ft> h5writer(file, "data", 2, 3); + HDF5::File file(filename.c_str()); + HDF5::SequenceIO<2, ft> h5writer(file, "data", 2, 3); appendEntry(h5writer, written0); appendEntry(h5writer, written1); } @@ -73,8 +71,8 @@ void checkAppendSequence(Vector const &written0, Vector const &written1) { Vector read0; Vector read1; { - HDF5File file(filename.c_str()); - HDF5SequenceIO<2, ft> h5reader(file, "data", 2, 3); + HDF5::File file(filename.c_str()); + HDF5::SequenceIO<2, ft> h5reader(file, "data", 2, 3); readEntry(h5reader, 0, read0); readEntry(h5reader, 1, read1); } @@ -95,8 +93,8 @@ void checkSetSingleton(Vector const &written0) { std::string const filename = "out-singleton.h5"; { deleteIfNecessary(filename); - HDF5File file(filename); - HDF5SingletonWriter<2, ft> h5writer(file, "data", 2, 3); + HDF5::File file(filename); + HDF5::SingletonWriter<2, ft> h5writer(file, "data", 2, 3); setEntry(h5writer, written0); } }