1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-09-20 01:05:38 +01:00

conversion of Grid tensors to nested std::vector in preparation for tensor serialisation

This commit is contained in:
Antonin Portelli 2018-03-06 19:22:03 +00:00
parent c399c2b44d
commit 485c5db0fe
2 changed files with 51 additions and 62 deletions

View File

@ -31,6 +31,7 @@ Author: Guido Cossu <guido.cossu@ed.ac.uk>
#define GRID_SERIALISATION_ABSTRACT_READER_H
#include <type_traits>
#include <Grid/tensors/Tensors.h>
namespace Grid {
// Vector IO utilities ///////////////////////////////////////////////////////
@ -69,6 +70,40 @@ namespace Grid {
return os;
}
// convert Grid scalar tensors to std::vectors
template <typename T, typename V>
void tensorToVec(V &v, const T& t)
{
v = t;
}
template <typename T, typename V>
void tensorToVec(V &v, const iScalar<T>& t)
{
tensorToVec(v, t._internal);
}
template <typename T, typename V, int N>
void tensorToVec(std::vector<V> &v, const iVector<T, N>& t)
{
v.resize(N);
for (unsigned int i = 0; i < N; i++)
{
tensorToVec(v[i], t._internal[i]);
}
}
template <typename T, typename V, int N>
void tensorToVec(std::vector<std::vector<V>> &v, const iMatrix<T, N>& t)
{
v.resize(N, std::vector<V>(N));
for (unsigned int i = 0; i < N; i++)
for (unsigned int j = 0; j < N; j++)
{
tensorToVec(v[i][j], t._internal[i][j]);
}
}
// Vector element trait //////////////////////////////////////////////////////
template <typename T>
struct element

View File

@ -197,68 +197,22 @@ int main(int argc,char **argv)
std::cout << flatdv.getVector() << std::endl;
std::cout << std::endl;
std::cout << "==== Grid tensor to vector test" << std::endl;
std::cout << ".:::::: Testing JSON classes "<< std::endl;
{
JSONWriter JW("bother.json");
// test basic type writing
myenum a = myenum::red;
push(JW,"BasicTypes");
write(JW,std::string("i16"),i16);
write(JW,"myenum",a);
write(JW,"u16",u16);
write(JW,"i32",i32);
write(JW,"u32",u32);
write(JW,"i64",i64);
write(JW,"u64",u64);
write(JW,"f",f);
write(JW,"d",d);
write(JW,"b",b);
pop(JW);
// test serializable class writing
myclass obj(1234); // non-trivial constructor
std::cout << obj << std::endl;
std::cout << "-- serialisable class writing to 'bother.json'..." << std::endl;
write(JW,"obj",obj);
JW.write("obj2", obj);
std::vector<myclass> vec;
vec.push_back(myclass(1234));
vec.push_back(myclass(5678));
vec.push_back(myclass(3838));
write(JW, "objvec", vec);
}
{
JSONReader RD("bother.json");
myclass jcopy1;
std::vector<myclass> jveccopy1;
read(RD,"obj",jcopy1);
read(RD,"objvec", jveccopy1);
std::cout << "Loaded (JSON) -----------------" << std::endl;
std::cout << jcopy1 << std::endl << jveccopy1 << std::endl;
}
/*
// This is still work in progress
{
// Testing the next element function
JSONReader RD("test.json");
RD.push("grid");
RD.push("Observable");
std::string name;
read(RD,"name", name);
}
*/
GridSerialRNG rng;
SpinColourMatrix scm;
SpinColourVector scv;
std::vector<std::vector<std::vector<std::vector<ComplexD>>>> scmv;
std::vector<std::vector<ComplexD>> scvv;
rng.SeedFixedIntegers(std::vector<int>({42,10,81,9}));
random(rng, scm);
random(rng, scv);
std::cout << "Test spin-color matrix: " << scm << std::endl;
std::cout << "Test spin-color vector: " << scv << std::endl;
std::cout << "Converting to std::vector" << std::endl;
tensorToVec(scmv, scm);
tensorToVec(scvv, scv);
std::cout << "Spin-color matrix: " << scmv << std::endl;
std::cout << "Spin-color vector: " << scvv << std::endl;
}