OpenLB 1.7
Loading...
Searching...
No Matches
scalarVector.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2020 Adrian Kummerlaender
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24
25#ifndef SCALAR_VECTOR_H
26#define SCALAR_VECTOR_H
27
28#include <vector>
29#include <limits>
30#include <iostream>
31#include <type_traits>
32
33#include "genericVector.h"
34#include "meta.h"
35#include "utilities/omath.h"
37
38namespace olb {
39
40
42template<typename T, unsigned D, typename IMPL>
43struct ScalarVector : public GenericVector<T,D,IMPL> {
45
46 //static_assert(meta::is_arithmetic<T>::type::value, "T must be scalar type");
47
48 constexpr ScalarVector() = default;
49 ScalarVector(const ScalarVector&) = delete;
51};
52
54template<typename T, unsigned D, typename IMPL>
55inline constexpr T norm_squared(const ScalarVector<T,D,IMPL>& a)
56{
57 T sqNorm{};
58 for (unsigned iDim=0; iDim < D; ++iDim) {
59 sqNorm += a[iDim] * a[iDim];
60 }
61 return sqNorm;
62}
63
65template<typename T, unsigned D, typename IMPL>
66inline constexpr T norm(const ScalarVector<T,D,IMPL>& a)
67{
68 using namespace util;
69 T sqNorm = norm_squared(a);
70 return sqrt(sqNorm);
71}
72
74template<typename T, unsigned D, typename IMPL>
76{
77 const T eps = std::numeric_limits<T>::epsilon();
78 for (unsigned iDim=0; iDim < D; ++iDim) {
79 if (util::fabs(a[iDim]) > eps) {
80 return false;
81 }
82 }
83 return true;
84}
85
87template<typename T, unsigned D, typename IMPL>
88constexpr std::vector<T> toStdVector(const ScalarVector<T,D,IMPL>& a)
89{
90 std::vector<T> v(D);
91 for (unsigned iDim=0; iDim < D; ++iDim) {
92 v[iDim] = a[iDim];
93 }
94 return v;
95}
96
98template<typename T, unsigned D, typename U, typename IMPL, typename IMPL_>
99inline constexpr bool operator< (const ScalarVector<T,D,IMPL>& lhs, const ScalarVector<U,D,IMPL_>& rhs)
100{
101 bool smaller = true;
102 for (unsigned iDim=0; iDim < D; ++iDim) {
103 smaller &= (lhs[iDim] < rhs[iDim]);
104 }
105 return smaller;
106}
107
109template<typename T, unsigned D, typename U, typename IMPL, typename IMPL_>
110inline constexpr bool operator> (const ScalarVector<T,D,IMPL>& lhs, const ScalarVector<U,D,IMPL_>& rhs)
111{
112 return rhs < lhs;
113}
114
116template<typename T, unsigned D, typename U, typename IMPL, typename IMPL_>
117inline constexpr bool operator<= (const ScalarVector<T,D,IMPL>& lhs, const ScalarVector<U,D,IMPL_>& rhs)
118{
119 bool smallerEq = true;
120 for (unsigned iDim=0; iDim < D; ++iDim) {
121 smallerEq &= (lhs[iDim] <= rhs[iDim]);
122 }
123 return smallerEq;
124}
125
127template<typename T, unsigned D, typename U, typename IMPL, typename IMPL_>
128inline constexpr bool operator>= (const ScalarVector<T,D,IMPL>& lhs, const ScalarVector<U,D,IMPL_>& rhs)
129{
130 return rhs <= lhs;
131}
132
134template<typename T, unsigned D, typename U, typename IMPL, typename IMPL_>
135inline constexpr bool lex_smaller (const ScalarVector<T,D,IMPL>& lhs, const ScalarVector<U,D,IMPL_>& rhs)
136{
137 for (unsigned iDim=0; iDim < D; ++iDim) {
138 if (lhs[iDim] < rhs[iDim]) return true;
139 if (lhs[iDim] > rhs[iDim]) return false;
140 }
141 return false;
142}
143
145template<typename T, unsigned D, typename U, typename IMPL, typename IMPL_>
146inline constexpr bool lex_greater (const ScalarVector<T,D,IMPL>& lhs, const ScalarVector<U,D,IMPL_>& rhs)
147{
148 for (unsigned iDim=0; iDim < D; ++iDim) {
149 if (lhs[iDim] > rhs[iDim]) return true;
150 if (lhs[iDim] < rhs[iDim]) return false;
151 }
152 return false;
153}
154
156template<typename T, unsigned D, typename U, typename IMPL, typename IMPL_>
157inline constexpr bool lex_smaller_eq (const ScalarVector<T,D,IMPL>& lhs, const ScalarVector<U,D,IMPL_>& rhs)
158{
159 for (unsigned iDim=0; iDim < D; ++iDim) {
160 if (lhs[iDim] < rhs[iDim]) return true;
161 if (lhs[iDim] > rhs[iDim]) return false;
162 }
163 return true;
164}
165
167template<typename T, unsigned D, typename U, typename IMPL, typename IMPL_>
168inline constexpr bool lex_greater_eq (const ScalarVector<T,D,IMPL>& lhs, const ScalarVector<U,D,IMPL_>& rhs)
169{
170 for (unsigned iDim=0; iDim < D; ++iDim) {
171 if (lhs[iDim] > rhs[iDim]) return true;
172 if (lhs[iDim] < rhs[iDim]) return false;
173 }
174 return true;
175}
176
178template<typename T, unsigned D, typename IMPL>
179inline std::ostream& operator << (std::ostream& os, const ScalarVector<T,D,IMPL>& o)
180{
181 if (D > 0) {
182 os << "[";
183 for (unsigned iDim=0; iDim < D-1; ++iDim) {
184 os << o[iDim] << " ";
185 }
186 os << o[D-1]<<"]";
187 }
188 else {
189 os << "[empty]";
190 }
191 return os;
192}
193
194
195}
196
197#endif
cpu::simd::Pack< T > fabs(cpu::simd::Pack< T > value)
Definition pack.h:106
Top level namespace for all of OpenLB.
constexpr bool lex_greater(const ScalarVector< T, D, IMPL > &lhs, const ScalarVector< U, D, IMPL_ > &rhs)
Returns true if lhs is lexicographically greater than rhs.
constexpr std::vector< T > toStdVector(const ScalarVector< T, D, IMPL > &a)
Copies data into a standard vector.
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.
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 bool lex_smaller(const ScalarVector< T, D, IMPL > &lhs, const ScalarVector< U, D, IMPL_ > &rhs)
Returns true if lhs is lexicographically smaller than rhs.
constexpr T norm(const ScalarVector< T, D, IMPL > &a)
Euclidean vector norm.
constexpr bool lex_smaller_eq(const ScalarVector< T, D, IMPL > &lhs, const ScalarVector< U, D, IMPL_ > &rhs)
Returns true if lhs is lexicographically smaller or equal to rhs.
constexpr bool lex_greater_eq(const ScalarVector< T, D, IMPL > &lhs, const ScalarVector< U, D, IMPL_ > &rhs)
Returns true if lhs is lexicographically greater or equal to rhs.
constexpr T norm_squared(const ScalarVector< T, D, IMPL > &a)
Squared euclidean vector norm.
bool closeToZero(const ScalarVector< T, D, IMPL > &a)
Returns true iff all components are within floating point error distance of 0.
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.
std::ostream & operator<<(std::ostream &os, const ScalarVector< T, D, IMPL > &o)
Print vector entries to ostream in a human-readable fashion.
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.
Generic vector of values supporting basic arithmetic.
Vector of scalars.
ScalarVector(const ScalarVector &)=delete
ScalarVector(ScalarVector &&)=delete
constexpr ScalarVector()=default