NeoN
A framework for CFD software
Loading...
Searching...
No Matches
vec3.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 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
23class Vec3
24{
25public:
26
27 KOKKOS_INLINE_FUNCTION
29 {
30 cmpts_[0] = 0.0;
31 cmpts_[1] = 0.0;
32 cmpts_[2] = 0.0;
33 }
34
35 KOKKOS_INLINE_FUNCTION
37 {
38 cmpts_[0] = x;
39 cmpts_[1] = y;
40 cmpts_[2] = z;
41 }
42
43 KOKKOS_INLINE_FUNCTION
44 explicit Vec3(const scalar constValue)
45 {
46 cmpts_[0] = constValue;
47 cmpts_[1] = constValue;
48 cmpts_[2] = constValue;
49 }
50
56 scalar* data() { return cmpts_; }
57
63 const scalar* data() const { return cmpts_; }
64
70 constexpr size_t size() const { return 3; }
71
72 KOKKOS_INLINE_FUNCTION
73 scalar& operator[](const size_t i) { return cmpts_[i]; }
74
75 KOKKOS_INLINE_FUNCTION
76 scalar operator[](const size_t i) const { return cmpts_[i]; }
77
78 KOKKOS_INLINE_FUNCTION
79 scalar& operator()(const size_t i) { return cmpts_[i]; }
80
81 KOKKOS_INLINE_FUNCTION
82 scalar operator()(const size_t i) const { return cmpts_[i]; }
83
84 KOKKOS_INLINE_FUNCTION
85 bool operator==(const Vec3& rhs) const
86 {
87 return cmpts_[0] == rhs(0) && cmpts_[1] == rhs(1) && cmpts_[2] == rhs(2);
88 }
89
90 KOKKOS_INLINE_FUNCTION
91 Vec3 operator+(const Vec3& rhs) const
92 {
93 return Vec3(cmpts_[0] + rhs(0), cmpts_[1] + rhs(1), cmpts_[2] + rhs(2));
94 }
95
96 KOKKOS_INLINE_FUNCTION
97 Vec3& operator+=(const Vec3& rhs)
98 {
99 cmpts_[0] += rhs(0);
100 cmpts_[1] += rhs(1);
101 cmpts_[2] += rhs(2);
102 return *this;
103 }
104
105 KOKKOS_INLINE_FUNCTION
106 Vec3 operator-(const Vec3& rhs) const
107 {
108 return Vec3(cmpts_[0] - rhs(0), cmpts_[1] - rhs(1), cmpts_[2] - rhs(2));
109 }
110
111 KOKKOS_INLINE_FUNCTION
112 Vec3& operator-=(const Vec3& rhs)
113 {
114 cmpts_[0] -= rhs(0);
115 cmpts_[1] -= rhs(1);
116 cmpts_[2] -= rhs(2);
117 return *this;
118 }
119
120 KOKKOS_INLINE_FUNCTION
121 Vec3 operator*(const scalar& rhs) const
122 {
123 return Vec3(cmpts_[0] * rhs, cmpts_[1] * rhs, cmpts_[2] * rhs);
124 }
125
126
127 KOKKOS_INLINE_FUNCTION
128 Vec3 operator*(const label& rhs) const
129 {
130 return Vec3(cmpts_[0] * rhs, cmpts_[1] * rhs, cmpts_[2] * rhs);
131 }
132
133
134 KOKKOS_INLINE_FUNCTION
135 Vec3& operator*=(const scalar& rhs)
136 {
137 cmpts_[0] *= rhs;
138 cmpts_[1] *= rhs;
139 cmpts_[2] *= rhs;
140 return *this;
141 }
142
143private:
144
145 scalar cmpts_[3];
146};
147
148
149KOKKOS_INLINE_FUNCTION
150Vec3 operator*(const scalar& sclr, Vec3 rhs)
151{
152 rhs *= sclr;
153 return rhs;
154}
155
156// TODO replace by compProd, innerProd, outerProd
158KOKKOS_INLINE_FUNCTION
159Vec3 operator*(const Vec3& lhs, Vec3 rhs)
160{
161 return {lhs[0] * rhs[0], lhs[1] * rhs[1], lhs[2] * rhs[2]};
162}
163
165KOKKOS_INLINE_FUNCTION
166scalar operator&(const Vec3& lhs, Vec3 rhs)
167{
168 return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
169}
170
172KOKKOS_INLINE_FUNCTION
173Vec3 operator^(const Vec3& lhs, Vec3 rhs)
174{
175 return {
176 lhs[1] * rhs[2] - lhs[2] * rhs[1],
177 lhs[2] * rhs[0] - lhs[0] * rhs[2],
178 lhs[0] * rhs[1] - lhs[1] * rhs[0]
179 };
180}
181
184KOKKOS_INLINE_FUNCTION
185Vec3 cross(const Vec3& lhs, const Vec3& rhs) { return lhs ^ rhs; }
186
187
188KOKKOS_INLINE_FUNCTION
189Vec3 operator/(const Vec3& lhs, scalar rhs) { return {lhs[0] / rhs, lhs[1] / rhs, lhs[2] / rhs}; }
190
191
192KOKKOS_INLINE_FUNCTION
193scalar mag(const Vec3& vec) { return sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]); }
194
195std::ostream& operator<<(std::ostream& out, const Vec3& vec);
196
197
198template<>
199KOKKOS_INLINE_FUNCTION Vec3 one<Vec3>()
200{
201 return Vec3(1.0, 1.0, 1.0);
202}
203
204template<>
205KOKKOS_INLINE_FUNCTION Vec3 zero<Vec3>()
206{
207 return Vec3(0.0, 0.0, 0.0);
208}
209
210template<>
211KOKKOS_INLINE_FUNCTION Vec3 inv<Vec3>(Vec3 in)
212{
213 return Vec3(1.0 / in[0], 1.0 / in[1], 1.0 / in[2]);
214}
215
216
217} // namespace NeoN
A class for the representation of a 3D Vec3.
Definition vec3.hpp:24
scalar * data()
Returns pointer to the data of the vector.
Definition vec3.hpp:56
KOKKOS_INLINE_FUNCTION Vec3 operator-(const Vec3 &rhs) const
Definition vec3.hpp:106
KOKKOS_INLINE_FUNCTION Vec3 operator+(const Vec3 &rhs) const
Definition vec3.hpp:91
KOKKOS_INLINE_FUNCTION scalar & operator()(const size_t i)
Definition vec3.hpp:79
KOKKOS_INLINE_FUNCTION Vec3 & operator-=(const Vec3 &rhs)
Definition vec3.hpp:112
KOKKOS_INLINE_FUNCTION Vec3 & operator+=(const Vec3 &rhs)
Definition vec3.hpp:97
constexpr size_t size() const
Returns the size of the vector.
Definition vec3.hpp:70
KOKKOS_INLINE_FUNCTION Vec3(const scalar constValue)
Definition vec3.hpp:44
KOKKOS_INLINE_FUNCTION Vec3 & operator*=(const scalar &rhs)
Definition vec3.hpp:135
KOKKOS_INLINE_FUNCTION Vec3()
Definition vec3.hpp:28
KOKKOS_INLINE_FUNCTION Vec3 operator*(const scalar &rhs) const
Definition vec3.hpp:121
const scalar * data() const
Returns pointer to the data of the vector.
Definition vec3.hpp:63
KOKKOS_INLINE_FUNCTION bool operator==(const Vec3 &rhs) const
Definition vec3.hpp:85
KOKKOS_INLINE_FUNCTION Vec3(scalar x, scalar y, scalar z)
Definition vec3.hpp:36
KOKKOS_INLINE_FUNCTION scalar & operator[](const size_t i)
Definition vec3.hpp:73
KOKKOS_INLINE_FUNCTION scalar operator[](const size_t i) const
Definition vec3.hpp:76
KOKKOS_INLINE_FUNCTION scalar operator()(const size_t i) const
Definition vec3.hpp:82
KOKKOS_INLINE_FUNCTION Vec3 operator*(const label &rhs) const
Definition vec3.hpp:128
Integer types used throughout NeoN.
Definition array.hpp:18
std::ostream & operator<<(std::ostream &os, const Dictionary &in)
KOKKOS_INLINE_FUNCTION Vec3 zero< Vec3 >()
Definition vec3.hpp:205
KOKKOS_INLINE_FUNCTION Vec3 one< Vec3 >()
Definition vec3.hpp:199
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 Vec3 inv< Vec3 >(Vec3 in)
Definition vec3.hpp:211
KOKKOS_INLINE_FUNCTION SymmTensor operator*(const scalar s, const SymmTensor &st)
float scalar
Definition scalar.hpp:17
KOKKOS_INLINE_FUNCTION Vec3 operator^(const Vec3 &lhs, Vec3 rhs)
cross (vector) product, right-handed: lhs ^ rhs
Definition vec3.hpp:173
KOKKOS_INLINE_FUNCTION Vec3 cross(const Vec3 &lhs, const Vec3 &rhs)
cross (vector) product, spelled out for call sites that read better as a named function than as ^
Definition vec3.hpp:185
KOKKOS_INLINE_FUNCTION Vec3 operator/(const Vec3 &lhs, scalar rhs)
Definition vec3.hpp:189
int32_t label
Definition label.hpp:44