From 78a72b52768710476d8073c555e2f653d6f2a074 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Wed, 7 Aug 2019 09:43:00 -0600 Subject: [PATCH] Address comments --- include/deal.II/base/symmetric_tensor.h | 3 + include/deal.II/base/tensor.h | 21 ++-- tests/tensors/constexpr_symmetric_tensor.cc | 41 +++----- tests/tensors/constexpr_tensor.cc | 108 ++++++++++---------- tests/tensors/constexpr_tensor.output | 18 ++++ 5 files changed, 104 insertions(+), 87 deletions(-) diff --git a/include/deal.II/base/symmetric_tensor.h b/include/deal.II/base/symmetric_tensor.h index a552189bb3..83901d0d48 100644 --- a/include/deal.II/base/symmetric_tensor.h +++ b/include/deal.II/base/symmetric_tensor.h @@ -596,6 +596,9 @@ public: * * If you want to use this function in a `constexpr` context, * use symmetrize() instead. + * + * Because we check for symmetry via a non-constexpr function call, you will + * have to use the symmetrize() function in constexpr contexts instead. */ template explicit SymmetricTensor(const Tensor<2, dim, OtherNumber> &t); diff --git a/include/deal.II/base/tensor.h b/include/deal.II/base/tensor.h index 8d96f7d4a2..49725bfcee 100644 --- a/include/deal.II/base/tensor.h +++ b/include/deal.II/base/tensor.h @@ -358,7 +358,7 @@ private: * Internal helper function for unroll. */ template - DEAL_II_CONSTEXPR void + void unroll_recursion(Vector &result, unsigned int & start_index) const; @@ -1006,7 +1006,7 @@ Tensor<0, dim, Number>::norm() const template -DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline +DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE typename Tensor<0, dim, Number>::real_type Tensor<0, dim, Number>::norm_square() const { @@ -1021,7 +1021,7 @@ DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline template template -inline DEAL_II_CONSTEXPR void +inline void Tensor<0, dim, Number>::unroll_recursion(Vector &result, unsigned int & index) const { @@ -1233,7 +1233,8 @@ template DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE Tensor & Tensor::operator=(const Tensor &t) { - // std::copy is constexpr only from C++20 on. + // The following loop could be written more concisely using std::copy, but + // that function is only constexpr from C++20 on. for (unsigned int i = 0; i < dim; ++i) values[i] = t.values[i]; return *this; @@ -1431,7 +1432,7 @@ namespace internal inline unsigned int mod<0>(const unsigned int x) { - // Assert(false, ExcInternalError()); + Assert(false, ExcInternalError()); return x; } @@ -1563,7 +1564,7 @@ operator<<(std::ostream &out, const Tensor<0, dim, Number> &p) * @relatesalso Tensor<0,dim,Number> */ template -constexpr DEAL_II_CUDA_HOST_DEV DEAL_II_ALWAYS_INLINE +DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV DEAL_II_ALWAYS_INLINE typename ProductType::type operator*(const Other &object, const Tensor<0, dim, Number> &t) { @@ -1583,7 +1584,7 @@ constexpr DEAL_II_CUDA_HOST_DEV DEAL_II_ALWAYS_INLINE * @relatesalso Tensor<0,dim,Number> */ template -constexpr DEAL_II_CUDA_HOST_DEV DEAL_II_ALWAYS_INLINE +DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV DEAL_II_ALWAYS_INLINE typename ProductType::type operator*(const Tensor<0, dim, Number> &t, const Other &object) { @@ -1707,12 +1708,12 @@ DEAL_II_CONSTEXPR DEAL_II_CUDA_HOST_DEV inline DEAL_II_ALWAYS_INLINE * @relatesalso Tensor */ template -DEAL_II_CUDA_HOST_DEV constexpr DEAL_II_ALWAYS_INLINE - Tensor::type, OtherNumber>::type> - operator*(const Number &factor, const Tensor &t) + operator*(const Number &factor, const Tensor &t) { // simply forward to the operator above return t * factor; diff --git a/tests/tensors/constexpr_symmetric_tensor.cc b/tests/tensors/constexpr_symmetric_tensor.cc index bf4ef812a8..c700deaf06 100644 --- a/tests/tensors/constexpr_symmetric_tensor.cc +++ b/tests/tensors/constexpr_symmetric_tensor.cc @@ -112,34 +112,27 @@ main() deallog << "SymmetricTensor<4,2> = " << B << std::endl; { - DEAL_II_CONSTEXPR double a_init[3][3] = {{1., 0., 0.}, - {2., 1., 0.}, - {3., 2., 1.}}; - DEAL_II_CONSTEXPR Tensor<2, 3> dummy_a{a_init}; - DEAL_II_CONSTEXPR SymmetricTensor<2, 3> a = symmetrize(dummy_a); - std::cout << a << std::endl; - DEAL_II_CONSTEXPR auto inverted = invert(a); - std::cout << inverted << std::endl; - DEAL_II_CONSTEXPR double ref_init[3][3] = {{0., -2., 2.}, - {-2., 5., -2.}, - {2., -2., 0.}}; - DEAL_II_CONSTEXPR Tensor<2, 3> dummy_ref{ref_init}; - DEAL_II_CONSTEXPR SymmetricTensor<2, 3> ref = symmetrize(dummy_ref); - std::cout << ref << std::endl; + constexpr double a_init[3][3] = {{1., 0., 0.}, {2., 1., 0.}, {3., 2., 1.}}; + constexpr Tensor<2, 3> dummy_a{a_init}; + DEAL_II_CONSTEXPR const SymmetricTensor<2, 3> a = symmetrize(dummy_a); + DEAL_II_CONSTEXPR const auto inverted = invert(a); + constexpr double ref_init[3][3] = {{0., -2., 2.}, + {-2., 5., -2.}, + {2., -2., 0.}}; + constexpr Tensor<2, 3> dummy_ref{ref_init}; + DEAL_II_CONSTEXPR const SymmetricTensor<2, 3> ref = symmetrize(dummy_ref); Assert(inverted == ref, ExcInternalError()); } { - DEAL_II_CONSTEXPR double a_init[3][3] = {{1., 2., 3.}, - {2., 1., 2.}, - {3., 2., 1.}}; - DEAL_II_CONSTEXPR Tensor<2, 3> dummy_a{a_init}; - DEAL_II_CONSTEXPR SymmetricTensor<2, 3> a = symmetrize(dummy_a); - DEAL_II_CONSTEXPR auto transposed = transpose(a); + constexpr double a_init[3][3] = {{1., 2., 3.}, {2., 1., 2.}, {3., 2., 1.}}; + constexpr Tensor<2, 3> dummy_a{a_init}; + DEAL_II_CONSTEXPR const SymmetricTensor<2, 3> a = symmetrize(dummy_a); + DEAL_II_CONSTEXPR const auto transposed = transpose(a); Assert(transposed == a, ExcInternalError()); - constexpr auto dummy = scalar_product(a, a); - constexpr auto dummy_5 = a * a; - constexpr auto middle = outer_product(a, a); - constexpr auto dummy_6 = contract3(a, middle, a); + DEAL_II_CONSTEXPR const auto dummy = scalar_product(a, a); + DEAL_II_CONSTEXPR const auto dummy_5 = a * a; + DEAL_II_CONSTEXPR const auto middle = outer_product(a, a); + DEAL_II_CONSTEXPR const auto dummy_6 = contract3(a, middle, a); } deallog << "OK" << std::endl; diff --git a/tests/tensors/constexpr_tensor.cc b/tests/tensors/constexpr_tensor.cc index a5cc8696f6..35c96f433c 100644 --- a/tests/tensors/constexpr_tensor.cc +++ b/tests/tensors/constexpr_tensor.cc @@ -31,14 +31,16 @@ test_constexpr_tensor_constructors() deallog << b << std::endl; deallog << c << std::endl; - constexpr auto d = a * 2.; - constexpr auto e = a / 2.; - constexpr auto f = d + e; - constexpr auto g = d - e; + DEAL_II_CONSTEXPR const auto d = a * 2.; + DEAL_II_CONSTEXPR const auto e = 2. * a; + DEAL_II_CONSTEXPR const auto f = a / 2.; + DEAL_II_CONSTEXPR const auto g = d + e; + DEAL_II_CONSTEXPR const auto h = d - e; deallog << d << std::endl; deallog << e << std::endl; deallog << f << std::endl; deallog << g << std::endl; + deallog << h << std::endl; } DEAL_II_CONSTEXPR double @@ -117,72 +119,72 @@ main() deallog << tensor_rank2_result << std::endl; { - DEAL_II_CONSTEXPR double initializer[2] = {1., -1.}; - DEAL_II_CONSTEXPR Tensor<1, 2> c{initializer}; - DEAL_II_CONSTEXPR Tensor<1, 2> c_cross = cross_product_2d(c); - DEAL_II_CONSTEXPR double initializer_ref[2] = {-1., -1.}; - DEAL_II_CONSTEXPR Tensor<1, 2> ref{initializer_ref}; - DEAL_II_CONSTEXPR bool is_same = (c_cross == ref); - DEAL_II_CONSTEXPR bool is_not_same = (c_cross != ref); + constexpr double initializer[2] = {1., -1.}; + constexpr Tensor<1, 2> c{initializer}; + DEAL_II_CONSTEXPR const Tensor<1, 2> c_cross = cross_product_2d(c); + constexpr double initializer_ref[2] = {-1., -1.}; + constexpr Tensor<1, 2> ref{initializer_ref}; + DEAL_II_CONSTEXPR const bool is_same = (c_cross == ref); + DEAL_II_CONSTEXPR const bool is_not_same = (c_cross != ref); Assert(is_same && !is_not_same, ExcInternalError()); } { - DEAL_II_CONSTEXPR double initializer_1[3] = {1., 0., 0.}; - DEAL_II_CONSTEXPR Tensor<1, 3> c_1{initializer_1}; - DEAL_II_CONSTEXPR double initializer_2[3] = {0., 1., 0.}; - DEAL_II_CONSTEXPR Tensor<1, 3> c_2{initializer_2}; - - DEAL_II_CONSTEXPR auto c_cross = cross_product_3d(c_1, c_2); - DEAL_II_CONSTEXPR double initializer_ref[3] = {0., 0., 1.}; - DEAL_II_CONSTEXPR Tensor<1, 3> ref{initializer_ref}; - DEAL_II_CONSTEXPR bool is_same = (c_cross == ref); - DEAL_II_CONSTEXPR bool is_not_same = (c_cross != ref); + DEAL_II_CONSTEXPR const double initializer_1[3] = {1., 0., 0.}; + DEAL_II_CONSTEXPR const Tensor<1, 3> c_1{initializer_1}; + DEAL_II_CONSTEXPR const double initializer_2[3] = {0., 1., 0.}; + DEAL_II_CONSTEXPR const Tensor<1, 3> c_2{initializer_2}; + + DEAL_II_CONSTEXPR const auto c_cross = cross_product_3d(c_1, c_2); + DEAL_II_CONSTEXPR const double initializer_ref[3] = {0., 0., 1.}; + DEAL_II_CONSTEXPR const Tensor<1, 3> ref{initializer_ref}; + DEAL_II_CONSTEXPR const bool is_same = (c_cross == ref); + DEAL_II_CONSTEXPR const bool is_not_same = (c_cross != ref); Assert(is_same && !is_not_same, ExcInternalError()); } - DEAL_II_CONSTEXPR auto table_indices = + DEAL_II_CONSTEXPR const auto table_indices = Tensor<2, 3>::unrolled_to_component_indices(0); - DEAL_II_CONSTEXPR auto index = + DEAL_II_CONSTEXPR const auto index = Tensor<2, 3>::component_to_unrolled_index(TableIndices<2>{}); Assert(index == 0, ExcInternalError()); - DEAL_II_CONSTEXPR auto used_memory = Tensor<2, 3>::memory_consumption(); + DEAL_II_CONSTEXPR const auto used_memory = Tensor<2, 3>::memory_consumption(); deallog << "Used memory: " << used_memory << std::endl; { - DEAL_II_CONSTEXPR double a_init[3][3] = {{1., 0., 0.}, - {2., 1., 0.}, - {3., 2., 1.}}; - DEAL_II_CONSTEXPR Tensor<2, 3> a{a_init}; - DEAL_II_CONSTEXPR auto inverted = invert(a); - DEAL_II_CONSTEXPR double ref_init[3][3] = {{1., 0., 0.}, - {-2., 1., 0.}, - {1., -2., 1}}; - DEAL_II_CONSTEXPR Tensor<2, 3> ref{ref_init}; + DEAL_II_CONSTEXPR const double a_init[3][3] = {{1., 0., 0.}, + {2., 1., 0.}, + {3., 2., 1.}}; + DEAL_II_CONSTEXPR const Tensor<2, 3> a{a_init}; + DEAL_II_CONSTEXPR const auto inverted = invert(a); + DEAL_II_CONSTEXPR const double ref_init[3][3] = {{1., 0., 0.}, + {-2., 1., 0.}, + {1., -2., 1}}; + DEAL_II_CONSTEXPR const Tensor<2, 3> ref{ref_init}; Assert(inverted == ref, ExcInternalError()); } { - DEAL_II_CONSTEXPR double a_init[3][3] = {{1., 0., 0.}, - {2., 1., 0.}, - {3., 2., 1.}}; - DEAL_II_CONSTEXPR Tensor<2, 3> a{a_init}; - DEAL_II_CONSTEXPR auto transposed = transpose(a); - DEAL_II_CONSTEXPR double ref_init[3][3] = {{1., 2., 3.}, - {0., 1., 2.}, - {0., 0., 1}}; - DEAL_II_CONSTEXPR Tensor<2, 3> ref{ref_init}; + DEAL_II_CONSTEXPR const double a_init[3][3] = {{1., 0., 0.}, + {2., 1., 0.}, + {3., 2., 1.}}; + DEAL_II_CONSTEXPR const Tensor<2, 3> a{a_init}; + DEAL_II_CONSTEXPR const auto transposed = transpose(a); + DEAL_II_CONSTEXPR const double ref_init[3][3] = {{1., 2., 3.}, + {0., 1., 2.}, + {0., 0., 1}}; + DEAL_II_CONSTEXPR const Tensor<2, 3> ref{ref_init}; Assert(transposed == ref, ExcInternalError()); - constexpr auto dummy = scalar_product(a, ref); - constexpr auto dummy_2 = contract<0, 0>(a, ref); - constexpr auto dummy_3 = double_contract<0, 0, 1, 1>(a, ref); - constexpr auto dummy_4 = schur_product(a, ref); - constexpr auto dummy_5 = a * ref; - constexpr auto middle = outer_product(a, a); - constexpr auto dummy_6 = contract3(a, middle, a); - constexpr auto dummy_7 = adjugate(a); - constexpr auto dummy_8 = cofactor(a); - constexpr auto dummy_9 = l1_norm(a); - constexpr auto dummy_10 = linfty_norm(a); + DEAL_II_CONSTEXPR const auto dummy = scalar_product(a, ref); + DEAL_II_CONSTEXPR const auto dummy_2 = contract<0, 0>(a, ref); + DEAL_II_CONSTEXPR const auto dummy_3 = double_contract<0, 0, 1, 1>(a, ref); + DEAL_II_CONSTEXPR const auto dummy_4 = schur_product(a, ref); + DEAL_II_CONSTEXPR const auto dummy_5 = a * ref; + DEAL_II_CONSTEXPR const auto middle = outer_product(a, a); + DEAL_II_CONSTEXPR const auto dummy_6 = contract3(a, middle, a); + DEAL_II_CONSTEXPR const auto dummy_7 = adjugate(a); + DEAL_II_CONSTEXPR const auto dummy_8 = cofactor(a); + DEAL_II_CONSTEXPR const auto dummy_9 = l1_norm(a); + DEAL_II_CONSTEXPR const auto dummy_10 = linfty_norm(a); } deallog << "OK" << std::endl; diff --git a/tests/tensors/constexpr_tensor.output b/tests/tensors/constexpr_tensor.output index 218e060df4..5b07862e37 100644 --- a/tests/tensors/constexpr_tensor.output +++ b/tests/tensors/constexpr_tensor.output @@ -8,6 +8,7 @@ DEAL:float::0.00000 DEAL:float::0.00000 DEAL:float::0.00000 DEAL:float::0.00000 +DEAL:float::0.00000 DEAL:float:: Tensor<0,2> DEAL:float::0.00000 DEAL:float::0.00000 @@ -16,6 +17,7 @@ DEAL:float::0.00000 DEAL:float::0.00000 DEAL:float::0.00000 DEAL:float::0.00000 +DEAL:float::0.00000 DEAL:float:: Tensor<0,3> DEAL:float::0.00000 DEAL:float::0.00000 @@ -24,6 +26,7 @@ DEAL:float::0.00000 DEAL:float::0.00000 DEAL:float::0.00000 DEAL:float::0.00000 +DEAL:float::0.00000 DEAL:float:: Tensor<1,1> DEAL:float::0.00000 DEAL:float::0.00000 @@ -32,6 +35,7 @@ DEAL:float::0.00000 DEAL:float::0.00000 DEAL:float::0.00000 DEAL:float::0.00000 +DEAL:float::0.00000 DEAL:float:: Tensor<1,2> DEAL:float::0.00000 0.00000 DEAL:float::0.00000 0.00000 @@ -40,6 +44,7 @@ DEAL:float::0.00000 0.00000 DEAL:float::0.00000 0.00000 DEAL:float::0.00000 0.00000 DEAL:float::0.00000 0.00000 +DEAL:float::0.00000 0.00000 DEAL:float:: Tensor<1,3> DEAL:float::0.00000 0.00000 0.00000 DEAL:float::0.00000 0.00000 0.00000 @@ -48,6 +53,7 @@ DEAL:float::0.00000 0.00000 0.00000 DEAL:float::0.00000 0.00000 0.00000 DEAL:float::0.00000 0.00000 0.00000 DEAL:float::0.00000 0.00000 0.00000 +DEAL:float::0.00000 0.00000 0.00000 DEAL:float:: Tensor<2,1> DEAL:float::0.00000 DEAL:float::0.00000 @@ -56,6 +62,7 @@ DEAL:float::0.00000 DEAL:float::0.00000 DEAL:float::0.00000 DEAL:float::0.00000 +DEAL:float::0.00000 DEAL:float:: Tensor<2,2> DEAL:float::0.00000 0.00000 0.00000 0.00000 DEAL:float::0.00000 0.00000 0.00000 0.00000 @@ -64,6 +71,7 @@ DEAL:float::0.00000 0.00000 0.00000 0.00000 DEAL:float::0.00000 0.00000 0.00000 0.00000 DEAL:float::0.00000 0.00000 0.00000 0.00000 DEAL:float::0.00000 0.00000 0.00000 0.00000 +DEAL:float::0.00000 0.00000 0.00000 0.00000 DEAL:float:: Tensor<2,3> DEAL:float::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 DEAL:float::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 @@ -72,6 +80,7 @@ DEAL:float::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00 DEAL:float::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 DEAL:float::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 DEAL:float::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 +DEAL:float::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 DEAL:double:: Tensor<0,1> DEAL:double::0.00000 DEAL:double::0.00000 @@ -80,6 +89,7 @@ DEAL:double::0.00000 DEAL:double::0.00000 DEAL:double::0.00000 DEAL:double::0.00000 +DEAL:double::0.00000 DEAL:double:: Tensor<0,2> DEAL:double::0.00000 DEAL:double::0.00000 @@ -88,6 +98,7 @@ DEAL:double::0.00000 DEAL:double::0.00000 DEAL:double::0.00000 DEAL:double::0.00000 +DEAL:double::0.00000 DEAL:double:: Tensor<0,3> DEAL:double::0.00000 DEAL:double::0.00000 @@ -96,6 +107,7 @@ DEAL:double::0.00000 DEAL:double::0.00000 DEAL:double::0.00000 DEAL:double::0.00000 +DEAL:double::0.00000 DEAL:double:: Tensor<1,1> DEAL:double::0.00000 DEAL:double::0.00000 @@ -104,6 +116,7 @@ DEAL:double::0.00000 DEAL:double::0.00000 DEAL:double::0.00000 DEAL:double::0.00000 +DEAL:double::0.00000 DEAL:double:: Tensor<1,2> DEAL:double::0.00000 0.00000 DEAL:double::0.00000 0.00000 @@ -112,6 +125,7 @@ DEAL:double::0.00000 0.00000 DEAL:double::0.00000 0.00000 DEAL:double::0.00000 0.00000 DEAL:double::0.00000 0.00000 +DEAL:double::0.00000 0.00000 DEAL:double:: Tensor<1,3> DEAL:double::0.00000 0.00000 0.00000 DEAL:double::0.00000 0.00000 0.00000 @@ -120,6 +134,7 @@ DEAL:double::0.00000 0.00000 0.00000 DEAL:double::0.00000 0.00000 0.00000 DEAL:double::0.00000 0.00000 0.00000 DEAL:double::0.00000 0.00000 0.00000 +DEAL:double::0.00000 0.00000 0.00000 DEAL:double:: Tensor<2,1> DEAL:double::0.00000 DEAL:double::0.00000 @@ -128,6 +143,7 @@ DEAL:double::0.00000 DEAL:double::0.00000 DEAL:double::0.00000 DEAL:double::0.00000 +DEAL:double::0.00000 DEAL:double:: Tensor<2,2> DEAL:double::0.00000 0.00000 0.00000 0.00000 DEAL:double::0.00000 0.00000 0.00000 0.00000 @@ -136,6 +152,7 @@ DEAL:double::0.00000 0.00000 0.00000 0.00000 DEAL:double::0.00000 0.00000 0.00000 0.00000 DEAL:double::0.00000 0.00000 0.00000 0.00000 DEAL:double::0.00000 0.00000 0.00000 0.00000 +DEAL:double::0.00000 0.00000 0.00000 0.00000 DEAL:double:: Tensor<2,3> DEAL:double::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 DEAL:double::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 @@ -144,6 +161,7 @@ DEAL:double::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.0 DEAL:double::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 DEAL:double::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 DEAL:double::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 +DEAL:double::0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 DEAL::Using Tensor within constexpr functions DEAL::15.2000 DEAL::116.000 -- 2.39.5