Tensor.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::Tensor
29 
30 Description
31  A templated (3 x 3) tensor of objects of <T> derived from MatrixSpace.
32 
33 SourceFiles
34  TensorI.H
35 
36 See also
37  Foam::MatrixSpace
38  Foam::Vector
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef Foam_Tensor_H
43 #define Foam_Tensor_H
44 
45 #include "MatrixSpace.H"
46 #include "Vector.H"
47 #include "SphericalTensor.H"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 template<class Cmpt> class SymmTensor;
56 
57 /*---------------------------------------------------------------------------*\
58  Class Tensor Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 template<class Cmpt>
62 class Tensor
63 :
64  public MatrixSpace<Tensor<Cmpt>, Cmpt, 3, 3>
65 {
66 public:
67 
68  // Typedefs
69 
70  //- Equivalent type of labels used for valid component indexing
71  typedef Tensor<label> labelType;
72 
73 
74  // Member Constants
75 
76  //- Rank of Tensor is 2
77  static constexpr direction rank = 2;
78 
79 
80  // Static Data Members
81 
82  static const Tensor I;
83 
84 
85  //- Component labeling enumeration
86  enum components { XX, XY, XZ, YX, YY, YZ, ZX, ZY, ZZ };
87 
88 
89  // Generated Methods
90 
91  //- Default construct
92  Tensor() = default;
93 
94  //- Copy construct
95  Tensor(const Tensor&) = default;
96 
97  //- Copy assignment
98  Tensor& operator=(const Tensor&) = default;
99 
100 
101  // Constructors
102 
103  //- Construct initialized to zero
104  inline Tensor(const Foam::zero);
105 
106  //- Construct given MatrixSpace of the same rank
107  template<class Cmpt2>
108  inline Tensor(const MatrixSpace<Tensor<Cmpt2>, Cmpt2, 3, 3>& vs);
109 
110  //- Construct given VectorSpace of the same rank
111  template<class Cmpt2>
112  inline Tensor(const VectorSpace<Tensor<Cmpt2>, Cmpt2, 9>& vs);
113 
114  //- Construct given SphericalTensor
115  inline Tensor(const SphericalTensor<Cmpt>& st);
116 
117  //- Construct given SymmTensor
118  inline Tensor(const SymmTensor<Cmpt>& st);
119 
120  //- Construct given triad of row vectors,
121  //- optionally treated as transposed (ie, column vectors)
122  inline Tensor
123  (
124  const Vector<Vector<Cmpt>>& vecs,
125  const bool transposed = false
126  );
127 
128  //- Construct given the three row vectors,
129  //- optionally treated as transposed (ie, column vectors)
130  inline Tensor
131  (
132  const Vector<Cmpt>& x,
133  const Vector<Cmpt>& y,
134  const Vector<Cmpt>& z,
135  const bool transposed = false
136  );
137 
138  //- Construct given the nine components
139  inline Tensor
140  (
141  const Cmpt txx, const Cmpt txy, const Cmpt txz,
142  const Cmpt tyx, const Cmpt tyy, const Cmpt tyz,
143  const Cmpt tzx, const Cmpt tzy, const Cmpt tzz
144  );
145 
146  //- Construct from a block of another matrix space
147  template
148  <
149  template<class, direction, direction> class Block2,
150  direction BRowStart,
151  direction BColStart
152  >
153  Tensor
154  (
155  const Block2<Tensor<Cmpt>, BRowStart, BColStart>& block
156  );
157 
158  //- Construct from Istream
159  inline explicit Tensor(Istream& is);
160 
161 
162  // Member Functions
163 
164  // Component Access
165 
166  const Cmpt& xx() const noexcept { return this->v_[XX]; }
167  const Cmpt& xy() const noexcept { return this->v_[XY]; }
168  const Cmpt& xz() const noexcept { return this->v_[XZ]; }
169  const Cmpt& yx() const noexcept { return this->v_[YX]; }
170  const Cmpt& yy() const noexcept { return this->v_[YY]; }
171  const Cmpt& yz() const noexcept { return this->v_[YZ]; }
172  const Cmpt& zx() const noexcept { return this->v_[ZX]; }
173  const Cmpt& zy() const noexcept { return this->v_[ZY]; }
174  const Cmpt& zz() const noexcept { return this->v_[ZZ]; }
175 
176  Cmpt& xx() noexcept { return this->v_[XX]; }
177  Cmpt& xy() noexcept { return this->v_[XY]; }
178  Cmpt& xz() noexcept { return this->v_[XZ]; }
179  Cmpt& yx() noexcept { return this->v_[YX]; }
180  Cmpt& yy() noexcept { return this->v_[YY]; }
181  Cmpt& yz() noexcept { return this->v_[YZ]; }
182  Cmpt& zx() noexcept { return this->v_[ZX]; }
183  Cmpt& zy() noexcept { return this->v_[ZY]; }
184  Cmpt& zz() noexcept { return this->v_[ZZ]; }
185 
186 
187  // Column-vector access
188 
189  //- Extract vector for column 0
190  inline Vector<Cmpt> cx() const;
191 
192  //- Extract vector for column 1
193  inline Vector<Cmpt> cy() const;
195  //- Extract vector for column 2
196  inline Vector<Cmpt> cz() const;
198  //- Extract vector for given column: compile-time check of index
199  template<direction Idx>
200  inline Vector<Cmpt> col() const;
202  //- Extract vector for given column (0,1,2): runtime check of index
203  inline Vector<Cmpt> col(const direction c) const;
205  //- Set values of given column: compile-time check of index
206  template<direction Idx>
207  inline void col(const Vector<Cmpt>& v);
209  //- Set values of given column (0,1,2): runtime check of index
210  inline void col(const direction c, const Vector<Cmpt>& v);
212  //- Set column values
213  inline void cols
214  (
215  const Vector<Cmpt>& x,
216  const Vector<Cmpt>& y,
217  const Vector<Cmpt>& z
218  );
219 
220 
221  // Row-vector access
222 
223  //- Extract vector for row 0
224  inline Vector<Cmpt> x() const;
225 
226  //- Extract vector for row 1
227  inline Vector<Cmpt> y() const;
228 
229  //- Extract vector for row 2
230  inline Vector<Cmpt> z() const;
231 
232  //- Extract vector for given row: compile-time check of index
233  template<direction Idx>
234  inline Vector<Cmpt> row() const;
235 
236  //- Extract vector for given row (0,1,2): runtime check of index
237  inline Vector<Cmpt> row(const direction r) const;
238 
239  //- Set values of given row: compile-time check of index
240  template<direction Idx>
241  inline void row(const Vector<Cmpt>& v);
242 
243  //- Set values of given row (0,1,2): runtime check of row
244  inline void row(const direction r, const Vector<Cmpt>& v);
245 
246  //- Set row values
247  inline void rows
248  (
249  const Vector<Cmpt>& x,
250  const Vector<Cmpt>& y,
251  const Vector<Cmpt>& z
252  );
253 
254 
255  // Diagonal access and manipulation
256 
257  //- Extract the diagonal as a vector
258  inline Vector<Cmpt> diag() const;
259 
260  //- Set values of the diagonal
261  inline void diag(const Vector<Cmpt>& v);
262 
263  //- Add to the diagonal
264  inline void addDiag(const Vector<Cmpt>& v);
265 
266  //- Subtract from the diagonal
267  inline void subtractDiag(const Vector<Cmpt>& v);
268 
269  //- The L2-norm squared of the diagonal
270  inline scalar diagSqr() const;
271 
272 
273  // Characteristics
274 
275  //- Is identity tensor?
276  inline bool is_identity(const scalar tol = ROOTVSMALL) const;
277 
278 
279  // Tensor Operations
280 
281  //- Return non-Hermitian transpose
282  inline Tensor<Cmpt> T() const;
283 
284  //- The determinate
285  inline Cmpt det() const;
286 
287  //- The 2D determinant by excluding given direction
288  inline Cmpt det2D(const direction excludeCmpt) const;
289 
290  //- Return adjunct matrix (transpose of cofactor matrix)
291  inline Tensor<Cmpt> adjunct() const;
292 
293  //- Return cofactor matrix (transpose of adjunct matrix)
294  inline Tensor<Cmpt> cof() const;
295 
296  //- Return 2D adjunct matrix by excluding given direction
297  inline Tensor<Cmpt> adjunct2D(const direction excludeCmpt) const;
298 
299  //- Return inverse
300  inline Tensor<Cmpt> inv() const;
301 
302  //- Return inverse, with (ad hoc) failsafe handling of 2D tensors
303  inline Tensor<Cmpt> safeInv() const;
304 
305  //- Return inverse of 2D tensor (by excluding given direction)
306  inline Tensor<Cmpt> inv2D(const direction excludeCmpt) const;
307 
308  //- Inner-product of this with another Tensor.
309  inline Tensor<Cmpt> inner(const Tensor<Cmpt>& t2) const;
310 
311  //- Schur-product of this with another Tensor.
312  inline Tensor<Cmpt> schur(const Tensor<Cmpt>& t2) const;
313 
314 
315  // Member Operators
316 
317  //- Assign inner-product of this with another Tensor.
318  inline void operator&=(const Tensor<Cmpt>& t);
319 
320  //- Inherit MatrixSpace assignment operators
322 
323  //- Assign to an equivalent vector space
324  template<class Cmpt2>
325  inline void operator=(const VectorSpace<Tensor<Cmpt2>, Cmpt2, 9>&);
326 
327  //- Assign to a SphericalTensor
328  inline void operator=(const SphericalTensor<Cmpt>&);
329 
330  //- Assign to a SymmTensor
331  inline void operator=(const SymmTensor<Cmpt>&);
332 
333  //- Assign to a triad of row vectors
334  inline void operator=(const Vector<Vector<Cmpt>>&);
335 
336 
337  // Housekeeping
338 
339  //- Deprecated(2018-12) Return vector for given row (0,1)
340  // \deprecated(2018-12) use row() method
341  Vector<Cmpt> vectorComponent(const direction cmpt) const
342  {
343  return row(cmpt);
344  }
345 };
346 
347 
348 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
349 
350 //- Data are contiguous if component type is contiguous
351 template<class Cmpt>
352 struct is_contiguous<Tensor<Cmpt>> : is_contiguous<Cmpt> {};
353 
354 //- Data are contiguous label if component type is label
355 template<class Cmpt>
356 struct is_contiguous_label<Tensor<Cmpt>> : is_contiguous_label<Cmpt> {};
357 
358 //- Data are contiguous scalar if component type is scalar
359 template<class Cmpt>
360 struct is_contiguous_scalar<Tensor<Cmpt>> : is_contiguous_scalar<Cmpt> {};
361 
362 
363 template<class Cmpt>
364 class typeOfRank<Cmpt, 2>
365 {
366 public:
367 
368  typedef Tensor<Cmpt> type;
369 };
370 
371 
372 template<class Cmpt>
373 class typeOfTranspose<Cmpt, Tensor<Cmpt>>
374 {
375 public:
376 
377  typedef Tensor<Cmpt> type;
378 };
379 
380 
381 template<class Cmpt>
382 class typeOfSolve<Tensor<Cmpt>>
383 {
384 public:
385 
386  typedef Tensor<solveScalar> type;
387 };
388 
389 
390 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
391 
392 } // End namespace Foam
393 
394 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
395 
396 // Include inline implementations
397 #include "TensorI.H"
398 
399 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
400 
401 #endif
402 
403 // ************************************************************************* //
Tensor< Cmpt > adjunct2D(const direction excludeCmpt) const
Return 2D adjunct matrix by excluding given direction.
Definition: TensorI.H:484
uint8_t direction
Definition: direction.H:46
Vector< Cmpt > cx() const
Extract vector for column 0.
Definition: TensorI.H:167
A templated (3 x 3) symmetric tensor of objects of <T>, effectively containing 6 elements, derived from VectorSpace.
Definition: SymmTensor.H:50
const Cmpt & yx() const noexcept
Definition: Tensor.H:196
static const Tensor I
Definition: Tensor.H:81
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Vector< Cmpt > cz() const
Extract vector for column 2.
Definition: TensorI.H:181
Tensor< Cmpt > T() const
Return non-Hermitian transpose.
Definition: TensorI.H:419
Vector< Cmpt > x() const
Extract vector for row 0.
Definition: TensorI.H:146
void subtractDiag(const Vector< Cmpt > &v)
Subtract from the diagonal.
Definition: TensorI.H:383
Cmpt det() const
The determinate.
Definition: TensorI.H:431
const Cmpt & xy() const noexcept
Definition: Tensor.H:194
Templated vector space.
Definition: VectorSpace.H:52
Vector< Cmpt > y() const
Extract vector for row 1.
Definition: TensorI.H:153
Templated matrix space.
Definition: MatrixSpace.H:54
components
Component labeling enumeration.
Definition: Tensor.H:87
Tensor()=default
Default construct.
A templated (3 x 3) diagonal tensor of objects of <T>, effectively containing 1 element, derived from VectorSpace.
Tensor< Cmpt > inv2D(const direction excludeCmpt) const
Return inverse of 2D tensor (by excluding given direction)
Definition: TensorI.H:523
Vector< Cmpt > z() const
Extract vector for row 2.
Definition: TensorI.H:160
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
const Cmpt & yz() const noexcept
Definition: Tensor.H:198
const Cmpt & xx() const noexcept
Definition: Tensor.H:193
const Cmpt & yy() const noexcept
Definition: Tensor.H:197
void cols(const Vector< Cmpt > &x, const Vector< Cmpt > &y, const Vector< Cmpt > &z)
Set column values.
Definition: TensorI.H:297
Vector< Cmpt > col() const
Extract vector for given column: compile-time check of index.
bool is_identity(const scalar tol=ROOTVSMALL) const
Is identity tensor?
Definition: TensorI.H:402
const Cmpt & zx() const noexcept
Definition: Tensor.H:199
void rows(const Vector< Cmpt > &x, const Vector< Cmpt > &y, const Vector< Cmpt > &z)
Set row values.
Definition: TensorI.H:311
void addDiag(const Vector< Cmpt > &v)
Add to the diagonal.
Definition: TensorI.H:376
Creates a single block of cells from point coordinates, numbers of cells in each direction and an exp...
Definition: block.H:54
Templated 3D Vector derived from VectorSpace adding construction from 3 components, element access using x(), y() and z() member functions and the inner-product (dot-product) and cross-product operators.
Definition: Vector.H:58
const direction noexcept
Definition: Scalar.H:258
Tensor< Cmpt > adjunct() const
Return adjunct matrix (transpose of cofactor matrix)
Definition: TensorI.H:464
Tensor< label > labelType
Equivalent type of labels used for valid component indexing.
Definition: Tensor.H:68
Tensor< Cmpt > inv() const
Return inverse.
Definition: TensorI.H:573
friend Ostream & operator(Ostream &, const VectorSpace< Form, Cmpt, Ncmpts > &)
const Cmpt & zy() const noexcept
Definition: Tensor.H:200
Tensor< Cmpt > schur(const Tensor< Cmpt > &t2) const
Schur-product of this with another Tensor.
Definition: TensorI.H:558
Vector< Cmpt > vectorComponent(const direction cmpt) const
Deprecated(2018-12) Return vector for given row (0,1)
Definition: Tensor.H:449
Tensor< Cmpt > safeInv() const
Return inverse, with (ad hoc) failsafe handling of 2D tensors.
Definition: TensorI.H:593
Tensor & operator=(const Tensor &)=default
Copy assignment.
Vector< Cmpt > diag() const
Extract the diagonal as a vector.
Definition: TensorI.H:362
const Cmpt & zz() const noexcept
Definition: Tensor.H:201
const dimensionedScalar c
Speed of light in a vacuum.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
void operator &=(const Tensor< Cmpt > &t)
Assign inner-product of this with another Tensor.
Tensor< Cmpt > cof() const
Return cofactor matrix (transpose of adjunct matrix)
Definition: TensorI.H:476
A templated (3 x 3) tensor of objects of <T> derived from MatrixSpace.
Definition: complexI.H:266
static constexpr direction rank
Rank of Tensor is 2.
Definition: Tensor.H:76
Cmpt det2D(const direction excludeCmpt) const
The 2D determinant by excluding given direction.
Definition: TensorI.H:443
const Cmpt & xz() const noexcept
Definition: Tensor.H:195
Tensor< Cmpt > inner(const Tensor< Cmpt > &t2) const
Inner-product of this with another Tensor.
Definition: TensorI.H:535
scalar diagSqr() const
The L2-norm squared of the diagonal.
Definition: TensorI.H:390
Vector< Cmpt > cy() const
Extract vector for column 1.
Definition: TensorI.H:174
Cmpt v_[Ncmpts]
The components of this vector space.
Definition: VectorSpace.H:81
Namespace for OpenFOAM.
Vector< Cmpt > row() const
Extract vector for given row: compile-time check of index.