/
symmetric_tensor.h
4039 lines (3480 loc) · 139 KB
/
symmetric_tensor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ---------------------------------------------------------------------
//
// Copyright (C) 2005 - 2021 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
#ifndef dealii_symmetric_tensor_h
#define dealii_symmetric_tensor_h
#include <deal.II/base/config.h>
#include <deal.II/base/numbers.h>
#include <deal.II/base/table_indices.h>
#include <deal.II/base/template_constraints.h>
#include <deal.II/base/tensor.h>
#include <algorithm>
#include <array>
#include <functional>
DEAL_II_NAMESPACE_OPEN
// Forward declaration
#ifndef DOXYGEN
template <int rank, int dim, typename Number = double>
class SymmetricTensor;
#endif
/**
* Return a unit symmetric tensor of rank 2, i.e., the
* $\text{dim}\times\text{dim}$ identity matrix $\mathbf I$.
*
* @relatesalso SymmetricTensor
*/
template <int dim, typename Number = double>
DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE SymmetricTensor<2, dim, Number>
unit_symmetric_tensor();
/**
* Return the tensor of rank 4 that, when multiplied by a symmetric rank 2
* tensor $\mathbf T$ returns the deviator $\text{dev}\ \mathbf T$. It is the
* operator representation of the linear deviator operator $\mathbb P$, also
* known as the volumetric projection tensor, calculated as:
* \f{align*}{
* \mathbb{P} &=\mathbb{I} -\frac{1}{\text{dim}} \mathbf I \otimes \mathbf I
* \\
* \mathcal{P}_{ijkl} &= \frac 12 \left(\delta_{ik} \delta_{jl} +
* \delta_{il} \delta_{jk} \right)
* - \frac{1}{\text{dim}} \delta_{ij} \delta_{kl}
* \f}
*
* For every tensor <tt>T</tt>, there holds the identity
* <tt>deviator<dim,Number>(T) == deviator_tensor<dim,Number>() * T</tt>,
* up to numerical round-off.
* \f[
* \text{dev}\mathbf T = \mathbb P : \mathbf T
* \f]
*
* @note The reason this operator representation is provided is to simplify
* taking derivatives of the deviatoric part of tensors:
* \f[
* \frac{\partial \text{dev}\mathbf{T}}{\partial \mathbf T} = \mathbb P.
* \f]
*
* @relatesalso SymmetricTensor
*/
template <int dim, typename Number = double>
DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE SymmetricTensor<4, dim, Number>
deviator_tensor();
/**
* Return the fourth-order symmetric identity tensor $\mathbb I$ which maps
* symmetric second-order tensors, such as $\mathbf A$, to themselves.
* \f[
* \mathbb I : \mathbf A = \mathbf A
* \f]
*
* Note that this tensor, even though it is the identity, has a somewhat funny
* form, and in particular does not only consist of zeros and ones. For
* example, for <tt>dim=2</tt>, the identity tensor has all zero entries
* except for
* \f[
* \mathcal{I}_{0000} = \mathcal{I}_{1111} = 1
* \f]
* \f[
* \mathcal{I}_{0101} = \mathcal{I}_{0110} = \mathcal{I}_{1001}
* = \mathcal{I}_{1010} = \frac 12.
* \f]
* In index notation, we can write the general form
* \f[
* \mathcal{I}_{ijkl} = \frac 12 \left( \delta_{ik} \delta_{jl} +
* \delta_{il} \delta_{jl} \right).
* \f]
* To see why this factor of $1 / 2$ is necessary, consider computing
* $\mathbf A= \mathbb I : \mathbf B$.
* For the element $A_{01}$ we have $A_{01} = \mathcal{I}_{0100} B_{00} +
* \mathcal{I}_{0111} B_{11} + \mathcal{I}_{0101} B_{01} +
* \mathcal{I}_{0110} B_{10}$. On the other hand, we need
* to have $A_{01} = B_{01}$, and symmetry implies $B_{01}=B_{10}$,
* leading to $A_{01} = (\mathcal{I}_{0101} + \mathcal{I}_{0110}) B_{01}$, or,
* again by symmetry, $\mathcal{I}_{0101} = \mathcal{I}_{0110} = \frac 12$.
* Similar considerations hold for the three-dimensional case.
*
* This issue is also explained in the introduction to step-44.
*
* @relatesalso SymmetricTensor
*/
template <int dim, typename Number = double>
DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE SymmetricTensor<4, dim, Number>
identity_tensor();
template <int dim, typename Number>
constexpr DEAL_II_ALWAYS_INLINE SymmetricTensor<2, dim, Number>
invert(const SymmetricTensor<2, dim, Number> &);
template <int dim, typename Number>
constexpr DEAL_II_ALWAYS_INLINE SymmetricTensor<4, dim, Number>
invert(const SymmetricTensor<4, dim, Number> &);
template <int dim2, typename Number>
constexpr inline DEAL_II_ALWAYS_INLINE Number
trace(const SymmetricTensor<2, dim2, Number> &);
template <int dim, typename Number>
constexpr inline DEAL_II_ALWAYS_INLINE SymmetricTensor<2, dim, Number>
deviator(const SymmetricTensor<2, dim, Number> &);
template <int dim, typename Number>
constexpr inline DEAL_II_ALWAYS_INLINE Number
determinant(const SymmetricTensor<2, dim, Number> &);
namespace internal
{
// Workaround: The following 4 overloads are necessary to be able to
// compile the library with Apple Clang 8 and older. We should remove
// these overloads again when we bump the minimal required version to
// something later than clang-3.6 / Apple Clang 6.3.
template <int rank, int dim, typename T, typename U>
struct ProductTypeImpl<SymmetricTensor<rank, dim, T>, std::complex<U>>
{
using type =
SymmetricTensor<rank,
dim,
std::complex<typename ProductType<T, U>::type>>;
};
template <int rank, int dim, typename T, typename U>
struct ProductTypeImpl<SymmetricTensor<rank, dim, std::complex<T>>,
std::complex<U>>
{
using type =
SymmetricTensor<rank,
dim,
std::complex<typename ProductType<T, U>::type>>;
};
template <typename T, int rank, int dim, typename U>
struct ProductTypeImpl<std::complex<T>, SymmetricTensor<rank, dim, U>>
{
using type =
SymmetricTensor<rank,
dim,
std::complex<typename ProductType<T, U>::type>>;
};
template <int rank, int dim, typename T, typename U>
struct ProductTypeImpl<std::complex<T>,
SymmetricTensor<rank, dim, std::complex<U>>>
{
using type =
SymmetricTensor<rank,
dim,
std::complex<typename ProductType<T, U>::type>>;
};
// end workaround
/**
* A namespace for functions and classes that are internal to how the
* SymmetricTensor class (and its associate functions) works.
*/
namespace SymmetricTensorImplementation
{
/**
* Compute the inverse of a symmetric tensor of a
* generic @p rank, @p dim and @p Number type.
*/
template <int rank, int dim, typename Number>
struct Inverse;
} // namespace SymmetricTensorImplementation
/**
* A namespace for classes that are internal to how the SymmetricTensor
* class works.
*/
namespace SymmetricTensorAccessors
{
/**
* Create a TableIndices<2> object where the first entries up to
* <tt>position-1</tt> are taken from previous_indices, and new_index is
* put at position <tt>position</tt>. The remaining indices remain in
* invalid state.
*/
DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE TableIndices<2>
merge(const TableIndices<2> &previous_indices,
const unsigned int new_index,
const unsigned int position)
{
AssertIndexRange(position, 2);
if (position == 0)
return {new_index, numbers::invalid_unsigned_int};
else
return {previous_indices[0], new_index};
}
/**
* Create a TableIndices<4> object where the first entries up to
* <tt>position-1</tt> are taken from previous_indices, and new_index is
* put at position <tt>position</tt>. The remaining indices remain in
* invalid state.
*/
DEAL_II_CONSTEXPR inline DEAL_II_ALWAYS_INLINE TableIndices<4>
merge(const TableIndices<4> &previous_indices,
const unsigned int new_index,
const unsigned int position)
{
AssertIndexRange(position, 4);
switch (position)
{
case 0:
return {new_index,
numbers::invalid_unsigned_int,
numbers::invalid_unsigned_int,
numbers::invalid_unsigned_int};
case 1:
return {previous_indices[0],
new_index,
numbers::invalid_unsigned_int,
numbers::invalid_unsigned_int};
case 2:
return {previous_indices[0],
previous_indices[1],
new_index,
numbers::invalid_unsigned_int};
case 3:
return {previous_indices[0],
previous_indices[1],
previous_indices[2],
new_index};
default:
Assert(false, ExcInternalError());
return {};
}
}
/**
* Typedef template magic denoting the result of a double contraction
* between two tensors or ranks rank1 and rank2. In general, this is a
* tensor of rank <tt>rank1+rank2-4</tt>, but if this is zero it is a
* single scalar Number. For this case, we have a specialization.
*/
template <int rank1,
int rank2,
int dim,
typename Number,
typename OtherNumber = Number>
struct double_contraction_result
{
using value_type = typename ProductType<Number, OtherNumber>::type;
using type =
::dealii::SymmetricTensor<rank1 + rank2 - 4, dim, value_type>;
};
/**
* Typedef template magic denoting the result of a double contraction
* between two tensors or ranks rank1 and rank2. In general, this is a
* tensor of rank <tt>rank1+rank2-4</tt>, but if this is zero it is a
* single scalar Number. For this case, we have a specialization.
*/
template <int dim, typename Number, typename OtherNumber>
struct double_contraction_result<2, 2, dim, Number, OtherNumber>
{
using type = typename ProductType<Number, OtherNumber>::type;
};
/**
* Declaration of alias for the type of data structures which are used
* to store symmetric tensors. For example, for rank-2 symmetric tensors,
* we use a flat vector to store all the elements. On the other hand,
* symmetric rank-4 tensors are mappings from symmetric rank-2 tensors
* into symmetric rank-2 tensors, so they can be represented as matrices,
* etc.
*
* This information is probably of little interest to all except the
* accessor classes that need it. In particular, you shouldn't make any
* assumptions about the storage format in your application programs.
*/
template <int rank, int dim, typename Number>
struct StorageType;
/**
* Specialization of StorageType for rank-2 tensors.
*/
template <int dim, typename Number>
struct StorageType<2, dim, Number>
{
/**
* Number of independent components of a symmetric tensor of rank 2. We
* store only the upper right half of it.
*/
static const unsigned int n_independent_components =
(dim * dim + dim) / 2;
/**
* Declare the type in which we actually store the data.
*/
using base_tensor_type = Tensor<1, n_independent_components, Number>;
};
/**
* Specialization of StorageType for rank-4 tensors.
*/
template <int dim, typename Number>
struct StorageType<4, dim, Number>
{
/**
* Number of independent components of a symmetric tensor of rank 2.
* Since rank-4 tensors are mappings between such objects, we need this
* information.
*/
static const unsigned int n_rank2_components = (dim * dim + dim) / 2;
/**
* Number of independent components of a symmetric tensor of rank 4.
*/
static const unsigned int n_independent_components =
(n_rank2_components *
StorageType<2, dim, Number>::n_independent_components);
/**
* Declare the type in which we actually store the data. Symmetric
* rank-4 tensors are mappings between symmetric rank-2 tensors, so we
* can represent the data as a matrix if we represent the rank-2 tensors
* as vectors.
*/
using base_tensor_type = Tensor<2, n_rank2_components, Number>;
};
/**
* Switch type to select a tensor of rank 2 and dimension <tt>dim</tt>,
* switching on whether the tensor should be constant or not.
*/
template <int rank, int dim, bool constness, typename Number>
struct AccessorTypes;
/**
* Switch type to select a tensor of rank 2 and dimension <tt>dim</tt>,
* switching on whether the tensor should be constant or not.
*
* Specialization for constant tensors.
*/
template <int rank, int dim, typename Number>
struct AccessorTypes<rank, dim, true, Number>
{
using tensor_type = const ::dealii::SymmetricTensor<rank, dim, Number>;
using reference = Number;
};
/**
* Switch type to select a tensor of rank 2 and dimension <tt>dim</tt>,
* switching on whether the tensor should be constant or not.
*
* Specialization for non-constant tensors.
*/
template <int rank, int dim, typename Number>
struct AccessorTypes<rank, dim, false, Number>
{
using tensor_type = ::dealii::SymmetricTensor<rank, dim, Number>;
using reference = Number &;
};
/**
* @internal
*
* Class that acts as accessor to elements of type SymmetricTensor. The
* template parameter <tt>constness</tt> may be either true or false, and
* indicates whether the objects worked on are constant or not (i.e. write
* access is only allowed if the value is false).
*
* Since with <tt>N</tt> indices, the effect of applying
* <tt>operator[]</tt> is getting access to something with <tt>N-1</tt>
* indices, we have to implement these accessor classes recursively, with
* stopping when we have only one index left. For the latter case, a
* specialization of this class is declared below, where calling
* <tt>operator[]</tt> gives you access to the objects actually stored by
* the tensor; the tensor class also makes sure that only those elements
* are actually accessed which we actually store, i.e. it reorders indices
* if necessary. The template parameter <tt>P</tt> indicates how many
* remaining indices there are. For a rank-2 tensor, <tt>P</tt> may be
* two, and when using <tt>operator[]</tt>, an object with <tt>P=1</tt>
* emerges.
*
* As stated for the entire namespace, you will not usually have to do
* with these classes directly, and should not try to use their interface
* directly as it may change without notice. In fact, since the
* constructors are made private, you will not even be able to generate
* objects of this class, as they are only thought as temporaries for
* access to elements of the table class, not for passing them around as
* arguments of functions, etc.
*
* This class is an adaptation of a similar class used for the Table
* class.
*/
template <int rank, int dim, bool constness, int P, typename Number>
class Accessor
{
public:
/**
* Import two alias from the switch class above.
*/
using reference =
typename AccessorTypes<rank, dim, constness, Number>::reference;
using tensor_type =
typename AccessorTypes<rank, dim, constness, Number>::tensor_type;
private:
/**
* Constructor. Take a reference to the tensor object which we will
* access.
*
* The second argument denotes the values of previous indices into the
* tensor. For example, for a rank-4 tensor, if P=2, then we will
* already have had two successive element selections (e.g. through
* <tt>tensor[1][2]</tt>), and the two index values have to be stored
* somewhere. This class therefore only makes use of the first rank-P
* elements of this array, but passes it on to the next level with P-1
* which fills the next entry, and so on.
*
* The constructor is made private in order to prevent you having such
* objects around. The only way to create such objects is via the
* <tt>Table</tt> class, which only generates them as temporary objects.
* This guarantees that the accessor objects go out of scope earlier
* than the mother object, avoid problems with data consistency.
*/
constexpr Accessor(tensor_type & tensor,
const TableIndices<rank> &previous_indices);
/**
* Copy constructor.
*/
constexpr DEAL_II_ALWAYS_INLINE
Accessor(const Accessor &) = default;
public:
/**
* Index operator.
*/
constexpr Accessor<rank, dim, constness, P - 1, Number>
operator[](const unsigned int i);
/**
* Index operator.
*/
constexpr Accessor<rank, dim, constness, P - 1, Number>
operator[](const unsigned int i) const;
private:
/**
* Store the data given to the constructor.
*/
tensor_type & tensor;
const TableIndices<rank> previous_indices;
// Declare some other classes as friends. Make sure to work around bugs
// in some compilers:
template <int, int, typename>
friend class dealii::SymmetricTensor;
template <int, int, bool, int, typename>
friend class Accessor;
friend class ::dealii::SymmetricTensor<rank, dim, Number>;
friend class Accessor<rank, dim, constness, P + 1, Number>;
};
/**
* @internal Accessor class for SymmetricTensor. This is the
* specialization for the last index, which actually allows access to the
* elements of the table, rather than recursively returning access objects
* for further subsets. The same holds for this specialization as for the
* general template; see there for more information.
*/
template <int rank, int dim, bool constness, typename Number>
class Accessor<rank, dim, constness, 1, Number>
{
public:
/**
* Import two alias from the switch class above.
*/
using reference =
typename AccessorTypes<rank, dim, constness, Number>::reference;
using tensor_type =
typename AccessorTypes<rank, dim, constness, Number>::tensor_type;
private:
/**
* Constructor. Take a reference to the tensor object which we will
* access.
*
* The second argument denotes the values of previous indices into the
* tensor. For example, for a rank-4 tensor, if P=2, then we will
* already have had two successive element selections (e.g. through
* <tt>tensor[1][2]</tt>), and the two index values have to be stored
* somewhere. This class therefore only makes use of the first rank-P
* elements of this array, but passes it on to the next level with P-1
* which fills the next entry, and so on.
*
* For this particular specialization, i.e. for P==1, all but the last
* index are already filled.
*
* The constructor is made private in order to prevent you having such
* objects around. The only way to create such objects is via the
* <tt>Table</tt> class, which only generates them as temporary objects.
* This guarantees that the accessor objects go out of scope earlier
* than the mother object, avoid problems with data consistency.
*/
constexpr Accessor(tensor_type & tensor,
const TableIndices<rank> &previous_indices);
/**
* Copy constructor.
*/
constexpr DEAL_II_ALWAYS_INLINE
Accessor(const Accessor &) = default;
public:
/**
* Index operator.
*/
constexpr reference operator[](const unsigned int);
/**
* Index operator.
*/
constexpr reference operator[](const unsigned int) const;
private:
/**
* Store the data given to the constructor.
*/
tensor_type & tensor;
const TableIndices<rank> previous_indices;
// Declare some other classes as friends. Make sure to work around bugs
// in some compilers:
template <int, int, typename>
friend class dealii::SymmetricTensor;
template <int, int, bool, int, typename>
friend class SymmetricTensorAccessors::Accessor;
friend class ::dealii::SymmetricTensor<rank, dim, Number>;
friend class SymmetricTensorAccessors::
Accessor<rank, dim, constness, 2, Number>;
};
} // namespace SymmetricTensorAccessors
} // namespace internal
/**
* Provide a class that stores symmetric tensors of rank 2,4,... efficiently,
* i.e. only store those off-diagonal elements of the full tensor that are not
* redundant. For example, for symmetric $2\times 2$ tensors, this would be the
* elements 11, 22, and 12, while the element 21 is equal to the 12 element.
* Within this documentation, second order symmetric tensors are denoted as
* bold-faced upper-case Latin letters such as $\mathbf A, \mathbf B, \dots$
* or bold-faced Greek letters such as $\boldsymbol{\varepsilon}$,
* $\boldsymbol{\sigma}$. The Cartesian coordinates of a second-order tensor
* such as $\mathbf A$ are represented as $A_{ij}$ where $i,j$ are indices
* ranging from 0 to <tt>dim-1</tt>.
*
* Using this class for symmetric tensors of rank 2 has advantages over
* matrices in many cases since the dimension is known to the compiler as well
* as the location of the data. It is therefore possible to produce far more
* efficient code than for matrices with runtime-dependent dimension. It is
* also more efficient than using the more general <tt>Tensor</tt> class,
* since fewer elements are stored, and the class automatically makes sure that
* the tensor represents a symmetric object.
*
* For tensors of higher rank, the savings in storage are even higher. For
* example for the $3 \times 3 \times 3 \times 3$ tensors of rank 4, only 36
* instead of the full 81 entries have to be stored. These rank 4 tensors are
* denoted by blackboard-style upper-case Latin letters such as $\mathbb A$
* with components $\mathcal{A}_{ijkl}$.
*
* While the definition of a symmetric rank-2 tensor is obvious, tensors of
* rank 4 are considered symmetric if they are operators mapping symmetric
* rank-2 tensors onto symmetric rank-2 tensors. This so-called minor symmetry
* of the rank 4 tensor requires that for every set of four indices
* $i, j, k, l$, the identity $\mathcal{C}_{ijkl} = \mathcal{C}_{jikl} =
* \mathcal{C}_{ijlk}$ holds. However, it does not imply the relation
* $\mathcal{C}_{ijkl} = \mathcal{C}_{klij}$.
* Consequently, symmetric tensors of rank 4 as understood here are only
* tensors that map symmetric tensors onto symmetric tensors, but they do not
* necessarily induce a symmetric scalar product $\mathbf A : \mathbb C :
* \mathbf B = \mathbf B : \mathbb C : \mathbf A$ or even
* a positive (semi-)definite form $\mathbf A : \mathbb C : \mathbf A$, where
* $\mathbf A, \mathbf B$ are symmetric rank-2 tensors and the colon indicates
* the common double-index contraction that acts as a scalar product for
* symmetric tensors.
*
* Symmetric tensors are most often used in structural and fluid
* mechanics, where strains and stresses are usually symmetric
* tensors, and the stress-strain relationship is given by a symmetric
* rank-4 tensor.
*
* @note Symmetric tensors only exist with even numbers of indices. In
* other words, the only objects that you can use are
* <tt>SymmetricTensor<2,dim></tt>, <tt>SymmetricTensor<4,dim></tt>, etc, but
* <tt>SymmetricTensor<1,dim></tt> and <tt>SymmetricTensor<3,dim></tt> do not
* exist and their use will most likely lead to compiler errors.
*
*
* <h3>Accessing elements</h3>
*
* The elements of a tensor $\mathbb C$ can be accessed using the bracket
* operator, i.e. for a tensor of rank 4, <tt>C[0][1][0][1]</tt> accesses the
* element $\mathcal{C}_{0101}$. This access can be used for both reading
* and writing (if the tensor is non-constant at least). You may also perform
* other operations on it, although that may lead to confusing situations
* because several elements of the tensor are stored at the same location. For
* example, for a rank-2 tensor that is assumed to be zero at the beginning,
* writing <tt>A[0][1]+=1; A[1][0]+=1;</tt> will lead to the same element
* being increased by one <em>twice</em>, because even though the accesses use
* different indices, the elements that are accessed are symmetric and
* therefore stored at the same location. It may therefore be useful in
* application programs to restrict operations on individual elements to
* simple reads or writes.
*
* @ingroup geomprimitives
*/
template <int rank_, int dim, typename Number>
class SymmetricTensor
{
public:
static_assert(rank_ % 2 == 0, "A SymmetricTensor must have even rank!");
/**
* Provide a way to get the dimension of an object without explicit
* knowledge of it's data type. Implementation is this way instead of
* providing a function <tt>dimension()</tt> because now it is possible to
* get the dimension at compile time without the expansion and preevaluation
* of an inlined function; the compiler may therefore produce more efficient
* code and you may use this value to declare other data types.
*/
static const unsigned int dimension = dim;
/**
* Publish the rank of this tensor to the outside world.
*/
static const unsigned int rank = rank_;
/**
* An integer denoting the number of independent components that fully
* describe a symmetric tensor. In $d$ space dimensions, this number equals
* $\frac 12 (d^2+d)$ for symmetric tensors of rank 2.
*/
static constexpr unsigned int n_independent_components =
internal::SymmetricTensorAccessors::StorageType<rank_, dim, Number>::
n_independent_components;
/**
* Default constructor. Creates a tensor with all entries equal to zero.
*/
constexpr DEAL_II_ALWAYS_INLINE
SymmetricTensor() = default;
/**
* Constructor. Generate a symmetric tensor from a general one. Assumes that
* @p t is already symmetric, and in debug mode this is in fact checked.
* Note that no provision is made to assure that the tensor is symmetric
* only up to round-off error: if the incoming tensor is not exactly
* symmetric, then an exception is thrown. If you know that incoming tensor
* is symmetric only up to round-off, then you may want to call the
* <tt>symmetrize()</tt> function first. If you aren't sure, it is good
* practice to check before calling <tt>symmetrize()</tt>.
*
* Because we check for symmetry via a non-constexpr function call, you will
* have to use the symmetrize() function in constexpr contexts instead.
*/
template <typename OtherNumber>
explicit SymmetricTensor(const Tensor<2, dim, OtherNumber> &t);
/**
* A constructor that creates a symmetric tensor from an array holding its
* independent elements. Using this constructor assumes that the caller
* knows the order in which elements are stored in symmetric tensors; its
* use is therefore discouraged, but if you think you want to use it anyway
* you can query the order of elements using the unrolled_index() function.
*
* This constructor is currently only implemented for symmetric tensors of
* rank 2.
*
* The size of the array passed is equal to
* SymmetricTensor<rank_,dim>::n_independent_components; the reason for using
* the object from the internal namespace is to work around bugs in some
* older compilers.
*/
constexpr SymmetricTensor(const Number (&array)[n_independent_components]);
/**
* Copy constructor from tensors with different underlying scalar type. This
* obviously requires that the @p OtherNumber type is convertible to @p
* Number.
*/
template <typename OtherNumber>
constexpr explicit SymmetricTensor(
const SymmetricTensor<rank_, dim, OtherNumber> &initializer);
/**
* Return a pointer to the first element of the underlying storage.
*/
Number *
begin_raw();
/**
* Return a const pointer to the first element of the underlying storage.
*/
const Number *
begin_raw() const;
/**
* Return a pointer to the element past the end of the underlying storage.
*/
Number *
end_raw();
/**
* Return a const pointer to the element past the end of the underlying
* storage.
*/
const Number *
end_raw() const;
/**
* Assignment operator from symmetric tensors with different underlying scalar
* type.
* This obviously requires that the @p OtherNumber type is convertible to
* @p Number.
*/
template <typename OtherNumber>
constexpr SymmetricTensor &
operator=(const SymmetricTensor<rank_, dim, OtherNumber> &rhs);
/**
* This operator assigns a scalar to a tensor. To avoid confusion with what
* exactly it means to assign a scalar value to a tensor, zero is the only
* value allowed for <tt>d</tt>, allowing the intuitive notation
* $\mathbf A = 0$ to reset all elements of the tensor to zero.
*/
constexpr SymmetricTensor &
operator=(const Number &d);
/**
* Convert the present symmetric tensor into a full tensor with the same
* elements, but using the different storage scheme of full tensors.
*/
constexpr operator Tensor<rank_, dim, Number>() const;
/**
* Test for equality of two tensors.
*/
constexpr bool
operator==(const SymmetricTensor &) const;
/**
* Test for inequality of two tensors.
*/
constexpr bool
operator!=(const SymmetricTensor &) const;
/**
* Add another tensor.
*/
template <typename OtherNumber>
constexpr SymmetricTensor &
operator+=(const SymmetricTensor<rank_, dim, OtherNumber> &);
/**
* Subtract another tensor.
*/
template <typename OtherNumber>
constexpr SymmetricTensor &
operator-=(const SymmetricTensor<rank_, dim, OtherNumber> &);
/**
* Scale the tensor by <tt>factor</tt>, i.e. multiply all components by
* <tt>factor</tt>.
*/
template <typename OtherNumber>
constexpr SymmetricTensor &
operator*=(const OtherNumber &factor);
/**
* Scale the tensor by <tt>1/factor</tt>.
*/
template <typename OtherNumber>
constexpr SymmetricTensor &
operator/=(const OtherNumber &factor);
/**
* Unary minus operator. Negate all entries of a tensor.
*/
constexpr SymmetricTensor
operator-() const;
/**
* Double contraction product between the present symmetric tensor and a
* tensor of rank 2. For example, if the present object is the symmetric
* rank-2 tensor $\mathbf{A}$ and it is multiplied by another symmetric
* rank-2 tensor $\mathbf{B}$, then the result is the scalar-product double
* contraction $\mathbf A : \mathbf B = \sum_{i,j} A_{ij} B_{ij}$.
* In this case, the return value evaluates to a single
* scalar. While it is possible to define other scalar products (and
* associated induced norms), this one seems to be the most appropriate one.
*
* If the present object is a rank-4 tensor such as $\mathbb A$, then the
* result is a rank-2 tensor $\mathbf C = \mathbb A : \mathbf B$, i.e.,
* the operation contracts over the last two indices of the present object
* and the indices of the argument, and the result is a tensor of rank 2
* ($C_{ij} = \sum_{k,l} \mathcal{A}_{ijkl} B_{kl}$).
*
* Note that the multiplication operator for symmetric tensors is defined to
* be a double contraction over two indices, while it is defined as a single
* contraction over only one index for regular <tt>Tensor</tt> objects. For
* symmetric tensors it therefore acts in a way that is commonly denoted by
* a "colon multiplication" in the mathematical literature.
*
* There are global functions <tt>double_contract</tt> that do the same work
* as this operator, but rather than returning the result as a return value,
* they write it into the first argument to the function.
*/
template <typename OtherNumber>
DEAL_II_CONSTEXPR typename internal::SymmetricTensorAccessors::
double_contraction_result<rank_, 2, dim, Number, OtherNumber>::type
operator*(const SymmetricTensor<2, dim, OtherNumber> &s) const;
/**
* Contraction over two indices of the present object with the rank-4
* symmetric tensor given as argument.
*/
template <typename OtherNumber>
DEAL_II_CONSTEXPR typename internal::SymmetricTensorAccessors::
double_contraction_result<rank_, 4, dim, Number, OtherNumber>::type
operator*(const SymmetricTensor<4, dim, OtherNumber> &s) const;
/**
* Return a read-write reference to the indicated element.
*/
constexpr Number &
operator()(const TableIndices<rank_> &indices);
/**
* Return a @p const reference to the value referred to by the argument.
*/
constexpr const Number &
operator()(const TableIndices<rank_> &indices) const;
/**
* Access the elements of a row of this symmetric tensor. This function is
* called for constant tensors.
*/
constexpr internal::SymmetricTensorAccessors::
Accessor<rank_, dim, true, rank_ - 1, Number>
operator[](const unsigned int row) const;
/**
* Access the elements of a row of this symmetric tensor. This function is
* called for non-constant tensors.
*/
constexpr internal::SymmetricTensorAccessors::
Accessor<rank_, dim, false, rank_ - 1, Number>
operator[](const unsigned int row);
/**
* Return a @p const reference to the value referred to by the argument.
*
* Exactly the same as operator().
*/
constexpr const Number &operator[](const TableIndices<rank_> &indices) const;
/**
* Return a read-write reference to the indicated element.
*
* Exactly the same as operator().
*/
constexpr Number &operator[](const TableIndices<rank_> &indices);
/**
* Access to an element according to unrolled index. The function
* <tt>s.access_raw_entry(unrolled_index)</tt> does the same as
* <tt>s[s.unrolled_to_component_indices(unrolled_index)]</tt>, but more
* efficiently.
*/
constexpr const Number &
access_raw_entry(const unsigned int unrolled_index) const;
/**
* Access to an element according to unrolled index. The function
* <tt>s.access_raw_entry(unrolled_index)</tt> does the same as
* <tt>s[s.unrolled_to_component_indices(unrolled_index)]</tt>, but more
* efficiently.
*/
constexpr Number &
access_raw_entry(const unsigned int unrolled_index);
/**
* Return the Frobenius-norm of a tensor, i.e. the square root of the sum of
* squares of all entries. This norm is induced by the scalar product
* defined above for two symmetric tensors. Note that it includes <i>all</i>
* entries of the tensor, counting symmetry, not only the unique ones (for
* example, for rank-2 tensors, this norm includes adding up the squares of
* upper right as well as lower left entries, not just one of them, although
* they are equal for symmetric tensors).
*/
constexpr typename numbers::NumberTraits<Number>::real_type
norm() const;
/**
* Tensor objects can be unrolled by simply pasting all elements into one
* long vector, but for this an order of elements has to be defined. For
* symmetric tensors, this function returns which index within the range
* <code>[0,n_independent_components)</code> the given entry in a symmetric
* tensor has.
*/
static constexpr unsigned int
component_to_unrolled_index(const TableIndices<rank_> &indices);
/**
* The opposite of the previous function: given an index $i$ in the unrolled
* form of the tensor, return what set of indices $(k,l)$ (for rank-2
* tensors) or $(k,l,m,n)$ (for rank-4 tensors) corresponds to it.
*/
static constexpr TableIndices<rank_>
unrolled_to_component_indices(const unsigned int i);
/**
* Reset all values to zero.
*
* Note that this is partly inconsistent with the semantics of the @p
* clear() member functions of the standard library containers and of
* several other classes within deal.II, which not only reset the values of
* stored elements to zero, but release all memory and return the object
* into a virginial state. However, since the size of objects of the present
* type is determined by its template parameters, resizing is not an option,
* and indeed the state where all elements have a zero value is the state
* right after construction of such an object.
*/
constexpr void
clear();
/**
* Determine an estimate for the memory consumption (in bytes) of this
* object.
*/
static constexpr std::size_t
memory_consumption();
/**
* Read or write the data of this object to or from a stream for the purpose
* of serialization using the [BOOST serialization
* library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html).
*/
template <class Archive>
void