Alsvinn  0.5.3
The fast FVM simulator with UQ support
vec5.hpp
Go to the documentation of this file.
1 /* Copyright (c) 2018 ETH Zurich, Kjetil Olsen Lye
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #pragma once
17 #include <vector>
18 #include "alsutils/vec3.hpp"
19 namespace alsutils {
20 
24 template<class T>
25 struct vec5 {
26  T x;
27  T y;
28  T z;
29  T v;
30  T w;
31 
32 
34  x(t), y(t), z(t), v(t), w(t) {
35 
36  }
38  : x(0), y(0), z(0), v(0), w(0) {
39 
40  }
41  __device__ __host__ vec5(T x, T y, T z, T v, T w)
42  : x(x), y(y), z(z), v(v), w(w) {
43  // Empty
44  }
45 
47  : x(x), y(y.x), z(y.y), v(y.z), w(z) {
48  // Empty
49  }
50 
52  : x(other.x), y(other.y), z(other.z), v(other.v), w(other.w) {
53 
54  }
55 
56 
57  template<class S>
59  x = other.x;
60  y = other.y;
61  z = other.z;
62  v = other.v;
63  w = other.w;
64 
65  return *this;
66  }
67 
68  __device__ __host__ bool operator==(const vec5& other) const {
69  return other.x == x && other.y == y && other.z == z && other.v == v
70  && other.w == w;
71  }
72 #if __cplusplus > 199711L || WIN32
73  std::vector<T> toStdVector() {
78  return std::vector<T>({ x, y, z, v, w });
79  }
80 #endif
81  template<class S>
86  return vec5<S>(S(x), S(y), S(z), S(v), S(w));
87  }
88 
92  __device__ __host__ const T& operator[](size_t i) const {
93  // Note: We only store five numbers in this struct, hence this is safe
94  return ((T*)this)[i];
95  }
96 
101  // Note: We only store five numbers in this struct, hence this is safe
102  return ((T*)this)[i];
103  }
104 
108  __device__ __host__ T dot(const vec5<T>& other) const {
109  return x * other.x + y * other.y + z * other.z + v * other.v + w * other.w;
110  }
111 
115  __device__ __host__ static constexpr size_t size() {
116  return 5;
117  }
118 
119  template<class S>
121  x += b.x;
122  y += b.y;
123  z += b.z;
124  v += b.v;
125  w += b.w;
126  return *this;
127  }
128 
129  __host__ std::string str() const;
130 };
131 
137 template<class T>
139  const vec5<T>& b) {
140  return vec5<T>(a.x / b.x, a.y / b.y, a.z / b.z, a.v / b.v, a.w / b.w);
141 }
142 
147 template<class T>
148 __device__ __host__ inline vec5<T> operator*(T scalar, const vec5<T>& a) {
149  return vec5<T>(a.x * scalar, a.y * scalar, a.z * scalar, a.v * scalar,
150  a.w * scalar);
151 }
152 
157 template<class T>
159  const vec5<T>& b) {
160  return vec5<T>(a.x - b.x, a.y - b.y, a.z - b.z, a.v - b.v, a.w - b.w);
161 }
162 
167 template<class T>
168 __device__ __host__ inline vec5<T> operator/(const vec5<T>& a, T scalar) {
169  return vec5<T>(a.x / scalar, a.y / scalar, a.z / scalar, a.v / scalar,
170  a.w / scalar);
171 }
172 
177 template<class T, class S>
179  const vec5<S>& b) {
180  return vec5<T>(a.x + b.x, a.y + b.y, a.z + b.z, a.v + b.v, a.w + b.w);
181 }
182 
183 
184 
185 
186 
187 }
188 
189 
190 template<typename T>
191 std::ostream& operator<<(std::ostream& os,
192  const alsutils::vec5<T>& vec) {
193  os << "[";
194 
195  for (int i = 0; i < 5; ++i) {
196  os << vec[i];
197 
198  if (i < 4) {
199  os << ", ";
200  }
201  }
202 
203  os << "]";
204  return os;
205 }
206 
207 template<class T>
208 inline __host__ std::string alsutils::vec5<T>::str() const {
209  std::stringstream ss;
210  ss << *this;
211  return ss.str();
212 }
213 
__device__ __host__ vec1< T > operator-(const vec1< T > &a, const vec1< T > &b)
Definition: vec1.hpp:140
T y
Definition: vec5.hpp:27
__device__ __host__ bool operator==(const vec5 &other) const
Definition: vec5.hpp:68
__device__ __host__ vec1< T > operator*(T scalar, const vec1< T > &a)
Definition: vec1.hpp:131
Definition: vec3.hpp:25
#define __host__
Definition: types.hpp:46
__device__ __host__ vec5 & operator=(const vec5< S > &other)
Definition: vec5.hpp:58
__device__ __host__ vec5(T x, vec3< T > y, T z)
Definition: vec5.hpp:46
__host__ std::string str() const
Definition: vec5.hpp:208
__device__ __host__ T & operator[](size_t i)
Definition: vec5.hpp:100
__device__ __host__ vec1< T > operator+(const vec1< T > &a, const vec1< S > &b)
Definition: vec1.hpp:159
__device__ __host__ vec5< T > & operator+=(const vec5< S > &b)
Definition: vec5.hpp:120
Various utilities for mpi and cuda.
Definition: Factory.hpp:3
T v
Definition: vec5.hpp:29
__device__ __host__ vec5< S > convert()
Definition: vec5.hpp:85
__device__ __host__ vec5(const vec5< T &> &other)
Definition: vec5.hpp:51
__device__ static __host__ constexpr size_t size()
Definition: vec5.hpp:115
__device__ __host__ vec5(T t)
Definition: vec5.hpp:33
__device__ __host__ const T & operator[](size_t i) const
Definition: vec5.hpp:92
#define __device__
Definition: types.hpp:45
Definition: vec5.hpp:25
__device__ __host__ vec5()
Definition: vec5.hpp:37
__device__ __host__ T dot(const vec5< T > &other) const
Definition: vec5.hpp:108
T w
Definition: vec5.hpp:30
__device__ __host__ vec1< T > operator/(const vec1< T > &a, const vec1< T > &b)
Definition: vec1.hpp:121
__device__ __host__ vec5(T x, T y, T z, T v, T w)
Definition: vec5.hpp:41
T z
Definition: vec5.hpp:28
T x
Definition: vec5.hpp:26
std::ostream & operator<<(std::ostream &os, const alsutils::vec5< T > &vec)
Definition: vec5.hpp:191