From 485c5db0fe28b04c867caf33c879c58f9b924d96 Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Tue, 6 Mar 2018 19:22:03 +0000 Subject: [PATCH] conversion of Grid tensors to nested std::vector in preparation for tensor serialisation --- lib/serialisation/BaseIO.h | 35 +++++++++++++++ tests/IO/Test_serialisation.cc | 78 +++++++--------------------------- 2 files changed, 51 insertions(+), 62 deletions(-) diff --git a/lib/serialisation/BaseIO.h b/lib/serialisation/BaseIO.h index 24e1cec7..1af5acc6 100644 --- a/lib/serialisation/BaseIO.h +++ b/lib/serialisation/BaseIO.h @@ -31,6 +31,7 @@ Author: Guido Cossu #define GRID_SERIALISATION_ABSTRACT_READER_H #include +#include namespace Grid { // Vector IO utilities /////////////////////////////////////////////////////// @@ -69,6 +70,40 @@ namespace Grid { return os; } + // convert Grid scalar tensors to std::vectors + template + void tensorToVec(V &v, const T& t) + { + v = t; + } + + template + void tensorToVec(V &v, const iScalar& t) + { + tensorToVec(v, t._internal); + } + + template + void tensorToVec(std::vector &v, const iVector& t) + { + v.resize(N); + for (unsigned int i = 0; i < N; i++) + { + tensorToVec(v[i], t._internal[i]); + } + } + + template + void tensorToVec(std::vector> &v, const iMatrix& t) + { + v.resize(N, std::vector(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 struct element diff --git a/tests/IO/Test_serialisation.cc b/tests/IO/Test_serialisation.cc index 82638ad9..cdafd5c0 100644 --- a/tests/IO/Test_serialisation.cc +++ b/tests/IO/Test_serialisation.cc @@ -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 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 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>>> scmv; + std::vector> scvv; + rng.SeedFixedIntegers(std::vector({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; }