Alsvinn  0.5.3
The fast FVM simulator with UQ support
vec2.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 namespace alsutils {
19 
23 template<class T>
24 struct vec2 {
25  T x;
26  T y;
27 
29  : x(0), y(0) {
30 
31  }
32 
34  x(t), y(t) {
35 
36  }
37 
39  : x(x), y(y) {
40  // Empty
41  }
42 
43 
44 
46  : x(other.x), y(other.y) {
47 
48 
49 
50  }
51 
52  template<class S>
54  x = other.x;
55  y = other.y;
56 
57 
58  return *this;
59  }
60 
61 
62  __device__ __host__ bool operator==(const vec2& other) const {
63  return other.x == x && other.y == y;
64  }
65 #if __cplusplus > 199711L || WIN32
66  std::vector<T> toStdVector() {
71  return std::vector<T>({ x, y });
72  }
73 #endif
74  template<class S>
79  return vec2<S>(S(x), S(y));
80  }
81 
85  __device__ __host__ const T& operator[](size_t i) const {
86  // Note: We only store three numbers in this struct, hence this is safe
87  return ((T*)this)[i];
88  }
89 
94  // Note: We only store three numbers in this struct, hence this is safe
95  return ((T*)this)[i];
96  }
97 
101  __device__ __host__ T dot(const vec2<T>& other) const {
102  return x * other.x + y * other.y;
103  }
104 
108  __device__ __host__ static constexpr size_t size() {
109  return 2;
110  }
111 
112  template<class S>
114  x += b.x;
115  y += b.y;
116 
117  return *this;
118  }
119 };
120 
126 template<class T>
128  const vec2<T>& b) {
129  return vec2<T>(a.x / b.x, a.y / b.y);
130 }
131 
136 template<class T>
137 __device__ __host__ inline vec2<T> operator*(T scalar, const vec2<T>& a) {
138  return vec2<T>(a.x * scalar, a.y * scalar);
139 }
140 
145 template<class T>
147  const vec2<T>& b) {
148  return vec2<T>(a.x - b.x, a.y - b.y);
149 }
150 
155 template<class T>
156 __device__ __host__ inline vec2<T> operator/(const vec2<T>& a, T scalar) {
157  return vec2<T>(a.x / scalar, a.y / scalar);
158 }
159 
164 template<class T, class S>
166  const vec2<S>& b) {
167  return vec2<T>(a.x + b.x, a.y + b.y);
168 }
169 
170 }
T x
Definition: vec2.hpp:25
__device__ __host__ vec1< T > operator-(const vec1< T > &a, const vec1< T > &b)
Definition: vec1.hpp:140
T y
Definition: vec2.hpp:26
__device__ __host__ const T & operator[](size_t i) const
Definition: vec2.hpp:85
__device__ __host__ vec1< T > operator*(T scalar, const vec1< T > &a)
Definition: vec1.hpp:131
#define __host__
Definition: types.hpp:46
Definition: vec2.hpp:24
__device__ __host__ vec2(const vec2< T &> &other)
Definition: vec2.hpp:45
__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
__device__ __host__ bool operator==(const vec2 &other) const
Definition: vec2.hpp:62
__device__ __host__ vec2(T t)
Definition: vec2.hpp:33
__device__ __host__ T dot(const vec2< T > &other) const
Definition: vec2.hpp:101
__device__ __host__ vec2< S > convert() const
Definition: vec2.hpp:78
#define __device__
Definition: types.hpp:45
__device__ __host__ vec2< T > & operator+=(const vec2< S > &b)
Definition: vec2.hpp:113
__device__ __host__ vec1< T > operator/(const vec1< T > &a, const vec1< T > &b)
Definition: vec1.hpp:121
__device__ static __host__ constexpr size_t size()
Definition: vec2.hpp:108
__device__ __host__ T & operator[](size_t i)
Definition: vec2.hpp:93
__device__ __host__ vec2()
Definition: vec2.hpp:28
__device__ __host__ vec2 & operator=(const vec2< S > &other)
Definition: vec2.hpp:53
__device__ __host__ vec2(T x, T y)
Definition: vec2.hpp:38