// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 2017 - 2024 by the deal.II authors // // This file is part of the deal.II library. // // Part of the source code is dual licensed under Apache-2.0 WITH // LLVM-exception OR LGPL-2.1-or-later. Detailed license information // governing the source code and code contributions can be found in // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. // // ------------------------------------------------------------------------ #ifndef dealii_physics_notation_h #define dealii_physics_notation_h #include #include #include #include #include #include #include #include DEAL_II_NAMESPACE_OPEN namespace Physics { namespace Notation { /** * @brief A namespace with functions that assist in the conversion of * vectors and tensors to and from a compressed format using Kelvin notation * and weighting. * * Both Kelvin and Voigt notation adopt the same indexing convention. * With specific reference to the spatial dimension 3 case, for * a rank-2 symmetric tensor $\mathbf{S}$ we enumerate its tensor * components * @f[ * \mathbf{S} \dealcoloneq \left[ \begin{array}{ccc} * S_{00} & S_{01} & S_{02} \\ * S_{10} = S_{01} & S_{11} & S_{12} \\ * S_{20} = S_{02} & S_{21} = S_{12} & S_{22} * \end{array} \right] * \quad \Rightarrow \quad * \left[ \begin{array}{ccc} * n = 0 & n = 5 & n = 4 \\ * sym & n = 1 & n = 3 \\ * sym & sym & n = 2 * \end{array} \right] , * @f] * where $n$ denotes the Kelvin index for the tensor component, * while for a general rank-2 tensor $\mathbf{T}$ * @f[ * \mathbf{T} \dealcoloneq \left[ \begin{array}{ccc} * T_{00} & T_{01} & T_{02} \\ * T_{10} & T_{11} & T_{12} \\ * T_{20} & T_{21} & T_{22} * \end{array}\right] * \quad \Rightarrow \quad * \left[ \begin{array}{ccc} * n = 0 & n = 5 & n = 4 \\ * n = 6 & n = 1 & n = 3 \\ * n = 7 & n = 8 & n = 2 * \end{array}\right] , * @f] * and for a rank-1 tensor $\mathbf{v}$ * @f[ * \mathbf{v} \dealcoloneq \left[ \begin{array}{c} * v_{0} \\ v_{1} \\ v_{2} * \end{array}\right] * \quad \Rightarrow \quad * \left[ \begin{array}{c} * n = 0 \\ n = 1 \\ n = 2 * \end{array}\right] . * @f] * To summarize, the relationship between tensor and Kelvin indices for both * the three-dimensional case and the analogously discerned two-dimensional * case outlined in the following table: * * * * * * * * * *
Dimension 2 Dimension 3
* * * * * * * * * * * * * * * * * * * * * *
Tensor index pairsKelvin index
000
111
122
213
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Tensor index pairsKelvin index
000
111
222
123
024
015
106
207
218
*
* * To illustrate the purpose of this notation, consider the rank-2 symmetric * tensors $\mathbf{S}$ and $\mathbf{E}$ that are related to one another by * $\mathbf{S} = \cal{C} : \mathbf{E}$, where the operator $\cal{C}$ is a * fourth-order symmetric tensor. As opposed to the commonly used Voigt * notation, Kelvin (or Mandel) notation keeps the same definition of the * inner product $\mathbf{S} : \mathbf{E}$ when both $\mathbf{S}$ and * $\mathbf{E}$ are symmetric. In general, the inner product of all * symmetric and general tensors remain the same regardless of the notation * with which it is represented. * * To achieve these two properties, namely that * @f[ * \mathbf{S} = \cal{C} : \mathbf{E} * \quad \Rightarrow \quad * \tilde{\mathbf{S}} = \tilde{\cal{C}} \; \tilde{\mathbf{E}} * @f] * and * @f[ * \mathbf{S} : \mathbf{E} * \, \equiv \, * \tilde{\mathbf{S}} \cdot \tilde{\mathbf{E}} , * @f] * it holds that the Kelvin-condensed equivalents of the previously defined * symmetric tensors, indicated by the $\tilde{\left(\bullet\right)}$, must * be defined as * @f[ * \tilde{\mathbf{S}} * = \left[ \begin{array}{c} * S_{00} \\ S_{11} \\ S_{22} \\ \sqrt{2} S_{12} \\ \sqrt{2} S_{02} \\ * \sqrt{2} S_{01} \end{array}\right] \quad \text{and} \quad * \tilde{\mathbf{E}} * = \left[ \begin{array}{c} * E_{00} \\ E_{11} \\ E_{22} \\ \sqrt{2} E_{12} \\ \sqrt{2} E_{02} \\ * \sqrt{2} E_{01} \end{array}\right] . * @f] * The corresponding and consistent condensed fourth-order symmetric tensor * is * @f[ * \tilde{\cal{C}} * = \left[ \begin{array}{cccccc} * \tilde{\cal{C}}_{00} & \tilde{\cal{C}}_{01} & \tilde{\cal{C}}_{02} & * \tilde{\cal{C}}_{03} & \tilde{\cal{C}}_{04} & \tilde{\cal{C}}_{05} \\ * \tilde{\cal{C}}_{10} & \tilde{\cal{C}}_{11} & \tilde{\cal{C}}_{12} & * \tilde{\cal{C}}_{13} & \tilde{\cal{C}}_{14} & \tilde{\cal{C}}_{15} \\ * \tilde{\cal{C}}_{20} & \tilde{\cal{C}}_{21} & \tilde{\cal{C}}_{22} & * \tilde{\cal{C}}_{23} & \tilde{\cal{C}}_{24} & \tilde{\cal{C}}_{25} \\ * \tilde{\cal{C}}_{30} & \tilde{\cal{C}}_{31} & \tilde{\cal{C}}_{32} & * \tilde{\cal{C}}_{33} & \tilde{\cal{C}}_{34} & \tilde{\cal{C}}_{35} \\ * \tilde{\cal{C}}_{40} & \tilde{\cal{C}}_{41} & \tilde{\cal{C}}_{42} & * \tilde{\cal{C}}_{43} & \tilde{\cal{C}}_{44} & \tilde{\cal{C}}_{45} \\ * \tilde{\cal{C}}_{50} & \tilde{\cal{C}}_{51} & \tilde{\cal{C}}_{52} & * \tilde{\cal{C}}_{53} & \tilde{\cal{C}}_{54} & \tilde{\cal{C}}_{55} * \end{array}\right] * \equiv * \left[ \begin{array}{cccccc} * {\cal{C}}_{0000} & {\cal{C}}_{0011} & * {\cal{C}}_{0022} & \sqrt{2} {\cal{C}}_{0012} & \sqrt{2} * {\cal{C}}_{0002} & \sqrt{2} {\cal{C}}_{0001} \\ * {\cal{C}}_{1100} & {\cal{C}}_{1111} & * {\cal{C}}_{1122} & \sqrt{2} {\cal{C}}_{1112} & \sqrt{2} * {\cal{C}}_{1102} & \sqrt{2} {\cal{C}}_{1101} \\ * {\cal{C}}_{2200} & {\cal{C}}_{2211} & * {\cal{C}}_{2222} & \sqrt{2} {\cal{C}}_{2212} & \sqrt{2} * {\cal{C}}_{2202} & \sqrt{2} {\cal{C}}_{2201} \\ * \sqrt{2} {\cal{C}}_{1200} & \sqrt{2} {\cal{C}}_{1211} & \sqrt{2} * {\cal{C}}_{1222} & 2 {\cal{C}}_{1212} & 2 {\cal{C}}_{1202} & 2 * {\cal{C}}_{1201} \\ * \sqrt{2} {\cal{C}}_{0200} & \sqrt{2} {\cal{C}}_{0211} & \sqrt{2} * {\cal{C}}_{0222} & 2 {\cal{C}}_{0212} & 2 {\cal{C}}_{0202} & 2 * {\cal{C}}_{0201} \\ \sqrt{2} {\cal{C}}_{0100} & \sqrt{2} * {\cal{C}}_{0111} & \sqrt{2} {\cal{C}}_{0122} & 2 {\cal{C}}_{0112} & 2 * {\cal{C}}_{0102} & 2 {\cal{C}}_{0101} \end{array}\right] . * @f] * The mapping from the two Kelvin indices of the FullMatrix * $\tilde{\cal{C}}$ to the rank-4 SymmetricTensor $\cal{C}$ can be inferred * using the table shown above. * * An important observation is that both the left-hand side tensor * $\tilde{\mathbf{S}}$ and right-hand side tensor $\tilde{\mathbf{E}}$ have * the same form; this is a property that is not present in Voigt notation. * The various factors introduced into $\tilde{\mathbf{S}}$, * $\tilde{\mathbf{E}}$ and $\tilde{\cal{C}}$ account for the symmetry of * the tensors. The Kelvin description of their non-symmetric counterparts * include no such factors. * * Some useful references that show how this notation works include, amongst * others, @cite Nagel2016 and @cite Dellinger1998 * as well as the online reference found on * this * wikipedia page and the * unit tests. */ namespace Kelvin { /** * Input matrix has incorrect number of rows. */ DeclException3(ExcNotationExcFullMatrixToTensorRowSize2, int, int, int, << "The number of rows in the input matrix is " << arg1 << ", but needs to be either " << arg2 << " or " << arg3 << '.'); /** * Input matrix has incorrect number of rows. */ DeclException4(ExcNotationExcFullMatrixToTensorRowSize3, int, int, int, int, << "The number of rows in the input matrix is " << arg1 << ", but needs to be either " << arg2 << ',' << arg3 << ", or " << arg4 << '.'); /** * Input matrix has incorrect number of columns. */ DeclException3(ExcNotationExcFullMatrixToTensorColSize2, int, int, int, << "The number of columns in the input matrix is " << arg1 << ", but needs to be either " << arg2 << " or " << arg3 << '.'); /** * Input matrix has incorrect number of columns. */ DeclException4(ExcNotationExcFullMatrixToTensorColSize3, int, int, int, int, << "The number of columns in the input matrix is " << arg1 << ", but needs to be either " << arg2 << ',' << arg3 << ", or " << arg4 << '.'); /** * @name Forward operation: Tensor notation to Kelvin notation */ /** @{ */ /** * Convert a scalar value to its compressed vector equivalent. * * The output vector has one entry. */ template Vector to_vector(const Number &s); /** * Convert a rank-0 tensor to its compressed vector equivalent. * * The output vector has one entry. */ template Vector to_vector(const Tensor<0, dim, Number> &s); /** * Convert a rank-1 tensor to its compressed vector equivalent. * * The output vector has $dim$ entries. */ template Vector to_vector(const Tensor<1, dim, Number> &v); /** * Convert a rank-2 tensor to its compressed vector equivalent. * * The output vector has Tensor<2,dim>::n_independent_components entries. */ template Vector to_vector(const Tensor<2, dim, Number> &t); /** * Convert a rank-2 symmetric tensor to its compressed vector equivalent. * * The output vector has SymmetricTensor<2,dim>::n_independent_components * entries. */ template Vector to_vector(const SymmetricTensor<2, dim, Number> &st); /** * Convert a scalar value to its compressed matrix equivalent. * * The output matrix will have one row and one column. */ template FullMatrix to_matrix(const Number &s); /** * Convert a rank-0 tensor to its compressed matrix equivalent. * * The output matrix will have one row and one column. */ template FullMatrix to_matrix(const Tensor<0, dim, Number> &s); /** * Convert a rank-1 tensor to its compressed matrix equivalent. * * The output matrix will have $dim$ rows and one column. */ template FullMatrix to_matrix(const Tensor<1, dim, Number> &v); /** * Convert a rank-2 tensor to its compressed matrix equivalent. * * The output matrix will have $dim$ rows and $dim$ columns. */ template FullMatrix to_matrix(const Tensor<2, dim, Number> &t); /** * Convert a rank-2 symmetric tensor to its compressed matrix equivalent. * * The output matrix will have $dim$ rows and $dim$ columns, with the same * format as the equivalent function for non-symmetric tensors. This is * because it is not possible to compress the * SymmetricTensor<2,dim>::n_independent_components unique entries into a * square matrix. */ template FullMatrix to_matrix(const SymmetricTensor<2, dim, Number> &st); /** * Convert a rank-3 tensor to its compressed matrix equivalent. * * The template arguments @p SubTensor1 and @p SubTensor2 determine how * the unrolling occurs, in particular how the elements of the rank-3 * tensor are to be interpreted. * * So, for example, with the following two conversions * @code * Tensor<3,dim> r3_tnsr; // All elements filled differently * Tensor<3,dim> r3_symm_tnsr; // Some elements filled symmetrically * * const FullMatrix mtrx_1 = * Physics::Notation::to_matrix, * Tensor<1,dim>*>(r3_tnsr); * const FullMatrix mtrx_2 = * Physics::Notation::to_matrix, * SymmetricTensor<2,dim>*>(r3_symm_tnsr); * @endcode * the matrix @p mtrx_1 will have $dim \times dim$ rows and $dim$ columns * (i.e. size Tensor<2,dim>::n_independent_components $\times$ * Tensor<1,dim>::n_independent_components), * while those of the matrix @p mtrx_2 will have $dim$ rows and * $(dim \times dim + dim)/2$ columns * (i.e. size Tensor<1,dim>::n_independent_components $\times$ * SymmetricTensor<2,dim>::n_independent_components), as it is assumed * that the entries corresponding to the alternation of the second and * third indices are equal. That is to say that * r3_symm_tnsr[i][j][k] == r3_symm_tnsr[i][k][j]. */ template , typename SubTensor2 = Tensor<1, dim>, typename Number> FullMatrix to_matrix(const Tensor<3, dim, Number> &t); /** * Convert a rank-4 tensor to its compressed matrix equivalent. * * The output matrix will have Tensor<2,dim>::n_independent_components * rows and Tensor<2,dim>::n_independent_components columns. */ template FullMatrix to_matrix(const Tensor<4, dim, Number> &t); /** * Convert a rank-4 symmetric tensor to its compressed matrix equivalent. * * The output matrix will have * SymmetricTensor<2,dim>::n_independent_components rows and * SymmetricTensor<2,dim>::n_independent_components columns. */ template FullMatrix to_matrix(const SymmetricTensor<4, dim, Number> &st); /** @} */ /** * @name Reverse operation: Kelvin notation to tensor notation */ /** @{ */ /** * Convert a compressed vector to its equivalent scalar value. */ template void to_tensor(const Vector &vec, Number &s); /** * Convert a compressed vector to its equivalent rank-0 tensor. */ template void to_tensor(const Vector &vec, Tensor<0, dim, Number> &s); /** * Convert a compressed vector to its equivalent rank-1 tensor. */ template void to_tensor(const Vector &vec, Tensor<1, dim, Number> &v); /** * Convert a compressed vector to its equivalent rank-2 tensor. */ template void to_tensor(const Vector &vec, Tensor<2, dim, Number> &t); /** * Convert a compressed vector to its equivalent rank-2 symmetric tensor. */ template void to_tensor(const Vector &vec, SymmetricTensor<2, dim, Number> &st); /** * Convert a compressed matrix to its equivalent scalar value. */ template void to_tensor(const FullMatrix &mtrx, Number &s); /** * Convert a compressed matrix to its equivalent rank-0 tensor. */ template void to_tensor(const FullMatrix &mtrx, Tensor<0, dim, Number> &s); /** * Convert a compressed matrix to its equivalent rank-1 tensor. */ template void to_tensor(const FullMatrix &mtrx, Tensor<1, dim, Number> &v); /** * Convert a compressed matrix to its equivalent rank-2 tensor. */ template void to_tensor(const FullMatrix &mtrx, Tensor<2, dim, Number> &t); /** * Convert a compressed matrix to its equivalent rank-2 symmetric tensor. */ template void to_tensor(const FullMatrix &mtrx, SymmetricTensor<2, dim, Number> &st); /** * Convert a compressed matrix to its equivalent rank-3 tensor. * * @note Based on the size of the matrix @p mtrx, some of the * components of @p t may be interpreted as having symmetric * counterparts. This is the reverse of the operation explained * in the documentation of the counterpart to_matrix() * function. */ template void to_tensor(const FullMatrix &mtrx, Tensor<3, dim, Number> &t); /** * Convert a compressed matrix to its equivalent rank-4 tensor. */ template void to_tensor(const FullMatrix &mtrx, Tensor<4, dim, Number> &t); /** * Convert a compressed matrix to its equivalent rank-4 symmetric tensor. */ template void to_tensor(const FullMatrix &mtrx, SymmetricTensor<4, dim, Number> &st); /** * A generic helper function that will convert a compressed vector * to its equivalent @p TensorType. */ template TensorType to_tensor(const Vector &vec); /** * A generic helper function that will convert a compressed matrix * to its equivalent @p TensorType. */ template TensorType to_tensor(const FullMatrix &vec); /** @} */ } // namespace Kelvin } // namespace Notation } // namespace Physics #ifndef DOXYGEN // ------------------------- inline functions ------------------------ namespace Physics { namespace Notation { namespace Kelvin { namespace internal { /** * Return the tensor indices * associated with a condensed component index. The * @p symmetric flag indicates whether or not the * @p component_n index is associated with a tensor that * has symmetric entries. */ template std::pair indices_from_component(const unsigned int component_n, const bool symmetric); template std::pair indices_from_component(const unsigned int /*component_n*/, const bool) { AssertThrow(false, ExcNotImplemented()); return std::make_pair(0u, 0u); } template <> inline std::pair indices_from_component<1>(const unsigned int component_n, const bool) { AssertIndexRange(component_n, 1); return std::make_pair(0u, 0u); } template <> inline std::pair indices_from_component<2>(const unsigned int component_n, const bool symmetric) { if (symmetric == true) { Assert( (component_n < SymmetricTensor<2, 2>::n_independent_components), ExcIndexRange(component_n, 0, SymmetricTensor<2, 2>::n_independent_components)); } else { Assert((component_n < Tensor<2, 2>::n_independent_components), ExcIndexRange(component_n, 0, Tensor<2, 2>::n_independent_components)); } static const unsigned int indices[4][2] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; return std::make_pair(indices[component_n][0], indices[component_n][1]); } template <> inline std::pair indices_from_component<3>(const unsigned int component_n, const bool symmetric) { if (symmetric == true) { Assert( (component_n < SymmetricTensor<2, 3>::n_independent_components), ExcIndexRange(component_n, 0, SymmetricTensor<2, 3>::n_independent_components)); } else { Assert((component_n < Tensor<2, 3>::n_independent_components), ExcIndexRange(component_n, 0, Tensor<2, 3>::n_independent_components)); } static const unsigned int indices[9][2] = {{0, 0}, {1, 1}, {2, 2}, {1, 2}, {0, 2}, {0, 1}, {1, 0}, {2, 0}, {2, 1}}; return std::make_pair(indices[component_n][0], indices[component_n][1]); } /** * Return the scaling factor to be applied to the * entry in the condensed vector. */ template double vector_component_factor(const unsigned int component_i, const bool symmetric) { if (symmetric == false) return 1.0; if (component_i < dim) return 1.0; else return numbers::SQRT2; } /** * Return the scaling factor to be applied to the * entry in the condensed matrix. */ template double matrix_component_factor(const unsigned int component_i, const unsigned int component_j, const bool symmetric) { if (symmetric == false) return 1.0; // This case check returns equivalent of this result: // internal::vector_component_factor(component_i,symmetric)*internal::vector_component_factor(component_j,symmetric); if (component_i < dim && component_j < dim) return 1.0; else if (component_i >= dim && component_j >= dim) return 2.0; else // ((component_i >= dim && component_j < dim) || (component_i < // dim && component_j >= dim)) return numbers::SQRT2; } } // namespace internal template Vector to_vector(const Number &s) { Vector out(1); const unsigned int n_rows = out.size(); for (unsigned int r = 0; r < n_rows; ++r) out(r) = s; return out; } template Vector to_vector(const Tensor<0, dim, Number> &s) { return to_vector(s.operator const Number &()); } template Vector to_vector(const Tensor<1, dim, Number> &v) { Vector out(v.n_independent_components); const unsigned int n_rows = out.size(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices = internal::indices_from_component(r, false); Assert(indices.first < dim, ExcInternalError()); const unsigned int i = indices.first; out(r) = v[i]; } return out; } template Vector to_vector(const Tensor<2, dim, Number> &t) { Vector out(t.n_independent_components); const unsigned int n_rows = out.size(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices = internal::indices_from_component(r, false); Assert(indices.first < dim, ExcInternalError()); Assert(indices.second < dim, ExcInternalError()); const unsigned int i = indices.first; const unsigned int j = indices.second; out(r) = t[i][j]; } return out; } template Vector to_vector(const SymmetricTensor<2, dim, Number> &st) { Vector out(st.n_independent_components); const unsigned int n_rows = out.size(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices = internal::indices_from_component(r, true); Assert(indices.first < dim, ExcInternalError()); Assert(indices.second < dim, ExcInternalError()); Assert(indices.second >= indices.first, ExcInternalError()); const unsigned int i = indices.first; const unsigned int j = indices.second; const double factor = internal::vector_component_factor(r, true); out(r) = factor * st[i][j]; } return out; } template FullMatrix to_matrix(const Number &s) { FullMatrix out(1, 1); out(0, 0) = s; return out; } template FullMatrix to_matrix(const Tensor<0, dim, Number> &s) { return to_matrix(s.operator const Number &()); } template FullMatrix to_matrix(const Tensor<1, dim, Number> &v) { FullMatrix out(v.n_independent_components, 1); const unsigned int n_rows = out.m(); const unsigned int n_cols = out.n(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices = internal::indices_from_component(r, false); Assert(indices.first < dim, ExcInternalError()); const unsigned int i = indices.first; for (unsigned int c = 0; c < n_cols; ++c) { Assert(c < 1, ExcInternalError()); out(r, c) = v[i]; } } return out; } template FullMatrix to_matrix(const Tensor<2, dim, Number> &t) { FullMatrix out(dim, dim); const unsigned int n_rows = out.m(); const unsigned int n_cols = out.n(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices_i = internal::indices_from_component(r, false); Assert(indices_i.first < dim, ExcInternalError()); Assert(indices_i.second < dim, ExcInternalError()); const unsigned int i = indices_i.first; for (unsigned int c = 0; c < n_cols; ++c) { const std::pair indices_j = internal::indices_from_component(c, false); Assert(indices_j.first < dim, ExcInternalError()); Assert(indices_j.second < dim, ExcInternalError()); const unsigned int j = indices_j.second; out(r, c) = t[i][j]; } } return out; } template FullMatrix to_matrix(const SymmetricTensor<2, dim, Number> &st) { return to_matrix(Tensor<2, dim, Number>(st)); } namespace internal { template struct is_rank_2_symmetric_tensor : std::false_type {}; template struct is_rank_2_symmetric_tensor> : std::true_type {}; } // namespace internal template FullMatrix to_matrix(const Tensor<3, dim, Number> &t) { static_assert( (SubTensor1::dimension == dim && SubTensor2::dimension == dim), "Sub-tensor spatial dimension is different from those of the input tensor."); static_assert( (SubTensor1::rank == 2 && SubTensor2::rank == 1) || (SubTensor1::rank == 1 && SubTensor2::rank == 2), "Cannot build a rank 3 tensor from the given combination of sub-tensors."); FullMatrix out(SubTensor1::n_independent_components, SubTensor2::n_independent_components); const unsigned int n_rows = out.m(); const unsigned int n_cols = out.n(); if (SubTensor1::rank == 2 && SubTensor2::rank == 1) { const bool subtensor_is_rank_2_symmetric_tensor = internal::is_rank_2_symmetric_tensor::value; for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices_ij = internal::indices_from_component( r, subtensor_is_rank_2_symmetric_tensor); Assert(indices_ij.first < dim, ExcInternalError()); Assert(indices_ij.second < dim, ExcInternalError()); if (subtensor_is_rank_2_symmetric_tensor) { Assert(indices_ij.second >= indices_ij.first, ExcInternalError()); } const unsigned int i = indices_ij.first; const unsigned int j = indices_ij.second; const double factor = internal::vector_component_factor( r, subtensor_is_rank_2_symmetric_tensor); for (unsigned int c = 0; c < n_cols; ++c) { const std::pair indices_k = internal::indices_from_component(c, false); Assert(indices_k.first < dim, ExcInternalError()); const unsigned int k = indices_k.first; if (subtensor_is_rank_2_symmetric_tensor) out(r, c) = factor * t[i][j][k]; else out(r, c) = t[i][j][k]; } } } else if (SubTensor1::rank == 1 && SubTensor2::rank == 2) { const bool subtensor_is_rank_2_symmetric_tensor = internal::is_rank_2_symmetric_tensor::value; for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices_k = internal::indices_from_component(r, false); Assert(indices_k.first < dim, ExcInternalError()); const unsigned int k = indices_k.first; for (unsigned int c = 0; c < n_cols; ++c) { const std::pair indices_ij = internal::indices_from_component( c, subtensor_is_rank_2_symmetric_tensor); Assert(indices_ij.first < dim, ExcInternalError()); Assert(indices_ij.second < dim, ExcInternalError()); if (subtensor_is_rank_2_symmetric_tensor) { Assert(indices_ij.second >= indices_ij.first, ExcInternalError()); } const unsigned int i = indices_ij.first; const unsigned int j = indices_ij.second; if (subtensor_is_rank_2_symmetric_tensor) { const double factor = internal::vector_component_factor( c, subtensor_is_rank_2_symmetric_tensor); out(r, c) = factor * t[k][i][j]; } else out(r, c) = t[k][i][j]; } } } else { AssertThrow(false, ExcNotImplemented()); } return out; } template FullMatrix to_matrix(const Tensor<4, dim, Number> &t) { FullMatrix out( Tensor<2, dim, Number>::n_independent_components, Tensor<2, dim, Number>::n_independent_components); const unsigned int n_rows = out.m(); const unsigned int n_cols = out.n(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices_ij = internal::indices_from_component(r, false); Assert(indices_ij.first < dim, ExcInternalError()); Assert(indices_ij.second < dim, ExcInternalError()); const unsigned int i = indices_ij.first; const unsigned int j = indices_ij.second; for (unsigned int c = 0; c < n_cols; ++c) { const std::pair indices_kl = internal::indices_from_component(c, false); Assert(indices_kl.first < dim, ExcInternalError()); Assert(indices_kl.second < dim, ExcInternalError()); const unsigned int k = indices_kl.first; const unsigned int l = indices_kl.second; out(r, c) = t[i][j][k][l]; } } return out; } template FullMatrix to_matrix(const SymmetricTensor<4, dim, Number> &st) { FullMatrix out( SymmetricTensor<2, dim, Number>::n_independent_components, SymmetricTensor<2, dim, Number>::n_independent_components); const unsigned int n_rows = out.m(); const unsigned int n_cols = out.n(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices_ij = internal::indices_from_component(r, true); Assert(indices_ij.first < dim, ExcInternalError()); Assert(indices_ij.second < dim, ExcInternalError()); Assert(indices_ij.second >= indices_ij.first, ExcInternalError()); const unsigned int i = indices_ij.first; const unsigned int j = indices_ij.second; for (unsigned int c = 0; c < n_cols; ++c) { const std::pair indices_kl = internal::indices_from_component(c, true); Assert(indices_kl.first < dim, ExcInternalError()); Assert(indices_kl.second < dim, ExcInternalError()); Assert(indices_kl.second >= indices_kl.first, ExcInternalError()); const unsigned int k = indices_kl.first; const unsigned int l = indices_kl.second; const double factor = internal::matrix_component_factor(r, c, true); out(r, c) = factor * st[i][j][k][l]; } } return out; } template void to_tensor(const Vector &vec, Number &s) { Assert(vec.size() == 1, ExcDimensionMismatch(vec.size(), 1)); s = vec(0); } template void to_tensor(const Vector &vec, Tensor<0, dim, Number> &s) { return to_tensor(vec, s.operator Number &()); } template void to_tensor(const Vector &vec, Tensor<1, dim, Number> &v) { Assert(vec.size() == v.n_independent_components, ExcDimensionMismatch(vec.size(), v.n_independent_components)); const unsigned int n_rows = vec.size(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices = internal::indices_from_component(r, false); Assert(indices.first < dim, ExcInternalError()); const unsigned int i = indices.first; v[i] = vec(r); } } template void to_tensor(const Vector &vec, Tensor<2, dim, Number> &t) { Assert(vec.size() == t.n_independent_components, ExcDimensionMismatch(vec.size(), t.n_independent_components)); const unsigned int n_rows = vec.size(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices = internal::indices_from_component(r, false); Assert(indices.first < dim, ExcInternalError()); Assert(indices.second < dim, ExcInternalError()); const unsigned int i = indices.first; const unsigned int j = indices.second; t[i][j] = vec(r); } } template void to_tensor(const Vector &vec, SymmetricTensor<2, dim, Number> &st) { Assert(vec.size() == st.n_independent_components, ExcDimensionMismatch(vec.size(), st.n_independent_components)); const unsigned int n_rows = vec.size(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices = internal::indices_from_component(r, true); Assert(indices.first < dim, ExcInternalError()); Assert(indices.second < dim, ExcInternalError()); Assert(indices.second >= indices.first, ExcInternalError()); const unsigned int i = indices.first; const unsigned int j = indices.second; const double inv_factor = 1.0 / internal::vector_component_factor(r, true); st[i][j] = inv_factor * vec(r); } } template void to_tensor(const FullMatrix &mtrx, Number &s) { Assert(mtrx.m() == 1, ExcDimensionMismatch(mtrx.m(), 1)); Assert(mtrx.n() == 1, ExcDimensionMismatch(mtrx.n(), 1)); Assert(mtrx.n_elements() == 1, ExcDimensionMismatch(mtrx.n_elements(), 1)); s = mtrx(0, 0); } template void to_tensor(const FullMatrix &mtrx, Tensor<0, dim, Number> &s) { return to_tensor(mtrx, s.operator Number &()); } template void to_tensor(const FullMatrix &mtrx, Tensor<1, dim, Number> &v) { Assert(mtrx.m() == dim, ExcDimensionMismatch(mtrx.m(), dim)); Assert(mtrx.n() == 1, ExcDimensionMismatch(mtrx.n(), 1)); Assert(mtrx.n_elements() == v.n_independent_components, ExcDimensionMismatch(mtrx.n_elements(), v.n_independent_components)); const unsigned int n_rows = mtrx.m(); const unsigned int n_cols = mtrx.n(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices = internal::indices_from_component(r, false); Assert(indices.first < dim, ExcInternalError()); Assert(indices.second == 0, ExcInternalError()); const unsigned int i = indices.first; for (unsigned int c = 0; c < n_cols; ++c) { Assert(c < 1, ExcInternalError()); v[i] = mtrx(r, c); } } } template void to_tensor(const FullMatrix &mtrx, Tensor<2, dim, Number> &t) { Assert(mtrx.m() == dim, ExcDimensionMismatch(mtrx.m(), dim)); Assert(mtrx.n() == dim, ExcDimensionMismatch(mtrx.n(), dim)); Assert(mtrx.n_elements() == t.n_independent_components, ExcDimensionMismatch(mtrx.n_elements(), t.n_independent_components)); const unsigned int n_rows = mtrx.m(); const unsigned int n_cols = mtrx.n(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices_i = internal::indices_from_component(r, false); Assert(indices_i.first < dim, ExcInternalError()); Assert(indices_i.second < dim, ExcInternalError()); const unsigned int i = indices_i.first; for (unsigned int c = 0; c < n_cols; ++c) { const std::pair indices_j = internal::indices_from_component(c, false); Assert(indices_j.first < dim, ExcInternalError()); Assert(indices_j.second < dim, ExcInternalError()); const unsigned int j = indices_j.second; t[i][j] = mtrx(r, c); } } } template void to_tensor(const FullMatrix &mtrx, SymmetricTensor<2, dim, Number> &st) { // Its impossible to fit the (dim^2 + dim)/2 entries into a square // matrix We therefore assume that its been converted to a standard // tensor format using to_matrix (SymmetricTensor<2,dim,Number>) at some // point... Assert(mtrx.m() == dim, ExcDimensionMismatch(mtrx.m(), dim)); Assert(mtrx.n() == dim, ExcDimensionMismatch(mtrx.n(), dim)); Assert((mtrx.n_elements() == Tensor<2, dim, Number>::n_independent_components), ExcDimensionMismatch( mtrx.n_elements(), Tensor<2, dim, Number>::n_independent_components)); Tensor<2, dim, Number> tmp; to_tensor(mtrx, tmp); st = symmetrize(tmp); Assert((Tensor<2, dim, Number>(st) - tmp).norm() < 1e-12, ExcMessage( "The entries stored inside the matrix were not symmetric")); } template void to_tensor(const FullMatrix &mtrx, Tensor<3, dim, Number> &t) { Assert((mtrx.m() == Tensor<1, dim, Number>::n_independent_components) || (mtrx.m() == Tensor<2, dim, Number>::n_independent_components) || (mtrx.m() == SymmetricTensor<2, dim, Number>::n_independent_components), ExcNotationExcFullMatrixToTensorColSize3( mtrx.m(), Tensor<1, dim, Number>::n_independent_components, Tensor<2, dim, Number>::n_independent_components, SymmetricTensor<2, dim, Number>::n_independent_components)); Assert((mtrx.n() == Tensor<1, dim, Number>::n_independent_components) || (mtrx.n() == Tensor<2, dim, Number>::n_independent_components) || (mtrx.n() == SymmetricTensor<2, dim, Number>::n_independent_components), ExcNotationExcFullMatrixToTensorColSize3( mtrx.n(), Tensor<1, dim, Number>::n_independent_components, Tensor<2, dim, Number>::n_independent_components, SymmetricTensor<2, dim, Number>::n_independent_components)); const unsigned int n_rows = mtrx.m(); const unsigned int n_cols = mtrx.n(); if (mtrx.n() == Tensor<1, dim, Number>::n_independent_components) { Assert( (mtrx.m() == Tensor<2, dim, Number>::n_independent_components) || (mtrx.m() == SymmetricTensor<2, dim, Number>::n_independent_components), ExcNotationExcFullMatrixToTensorRowSize2( mtrx.m(), Tensor<2, dim, Number>::n_independent_components, SymmetricTensor<2, dim, Number>::n_independent_components)); const bool subtensor_is_rank_2_symmetric_tensor = (mtrx.m() == SymmetricTensor<2, dim, Number>::n_independent_components); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices_ij = internal::indices_from_component( r, subtensor_is_rank_2_symmetric_tensor); Assert(indices_ij.first < dim, ExcInternalError()); Assert(indices_ij.second < dim, ExcInternalError()); if (subtensor_is_rank_2_symmetric_tensor) { Assert(indices_ij.second >= indices_ij.first, ExcInternalError()); } const unsigned int i = indices_ij.first; const unsigned int j = indices_ij.second; const double inv_factor = 1.0 / internal::vector_component_factor( r, subtensor_is_rank_2_symmetric_tensor); for (unsigned int c = 0; c < n_cols; ++c) { const std::pair indices_k = internal::indices_from_component(c, false); Assert(indices_k.first < dim, ExcInternalError()); const unsigned int k = indices_k.first; if (subtensor_is_rank_2_symmetric_tensor) { t[i][j][k] = inv_factor * mtrx(r, c); t[j][i][k] = t[i][j][k]; } else t[i][j][k] = mtrx(r, c); } } } else { Assert( (mtrx.m() == Tensor<1, dim, Number>::n_independent_components), ExcDimensionMismatch( mtrx.m(), Tensor<1, dim, Number>::n_independent_components)); Assert( (mtrx.n() == Tensor<2, dim, Number>::n_independent_components) || (mtrx.n() == SymmetricTensor<2, dim, Number>::n_independent_components), ExcNotationExcFullMatrixToTensorColSize2( mtrx.n(), Tensor<2, dim, Number>::n_independent_components, SymmetricTensor<2, dim, Number>::n_independent_components)); const bool subtensor_is_rank_2_symmetric_tensor = (mtrx.n() == SymmetricTensor<2, dim, Number>::n_independent_components); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices_k = internal::indices_from_component(r, false); Assert(indices_k.first < dim, ExcInternalError()); const unsigned int k = indices_k.first; for (unsigned int c = 0; c < n_cols; ++c) { const std::pair indices_ij = internal::indices_from_component( c, subtensor_is_rank_2_symmetric_tensor); Assert(indices_ij.first < dim, ExcInternalError()); Assert(indices_ij.second < dim, ExcInternalError()); if (subtensor_is_rank_2_symmetric_tensor) { Assert(indices_ij.second >= indices_ij.first, ExcInternalError()); } const unsigned int i = indices_ij.first; const unsigned int j = indices_ij.second; if (subtensor_is_rank_2_symmetric_tensor) { const double inv_factor = 1.0 / internal::vector_component_factor( c, subtensor_is_rank_2_symmetric_tensor); t[k][i][j] = inv_factor * mtrx(r, c); t[k][j][i] = t[k][i][j]; } else t[k][i][j] = mtrx(r, c); } } } } template void to_tensor(const FullMatrix &mtrx, Tensor<4, dim, Number> &t) { Assert((mtrx.m() == Tensor<2, dim, Number>::n_independent_components), ExcDimensionMismatch( mtrx.m(), Tensor<2, dim, Number>::n_independent_components)); Assert((mtrx.n() == Tensor<2, dim, Number>::n_independent_components), ExcDimensionMismatch( mtrx.n(), Tensor<2, dim, Number>::n_independent_components)); Assert(mtrx.n_elements() == t.n_independent_components, ExcDimensionMismatch(mtrx.n_elements(), t.n_independent_components)); const unsigned int n_rows = mtrx.m(); const unsigned int n_cols = mtrx.n(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices_ij = internal::indices_from_component(r, false); Assert(indices_ij.first < dim, ExcInternalError()); Assert(indices_ij.second < dim, ExcInternalError()); const unsigned int i = indices_ij.first; const unsigned int j = indices_ij.second; for (unsigned int c = 0; c < n_cols; ++c) { const std::pair indices_kl = internal::indices_from_component(c, false); Assert(indices_kl.first < dim, ExcInternalError()); Assert(indices_kl.second < dim, ExcInternalError()); const unsigned int k = indices_kl.first; const unsigned int l = indices_kl.second; t[i][j][k][l] = mtrx(r, c); } } } template void to_tensor(const FullMatrix &mtrx, SymmetricTensor<4, dim, Number> &st) { Assert((mtrx.m() == SymmetricTensor<2, dim, Number>::n_independent_components), ExcDimensionMismatch( mtrx.m(), SymmetricTensor<2, dim, Number>::n_independent_components)); Assert((mtrx.n() == SymmetricTensor<2, dim, Number>::n_independent_components), ExcDimensionMismatch( mtrx.n(), SymmetricTensor<2, dim, Number>::n_independent_components)); Assert(mtrx.n_elements() == st.n_independent_components, ExcDimensionMismatch(mtrx.n_elements(), st.n_independent_components)); const unsigned int n_rows = mtrx.m(); const unsigned int n_cols = mtrx.n(); for (unsigned int r = 0; r < n_rows; ++r) { const std::pair indices_ij = internal::indices_from_component(r, false); Assert(indices_ij.first < dim, ExcInternalError()); Assert(indices_ij.second < dim, ExcInternalError()); const unsigned int i = indices_ij.first; const unsigned int j = indices_ij.second; for (unsigned int c = 0; c < n_cols; ++c) { const std::pair indices_kl = internal::indices_from_component(c, false); Assert(indices_kl.first < dim, ExcInternalError()); Assert(indices_kl.second < dim, ExcInternalError()); const unsigned int k = indices_kl.first; const unsigned int l = indices_kl.second; const double inv_factor = 1.0 / internal::matrix_component_factor(r, c, true); st[i][j][k][l] = inv_factor * mtrx(r, c); } } } template inline TensorType to_tensor(const Vector &vec) { TensorType out; to_tensor(vec, out); return out; } template inline TensorType to_tensor(const FullMatrix &mtrx) { TensorType out; to_tensor(mtrx, out); return out; } } // namespace Kelvin } // namespace Notation } // namespace Physics #endif // DOXYGEN DEAL_II_NAMESPACE_CLOSE #endif