OpenLB 1.7
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2015 Asher Zarth, Mathias J. Krause, Albert Mink
4 * 2020 Adrian Kummerlaender
5 * E-mail contact: info@openlb.net
6 * The most recent release of OpenLB can be downloaded at
7 * <http://www.openlb.net/>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23*/
24
25
30#ifndef VECTOR_H
31#define VECTOR_H
32
33#include <cstring>
34#include <type_traits>
35#include <array>
36
37#include "scalarVector.h"
38#include "utilities/omath.h"
40#include "olbDebug.h"
41
42namespace olb {
43
44
46template <typename T, unsigned D>
47class Vector : public ScalarVector<T,D,Vector<T,D>> {
48private:
49 std::array<T,D> _data;
50
52
53protected:
54 constexpr const T* getComponentPointer(unsigned iDim) const any_platform
55 {
56 return &_data[iDim];
57 }
58 constexpr T* getComponentPointer(unsigned iDim) any_platform
59 {
60 return &_data[iDim];
61 }
62
63public:
64 using value_t = T;
65
66 constexpr Vector() any_platform:
67 _data{}
68 { }
69
70 constexpr Vector(const Vector& rhs) any_platform:
71 _data(rhs._data)
72 { }
73
74 constexpr Vector(Vector&& rhs) any_platform:
75 _data(rhs._data)
76 { }
77
78 template <typename W, typename IMPL>
80 {
81 for (unsigned iDim=0; iDim < D; ++iDim) {
82 _data[iDim] = rhs[iDim];
83 }
84 }
85
86 template <typename... I,
87 typename std::enable_if_t< (sizeof...(I) > 1
89 && sizeof...(I) == D), int> = 0>
90 constexpr Vector(I... indexes) any_platform:
91 _data{T(indexes)...}
92 { }
93
94 // Should be declared explicit
95 template <typename U>
96 constexpr Vector(const U* v) any_platform
97 {
98 for (unsigned iDim=0; iDim < D; ++iDim) {
99 _data[iDim] = static_cast<T>(v[iDim]);
100 }
101 }
102
103 // Should be declared explicit
104 constexpr Vector(const std::vector<T>& v)
105 {
106 OLB_PRECONDITION(v.size() == D);
107 for (unsigned iDim=0; iDim < D; ++iDim) {
108 _data[iDim] = v[iDim];
109 }
110 }
111
112 #ifndef __CUDA_ARCH__
113 constexpr Vector(std::initializer_list<T> v)
114 {
115 OLB_PRECONDITION(v.size() == D);
116 std::copy(v.begin(), v.end(), _data.begin());
117 }
118 #endif
119
120 constexpr Vector(T scalar) any_platform
121 {
122 for (unsigned iDim=0; iDim < D; ++iDim) {
123 _data[iDim] = scalar;
124 }
125 }
126
128 template <typename F, typename = decltype(std::declval<F&>()(std::size_t{0}))>
129 constexpr Vector(F&& f) any_platform
130 {
131 for (unsigned iDim=0; iDim < D; ++iDim) {
132 _data[iDim] = f(iDim);
133 }
134 }
135
136 template <typename U, typename IMPL_>
138 {
139 for (unsigned iDim=0; iDim < D; ++iDim) {
140 this->operator[](iDim) = rhs[iDim];
141 }
142 return *this;
143 }
144
145 constexpr Vector& operator = (const Vector& rhs) any_platform
146 {
147 for (unsigned iDim=0; iDim < D; ++iDim) {
148 _data[iDim] = rhs[iDim];
149 }
150 return *this;
151 }
152
153 constexpr Vector& operator = (const Vector&& rhs) any_platform
154 {
155 for (unsigned iDim=0; iDim < D; ++iDim) {
156 _data[iDim] = rhs[iDim];
157 }
158 return *this;
159 }
160
161 constexpr const T* data() const any_platform
162 {
163 return _data.data();
164 }
165
166 constexpr T* data() any_platform
167 {
168 return _data.data();
169 }
170
171 constexpr auto begin()
172 {
173 return _data.begin();
174 }
175
176 constexpr auto end()
177 {
178 return _data.end();
179 }
180
181 constexpr int getDim() const any_platform
182 {
183 return D;
184 }
185
186 constexpr Vector<T,D+1> withPrefix(T prefix) const any_platform
187 {
188 Vector<T,D+1> tmp;
189 tmp[0] = prefix;
190 for (unsigned iDim=0; iDim < D; ++iDim) {
191 tmp[iDim+1] = _data[iDim];
192 }
193 return tmp;
194 }
195
196 constexpr std::size_t getNblock() const { return 1; }
197 constexpr std::size_t getSerializableSize() const
198 {
199 return D * sizeof(T);
200 };
201 bool* getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode)
202 {
203 std::size_t currentBlock = 0;
204 bool* dataPtr = nullptr;
205 //registerVar(iBlock, sizeBlock, currentBlock, dataPtr, *_data.data(), D);
206 if (iBlock == currentBlock) {
207 sizeBlock = sizeof(T) * D;
208 dataPtr = (bool*) _data.data();
209 }
210 currentBlock++;
211 return dataPtr;
212 }
213
214};
215
216template <typename T, typename IMPL, typename IMPL_>
217constexpr T crossProduct2D(
219{
220 return (a[0]*b[1] - a[1]*b[0]);
221}
222
223template <typename T, typename IMPL, typename IMPL_>
226{
227 return Vector<T,3>(
228 a[1]*b[2] - a[2]*b[1],
229 a[2]*b[0] - a[0]*b[2],
230 a[0]*b[1] - a[1]*b[0]
231 );
232}
233
234template <typename T, unsigned D, typename IMPL, typename IMPL_>
236 static_assert((D==2 || D==3), "ERROR: Unknown dimension!");
237 if constexpr (D==2){
238 return crossProduct2D(a,b);
239 } else {
240 return crossProduct3D(a,b);
241 }
242}
243
244template <typename T, unsigned D, typename IMPL>
245constexpr Vector<T,D> normalize(const ScalarVector<T,D,IMPL>& a, T scale = T{1})
246{
247 T invScale (scale / norm(a));
248 return Vector<T,D>([invScale,&a](unsigned iDim) -> T {
249 return a[iDim] * invScale;
250 });
251}
252
253template <typename T, unsigned D, typename IMPL>
255{
256 using namespace util;
257 return Vector<T,D>([&a](unsigned iDim) -> T {
258 return abs(a[iDim]);
259 });
260}
261
262
263template <typename T, unsigned D, typename U, typename IMPL>
264constexpr meta::enable_if_arithmetic_t<U, Vector<T,D>>
266{
267 return Vector<T,D>(b) += a;
268}
269
270template <typename T, unsigned D, typename U, typename IMPL>
271constexpr meta::enable_if_arithmetic_t<U, Vector<T,D>>
273{
274 return Vector<T,D>(a) += b;
275}
276
277template <typename T, unsigned D, typename IMPL, typename IMPL_>
280{
281 return Vector<T,D>(a) += b;
282}
283
284template <typename T, typename W, unsigned D, typename IMPL, typename IMPL_>
285constexpr Vector<decltype(T{}+W{}),D> operator+ (
286 const ScalarVector<T,D,IMPL>& a, const ScalarVector<W,D,IMPL_>& b) any_platform
287{
288 Vector<decltype(T{}+W{}),D> result;
289 for (unsigned iDim=0; iDim < D; ++iDim) {
290 result[iDim] = a[iDim] + b[iDim];
291 }
292 return result;
293}
294
295template <typename T, unsigned D, typename U, typename IMPL>
296constexpr meta::enable_if_arithmetic_t<U, Vector<T,D>>
298{
299 return Vector<T,D>(a) - b;
300}
301
302template <typename T, unsigned D, typename U, typename IMPL>
303constexpr meta::enable_if_arithmetic_t<U, Vector<T,D>>
305{
306 return Vector<T,D>(a) -= b;
307}
308
309template <typename T, unsigned D, typename IMPL, typename IMPL_>
312{
313 return Vector<T,D>(a) -= b;
314}
315
316template <typename T, typename W, unsigned D, typename IMPL, typename IMPL_>
317constexpr Vector<decltype(T{}-W{}),D> operator- (
318 const ScalarVector<T,D,IMPL>& a, const ScalarVector<W,D,IMPL_>& b) any_platform
319{
320 Vector<decltype(T{}-W{}),D> result;
321 for (unsigned iDim=0; iDim < D; ++iDim) {
322 result[iDim] = a[iDim] - b[iDim];
323 }
324 return result;
325}
326
327template <typename T, unsigned D, typename U, typename IMPL>
328constexpr meta::enable_if_arithmetic_t<U, Vector<decltype(T{}*U{}),D>>
329operator* (U a, const ScalarVector<T,D,IMPL>& b) any_platform
330{
331 Vector<decltype(T{}*U{}),D> result(b);
332 return result *= a;
333}
334
335template <typename T, unsigned D, typename U, typename IMPL>
336constexpr meta::enable_if_arithmetic_t<U, Vector<decltype(T{}*U{}),D>>
337operator* (const ScalarVector<T,D,IMPL>& a, U b) any_platform
338{
339 Vector<decltype(T{}*U{}),D> result(a);
340 return result *= b;
341}
342
344template <typename T, typename U, unsigned D, typename IMPL, typename IMPL_>
345constexpr auto operator* (
347{
348 decltype(T{}*U{}) scalarProduct{};
349 for (unsigned iDim=0; iDim < D; ++iDim) {
350 scalarProduct += a[iDim] * b[iDim];
351 }
352 return scalarProduct;
353}
354
355template <typename T, unsigned D, typename U, typename IMPL>
356constexpr meta::enable_if_arithmetic_t<U, Vector<T,D>>
358{
359 return Vector<T,D>(a) /= b;
360}
361
362template<typename T, unsigned D, typename U, typename IMPL>
365{
366 return Vector<U,D>(lhs) < rhs;
367}
368
369template<typename T, unsigned D, typename U, typename IMPL>
370constexpr meta::enable_if_arithmetic_t<U, bool>
372{
373 return lhs > Vector<U,D>(rhs);
374}
375
376template<typename T, unsigned D, typename U, typename IMPL>
379{
380 return Vector<U,D>(lhs) <= rhs;
381}
382
383template<typename T, unsigned D, typename U, typename IMPL>
384constexpr meta::enable_if_arithmetic_t<U, bool>
386{
387 return lhs >= Vector<U,D>(rhs);
388}
389
390template <typename T, unsigned D, typename IMPL, typename IMPL_>
393{
394 return Vector<T,D>([&v,&w](unsigned iDim) -> T {
395 return util::min(v[iDim], w[iDim]);
396 });
397}
398
399template <typename T, unsigned D, typename IMPL, typename IMPL_>
402{
403 return Vector<T,D>([&v,&w](unsigned iDim) -> T {
404 return util::max(v[iDim], w[iDim]);
405 });
406}
407
408
409} // end namespace olb
410
411#endif
Plain old scalar vector.
Definition vector.h:47
constexpr Vector< T, D+1 > withPrefix(T prefix) const any_platform
Definition vector.h:186
constexpr Vector() any_platform
Definition vector.h:66
constexpr Vector(F &&f) any_platform
Construct with entries given by a lambda expression.
Definition vector.h:129
constexpr const T * data() const any_platform
Definition vector.h:161
constexpr std::size_t getNblock() const
Definition vector.h:196
constexpr Vector(const std::vector< T > &v)
Definition vector.h:104
bool * getBlock(std::size_t iBlock, std::size_t &sizeBlock, bool loadingMode)
Definition vector.h:201
constexpr Vector(I... indexes) any_platform
Definition vector.h:90
constexpr Vector & operator=(const GenericVector< U, D, IMPL_ > &rhs) any_platform
Definition vector.h:137
constexpr T * data() any_platform
Definition vector.h:166
constexpr int getDim() const any_platform
Definition vector.h:181
constexpr Vector(std::initializer_list< T > v)
Definition vector.h:113
constexpr const T * getComponentPointer(unsigned iDim) const any_platform
Definition vector.h:54
constexpr Vector(const Vector &rhs) any_platform
Definition vector.h:70
constexpr std::size_t getSerializableSize() const
Definition vector.h:197
constexpr auto begin()
Definition vector.h:171
constexpr Vector(const U *v) any_platform
Definition vector.h:96
constexpr Vector(T scalar) any_platform
Definition vector.h:120
constexpr auto end()
Definition vector.h:176
constexpr Vector(Vector &&rhs) any_platform
Definition vector.h:74
constexpr Vector(const ScalarVector< W, D, IMPL > &rhs) any_platform
Definition vector.h:79
constexpr T * getComponentPointer(unsigned iDim) any_platform
Definition vector.h:58
typename std::integral_constant< bool, std::is_base_of_v< AD, T >||std::is_base_of_v< SimdBase, T >||std::is_base_of_v< ExprBase, T >||std::is_arithmetic_v< T >||std::is_pointer_v< T >||std::is_enum_v< T > > is_arithmetic
Checks whether T can be used as a scalar arithmetic type.
Definition meta.h:109
std::enable_if_t< is_arithmetic< T >::type::value, U > enable_if_arithmetic_t
Definition meta.h:121
cpu::simd::Pack< T > min(cpu::simd::Pack< T > rhs, cpu::simd::Pack< T > lhs)
Definition pack.h:124
cpu::simd::Pack< T > max(cpu::simd::Pack< T > rhs, cpu::simd::Pack< T > lhs)
Definition pack.h:130
Top level namespace for all of OpenLB.
constexpr Vector< T, D > minv(const ScalarVector< T, D, IMPL > &v, const ScalarVector< T, D, IMPL_ > &w)
Definition vector.h:391
constexpr meta::enable_if_arithmetic_t< U, Vector< T, D > > operator-(U a, const ScalarVector< T, D, IMPL > &b) any_platform
Definition vector.h:297
constexpr bool operator<(const ScalarVector< T, D, IMPL > &lhs, const ScalarVector< U, D, IMPL_ > &rhs)
Returns true if all lhs components are smaller than rhs.
std::enable_if_t< std::is_arithmetic< T >::type::value, T > abs(T x)
Definition util.h:396
constexpr bool operator>=(const ScalarVector< T, D, IMPL > &lhs, const ScalarVector< U, D, IMPL_ > &rhs)
Returns true if all lhs components are smaller or equal than rhs.
constexpr Vector< T, D > maxv(const ScalarVector< T, D, IMPL > &v, const ScalarVector< T, D, IMPL_ > &w)
Definition vector.h:400
constexpr T norm(const ScalarVector< T, D, IMPL > &a)
Euclidean vector norm.
auto crossProduct(const ScalarVector< T, D, IMPL > &a, const ScalarVector< T, D, IMPL_ > &b)
Definition vector.h:235
constexpr T crossProduct2D(const ScalarVector< T, 2, IMPL > &a, const ScalarVector< T, 2, IMPL_ > &b)
Definition vector.h:217
constexpr meta::enable_if_arithmetic_t< U, Vector< T, D > > operator+(U a, const ScalarVector< T, D, IMPL > &b) any_platform
Definition vector.h:265
constexpr meta::enable_if_arithmetic_t< U, Vector< decltype(T{} *U{}), D > > operator*(U a, const ScalarVector< T, D, IMPL > &b) any_platform
Definition vector.h:329
constexpr meta::enable_if_arithmetic_t< U, Vector< T, D > > operator/(const ScalarVector< T, D, IMPL > &a, U b) any_platform
Definition vector.h:357
constexpr Vector< T, 3 > crossProduct3D(const ScalarVector< T, 3, IMPL > &a, const ScalarVector< T, 3, IMPL_ > &b) any_platform
Definition vector.h:224
constexpr bool operator>(const ScalarVector< T, D, IMPL > &lhs, const ScalarVector< U, D, IMPL_ > &rhs)
Returns true if all lhs components are greater than rhs.
constexpr Vector< T, D > normalize(const ScalarVector< T, D, IMPL > &a, T scale=T{1})
Definition vector.h:245
constexpr bool operator<=(const ScalarVector< T, D, IMPL > &lhs, const ScalarVector< U, D, IMPL_ > &rhs)
Returns true if all lhs components are smaller or equal than rhs.
#define OLB_PRECONDITION(COND)
Definition olbDebug.h:46
#define any_platform
Define preprocessor macros for device-side functions, constant storage.
Definition platform.h:78
Generic vector of values supporting basic arithmetic.
constexpr const T & operator[](unsigned iDim) const any_platform
Vector of scalars.
GenericVector< T, D, Vector< T, D > > type