From 71b9af43bb1050750e392b116f45370ccdba667e Mon Sep 17 00:00:00 2001 From: David Wells Date: Sun, 29 May 2016 18:42:31 -0400 Subject: [PATCH] Add a test for symmetric tensor access. --- tests/base/symmetric_tensor_30.cc | 118 ++++++++++++++++++++++++++ tests/base/symmetric_tensor_30.output | 4 + 2 files changed, 122 insertions(+) create mode 100644 tests/base/symmetric_tensor_30.cc create mode 100644 tests/base/symmetric_tensor_30.output diff --git a/tests/base/symmetric_tensor_30.cc b/tests/base/symmetric_tensor_30.cc new file mode 100644 index 0000000000..61a6baf7c9 --- /dev/null +++ b/tests/base/symmetric_tensor_30.cc @@ -0,0 +1,118 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2016 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + +// Verify that SymmetricTensor::operator() and SymmetricTensor::operator[] do +// what we think they should do. + +#include "../tests.h" +#include +#include +#include + + +template +void check_2 () +{ + using namespace dealii; + Testing::rand(true, 42); + + SymmetricTensor<2, dim> change_with_brackets; + SymmetricTensor<2, dim> change_with_parentheses; + for (unsigned int k = 0; k < dim; ++k) + { + for (unsigned int l = 0; l < dim; ++l) + { + const double entry = double(Testing::rand()); + TableIndices<2> indices(k, l); + + // check assignment + change_with_brackets[indices] = entry; + change_with_parentheses(indices) = entry; + AssertThrow(change_with_brackets[k][l] == entry, + ExcMessage("Entries should match")); + AssertThrow(change_with_parentheses[k][l] == entry, + ExcMessage("Entries should match")); + + // and access + const double brackets_entry = change_with_brackets[indices]; + const double parentheses_entry = change_with_parentheses(indices); + AssertThrow(brackets_entry == entry, + ExcMessage("Entries should match")); + AssertThrow(parentheses_entry == entry, + ExcMessage("Entries should match")); + } + } +} + +template +void check_4 () +{ + using namespace dealii; + Testing::rand(true, 42); + + SymmetricTensor<4, dim> change_with_brackets; + SymmetricTensor<4, dim> change_with_parentheses; + for (unsigned int i = 0; i < dim; ++i) + { + for (unsigned int j = 0; j < dim; ++j) + { + for (unsigned int k = 0; k < dim; ++k) + { + for (unsigned int l = 0; l < dim; ++l) + { + const double entry = double(Testing::rand()); + TableIndices<4> indices(i, j, k, l); + + // check assignment + change_with_brackets[indices] = entry; + change_with_parentheses(indices) = entry; + AssertThrow(change_with_brackets[i][j][k][l] == entry, + ExcMessage("Entries should match")); + AssertThrow(change_with_parentheses[i][j][k][l] == entry, + ExcMessage("Entries should match")); + + // and access + const double brackets_entry = change_with_brackets[indices]; + const double parentheses_entry = change_with_parentheses(indices); + AssertThrow(brackets_entry == entry, + ExcMessage("Entries should match")); + AssertThrow(parentheses_entry == entry, + ExcMessage("Entries should match")); + } + } + } + } +} + + +int main () +{ + std::ofstream logfile("output"); + deallog << std::setprecision(3); + deallog.attach(logfile); + deallog.threshold_double(1.e-10); + + deallog << "check rank 2 tensors" << std::endl; + check_2<1>(); + check_2<2>(); + check_2<3>(); + + deallog << "check rank 4 tensors" << std::endl; + check_4<1>(); + check_4<2>(); + check_4<3>(); + + deallog << "OK" << std::endl; +} diff --git a/tests/base/symmetric_tensor_30.output b/tests/base/symmetric_tensor_30.output new file mode 100644 index 0000000000..8201db968c --- /dev/null +++ b/tests/base/symmetric_tensor_30.output @@ -0,0 +1,4 @@ + +DEAL::check rank 2 tensors +DEAL::check rank 4 tensors +DEAL::OK -- 2.39.5