VM2D 1.14
Vortex methods for 2D flows simulation
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#pragma once
2#include "defs.h"
3#include <complex>
4#include <array>
5#include <vector>
6#include <cassert>
7//#include <cuda/std/complex>
8#include "omp.h"
9
10#ifdef max
11#undef max
12#endif
13
14#ifdef min
15#undef min
16#endif
17
18namespace fmm {
19
28
29template <typename point, typename value>
31{
32 point center;
33 value q;
35};
36
37template <typename T>
38class Vector3 : public std::array<T, 3>
39{
40public:
41
42 constexpr Vector3() = default;
43 constexpr Vector3(const Vector3<T>& other) = default;
44 constexpr Vector3(Vector3<T>&& other) = default;
45 constexpr Vector3<T>& operator=(const Vector3<T>& other) = default;
46 constexpr Vector3<T>& operator=(Vector3<T>&& other) = default;
47 constexpr __HOST__ __DEVICE__ Vector3(T x, T y, T z) : std::array<T, 3> {x, y, z} {};
48
49 constexpr __HOST__ __DEVICE__ Vector3<T> operator+(const Vector3<T>& rhs) const {return Vector3<T>(std::array<T, 3>::operator[](0)+rhs[0], std::array<T, 3>::operator[](1)+rhs[1], std::array<T, 3>::operator[](2)+rhs[2]);}
50 constexpr __HOST__ __DEVICE__ Vector3<T> operator-(const Vector3<T>& rhs) const {return Vector3<T>(std::array<T, 3>::operator[](0)-rhs[0], std::array<T, 3>::operator[](1)-rhs[1], std::array<T, 3>::operator[](2)-rhs[2]);}
51 constexpr __HOST__ __DEVICE__ Vector3<T> operator*(const double& x) const {return Vector3<T>(std::array<T, 3>::operator[](0)*x, std::array<T, 3>::operator[](1)*x, std::array<T, 3>::operator[](2)*x);}
52 constexpr __HOST__ __DEVICE__ Vector3<T> operator/(const double& x) const {return Vector3<T>(std::array<T, 3>::operator[](0)/x, std::array<T, 3>::operator[](1)/x, std::array<T, 3>::operator[](2)/x);}
53
54 constexpr __HOST__ __DEVICE__ void operator+=(const Vector3<T>& rhs) { std::array<T, 3>::operator[](0)+=rhs[0]; std::array<T, 3>::operator[](1)+=rhs[1]; std::array<T, 3>::operator[](2)+=rhs[2];}
55 constexpr __HOST__ __DEVICE__ void operator-=(const Vector3<T>& rhs) { std::array<T, 3>::operator[](0)-=rhs[0]; std::array<T, 3>::operator[](1)-=rhs[1]; std::array<T, 3>::operator[](2)-=rhs[2];}
56 constexpr __HOST__ __DEVICE__ void operator*=(const double& x) { std::array<T, 3>::operator[](0)*=x; std::array<T, 3>::operator[](1)*=x; std::array<T, 3>::operator[](2)*=x;}
57 constexpr __HOST__ __DEVICE__ void operator/=(const double& x) { std::array<T, 3>::operator[](0)/=x; std::array<T, 3>::operator[](1)/=x; std::array<T, 3>::operator[](2)/=x;}
58
59 constexpr __HOST__ __DEVICE__ bool operator==(const Vector3<T>& rhs) const { return std::array<T, 3>::operator[](0) == rhs[0] && std::array<T, 3>::operator[](1) == rhs[1] && std::array<T, 3>::operator[](2) == rhs[2]; }
60 constexpr __HOST__ __DEVICE__ bool operator!=(const Vector3<T>& rhs) const { return !(*this == rhs); }
61};
62
64using Matrix3d = std::array<Vector3d, 3>;
66//using cuda_Vector3cd = Vector3<cuda::std::complex<double>>;
67
68template <typename T>
69inline __HOST__ __DEVICE__ constexpr Vector3<T> operator*(const double x, const Vector3<T>& rhs) {return Vector3<T>(rhs[0]*x, rhs[1]*x, rhs[2]*x);}
70
71inline __HOST__ __DEVICE__ constexpr Vector3cd operator*(const std::complex<double>& x, const Vector3d& rhs) { return Vector3cd(rhs[0] * x, rhs[1] * x, rhs[2] * x); }
72inline __HOST__ __DEVICE__ constexpr Vector3cd operator*(const Vector3d& rhs, const std::complex<double>& x) { return Vector3cd(rhs[0] * x, rhs[1] * x, rhs[2] * x); }
73inline __HOST__ __DEVICE__ constexpr Vector3cd operator*(const std::complex<double>& x, const Vector3cd& rhs) { return Vector3cd(rhs[0] * x, rhs[1] * x, rhs[2] * x); }
74inline __HOST__ __DEVICE__ constexpr Vector3cd operator*(const Vector3cd& rhs, const std::complex<double>& x) { return Vector3cd(rhs[0] * x, rhs[1] * x, rhs[2] * x); }
75
76inline __HOST__ __DEVICE__ /*constexpr*/ void operator*=(Vector3cd& lhs, const std::complex<double>& x) { lhs[0] *= x; lhs[1] *= x; lhs[2] *= x; }
77
78//inline __HOST__ __DEVICE__ cuda_Vector3cd operator*(const cuda::std::complex<double>& x, const Vector3d& rhs) { return cuda_Vector3cd(rhs[0] * x, rhs[1] * x, rhs[2] * x); }
79//inline __HOST__ __DEVICE__ cuda_Vector3cd operator*(const Vector3d& rhs, const cuda::std::complex<double>& x) { return cuda_Vector3cd(rhs[0] * x, rhs[1] * x, rhs[2] * x); }
80//inline __HOST__ __DEVICE__ cuda_Vector3cd operator*(const cuda::std::complex<double>& x, const cuda_Vector3cd& rhs) { return cuda_Vector3cd(rhs[0] * x, rhs[1] * x, rhs[2] * x); }
81//inline __HOST__ __DEVICE__ cuda_Vector3cd operator*(const cuda_Vector3cd& rhs, const cuda::std::complex<double>& x) { return cuda_Vector3cd(rhs[0] * x, rhs[1] * x, rhs[2] * x); }
82
83//inline __HOST__ __DEVICE__ void operator*=(cuda_Vector3cd& lhs, const cuda::std::complex<double>& x) { lhs[0] *= x; lhs[1] *= x; lhs[2] *= x; }
84
85inline __HOST__ __DEVICE__ /*constexpr*/ void operator+=(Vector3cd& lhs, const Vector3d& rhs) {
86 lhs[0].real(lhs[0].real() + rhs[0]);
87 lhs[1].real(lhs[1].real() + rhs[1]);
88 lhs[2].real(lhs[2].real() + rhs[2]);
89}
90
91//inline __HOST__ __DEVICE__ void operator+=(cuda_Vector3cd& lhs, const Vector3d& rhs) {
92// lhs[0].real(lhs[0].real() + rhs[0]);
93// lhs[1].real(lhs[1].real() + rhs[1]);
94// lhs[2].real(lhs[2].real() + rhs[2]);
95//}
96
97template <typename complex>
98inline __HOST__ __DEVICE__ constexpr Vector3d real(const Vector3cd& rhs) { return Vector3d(rhs[0].real(), rhs[1].real(), rhs[2].real()); }
99//template <typename complex>
100//inline __HOST__ __DEVICE__ constexpr Vector3d real(const cuda_Vector3cd& rhs) { return Vector3d(rhs[0].real(), rhs[1].real(), rhs[2].real()); }
101
102template <typename complex>
103inline __HOST__ __DEVICE__ constexpr Vector3d imag(const Vector3cd& rhs) { return Vector3d(rhs[0].imag(), rhs[1].imag(), rhs[2].imag()); }
104//template <typename complex>
105//inline __HOST__ __DEVICE__ constexpr Vector3d imag(const cuda_Vector3cd& rhs) { return Vector3d(rhs[0].imag(), rhs[1].imag(), rhs[2].imag()); }
106
107template <typename T>
108inline __HOST__ __DEVICE__ constexpr Vector3<T> cross(const Vector3<T>& lhs, const Vector3<T>& rhs) { return Vector3<T>(lhs[1] * rhs[2] - lhs[2] * rhs[1], lhs[2] * rhs[0] - lhs[0] * rhs[2], lhs[0] * rhs[1] - lhs[1] * rhs[0]); }
109
110inline __HOST__ __DEVICE__ constexpr double dot(const Vector3d& lhs, const Vector3d& rhs) { return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2]; }
111
112template <typename T>
113inline std::ostream& operator<<(std::ostream& os, const Vector3<T>& rhs) { os << rhs[0] << " " << rhs[1] << " " << rhs[2]; return os; }
114
115template <typename T>
116inline std::istream& operator>>(std::istream& is, Vector3<T>& rhs) {is >> rhs[0] >> rhs[1] >> rhs[2]; return is;}
117
118template <typename T>
119inline __HOST__ __DEVICE__ double norm(const Vector3<T>& vec) {return vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2];}
120
121template <typename T>
122inline __HOST__ __DEVICE__ double abs(const Vector3<T>& vec) {return sqrt(norm(vec));}
123
124inline __HOST__ __DEVICE__ int doublehash(double val)
125{
126 return int(val * 100);
127}
128
129inline __HOST__ __DEVICE__ int doublehash(double val1, double val2)
130{
131 return 100 * int(val1 * 100) + int(val2 * 100);
132}
133
134template<typename T>
135struct treevector : public std::vector<T>
136{
137 treevector() = default;
138 treevector(const treevector&) = default;
139 treevector(treevector&&) = default;
140 treevector& operator=(const treevector&) = default;
142 constexpr treevector(int n) : std::vector<T>(n * (n + 1) / 2) {}
143 T& operator()(int n, int m)
144 {
145 return this->operator[](n * (n + 1) / 2 + m);
146 }
147 const T& operator()(int n, int m) const
148 {
149 return this->operator[](n * (n + 1) / 2 + m);
150 }
151};
152
153using point2d = std::complex<double>;
158
159} // fmm
#define __DEVICE__
Definition defs.h:21
#define __HOST__
Definition defs.h:20
Описание базовых вспомогательных функций
constexpr Vector3< T > & operator=(Vector3< T > &&other)=default
constexpr void operator+=(const Vector3< T > &rhs)
Definition utils.h:54
constexpr bool operator==(const Vector3< T > &rhs) const
Definition utils.h:59
constexpr bool operator!=(const Vector3< T > &rhs) const
Definition utils.h:60
constexpr Vector3< T > operator/(const double &x) const
Definition utils.h:52
constexpr Vector3()=default
constexpr Vector3(const Vector3< T > &other)=default
constexpr void operator/=(const double &x)
Definition utils.h:57
constexpr Vector3< T > & operator=(const Vector3< T > &other)=default
constexpr Vector3< T > operator+(const Vector3< T > &rhs) const
Definition utils.h:49
constexpr Vector3< T > operator-(const Vector3< T > &rhs) const
Definition utils.h:50
constexpr Vector3< T > operator*(const double &x) const
Definition utils.h:51
constexpr void operator*=(const double &x)
Definition utils.h:56
constexpr Vector3(Vector3< T > &&other)=default
constexpr void operator-=(const Vector3< T > &rhs)
Definition utils.h:55
constexpr Vector3(T x, T y, T z)
Definition utils.h:47
Definition avx.h:5
Vector3< std::complex< double > > Vector3cd
Definition utils.h:65
void operator*=(Vector3cd &lhs, const std::complex< double > &x)
Definition utils.h:76
std::ostream & operator<<(std::ostream &os, const Vector3< T > &rhs)
Definition utils.h:113
constexpr Vector3d imag(const Vector3cd &rhs)
Definition utils.h:103
std::array< Vector3d, 3 > Matrix3d
Definition utils.h:64
constexpr Vector3d real(const Vector3cd &rhs)
Definition utils.h:98
double norm(const Vector3< T > &vec)
Definition utils.h:119
double abs(const Vector3< T > &vec)
Definition utils.h:122
std::istream & operator>>(std::istream &is, Vector3< T > &rhs)
Definition utils.h:116
int doublehash(double val)
Definition utils.h:124
constexpr double dot(const Vector3d &lhs, const Vector3d &rhs)
Definition utils.h:110
std::complex< double > point2d
Definition utils.h:153
constexpr Vector3< T > operator*(const double x, const Vector3< T > &rhs)
Definition utils.h:69
Vector3< double > Vector3d
Definition utils.h:63
constexpr Vector3< T > cross(const Vector3< T > &lhs, const Vector3< T > &rhs)
Definition utils.h:108
particle< point3d, point3d > particle3d3
Definition utils.h:157
void operator+=(Vector3cd &lhs, const Vector3d &rhs)
Definition utils.h:85
particle< point3d, double > particle3d
Definition utils.h:156
InteractionType
Definition utils.h:20
value q
Definition utils.h:33
point center
Definition utils.h:32
size_t morton_code
Definition utils.h:34
const T & operator()(int n, int m) const
Definition utils.h:147
treevector & operator=(treevector &&)=default
treevector()=default
T & operator()(int n, int m)
Definition utils.h:143
constexpr treevector(int n)
Definition utils.h:142
treevector(treevector &&)=default
treevector & operator=(const treevector &)=default
treevector(const treevector &)=default