NeoN
A framework for CFD software
Loading...
Searching...
No Matches
vec3.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2025 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
156KOKKOS_INLINE_FUNCTION
157scalar operator&(const Vec3& lhs, Vec3 rhs)
158{
159 return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
160}
161
162KOKKOS_INLINE_FUNCTION
163scalar mag(const Vec3& vec) { return sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]); }
164
165std::ostream& operator<<(std::ostream& out, const Vec3& vec);
166
167
168template<>
169KOKKOS_INLINE_FUNCTION Vec3 one<Vec3>()
170{
171 return Vec3(1.0, 1.0, 1.0);
172}
173
174template<>
175KOKKOS_INLINE_FUNCTION Vec3 zero<Vec3>()
176{
177 return Vec3(0.0, 0.0, 0.0);
178}
179
180} // 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
Definition array.hpp:20
KOKKOS_INLINE_FUNCTION scalar operator&(const Vec3 &lhs, Vec3 rhs)
Definition vec3.hpp:157
std::ostream & operator<<(std::ostream &os, const Dictionary &in)
KOKKOS_INLINE_FUNCTION Vec3 zero< Vec3 >()
Definition vec3.hpp:175
KOKKOS_INLINE_FUNCTION Vec3 one< Vec3 >()
Definition vec3.hpp:169
KOKKOS_INLINE_FUNCTION scalar mag(const scalar &s)
Definition scalar.hpp:22
KOKKOS_INLINE_FUNCTION Vec3 operator*(const scalar &sclr, Vec3 rhs)
Definition vec3.hpp:150
float scalar
Definition scalar.hpp:16
int32_t label
Definition label.hpp:26