Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::Tensor
29
30Description
31 A templated (3 x 3) tensor of objects of <T> derived from MatrixSpace.
32
33SourceFiles
34 TensorI.H
35
36See 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
51namespace Foam
52{
53
54// Forward Declarations
55template<class Cmpt> class SymmTensor;
56
57/*---------------------------------------------------------------------------*\
58 Class Tensor Declaration
59\*---------------------------------------------------------------------------*/
60
61template<class Cmpt>
62class Tensor
63:
64 public MatrixSpace<Tensor<Cmpt>, Cmpt, 3, 3>
65{
66public:
67
68 // Typedefs
69
70 //- Equivalent type of labels used for valid component indexing
72
73
74 // Member Constants
75
76 //- Rank of Tensor is 2
77 static constexpr direction rank = 2;
78
79
80 // Static Data Members
82 static const Tensor I;
83
84
85 //- Component labeling enumeration
86 enum components { XX, XY, XZ, YX, YY, YZ, ZX, ZY, ZZ };
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
101 // Constructors
102
103 //- Construct initialized to zero
104 inline Tensor(Foam::zero);
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);
114 //- Construct given SphericalTensor
115 inline Tensor(const SphericalTensor<Cmpt>& st);
116
117 //- Construct given SymmTensor
118 inline Tensor(const SymmTensor<Cmpt>& st);
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
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 );
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;
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,
252 );
253
254
255 // Diagonal access and manipulation
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
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;
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);
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>&);
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 FOAM_DEPRECATED_FOR(2018-12, "row()")
342 Vector<Cmpt> vectorComponent(const direction cmpt) const
343 {
344 return row(cmpt);
345 }
346};
348
349// * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
350
351//- Data are contiguous if component type is contiguous
352template<class Cmpt>
353struct is_contiguous<Tensor<Cmpt>> : is_contiguous<Cmpt> {};
354
355//- Data are contiguous label if component type is label
356template<class Cmpt>
357struct is_contiguous_label<Tensor<Cmpt>> : is_contiguous_label<Cmpt> {};
358
359//- Data are contiguous scalar if component type is scalar
360template<class Cmpt>
361struct is_contiguous_scalar<Tensor<Cmpt>> : is_contiguous_scalar<Cmpt> {};
362
363
364template<class Cmpt>
365class typeOfRank<Cmpt, 2>
366{
367public:
368
369 typedef Tensor<Cmpt> type;
371
372
373template<class Cmpt>
374class typeOfTranspose<Cmpt, Tensor<Cmpt>>
376public:
377
378 typedef Tensor<Cmpt> type;
379};
381
382template<class Cmpt>
383class typeOfSolve<Tensor<Cmpt>>
384{
385public:
386
388};
389
391// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
392
393} // End namespace Foam
394
395// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
396
397// Include inline implementations
398#include "TensorI.H"
399
400// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
401
402#endif
403
404// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
ConstBlock< SubTensor, BRowStart, BColStart > block() const
Return a const sub-block corresponding to the specified type.
A templated (3 x 3) diagonal tensor of objects of <T>, effectively containing 1 element,...
A templated (3 x 3) symmetric tensor of objects of <T>, effectively containing 6 elements,...
Definition SymmTensor.H:53
A templated (3 x 3) tensor of objects of <T> derived from MatrixSpace.
Definition Tensor.H:60
void diag(const Vector< Cmpt > &v)
Set values of the diagonal.
Definition TensorI.H:369
Cmpt & xy() noexcept
Definition Tensor.H:204
scalar diagSqr() const
The L2-norm squared of the diagonal.
Definition TensorI.H:390
void row(const Vector< Cmpt > &v)
Set values of given row: compile-time check of index.
Definition TensorI.H:276
bool is_identity(const scalar tol=ROOTVSMALL) const
Is identity tensor?
Definition TensorI.H:402
Tensor< Cmpt > T() const
Return non-Hermitian transpose.
Definition TensorI.H:419
Cmpt & yz() noexcept
Definition Tensor.H:208
void col(const Vector< Cmpt > &v)
Set values of given column: compile-time check of index.
Definition TensorI.H:249
const Cmpt & yy() const noexcept
Definition Tensor.H:197
Tensor(const Cmpt txx, const Cmpt txy, const Cmpt txz, const Cmpt tyx, const Cmpt tyy, const Cmpt tyz, const Cmpt tzx, const Cmpt tzy, const Cmpt tzz)
Construct given the nine components.
Definition TensorI.H:108
Tensor< Cmpt > safeInv() const
Return inverse, with (ad hoc) failsafe handling of 2D tensors.
Definition TensorI.H:601
void addDiag(const Vector< Cmpt > &v)
Add to the diagonal.
Definition TensorI.H:376
Tensor< Cmpt > inv2D(const direction excludeCmpt) const
Return inverse of 2D tensor (by excluding given direction).
Definition TensorI.H:523
void rows(const Vector< Cmpt > &x, const Vector< Cmpt > &y, const Vector< Cmpt > &z)
Set row values.
Definition TensorI.H:311
components
Component labeling enumeration.
Definition Tensor.H:87
void operator=(const Vector< Vector< Cmpt > > &)
Assign to a triad of row vectors.
Definition TensorI.H:696
Tensor< label > labelType
Definition Tensor.H:68
void operator&=(const Tensor< Cmpt > &t)
Assign inner-product of this with another Tensor.
Definition TensorI.H:660
Tensor & operator=(const Tensor &)=default
Copy assignment.
Tensor< Cmpt > adjunct() const
Return adjunct matrix (transpose of cofactor matrix).
Definition TensorI.H:464
Tensor(const Vector< Cmpt > &x, const Vector< Cmpt > &y, const Vector< Cmpt > &z, const bool transposed=false)
Construct given the three row vectors, optionally treated as transposed (ie, column vectors).
Definition TensorI.H:88
Cmpt & zx() noexcept
Definition Tensor.H:209
Tensor(const Block2< Tensor< Cmpt >, BRowStart, BColStart > &block)
Construct from a block of another matrix space.
Definition TensorI.H:128
Cmpt det2D(const direction excludeCmpt) const
The 2D determinant by excluding given direction.
Definition TensorI.H:443
void operator=(const SymmTensor< Cmpt > &)
Assign to a SymmTensor.
Definition TensorI.H:687
void row(const direction r, const Vector< Cmpt > &v)
Set values of given row (0,1,2): runtime check of row.
Definition TensorI.H:344
Vector< Cmpt > row() const
Extract vector for given row: compile-time check of index.
void subtractDiag(const Vector< Cmpt > &v)
Subtract from the diagonal.
Definition TensorI.H:383
static const Tensor I
Definition Tensor.H:81
Vector< scalar > z() const
Tensor(Istream &is)
Construct from Istream.
Definition TensorI.H:137
Cmpt & zy() noexcept
Definition Tensor.H:210
Vector< Cmpt > cz() const
Extract vector for column 2.
Definition TensorI.H:181
static constexpr direction rank
Definition Tensor.H:76
Tensor(const VectorSpace< Tensor< Cmpt2 >, Cmpt2, 9 > &vs)
Construct given VectorSpace of the same rank.
Definition TensorI.H:49
const Cmpt & zy() const noexcept
Definition Tensor.H:200
const Cmpt & yx() const noexcept
Definition Tensor.H:196
void col(const direction c, const Vector< Cmpt > &v)
Set values of given column (0,1,2): runtime check of index.
Definition TensorI.H:325
Cmpt & xx() noexcept
Definition Tensor.H:203
Tensor(const Tensor &)=default
Copy construct.
Tensor< Cmpt > adjunct2D(const direction excludeCmpt) const
Return 2D adjunct matrix by excluding given direction.
Definition TensorI.H:484
Tensor< Cmpt > inner(const Tensor< Cmpt > &t2) const
Inner-product of this with another Tensor.
Definition TensorI.H:539
Cmpt & zz() noexcept
Definition Tensor.H:211
void operator=(const VectorSpace< Tensor< Cmpt2 >, Cmpt2, 9 > &)
Assign to an equivalent vector space.
Definition TensorI.H:669
Cmpt det() const
The determinate.
Definition TensorI.H:431
Tensor< Cmpt > inv() const
Return inverse.
Definition TensorI.H:581
Cmpt & yx() noexcept
Definition Tensor.H:206
Vector< Cmpt > cx() const
Extract vector for column 0.
Definition TensorI.H:167
Vector< Cmpt > row(const direction r) const
Extract vector for given row (0,1,2): runtime check of index.
Definition TensorI.H:231
Vector< Cmpt > cy() const
Extract vector for column 1.
Definition TensorI.H:174
void cols(const Vector< Cmpt > &x, const Vector< Cmpt > &y, const Vector< Cmpt > &z)
Set column values.
Definition TensorI.H:297
Vector< scalar > y() const
const Cmpt & xx() const noexcept
Definition Tensor.H:193
Tensor(const SphericalTensor< Cmpt > &st)
Construct given SphericalTensor.
Definition TensorI.H:58
Tensor< Cmpt > cof() const
Return cofactor matrix (transpose of adjunct matrix).
Definition TensorI.H:476
Tensor(const SymmTensor< Cmpt > &st)
Construct given SymmTensor.
Definition TensorI.H:67
Cmpt & xz() noexcept
Definition Tensor.H:205
Vector< Cmpt > col() const
Extract vector for given column: compile-time check of index.
Tensor(const MatrixSpace< Tensor< Cmpt2 >, Cmpt2, 3, 3 > &vs)
Construct given MatrixSpace of the same rank.
Definition TensorI.H:38
const Cmpt & yz() const noexcept
Definition Tensor.H:198
Vector< scalar > vectorComponent(const direction cmpt) const
Definition Tensor.H:450
Tensor()=default
Default construct.
Tensor< Cmpt > schur(const Tensor< Cmpt > &t2) const
Schur-product of this with another Tensor.
Definition TensorI.H:566
Tensor(const Vector< Vector< Cmpt > > &vecs, const bool transposed=false)
Construct given triad of row vectors, optionally treated as transposed (ie, column vectors).
Definition TensorI.H:77
void operator=(const SphericalTensor< Cmpt > &)
Assign to a SphericalTensor.
Definition TensorI.H:678
Vector< Cmpt > diag() const
Extract the diagonal as a vector.
Definition TensorI.H:362
const Cmpt & zz() const noexcept
Definition Tensor.H:201
Vector< Cmpt > col(const direction c) const
Extract vector for given column (0,1,2): runtime check of index.
Definition TensorI.H:201
Tensor(Foam::zero)
Construct initialized to zero.
Definition TensorI.H:29
const Cmpt & xy() const noexcept
Definition Tensor.H:194
Cmpt & yy() noexcept
Definition Tensor.H:207
const Cmpt & xz() const noexcept
Definition Tensor.H:195
const Cmpt & zx() const noexcept
Definition Tensor.H:199
Vector< scalar > x() const
Templated vector space.
Definition VectorSpace.H:75
friend Ostream & operator(Ostream &, const VectorSpace< Form, Cmpt, Ncmpts > &)
Templated 3D Vector derived from VectorSpace adding construction from 3 components,...
Definition Vector.H:61
Tensor< Cmpt > type
Definition Tensor.H:483
Tensor< solveScalar > type
Definition Tensor.H:501
The extended precision type (solveScalar for float).
Definition products.H:81
Abstract template class to provide the transpose form of a form.
Definition products.H:63
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
Namespace for OpenFOAM.
uint8_t direction
Definition direction.H:49
const direction noexcept
Definition scalarImpl.H:265
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43
A template class to specify if a data type is composed solely of Foam::label elements.
Definition contiguous.H:82
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition contiguous.H:87
A template class to specify that a data type can be considered as being contiguous in memory.
Definition contiguous.H:70