From b8c299c4af53d5ce1822d6415afea3a6ec6633ca Mon Sep 17 00:00:00 2001 From: kronbichler Date: Tue, 2 Aug 2011 12:17:17 +0000 Subject: [PATCH] Test Tensor with non-default template arguments: float and std::complex. git-svn-id: https://svn.dealii.org/trunk@23990 0785d39b-7218-0410-832d-ea1e28bc413d --- tests/base/tensor_complex.cc | 116 ++++++++++++++++++++++++++ tests/base/tensor_complex/cmp/generic | 16 ++++ tests/base/tensor_float.cc | 108 ++++++++++++++++++++++++ tests/base/tensor_float/cmp/generic | 15 ++++ 4 files changed, 255 insertions(+) create mode 100644 tests/base/tensor_complex.cc create mode 100644 tests/base/tensor_complex/cmp/generic create mode 100644 tests/base/tensor_float.cc create mode 100644 tests/base/tensor_float/cmp/generic diff --git a/tests/base/tensor_complex.cc b/tests/base/tensor_complex.cc new file mode 100644 index 0000000000..7f60cb5b79 --- /dev/null +++ b/tests/base/tensor_complex.cc @@ -0,0 +1,116 @@ +//---------------------------- tensor_complex.cc --------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2011 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------- tensor_complex.cc --------------------------- + +// Same as tensor.cc, but uses tensors based on std::complex instead +// of double + +#include "../tests.h" +#include +#include +#include +#include +#include +#include + +int main () +{ + std::ofstream logfile("tensor_complex/output"); + deallog << std::setprecision(3); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + std::complex a[3][3] = {{{1,-1}, {2,0}, {3,0}}, + {{3,0}, {4,0}, {5,0}}, + {{6,0}, {7,0}, {8,3}}}; + std::complex b[3][3] = {{{24,-2}, {31,-2}, {37,6}}, + {{45,-3}, {57,0}, {69,15}}, + {{75,12}, {96,21}, {108,48}}}; + + const unsigned int dim=3; + Tensor<2,dim,std::complex > t(a); + Tensor<2,dim,std::complex > tt; + Tensor<2,dim,std::complex > result(b); + Assert (transpose(transpose(t)) == t, ExcInternalError()); + Assert (transpose(transpose(result)) == result, ExcInternalError()); + + Vector > unrolled(9); + + // cast result to double to profit from zero + // threshold and so on + t.unroll(unrolled); + deallog << "unrolled:"; + for (unsigned i=0;i<9;i++) + deallog << ' ' << unrolled(i); + deallog << std::endl; + + deallog << "t=" << std::endl; + for (unsigned int i=0; i > e1; + Tensor<1,3,std::complex > e2; + Tensor<1,3,std::complex > e3; + e1[0] = 1.; + e2[1] = 1.; + e3[2] = 1.; + Tensor<1,3,std::complex > result; + cross_product(result,e1,e2); + deallog << '\t' << result[0] + << '\t' << result[1] + << '\t' << result[2] << std::endl; + + cross_product(result,e2,e3); + deallog << '\t' << result[0] + << '\t' << result[1] << '\t' + << result[2] << std::endl; + + cross_product(result,e3,e1); + deallog << '\t' << result[0] + << '\t' << result[1] + << '\t' << result[2] << std::endl; + + deallog.pop(); + } + + if (tt==result) + { + deallog << "Result OK." << std::endl; + } + else + { + deallog << "Result WRONG!" << std::endl; + }; + + t = 0; + deallog << t << std::endl; +} diff --git a/tests/base/tensor_complex/cmp/generic b/tests/base/tensor_complex/cmp/generic new file mode 100644 index 0000000000..e7c8d24a43 --- /dev/null +++ b/tests/base/tensor_complex/cmp/generic @@ -0,0 +1,16 @@ + +DEAL::unrolled: (1.00,-1.00) (2.00,0.00) (3.00,0.00) (3.00,0.00) (4.00,0.00) (5.00,0.00) (6.00,0.00) (7.00,0.00) (8.00,3.00) +DEAL::t= +DEAL::(1.00,-1.00) (2.00,0.00) (3.00,0.00) +DEAL::(3.00,0.00) (4.00,0.00) (5.00,0.00) +DEAL::(6.00,0.00) (7.00,0.00) (8.00,3.00) +DEAL::norm(t)=14.9 +DEAL::tt= +DEAL::(24.0,-2.00) (31.0,-2.00) (37.0,6.00) +DEAL::(45.0,-3.00) (57.0,0.00) (69.0,15.0) +DEAL::(75.0,12.0) (96.0,21.0) (108.,48.0) +DEAL:Cross product:: (0.00,0.00) (0.00,0.00) (1.00,0.00) +DEAL:Cross product:: (1.00,0.00) (0.00,0.00) (0.00,0.00) +DEAL:Cross product:: (0.00,0.00) (1.00,0.00) (0.00,0.00) +DEAL::Result OK. +DEAL::(0.00,0.00) (0.00,0.00) (0.00,0.00) (0.00,0.00) (0.00,0.00) (0.00,0.00) (0.00,0.00) (0.00,0.00) (0.00,0.00) diff --git a/tests/base/tensor_float.cc b/tests/base/tensor_float.cc new file mode 100644 index 0000000000..2ba5e88b42 --- /dev/null +++ b/tests/base/tensor_float.cc @@ -0,0 +1,108 @@ +//---------------------------- tensor_float.cc --------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2011 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------- tensor_float.cc --------------------------- + +// Same as tensor.cc, but uses tensors based on floats instead of doubles + +#include "../tests.h" +#include +#include +#include +#include +#include + +int main () +{ + std::ofstream logfile("tensor_float/output"); + deallog << std::setprecision(3); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + float a[3][3] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}}; + float b[3][3] = {{25,31,37}, {45,57,69}, {75,96,117}}; + + const unsigned int dim=3; + Tensor<2,dim,float> t(a); + Tensor<2,dim,float> tt; + Tensor<2,dim,float> result(b); + Assert (transpose(transpose(t)) == t, ExcInternalError()); + Assert (transpose(transpose(result)) == result, ExcInternalError()); + + Vector unrolled(9); + + // cast result to double to profit from zero + // threshold and so on + t.unroll(unrolled); + deallog << "unrolled:"; + for (unsigned i=0;i<9;i++) + deallog << ' ' << static_cast(unrolled(i)); + deallog << std::endl; + + deallog << "t=" << std::endl; + for (unsigned int i=0; i(t[i][j]) << ' '; + deallog << std::endl; + }; + + deallog << "norm(t)=" << t.norm() << std::endl; + + contract (tt,t,t); + + deallog << "tt=" << std::endl; + for (unsigned int i=0; i(tt[i][j]) << ' '; + deallog << std::endl; + }; + + if (true) + { + deallog.push("Cross product"); + Tensor<1,3,float> e1; + Tensor<1,3,float> e2; + Tensor<1,3,float> e3; + e1[0] = 1.; + e2[1] = 1.; + e3[2] = 1.; + Tensor<1,3,float> result; + cross_product(result,e1,e2); + deallog << '\t' << static_cast(result[0]) + << '\t' << static_cast(result[1]) + << '\t' << static_cast(result[2]) << std::endl; + + cross_product(result,e2,e3); + deallog << '\t' << static_cast(result[0]) + << '\t' << static_cast(result[1]) << '\t' + << static_cast(result[2]) << std::endl; + + cross_product(result,e3,e1); + deallog << '\t' << static_cast(result[0]) + << '\t' << static_cast(result[1]) + << '\t' << static_cast(result[2]) << std::endl; + + deallog.pop(); + } + + if (tt==result) + { + deallog << "Result OK." << std::endl; + } + else + { + deallog << "Result WRONG!" << std::endl; + }; + +} diff --git a/tests/base/tensor_float/cmp/generic b/tests/base/tensor_float/cmp/generic new file mode 100644 index 0000000000..a0d31770f7 --- /dev/null +++ b/tests/base/tensor_float/cmp/generic @@ -0,0 +1,15 @@ + +DEAL::unrolled: 1.00 2.00 3.00 3.00 4.00 5.00 6.00 7.00 8.00 +DEAL::t= +DEAL::1.00 2.00 3.00 +DEAL::3.00 4.00 5.00 +DEAL::6.00 7.00 8.00 +DEAL::norm(t)=14.6 +DEAL::tt= +DEAL::25.0 31.0 37.0 +DEAL::45.0 57.0 69.0 +DEAL::75.0 96.0 117. +DEAL:Cross product:: 0 0 1.00 +DEAL:Cross product:: 1.00 0 0 +DEAL:Cross product:: 0 1.00 0 +DEAL::Result OK. -- 2.39.5