1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 07:55:35 +00:00

std::vector to tensor conversion + test units

This commit is contained in:
Antonin Portelli 2018-03-08 09:50:39 +00:00
parent a7d19dbb64
commit 971c2379bd
2 changed files with 56 additions and 11 deletions

View File

@ -103,6 +103,38 @@ namespace Grid {
return v;
}
template <typename T>
void vecToTensor(T &t, const typename TensorToVec<T>::type &v)
{
t = v;
}
template <typename T>
void vecToTensor(iScalar<T> &t, const typename TensorToVec<iScalar<T>>::type &v)
{
vecToTensor(t._internal, v);
}
template <typename T, int N>
void vecToTensor(iVector<T, N> &t, const typename TensorToVec<iVector<T, N>>::type &v)
{
for (unsigned int i = 0; i < N; i++)
{
vecToTensor(t._internal[i], v[i]);
}
}
template <typename T, int N>
void vecToTensor(iMatrix<T, N> &t, const typename TensorToVec<iMatrix<T, N>>::type &v)
{
for (unsigned int i = 0; i < N; i++)
for (unsigned int j = 0; j < N; j++)
{
vecToTensor(t._internal[i][j], v[i][j]);
}
}
// Vector element trait //////////////////////////////////////////////////////
template <typename T>
struct element

View File

@ -93,6 +93,24 @@ void ioTest(const std::string &filename, const O &object, const std::string &nam
if (!good) exit(EXIT_FAILURE);
}
template <typename T>
void tensorConvTestFn(GridSerialRNG &rng, const std::string label)
{
T t, ft;
Real n;
bool good;
random(rng, t);
auto tv = tensorToVec(t);
vecToTensor(ft, tv);
n = norm2(t - ft);
good = (n == 0);
std::cout << label << " norm 2 diff: " << n << " -- "
<< (good ? "success" : "failure") << std::endl;
}
#define tensorConvTest(rng, type) tensorConvTestFn<type>(rng, #type)
int main(int argc,char **argv)
{
std::cout << "==== basic IO" << std::endl;
@ -200,17 +218,12 @@ int main(int argc,char **argv)
std::cout << "==== Grid tensor to vector test" << std::endl;
GridSerialRNG rng;
SpinColourMatrix scm;
SpinColourVector scv;
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;
auto scmv = tensorToVec(scm);
auto scvv = tensorToVec(scv);
std::cout << "Spin-color matrix: " << scmv << std::endl;
std::cout << "Spin-color vector: " << scvv << std::endl;
tensorConvTest(rng, SpinColourMatrix);
tensorConvTest(rng, SpinColourVector);
tensorConvTest(rng, ColourMatrix);
tensorConvTest(rng, ColourVector);
tensorConvTest(rng, SpinMatrix);
tensorConvTest(rng, SpinVector);
}