NeoN
A framework for CFD software
Loading...
Searching...
No Matches
symmTensor.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <Kokkos_Core.hpp> // IWYU pragma: keep
8
13
14
15namespace NeoN
16{
17
18
28{
29public:
30
31 KOKKOS_INLINE_FUNCTION
33 {
34 for (int k = 0; k < 6; ++k)
35 {
36 data_[k] = 0.0;
37 }
38 }
39
41 KOKKOS_INLINE_FUNCTION
42 explicit SymmTensor(const scalar diag)
43 {
44 data_[0] = diag; // xx
45 data_[1] = 0.0; // xy
46 data_[2] = 0.0; // xz
47 data_[3] = diag; // yy
48 data_[4] = 0.0; // yz
49 data_[5] = diag; // zz
50 }
51
52 KOKKOS_INLINE_FUNCTION
54 {
55 data_[0] = xx;
56 data_[1] = xy;
57 data_[2] = xz;
58 data_[3] = yy;
59 data_[4] = yz;
60 data_[5] = zz;
61 }
62
63 KOKKOS_INLINE_FUNCTION scalar* data() { return data_; }
64 KOKKOS_INLINE_FUNCTION const scalar* data() const { return data_; }
65
66 KOKKOS_INLINE_FUNCTION constexpr size_t size() const { return 6; }
67
69 KOKKOS_INLINE_FUNCTION
70 scalar operator()(const size_t i, const size_t j) const
71 {
72 // index map: (0,0)->0, (0,1)->1, (0,2)->2, (1,1)->3, (1,2)->4, (2,2)->5
73 // symmetric: (1,0)->(0,1), (2,0)->(0,2), (2,1)->(1,2)
74 static constexpr int lut[3][3] = {{0, 1, 2}, {1, 3, 4}, {2, 4, 5}};
75 return data_[lut[i][j]];
76 }
77
78 KOKKOS_INLINE_FUNCTION
79 scalar& operator()(const size_t i, const size_t j)
80 {
81 static constexpr int lut[3][3] = {{0, 1, 2}, {1, 3, 4}, {2, 4, 5}};
82 return data_[lut[i][j]];
83 }
84
85 KOKKOS_INLINE_FUNCTION
86 bool operator==(const SymmTensor& rhs) const
87 {
88 for (int k = 0; k < 6; ++k)
89 {
90 if (data_[k] != rhs.data_[k]) return false;
91 }
92 return true;
93 }
94
95 KOKKOS_INLINE_FUNCTION
97 {
98 SymmTensor res;
99 for (int k = 0; k < 6; ++k)
100 {
101 res.data_[k] = data_[k] + rhs.data_[k];
102 }
103 return res;
104 }
105
106 KOKKOS_INLINE_FUNCTION
108 {
109 for (int k = 0; k < 6; ++k)
110 {
111 data_[k] += rhs.data_[k];
112 }
113 return *this;
114 }
115
116 KOKKOS_INLINE_FUNCTION
118 {
119 SymmTensor res;
120 for (int k = 0; k < 6; ++k)
121 {
122 res.data_[k] = data_[k] - rhs.data_[k];
123 }
124 return res;
125 }
126
127 KOKKOS_INLINE_FUNCTION
129 {
130 for (int k = 0; k < 6; ++k)
131 {
132 data_[k] -= rhs.data_[k];
133 }
134 return *this;
135 }
136
137 KOKKOS_INLINE_FUNCTION
139 {
140 SymmTensor res;
141 for (int k = 0; k < 6; ++k)
142 {
143 res.data_[k] = data_[k] * s;
144 }
145 return res;
146 }
147
148 KOKKOS_INLINE_FUNCTION
150 {
151 for (int k = 0; k < 6; ++k)
152 {
153 data_[k] *= s;
154 }
155 return *this;
156 }
157
159 KOKKOS_INLINE_FUNCTION
160 scalar trace() const { return data_[0] + data_[3] + data_[5]; }
161
163 KOKKOS_INLINE_FUNCTION
165 {
166 const scalar tr3 = trace() / scalar(3);
167 return SymmTensor(
168 data_[0] - tr3, data_[1], data_[2], data_[3] - tr3, data_[4], data_[5] - tr3
169 );
170 }
171
173 KOKKOS_INLINE_FUNCTION
175 {
176 // TODO in case of trace returning a float, when NeoN_DEFINE_DP_SCALAR = OFF, this will
177 // trigger a compiler warning
178 const scalar tr23 = (2.0 / 3.0) * trace();
179 return SymmTensor(
180 data_[0] - tr23, data_[1], data_[2], data_[3] - tr23, data_[4], data_[5] - tr23
181 );
182 }
183
184private:
185
186 scalar data_[6];
187};
188
189
190KOKKOS_INLINE_FUNCTION
191SymmTensor operator*(const scalar s, const SymmTensor& st) { return st * s; }
192
194KOKKOS_INLINE_FUNCTION
195Vec3 operator&(const SymmTensor& s, const Vec3& v)
196{
197 return Vec3(
198 s(0, 0) * v[0] + s(0, 1) * v[1] + s(0, 2) * v[2],
199 s(1, 0) * v[0] + s(1, 1) * v[1] + s(1, 2) * v[2],
200 s(2, 0) * v[0] + s(2, 1) * v[1] + s(2, 2) * v[2]
201 );
202}
203
205KOKKOS_INLINE_FUNCTION
207{
208 // off-diagonal entries appear twice
209 return sqrt(
210 s(0, 0) * s(0, 0) + s(1, 1) * s(1, 1) + s(2, 2) * s(2, 2)
211 + scalar(2) * (s(0, 1) * s(0, 1) + s(0, 2) * s(0, 2) + s(1, 2) * s(1, 2))
212 );
213}
214
216KOKKOS_INLINE_FUNCTION
218{
219 return s(0, 0) * s(0, 0) + s(1, 1) * s(1, 1) + s(2, 2) * s(2, 2)
220 + scalar(2) * (s(0, 1) * s(0, 1) + s(0, 2) * s(0, 2) + s(1, 2) * s(1, 2));
221}
222
224KOKKOS_INLINE_FUNCTION
226{
227 return SymmTensor(
228 t(0, 0),
229 0.5 * (t(0, 1) + t(1, 0)),
230 0.5 * (t(0, 2) + t(2, 0)),
231 t(1, 1),
232 0.5 * (t(1, 2) + t(2, 1)),
233 t(2, 2)
234 );
235}
236
238KOKKOS_INLINE_FUNCTION
240{
241 return Tensor(
242 scalar(2) * t(0, 0),
243 t(0, 1) + t(1, 0),
244 t(0, 2) + t(2, 0),
245 t(1, 0) + t(0, 1),
246 scalar(2) * t(1, 1),
247 t(1, 2) + t(2, 1),
248 t(2, 0) + t(0, 2),
249 t(2, 1) + t(1, 2),
250 scalar(2) * t(2, 2)
251 );
252}
253
255KOKKOS_INLINE_FUNCTION
257{
258 const scalar sph = (scalar(2) / scalar(3)) * (t(0, 0) + t(1, 1) + t(2, 2));
259 return SymmTensor(
260 scalar(2) * t(0, 0) - sph,
261 t(0, 1) + t(1, 0),
262 t(0, 2) + t(2, 0),
263 scalar(2) * t(1, 1) - sph,
264 t(1, 2) + t(2, 1),
265 scalar(2) * t(2, 2) - sph
266 );
267}
268
269std::ostream& operator<<(std::ostream& out, const SymmTensor& s);
270
271
272template<>
273KOKKOS_INLINE_FUNCTION SymmTensor zero<SymmTensor>()
274{
275 return SymmTensor(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
276}
277
278template<>
279KOKKOS_INLINE_FUNCTION SymmTensor one<SymmTensor>()
280{
281 return SymmTensor(scalar(1));
282}
283
284template<>
285KOKKOS_INLINE_FUNCTION SymmTensor inv<SymmTensor>(SymmTensor)
286{
287 return SymmTensor(scalar(1));
288}
289
290
291} // namespace NeoN
A class for the representation of a symmetric 3x3 tensor.
KOKKOS_INLINE_FUNCTION bool operator==(const SymmTensor &rhs) const
KOKKOS_INLINE_FUNCTION SymmTensor(const scalar diag)
Construct scalar-times-identity symmetric tensor.
KOKKOS_INLINE_FUNCTION SymmTensor operator+(const SymmTensor &rhs) const
KOKKOS_INLINE_FUNCTION SymmTensor & operator+=(const SymmTensor &rhs)
KOKKOS_INLINE_FUNCTION SymmTensor operator*(const scalar s) const
KOKKOS_INLINE_FUNCTION scalar operator()(const size_t i, const size_t j) const
Component access — maps (i,j) to the stored upper-triangle.
KOKKOS_INLINE_FUNCTION scalar trace() const
Trace: xx + yy + zz.
KOKKOS_INLINE_FUNCTION SymmTensor & operator*=(const scalar s)
KOKKOS_INLINE_FUNCTION SymmTensor dev2() const
dev2: S - (2/3)*tr(S)*I (used in viscous stress)
KOKKOS_INLINE_FUNCTION SymmTensor dev() const
Deviatoric part: S - (1/3)*tr(S)*I.
KOKKOS_INLINE_FUNCTION SymmTensor & operator-=(const SymmTensor &rhs)
KOKKOS_INLINE_FUNCTION SymmTensor operator-(const SymmTensor &rhs) const
KOKKOS_INLINE_FUNCTION scalar * data()
KOKKOS_INLINE_FUNCTION SymmTensor(scalar xx, scalar xy, scalar xz, scalar yy, scalar yz, scalar zz)
KOKKOS_INLINE_FUNCTION scalar & operator()(const size_t i, const size_t j)
KOKKOS_INLINE_FUNCTION constexpr size_t size() const
KOKKOS_INLINE_FUNCTION const scalar * data() const
KOKKOS_INLINE_FUNCTION SymmTensor()
A class for the representation of a 3x3 tensor.
Definition tensor.hpp:26
A class for the representation of a 3D Vec3.
Definition vec3.hpp:24
Integer types used throughout NeoN.
Definition array.hpp:18
std::ostream & operator<<(std::ostream &os, const Dictionary &in)
KOKKOS_INLINE_FUNCTION scalar mag(const scalar &s)
Definition scalar.hpp:23
KOKKOS_INLINE_FUNCTION Tensor twoSymm(const Tensor &t)
T + T^T (= 2*symm(T)) as a full tensor.
KOKKOS_INLINE_FUNCTION Vec3 operator&(const SymmTensor &s, const Vec3 &v)
Symmetric matrix-vector product S·v.
KOKKOS_INLINE_FUNCTION SymmTensor operator*(const scalar s, const SymmTensor &st)
float scalar
Definition scalar.hpp:17
KOKKOS_INLINE_FUNCTION SymmTensor symm(const Tensor &t)
Symmetric part of a full tensor: ½(T + T^T)
KOKKOS_INLINE_FUNCTION SymmTensor devTwoSymm(const Tensor &t)
T + T^T - (2/3)*tr(T)*I — used for viscous stress.
KOKKOS_INLINE_FUNCTION SymmTensor one< SymmTensor >()
KOKKOS_INLINE_FUNCTION SymmTensor zero< SymmTensor >()
KOKKOS_INLINE_FUNCTION scalar magSqr(const SymmTensor &s)
Frobenius norm squared.
KOKKOS_INLINE_FUNCTION SymmTensor inv< SymmTensor >(SymmTensor)