1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-07-29 18:57:08 +01:00

Lattice serialisation, just HDF5 for the moment

This commit is contained in:
2018-03-26 19:16:16 +01:00
parent 1c680d4b7a
commit 8a0cf0194f
3 changed files with 199 additions and 2 deletions

View File

@@ -22,6 +22,8 @@
namespace Grid
{
template<class vobj> class Lattice;
class Hdf5Writer: public Writer<Hdf5Writer>
{
public:
@@ -33,6 +35,8 @@ namespace Grid
template <typename U>
void writeDefault(const std::string &s, const U &x);
template <typename U>
void writeDefault(const std::string &s, const Lattice<U> &field);
template <typename U>
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
writeDefault(const std::string &s, const std::vector<U> &x);
template <typename U>
@@ -60,6 +64,8 @@ namespace Grid
template <typename U>
void readDefault(const std::string &s, U &output);
template <typename U>
void readDefault(const std::string &s, Lattice<U> &field);
template <typename U>
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
readDefault(const std::string &s, std::vector<U> &x);
template <typename U>
@@ -98,7 +104,40 @@ namespace Grid
template <>
void Hdf5Writer::writeDefault(const std::string &s, const std::string &x);
template <typename U>
void Hdf5Writer::writeDefault(const std::string &s, const Lattice<U> &field)
{
// alias scalar types
typedef std::vector<typename U::scalar_object> ScalarLattice;
typedef typename U::scalar_type ScalarType;
ScalarLattice scalField;
unvectorizeToRevLexOrdArray(scalField, field);
std::vector<hsize_t> dim;
std::vector<size_t> tDim;
tensorDim(tDim, scalField[0]);
for (auto &d: field._grid->GlobalDimensions())
{
dim.push_back(d);
}
for (auto &d: tDim)
{
dim.push_back(d);
}
// write to file
H5NS::DataSpace dataSpace(dim.size(), dim.data());
H5NS::DataSet dataSet;
dataSet = group_.createDataSet(s, Hdf5Type<ScalarType>::type(), dataSpace);
dataSet.write(scalField.data(), Hdf5Type<ScalarType>::type());
}
template <typename U>
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
Hdf5Writer::writeDefault(const std::string &s, const std::vector<U> &x)
@@ -169,6 +208,29 @@ namespace Grid
template <>
void Hdf5Reader::readDefault(const std::string &s, std::string &x);
template <typename U>
void Hdf5Reader::readDefault(const std::string &s, Lattice<U> &field)
{
// alias scalar types
typedef std::vector<typename U::scalar_object> ScalarLattice;
typedef typename U::scalar_type ScalarType;
ScalarLattice scalField;
H5NS::DataSet dataSet;
std::vector<hsize_t> dim;
hsize_t size = 1;
dataSet = group_.openDataSet(s);
for (auto &d: field._grid->GlobalDimensions())
{
dim.push_back(d);
size *= d;
}
scalField.resize(size);
dataSet.read(scalField.data(), Hdf5Type<ScalarType>::type());
vectorizeFromRevLexOrdArray(scalField, field);
}
template <typename U>
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
Hdf5Reader::readDefault(const std::string &s, std::vector<U> &x)

View File

@@ -2,7 +2,7 @@
#define GRID_SERIALISATION_VECTORUTILS_H
#include <type_traits>
#include <Grid/tensors/Tensors.h>
#include <Grid/tensors/Tensors.h>
namespace Grid {
// Pair IO utilities /////////////////////////////////////////////////////////
@@ -78,6 +78,48 @@ namespace Grid {
typedef typename std::vector<std::vector<typename TensorToVec<T>::type>> type;
};
template <typename T>
void tensorDim(std::vector<size_t> &dim, const T &t, const bool wipe = true)
{
if (wipe)
{
dim.clear();
}
}
template <typename T>
void tensorDim(std::vector<size_t> &dim, const iScalar<T> &t, const bool wipe = true)
{
if (wipe)
{
dim.clear();
}
tensorDim(dim, t._internal, false);
}
template <typename T, int N>
void tensorDim(std::vector<size_t> &dim, const iVector<T, N> &t, const bool wipe = true)
{
if (wipe)
{
dim.clear();
}
dim.push_back(N);
tensorDim(dim, t._internal[0], false);
}
template <typename T, int N>
void tensorDim(std::vector<size_t> &dim, const iMatrix<T, N> &t, const bool wipe = true)
{
if (wipe)
{
dim.clear();
}
dim.push_back(N);
dim.push_back(N);
tensorDim(dim, t._internal[0][0], false);
}
template <typename T>
typename TensorToVec<T>::type tensorToVec(const T &t)
{