Skip to content

Commit fa16019

Browse files
committed
test: add comments and unittest for math_sphbes.h
range: source/module_base
1 parent a1f53d1 commit fa16019

File tree

2 files changed

+168
-10
lines changed

2 files changed

+168
-10
lines changed

source/module_base/math_sphbes.h

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,41 @@ class Sphbes
1414
Sphbes();
1515
~Sphbes();
1616

17+
/**
18+
* @brief spherical bessel
19+
*
20+
* @param msh [in] number of grid points
21+
* @param r [in] radial grid (1:msh)
22+
* @param q [in] k_radial
23+
* @param l [in] angular momentum
24+
* @param jl [out] jl(1:msh) spherical bessel function
25+
*/
1726
static void Spherical_Bessel
1827
(
19-
const int &msh, //number of grid points
20-
const double *r,//radial grid
21-
const double &q, //
22-
const int &l, //angular momentum
23-
double *jl //jl(1:msh) = j_l(q*r(i)),spherical bessel function
28+
const int &msh,
29+
const double *r,
30+
const double &q,
31+
const int &l,
32+
double *jl
2433
);
2534

35+
/**
36+
* @brief spherical bessel
37+
*
38+
* @param msh [in] number of grid points
39+
* @param r [in] radial grid (1:msh)
40+
* @param q [in] k_radial
41+
* @param l [in] angular momentum
42+
* @param jl [out] jl(1:msh) spherical bessel function
43+
* @param sjp [out] sjp[i] is assigned to be 1.0. i < msh.
44+
*/
2645
static void Spherical_Bessel
2746
(
28-
const int &msh, //number of grid points
29-
const double *r,//radial grid
30-
const double &q, //
31-
const int &l, //angular momentum
32-
double *sj, //jl(1:msh) = j_l(q*r(i)),spherical bessel function
47+
const int &msh,
48+
const double *r,
49+
const double &q,
50+
const int &l,
51+
double *sj,
3352
double *sjp
3453
);
3554

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include"../math_sphbes.h"
2+
#include<iostream>
3+
4+
#ifdef __MPI
5+
#include"mpi.h"
6+
#endif
7+
8+
#include"gtest/gtest.h"
9+
10+
#define doublethreshold 1e-12
11+
12+
13+
/************************************************
14+
* unit test of class Integral
15+
***********************************************/
16+
17+
/**
18+
* Note: this unit test try to ensure the invariance
19+
* of the spherical Bessel produced by class Sphbes,
20+
* and the reference results are produced by ABACUS
21+
* at 2022-1-27.
22+
*
23+
* Tested function: Spherical_Bessel.
24+
*
25+
*/
26+
27+
double mean(const double* vect, const int totN)
28+
{
29+
double meanv = 0.0;
30+
for (int i=0; i< totN; ++i) {meanv += vect[i]/totN;}
31+
return meanv;
32+
}
33+
34+
class Sphbes : public testing::Test
35+
{
36+
protected:
37+
38+
int msh = 700;
39+
int l0 = 0;
40+
int l1 = 1;
41+
int l2 = 2;
42+
int l3 = 3;
43+
int l4 = 4;
44+
int l5 = 5;
45+
int l6 = 6;
46+
int l7 = 7;
47+
double q = 1.0;
48+
double *r = new double[msh];
49+
double *jl = new double[msh];
50+
51+
void SetUp()
52+
{
53+
for(int i=0; i<msh; ++i) {r[i] = 0.01*(i);}
54+
}
55+
56+
void TearDown()
57+
{
58+
delete r;
59+
delete jl;
60+
}
61+
};
62+
63+
64+
TEST_F(Sphbes,SphericalBessel)
65+
{
66+
//int l = 0;
67+
ModuleBase::Sphbes::Spherical_Bessel(msh,r,q,l0,jl);
68+
//reference result is from bessel_test.cpp which is calculated by
69+
//ModuleBase::Sph_Bessel_Recursive::D1
70+
EXPECT_NEAR(mean(jl,msh)/0.2084468748396, 1.0,doublethreshold);
71+
72+
73+
//int l = 1;
74+
ModuleBase::Sphbes::Spherical_Bessel(msh,r,q,l1,jl);
75+
//reference result is from bessel_test.cpp which is calculated by
76+
//ModuleBase::Sph_Bessel_Recursive::D1
77+
EXPECT_NEAR(mean(jl,msh)/0.12951635180384, 1.0,doublethreshold);
78+
79+
80+
//int l = 2;
81+
ModuleBase::Sphbes::Spherical_Bessel(msh,r,q,l2,jl);
82+
//the result from bessel_test.cpp calculated by
83+
//ModuleBase::Sph_Bessel_Recursive::D1 is 0.124201140093879
84+
//reference result is calculated by Sphbes::Spherical_Bessel(msh,r,q,l,jl)
85+
EXPECT_NEAR(mean(jl,msh)/0.12420114009271901456, 1.0,doublethreshold);
86+
87+
//int l = 3;
88+
ModuleBase::Sphbes::Spherical_Bessel(msh,r,q,l3,jl);
89+
//the result from bessel_test.cpp calculated by
90+
//ModuleBase::Sph_Bessel_Recursive::D1 is 0.118268654505568
91+
//reference result is calculated by Sphbes::Spherical_Bessel(msh,r,q,l,jl)
92+
EXPECT_NEAR(mean(jl,msh)/0.11826865448477598408, 1.0,doublethreshold);
93+
94+
95+
//int l = 4;
96+
ModuleBase::Sphbes::Spherical_Bessel(msh,r,q,l4,jl);
97+
//the result from bessel_test.cpp calculated by
98+
//ModuleBase::Sph_Bessel_Recursive::D1 is 0.0933871035384385
99+
//reference result is calculated by Sphbes::Spherical_Bessel(msh,r,q,l,jl)
100+
EXPECT_NEAR(mean(jl,msh)/0.093387100084701621383, 1.0,doublethreshold);
101+
102+
103+
//int l = 5;
104+
ModuleBase::Sphbes::Spherical_Bessel(msh,r,q,l5,jl);
105+
//the result from bessel_test.cpp calculated by
106+
//ModuleBase::Sph_Bessel_Recursive::D1 is 0.0603800487910689
107+
//reference result is calculated by Sphbes::Spherical_Bessel(msh,r,q,l,jl)
108+
EXPECT_NEAR(mean(jl,msh)/0.060380048719821471925, 1.0,doublethreshold);
109+
110+
111+
//int l = 6;
112+
ModuleBase::Sphbes::Spherical_Bessel(msh,r,q,l6,jl);
113+
//the result from bessel_test.cpp calculated by
114+
//ModuleBase::Sph_Bessel_Recursive::D1 is 0.0327117051555907
115+
//reference result is calculated by Sphbes::Spherical_Bessel(msh,r,q,l,jl)
116+
EXPECT_NEAR(mean(jl,msh)/0.032711705053977857549, 1.0,doublethreshold);
117+
118+
119+
//int l = 7;
120+
ModuleBase::Sphbes::Spherical_Bessel(msh,r,q,l7,jl);
121+
//the result from bessel_test.cpp calculated by
122+
//ModuleBase::Sph_Bessel_Recursive::D1 is 0.0152155566653926
123+
//reference result is calculated by Sphbes::Spherical_Bessel(msh,r,q,l,jl)
124+
EXPECT_NEAR(mean(jl,msh)/0.015215556095798710851, 1.0,doublethreshold);
125+
}
126+
127+
int main(int argc, char **argv)
128+
{
129+
#ifdef __MPI
130+
MPI_Init(&argc, &argv);
131+
#endif
132+
133+
testing::InitGoogleTest(&argc, argv);
134+
int result = RUN_ALL_TESTS();
135+
#ifdef __MPI
136+
MPI_Finalize();
137+
#endif
138+
return result;
139+
}

0 commit comments

Comments
 (0)