47 #include "Intrepid_MiniTensor.h" 48 #include "Teuchos_UnitTestHarness.hpp" 49 #include "Teuchos_UnitTestRepository.hpp" 50 #include "Teuchos_GlobalMPISession.hpp" 52 int main(
int argc,
char* argv[])
54 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
55 std::cout <<
"End Result: TEST PASSED";
56 std::cout << std::endl;
57 return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
68 Index
const number_elements, T
const & start, T
const & increment)
73 for (Index i = 0; i < number_elements; ++i) {
74 v[i] = start + i * increment;
82 TEUCHOS_UNIT_TEST(MiniTensor, VectorFundamental)
90 std::vector<Real>
const 91 X = generate_sequence<Real>(number_components, 1.0, 1.0);
110 TEST_COMPARE(error, <=, machine_epsilon<Real>());
119 TEST_COMPARE(error, <=, machine_epsilon<Real>());
121 std::vector<Real>
const 122 Y = generate_sequence<Real>(number_components, -1.0, -1.0);
131 TEST_COMPARE(error, <=, machine_epsilon<Real>());
140 TEST_COMPARE(error, <=, machine_epsilon<Real>());
146 for (Index i = 0; i < dimension; ++i) {
147 error += integer_power(u(i) - u[counter], 2);
148 error += integer_power(v(i) - v[counter], 2);
152 TEST_COMPARE(error, <=, machine_epsilon<Real>());
155 TEUCHOS_UNIT_TEST(MiniTensor, TensorFundamental)
163 std::vector<Real>
const 164 X = generate_sequence<Real>(number_components, 1.0, 1.0);
183 TEST_COMPARE(error, <=, machine_epsilon<Real>());
192 TEST_COMPARE(error, <=, machine_epsilon<Real>());
194 std::vector<Real>
const 195 Y = generate_sequence<Real>(number_components, -1.0, -1.0);
204 TEST_COMPARE(error, <=, machine_epsilon<Real>());
213 TEST_COMPARE(error, <=, machine_epsilon<Real>());
219 for (Index i = 0; i < dimension; ++i) {
220 for (Index j = 0; j < dimension; ++j) {
221 error += integer_power(A(i,j) - A[counter], 2);
222 error += integer_power(B(i,j) - B[counter], 2);
227 TEST_COMPARE(error, <=, machine_epsilon<Real>());
230 TEUCHOS_UNIT_TEST(MiniTensor, Tensor3Fundamental)
238 std::vector<Real>
const 239 X = generate_sequence<Real>(number_components, 1.0, 1.0);
258 TEST_COMPARE(error, <=, machine_epsilon<Real>());
267 TEST_COMPARE(error, <=, machine_epsilon<Real>());
269 std::vector<Real>
const 270 Y = generate_sequence<Real>(number_components, -1.0, -1.0);
279 TEST_COMPARE(error, <=, machine_epsilon<Real>());
288 TEST_COMPARE(error, <=, machine_epsilon<Real>());
294 for (Index i = 0; i < dimension; ++i) {
295 for (Index j = 0; j < dimension; ++j) {
296 for (Index k = 0; k < dimension; ++k) {
297 error += integer_power(A(i,j,k) - A[counter], 2);
298 error += integer_power(B(i,j,k) - B[counter], 2);
304 TEST_COMPARE(error, <=, machine_epsilon<Real>());
307 TEUCHOS_UNIT_TEST(MiniTensor, Tensor4Fundamental)
315 std::vector<Real>
const 316 X = generate_sequence<Real>(number_components, 1.0, 1.0);
335 TEST_COMPARE(error, <=, machine_epsilon<Real>());
344 TEST_COMPARE(error, <=, machine_epsilon<Real>());
346 std::vector<Real>
const 347 Y = generate_sequence<Real>(number_components, -1.0, -1.0);
356 TEST_COMPARE(error, <=, machine_epsilon<Real>());
365 TEST_COMPARE(error, <=, machine_epsilon<Real>());
371 for (Index i = 0; i < dimension; ++i) {
372 for (Index j = 0; j < dimension; ++j) {
373 for (Index k = 0; k < dimension; ++k) {
374 for (Index l = 0; l < dimension; ++l) {
375 error += integer_power(A(i,j,k,l) - A[counter], 2);
376 error += integer_power(B(i,j,k,l) - B[counter], 2);
383 TEST_COMPARE(error, <=, machine_epsilon<Real>());
386 TEUCHOS_UNIT_TEST(MiniTensor, VectorFilling)
396 error = norm_f_square(u);
398 TEST_COMPARE(error, <=, machine_epsilon<Real>());
407 error = norm_f_square(v) - number_components;
409 TEST_COMPARE(error, <=, machine_epsilon<Real>());
413 w(dimension, RANDOM);
417 TEST_COMPARE(error, >, 0.0);
418 TEST_COMPARE(error, <, number_components);
425 TEST_COMPARE(error, >, 0.0);
426 TEST_COMPARE(error, <, number_components);
431 error = norm_f_square(v);
433 TEST_COMPARE(error, <=, machine_epsilon<Real>());
438 error = norm_f_square(w) - number_components;
440 TEST_COMPARE(error, <=, machine_epsilon<Real>());
443 TEUCHOS_UNIT_TEST(MiniTensor, TensorFilling)
453 error = norm_f_square(A);
455 TEST_COMPARE(error, <=, machine_epsilon<Real>());
464 error = norm_f_square(B) - number_components;
466 TEST_COMPARE(error, <=, machine_epsilon<Real>());
469 C(dimension, RANDOM);
473 TEST_COMPARE(error, >, 0.0);
474 TEST_COMPARE(error, <, number_components);
481 TEST_COMPARE(error, >, 0.0);
482 TEST_COMPARE(error, <, number_components);
487 error = norm_f_square(B);
489 TEST_COMPARE(error, <=, machine_epsilon<Real>());
494 error = norm_f_square(C) - number_components;
496 TEST_COMPARE(error, <=, machine_epsilon<Real>());
499 TEUCHOS_UNIT_TEST(MiniTensor, Tensor3Filling)
509 error = norm_f_square(A);
511 TEST_COMPARE(error, <=, machine_epsilon<Real>());
520 error = norm_f_square(B) - number_components;
522 TEST_COMPARE(error, <=, machine_epsilon<Real>());
525 C(dimension, RANDOM);
529 TEST_COMPARE(error, >, 0.0);
530 TEST_COMPARE(error, <, number_components);
537 TEST_COMPARE(error, >, 0.0);
538 TEST_COMPARE(error, <, number_components);
543 error = norm_f_square(B);
545 TEST_COMPARE(error, <=, machine_epsilon<Real>());
550 error = norm_f_square(C) - number_components;
552 TEST_COMPARE(error, <=, machine_epsilon<Real>());
555 TEUCHOS_UNIT_TEST(MiniTensor, Tensor4Filling)
565 error = norm_f_square(A);
567 TEST_COMPARE(error, <=, machine_epsilon<Real>());
576 error = norm_f_square(B) - number_components;
578 TEST_COMPARE(error, <=, machine_epsilon<Real>());
581 C(dimension, RANDOM);
585 TEST_COMPARE(error, >, 0.0);
586 TEST_COMPARE(error, <, number_components);
593 TEST_COMPARE(error, >, 0.0);
594 TEST_COMPARE(error, <, number_components);
599 error = norm_f_square(B);
601 TEST_COMPARE(error, <=, machine_epsilon<Real>());
606 error = norm_f_square(C) - number_components;
608 TEST_COMPARE(error, <=, machine_epsilon<Real>());
611 TEUCHOS_UNIT_TEST(MiniTensor, Tensor4Products)
626 error = norm_f_square(B) - factor * factor * B.get_number_components();
628 TEST_COMPARE(error, <=, machine_epsilon<Real>());
633 error = norm_f_square(C) - factor * factor * C.get_number_components();
635 TEST_COMPARE(error, <=, machine_epsilon<Real>());
638 TEUCHOS_UNIT_TEST(MiniTensor, Initialization)
640 FieldContainer<Real> FC(3, 3);
651 Real
const * dataPtr0 = &FC(0, 0);
654 Vector<Real> u(N, dataPtr0);
656 TEST_COMPARE( u(0), ==, 1.0);
657 TEST_COMPARE( u(1), ==, 2.0);
658 TEST_COMPARE( u(2), ==, 3.0);
660 Real
const * dataPtr1 = &FC(1, 0);
662 u = Vector<Real>(N, dataPtr1);
664 TEST_COMPARE( u(0), ==, 4.0);
665 TEST_COMPARE( u(1), ==, 5.0);
666 TEST_COMPARE( u(2), ==, 6.0);
668 Real
const * dataPtr2 = &FC(2, 0);
670 u = Vector<Real>(N, dataPtr2);
672 TEST_COMPARE( u(0), ==, 7.0);
673 TEST_COMPARE( u(1), ==, 8.0);
674 TEST_COMPARE( u(2), ==, 9.0);
677 TEUCHOS_UNIT_TEST(MiniTensor, VectorAddition)
679 Vector<Real>
const u(1.0, 0.0, 0.0);
680 Vector<Real>
const v(0.0, 1.0, 0.0);
681 Vector<Real>
const w(1.0, 1.0, 0.0);
683 TEST_COMPARE( u + v == w, !=, 0);
686 TEUCHOS_UNIT_TEST(MiniTensor, VectorSubtraction)
696 TEST_COMPARE(norm(v), <=, machine_epsilon<Real>());
699 TEUCHOS_UNIT_TEST(MiniTensor, VectorScalarMultipliaction)
712 w = 4.0 * u + 2.0 * v;
714 TEST_COMPARE( norm(w), <=, machine_epsilon<Real>());
717 TEUCHOS_UNIT_TEST(MiniTensor, TensorInstantiation)
719 FieldContainer<Real> FC(2, 3, 3);
739 Real
const * dataPtr0 = &FC(0, 0, 0);
741 Tensor<Real>
const A(3, dataPtr0);
743 TEST_COMPARE( A(0,0), ==, 1.0);
744 TEST_COMPARE( A(0,1), ==, 2.0);
745 TEST_COMPARE( A(0,2), ==, 3.0);
746 TEST_COMPARE( A(1,0), ==, 4.0);
747 TEST_COMPARE( A(1,1), ==, 5.0);
748 TEST_COMPARE( A(1,2), ==, 6.0);
749 TEST_COMPARE( A(2,0), ==, 7.0);
750 TEST_COMPARE( A(2,1), ==, 8.0);
751 TEST_COMPARE( A(2,2), ==, 9.0);
753 Real
const * dataPtr1 = &FC(1, 0, 0);
755 Tensor<Real>
const B(3, dataPtr1);
757 TEST_COMPARE( B(0,0), ==, 10.0);
758 TEST_COMPARE( B(0,1), ==, 11.0);
759 TEST_COMPARE( B(0,2), ==, 12.0);
760 TEST_COMPARE( B(1,0), ==, 13.0);
761 TEST_COMPARE( B(1,1), ==, 14.0);
762 TEST_COMPARE( B(1,2), ==, 15.0);
763 TEST_COMPARE( B(2,0), ==, 16.0);
764 TEST_COMPARE( B(2,1), ==, 17.0);
765 TEST_COMPARE( B(2,2), ==, 18.0);
768 TEUCHOS_UNIT_TEST(MiniTensor, TensorAddition)
770 Tensor<Real>
const A(3, 1.0);
771 Tensor<Real>
const B(3, 2.0);
772 Tensor<Real>
const C(3, 3.0);
774 TEST_COMPARE( C == A + B, !=, 0);
777 TEUCHOS_UNIT_TEST(MiniTensor, Inverse)
779 std::srand(std::time(NULL));
780 Index
const N = double(std::rand()) / double(RAND_MAX) * 7.0 + 3.0;
785 for (Index i = 0; i < N; ++i) {
786 for (Index j = 0; j < N; ++j) {
787 A(i, j) = double(std::rand()) / double(RAND_MAX) * 20.0 - 10.0;
795 Real
const error = norm(C - eye<Real>(N)) / norm(A);
797 TEST_COMPARE(error, <=, 100.0 * machine_epsilon<Real>());
800 TEUCHOS_UNIT_TEST(MiniTensor, TensorManipulation)
802 Tensor<Real> A = eye<Real>(3);
808 A(1, 0) = A(0, 1) = 1.0;
809 A(2, 1) = A(1, 2) = 1.0;
815 TEST_COMPARE(norm(C - eye<Real>(3)), <=, machine_epsilon<Real>());
825 Real
const error = norm(u);
827 TEST_COMPARE(error, <=, machine_epsilon<Real>());
830 TEUCHOS_UNIT_TEST(MiniTensor, Exponential)
832 Tensor<Real>
const A(1, 2, 3, 4, 5, 6, 7, 8, 9);
834 Tensor<Real>
const B = exp_pade(A);
836 Tensor<Real>
const C = exp_taylor(A);
838 Tensor<Real>
const D = B - C;
840 Real
const error = norm(D) / norm(B);
842 TEST_COMPARE( error, <=, 100.0 * machine_epsilon<Real>());
845 TEUCHOS_UNIT_TEST(MiniTensor, SymmetricEigen)
847 Tensor<Real> A = eye<Real>(3);
854 boost::tie(V, D) = eig_sym(A);
856 TEST_COMPARE(std::abs(D(0,0) - 1.1), <=, machine_epsilon<Real>());
857 TEST_COMPARE(std::abs(D(1,1) - 1.0), <=, machine_epsilon<Real>());
858 TEST_COMPARE(std::abs(D(2,2) - 0.9), <=, machine_epsilon<Real>());
861 TEUCHOS_UNIT_TEST(MiniTensor, LeftPolarDecomposition)
863 Tensor<Real> V0(1.1, 0.2, 0.0, 0.2, 1.0, 0.0, 0.0, 0.0, 1.2);
865 Tensor<Real> R0(sqrt(2) / 2, -sqrt(2) / 2, 0.0, sqrt(2) / 2, sqrt(2) / 2,
868 Tensor<Real> F = V0 * R0;
871 boost::tie(V, R) = polar_left(F);
873 TEST_COMPARE(norm(V-V0), <=, 10.0*machine_epsilon<Real>());
874 TEST_COMPARE(norm(R-R0), <=, machine_epsilon<Real>());
877 TEUCHOS_UNIT_TEST(MiniTensor, LogRotation)
879 Tensor<Real> R = identity<Real>(3);
880 Tensor<Real> R0(sqrt(2) / 2, -sqrt(2) / 2, 0.0, sqrt(2) / 2, sqrt(2) / 2,
883 Tensor<Real> r = log_rotation(R);
884 Tensor<Real> r0 = log_rotation(R0);
886 TEST_COMPARE(norm(r), <=, machine_epsilon<Real>());
888 TEST_COMPARE( std::abs(r0(0,1) + 0.785398163397448), <=,
889 10.0*machine_epsilon<Real>());
891 TEST_COMPARE( std::abs(r0(0,1) + r0(1,0)), <=, machine_epsilon<Real>());
893 Real theta = std::acos(-1.0) + 10 * machine_epsilon<Real>();
895 R(0, 0) = cos(theta);
896 R(1, 1) = cos(theta);
897 R(0, 1) = sin(theta);
898 R(1, 0) = -sin(theta);
901 Tensor<Real> logR = log_rotation(R);
903 Tensor<Real> Rref(3, 0.0);
907 TEST_COMPARE(norm(logR - Rref), <=, 100*machine_epsilon<Real>());
910 TEUCHOS_UNIT_TEST(MiniTensor, BakerCampbellHausdorff)
912 Tensor<Real> F = 3.0 * identity<Real>(3);
913 Tensor<Real> V(3), R(3), logV(3), logR(3);
915 boost::tie(V, R, logV) = polar_left_logV(F);
916 logR = log_rotation(R);
918 Tensor<Real> f = bch(logV, logR);
920 TEST_COMPARE( std::abs(f(0,0) - std::log(3.0)), <=,
921 machine_epsilon<Real>());
924 u(0) = std::acos(-1.0) / std::sqrt(2.0);
928 Tensor<Real> R1(3, 0.0);
929 Tensor<Real> logR2(3, 0.0);
937 R1 = exp_skew_symmetric(logR2);
938 Tensor<Real> Rref = zero<Real>(3);
943 TEST_COMPARE( norm(Rref-R1), <=, 100.0*machine_epsilon<Real>());
944 TEST_COMPARE( norm(exp_skew_symmetric(logR) - R), <=,
945 100.0*machine_epsilon<Real>());
948 TEUCHOS_UNIT_TEST(MiniTensor, PolarLeftLog)
950 Tensor<Real>
const F(3.60070151614402, 0.00545892068653966,
951 0.144580850331452, -5.73345529510674, 0.176660910549112,
952 1.39627497290058, 2.51510445213514, 0.453212159218359,
955 Tensor<Real>
const L(0.265620603957487, -1.066921781600734,
956 -0.089540974250415, -1.066921781600734, 0.927394431410918,
957 -0.942214085118614, -0.089540974250415, -0.942214085118613,
960 Tensor<Real> V(3), R(3), v(3), r(3);
962 boost::tie(V, R, v) = polar_left_logV(F);
964 Real
const error = norm(v - L) / norm(L);
966 TEST_COMPARE( error, <=, 100*machine_epsilon<Real>());
969 TEUCHOS_UNIT_TEST(MiniTensor, VolumetricDeviatoric)
971 Tensor<Real> A = 3.0 * eye<Real>(3);
973 TEST_COMPARE( norm(A - vol(A)), <=, 100.0*machine_epsilon<Real>());
975 Tensor<Real> B = dev(A);
981 TEST_COMPARE( norm(A - B), <=, 100.0*machine_epsilon<Real>());
984 TEUCHOS_UNIT_TEST(MiniTensor, SVD2x2)
986 Real
const phi = 1.0;
988 Real
const psi = 2.0;
990 Real
const s0 = sqrt(3.0);
992 Real
const s1 = sqrt(2.0);
994 Real
const cl = cos(phi);
996 Real
const sl = sin(phi);
998 Real
const cr = cos(psi);
1000 Real
const sr = sin(psi);
1002 Tensor<Real>
const X(cl, -sl, sl, cl);
1004 Tensor<Real>
const Y(cr, -sr, sr, cr);
1006 Tensor<Real>
const D(s0, 0.0, 0.0, s1);
1008 Tensor<Real>
const A = X * D * transpose(Y);
1010 Tensor<Real> U(2), S(2), V(2);
1012 boost::tie(U, S, V) = svd(A);
1014 Tensor<Real> B = U * S * transpose(V);
1016 Real
const error = norm(A - B) / norm(A);
1018 TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1021 TEUCHOS_UNIT_TEST(MiniTensor, SVD3x3)
1023 Tensor<Real>
const A(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
1025 Tensor<Real> U(3), S(3), V(3);
1027 boost::tie(U, S, V) = svd(A);
1029 Tensor<Real>
const B = U * S * transpose(V);
1031 Real
const error = norm(A - B) / norm(A);
1033 TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1036 TEUCHOS_UNIT_TEST(MiniTensor, SVD3x3Fad)
1038 Tensor<Sacado::Fad::DFad<double> > A(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
1041 Tensor<Sacado::Fad::DFad<double> > U(3), S(3), V(3);
1043 boost::tie(U, S, V) = svd(A);
1045 Tensor<Sacado::Fad::DFad<double> > B = U * S * transpose(V);
1047 Sacado::Fad::DFad<double>
const error = norm(B - A) / norm(A);
1049 TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1052 TEUCHOS_UNIT_TEST(MiniTensor, SymmetricEigen2x2)
1054 Tensor<Real>
const A(2.0, 1.0, 1.0, 2.0);
1056 Tensor<Real> V(2), D(2);
1058 boost::tie(V, D) = eig_sym(A);
1060 Tensor<Real>
const B = V * D * transpose(V);
1062 Real
const error = norm(A - B) / norm(A);
1064 TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1067 TEUCHOS_UNIT_TEST(MiniTensor, SymmetricEigen3x3)
1069 Tensor<Real>
const A(2.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0);
1071 Tensor<Real> V(3), D(3);
1073 boost::tie(V, D) = eig_sym(A);
1075 Tensor<Real>
const B = V * D * transpose(V);
1077 Real
const error = norm(A - B) / norm(A);
1079 TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1082 TEUCHOS_UNIT_TEST(MiniTensor, Inverse4x4)
1084 Tensor<Real> A = 2.0 * identity<Real>(4);
1095 Tensor<Real>
const B = inverse(A);
1097 Tensor<Real>
const C = A * B;
1099 Tensor<Real>
const I = eye<Real>(4);
1101 Real
const error = norm(C - I) / norm(A);
1103 TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1106 TEUCHOS_UNIT_TEST(MiniTensor, Polar3x3)
1108 Tensor<Real> A(2.0, 1.0, 0.0, 0.0, 2.0, 1.0, 0.0, 0.0, 2.0);
1110 Tensor<Real> R(3), U(3);
1112 boost::tie(R, U) = polar_right(A);
1114 Tensor<Real> X(3), D(3), Y(3);
1116 boost::tie(X, D, Y) = svd(A);
1118 Tensor<Real> B = R - X * transpose(Y) + U - Y * D * transpose(Y);
1120 Real
const error = norm(B) / norm(A);
1122 TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1125 TEUCHOS_UNIT_TEST(MiniTensor, Cholesky)
1127 Tensor<Real> A(1.0, 1.0, 1.0, 1.0, 5.0, 3.0, 1.0, 3.0, 3.0);
1133 boost::tie(G, is_spd) = cholesky(A);
1135 Tensor<Real> B(1.0, 0.0, 0.0, 1.0, 2.0, 0.0, 1.0, 1.0, 1.0);
1137 Real
const error = norm(G - B) / norm(A);
1139 TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1142 TEUCHOS_UNIT_TEST(MiniTensor, MechanicsTransforms)
1144 Tensor<Real> F(0.0, -6.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 1.0 / 3.0);
1146 Tensor<Real> sigma(0.0, 0.0, 0.0, 0.0, 50.0, 0.0, 0.0, 0.0, 0.0);
1148 Tensor<Real> P = piola(F, sigma);
1150 Real error = abs(P(1, 0) - 100.0) / 100.0;
1152 TEST_COMPARE(error, <=, machine_epsilon<Real>());
1154 sigma = piola_inverse(F, P);
1156 error = abs(sigma(1, 1) - 50.0) / 50.0;
1158 TEST_COMPARE(error, <=, machine_epsilon<Real>());
1160 Tensor<Real> E = 0.5 * (t_dot(F, F) - eye<Real>(3));
1162 Tensor<Real> e = 0.5 * (eye<Real>(3) - inverse(dot_t(F, F)));
1164 Tensor<Real> g = push_forward_covariant(F, E);
1166 error = norm(g - e) / norm(e);
1168 TEST_COMPARE(error, <=, machine_epsilon<Real>());
1170 Tensor<Real> G = pull_back_covariant(F, e);
1172 error = norm(G - E) / norm(E);
1174 TEST_COMPARE(error, <=, machine_epsilon<Real>());
int main(int argc, char *argv[])
Performs a code-code comparison to FIAT for Nedelec bases on tets (values and curls) ...
Header file for utility class to provide multidimensional containers.