Skip to content

Commit 4639adb

Browse files
committed
- move function read_hs() to diago_elpa_utils.h
- rename H-KPonts.dat to be H-KPoints.dat - delete some redundant information output
1 parent 1e3d4f1 commit 4639adb

File tree

4 files changed

+64
-49
lines changed

4 files changed

+64
-49
lines changed

source/src_pdiag/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ AddTest(
44
SOURCES diago_elpa_test.cpp
55
)
66

7-
install(FILES H-KPonts.dat DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
7+
install(FILES H-KPoints.dat DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
88
install(FILES H-GammaOnly.dat DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
99
install(FILES S-KPoints.dat DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
1010
install(FILES S-GammaOnly.dat DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
File renamed without changes.

source/src_pdiag/test/diago_elpa_test.cpp

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
#define DETAILINFO true
99
#define PASSTHRESHOLD 1e-4
10-
#define PRINT_NEW_MATRIX false // print out the H matrix after doing block-cycle distribution
10+
#define PRINT_NEW_MATRIX true // print out the H matrix after doing block-cycle distribution
11+
#define PRINT_HS false
1112

1213
/************************************************
1314
* unit test of ELPA
@@ -42,10 +43,10 @@ template <class T> class ElpaPrepare
4243
int n = 10; // dimension of H/S matrix
4344
int nblk = 1; // dimension of sub-matrix
4445
int sparsity = 100; // sparsity of H matrix;
45-
T *hmatrix = 0;
46-
T *smatrix = 0;
47-
double *e_lapack = 0; // eigenvalues solvered by LAPACK
48-
double *e_elpa = 0;
46+
T *hmatrix = nullptr;
47+
T *smatrix = nullptr;
48+
double *e_lapack = nullptr; // eigenvalues solved by LAPACK
49+
double *e_elpa = nullptr;
4950
double lapack_time = 0.0, elpa_time = 0.0;
5051
int mypnum = 0;
5152

@@ -54,66 +55,54 @@ template <class T> class ElpaPrepare
5455
LCAO_DIAGO_TEST::random_hs<T>(hmatrix, smatrix, n, sparsity);
5556
}
5657

57-
inline void readmatrix(std::ifstream &inf, T *&matrix, int &npw)
58-
{
59-
inf >> npw;
60-
matrix = new T[npw * npw];
61-
for (int i = 0; i < npw; i++)
62-
{
63-
for (int j = i; j < npw; j++)
64-
{
65-
inf >> matrix[i * npw + j];
66-
if (i != j)
67-
{
68-
matrix[j * npw + i] = matrix[i * npw + j];
69-
}
70-
}
71-
}
72-
}
73-
74-
void read_HS()
58+
bool read_HS()
7559
{
60+
bool readhfile = false;
61+
bool readsfile = false;
7662
if (mypnum == 0)
7763
{
78-
int hdim = 1;
79-
int sdim = 1;
80-
81-
std::ifstream ifs;
64+
std::vector<T> htmp,stmp;
8265
if (std::is_same<T, double>::value)
8366
{
84-
ifs.open("H-GammaOnly.dat");
67+
readhfile = LCAO_DIAGO_TEST::read_hs<std::vector<T>>(std::string("H-GammaOnly.dat"),htmp);
68+
readsfile = LCAO_DIAGO_TEST::read_hs<std::vector<T>>(std::string("S-GammaOnly.dat"),stmp);
8569
}
8670
else if ((std::is_same<T, std::complex<double>>::value))
87-
ifs.open("H-KPonts.dat");
88-
readmatrix(ifs, hmatrix, hdim);
89-
ifs.close();
90-
91-
if (std::is_same<T, double>::value)
9271
{
93-
ifs.open("S-GammaOnly.dat");
72+
readhfile = LCAO_DIAGO_TEST::read_hs<std::vector<T>>(std::string("H-KPoints.dat"),htmp);
73+
readsfile = LCAO_DIAGO_TEST::read_hs<std::vector<T>>(std::string("S-KPoints.dat"),stmp);
9474
}
95-
else if ((std::is_same<T, std::complex<double>>::value))
96-
ifs.open("S-KPoints.dat");
97-
readmatrix(ifs, smatrix, sdim);
98-
ifs.close();
75+
if (htmp.size() != stmp.size())
76+
{
77+
printf("Error: dimensions of H and S are not equal, %d, %d", htmp.size(), stmp.size());
78+
readhfile = readsfile = false;
79+
}
80+
81+
hmatrix = new T[htmp.size()];
82+
smatrix = new T[stmp.size()];
9983

100-
if (hdim != sdim)
84+
for(int i=0;i<htmp.size();i++)
10185
{
102-
printf("Error: dimensions of H and S are not equal, %d, %d", hdim, sdim);
103-
exit(1);
86+
hmatrix[i] = htmp[i];
87+
smatrix[i] = stmp[i];
10488
}
105-
n = hdim;
89+
90+
n = sqrt(stmp.size());
10691
nblk = 1;
10792
}
10893
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
10994
MPI_Bcast(&nblk, 1, MPI_INT, 0, MPI_COMM_WORLD);
95+
MPI_Bcast(&readhfile, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD);
96+
MPI_Bcast(&readsfile, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD);
11097
e_lapack = new double[n];
11198
e_elpa = new double[n];
11299
for (int i = 0; i < n; i++)
113100
{
114101
e_lapack[i] = 0.0;
115102
e_elpa[i] = 1.0;
116103
}
104+
if (readhfile && readsfile) return true;
105+
else return false;
117106
}
118107

119108
void new_matrix()
@@ -131,6 +120,7 @@ template <class T> class ElpaPrepare
131120

132121
void print_hs()
133122
{
123+
if(! PRINT_HS) {return;}
134124
if (mypnum == 0)
135125
{
136126
std::ofstream fp("hmatrix.dat");
@@ -207,16 +197,16 @@ class ElpaComplexDoubleTest : public ::testing::TestWithParam<ElpaPrepare<std::c
207197
TEST(VerifyElpaDiaoDouble, ReadHS)
208198
{
209199
ElpaPrepare<double> edp;
210-
edp.read_HS();
211-
// edp.print_hs();
200+
ASSERT_TRUE(edp.read_HS());
201+
edp.print_hs();
212202
edp.run_diago();
213203
}
214204

215205
TEST(VerifyElpaDiaoComplexDouble, ReadHS)
216206
{
217207
ElpaPrepare<std::complex<double>> edp;
218-
edp.read_HS();
219-
// edp.print_hs();
208+
ASSERT_TRUE(edp.read_HS());
209+
edp.print_hs();
220210
edp.run_diago();
221211
}
222212

source/src_pdiag/test/diago_elpa_utils.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,32 @@ template <class T> inline void print_matrix(std::ofstream &fp, T *matrix, int &n
136136
}
137137
}
138138

139+
template <class T> bool read_hs(std::string fname, T &matrix)
140+
{
141+
int ndim;
142+
std::ifstream inf(fname);
143+
if(! inf.is_open())
144+
{
145+
std::cout << "Error: open file " << fname << " failed, skip!" << std::endl;
146+
return false;
147+
}
148+
inf >> ndim;
149+
matrix.resize(ndim*ndim);
150+
for (int i = 0; i < ndim; i++)
151+
{
152+
for (int j = i; j < ndim; j++)
153+
{
154+
inf >> matrix[i * ndim + j];
155+
if (i != j)
156+
{
157+
matrix[j * ndim + i] = matrix[i * ndim + j];
158+
}
159+
}
160+
}
161+
inf.close();
162+
return true;
163+
}
164+
139165
int elpa_sethandle(elpa_t &handle,
140166
int nFull,
141167
int nev,
@@ -322,7 +348,6 @@ void lapack_diago(double *hmatrix, double *smatrix, double *e, int &nFull)
322348
}
323349

324350
dsygv_(&itype, &jobz, &uplo, &nFull, a, &nFull, b, &nFull, e, ev, &lwork, &info);
325-
std::cout << "lapack solver over: e[0]=" << e[0] << std::endl;
326351
if (info != 0)
327352
{
328353
std::cout << "ERROR: solvered by LAPACK error, info=" << info << std::endl;
@@ -364,12 +389,12 @@ void lapack_diago(std::complex<double> *hmatrix, std::complex<double> *smatrix,
364389
delete[] rwork;
365390
}
366391

367-
// produce the random H/S matrix
368392
double conj(double &a)
369393
{
370394
return a;
371395
}
372396

397+
// produce the random H/S matrix
373398
template <class T> void random_hs(T *hmatrix, T *smatrix, int &n, int &sparsity)
374399
{
375400
int min = 0;

0 commit comments

Comments
 (0)