Intrepid
test_01.cpp
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions: Alejandro Mota (amota@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #include <ctime>
43 #include <vector>
44 
46 #include "Sacado.hpp"
47 #include "Intrepid_MiniTensor.h"
48 #include "Teuchos_UnitTestHarness.hpp"
49 #include "Teuchos_UnitTestRepository.hpp"
50 #include "Teuchos_GlobalMPISession.hpp"
51 
52 int main(int argc, char* argv[])
53 {
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);
58 }
59 
60 namespace Intrepid
61 {
62 
63 namespace {
64 
65 template<typename T>
66 std::vector<T>
67 generate_sequence(
68  Index const number_elements, T const & start, T const & increment)
69 {
70  std::vector<T>
71  v(number_elements);
72 
73  for (Index i = 0; i < number_elements; ++i) {
74  v[i] = start + i * increment;
75  }
76 
77  return v;
78 }
79 
80 } // anonymous namescape
81 
82 TEUCHOS_UNIT_TEST(MiniTensor, VectorFundamental)
83 {
84  Index const
85  dimension = 3;
86 
87  Index const
88  number_components = integer_power(dimension, Vector<Real>::order);
89 
90  std::vector<Real> const
91  X = generate_sequence<Real>(number_components, 1.0, 1.0);
92 
93  // Test constructor with pointer
94  Vector<Real> const
95  u(dimension, &X[0]);
96 
97  // Test copy constructor
98  Vector<Real>
99  v = u;
100 
101  Vector<Real>
102  w;
103 
104  // Test copy assignment
105  w = v - u;
106 
107  Real
108  error = norm_f(w);
109 
110  TEST_COMPARE(error, <=, machine_epsilon<Real>());
111 
112  // Test fill with pointer
113  v.fill(&X[0]);
114 
115  w = v - u;
116 
117  error = norm_f(w);
118 
119  TEST_COMPARE(error, <=, machine_epsilon<Real>());
120 
121  std::vector<Real> const
122  Y = generate_sequence<Real>(number_components, -1.0, -1.0);
123 
124  w.fill(&Y[0]);
125 
126  // Test increment
127  w += u;
128 
129  error = norm_f(w);
130 
131  TEST_COMPARE(error, <=, machine_epsilon<Real>());
132 
133  w.fill(&X[0]);
134 
135  // Test decrement
136  w -= u;
137 
138  error = norm_f(w);
139 
140  TEST_COMPARE(error, <=, machine_epsilon<Real>());
141 
142  // Test access to components
143  Index
144  counter = 0;
145  error = 0.0;
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);
149  ++counter;
150  }
151 
152  TEST_COMPARE(error, <=, machine_epsilon<Real>());
153 }
154 
155 TEUCHOS_UNIT_TEST(MiniTensor, TensorFundamental)
156 {
157  Index const
158  dimension = 3;
159 
160  Index const
161  number_components = integer_power(dimension, Tensor<Real>::order);
162 
163  std::vector<Real> const
164  X = generate_sequence<Real>(number_components, 1.0, 1.0);
165 
166  // Test constructor with pointer
167  Tensor<Real> const
168  A(dimension, &X[0]);
169 
170  // Test copy constructor
171  Tensor<Real>
172  B = A;
173 
174  Tensor<Real>
175  C;
176 
177  // Test copy assignment
178  C = B - A;
179 
180  Real
181  error = norm_f(C);
182 
183  TEST_COMPARE(error, <=, machine_epsilon<Real>());
184 
185  // Test fill with pointer
186  B.fill(&X[0]);
187 
188  C = B - A;
189 
190  error = norm_f(C);
191 
192  TEST_COMPARE(error, <=, machine_epsilon<Real>());
193 
194  std::vector<Real> const
195  Y = generate_sequence<Real>(number_components, -1.0, -1.0);
196 
197  C.fill(&Y[0]);
198 
199  // Test increment
200  C += A;
201 
202  error = norm_f(C);
203 
204  TEST_COMPARE(error, <=, machine_epsilon<Real>());
205 
206  C.fill(&X[0]);
207 
208  // Test decrement
209  C -= A;
210 
211  error = norm_f(C);
212 
213  TEST_COMPARE(error, <=, machine_epsilon<Real>());
214 
215  // Test access to components
216  Index
217  counter = 0;
218  error = 0.0;
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);
223  ++counter;
224  }
225  }
226 
227  TEST_COMPARE(error, <=, machine_epsilon<Real>());
228 }
229 
230 TEUCHOS_UNIT_TEST(MiniTensor, Tensor3Fundamental)
231 {
232  Index const
233  dimension = 3;
234 
235  Index const
236  number_components = integer_power(dimension, Tensor3<Real>::order);
237 
238  std::vector<Real> const
239  X = generate_sequence<Real>(number_components, 1.0, 1.0);
240 
241  // Test constructor with pointer
242  Tensor3<Real> const
243  A(dimension, &X[0]);
244 
245  // Test copy constructor
246  Tensor3<Real>
247  B = A;
248 
249  Tensor3<Real>
250  C;
251 
252  // Test copy assignment
253  C = B - A;
254 
255  Real
256  error = norm_f(C);
257 
258  TEST_COMPARE(error, <=, machine_epsilon<Real>());
259 
260  // Test fill with pointer
261  B.fill(&X[0]);
262 
263  C = B - A;
264 
265  error = norm_f(C);
266 
267  TEST_COMPARE(error, <=, machine_epsilon<Real>());
268 
269  std::vector<Real> const
270  Y = generate_sequence<Real>(number_components, -1.0, -1.0);
271 
272  C.fill(&Y[0]);
273 
274  // Test increment
275  C += A;
276 
277  error = norm_f(C);
278 
279  TEST_COMPARE(error, <=, machine_epsilon<Real>());
280 
281  C.fill(&X[0]);
282 
283  // Test decrement
284  C -= A;
285 
286  error = norm_f(C);
287 
288  TEST_COMPARE(error, <=, machine_epsilon<Real>());
289 
290  // Test access to components
291  Index
292  counter = 0;
293  error = 0.0;
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);
299  ++counter;
300  }
301  }
302  }
303 
304  TEST_COMPARE(error, <=, machine_epsilon<Real>());
305 }
306 
307 TEUCHOS_UNIT_TEST(MiniTensor, Tensor4Fundamental)
308 {
309  Index const
310  dimension = 3;
311 
312  Index const
313  number_components = integer_power(dimension, Tensor4<Real>::order);
314 
315  std::vector<Real> const
316  X = generate_sequence<Real>(number_components, 1.0, 1.0);
317 
318  // Test constructor with pointer
319  Tensor4<Real> const
320  A(dimension, &X[0]);
321 
322  // Test copy constructor
323  Tensor4<Real>
324  B = A;
325 
326  Tensor4<Real>
327  C;
328 
329  // Test copy assignment
330  C = B - A;
331 
332  Real
333  error = norm_f(C);
334 
335  TEST_COMPARE(error, <=, machine_epsilon<Real>());
336 
337  // Test fill with pointer
338  B.fill(&X[0]);
339 
340  C = B - A;
341 
342  error = norm_f(C);
343 
344  TEST_COMPARE(error, <=, machine_epsilon<Real>());
345 
346  std::vector<Real> const
347  Y = generate_sequence<Real>(number_components, -1.0, -1.0);
348 
349  C.fill(&Y[0]);
350 
351  // Test increment
352  C += A;
353 
354  error = norm_f(C);
355 
356  TEST_COMPARE(error, <=, machine_epsilon<Real>());
357 
358  C.fill(&X[0]);
359 
360  // Test decrement
361  C -= A;
362 
363  error = norm_f(C);
364 
365  TEST_COMPARE(error, <=, machine_epsilon<Real>());
366 
367  // Test access to components
368  Index
369  counter = 0;
370  error = 0.0;
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);
377  ++counter;
378  }
379  }
380  }
381  }
382 
383  TEST_COMPARE(error, <=, machine_epsilon<Real>());
384 }
385 
386 TEUCHOS_UNIT_TEST(MiniTensor, VectorFilling)
387 {
388  Index const
389  dimension = 3;
390 
391  // Test construct with zeros
392  Vector<Real>
393  u(dimension, ZEROS);
394 
395  Real
396  error = norm_f_square(u);
397 
398  TEST_COMPARE(error, <=, machine_epsilon<Real>());
399 
400  // Test construct with ones
401  Vector<Real>
402  v(dimension, ONES);
403 
404  Index const
405  number_components = integer_power(dimension, Vector<Real>::order);
406 
407  error = norm_f_square(v) - number_components;
408 
409  TEST_COMPARE(error, <=, machine_epsilon<Real>());
410 
411  // Test construct random components
412  Vector<Real>
413  w(dimension, RANDOM);
414 
415  error = norm_f(w);
416 
417  TEST_COMPARE(error, >, 0.0);
418  TEST_COMPARE(error, <, number_components);
419 
420  // Test fill with random components
421  u.fill(RANDOM);
422 
423  error = norm_f(u);
424 
425  TEST_COMPARE(error, >, 0.0);
426  TEST_COMPARE(error, <, number_components);
427 
428  // Test fill with zeros
429  v.fill(ZEROS);
430 
431  error = norm_f_square(v);
432 
433  TEST_COMPARE(error, <=, machine_epsilon<Real>());
434 
435  // Test fill with ones
436  w.fill(ZEROS);
437 
438  error = norm_f_square(w) - number_components;
439 
440  TEST_COMPARE(error, <=, machine_epsilon<Real>());
441 }
442 
443 TEUCHOS_UNIT_TEST(MiniTensor, TensorFilling)
444 {
445  Index const
446  dimension = 3;
447 
448  // Test construct with zeros
449  Tensor<Real>
450  A(dimension, ZEROS);
451 
452  Real
453  error = norm_f_square(A);
454 
455  TEST_COMPARE(error, <=, machine_epsilon<Real>());
456 
457  // Test construct with ones
458  Tensor<Real>
459  B(dimension, ONES);
460 
461  Index const
462  number_components = integer_power(dimension, Tensor<Real>::order);
463 
464  error = norm_f_square(B) - number_components;
465 
466  TEST_COMPARE(error, <=, machine_epsilon<Real>());
467 
468  Tensor<Real>
469  C(dimension, RANDOM);
470 
471  error = norm_f(C);
472 
473  TEST_COMPARE(error, >, 0.0);
474  TEST_COMPARE(error, <, number_components);
475 
476  // Test fill with random components
477  A.fill(RANDOM);
478 
479  error = norm_f(A);
480 
481  TEST_COMPARE(error, >, 0.0);
482  TEST_COMPARE(error, <, number_components);
483 
484  // Test fill with zeros
485  B.fill(ZEROS);
486 
487  error = norm_f_square(B);
488 
489  TEST_COMPARE(error, <=, machine_epsilon<Real>());
490 
491  // Test fill with ones
492  C.fill(ZEROS);
493 
494  error = norm_f_square(C) - number_components;
495 
496  TEST_COMPARE(error, <=, machine_epsilon<Real>());
497 }
498 
499 TEUCHOS_UNIT_TEST(MiniTensor, Tensor3Filling)
500 {
501  Index const
502  dimension = 3;
503 
504  // Test construct with zeros
505  Tensor3<Real>
506  A(dimension, ZEROS);
507 
508  Real
509  error = norm_f_square(A);
510 
511  TEST_COMPARE(error, <=, machine_epsilon<Real>());
512 
513  // Test construct with ones
514  Tensor3<Real>
515  B(dimension, ONES);
516 
517  Index const
518  number_components = integer_power(dimension, Tensor3<Real>::order);
519 
520  error = norm_f_square(B) - number_components;
521 
522  TEST_COMPARE(error, <=, machine_epsilon<Real>());
523 
524  Tensor3<Real>
525  C(dimension, RANDOM);
526 
527  error = norm_f(C);
528 
529  TEST_COMPARE(error, >, 0.0);
530  TEST_COMPARE(error, <, number_components);
531 
532  // Test fill with random components
533  A.fill(RANDOM);
534 
535  error = norm_f(A);
536 
537  TEST_COMPARE(error, >, 0.0);
538  TEST_COMPARE(error, <, number_components);
539 
540  // Test fill with zeros
541  B.fill(ZEROS);
542 
543  error = norm_f_square(B);
544 
545  TEST_COMPARE(error, <=, machine_epsilon<Real>());
546 
547  // Test fill with ones
548  C.fill(ZEROS);
549 
550  error = norm_f_square(C) - number_components;
551 
552  TEST_COMPARE(error, <=, machine_epsilon<Real>());
553 }
554 
555 TEUCHOS_UNIT_TEST(MiniTensor, Tensor4Filling)
556 {
557  Index const
558  dimension = 3;
559 
560  // Test construct with zeros
561  Tensor4<Real>
562  A(dimension, ZEROS);
563 
564  Real
565  error = norm_f_square(A);
566 
567  TEST_COMPARE(error, <=, machine_epsilon<Real>());
568 
569  // Test construct with ones
570  Tensor4<Real>
571  B(dimension, ONES);
572 
573  Index const
574  number_components = integer_power(dimension, Tensor4<Real>::order);
575 
576  error = norm_f_square(B) - number_components;
577 
578  TEST_COMPARE(error, <=, machine_epsilon<Real>());
579 
580  Tensor4<Real>
581  C(dimension, RANDOM);
582 
583  error = norm_f(C);
584 
585  TEST_COMPARE(error, >, 0.0);
586  TEST_COMPARE(error, <, number_components);
587 
588  // Test fill with random components
589  A.fill(RANDOM);
590 
591  error = norm_f(A);
592 
593  TEST_COMPARE(error, >, 0.0);
594  TEST_COMPARE(error, <, number_components);
595 
596  // Test fill with zeros
597  B.fill(ZEROS);
598 
599  error = norm_f_square(B);
600 
601  TEST_COMPARE(error, <=, machine_epsilon<Real>());
602 
603  // Test fill with ones
604  C.fill(ZEROS);
605 
606  error = norm_f_square(C) - number_components;
607 
608  TEST_COMPARE(error, <=, machine_epsilon<Real>());
609 }
610 
611 TEUCHOS_UNIT_TEST(MiniTensor, Tensor4Products)
612 {
613  Index const
614  dimension = 3;
615 
616  Tensor4<Real> const
617  A(dimension, ONES);
618 
619  Real const
620  factor = 2.0;
621 
622  Tensor4<Real> const
623  B = factor * A;
624 
625  Real
626  error = norm_f_square(B) - factor * factor * B.get_number_components();
627 
628  TEST_COMPARE(error, <=, machine_epsilon<Real>());
629 
630  Tensor4<Real> const
631  C = A * factor;
632 
633  error = norm_f_square(C) - factor * factor * C.get_number_components();
634 
635  TEST_COMPARE(error, <=, machine_epsilon<Real>());
636 }
637 
638 TEUCHOS_UNIT_TEST(MiniTensor, Initialization)
639 {
640  FieldContainer<Real> FC(3, 3);
641  FC(0, 0) = 1.0;
642  FC(0, 1) = 2.0;
643  FC(0, 2) = 3.0;
644  FC(1, 0) = 4.0;
645  FC(1, 1) = 5.0;
646  FC(1, 2) = 6.0;
647  FC(2, 0) = 7.0;
648  FC(2, 1) = 8.0;
649  FC(2, 2) = 9.0;
650 
651  Real const * dataPtr0 = &FC(0, 0);
652 
653  Index const N = 3;
654  Vector<Real> u(N, dataPtr0);
655 
656  TEST_COMPARE( u(0), ==, 1.0);
657  TEST_COMPARE( u(1), ==, 2.0);
658  TEST_COMPARE( u(2), ==, 3.0);
659 
660  Real const * dataPtr1 = &FC(1, 0);
661 
662  u = Vector<Real>(N, dataPtr1);
663 
664  TEST_COMPARE( u(0), ==, 4.0);
665  TEST_COMPARE( u(1), ==, 5.0);
666  TEST_COMPARE( u(2), ==, 6.0);
667 
668  Real const * dataPtr2 = &FC(2, 0);
669 
670  u = Vector<Real>(N, dataPtr2);
671 
672  TEST_COMPARE( u(0), ==, 7.0);
673  TEST_COMPARE( u(1), ==, 8.0);
674  TEST_COMPARE( u(2), ==, 9.0);
675 }
676 
677 TEUCHOS_UNIT_TEST(MiniTensor, VectorAddition)
678 {
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);
682 
683  TEST_COMPARE( u + v == w, !=, 0);
684 }
685 
686 TEUCHOS_UNIT_TEST(MiniTensor, VectorSubtraction)
687 {
688  Vector<Real> u(3);
689  Vector<Real> v(3);
690  u(0) = 1.0;
691  u(1) = 2.0;
692  u(2) = 3.0;
693 
694  v = u - u;
695 
696  TEST_COMPARE(norm(v), <=, machine_epsilon<Real>());
697 }
698 
699 TEUCHOS_UNIT_TEST(MiniTensor, VectorScalarMultipliaction)
700 {
701  Vector<Real> u(3);
702  Vector<Real> v(3);
703  Vector<Real> w(3);
704  u(0) = 1.0;
705  u(1) = 2.0;
706  u(2) = 3.0;
707 
708  v(0) = -2.0;
709  v(1) = -4.0;
710  v(2) = -6.0;
711 
712  w = 4.0 * u + 2.0 * v;
713 
714  TEST_COMPARE( norm(w), <=, machine_epsilon<Real>());
715 }
716 
717 TEUCHOS_UNIT_TEST(MiniTensor, TensorInstantiation)
718 {
719  FieldContainer<Real> FC(2, 3, 3);
720  FC(0, 0, 0) = 1.0;
721  FC(0, 0, 1) = 2.0;
722  FC(0, 0, 2) = 3.0;
723  FC(0, 1, 0) = 4.0;
724  FC(0, 1, 1) = 5.0;
725  FC(0, 1, 2) = 6.0;
726  FC(0, 2, 0) = 7.0;
727  FC(0, 2, 1) = 8.0;
728  FC(0, 2, 2) = 9.0;
729  FC(1, 0, 0) = 10.0;
730  FC(1, 0, 1) = 11.0;
731  FC(1, 0, 2) = 12.0;
732  FC(1, 1, 0) = 13.0;
733  FC(1, 1, 1) = 14.0;
734  FC(1, 1, 2) = 15.0;
735  FC(1, 2, 0) = 16.0;
736  FC(1, 2, 1) = 17.0;
737  FC(1, 2, 2) = 18.0;
738 
739  Real const * dataPtr0 = &FC(0, 0, 0);
740 
741  Tensor<Real> const A(3, dataPtr0);
742 
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);
752 
753  Real const * dataPtr1 = &FC(1, 0, 0);
754 
755  Tensor<Real> const B(3, dataPtr1);
756 
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);
766 }
767 
768 TEUCHOS_UNIT_TEST(MiniTensor, TensorAddition)
769 {
770  Tensor<Real> const A(3, 1.0);
771  Tensor<Real> const B(3, 2.0);
772  Tensor<Real> const C(3, 3.0);
773 
774  TEST_COMPARE( C == A + B, !=, 0);
775 }
776 
777 TEUCHOS_UNIT_TEST(MiniTensor, Inverse)
778 {
779  std::srand(std::time(NULL));
780  Index const N = double(std::rand()) / double(RAND_MAX) * 7.0 + 3.0;
781  Tensor<Real> A(N);
782  Tensor<Real> B(N);
783  Tensor<Real> C(N);
784 
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;
788  }
789  }
790 
791  B = inverse(A);
792 
793  C = A * B;
794 
795  Real const error = norm(C - eye<Real>(N)) / norm(A);
796 
797  TEST_COMPARE(error, <=, 100.0 * machine_epsilon<Real>());
798 }
799 
800 TEUCHOS_UNIT_TEST(MiniTensor, TensorManipulation)
801 {
802  Tensor<Real> A = eye<Real>(3);
803  Tensor<Real> B(3);
804  Tensor<Real> C(3);
805  Vector<Real> u(3);
806 
807  A = 2.0 * A;
808  A(1, 0) = A(0, 1) = 1.0;
809  A(2, 1) = A(1, 2) = 1.0;
810 
811  B = inverse(A);
812 
813  C = A * B;
814 
815  TEST_COMPARE(norm(C - eye<Real>(3)), <=, machine_epsilon<Real>());
816 
817  Real I1_A = I1(A);
818  Real I2_A = I2(A);
819  Real I3_A = I3(A);
820 
821  u(0) = I1_A - 6;
822  u(1) = I2_A - 10;
823  u(2) = I3_A - 4;
824 
825  Real const error = norm(u);
826 
827  TEST_COMPARE(error, <=, machine_epsilon<Real>());
828 }
829 
830 TEUCHOS_UNIT_TEST(MiniTensor, Exponential)
831 {
832  Tensor<Real> const A(1, 2, 3, 4, 5, 6, 7, 8, 9);
833 
834  Tensor<Real> const B = exp_pade(A);
835 
836  Tensor<Real> const C = exp_taylor(A);
837 
838  Tensor<Real> const D = B - C;
839 
840  Real const error = norm(D) / norm(B);
841 
842  TEST_COMPARE( error, <=, 100.0 * machine_epsilon<Real>());
843 }
844 
845 TEUCHOS_UNIT_TEST(MiniTensor, SymmetricEigen)
846 {
847  Tensor<Real> A = eye<Real>(3);
848  A(0, 1) = 0.1;
849  A(1, 0) = 0.1;
850 
851  Tensor<Real> V(3);
852  Tensor<Real> D(3);
853 
854  boost::tie(V, D) = eig_sym(A);
855 
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>());
859 }
860 
861 TEUCHOS_UNIT_TEST(MiniTensor, LeftPolarDecomposition)
862 {
863  Tensor<Real> V0(1.1, 0.2, 0.0, 0.2, 1.0, 0.0, 0.0, 0.0, 1.2);
864 
865  Tensor<Real> R0(sqrt(2) / 2, -sqrt(2) / 2, 0.0, sqrt(2) / 2, sqrt(2) / 2,
866  0.0, 0.0, 0.0, 1.0);
867 
868  Tensor<Real> F = V0 * R0;
869  Tensor<Real> V(3);
870  Tensor<Real> R(3);
871  boost::tie(V, R) = polar_left(F);
872 
873  TEST_COMPARE(norm(V-V0), <=, 10.0*machine_epsilon<Real>());
874  TEST_COMPARE(norm(R-R0), <=, machine_epsilon<Real>());
875 }
876 
877 TEUCHOS_UNIT_TEST(MiniTensor, LogRotation)
878 {
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,
881  0.0, 0.0, 0.0, 1.0);
882 
883  Tensor<Real> r = log_rotation(R);
884  Tensor<Real> r0 = log_rotation(R0);
885 
886  TEST_COMPARE(norm(r), <=, machine_epsilon<Real>());
887 
888  TEST_COMPARE( std::abs(r0(0,1) + 0.785398163397448), <=,
889  10.0*machine_epsilon<Real>());
890 
891  TEST_COMPARE( std::abs(r0(0,1) + r0(1,0)), <=, machine_epsilon<Real>());
892 
893  Real theta = std::acos(-1.0) + 10 * machine_epsilon<Real>();
894 
895  R(0, 0) = cos(theta);
896  R(1, 1) = cos(theta);
897  R(0, 1) = sin(theta);
898  R(1, 0) = -sin(theta);
899  R(2, 2) = 1.0;
900 
901  Tensor<Real> logR = log_rotation(R);
902 
903  Tensor<Real> Rref(3, 0.0);
904  Rref(0, 1) = -theta;
905  Rref(1, 0) = theta;
906 
907  TEST_COMPARE(norm(logR - Rref), <=, 100*machine_epsilon<Real>());
908 }
909 
910 TEUCHOS_UNIT_TEST(MiniTensor, BakerCampbellHausdorff)
911 {
912  Tensor<Real> F = 3.0 * identity<Real>(3);
913  Tensor<Real> V(3), R(3), logV(3), logR(3);
914 
915  boost::tie(V, R, logV) = polar_left_logV(F);
916  logR = log_rotation(R);
917 
918  Tensor<Real> f = bch(logV, logR);
919 
920  TEST_COMPARE( std::abs(f(0,0) - std::log(3.0)), <=,
921  machine_epsilon<Real>());
922 
923  Vector<Real> u(3);
924  u(0) = std::acos(-1.0) / std::sqrt(2.0);
925  u(1) = u(0);
926  u(2) = 0.0;
927 
928  Tensor<Real> R1(3, 0.0);
929  Tensor<Real> logR2(3, 0.0);
930  logR2(0, 2) = u(1);
931  logR2(1, 2) = -u(0);
932  logR2(2, 0) = -u(1);
933  logR2(2, 1) = u(0);
934  logR2(0, 1) = -u(2);
935  logR2(1, 0) = u(2);
936 
937  R1 = exp_skew_symmetric(logR2);
938  Tensor<Real> Rref = zero<Real>(3);
939  Rref(0, 1) = 1.0;
940  Rref(1, 0) = 1.0;
941  Rref(2, 2) = -1.0;
942 
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>());
946 }
947 
948 TEUCHOS_UNIT_TEST(MiniTensor, PolarLeftLog)
949 {
950  Tensor<Real> const F(3.60070151614402, 0.00545892068653966,
951  0.144580850331452, -5.73345529510674, 0.176660910549112,
952  1.39627497290058, 2.51510445213514, 0.453212159218359,
953  -1.44616077859513);
954 
955  Tensor<Real> const L(0.265620603957487, -1.066921781600734,
956  -0.089540974250415, -1.066921781600734, 0.927394431410918,
957  -0.942214085118614, -0.089540974250415, -0.942214085118613,
958  0.105672693695746);
959 
960  Tensor<Real> V(3), R(3), v(3), r(3);
961 
962  boost::tie(V, R, v) = polar_left_logV(F);
963 
964  Real const error = norm(v - L) / norm(L);
965 
966  TEST_COMPARE( error, <=, 100*machine_epsilon<Real>());
967 }
968 
969 TEUCHOS_UNIT_TEST(MiniTensor, VolumetricDeviatoric)
970 {
971  Tensor<Real> A = 3.0 * eye<Real>(3);
972 
973  TEST_COMPARE( norm(A - vol(A)), <=, 100.0*machine_epsilon<Real>());
974 
975  Tensor<Real> B = dev(A);
976 
977  A(0, 0) = 0.0;
978  A(1, 1) = 0.0;
979  A(2, 2) = 0.0;
980 
981  TEST_COMPARE( norm(A - B), <=, 100.0*machine_epsilon<Real>());
982 }
983 
984 TEUCHOS_UNIT_TEST(MiniTensor, SVD2x2)
985 {
986  Real const phi = 1.0;
987 
988  Real const psi = 2.0;
989 
990  Real const s0 = sqrt(3.0);
991 
992  Real const s1 = sqrt(2.0);
993 
994  Real const cl = cos(phi);
995 
996  Real const sl = sin(phi);
997 
998  Real const cr = cos(psi);
999 
1000  Real const sr = sin(psi);
1001 
1002  Tensor<Real> const X(cl, -sl, sl, cl);
1003 
1004  Tensor<Real> const Y(cr, -sr, sr, cr);
1005 
1006  Tensor<Real> const D(s0, 0.0, 0.0, s1);
1007 
1008  Tensor<Real> const A = X * D * transpose(Y);
1009 
1010  Tensor<Real> U(2), S(2), V(2);
1011 
1012  boost::tie(U, S, V) = svd(A);
1013 
1014  Tensor<Real> B = U * S * transpose(V);
1015 
1016  Real const error = norm(A - B) / norm(A);
1017 
1018  TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1019 }
1020 
1021 TEUCHOS_UNIT_TEST(MiniTensor, SVD3x3)
1022 {
1023  Tensor<Real> const A(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
1024 
1025  Tensor<Real> U(3), S(3), V(3);
1026 
1027  boost::tie(U, S, V) = svd(A);
1028 
1029  Tensor<Real> const B = U * S * transpose(V);
1030 
1031  Real const error = norm(A - B) / norm(A);
1032 
1033  TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1034 }
1035 
1036 TEUCHOS_UNIT_TEST(MiniTensor, SVD3x3Fad)
1037 {
1038  Tensor<Sacado::Fad::DFad<double> > A(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
1039  9.0);
1040 
1041  Tensor<Sacado::Fad::DFad<double> > U(3), S(3), V(3);
1042 
1043  boost::tie(U, S, V) = svd(A);
1044 
1045  Tensor<Sacado::Fad::DFad<double> > B = U * S * transpose(V);
1046 
1047  Sacado::Fad::DFad<double> const error = norm(B - A) / norm(A);
1048 
1049  TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1050 }
1051 
1052 TEUCHOS_UNIT_TEST(MiniTensor, SymmetricEigen2x2)
1053 {
1054  Tensor<Real> const A(2.0, 1.0, 1.0, 2.0);
1055 
1056  Tensor<Real> V(2), D(2);
1057 
1058  boost::tie(V, D) = eig_sym(A);
1059 
1060  Tensor<Real> const B = V * D * transpose(V);
1061 
1062  Real const error = norm(A - B) / norm(A);
1063 
1064  TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1065 }
1066 
1067 TEUCHOS_UNIT_TEST(MiniTensor, SymmetricEigen3x3)
1068 {
1069  Tensor<Real> const A(2.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0);
1070 
1071  Tensor<Real> V(3), D(3);
1072 
1073  boost::tie(V, D) = eig_sym(A);
1074 
1075  Tensor<Real> const B = V * D * transpose(V);
1076 
1077  Real const error = norm(A - B) / norm(A);
1078 
1079  TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1080 }
1081 
1082 TEUCHOS_UNIT_TEST(MiniTensor, Inverse4x4)
1083 {
1084  Tensor<Real> A = 2.0 * identity<Real>(4);
1085 
1086  A(0, 1) = 1.0;
1087  A(1, 0) = 1.0;
1088 
1089  A(1, 2) = 1.0;
1090  A(2, 1) = 1.0;
1091 
1092  A(2, 3) = 1.0;
1093  A(3, 2) = 1.0;
1094 
1095  Tensor<Real> const B = inverse(A);
1096 
1097  Tensor<Real> const C = A * B;
1098 
1099  Tensor<Real> const I = eye<Real>(4);
1100 
1101  Real const error = norm(C - I) / norm(A);
1102 
1103  TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1104 }
1105 
1106 TEUCHOS_UNIT_TEST(MiniTensor, Polar3x3)
1107 {
1108  Tensor<Real> A(2.0, 1.0, 0.0, 0.0, 2.0, 1.0, 0.0, 0.0, 2.0);
1109 
1110  Tensor<Real> R(3), U(3);
1111 
1112  boost::tie(R, U) = polar_right(A);
1113 
1114  Tensor<Real> X(3), D(3), Y(3);
1115 
1116  boost::tie(X, D, Y) = svd(A);
1117 
1118  Tensor<Real> B = R - X * transpose(Y) + U - Y * D * transpose(Y);
1119 
1120  Real const error = norm(B) / norm(A);
1121 
1122  TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1123 }
1124 
1125 TEUCHOS_UNIT_TEST(MiniTensor, Cholesky)
1126 {
1127  Tensor<Real> A(1.0, 1.0, 1.0, 1.0, 5.0, 3.0, 1.0, 3.0, 3.0);
1128 
1129  Tensor<Real> G(3);
1130 
1131  bool is_spd;
1132 
1133  boost::tie(G, is_spd) = cholesky(A);
1134 
1135  Tensor<Real> B(1.0, 0.0, 0.0, 1.0, 2.0, 0.0, 1.0, 1.0, 1.0);
1136 
1137  Real const error = norm(G - B) / norm(A);
1138 
1139  TEST_COMPARE(error, <=, 100.0*machine_epsilon<Real>());
1140 }
1141 
1142 TEUCHOS_UNIT_TEST(MiniTensor, MechanicsTransforms)
1143 {
1144  Tensor<Real> F(0.0, -6.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 1.0 / 3.0);
1145 
1146  Tensor<Real> sigma(0.0, 0.0, 0.0, 0.0, 50.0, 0.0, 0.0, 0.0, 0.0);
1147 
1148  Tensor<Real> P = piola(F, sigma);
1149 
1150  Real error = abs(P(1, 0) - 100.0) / 100.0;
1151 
1152  TEST_COMPARE(error, <=, machine_epsilon<Real>());
1153 
1154  sigma = piola_inverse(F, P);
1155 
1156  error = abs(sigma(1, 1) - 50.0) / 50.0;
1157 
1158  TEST_COMPARE(error, <=, machine_epsilon<Real>());
1159 
1160  Tensor<Real> E = 0.5 * (t_dot(F, F) - eye<Real>(3));
1161 
1162  Tensor<Real> e = 0.5 * (eye<Real>(3) - inverse(dot_t(F, F)));
1163 
1164  Tensor<Real> g = push_forward_covariant(F, E);
1165 
1166  error = norm(g - e) / norm(e);
1167 
1168  TEST_COMPARE(error, <=, machine_epsilon<Real>());
1169 
1170  Tensor<Real> G = pull_back_covariant(F, e);
1171 
1172  error = norm(G - E) / norm(E);
1173 
1174  TEST_COMPARE(error, <=, machine_epsilon<Real>());
1175 }
1176 
1177 } // namespace Intrepid
Header file for utility class to provide multidimensional containers.