diff --git a/MathLib/KelvinVector.cpp b/MathLib/KelvinVector.cpp index 9dccaff6bb95ebba0e8eb19e301f5ea34ebd7000..94ccd6482bf1dd0cc2cd7105190f89e804aca713 100644 --- a/MathLib/KelvinVector.cpp +++ b/MathLib/KelvinVector.cpp @@ -9,6 +9,8 @@ #include "KelvinVector.h" +#include "BaseLib/Error.h" + namespace MathLib { namespace KelvinVector @@ -78,5 +80,50 @@ Eigen::Matrix<double, 3, 3> kelvinVectorToTensor( return m; } +template <> +Eigen::Matrix<double, 4, 1> kelvinVectorToSymmetricTensor( + Eigen::Matrix<double, 4, 1, Eigen::ColMajor, 4, 1> const& v) +{ + Eigen::Matrix<double, 4, 1> m; + m << v[0], v[1], v[2], v[3] / std::sqrt(2.); + return m; +} + +template <> +Eigen::Matrix<double, 6, 1> kelvinVectorToSymmetricTensor( + Eigen::Matrix<double, 6, 1, Eigen::ColMajor, 6, 1> const& v) +{ + Eigen::Matrix<double, 6, 1> m; + m << v[0], v[1], v[2], v[3] / std::sqrt(2.), v[4] / std::sqrt(2.), + v[5] / std::sqrt(2.); + return m; +} + +template <> +Eigen::Matrix<double, Eigen::Dynamic, 1, Eigen::ColMajor, Eigen::Dynamic, 1> +kelvinVectorToSymmetricTensor(Eigen::Matrix<double, + Eigen::Dynamic, + 1, + Eigen::ColMajor, + Eigen::Dynamic, + 1> const& v) +{ + if (v.size() == 4) + { + return kelvinVectorToSymmetricTensor<4>(v); + } + else if (v.size() == 6) + { + return kelvinVectorToSymmetricTensor<6>(v); + } + else + { + OGS_FATAL( + "Kelvin vector to tensor conversion expected an input vector of " + "size 4 or 6, but a vector of size %d was given.", + v.size()); + } +} + } // namespace KelvinVector } // namespace MathLib diff --git a/MathLib/KelvinVector.h b/MathLib/KelvinVector.h index 3274327e755c6f987dd9eea1892559aa55aff427..d57a1eac3b84d9ec53af4bf6106616fa20a9d8fb 100644 --- a/MathLib/KelvinVector.h +++ b/MathLib/KelvinVector.h @@ -125,6 +125,24 @@ Eigen::Matrix<double, 3, 3> kelvinVectorToTensor(Eigen::Matrix<double, KelvinVectorSize, 1> const& v); +/// Conversion of a Kelvin vector to a short vector representation of a +/// symmetric 3x3 matrix. +/// +/// In the 2D case the entries for the xx, yy, zz, and xy components are stored. +/// In the 3D case the entries for the xx, yy, zz, xy, yz, and xz components in +/// that particular order are stored. +/// +/// Only implementations for KelvinVectorSize 4 and 6, and dynamic size vectors +/// are provided. +template <int KelvinVectorSize> +Eigen::Matrix<double, KelvinVectorSize, 1, Eigen::ColMajor, KelvinVectorSize, 1> +kelvinVectorToSymmetricTensor(Eigen::Matrix<double, + KelvinVectorSize, + 1, + Eigen::ColMajor, + KelvinVectorSize, + 1> const& v); + } // namespace KelvinVector } // namespace MathLib