NeoFOAM
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoFOAM authors
3
4#pragma once
5
6#include <Kokkos_Core.hpp> // IWYU pragma: keep
7
10
11namespace NeoFOAM
12{
13
14
20class Vector
21{
22public:
23
24 KOKKOS_INLINE_FUNCTION
26 {
27 cmpts_[0] = 0.0;
28 cmpts_[1] = 0.0;
29 cmpts_[2] = 0.0;
30 }
31
32 KOKKOS_INLINE_FUNCTION
34 {
35 cmpts_[0] = x;
36 cmpts_[1] = y;
37 cmpts_[2] = z;
38 }
39
45 scalar* data() { return cmpts_; }
46
52 const scalar* data() const { return cmpts_; }
53
59 constexpr size_t size() const { return 3; }
60
61 KOKKOS_INLINE_FUNCTION
62 scalar& operator[](const size_t i) { return cmpts_[i]; }
63
64 KOKKOS_INLINE_FUNCTION
65 scalar operator[](const size_t i) const { return cmpts_[i]; }
66
67 KOKKOS_INLINE_FUNCTION
68 scalar& operator()(const size_t i) { return cmpts_[i]; }
69
70 KOKKOS_INLINE_FUNCTION
71 scalar operator()(const size_t i) const { return cmpts_[i]; }
72
73 KOKKOS_INLINE_FUNCTION
74 bool operator==(const Vector& rhs) const
75 {
76 return cmpts_[0] == rhs(0) && cmpts_[1] == rhs(1) && cmpts_[2] == rhs(2);
77 }
78
79 KOKKOS_INLINE_FUNCTION
80 Vector operator+(const Vector& rhs) const
81 {
82 return Vector(cmpts_[0] + rhs(0), cmpts_[1] + rhs(1), cmpts_[2] + rhs(2));
83 }
84
85 KOKKOS_INLINE_FUNCTION
87 {
88 cmpts_[0] += rhs(0);
89 cmpts_[1] += rhs(1);
90 cmpts_[2] += rhs(2);
91 return *this;
92 }
93
94 KOKKOS_INLINE_FUNCTION
95 Vector operator-(const Vector& rhs) const
96 {
97 return Vector(cmpts_[0] - rhs(0), cmpts_[1] - rhs(1), cmpts_[2] - rhs(2));
98 }
99
100 KOKKOS_INLINE_FUNCTION
102 {
103 cmpts_[0] -= rhs(0);
104 cmpts_[1] -= rhs(1);
105 cmpts_[2] -= rhs(2);
106 return *this;
107 }
108
109 KOKKOS_INLINE_FUNCTION
110 Vector operator*(const scalar& rhs) const
111 {
112 return Vector(cmpts_[0] * rhs, cmpts_[1] * rhs, cmpts_[2] * rhs);
113 }
114
115
116 KOKKOS_INLINE_FUNCTION
117 Vector operator*(const label& rhs) const
118 {
119 return Vector(cmpts_[0] * rhs, cmpts_[1] * rhs, cmpts_[2] * rhs);
120 }
121
122
123 KOKKOS_INLINE_FUNCTION
125 {
126 cmpts_[0] *= rhs;
127 cmpts_[1] *= rhs;
128 cmpts_[2] *= rhs;
129 return *this;
130 }
131
132private:
133
134 scalar cmpts_[3];
135};
136
137
138KOKKOS_INLINE_FUNCTION
139Vector operator*(const scalar& sclr, Vector rhs)
140{
141 rhs *= sclr;
142 return rhs;
143}
144
145KOKKOS_INLINE_FUNCTION
147{
148 return Vector(rhs[0] * lhs[0], rhs[1] * lhs[1], rhs[2] * lhs[2]);
149}
150
151KOKKOS_INLINE_FUNCTION
152scalar mag(const Vector& vec) { return sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]); }
153
154std::ostream& operator<<(std::ostream& out, const Vector& vec);
155
156} // namespace NeoFOAM
A class for the representation of a 3D Vector.
Definition vector.hpp:21
KOKKOS_INLINE_FUNCTION Vector & operator+=(const Vector &rhs)
Definition vector.hpp:86
KOKKOS_INLINE_FUNCTION Vector(scalar x, scalar y, scalar z)
Definition vector.hpp:33
KOKKOS_INLINE_FUNCTION scalar operator[](const size_t i) const
Definition vector.hpp:65
KOKKOS_INLINE_FUNCTION scalar & operator()(const size_t i)
Definition vector.hpp:68
KOKKOS_INLINE_FUNCTION bool operator==(const Vector &rhs) const
Definition vector.hpp:74
scalar * data()
Returns pointer to the data of the vector.
Definition vector.hpp:45
KOKKOS_INLINE_FUNCTION Vector & operator*=(const scalar &rhs)
Definition vector.hpp:124
constexpr size_t size() const
Returns the size of the vector.
Definition vector.hpp:59
KOKKOS_INLINE_FUNCTION Vector()
Definition vector.hpp:25
KOKKOS_INLINE_FUNCTION scalar operator()(const size_t i) const
Definition vector.hpp:71
KOKKOS_INLINE_FUNCTION Vector operator*(const scalar &rhs) const
Definition vector.hpp:110
KOKKOS_INLINE_FUNCTION Vector operator*(const label &rhs) const
Definition vector.hpp:117
KOKKOS_INLINE_FUNCTION scalar & operator[](const size_t i)
Definition vector.hpp:62
KOKKOS_INLINE_FUNCTION Vector & operator-=(const Vector &rhs)
Definition vector.hpp:101
const scalar * data() const
Returns pointer to the data of the vector.
Definition vector.hpp:52
KOKKOS_INLINE_FUNCTION Vector operator+(const Vector &rhs) const
Definition vector.hpp:80
KOKKOS_INLINE_FUNCTION Vector operator-(const Vector &rhs) const
Definition vector.hpp:95
float scalar
Definition scalar.hpp:11
KOKKOS_INLINE_FUNCTION scalar mag(const Vector &vec)
Definition vector.hpp:152
std::ostream & operator<<(std::ostream &out, const Vector &vec)
int32_t label
Definition label.hpp:13
KOKKOS_INLINE_FUNCTION Vector operator*(const scalar &sclr, Vector rhs)
Definition vector.hpp:139
KOKKOS_INLINE_FUNCTION Vector operator&(const Vector &lhs, Vector rhs)
Definition vector.hpp:146