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