Alsvinn  0.5.3
The fast FVM simulator with UQ support
vec3.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/vec1.hpp"
19 namespace alsutils {
20 
24 template<class T>
25 struct vec3 {
26  T x;
27  T y;
28  T z;
29 
31  : x(0), y(0), z(0) {
32 
33  }
34 
36  x(t), y(t), z(t) {
37 
38  }
39  __device__ __host__ vec3(T x, T y, T z)
40  : x(x), y(y), z(z) {
41  // Empty
42  }
43 
45  : x(x), y(y.x), z(z) {
46  // Empty
47  }
48 
50  : x(other.x), y(other.y), z(other.z) {
51 
52  }
53 
54  template<class S>
56  x = other.x;
57  y = other.y;
58  z = other.z;
59 
60  return *this;
61  }
62 
63  __device__ __host__ bool operator==(const vec3& other) const {
64  return other.x == x && other.y == y && other.z == z;
65  }
66 #if __cplusplus > 199711L || WIN32
67  std::vector<T> toStdVector() {
72  return std::vector<T>({ x, y, z });
73  }
74 #endif
75  template<class S>
80  return vec3<S>(S(x), S(y), S(z));
81  }
82 
86  __device__ __host__ const T& operator[](size_t i) const {
87  // Note: We only store three numbers in this struct, hence this is safe
88  return ((T*)this)[i];
89  }
90 
95  // Note: We only store three numbers in this struct, hence this is safe
96  return ((T*)this)[i];
97  }
98 
102  __device__ __host__ T dot(const vec3<T>& other) const {
103  return x * other.x + y * other.y + z * other.z;
104  }
105 
109  __device__ __host__ static constexpr size_t size() {
110  return 3;
111  }
112 
113  template<class S>
115  x += b.x;
116  y += b.y;
117  z += b.z;
118 
119  return *this;
120  }
121 
122  __host__ std::string str() const;
123 
124 };
125 
131 template<class T>
133  const vec3<T>& b) {
134  return vec3<T>(a.x / b.x, a.y / b.y, a.z / b.z);
135 }
136 
141 template<class T>
142 __device__ __host__ inline vec3<T> operator*(T scalar, const vec3<T>& a) {
143  return vec3<T>(a.x * scalar, a.y * scalar, a.z * scalar);
144 }
145 
150 template<class T, class S>
152  const vec3<S>& b) {
153  return vec3<T>(a.x * b.x, a.y * b.y, a.z * b.z);
154 }
155 
160 template<class T>
162  const vec3<T>& b) {
163  return vec3<T>(a.x - b.x, a.y - b.y, a.z - b.z);
164 }
165 
170 template<class T>
171 __device__ __host__ inline vec3<T> operator/(const vec3<T>& a, T scalar) {
172  return vec3<T>(a.x / scalar, a.y / scalar, a.z / scalar);
173 }
174 
179 template<class T, class S>
181  const vec3<S>& b) {
182  return vec3<T>(a.x + b.x, a.y + b.y, a.z + b.z);
183 }
184 
185 
186 
187 }
188 
189 template<typename T>
190 std::ostream& operator<<(std::ostream& os,
191  const alsutils::vec3<T>& vec) {
192  os << "[";
193 
194  for (int i = 0; i < 3; ++i) {
195  os << vec[i];
196 
197  if (i < 2) {
198  os << ", ";
199  }
200  }
201 
202  os << "]";
203  return os;
204 }
205 
206 template<class T>
207 inline __host__ std::string alsutils::vec3<T>::str() const {
208  std::stringstream ss;
209  ss << *this;
210  return ss.str();
211 }
212 
__device__ __host__ vec1< T > operator-(const vec1< T > &a, const vec1< T > &b)
Definition: vec1.hpp:140
__device__ __host__ bool operator==(const vec3 &other) const
Definition: vec3.hpp:63
T z
Definition: vec3.hpp:28
__device__ __host__ vec3(const vec3< T &> &other)
Definition: vec3.hpp:49
__device__ __host__ vec3(T x, T y, T z)
Definition: vec3.hpp:39
__device__ __host__ vec1< T > operator*(T scalar, const vec1< T > &a)
Definition: vec1.hpp:131
__device__ __host__ vec3 & operator=(const vec3< S > &other)
Definition: vec3.hpp:55
__device__ __host__ const T & operator[](size_t i) const
Definition: vec3.hpp:86
Definition: vec3.hpp:25
#define __host__
Definition: types.hpp:46
__device__ static __host__ constexpr size_t size()
Definition: vec3.hpp:109
__device__ __host__ vec3< S > convert() const
Definition: vec3.hpp:79
__device__ __host__ vec3(T x, vec1< T > y, T z)
Definition: vec3.hpp:44
__device__ __host__ vec1< T > operator+(const vec1< T > &a, const vec1< S > &b)
Definition: vec1.hpp:159
Various utilities for mpi and cuda.
Definition: Factory.hpp:3
Definition: vec1.hpp:24
__device__ __host__ T dot(const vec3< T > &other) const
Definition: vec3.hpp:102
__device__ __host__ vec3< T > & operator+=(const vec3< S > &b)
Definition: vec3.hpp:114
#define __device__
Definition: types.hpp:45
__device__ __host__ T & operator[](size_t i)
Definition: vec3.hpp:94
T y
Definition: vec3.hpp:27
__device__ __host__ vec1< T > operator/(const vec1< T > &a, const vec1< T > &b)
Definition: vec1.hpp:121
__device__ __host__ vec3(T t)
Definition: vec3.hpp:35
__host__ std::string str() const
Definition: vec3.hpp:207
std::ostream & operator<<(std::ostream &os, const alsutils::vec3< T > &vec)
Definition: vec3.hpp:190
__device__ __host__ vec3()
Definition: vec3.hpp:30
T x
Definition: vec3.hpp:26