Repository navigation
Expand file tree
/
Copy pathmath_ylm_op.cpp
More file actions
130 lines (120 loc) · 4.94 KB
/
Copy pathmath_ylm_op.cpp
File metadata and controls
130 lines (120 loc) · 4.94 KB
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
123
124
125
126
127
128
129
#include "source_base/kernels/math_ylm_op.h"
#include "source_base/libm/libm.h"
namespace ModuleBase {
template <typename FPTYPE>
__inline__
FPTYPE __fact(const int n) {
FPTYPE f = 1.0;
for (int i = n; i > 1; i--) {
f *= i;
}
return f;
}
__inline__
int __semi_fact(const int n)
{
int semif = 1;
for (int i = n; i > 2; i -= 2)
{
semif *= i;
}
return semif;
}
template <typename FPTYPE>
struct cal_ylm_real_op<FPTYPE, base_device::DEVICE_CPU>
{
void operator()(const base_device::DEVICE_CPU* ctx,
const int& ng,
const int& lmax,
const FPTYPE& SQRT2,
const FPTYPE& PI,
const FPTYPE& PI_HALF,
const FPTYPE& FOUR_PI,
const FPTYPE& SQRT_INVERSE_FOUR_PI,
const FPTYPE* g,
FPTYPE* p,
FPTYPE* ylm)
{
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (int ig = 0; ig < ng; ig++) {
//----------------------------------------------------------
// EXPLAIN : if lmax = 1,only use Y00 , output result.
//----------------------------------------------------------
if (lmax == 0) {
ylm[0 * ng + ig] = SQRT_INVERSE_FOUR_PI;
continue;
}
//----------------------------------------------------------
// LOCAL VARIABLES :
// NAME : cost = cos(theta),theta and phi are polar angles
// NAME : phi
//----------------------------------------------------------
const FPTYPE gmod = sqrt(g[ig * 3 + 0] * g[ig * 3 + 0] + g[ig * 3 + 1] * g[ig * 3 + 1] + g[ig * 3 + 2] * g[ig * 3 + 2]);
FPTYPE cost = gmod < 1.0e-9 ? 0.0 : g[ig * 3 + 2] / gmod;
FPTYPE phi;
// beware the arc tan, it is defined modulo pi
if (g[ig * 3 + 0] > 1.0e-9) {
phi = atan(g[ig * 3 + 1] / g[ig * 3 + 0]);
}
else if (g[ig * 3 + 0] < -1.e-9) {
phi = atan(g[ig * 3 + 1] / g[ig * 3 + 0]) + PI;
}
else {
phi = PI_HALF * ((g[ig * 3 + 1] >= 0.0) ? 1.0 : -1.0); //HLX: modified on 10/13/2006
} // end if
//==========================================================
// NAME : p(Legendre Polynomials) (0 <= m <= l)
//==========================================================
int lm = -1;
for (int l = 0; l <= lmax; l++) {
const FPTYPE c = sqrt((2 * l + 1) / FOUR_PI);
if (l == 0) {
p[0 * (lmax + 1) * ng + 0 * ng + ig] = 1.0;
}
else if (l == 1) {
p[0 * (lmax + 1) * ng + 1 * ng + ig] = cost;
p[1 * (lmax + 1) * ng + 1 * ng + ig] = -sqrt(std::max(0.0, 1.0 - cost * cost));
}
else {
const int l1 = l - 1,
l2 = l - 2,
l3 = 2 * l - 1;
// recursion on l for P(:,l,m)
for (int m = 0; m <= l2; m++) { // do m = 0, l - 2//mohan modify 2007-10-13
p[m * (lmax + 1) * ng + l * ng + ig] =
(cost * l3 * p[m * (lmax + 1) * ng + l1 * ng + ig] -
(l1 + m) * p[m * (lmax + 1) * ng + l2 * ng + ig]) / (l - m);
} // end do
p[l1 * (lmax + 1) * ng + l * ng + ig] =
cost * l3 * p[l1 * (lmax + 1) * ng + l1 * ng + ig];
FPTYPE x2 = std::max(0.0, 1.0 - cost * cost);
p[l * (lmax + 1) * ng + l * ng + ig] = __semi_fact(l3) * pow(x2, static_cast<FPTYPE>(l) / 2.0);//mohan modify 2007-10-13
if (l % 2 == 1) {
p[l * (lmax + 1) * ng + l * ng + ig] *= -1;
}
} // end if
// Y_lm, m = 0
++lm;
ylm[lm * ng + ig] = c * p[0 * (lmax + 1) * ng + l * ng + ig];
for (int m = 1; m <= l; m++) {
// Y_lm, m > 0
const FPTYPE same =
c * sqrt(__fact<FPTYPE>(l - m) /
__fact<FPTYPE>(l + m)) * SQRT2;
FPTYPE sinp, cosp;
ModuleBase::libm::sincos(m * phi, &sinp, &cosp);
++lm;
ylm[lm * ng + ig] = same * p[m * (lmax + 1) * ng + l * ng + ig] * cosp;
// Y_lm, m < 0
++lm;
ylm[lm * ng + ig] = same * p[m * (lmax + 1) * ng + l * ng + ig] * sinp;
}
}// end do
}
}
};
template struct cal_ylm_real_op<float, base_device::DEVICE_CPU>;
template struct cal_ylm_real_op<double, base_device::DEVICE_CPU>;
} // namespace ModuleBase