NeoN
A framework for CFD software
Loading...
Searching...
No Matches
tensor.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
12
13
14namespace NeoN
15{
16
17
25class Tensor
26{
27public:
28
29 KOKKOS_INLINE_FUNCTION
31 {
32 for (int k = 0; k < 9; ++k)
33 {
34 data_[k] = 0.0;
35 }
36 }
37
39 KOKKOS_INLINE_FUNCTION
40 explicit Tensor(const scalar diag)
41 {
42 for (int k = 0; k < 9; ++k)
43 {
44 data_[k] = 0.0;
45 }
46 data_[0] = diag;
47 data_[4] = diag;
48 data_[8] = diag;
49 }
50
51 KOKKOS_INLINE_FUNCTION
53 scalar t00,
54 scalar t01,
55 scalar t02,
56 scalar t10,
57 scalar t11,
58 scalar t12,
59 scalar t20,
60 scalar t21,
61 scalar t22
62 )
63 {
64 data_[0] = t00;
65 data_[1] = t01;
66 data_[2] = t02;
67 data_[3] = t10;
68 data_[4] = t11;
69 data_[5] = t12;
70 data_[6] = t20;
71 data_[7] = t21;
72 data_[8] = t22;
73 }
74
75 KOKKOS_INLINE_FUNCTION scalar* data() { return data_; }
76 KOKKOS_INLINE_FUNCTION const scalar* data() const { return data_; }
77
78 constexpr size_t size() const { return 9; }
79
80 KOKKOS_INLINE_FUNCTION
81 scalar& operator()(const size_t i, const size_t j) { return data_[3 * i + j]; }
82
83 KOKKOS_INLINE_FUNCTION
84 scalar operator()(const size_t i, const size_t j) const { return data_[3 * i + j]; }
85
86 KOKKOS_INLINE_FUNCTION
87 bool operator==(const Tensor& rhs) const
88 {
89 for (int k = 0; k < 9; ++k)
90 {
91 if (data_[k] != rhs.data_[k]) return false;
92 }
93 return true;
94 }
95
96 KOKKOS_INLINE_FUNCTION
97 Tensor operator+(const Tensor& rhs) const
98 {
99 Tensor res;
100 for (int k = 0; k < 9; ++k)
101 {
102 res.data_[k] = data_[k] + rhs.data_[k];
103 }
104 return res;
105 }
106
107 KOKKOS_INLINE_FUNCTION
109 {
110 for (int k = 0; k < 9; ++k)
111 {
112 data_[k] += rhs.data_[k];
113 }
114 return *this;
115 }
116
117 KOKKOS_INLINE_FUNCTION
118 Tensor operator-(const Tensor& rhs) const
119 {
120 Tensor res;
121 for (int k = 0; k < 9; ++k)
122 {
123 res.data_[k] = data_[k] - rhs.data_[k];
124 }
125 return res;
126 }
127
128 KOKKOS_INLINE_FUNCTION
130 {
131 for (int k = 0; k < 9; ++k)
132 {
133 data_[k] -= rhs.data_[k];
134 }
135 return *this;
136 }
137
138 KOKKOS_INLINE_FUNCTION
139 Tensor operator*(const scalar s) const
140 {
141 Tensor res;
142 for (int k = 0; k < 9; ++k)
143 {
144 res.data_[k] = data_[k] * s;
145 }
146 return res;
147 }
148
149 KOKKOS_INLINE_FUNCTION
151 {
152 for (int k = 0; k < 9; ++k)
153 {
154 data_[k] *= s;
155 }
156 return *this;
157 }
158
160 KOKKOS_INLINE_FUNCTION
161 scalar trace() const { return data_[0] + data_[4] + data_[8]; }
162
164 KOKKOS_INLINE_FUNCTION
165 Tensor T() const // NOLINT
166 {
167 return Tensor(
168 data_[0], data_[3], data_[6], data_[1], data_[4], data_[7], data_[2], data_[5], data_[8]
169 );
170 }
171
172private:
173
174 scalar data_[9];
175};
176
177
178KOKKOS_INLINE_FUNCTION
179Tensor operator*(const scalar s, const Tensor& t) { return t * s; }
180
182KOKKOS_INLINE_FUNCTION
183Vec3 operator&(const Tensor& t, const Vec3& v)
184{
185 return Vec3(
186 t(0, 0) * v[0] + t(0, 1) * v[1] + t(0, 2) * v[2],
187 t(1, 0) * v[0] + t(1, 1) * v[1] + t(1, 2) * v[2],
188 t(2, 0) * v[0] + t(2, 1) * v[1] + t(2, 2) * v[2]
189 );
190}
191
193KOKKOS_INLINE_FUNCTION
194Vec3 operator&(const Vec3& v, const Tensor& t)
195{
196 return Vec3(
197 v[0] * t(0, 0) + v[1] * t(1, 0) + v[2] * t(2, 0),
198 v[0] * t(0, 1) + v[1] * t(1, 1) + v[2] * t(2, 1),
199 v[0] * t(0, 2) + v[1] * t(1, 2) + v[2] * t(2, 2)
200 );
201}
202
204KOKKOS_INLINE_FUNCTION
206{
207 scalar s = 0;
208 for (int k = 0; k < 9; ++k)
209 {
210 s += t.data()[k] * t.data()[k];
211 }
212 return sqrt(s);
213}
214
216KOKKOS_INLINE_FUNCTION
218{
219 scalar s = 0;
220 const scalar* d = t.data();
221 for (int k = 0; k < 9; ++k)
222 {
223 s += d[k] * d[k];
224 }
225 return s;
226}
227
228std::ostream& operator<<(std::ostream& out, const Tensor& t);
229
230
231template<>
232KOKKOS_INLINE_FUNCTION Tensor zero<Tensor>()
233{
234 return Tensor(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
235}
236
237template<>
238KOKKOS_INLINE_FUNCTION Tensor one<Tensor>()
239{
240 return Tensor(scalar(1));
241}
242
243template<>
244KOKKOS_INLINE_FUNCTION Tensor inv<Tensor>(Tensor)
245{
246 // Placeholder — matrix inverse not needed for field arithmetic
247 return Tensor(scalar(1));
248}
249
250
251} // namespace NeoN
A class for the representation of a 3x3 tensor.
Definition tensor.hpp:26
KOKKOS_INLINE_FUNCTION const scalar * data() const
Definition tensor.hpp:76
KOKKOS_INLINE_FUNCTION Tensor operator+(const Tensor &rhs) const
Definition tensor.hpp:97
KOKKOS_INLINE_FUNCTION bool operator==(const Tensor &rhs) const
Definition tensor.hpp:87
KOKKOS_INLINE_FUNCTION scalar * data()
Definition tensor.hpp:75
KOKKOS_INLINE_FUNCTION Tensor(scalar t00, scalar t01, scalar t02, scalar t10, scalar t11, scalar t12, scalar t20, scalar t21, scalar t22)
Definition tensor.hpp:52
KOKKOS_INLINE_FUNCTION Tensor operator-(const Tensor &rhs) const
Definition tensor.hpp:118
KOKKOS_INLINE_FUNCTION Tensor()
Definition tensor.hpp:30
KOKKOS_INLINE_FUNCTION Tensor & operator-=(const Tensor &rhs)
Definition tensor.hpp:129
KOKKOS_INLINE_FUNCTION Tensor & operator*=(const scalar s)
Definition tensor.hpp:150
KOKKOS_INLINE_FUNCTION Tensor(const scalar diag)
Construct scalar-times-identity tensor.
Definition tensor.hpp:40
constexpr size_t size() const
Definition tensor.hpp:78
KOKKOS_INLINE_FUNCTION scalar trace() const
Trace: sum of diagonal components.
Definition tensor.hpp:161
KOKKOS_INLINE_FUNCTION scalar operator()(const size_t i, const size_t j) const
Definition tensor.hpp:84
KOKKOS_INLINE_FUNCTION scalar & operator()(const size_t i, const size_t j)
Definition tensor.hpp:81
KOKKOS_INLINE_FUNCTION Tensor T() const
Transpose.
Definition tensor.hpp:165
KOKKOS_INLINE_FUNCTION Tensor & operator+=(const Tensor &rhs)
Definition tensor.hpp:108
KOKKOS_INLINE_FUNCTION Tensor operator*(const scalar s) const
Definition tensor.hpp:139
A class for the representation of a 3D Vec3.
Definition vec3.hpp:24
Integer types used throughout NeoN.
Definition array.hpp:18
KOKKOS_INLINE_FUNCTION Tensor one< Tensor >()
Definition tensor.hpp:238
std::ostream & operator<<(std::ostream &os, const Dictionary &in)
KOKKOS_INLINE_FUNCTION Tensor inv< Tensor >(Tensor)
Definition tensor.hpp:244
KOKKOS_INLINE_FUNCTION Tensor zero< Tensor >()
Definition tensor.hpp:232
KOKKOS_INLINE_FUNCTION scalar mag(const scalar &s)
Definition scalar.hpp:23
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 scalar magSqr(const SymmTensor &s)
Frobenius norm squared.