-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathUtils.h
122 lines (99 loc) · 2.46 KB
/
mathUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#pragma once
/*
*
* Some math functions repeatedly used in the code
*
*/
#ifndef MATH_UTILITIES_H_
# define MATH_UTILITIES_H_
# include <stdexcept>
# include <vector>
# include <array>
# include <deal.II/grid/tria.h>
template <typename T>
inline T
pow2(T x)
{
return x * x;
}
template <typename T>
inline T
pow3(T x)
{
return x * x * x;
}
template <typename T>
inline T
pow4(T x)
{
return x * x * x * x;
}
template <typename T>
inline T
pow5(T x)
{
return x * x * x * x * x;
}
// for the above functions types are inferred
inline double
factorial(unsigned int N)
{
if (N > 20)
throw std::out_of_range(
"This factorial function can handle 0 to 20 range only!!");
double fac = 1.0;
for (unsigned int i = 2; i <= N; ++i) // unsigned int i
{
fac *= i;
}
return fac;
}
inline double
distance3d(const dealii::Point<3> & evalPoint,
const std::vector<double> &atomPos)
{
double sqdist = (evalPoint[0] - atomPos[0]) * (evalPoint[0] - atomPos[0]) +
(evalPoint[1] - atomPos[1]) * (evalPoint[1] - atomPos[1]) +
(evalPoint[2] - atomPos[2]) * (evalPoint[2] - atomPos[2]);
return std::sqrt(sqdist);
}
inline double
distance3d(const dealii::Point<3> & evalPoint,
const std::array<double, 3> &atomPos)
{
double sqdist = (evalPoint[0] - atomPos[0]) * (evalPoint[0] - atomPos[0]) +
(evalPoint[1] - atomPos[1]) * (evalPoint[1] - atomPos[1]) +
(evalPoint[2] - atomPos[2]) * (evalPoint[2] - atomPos[2]);
return std::sqrt(sqdist);
}
inline double
norm3d(const std::vector<double> &x)
{
return std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
}
inline double
norm3d(const std::array<double, 3> &x)
{
return std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
}
inline std::vector<double>
relativeVector3d(const dealii::Point<3> & evalPoint,
const std::vector<double> &atomPos)
{
std::vector<double> relativeVec(3, 0.0);
relativeVec[0] = evalPoint[0] - atomPos[0];
relativeVec[1] = evalPoint[1] - atomPos[1];
relativeVec[2] = evalPoint[2] - atomPos[2];
return relativeVec;
}
inline std::array<double, 3>
relativeVector3d(const dealii::Point<3> & evalPoint,
const std::array<double, 3> &atomPos)
{
std::array<double, 3> relativeVec{};
relativeVec[0] = evalPoint[0] - atomPos[0];
relativeVec[1] = evalPoint[1] - atomPos[1];
relativeVec[2] = evalPoint[2] - atomPos[2];
return relativeVec;
}
#endif