Skip to content

Commit fedc0fa

Browse files
Cstandardlibpre-commit-ci-lite[bot]
authored andcommitted
Tests: add checks for opening data files by hsolver tests (deepmodeling#5823)
* Tests: add checks for opening data files by hsolver tests * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 09a328c commit fedc0fa

File tree

7 files changed

+65
-10
lines changed

7 files changed

+65
-10
lines changed

source/module_hsolver/test/diago_bpcg_test.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ void lapackEigen(int &npw, std::vector<std::complex<double>> &hm, double *e, boo
4646
char tmp_c1 = 'V', tmp_c2 = 'U';
4747
zheev_(&tmp_c1, &tmp_c2, &npw, hm.data(), &npw, e, work2, &lwork, rwork, &info);
4848
end = clock();
49-
if (outtime)
49+
if (outtime) {
5050
std::cout << "Lapack Run time: " << (double)(end - start) / CLOCKS_PER_SEC << " S" << std::endl;
51+
}
5152
delete[] rwork;
5253
delete[] work2;
5354
}
@@ -78,7 +79,8 @@ class DiagoBPCGPrepare
7879
// calculate eigenvalues by LAPACK;
7980
double *e_lapack = new double[npw];
8081
auto ev = DIAGOTEST::hmatrix;
81-
if(mypnum == 0) lapackEigen(npw, ev, e_lapack, false);
82+
if(mypnum == 0) { lapackEigen(npw, ev, e_lapack, false);
83+
}
8284
// initial guess of psi by perturbing lapack psi
8385
ModuleBase::ComplexMatrix psiguess(nband, npw);
8486
std::default_random_engine p(1);
@@ -248,7 +250,14 @@ TEST(DiagoBPCGTest, readH)
248250
// read Hamilt matrix from file data-H
249251
std::vector<std::complex<double>> hm;
250252
std::ifstream ifs;
251-
ifs.open("H-KPoints-Si64.dat");
253+
std::string filename = "H-KPoints-Si64.dat";
254+
ifs.open(filename);
255+
// open file and check status
256+
if (!ifs.is_open())
257+
{
258+
std::cout << "Error opening file " << filename << std::endl;
259+
exit(1);
260+
}
252261
DIAGOTEST::readh(ifs, hm);
253262
ifs.close();
254263
int dim = DIAGOTEST::npw;
@@ -280,7 +289,8 @@ int main(int argc, char **argv)
280289

281290
testing::InitGoogleTest(&argc, argv);
282291
::testing::TestEventListeners &listeners = ::testing::UnitTest::GetInstance()->listeners();
283-
if (myrank != 0) delete listeners.Release(listeners.default_result_printer());
292+
if (myrank != 0) { delete listeners.Release(listeners.default_result_printer());
293+
}
284294

285295
int result = RUN_ALL_TESTS();
286296
if (myrank == 0 && result != 0)

source/module_hsolver/test/diago_cg_float_test.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,14 @@ TEST(DiagoCGFloatTest, readH)
314314
// read Hamilt matrix from file data-H
315315
std::vector<std::complex<float>> hm;
316316
std::ifstream ifs;
317-
ifs.open("H-KPoints-Si64.dat");
317+
std::string filename = "H-KPoints-Si64.dat";
318+
// open file and check status
319+
ifs.open(filename);
320+
if (!ifs.is_open())
321+
{
322+
std::cout << "Error opening file " << filename << std::endl;
323+
exit(1);
324+
}
318325
DIAGOTEST::readh(ifs, hm);
319326
ifs.close();
320327
int dim = DIAGOTEST::npw;

source/module_hsolver/test/diago_cg_real_test.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,14 @@ TEST(DiagoCGTest, readH)
289289
// read Hamilt matrix from file data-H
290290
std::vector<double> hm;
291291
std::ifstream ifs;
292-
ifs.open("H-GammaOnly-Si64.dat");
292+
std::string filename = "H-GammaOnly-Si64.dat";
293+
ifs.open(filename);
294+
// open file and check status
295+
if (!ifs.is_open())
296+
{
297+
std::cout << "Error opening file " << filename << std::endl;
298+
exit(1);
299+
}
293300
DIAGOTEST::readh(ifs, hm);
294301
ifs.close();
295302
int dim = DIAGOTEST::npw;

source/module_hsolver/test/diago_cg_test.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,14 @@ TEST(DiagoCGTest, readH)
309309
// read Hamilt matrix from file data-H
310310
std::vector<std::complex<double>> hm;
311311
std::ifstream ifs;
312-
ifs.open("H-KPoints-Si64.dat");
312+
std::string filename = "H-KPoints-Si64.dat";
313+
ifs.open(filename);
314+
// open file and check status
315+
if (!ifs.is_open())
316+
{
317+
std::cout << "Error opening file " << filename << std::endl;
318+
exit(1);
319+
}
313320
DIAGOTEST::readh(ifs, hm);
314321
ifs.close();
315322
int dim = DIAGOTEST::npw;

source/module_hsolver/test/diago_david_float_test.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,15 @@ INSTANTIATE_TEST_SUITE_P(VerifyDiag,DiagoDavTest,::testing::Values(
196196
TEST(DiagoDavRealSystemTest,dataH)
197197
{
198198
std::vector<std::complex<float>> hmatrix;
199-
std::ifstream ifs("H-KPoints-Si64.dat");
199+
std::ifstream ifs;
200+
std::string filename = "H-KPoints-Si64.dat";
201+
ifs.open(filename);
202+
// open file and check status
203+
if (!ifs.is_open())
204+
{
205+
std::cout << "Error opening file " << filename << std::endl;
206+
exit(1);
207+
}
200208
DIAGOTEST::readh(ifs,hmatrix);
201209
ifs.close();
202210
DIAGOTEST::hmatrix_f = hmatrix;

source/module_hsolver/test/diago_david_real_test.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,15 @@ INSTANTIATE_TEST_SUITE_P(VerifyDiag, DiagoDavTest, ::testing::Values(
194194
TEST(DiagoDavRealSystemTest, dataH)
195195
{
196196
std::vector<double> hmatrix;
197-
std::ifstream ifs("H-GammaOnly-Si64.dat");
197+
std::ifstream ifs;
198+
std::string filename = "H-GammaOnly-Si64.dat";
199+
ifs.open(filename);
200+
// open file and check status
201+
if (!ifs.is_open())
202+
{
203+
std::cout << "Error opening file " << filename << std::endl;
204+
exit(1);
205+
}
198206
DIAGOTEST::readh(ifs, hmatrix);
199207
ifs.close();
200208
DIAGOTEST::hmatrix_d = hmatrix;

source/module_hsolver/test/diago_david_test.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,15 @@ INSTANTIATE_TEST_SUITE_P(VerifyDiag,DiagoDavTest,::testing::Values(
198198
TEST(DiagoDavRealSystemTest, dataH)
199199
{
200200
std::vector<std::complex<double>> hmatrix;
201-
std::ifstream ifs("H-KPoints-Si64.dat");
201+
std::ifstream ifs;
202+
std::string filename = "H-KPoints-Si64.dat";
203+
ifs.open(filename);
204+
// open file and check status
205+
if (!ifs.is_open())
206+
{
207+
std::cout << "Error opening file " << filename << std::endl;
208+
exit(1);
209+
}
202210
DIAGOTEST::readh(ifs,hmatrix);
203211
ifs.close();
204212
DIAGOTEST::hmatrix = hmatrix;

0 commit comments

Comments
 (0)