Debian Codegraph @

include/deal.II/physics/notation.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2017 - 2024 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_physics_notation_h
16 #define dealii_physics_notation_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/exceptions.h>
21 #include <deal.II/base/numbers.h>
22 #include <deal.II/base/symmetric_tensor.h>
23 #include <deal.II/base/tensor.h>
24
25 #include <deal.II/lac/full_matrix.h>
26 #include <deal.II/lac/vector.h>
27
28 #include <type_traits>
29
30 DEAL_II_NAMESPACE_OPEN
31
32
33 namespace Physics
34 {
35 namespace Notation
36 {
37 /**
38 * @brief A namespace with functions that assist in the conversion of
39 * vectors and tensors to and from a compressed format using Kelvin notation
40 * and weighting.
41 *
42 * Both Kelvin and Voigt notation adopt the same indexing convention.
43 * With specific reference to the spatial dimension 3 case, for
44 * a rank-2 symmetric tensor $\mathbf{S}$ we enumerate its tensor
45 * components
46 * @f[
47 * \mathbf{S} \dealcoloneq \left[ \begin{array}{ccc}
48 * S_{00} & S_{01} & S_{02} \\
49 * S_{10} = S_{01} & S_{11} & S_{12} \\
50 * S_{20} = S_{02} & S_{21} = S_{12} & S_{22}
51 * \end{array} \right]
52 * \quad \Rightarrow \quad
53 * \left[ \begin{array}{ccc}
54 * n = 0 & n = 5 & n = 4 \\
55 * sym & n = 1 & n = 3 \\
56 * sym & sym & n = 2
57 * \end{array} \right] ,
58 * @f]
59 * where $n$ denotes the Kelvin index for the tensor component,
60 * while for a general rank-2 tensor $\mathbf{T}$
61 * @f[
62 * \mathbf{T} \dealcoloneq \left[ \begin{array}{ccc}
63 * T_{00} & T_{01} & T_{02} \\
64 * T_{10} & T_{11} & T_{12} \\
65 * T_{20} & T_{21} & T_{22}
66 * \end{array}\right]
67 * \quad \Rightarrow \quad
68 * \left[ \begin{array}{ccc}
69 * n = 0 & n = 5 & n = 4 \\
70 * n = 6 & n = 1 & n = 3 \\
71 * n = 7 & n = 8 & n = 2
72 * \end{array}\right] ,
73 * @f]
74 * and for a rank-1 tensor $\mathbf{v}$
75 * @f[
76 * \mathbf{v} \dealcoloneq \left[ \begin{array}{c}
77 * v_{0} \\ v_{1} \\ v_{2}
78 * \end{array}\right]
79 * \quad \Rightarrow \quad
80 * \left[ \begin{array}{c}
81 * n = 0 \\ n = 1 \\ n = 2
82 * \end{array}\right] .
83 * @f]
84 * To summarize, the relationship between tensor and Kelvin indices for both
85 * the three-dimensional case and the analogously discerned two-dimensional
86 * case outlined in the following table:
87 * <table>
88 * <tr>
89 * <th align="center"> Dimension 2 </th>
90 * <th align="center"> Dimension 3 </th>
91 * </tr>
92 * <tr>
93 * <td align="middle">
94 * <table>
95 * <tr>
96 * <th>Tensor index pairs</th>
97 * <th>Kelvin index</th>
98 * </tr>
99 * <tr>
100 * <td align="center">00</td>
101 * <td align="center">0</td>
102 * </tr>
103 * <tr>
104 * <td align="center">11</td>
105 * <td align="center">1</td>
106 * </tr>
107 * <tr>
108 * <td align="center">12</td>
109 * <td align="center">2</td>
110 * </tr>
111 * <tr>
112 * <td align="center">21</td>
113 * <td align="center">3</td>
114 * </tr>
115 * </table>
116 * </td>
117 * <td align="middle">
118 * <table>
119 * <tr>
120 * <th>Tensor index pairs</th>
121 * <th>Kelvin index</th>
122 * </tr>
123 * <tr>
124 * <td align="center">00</td>
125 * <td align="center">0</td>
126 * </tr>
127 * <tr>
128 * <td align="center">11</td>
129 * <td align="center">1</td>
130 * </tr>
131 * <tr>
132 * <td align="center">22</td>
133 * <td align="center">2</td>
134 * </tr>
135 * <tr>
136 * <td align="center">12</td>
137 * <td align="center">3</td>
138 * </tr>
139 * <tr>
140 * <td align="center">02</td>
141 * <td align="center">4</td>
142 * </tr>
143 * <tr>
144 * <td align="center">01</td>
145 * <td align="center">5</td>
146 * </tr>
147 * <tr>
148 * <td align="center">10</td>
149 * <td align="center">6</td>
150 * </tr>
151 * <tr>
152 * <td align="center">20</td>
153 * <td align="center">7</td>
154 * </tr>
155 * <tr>
156 * <td align="center">21</td>
157 * <td align="center">8</td>
158 * </tr>
159 * </table>
160 * </td>
161 * </tr>
162 * </table>
163 *
164 * To illustrate the purpose of this notation, consider the rank-2 symmetric
165 * tensors $\mathbf{S}$ and $\mathbf{E}$ that are related to one another by
166 * $\mathbf{S} = \cal{C} : \mathbf{E}$, where the operator $\cal{C}$ is a
167 * fourth-order symmetric tensor. As opposed to the commonly used Voigt
168 * notation, Kelvin (or Mandel) notation keeps the same definition of the
169 * inner product $\mathbf{S} : \mathbf{E}$ when both $\mathbf{S}$ and
170 * $\mathbf{E}$ are symmetric. In general, the inner product of all
171 * symmetric and general tensors remain the same regardless of the notation
172 * with which it is represented.
173 *
174 * To achieve these two properties, namely that
175 * @f[
176 * \mathbf{S} = \cal{C} : \mathbf{E}
177 * \quad \Rightarrow \quad
178 * \tilde{\mathbf{S}} = \tilde{\cal{C}} \; \tilde{\mathbf{E}}
179 * @f]
180 * and
181 * @f[
182 * \mathbf{S} : \mathbf{E}
183 * \, \equiv \,
184 * \tilde{\mathbf{S}} \cdot \tilde{\mathbf{E}} ,
185 * @f]
186 * it holds that the Kelvin-condensed equivalents of the previously defined
187 * symmetric tensors, indicated by the $\tilde{\left(\bullet\right)}$, must
188 * be defined as
189 * @f[
190 * \tilde{\mathbf{S}}
191 * = \left[ \begin{array}{c}
192 * S_{00} \\ S_{11} \\ S_{22} \\ \sqrt{2} S_{12} \\ \sqrt{2} S_{02} \\
193 * \sqrt{2} S_{01} \end{array}\right] \quad \text{and} \quad
194 * \tilde{\mathbf{E}}
195 * = \left[ \begin{array}{c}
196 * E_{00} \\ E_{11} \\ E_{22} \\ \sqrt{2} E_{12} \\ \sqrt{2} E_{02} \\
197 * \sqrt{2} E_{01} \end{array}\right] .
198 * @f]
199 * The corresponding and consistent condensed fourth-order symmetric tensor
200 * is
201 * @f[
202 * \tilde{\cal{C}}
203 * = \left[ \begin{array}{cccccc}
204 * \tilde{\cal{C}}_{00} & \tilde{\cal{C}}_{01} & \tilde{\cal{C}}_{02} &
205 * \tilde{\cal{C}}_{03} & \tilde{\cal{C}}_{04} & \tilde{\cal{C}}_{05} \\
206 * \tilde{\cal{C}}_{10} & \tilde{\cal{C}}_{11} & \tilde{\cal{C}}_{12} &
207 * \tilde{\cal{C}}_{13} & \tilde{\cal{C}}_{14} & \tilde{\cal{C}}_{15} \\
208 * \tilde{\cal{C}}_{20} & \tilde{\cal{C}}_{21} & \tilde{\cal{C}}_{22} &
209 * \tilde{\cal{C}}_{23} & \tilde{\cal{C}}_{24} & \tilde{\cal{C}}_{25} \\
210 * \tilde{\cal{C}}_{30} & \tilde{\cal{C}}_{31} & \tilde{\cal{C}}_{32} &
211 * \tilde{\cal{C}}_{33} & \tilde{\cal{C}}_{34} & \tilde{\cal{C}}_{35} \\
212 * \tilde{\cal{C}}_{40} & \tilde{\cal{C}}_{41} & \tilde{\cal{C}}_{42} &
213 * \tilde{\cal{C}}_{43} & \tilde{\cal{C}}_{44} & \tilde{\cal{C}}_{45} \\
214 * \tilde{\cal{C}}_{50} & \tilde{\cal{C}}_{51} & \tilde{\cal{C}}_{52} &
215 * \tilde{\cal{C}}_{53} & \tilde{\cal{C}}_{54} & \tilde{\cal{C}}_{55}
216 * \end{array}\right]
217 * \equiv
218 * \left[ \begin{array}{cccccc}
219 * {\cal{C}}_{0000} & {\cal{C}}_{0011} &
220 * {\cal{C}}_{0022} & \sqrt{2} {\cal{C}}_{0012} & \sqrt{2}
221 * {\cal{C}}_{0002} & \sqrt{2} {\cal{C}}_{0001} \\
222 * {\cal{C}}_{1100} & {\cal{C}}_{1111} &
223 * {\cal{C}}_{1122} & \sqrt{2} {\cal{C}}_{1112} & \sqrt{2}
224 * {\cal{C}}_{1102} & \sqrt{2} {\cal{C}}_{1101} \\
225 * {\cal{C}}_{2200} & {\cal{C}}_{2211} &
226 * {\cal{C}}_{2222} & \sqrt{2} {\cal{C}}_{2212} & \sqrt{2}
227 * {\cal{C}}_{2202} & \sqrt{2} {\cal{C}}_{2201} \\
228 * \sqrt{2} {\cal{C}}_{1200} & \sqrt{2} {\cal{C}}_{1211} & \sqrt{2}
229 * {\cal{C}}_{1222} & 2 {\cal{C}}_{1212} & 2 {\cal{C}}_{1202} & 2
230 * {\cal{C}}_{1201} \\
231 * \sqrt{2} {\cal{C}}_{0200} & \sqrt{2} {\cal{C}}_{0211} & \sqrt{2}
232 * {\cal{C}}_{0222} & 2 {\cal{C}}_{0212} & 2 {\cal{C}}_{0202} & 2
233 * {\cal{C}}_{0201} \\ \sqrt{2} {\cal{C}}_{0100} & \sqrt{2}
234 * {\cal{C}}_{0111} & \sqrt{2} {\cal{C}}_{0122} & 2 {\cal{C}}_{0112} & 2
235 * {\cal{C}}_{0102} & 2 {\cal{C}}_{0101} \end{array}\right] .
236 * @f]
237 * The mapping from the two Kelvin indices of the FullMatrix
238 * $\tilde{\cal{C}}$ to the rank-4 SymmetricTensor $\cal{C}$ can be inferred
239 * using the table shown above.
240 *
241 * An important observation is that both the left-hand side tensor
242 * $\tilde{\mathbf{S}}$ and right-hand side tensor $\tilde{\mathbf{E}}$ have
243 * the same form; this is a property that is not present in Voigt notation.
244 * The various factors introduced into $\tilde{\mathbf{S}}$,
245 * $\tilde{\mathbf{E}}$ and $\tilde{\cal{C}}$ account for the symmetry of
246 * the tensors. The Kelvin description of their non-symmetric counterparts
247 * include no such factors.
248 *
249 * Some useful references that show how this notation works include, amongst
250 * others, @cite Nagel2016 and @cite Dellinger1998
251 * as well as the online reference found on
252 * <a
253 * href="https://en.wikipedia.org/wiki/Voigt_notation#Mandel_notation">this
254 * wikipedia page</a> and <a
255 * href="https://github.com/dealii/dealii/tree/master/tests/physics/notation-kelvin_02.cc">the
256 * unit tests</a>.
257 */
258 namespace Kelvin
259 {
260 /**
261 * Input matrix has incorrect number of rows.
262 */
263 DeclException3(ExcNotationExcFullMatrixToTensorRowSize2,
264 int,
265 int,
266 int,
267 << "The number of rows in the input matrix is " << arg1
268 << ", but needs to be either " << arg2 << " or " << arg3
269 << '.');
270
271
272 /**
273 * Input matrix has incorrect number of rows.
274 */
275 DeclException4(ExcNotationExcFullMatrixToTensorRowSize3,
276 int,
277 int,
278 int,
279 int,
280 << "The number of rows in the input matrix is " << arg1
281 << ", but needs to be either " << arg2 << ',' << arg3
282 << ", or " << arg4 << '.');
283
284
285 /**
286 * Input matrix has incorrect number of columns.
287 */
288 DeclException3(ExcNotationExcFullMatrixToTensorColSize2,
289 int,
290 int,
291 int,
292 << "The number of columns in the input matrix is " << arg1
293 << ", but needs to be either " << arg2 << " or " << arg3
294 << '.');
295
296
297 /**
298 * Input matrix has incorrect number of columns.
299 */
300 DeclException4(ExcNotationExcFullMatrixToTensorColSize3,
301 int,
302 int,
303 int,
304 int,
305 << "The number of columns in the input matrix is " << arg1
306 << ", but needs to be either " << arg2 << ',' << arg3
307 << ", or " << arg4 << '.');
308
309
310 /**
311 * @name Forward operation: Tensor notation to Kelvin notation
312 */
313 /** @{ */
314
315 /**
316 * Convert a scalar value to its compressed vector equivalent.
317 *
318 * The output vector has one entry.
319 */
320 template <typename Number>
321 Vector<Number>
322 to_vector(const Number &s);
323
324
325 /**
326 * Convert a rank-0 tensor to its compressed vector equivalent.
327 *
328 * The output vector has one entry.
329 */
330 template <int dim, typename Number>
331 Vector<Number>
332 to_vector(const Tensor<0, dim, Number> &s);
333
334
335 /**
336 * Convert a rank-1 tensor to its compressed vector equivalent.
337 *
338 * The output vector has $dim$ entries.
339 */
340 template <int dim, typename Number>
341 Vector<Number>
342 to_vector(const Tensor<1, dim, Number> &v);
343
344
345 /**
346 * Convert a rank-2 tensor to its compressed vector equivalent.
347 *
348 * The output vector has Tensor<2,dim>::n_independent_components entries.
349 */
350 template <int dim, typename Number>
351 Vector<Number>
352 to_vector(const Tensor<2, dim, Number> &t);
353
354
355 /**
356 * Convert a rank-2 symmetric tensor to its compressed vector equivalent.
357 *
358 * The output vector has SymmetricTensor<2,dim>::n_independent_components
359 * entries.
360 */
361 template <int dim, typename Number>
362 Vector<Number>
363 to_vector(const SymmetricTensor<2, dim, Number> &st);
364
365
366 /**
367 * Convert a scalar value to its compressed matrix equivalent.
368 *
369 * The output matrix will have one row and one column.
370 */
371 template <typename Number>
372 FullMatrix<Number>
373 to_matrix(const Number &s);
374
375
376 /**
377 * Convert a rank-0 tensor to its compressed matrix equivalent.
378 *
379 * The output matrix will have one row and one column.
380 */
381 template <int dim, typename Number>
382 FullMatrix<Number>
383 to_matrix(const Tensor<0, dim, Number> &s);
384
385
386 /**
387 * Convert a rank-1 tensor to its compressed matrix equivalent.
388 *
389 * The output matrix will have $dim$ rows and one column.
390 */
391 template <int dim, typename Number>
392 FullMatrix<Number>
393 to_matrix(const Tensor<1, dim, Number> &v);
394
395
396 /**
397 * Convert a rank-2 tensor to its compressed matrix equivalent.
398 *
399 * The output matrix will have $dim$ rows and $dim$ columns.
400 */
401 template <int dim, typename Number>
402 FullMatrix<Number>
403 to_matrix(const Tensor<2, dim, Number> &t);
404
405
406 /**
407 * Convert a rank-2 symmetric tensor to its compressed matrix equivalent.
408 *
409 * The output matrix will have $dim$ rows and $dim$ columns, with the same
410 * format as the equivalent function for non-symmetric tensors. This is
411 * because it is not possible to compress the
412 * SymmetricTensor<2,dim>::n_independent_components unique entries into a
413 * square matrix.
414 */
415 template <int dim, typename Number>
416 FullMatrix<Number>
417 to_matrix(const SymmetricTensor<2, dim, Number> &st);
418
419 /**
420 * Convert a rank-3 tensor to its compressed matrix equivalent.
421 *
422 * The template arguments @p SubTensor1 and @p SubTensor2 determine how
423 * the unrolling occurs, in particular how the elements of the rank-3
424 * tensor are to be interpreted.
425 *
426 * So, for example, with the following two conversions
427 * @code
428 * Tensor<3,dim> r3_tnsr; // All elements filled differently
429 * Tensor<3,dim> r3_symm_tnsr; // Some elements filled symmetrically
430 *
431 * const FullMatrix<double> mtrx_1 =
432 * Physics::Notation::to_matrix<dim,
433 * Tensor<2,dim>,
434 * Tensor<1,dim>*>(r3_tnsr);
435 * const FullMatrix<double> mtrx_2 =
436 * Physics::Notation::to_matrix<dim,
437 * Tensor<1,dim>,
438 * SymmetricTensor<2,dim>*>(r3_symm_tnsr);
439 * @endcode
440 * the matrix @p mtrx_1 will have $dim \times dim$ rows and $dim$ columns
441 * (i.e. size Tensor<2,dim>::n_independent_components $\times$
442 * Tensor<1,dim>::n_independent_components),
443 * while those of the matrix @p mtrx_2 will have $dim$ rows and
444 * $(dim \times dim + dim)/2$ columns
445 * (i.e. size Tensor<1,dim>::n_independent_components $\times$
446 * SymmetricTensor<2,dim>::n_independent_components), as it is assumed
447 * that the entries corresponding to the alternation of the second and
448 * third indices are equal. That is to say that
449 * <code>r3_symm_tnsr[i][j][k] == r3_symm_tnsr[i][k][j]</code>.
450 */
451 template <int dim,
452 typename SubTensor1 = Tensor<2, dim>,
453 typename SubTensor2 = Tensor<1, dim>,
454 typename Number>
455 FullMatrix<Number>
456 to_matrix(const Tensor<3, dim, Number> &t);
457
458
459 /**
460 * Convert a rank-4 tensor to its compressed matrix equivalent.
461 *
462 * The output matrix will have Tensor<2,dim>::n_independent_components
463 * rows and Tensor<2,dim>::n_independent_components columns.
464 */
465 template <int dim, typename Number>
466 FullMatrix<Number>
467 to_matrix(const Tensor<4, dim, Number> &t);
468
469
470 /**
471 * Convert a rank-4 symmetric tensor to its compressed matrix equivalent.
472 *
473 * The output matrix will have
474 * SymmetricTensor<2,dim>::n_independent_components rows and
475 * SymmetricTensor<2,dim>::n_independent_components columns.
476 */
477 template <int dim, typename Number>
478 FullMatrix<Number>
479 to_matrix(const SymmetricTensor<4, dim, Number> &st);
480
481 /** @} */
482
483 /**
484 * @name Reverse operation: Kelvin notation to tensor notation
485 */
486 /** @{ */
487
488 /**
489 * Convert a compressed vector to its equivalent scalar value.
490 */
491 template <typename Number>
492 void
493 to_tensor(const Vector<Number> &vec, Number &s);
494
495
496 /**
497 * Convert a compressed vector to its equivalent rank-0 tensor.
498 */
499 template <int dim, typename Number>
500 void
501 to_tensor(const Vector<Number> &vec, Tensor<0, dim, Number> &s);
502
503
504 /**
505 * Convert a compressed vector to its equivalent rank-1 tensor.
506 */
507 template <int dim, typename Number>
508 void
509 to_tensor(const Vector<Number> &vec, Tensor<1, dim, Number> &v);
510
511
512 /**
513 * Convert a compressed vector to its equivalent rank-2 tensor.
514 */
515 template <int dim, typename Number>
516 void
517 to_tensor(const Vector<Number> &vec, Tensor<2, dim, Number> &t);
518
519
520 /**
521 * Convert a compressed vector to its equivalent rank-2 symmetric tensor.
522 */
523 template <int dim, typename Number>
524 void
525 to_tensor(const Vector<Number> &vec, SymmetricTensor<2, dim, Number> &st);
526
527
528 /**
529 * Convert a compressed matrix to its equivalent scalar value.
530 */
531 template <typename Number>
532 void
533 to_tensor(const FullMatrix<Number> &mtrx, Number &s);
534
535
536 /**
537 * Convert a compressed matrix to its equivalent rank-0 tensor.
538 */
539 template <int dim, typename Number>
540 void
541 to_tensor(const FullMatrix<Number> &mtrx, Tensor<0, dim, Number> &s);
542
543
544 /**
545 * Convert a compressed matrix to its equivalent rank-1 tensor.
546 */
547 template <int dim, typename Number>
548 void
549 to_tensor(const FullMatrix<Number> &mtrx, Tensor<1, dim, Number> &v);
550
551
552 /**
553 * Convert a compressed matrix to its equivalent rank-2 tensor.
554 */
555 template <int dim, typename Number>
556 void
557 to_tensor(const FullMatrix<Number> &mtrx, Tensor<2, dim, Number> &t);
558
559
560 /**
561 * Convert a compressed matrix to its equivalent rank-2 symmetric tensor.
562 */
563 template <int dim, typename Number>
564 void
565 to_tensor(const FullMatrix<Number> &mtrx,
566 SymmetricTensor<2, dim, Number> &st);
567
568
569 /**
570 * Convert a compressed matrix to its equivalent rank-3 tensor.
571 *
572 * @note Based on the size of the matrix @p mtrx, some of the
573 * components of @p t may be interpreted as having symmetric
574 * counterparts. This is the reverse of the operation explained
575 * in the documentation of the counterpart to_matrix()
576 * function.
577 */
578 template <int dim, typename Number>
579 void
580 to_tensor(const FullMatrix<Number> &mtrx, Tensor<3, dim, Number> &t);
581
582
583 /**
584 * Convert a compressed matrix to its equivalent rank-4 tensor.
585 */
586 template <int dim, typename Number>
587 void
588 to_tensor(const FullMatrix<Number> &mtrx, Tensor<4, dim, Number> &t);
589
590
591 /**
592 * Convert a compressed matrix to its equivalent rank-4 symmetric tensor.
593 */
594 template <int dim, typename Number>
595 void
596 to_tensor(const FullMatrix<Number> &mtrx,
597 SymmetricTensor<4, dim, Number> &st);
598
599
600 /**
601 * A generic helper function that will convert a compressed vector
602 * to its equivalent @p TensorType.
603 */
604 template <typename TensorType, typename Number>
605 TensorType
606 to_tensor(const Vector<Number> &vec);
607
608
609 /**
610 * A generic helper function that will convert a compressed matrix
611 * to its equivalent @p TensorType.
612 */
613 template <typename TensorType, typename Number>
614 TensorType
615 to_tensor(const FullMatrix<Number> &vec);
616 /** @} */
617
618 } // namespace Kelvin
619
620 } // namespace Notation
621 } // namespace Physics
622
623
624 #ifndef DOXYGEN
625
626
627 // ------------------------- inline functions ------------------------
628
629
630 namespace Physics
631 {
632 namespace Notation
633 {
634 namespace Kelvin
635 {
636 namespace internal
637 {
638 /**
639 * Return the tensor indices <code><row, column></code>
640 * associated with a condensed component index. The
641 * @p symmetric flag indicates whether or not the
642 * @p component_n index is associated with a tensor that
643 * has symmetric entries.
644 */
645 template <int dim>
646 std::pair<unsigned int, unsigned int>
647 indices_from_component(const unsigned int component_n,
648 const bool symmetric);
649
650
651 template <int dim>
652 std::pair<unsigned int, unsigned int>
653 indices_from_component(const unsigned int /*component_n*/, const bool)
654 {
655 AssertThrow(false, ExcNotImplemented());
656 return std::make_pair(0u, 0u);
657 }
658
659
660 template <>
661 inline std::pair<unsigned int, unsigned int>
662 indices_from_component<1>(const unsigned int component_n, const bool)
663 {
664 AssertIndexRange(component_n, 1);
665
666 return std::make_pair(0u, 0u);
667 }
668
669
670 template <>
671 inline std::pair<unsigned int, unsigned int>
672 indices_from_component<2>(const unsigned int component_n,
673 const bool symmetric)
674 {
675 if (symmetric == true)
676 {
677 Assert(
678 (component_n < SymmetricTensor<2, 2>::n_independent_components),
679 ExcIndexRange(component_n,
680 0,
681 SymmetricTensor<2, 2>::n_independent_components));
682 }
683 else
684 {
685 Assert((component_n < Tensor<2, 2>::n_independent_components),
686 ExcIndexRange(component_n,
687 0,
688 Tensor<2, 2>::n_independent_components));
689 }
690
691 static const unsigned int indices[4][2] = {{0, 0},
692 {1, 1},
693 {0, 1},
694 {1, 0}};
695 return std::make_pair(indices[component_n][0],
696 indices[component_n][1]);
697 }
698
699
700 template <>
701 inline std::pair<unsigned int, unsigned int>
702 indices_from_component<3>(const unsigned int component_n,
703 const bool symmetric)
704 {
705 if (symmetric == true)
706 {
707 Assert(
708 (component_n < SymmetricTensor<2, 3>::n_independent_components),
709 ExcIndexRange(component_n,
710 0,
711 SymmetricTensor<2, 3>::n_independent_components));
712 }
713 else
714 {
715 Assert((component_n < Tensor<2, 3>::n_independent_components),
716 ExcIndexRange(component_n,
717 0,
718 Tensor<2, 3>::n_independent_components));
719 }
720
721 static const unsigned int indices[9][2] = {{0, 0},
722 {1, 1},
723 {2, 2},
724 {1, 2},
725 {0, 2},
726 {0, 1},
727 {1, 0},
728 {2, 0},
729 {2, 1}};
730 return std::make_pair(indices[component_n][0],
731 indices[component_n][1]);
732 }
733
734
735 /**
736 * Return the scaling factor to be applied to the
737 * entry in the condensed vector.
738 */
739 template <int dim>
740 double
741 vector_component_factor(const unsigned int component_i,
742 const bool symmetric)
743 {
744 if (symmetric == false)
745 return 1.0;
746
747 if (component_i < dim)
748 return 1.0;
749 else
750 return numbers::SQRT2;
751 }
752
753
754 /**
755 * Return the scaling factor to be applied to the
756 * entry in the condensed matrix.
757 */
758 template <int dim>
759 double
760 matrix_component_factor(const unsigned int component_i,
761 const unsigned int component_j,
762 const bool symmetric)
763 {
764 if (symmetric == false)
765 return 1.0;
766
767 // This case check returns equivalent of this result:
768 // internal::vector_component_factor<dim>(component_i,symmetric)*internal::vector_component_factor<dim>(component_j,symmetric);
769 if (component_i < dim && component_j < dim)
770 return 1.0;
771 else if (component_i >= dim && component_j >= dim)
772 return 2.0;
773 else // ((component_i >= dim && component_j < dim) || (component_i <
774 // dim && component_j >= dim))
775 return numbers::SQRT2;
776 }
777
778 } // namespace internal
779
780
781 template <typename Number>
782 Vector<Number>
783 to_vector(const Number &s)
784 {
785 Vector<Number> out(1);
786 const unsigned int n_rows = out.size();
787 for (unsigned int r = 0; r < n_rows; ++r)
788 out(r) = s;
789 return out;
790 }
791
792
793 template <int dim, typename Number>
794 Vector<Number>
795 to_vector(const Tensor<0, dim, Number> &s)
796 {
797 return to_vector(s.operator const Number &());
798 }
799
800
801 template <int dim, typename Number>
802 Vector<Number>
803 to_vector(const Tensor<1, dim, Number> &v)
804 {
805 Vector<Number> out(v.n_independent_components);
806 const unsigned int n_rows = out.size();
807 for (unsigned int r = 0; r < n_rows; ++r)
808 {
809 const std::pair<unsigned int, unsigned int> indices =
810 internal::indices_from_component<dim>(r, false);
811 Assert(indices.first < dim, ExcInternalError());
812 const unsigned int i = indices.first;
813 out(r) = v[i];
814 }
815 return out;
816 }
817
818
819 template <int dim, typename Number>
820 Vector<Number>
821 to_vector(const Tensor<2, dim, Number> &t)
822 {
823 Vector<Number> out(t.n_independent_components);
824 const unsigned int n_rows = out.size();
825 for (unsigned int r = 0; r < n_rows; ++r)
826 {
827 const std::pair<unsigned int, unsigned int> indices =
828 internal::indices_from_component<dim>(r, false);
829 Assert(indices.first < dim, ExcInternalError());
830 Assert(indices.second < dim, ExcInternalError());
831 const unsigned int i = indices.first;
832 const unsigned int j = indices.second;
833 out(r) = t[i][j];
834 }
835 return out;
836 }
837
838
839 template <int dim, typename Number>
840 Vector<Number>
841 to_vector(const SymmetricTensor<2, dim, Number> &st)
842 {
843 Vector<Number> out(st.n_independent_components);
844 const unsigned int n_rows = out.size();
845 for (unsigned int r = 0; r < n_rows; ++r)
846 {
847 const std::pair<unsigned int, unsigned int> indices =
848 internal::indices_from_component<dim>(r, true);
849 Assert(indices.first < dim, ExcInternalError());
850 Assert(indices.second < dim, ExcInternalError());
851 Assert(indices.second >= indices.first, ExcInternalError());
852 const unsigned int i = indices.first;
853 const unsigned int j = indices.second;
854
855 const double factor =
856 internal::vector_component_factor<dim>(r, true);
857
858 out(r) = factor * st[i][j];
859 }
860 return out;
861 }
862
863
864 template <typename Number>
865 FullMatrix<Number>
866 to_matrix(const Number &s)
867 {
868 FullMatrix<Number> out(1, 1);
869 out(0, 0) = s;
870 return out;
871 }
872
873
874 template <int dim, typename Number>
875 FullMatrix<Number>
876 to_matrix(const Tensor<0, dim, Number> &s)
877 {
878 return to_matrix(s.operator const Number &());
879 }
880
881
882 template <int dim, typename Number>
883 FullMatrix<Number>
884 to_matrix(const Tensor<1, dim, Number> &v)
885 {
886 FullMatrix<Number> out(v.n_independent_components, 1);
887 const unsigned int n_rows = out.m();
888 const unsigned int n_cols = out.n();
889 for (unsigned int r = 0; r < n_rows; ++r)
890 {
891 const std::pair<unsigned int, unsigned int> indices =
892 internal::indices_from_component<dim>(r, false);
893 Assert(indices.first < dim, ExcInternalError());
894 const unsigned int i = indices.first;
895
896 for (unsigned int c = 0; c < n_cols; ++c)
897 {
898 Assert(c < 1, ExcInternalError());
899 out(r, c) = v[i];
900 }
901 }
902 return out;
903 }
904
905
906 template <int dim, typename Number>
907 FullMatrix<Number>
908 to_matrix(const Tensor<2, dim, Number> &t)
909 {
910 FullMatrix<Number> out(dim, dim);
911 const unsigned int n_rows = out.m();
912 const unsigned int n_cols = out.n();
913 for (unsigned int r = 0; r < n_rows; ++r)
914 {
915 const std::pair<unsigned int, unsigned int> indices_i =
916 internal::indices_from_component<dim>(r, false);
917 Assert(indices_i.first < dim, ExcInternalError());
918 Assert(indices_i.second < dim, ExcInternalError());
919 const unsigned int i = indices_i.first;
920
921 for (unsigned int c = 0; c < n_cols; ++c)
922 {
923 const std::pair<unsigned int, unsigned int> indices_j =
924 internal::indices_from_component<dim>(c, false);
925 Assert(indices_j.first < dim, ExcInternalError());
926 Assert(indices_j.second < dim, ExcInternalError());
927 const unsigned int j = indices_j.second;
928
929 out(r, c) = t[i][j];
930 }
931 }
932 return out;
933 }
934
935
936 template <int dim, typename Number>
937 FullMatrix<Number>
938 to_matrix(const SymmetricTensor<2, dim, Number> &st)
939 {
940 return to_matrix(Tensor<2, dim, Number>(st));
941 }
942
943
944 namespace internal
945 {
946 template <typename TensorType>
947 struct is_rank_2_symmetric_tensor : std::false_type
948 {};
949
950 template <int dim, typename Number>
951 struct is_rank_2_symmetric_tensor<SymmetricTensor<2, dim, Number>>
952 : std::true_type
953 {};
954 } // namespace internal
955
956
957 template <int dim,
958 typename SubTensor1,
959 typename SubTensor2,
960 typename Number>
961 FullMatrix<Number>
962 to_matrix(const Tensor<3, dim, Number> &t)
963 {
964 static_assert(
965 (SubTensor1::dimension == dim && SubTensor2::dimension == dim),
966 "Sub-tensor spatial dimension is different from those of the input tensor.");
967
968 static_assert(
969 (SubTensor1::rank == 2 && SubTensor2::rank == 1) ||
970 (SubTensor1::rank == 1 && SubTensor2::rank == 2),
971 "Cannot build a rank 3 tensor from the given combination of sub-tensors.");
972
973 FullMatrix<Number> out(SubTensor1::n_independent_components,
974 SubTensor2::n_independent_components);
975 const unsigned int n_rows = out.m();
976 const unsigned int n_cols = out.n();
977
978 if (SubTensor1::rank == 2 && SubTensor2::rank == 1)
979 {
980 const bool subtensor_is_rank_2_symmetric_tensor =
981 internal::is_rank_2_symmetric_tensor<SubTensor1>::value;
982
983 for (unsigned int r = 0; r < n_rows; ++r)
984 {
985 const std::pair<unsigned int, unsigned int> indices_ij =
986 internal::indices_from_component<dim>(
987 r, subtensor_is_rank_2_symmetric_tensor);
988 Assert(indices_ij.first < dim, ExcInternalError());
989 Assert(indices_ij.second < dim, ExcInternalError());
990 if (subtensor_is_rank_2_symmetric_tensor)
991 {
992 Assert(indices_ij.second >= indices_ij.first,
993 ExcInternalError());
994 }
995 const unsigned int i = indices_ij.first;
996 const unsigned int j = indices_ij.second;
997
998 const double factor = internal::vector_component_factor<dim>(
999 r, subtensor_is_rank_2_symmetric_tensor);
1000
1001 for (unsigned int c = 0; c < n_cols; ++c)
1002 {
1003 const std::pair<unsigned int, unsigned int> indices_k =
1004 internal::indices_from_component<dim>(c, false);
1005 Assert(indices_k.first < dim, ExcInternalError());
1006 const unsigned int k = indices_k.first;
1007
1008 if (subtensor_is_rank_2_symmetric_tensor)
1009 out(r, c) = factor * t[i][j][k];
1010 else
1011 out(r, c) = t[i][j][k];
1012 }
1013 }
1014 }
1015 else if (SubTensor1::rank == 1 && SubTensor2::rank == 2)
1016 {
1017 const bool subtensor_is_rank_2_symmetric_tensor =
1018 internal::is_rank_2_symmetric_tensor<SubTensor2>::value;
1019
1020 for (unsigned int r = 0; r < n_rows; ++r)
1021 {
1022 const std::pair<unsigned int, unsigned int> indices_k =
1023 internal::indices_from_component<dim>(r, false);
1024 Assert(indices_k.first < dim, ExcInternalError());
1025 const unsigned int k = indices_k.first;
1026
1027 for (unsigned int c = 0; c < n_cols; ++c)
1028 {
1029 const std::pair<unsigned int, unsigned int> indices_ij =
1030 internal::indices_from_component<dim>(
1031 c, subtensor_is_rank_2_symmetric_tensor);
1032 Assert(indices_ij.first < dim, ExcInternalError());
1033 Assert(indices_ij.second < dim, ExcInternalError());
1034 if (subtensor_is_rank_2_symmetric_tensor)
1035 {
1036 Assert(indices_ij.second >= indices_ij.first,
1037 ExcInternalError());
1038 }
1039 const unsigned int i = indices_ij.first;
1040 const unsigned int j = indices_ij.second;
1041
1042 if (subtensor_is_rank_2_symmetric_tensor)
1043 {
1044 const double factor =
1045 internal::vector_component_factor<dim>(
1046 c, subtensor_is_rank_2_symmetric_tensor);
1047 out(r, c) = factor * t[k][i][j];
1048 }
1049 else
1050 out(r, c) = t[k][i][j];
1051 }
1052 }
1053 }
1054 else
1055 {
1056 AssertThrow(false, ExcNotImplemented());
1057 }
1058
1059 return out;
1060 }
1061
1062
1063 template <int dim, typename Number>
1064 FullMatrix<Number>
1065 to_matrix(const Tensor<4, dim, Number> &t)
1066 {
1067 FullMatrix<Number> out(
1068 Tensor<2, dim, Number>::n_independent_components,
1069 Tensor<2, dim, Number>::n_independent_components);
1070 const unsigned int n_rows = out.m();
1071 const unsigned int n_cols = out.n();
1072 for (unsigned int r = 0; r < n_rows; ++r)
1073 {
1074 const std::pair<unsigned int, unsigned int> indices_ij =
1075 internal::indices_from_component<dim>(r, false);
1076 Assert(indices_ij.first < dim, ExcInternalError());
1077 Assert(indices_ij.second < dim, ExcInternalError());
1078 const unsigned int i = indices_ij.first;
1079 const unsigned int j = indices_ij.second;
1080
1081 for (unsigned int c = 0; c < n_cols; ++c)
1082 {
1083 const std::pair<unsigned int, unsigned int> indices_kl =
1084 internal::indices_from_component<dim>(c, false);
1085 Assert(indices_kl.first < dim, ExcInternalError());
1086 Assert(indices_kl.second < dim, ExcInternalError());
1087 const unsigned int k = indices_kl.first;
1088 const unsigned int l = indices_kl.second;
1089
1090 out(r, c) = t[i][j][k][l];
1091 }
1092 }
1093 return out;
1094 }
1095
1096
1097 template <int dim, typename Number>
1098 FullMatrix<Number>
1099 to_matrix(const SymmetricTensor<4, dim, Number> &st)
1100 {
1101 FullMatrix<Number> out(
1102 SymmetricTensor<2, dim, Number>::n_independent_components,
1103 SymmetricTensor<2, dim, Number>::n_independent_components);
1104 const unsigned int n_rows = out.m();
1105 const unsigned int n_cols = out.n();
1106 for (unsigned int r = 0; r < n_rows; ++r)
1107 {
1108 const std::pair<unsigned int, unsigned int> indices_ij =
1109 internal::indices_from_component<dim>(r, true);
1110 Assert(indices_ij.first < dim, ExcInternalError());
1111 Assert(indices_ij.second < dim, ExcInternalError());
1112 Assert(indices_ij.second >= indices_ij.first, ExcInternalError());
1113 const unsigned int i = indices_ij.first;
1114 const unsigned int j = indices_ij.second;
1115
1116 for (unsigned int c = 0; c < n_cols; ++c)
1117 {
1118 const std::pair<unsigned int, unsigned int> indices_kl =
1119 internal::indices_from_component<dim>(c, true);
1120 Assert(indices_kl.first < dim, ExcInternalError());
1121 Assert(indices_kl.second < dim, ExcInternalError());
1122 Assert(indices_kl.second >= indices_kl.first,
1123 ExcInternalError());
1124 const unsigned int k = indices_kl.first;
1125 const unsigned int l = indices_kl.second;
1126
1127 const double factor =
1128 internal::matrix_component_factor<dim>(r, c, true);
1129
1130 out(r, c) = factor * st[i][j][k][l];
1131 }
1132 }
1133 return out;
1134 }
1135
1136
1137 template <typename Number>
1138 void
1139 to_tensor(const Vector<Number> &vec, Number &s)
1140 {
1141 Assert(vec.size() == 1, ExcDimensionMismatch(vec.size(), 1));
1142 s = vec(0);
1143 }
1144
1145
1146 template <int dim, typename Number>
1147 void
1148 to_tensor(const Vector<Number> &vec, Tensor<0, dim, Number> &s)
1149 {
1150 return to_tensor(vec, s.operator Number &());
1151 }
1152
1153
1154 template <int dim, typename Number>
1155 void
1156 to_tensor(const Vector<Number> &vec, Tensor<1, dim, Number> &v)
1157 {
1158 Assert(vec.size() == v.n_independent_components,
1159 ExcDimensionMismatch(vec.size(), v.n_independent_components));
1160 const unsigned int n_rows = vec.size();
1161 for (unsigned int r = 0; r < n_rows; ++r)
1162 {
1163 const std::pair<unsigned int, unsigned int> indices =
1164 internal::indices_from_component<dim>(r, false);
1165 Assert(indices.first < dim, ExcInternalError());
1166 const unsigned int i = indices.first;
1167 v[i] = vec(r);
1168 }
1169 }
1170
1171
1172 template <int dim, typename Number>
1173 void
1174 to_tensor(const Vector<Number> &vec, Tensor<2, dim, Number> &t)
1175 {
1176 Assert(vec.size() == t.n_independent_components,
1177 ExcDimensionMismatch(vec.size(), t.n_independent_components));
1178 const unsigned int n_rows = vec.size();
1179 for (unsigned int r = 0; r < n_rows; ++r)
1180 {
1181 const std::pair<unsigned int, unsigned int> indices =
1182 internal::indices_from_component<dim>(r, false);
1183 Assert(indices.first < dim, ExcInternalError());
1184 Assert(indices.second < dim, ExcInternalError());
1185 const unsigned int i = indices.first;
1186 const unsigned int j = indices.second;
1187 t[i][j] = vec(r);
1188 }
1189 }
1190
1191
1192 template <int dim, typename Number>
1193 void
1194 to_tensor(const Vector<Number> &vec, SymmetricTensor<2, dim, Number> &st)
1195 {
1196 Assert(vec.size() == st.n_independent_components,
1197 ExcDimensionMismatch(vec.size(), st.n_independent_components));
1198 const unsigned int n_rows = vec.size();
1199 for (unsigned int r = 0; r < n_rows; ++r)
1200 {
1201 const std::pair<unsigned int, unsigned int> indices =
1202 internal::indices_from_component<dim>(r, true);
1203 Assert(indices.first < dim, ExcInternalError());
1204 Assert(indices.second < dim, ExcInternalError());
1205 Assert(indices.second >= indices.first, ExcInternalError());
1206 const unsigned int i = indices.first;
1207 const unsigned int j = indices.second;
1208
1209 const double inv_factor =
1210 1.0 / internal::vector_component_factor<dim>(r, true);
1211
1212 st[i][j] = inv_factor * vec(r);
1213 }
1214 }
1215
1216
1217 template <typename Number>
1218 void
1219 to_tensor(const FullMatrix<Number> &mtrx, Number &s)
1220 {
1221 Assert(mtrx.m() == 1, ExcDimensionMismatch(mtrx.m(), 1));
1222 Assert(mtrx.n() == 1, ExcDimensionMismatch(mtrx.n(), 1));
1223 Assert(mtrx.n_elements() == 1,
1224 ExcDimensionMismatch(mtrx.n_elements(), 1));
1225 s = mtrx(0, 0);
1226 }
1227
1228
1229 template <int dim, typename Number>
1230 void
1231 to_tensor(const FullMatrix<Number> &mtrx, Tensor<0, dim, Number> &s)
1232 {
1233 return to_tensor(mtrx, s.operator Number &());
1234 }
1235
1236
1237 template <int dim, typename Number>
1238 void
1239 to_tensor(const FullMatrix<Number> &mtrx, Tensor<1, dim, Number> &v)
1240 {
1241 Assert(mtrx.m() == dim, ExcDimensionMismatch(mtrx.m(), dim));
1242 Assert(mtrx.n() == 1, ExcDimensionMismatch(mtrx.n(), 1));
1243 Assert(mtrx.n_elements() == v.n_independent_components,
1244 ExcDimensionMismatch(mtrx.n_elements(),
1245 v.n_independent_components));
1246
1247 const unsigned int n_rows = mtrx.m();
1248 const unsigned int n_cols = mtrx.n();
1249 for (unsigned int r = 0; r < n_rows; ++r)
1250 {
1251 const std::pair<unsigned int, unsigned int> indices =
1252 internal::indices_from_component<dim>(r, false);
1253 Assert(indices.first < dim, ExcInternalError());
1254 Assert(indices.second == 0, ExcInternalError());
1255 const unsigned int i = indices.first;
1256
1257 for (unsigned int c = 0; c < n_cols; ++c)
1258 {
1259 Assert(c < 1, ExcInternalError());
1260 v[i] = mtrx(r, c);
1261 }
1262 }
1263 }
1264
1265
1266 template <int dim, typename Number>
1267 void
1268 to_tensor(const FullMatrix<Number> &mtrx, Tensor<2, dim, Number> &t)
1269 {
1270 Assert(mtrx.m() == dim, ExcDimensionMismatch(mtrx.m(), dim));
1271 Assert(mtrx.n() == dim, ExcDimensionMismatch(mtrx.n(), dim));
1272 Assert(mtrx.n_elements() == t.n_independent_components,
1273 ExcDimensionMismatch(mtrx.n_elements(),
1274 t.n_independent_components));
1275
1276 const unsigned int n_rows = mtrx.m();
1277 const unsigned int n_cols = mtrx.n();
1278 for (unsigned int r = 0; r < n_rows; ++r)
1279 {
1280 const std::pair<unsigned int, unsigned int> indices_i =
1281 internal::indices_from_component<dim>(r, false);
1282 Assert(indices_i.first < dim, ExcInternalError());
1283 Assert(indices_i.second < dim, ExcInternalError());
1284 const unsigned int i = indices_i.first;
1285
1286 for (unsigned int c = 0; c < n_cols; ++c)
1287 {
1288 const std::pair<unsigned int, unsigned int> indices_j =
1289 internal::indices_from_component<dim>(c, false);
1290 Assert(indices_j.first < dim, ExcInternalError());
1291 Assert(indices_j.second < dim, ExcInternalError());
1292 const unsigned int j = indices_j.second;
1293
1294 t[i][j] = mtrx(r, c);
1295 }
1296 }
1297 }
1298
1299
1300 template <int dim, typename Number>
1301 void
1302 to_tensor(const FullMatrix<Number> &mtrx,
1303 SymmetricTensor<2, dim, Number> &st)
1304 {
1305 // Its impossible to fit the (dim^2 + dim)/2 entries into a square
1306 // matrix We therefore assume that its been converted to a standard
1307 // tensor format using to_matrix (SymmetricTensor<2,dim,Number>) at some
1308 // point...
1309 Assert(mtrx.m() == dim, ExcDimensionMismatch(mtrx.m(), dim));
1310 Assert(mtrx.n() == dim, ExcDimensionMismatch(mtrx.n(), dim));
1311 Assert((mtrx.n_elements() ==
1312 Tensor<2, dim, Number>::n_independent_components),
1313 ExcDimensionMismatch(
1314 mtrx.n_elements(),
1315 Tensor<2, dim, Number>::n_independent_components));
1316
1317 Tensor<2, dim, Number> tmp;
1318 to_tensor(mtrx, tmp);
1319 st = symmetrize(tmp);
1320 Assert((Tensor<2, dim, Number>(st) - tmp).norm() < 1e-12,
1321 ExcMessage(
1322 "The entries stored inside the matrix were not symmetric"));
1323 }
1324
1325
1326 template <int dim, typename Number>
1327 void
1328 to_tensor(const FullMatrix<Number> &mtrx, Tensor<3, dim, Number> &t)
1329 {
1330 Assert((mtrx.m() == Tensor<1, dim, Number>::n_independent_components) ||
1331 (mtrx.m() ==
1332 Tensor<2, dim, Number>::n_independent_components) ||
1333 (mtrx.m() ==
1334 SymmetricTensor<2, dim, Number>::n_independent_components),
1335 ExcNotationExcFullMatrixToTensorColSize3(
1336 mtrx.m(),
1337 Tensor<1, dim, Number>::n_independent_components,
1338 Tensor<2, dim, Number>::n_independent_components,
1339 SymmetricTensor<2, dim, Number>::n_independent_components));
1340 Assert((mtrx.n() == Tensor<1, dim, Number>::n_independent_components) ||
1341 (mtrx.n() ==
1342 Tensor<2, dim, Number>::n_independent_components) ||
1343 (mtrx.n() ==
1344 SymmetricTensor<2, dim, Number>::n_independent_components),
1345 ExcNotationExcFullMatrixToTensorColSize3(
1346 mtrx.n(),
1347 Tensor<1, dim, Number>::n_independent_components,
1348 Tensor<2, dim, Number>::n_independent_components,
1349 SymmetricTensor<2, dim, Number>::n_independent_components));
1350
1351 const unsigned int n_rows = mtrx.m();
1352 const unsigned int n_cols = mtrx.n();
1353 if (mtrx.n() == Tensor<1, dim, Number>::n_independent_components)
1354 {
1355 Assert(
1356 (mtrx.m() == Tensor<2, dim, Number>::n_independent_components) ||
1357 (mtrx.m() ==
1358 SymmetricTensor<2, dim, Number>::n_independent_components),
1359 ExcNotationExcFullMatrixToTensorRowSize2(
1360 mtrx.m(),
1361 Tensor<2, dim, Number>::n_independent_components,
1362 SymmetricTensor<2, dim, Number>::n_independent_components));
1363
1364 const bool subtensor_is_rank_2_symmetric_tensor =
1365 (mtrx.m() ==
1366 SymmetricTensor<2, dim, Number>::n_independent_components);
1367
1368 for (unsigned int r = 0; r < n_rows; ++r)
1369 {
1370 const std::pair<unsigned int, unsigned int> indices_ij =
1371 internal::indices_from_component<dim>(
1372 r, subtensor_is_rank_2_symmetric_tensor);
1373 Assert(indices_ij.first < dim, ExcInternalError());
1374 Assert(indices_ij.second < dim, ExcInternalError());
1375 if (subtensor_is_rank_2_symmetric_tensor)
1376 {
1377 Assert(indices_ij.second >= indices_ij.first,
1378 ExcInternalError());
1379 }
1380 const unsigned int i = indices_ij.first;
1381 const unsigned int j = indices_ij.second;
1382
1383 const double inv_factor =
1384 1.0 / internal::vector_component_factor<dim>(
1385 r, subtensor_is_rank_2_symmetric_tensor);
1386
1387 for (unsigned int c = 0; c < n_cols; ++c)
1388 {
1389 const std::pair<unsigned int, unsigned int> indices_k =
1390 internal::indices_from_component<dim>(c, false);
1391 Assert(indices_k.first < dim, ExcInternalError());
1392 const unsigned int k = indices_k.first;
1393
1394 if (subtensor_is_rank_2_symmetric_tensor)
1395 {
1396 t[i][j][k] = inv_factor * mtrx(r, c);
1397 t[j][i][k] = t[i][j][k];
1398 }
1399 else
1400 t[i][j][k] = mtrx(r, c);
1401 }
1402 }
1403 }
1404 else
1405 {
1406 Assert(
1407 (mtrx.m() == Tensor<1, dim, Number>::n_independent_components),
1408 ExcDimensionMismatch(
1409 mtrx.m(), Tensor<1, dim, Number>::n_independent_components));
1410 Assert(
1411 (mtrx.n() == Tensor<2, dim, Number>::n_independent_components) ||
1412 (mtrx.n() ==
1413 SymmetricTensor<2, dim, Number>::n_independent_components),
1414 ExcNotationExcFullMatrixToTensorColSize2(
1415 mtrx.n(),
1416 Tensor<2, dim, Number>::n_independent_components,
1417 SymmetricTensor<2, dim, Number>::n_independent_components));
1418
1419 const bool subtensor_is_rank_2_symmetric_tensor =
1420 (mtrx.n() ==
1421 SymmetricTensor<2, dim, Number>::n_independent_components);
1422
1423 for (unsigned int r = 0; r < n_rows; ++r)
1424 {
1425 const std::pair<unsigned int, unsigned int> indices_k =
1426 internal::indices_from_component<dim>(r, false);
1427 Assert(indices_k.first < dim, ExcInternalError());
1428 const unsigned int k = indices_k.first;
1429
1430 for (unsigned int c = 0; c < n_cols; ++c)
1431 {
1432 const std::pair<unsigned int, unsigned int> indices_ij =
1433 internal::indices_from_component<dim>(
1434 c, subtensor_is_rank_2_symmetric_tensor);
1435 Assert(indices_ij.first < dim, ExcInternalError());
1436 Assert(indices_ij.second < dim, ExcInternalError());
1437 if (subtensor_is_rank_2_symmetric_tensor)
1438 {
1439 Assert(indices_ij.second >= indices_ij.first,
1440 ExcInternalError());
1441 }
1442 const unsigned int i = indices_ij.first;
1443 const unsigned int j = indices_ij.second;
1444
1445 if (subtensor_is_rank_2_symmetric_tensor)
1446 {
1447 const double inv_factor =
1448 1.0 / internal::vector_component_factor<dim>(
1449 c, subtensor_is_rank_2_symmetric_tensor);
1450 t[k][i][j] = inv_factor * mtrx(r, c);
1451 t[k][j][i] = t[k][i][j];
1452 }
1453 else
1454 t[k][i][j] = mtrx(r, c);
1455 }
1456 }
1457 }
1458 }
1459
1460
1461 template <int dim, typename Number>
1462 void
1463 to_tensor(const FullMatrix<Number> &mtrx, Tensor<4, dim, Number> &t)
1464 {
1465 Assert((mtrx.m() == Tensor<2, dim, Number>::n_independent_components),
1466 ExcDimensionMismatch(
1467 mtrx.m(), Tensor<2, dim, Number>::n_independent_components));
1468 Assert((mtrx.n() == Tensor<2, dim, Number>::n_independent_components),
1469 ExcDimensionMismatch(
1470 mtrx.n(), Tensor<2, dim, Number>::n_independent_components));
1471 Assert(mtrx.n_elements() == t.n_independent_components,
1472 ExcDimensionMismatch(mtrx.n_elements(),
1473 t.n_independent_components));
1474
1475 const unsigned int n_rows = mtrx.m();
1476 const unsigned int n_cols = mtrx.n();
1477 for (unsigned int r = 0; r < n_rows; ++r)
1478 {
1479 const std::pair<unsigned int, unsigned int> indices_ij =
1480 internal::indices_from_component<dim>(r, false);
1481 Assert(indices_ij.first < dim, ExcInternalError());
1482 Assert(indices_ij.second < dim, ExcInternalError());
1483 const unsigned int i = indices_ij.first;
1484 const unsigned int j = indices_ij.second;
1485
1486 for (unsigned int c = 0; c < n_cols; ++c)
1487 {
1488 const std::pair<unsigned int, unsigned int> indices_kl =
1489 internal::indices_from_component<dim>(c, false);
1490 Assert(indices_kl.first < dim, ExcInternalError());
1491 Assert(indices_kl.second < dim, ExcInternalError());
1492 const unsigned int k = indices_kl.first;
1493 const unsigned int l = indices_kl.second;
1494
1495 t[i][j][k][l] = mtrx(r, c);
1496 }
1497 }
1498 }
1499
1500
1501 template <int dim, typename Number>
1502 void
1503 to_tensor(const FullMatrix<Number> &mtrx,
1504 SymmetricTensor<4, dim, Number> &st)
1505 {
1506 Assert((mtrx.m() ==
1507 SymmetricTensor<2, dim, Number>::n_independent_components),
1508 ExcDimensionMismatch(
1509 mtrx.m(),
1510 SymmetricTensor<2, dim, Number>::n_independent_components));
1511 Assert((mtrx.n() ==
1512 SymmetricTensor<2, dim, Number>::n_independent_components),
1513 ExcDimensionMismatch(
1514 mtrx.n(),
1515 SymmetricTensor<2, dim, Number>::n_independent_components));
1516 Assert(mtrx.n_elements() == st.n_independent_components,
1517 ExcDimensionMismatch(mtrx.n_elements(),
1518 st.n_independent_components));
1519
1520 const unsigned int n_rows = mtrx.m();
1521 const unsigned int n_cols = mtrx.n();
1522 for (unsigned int r = 0; r < n_rows; ++r)
1523 {
1524 const std::pair<unsigned int, unsigned int> indices_ij =
1525 internal::indices_from_component<dim>(r, false);
1526 Assert(indices_ij.first < dim, ExcInternalError());
1527 Assert(indices_ij.second < dim, ExcInternalError());
1528 const unsigned int i = indices_ij.first;
1529 const unsigned int j = indices_ij.second;
1530
1531 for (unsigned int c = 0; c < n_cols; ++c)
1532 {
1533 const std::pair<unsigned int, unsigned int> indices_kl =
1534 internal::indices_from_component<dim>(c, false);
1535 Assert(indices_kl.first < dim, ExcInternalError());
1536 Assert(indices_kl.second < dim, ExcInternalError());
1537 const unsigned int k = indices_kl.first;
1538 const unsigned int l = indices_kl.second;
1539
1540 const double inv_factor =
1541 1.0 / internal::matrix_component_factor<dim>(r, c, true);
1542
1543 st[i][j][k][l] = inv_factor * mtrx(r, c);
1544 }
1545 }
1546 }
1547
1548
1549 template <typename TensorType, typename Number>
1550 inline TensorType
1551 to_tensor(const Vector<Number> &vec)
1552 {
1553 TensorType out;
1554 to_tensor(vec, out);
1555 return out;
1556 }
1557
1558
1559 template <typename TensorType, typename Number>
1560 inline TensorType
1561 to_tensor(const FullMatrix<Number> &mtrx)
1562 {
1563 TensorType out;
1564 to_tensor(mtrx, out);
1565 return out;
1566 }
1567
1568 } // namespace Kelvin
1569 } // namespace Notation
1570 } // namespace Physics
1571
1572
1573 #endif // DOXYGEN
1574
1575
1576 DEAL_II_NAMESPACE_CLOSE
1577
1578 #endif