Skip to content

Commit 65347e7

Browse files
authored
Test: add serial ut for the pw_basis class (#2225)
Co-authored-by: root <hongriTianqi>
1 parent 1bf3efa commit 65347e7

File tree

3 files changed

+392
-0
lines changed

3 files changed

+392
-0
lines changed

source/module_basis/module_pw/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ endif()
2323

2424
if(BUILD_TESTING)
2525
add_subdirectory(test)
26+
add_subdirectory(test_serial)
2627
add_subdirectory(kernels/test)
2728
endif()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
remove_definitions(-D__MPI)
2+
remove_definitions(-D__EXX)
3+
remove_definitions(-D__CUDA)
4+
remove_definitions(-D__ROCM)
5+
remove_definitions(-D__DEEPKS)
6+
AddTest(
7+
TARGET basis_pw_serial
8+
LIBS ${math_libs}
9+
SOURCES pw_basis_test.cpp ../pw_basis.cpp ../pw_init.cpp
10+
../pw_distributeg.cpp
11+
../pw_distributer.cpp
12+
../pw_distributeg_method1.cpp
13+
../pw_distributeg_method2.cpp
14+
../fft.cpp
15+
../../../module_base/blas_connector.h
16+
../../../module_base/matrix.cpp
17+
../../../module_base/matrix3.cpp
18+
../../../module_base/timer.cpp
19+
../../../module_base/mymath.cpp
20+
../../../module_base/tool_quit.cpp
21+
../../../module_base/memory.cpp
22+
../../../module_base/global_variable.cpp
23+
../../../module_base/global_function.cpp
24+
../../../module_base/global_file.cpp
25+
)
Lines changed: 366 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,366 @@
1+
#include "gtest/gtest.h"
2+
#include "module_base/global_function.h"
3+
#include "module_base/constants.h"
4+
#include "module_base/matrix3.h"
5+
6+
/************************************************
7+
* serial unit test of functions in pw_basis.cpp
8+
***********************************************/
9+
10+
/**
11+
* - Tested Functions:
12+
* - Constructor
13+
* - PW_Basis() and ~PW_Basis()
14+
* - Initgrids1
15+
* - initgrids() from gridecut
16+
* - Initgrids2
17+
* - initgrids() from nx,ny,nz
18+
* - Initparameters
19+
* - initparameters() for fft
20+
* - Setfullpw
21+
* - setfullpw(): set controlling parameters to get full pw in orbital free calculations
22+
* - DistributeR
23+
* - distribute_r(): distribute real space grids in z direction
24+
* - DistributeMethod1
25+
* - distribute_g() and distribution_method1(): set and distribute sticks
26+
* - DistributeMethod2
27+
* - distribute_g() and distribution_method2(): set and distribute sticks
28+
* - GetStartGR
29+
* - getstartgr(): get nmaxgr, numg, numr, startg, startr
30+
* - SetupTransform
31+
* - setuptransform(): for fft transform
32+
* - CollectLocalPW
33+
* - collect_local_pw: get gg, gdirect, gcar for local npw plane waves
34+
* - CollectUniqgg
35+
* - collect_uniqgg: get uniq gg without duplication in length
36+
*/
37+
38+
#define protected public
39+
#define private public
40+
#include "../pw_basis.h"
41+
#include "../fft.h"
42+
class PWBasisTEST: public testing::Test
43+
{
44+
public:
45+
std::string precision_flag = "double";
46+
std::string device_flag = "cpu";
47+
ModulePW::PW_Basis pwb;
48+
ModulePW::PW_Basis pwb1;
49+
void SetUp()
50+
{}
51+
void TearDown()
52+
{}
53+
};
54+
55+
TEST_F(PWBasisTEST,Constructor)
56+
{
57+
ModulePW::PW_Basis pwb2(device_flag, precision_flag);
58+
EXPECT_EQ(pwb1.classname,"PW_Basis");
59+
EXPECT_EQ(pwb2.classname,"PW_Basis");
60+
EXPECT_EQ(pwb2.ft.device,"cpu");
61+
EXPECT_EQ(pwb2.ft.precision,"double");
62+
}
63+
64+
TEST_F(PWBasisTEST,Initgrids1)
65+
{
66+
double lat0 = 1.8897261254578281;
67+
ModuleBase::Matrix3 latvec(10.0,0.0,0.0,
68+
0.0,10.0,0.0,
69+
0.0,0.0,10.0);
70+
double gridecut=10.0;
71+
pwb.initgrids(lat0,latvec,gridecut);
72+
EXPECT_DOUBLE_EQ(pwb.lat0,lat0);
73+
EXPECT_DOUBLE_EQ(pwb.tpiba,ModuleBase::TWO_PI/lat0);
74+
EXPECT_DOUBLE_EQ(pwb.tpiba2,pwb.tpiba*pwb.tpiba);
75+
EXPECT_DOUBLE_EQ(pwb.latvec.e11,latvec.e11);
76+
EXPECT_DOUBLE_EQ(pwb.GT.e11,latvec.Inverse().e11);
77+
EXPECT_DOUBLE_EQ(pwb.G.e11,pwb.GT.Transpose().e11);
78+
EXPECT_DOUBLE_EQ(pwb.GGT.e11,(pwb.G*pwb.GT).e11);
79+
EXPECT_DOUBLE_EQ(pwb.gridecut_lat,gridecut/pwb.tpiba2);
80+
EXPECT_NEAR(pwb.gridecut_lat,0.904561,1e-4);
81+
EXPECT_EQ(pwb.nx,20);
82+
EXPECT_EQ(pwb.ny,20);
83+
EXPECT_EQ(pwb.nz,20);
84+
EXPECT_TRUE(pwb.nx%2==0 || pwb.nx%3==0 || pwb.nx%5==0);
85+
EXPECT_TRUE(pwb.ny%2==0 || pwb.ny%3==0 || pwb.ny%5==0);
86+
EXPECT_TRUE(pwb.nz%2==0 || pwb.nz%3==0 || pwb.nz%5==0);
87+
}
88+
89+
TEST_F(PWBasisTEST,Initgrids2)
90+
{
91+
double lat0 = 1.8897261254578281;
92+
ModuleBase::Matrix3 latvec(10.0,0.0,0.0,
93+
0.0,10.0,0.0,
94+
0.0,0.0,10.0);
95+
int nx_in = 20;
96+
int ny_in = 20;
97+
int nz_in = 20;
98+
pwb.initgrids(lat0,latvec,nx_in,ny_in,nz_in);
99+
EXPECT_DOUBLE_EQ(pwb.lat0,lat0);
100+
EXPECT_DOUBLE_EQ(pwb.tpiba,ModuleBase::TWO_PI/lat0);
101+
EXPECT_DOUBLE_EQ(pwb.tpiba2,pwb.tpiba*pwb.tpiba);
102+
EXPECT_DOUBLE_EQ(pwb.latvec.e11,latvec.e11);
103+
EXPECT_DOUBLE_EQ(pwb.GT.e11,latvec.Inverse().e11);
104+
EXPECT_DOUBLE_EQ(pwb.G.e11,pwb.GT.Transpose().e11);
105+
EXPECT_DOUBLE_EQ(pwb.GGT.e11,(pwb.G*pwb.GT).e11);
106+
EXPECT_EQ(pwb.nx,nx_in);
107+
EXPECT_EQ(pwb.ny,ny_in);
108+
EXPECT_EQ(pwb.nz,nz_in);
109+
EXPECT_NEAR(pwb.gridecut_lat,0.999999,1e-4);
110+
EXPECT_NEAR(pwb.gridecut_lat*pwb.tpiba2,11.0551,1e-4);
111+
}
112+
113+
TEST_F(PWBasisTEST,Initparameters)
114+
{
115+
double lat0 = 1.8897261254578281;
116+
ModuleBase::Matrix3 latvec(10.0,0.0,0.0,
117+
0.0,10.0,0.0,
118+
0.0,0.0,10.0);
119+
double gridecut=10.0;
120+
//initparameters is always called after initgrids
121+
//because of nx,ny,nz, and tpiba2
122+
pwb.initgrids(lat0,latvec,gridecut);
123+
bool gamma_only_in = true;
124+
double pwecut_in = 11.0;
125+
int distribution_type_in = 1;
126+
bool xprime_in = true;
127+
pwb.initparameters(gamma_only_in,pwecut_in,distribution_type_in,xprime_in);
128+
EXPECT_EQ(pwb.xprime,xprime_in);
129+
EXPECT_EQ(pwb.gamma_only,gamma_only_in);
130+
EXPECT_EQ(pwb.xprime,xprime_in);
131+
EXPECT_TRUE(pwb.gamma_only);
132+
EXPECT_TRUE(pwb.xprime);
133+
EXPECT_EQ(pwb.fftnx,int(pwb.nx/2)+1);
134+
EXPECT_EQ(pwb.fftny,pwb.ny);
135+
EXPECT_EQ(pwb.fftnz,pwb.nz);
136+
EXPECT_EQ(pwb.ggecut,pwb.gridecut_lat);
137+
EXPECT_EQ(pwb.distribution_type,distribution_type_in);
138+
}
139+
140+
TEST_F(PWBasisTEST,Setfullpw)
141+
{
142+
bool inpt_full_pw = false;
143+
int inpt_full_pw_dim = 2;
144+
pwb.setfullpw(inpt_full_pw,inpt_full_pw_dim);
145+
EXPECT_FALSE(pwb.full_pw);
146+
EXPECT_EQ(pwb.full_pw_dim,0);
147+
}
148+
149+
TEST_F(PWBasisTEST,DistributeR)
150+
{
151+
//distribute_r depends on initgrids
152+
//because of nz
153+
double lat0 = 1.8897261254578281;
154+
ModuleBase::Matrix3 latvec(10.0,0.0,0.0,
155+
0.0,10.0,0.0,
156+
0.0,0.0,10.0);
157+
double gridecut=10.0;
158+
//initparameters is always called after initgrids
159+
//because of nx,ny,nz, and tpiba2
160+
pwb.initgrids(lat0,latvec,gridecut);
161+
//this is serial test, so that
162+
EXPECT_EQ(pwb.poolrank,0);
163+
EXPECT_EQ(pwb.poolnproc,1);
164+
pwb.distribute_r();
165+
EXPECT_EQ(pwb.startz[0],0);
166+
EXPECT_EQ(pwb.numz[0],pwb.nz);
167+
EXPECT_EQ(pwb.nplane,pwb.nz);
168+
EXPECT_EQ(pwb.nplane,20);
169+
EXPECT_EQ(pwb.nxy,400);
170+
EXPECT_EQ(pwb.nrxx,pwb.numz[0]*pwb.nxy);
171+
}
172+
173+
TEST_F(PWBasisTEST,DistributeMethod1)
174+
{
175+
double lat0 = 1.8897261254578281;
176+
ModuleBase::Matrix3 latvec(10.0,0.0,0.0,
177+
0.0,10.0,0.0,
178+
0.0,0.0,10.0);
179+
double gridecut=10.0;
180+
//initparameters is always called after initgrids
181+
//because of nx,ny,nz, and tpiba2
182+
//call initgrids
183+
pwb.initgrids(lat0,latvec,gridecut);
184+
bool gamma_only_in = true;
185+
double pwecut_in = 11.0;
186+
int distribution_type_in = 1;
187+
bool xprime_in = true;
188+
//call initparameters
189+
pwb.initparameters(gamma_only_in,pwecut_in,distribution_type_in,xprime_in);
190+
EXPECT_TRUE(pwb.gamma_only);
191+
EXPECT_TRUE(pwb.xprime);
192+
EXPECT_EQ(pwb.fftnx,int(pwb.nx/2)+1);
193+
EXPECT_EQ(pwb.fftny,pwb.ny);
194+
EXPECT_EQ(pwb.fftnz,pwb.nz);
195+
EXPECT_EQ(pwb.distribution_type,distribution_type_in);
196+
EXPECT_EQ(pwb.fftnxy,pwb.fftnx*pwb.fftny);
197+
EXPECT_EQ(pwb.fftnx,11);
198+
EXPECT_EQ(pwb.fftny,20);
199+
EXPECT_EQ(pwb.fftnz,20);
200+
//distribute_method1 depends on initparamters
201+
//because of fftnxy
202+
EXPECT_EQ(pwb.fftnxy,220);
203+
EXPECT_EQ(pwb.distribution_type,1);
204+
//call distribute_g
205+
pwb.distribute_g();
206+
EXPECT_EQ(pwb.npwtot,1994);
207+
EXPECT_EQ(pwb.nstot,156);
208+
}
209+
210+
TEST_F(PWBasisTEST,DistributeMethod2)
211+
{
212+
double lat0 = 1.8897261254578281;
213+
ModuleBase::Matrix3 latvec(10.0,0.0,0.0,
214+
0.0,10.0,0.0,
215+
0.0,0.0,10.0);
216+
double gridecut=10.0;
217+
//initparameters is always called after initgrids
218+
//because of nx,ny,nz, and tpiba2
219+
//call initgrids
220+
pwb.initgrids(lat0,latvec,gridecut);
221+
bool gamma_only_in = true;
222+
double pwecut_in = 11.0;
223+
int distribution_type_in = 2;
224+
bool xprime_in = true;
225+
//call initparameters
226+
pwb.initparameters(gamma_only_in,pwecut_in,distribution_type_in,xprime_in);
227+
EXPECT_TRUE(pwb.gamma_only);
228+
EXPECT_TRUE(pwb.xprime);
229+
EXPECT_EQ(pwb.fftnx,int(pwb.nx/2)+1);
230+
EXPECT_EQ(pwb.fftny,pwb.ny);
231+
EXPECT_EQ(pwb.fftnz,pwb.nz);
232+
EXPECT_EQ(pwb.distribution_type,distribution_type_in);
233+
EXPECT_EQ(pwb.fftnxy,pwb.fftnx*pwb.fftny);
234+
EXPECT_EQ(pwb.fftnx,11);
235+
EXPECT_EQ(pwb.fftny,20);
236+
EXPECT_EQ(pwb.fftnz,20);
237+
//distribute_method1 depends on initparamters
238+
//because of fftnxy
239+
EXPECT_EQ(pwb.fftnxy,220);
240+
EXPECT_EQ(pwb.distribution_type,2);
241+
//call distribute_g
242+
pwb.distribute_g();
243+
EXPECT_EQ(pwb.npwtot,1994);
244+
EXPECT_EQ(pwb.nstot,156);
245+
EXPECT_EQ(pwb.npw,1994);
246+
EXPECT_EQ(pwb.nst,156);
247+
EXPECT_EQ(pwb.nstnz,3120);
248+
}
249+
250+
TEST_F(PWBasisTEST,GetStartGR)
251+
{
252+
//getstartgr is called after distribute_r and distribute_g in setuptransform
253+
double lat0 = 1.8897261254578281;
254+
ModuleBase::Matrix3 latvec(10.0,0.0,0.0,
255+
0.0,10.0,0.0,
256+
0.0,0.0,10.0);
257+
double gridecut=10.0;
258+
//initparameters is always called after initgrids
259+
//because of nx,ny,nz, and tpiba2
260+
//call initgrids
261+
pwb.initgrids(lat0,latvec,gridecut);
262+
//call distribute_r
263+
pwb.distribute_r();
264+
bool gamma_only_in = true;
265+
double pwecut_in = 11.0;
266+
int distribution_type_in = 2;
267+
bool xprime_in = true;
268+
//call initparameters
269+
pwb.initparameters(gamma_only_in,pwecut_in,distribution_type_in,xprime_in);
270+
//call distribute_g
271+
pwb.distribute_g();
272+
//call getstartgr
273+
pwb.getstartgr();
274+
EXPECT_TRUE(pwb.gamma_only);
275+
EXPECT_EQ(pwb.npw,1994);
276+
EXPECT_EQ(pwb.nz,20);
277+
EXPECT_EQ(pwb.nst,156);
278+
EXPECT_EQ(pwb.nrxx,8000);
279+
EXPECT_EQ(pwb.nxy,400);
280+
EXPECT_EQ(pwb.nplane,20);
281+
EXPECT_EQ(pwb.nmaxgr,8000);
282+
EXPECT_EQ(pwb.numg[0],3120);
283+
EXPECT_EQ(pwb.numr[0],3120);
284+
EXPECT_EQ(pwb.startg[0],0);
285+
EXPECT_EQ(pwb.startr[0],0);
286+
}
287+
288+
TEST_F(PWBasisTEST,SetupTransform)
289+
{
290+
//getstartgr is called after distribute_r and distribute_g in setuptransform
291+
double lat0 = 1.8897261254578281;
292+
ModuleBase::Matrix3 latvec(10.0,0.0,0.0,
293+
0.0,10.0,0.0,
294+
0.0,0.0,10.0);
295+
double gridecut=10.0;
296+
//initparameters is always called after initgrids
297+
//because of nx,ny,nz, and tpiba2
298+
//call initgrids
299+
pwb.initgrids(lat0,latvec,gridecut);
300+
bool gamma_only_in = true;
301+
double pwecut_in = 11.0;
302+
int distribution_type_in = 2;
303+
bool xprime_in = true;
304+
pwb.initparameters(gamma_only_in,pwecut_in,distribution_type_in,xprime_in);
305+
//setuptransform for FFT
306+
//which calls fft planning functions
307+
//currently this is just a trivial test to see its successfull calling
308+
EXPECT_NO_THROW(pwb.setuptransform());
309+
EXPECT_EQ(pwb.npw,1994);
310+
}
311+
312+
TEST_F(PWBasisTEST,CollectLocalPW)
313+
{
314+
//getstartgr is called after distribute_r and distribute_g in setuptransform
315+
double lat0 = 1.8897261254578281;
316+
ModuleBase::Matrix3 latvec(10.0,0.0,0.0,
317+
0.0,10.0,0.0,
318+
0.0,0.0,10.0);
319+
double gridecut=10.0;
320+
//initparameters is always called after initgrids
321+
//because of nx,ny,nz, and tpiba2
322+
//call initgrids
323+
pwb.initgrids(lat0,latvec,gridecut);
324+
bool gamma_only_in = true;
325+
double pwecut_in = 11.0;
326+
int distribution_type_in = 2;
327+
bool xprime_in = true;
328+
pwb.initparameters(gamma_only_in,pwecut_in,distribution_type_in,xprime_in);
329+
//setuptransform for FFT
330+
//which calls fft planning functions
331+
//currently this is just a trivial test to see its successfull calling
332+
EXPECT_NO_THROW(pwb.setuptransform());
333+
EXPECT_EQ(pwb.npw,1994);
334+
pwb.collect_local_pw();
335+
EXPECT_EQ(pwb.ig_gge0,9);
336+
}
337+
338+
TEST_F(PWBasisTEST,CollectUniqgg)
339+
{
340+
//getstartgr is called after distribute_r and distribute_g in setuptransform
341+
double lat0 = 1.8897261254578281;
342+
ModuleBase::Matrix3 latvec(10.0,0.0,0.0,
343+
0.0,10.0,0.0,
344+
0.0,0.0,10.0);
345+
double gridecut=10.0;
346+
//initparameters is always called after initgrids
347+
//because of nx,ny,nz, and tpiba2
348+
//call initgrids
349+
pwb.initgrids(lat0,latvec,gridecut);
350+
bool gamma_only_in = true;
351+
double pwecut_in = 11.0;
352+
int distribution_type_in = 2;
353+
bool xprime_in = true;
354+
pwb.initparameters(gamma_only_in,pwecut_in,distribution_type_in,xprime_in);
355+
//setuptransform for FFT
356+
//which calls fft planning functions
357+
//currently this is just a trivial test to see its successfull calling
358+
EXPECT_NO_THROW(pwb.setuptransform());
359+
EXPECT_EQ(pwb.npw,1994);
360+
pwb.collect_local_pw();
361+
EXPECT_EQ(pwb.ig_gge0,9);
362+
pwb.collect_uniqgg();
363+
EXPECT_EQ(pwb.ngg,78);
364+
}
365+
#undef private
366+
#undef protected

0 commit comments

Comments
 (0)