Garfield 0.3
Toolkit for the detailed simulation of particle detectors based on ionization measurement in gases and semiconductors
Loading...
Searching...
No Matches
Vector.hh
Go to the documentation of this file.
1#ifndef G_VECTOR_H
2#define G_VECTOR_H
3
4#if defined(__NVCC__)
5#include <cuda/std/array>
6template <typename T, std::size_t D>
7using GarfieldArray = cuda::std::array<T, D>;
8#else
9#include <array>
10#if !defined(__device__)
11#define __GARFIELD_DEFINED_DEVICE__
12#define __device__
13#define __host__
14#endif
15template <typename T, std::size_t D>
16using GarfieldArray = std::array<T, D>;
17#endif
18#include <cstddef>
19
20namespace Garfield {
21template <typename type, std::size_t Dimension>
22class Vector : public GarfieldArray<type, Dimension> {
23 public:
24 template <typename... Args>
25 __host__ __device__ Vector(Args&&... args)
26 : GarfieldArray<type, Dimension>({std::forward<Args>(args)...}) {}
27 __host__ __device__ Vector() { this->fill(0.); }
29 Vector ret = *this;
30 for (std::size_t j = 0; j != ret.size(); ++j) {
31 ret[j] *= i;
32 }
33 return ret;
34 }
36 Vector ret = *this;
37 for (std::size_t j = 0; j != ret.size(); ++j) {
38 ret[j] /= i;
39 }
40 return ret;
41 }
42 template <typename T, std::size_t D>
44 static_assert(Dimension == D, "The vectors don't have the same dimension!");
45 Vector ret = *this;
46 for (std::size_t j = 0; j != ret.size(); ++j) {
47 ret[j] += vec[j];
48 }
49 return ret;
50 }
51 template <typename T, std::size_t D>
53 static_assert(Dimension == D, "The vectors don't have the same dimension!");
54 Vector ret = *this;
55 for (std::size_t j = 0; j != ret.size(); ++j) {
56 ret[j] -= vec[j];
57 }
58 return ret;
59 }
60};
61
62template <typename type>
63class Vec1Impl : public Vector<type, 1> {
64 public:
65 Vec1Impl() = default;
66 template <typename... xs>
67 __host__ __device__ Vec1Impl(xs... values) : Vector<type, 3>({values...}) {}
68 __host__ __device__ type& x() noexcept { return this->operator[](0); }
69 __host__ __device__ type x() const noexcept { return this->operator[](0); }
70};
71
72template <typename type>
73class Vec2Impl : public Vector<type, 2> {
74 public:
75 Vec2Impl() = default;
76 template <typename... xs>
77 __host__ __device__ Vec2Impl(xs... values) : Vector<type, 3>({values...}) {}
78 __host__ __device__ type& x() noexcept { return this->operator[](0); }
79 __host__ __device__ type x() const noexcept { return this->operator[](0); }
80 __host__ __device__ type& y() noexcept { return this->operator[](1); }
81 __host__ __device__ type y() const noexcept { return this->operator[](1); }
82};
83
84template <typename type>
85class Vec3Impl : public Vector<type, 3> {
86 public:
87 Vec3Impl() = default;
88 template <typename... xs>
89 __host__ __device__ Vec3Impl(xs... values) : Vector<type, 3>({values...}) {}
90 __host__ __device__ type& x() noexcept { return this->operator[](0); }
91 __host__ __device__ type x() const noexcept { return this->operator[](0); }
92 __host__ __device__ type& y() noexcept { return this->operator[](1); }
93 __host__ __device__ type y() const noexcept { return this->operator[](1); }
94 __host__ __device__ type& z() noexcept { return this->operator[](2); }
95 __host__ __device__ type z() const noexcept { return this->operator[](2); }
96};
97
100
101} // namespace Garfield
102
103#if defined(__GARFIELD_DEFINED_DEVICE__)
104#undef __device__
105#undef __host__
106#undef __GARFIELD_DEFINED_DEVICE__
107#endif
108
109#endif
std::array< T, D > GarfieldArray
Definition Vector.hh:16
#define __host__
Definition Vector.hh:13
#define __device__
Definition Vector.hh:12
__host__ __device__ type & x() noexcept
Definition Vector.hh:68
__host__ __device__ Vec1Impl(xs... values)
Definition Vector.hh:67
__host__ __device__ type x() const noexcept
Definition Vector.hh:69
__host__ __device__ type x() const noexcept
Definition Vector.hh:79
__host__ __device__ Vec2Impl(xs... values)
Definition Vector.hh:77
__host__ __device__ type & x() noexcept
Definition Vector.hh:78
__host__ __device__ type & y() noexcept
Definition Vector.hh:80
__host__ __device__ type y() const noexcept
Definition Vector.hh:81
__host__ __device__ type & z() noexcept
Definition Vector.hh:94
__host__ __device__ type z() const noexcept
Definition Vector.hh:95
__host__ __device__ type & x() noexcept
Definition Vector.hh:90
__host__ __device__ type & y() noexcept
Definition Vector.hh:92
__host__ __device__ type x() const noexcept
Definition Vector.hh:91
__host__ __device__ type y() const noexcept
Definition Vector.hh:93
__host__ __device__ Vec3Impl(xs... values)
Definition Vector.hh:89
__host__ __device__ Vector operator+(const Vector< T, D > &vec)
Definition Vector.hh:43
__host__ __device__ Vector operator/(const double &i)
Definition Vector.hh:35
__host__ __device__ Vector operator-(const Vector< T, D > &vec)
Definition Vector.hh:52
__host__ __device__ Vector operator*(const double &i)
Definition Vector.hh:28
__host__ __device__ Vector()
Definition Vector.hh:27
__host__ __device__ Vector(Args &&... args)
Definition Vector.hh:25
Vec3Impl< float > Vec3F
Definition Vector.hh:99
Vec3Impl< double > Vec3D
Definition Vector.hh:98