Skip to content

Commit 0fdb97f

Browse files
committed
Test: Address DeePKS unit test review comments
1 parent ba42c6c commit 0fdb97f

12 files changed

Lines changed: 98 additions & 5052 deletions

File tree

source/source_lcao/module_deepks/test/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/support DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
2+
set(DEEPKS_UT_PP_ORB_DIR "${PROJECT_SOURCE_DIR}/tests/PP_ORB")
3+
configure_file(
4+
${CMAKE_CURRENT_SOURCE_DIR}/support/NO_GO_deepks_UT/STRU
5+
${CMAKE_CURRENT_BINARY_DIR}/support/NO_GO_deepks_UT/STRU
6+
@ONLY
7+
)
8+
configure_file(
9+
${CMAKE_CURRENT_SOURCE_DIR}/support/NO_KP_deepks_UT/STRU
10+
${CMAKE_CURRENT_BINARY_DIR}/support/NO_KP_deepks_UT/STRU
11+
@ONLY
12+
)
213

314
set(DEEPKS_UNIT_COMMON_SOURCES
415
deepks_test_prep.cpp

source/source_lcao/module_deepks/test/deepks_test.cpp

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
11
#include "deepks_test.h"
22

3+
#include <cerrno>
34
#include <complex>
5+
#include <cstdlib>
46
#include <gtest/gtest.h>
7+
#include <string>
8+
9+
namespace
10+
{
11+
bool parse_double_token(const std::string& token, double* value)
12+
{
13+
char* end = nullptr;
14+
errno = 0;
15+
const double parsed = std::strtod(token.c_str(), &end);
16+
if (end == token.c_str() || *end != '\0' || errno == ERANGE)
17+
{
18+
return false;
19+
}
20+
*value = parsed;
21+
return true;
22+
}
23+
24+
bool parse_complex_token(const std::string& token, double* real, double* imag)
25+
{
26+
if (token.size() < 5 || token[0] != '(' || token[token.size() - 1] != ')')
27+
{
28+
return false;
29+
}
30+
31+
const std::string value = token.substr(1, token.size() - 2);
32+
const std::string::size_type comma = value.find(',');
33+
if (comma == std::string::npos || comma == 0 || comma == value.size() - 1)
34+
{
35+
return false;
36+
}
37+
38+
return parse_double_token(value.substr(0, comma), real) && parse_double_token(value.substr(comma + 1), imag);
39+
}
40+
} // namespace
541

642
namespace Test_Deepks
743
{
@@ -44,21 +80,30 @@ void test_deepks<T>::assert_file_matches_reference(const std::string& actual_fil
4480
{
4581
ASSERT_TRUE(reference >> reference_word)
4682
<< reference_file << " has fewer entries than " << actual_file << " at entry " << entry;
47-
if ((actual_word[0] - '0' >= 0 && actual_word[0] - '0' < 10) || actual_word[0] == '-')
83+
84+
double actual_num = 0.0;
85+
double reference_num = 0.0;
86+
const bool actual_is_number = parse_double_token(actual_word, &actual_num);
87+
const bool reference_is_number = parse_double_token(reference_word, &reference_num);
88+
double actual_real = 0.0;
89+
double actual_imag = 0.0;
90+
double reference_real = 0.0;
91+
double reference_imag = 0.0;
92+
const bool actual_is_complex = parse_complex_token(actual_word, &actual_real, &actual_imag);
93+
const bool reference_is_complex = parse_complex_token(reference_word, &reference_real, &reference_imag);
94+
95+
if (actual_is_number || reference_is_number)
4896
{
49-
const double num1 = std::stod(actual_word);
50-
const double num2 = std::stod(reference_word);
51-
EXPECT_NEAR(num1, num2, test_thr) << "numeric mismatch at entry " << entry;
97+
ASSERT_TRUE(actual_is_number) << "Cannot parse actual numeric entry " << entry << ": " << actual_word;
98+
ASSERT_TRUE(reference_is_number)
99+
<< "Cannot parse reference numeric entry " << entry << ": " << reference_word;
100+
EXPECT_NEAR(actual_num, reference_num, test_thr) << "numeric mismatch at entry " << entry;
52101
}
53-
else if (actual_word[0] == '(' && actual_word[actual_word.size() - 1] == ')' && reference_word[0] == '('
54-
&& reference_word[reference_word.size() - 1] == ')')
102+
else if (actual_is_complex || reference_is_complex)
55103
{
56-
const std::string actual_str = actual_word.substr(1, actual_word.size() - 2);
57-
const std::string reference_str = reference_word.substr(1, reference_word.size() - 2);
58-
const double actual_real = std::stod(actual_str.substr(0, actual_str.find(',')));
59-
const double actual_imag = std::stod(actual_str.substr(actual_str.find(',') + 1));
60-
const double reference_real = std::stod(reference_str.substr(0, reference_str.find(',')));
61-
const double reference_imag = std::stod(reference_str.substr(reference_str.find(',') + 1));
104+
ASSERT_TRUE(actual_is_complex) << "Cannot parse actual complex entry " << entry << ": " << actual_word;
105+
ASSERT_TRUE(reference_is_complex)
106+
<< "Cannot parse reference complex entry " << entry << ": " << reference_word;
62107
EXPECT_NEAR(actual_real, reference_real, test_thr) << "complex real mismatch at entry " << entry;
63108
EXPECT_NEAR(actual_imag, reference_imag, test_thr) << "complex imag mismatch at entry " << entry;
64109
}

source/source_lcao/module_deepks/test/deepks_test_e_deltabands.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
template <typename T>
1010
void test_deepks<T>::cal_V_delta()
1111
{
12-
hamilt::HS_Matrix_K<T>* hsk = new hamilt::HS_Matrix_K<T>(&ParaO);
13-
hamilt::HContainer<double>* hR = new hamilt::HContainer<double>(ucell, &ParaO);
14-
hamilt::Operator<T>* op_deepks = new hamilt::DeePKS<hamilt::OperatorLCAO<T, double>>(hsk,
15-
kv.kvec_d,
16-
hR,
17-
&ucell,
18-
&Test_Deepks::GridD,
19-
&overlap_orb_alpha_,
20-
&ORB,
21-
kv.get_nkstot(),
22-
p_elec_DM,
23-
&this->ld);
12+
hamilt::HS_Matrix_K<T> hsk(&ParaO);
13+
hamilt::HContainer<double> hR(ucell, &ParaO);
14+
hamilt::DeePKS<hamilt::OperatorLCAO<T, double>> op_deepks(&hsk,
15+
kv.kvec_d,
16+
&hR,
17+
&ucell,
18+
&Test_Deepks::GridD,
19+
&overlap_orb_alpha_,
20+
&ORB,
21+
kv.get_nkstot(),
22+
p_elec_DM,
23+
&this->ld);
2424
for (int ik = 0; ik < kv.get_nkstot(); ++ik)
2525
{
26-
op_deepks->init(ik);
26+
op_deepks.init(ik);
2727
}
2828
}
2929

source/source_lcao/module_deepks/test/main_deepks.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ void prepare_workdir()
5959
std::ostringstream command;
6060
command << "rm -rf " << shell_quote(run_root) << " && "
6161
<< "mkdir -p " << shell_quote(run_root) << " && "
62-
<< "cp -R support/PP_ORB " << shell_quote(run_root + "/PP_ORB") << " && "
6362
<< "cp -R " << shell_quote("support/" + case_dir) << " " << shell_quote(run_root + "/" + case_dir);
6463

6564
ASSERT_EQ(std::system(command.str().c_str()), 0) << "Failed to prepare DeePKS unit-test work directory";
@@ -80,21 +79,25 @@ void run_typed_check()
8079
DEEPKS_UT_RUNNER(test);
8180
}
8281

83-
bool gamma_only_case()
82+
void gamma_only_case(bool* gamma_only_local)
8483
{
8584
std::ifstream ifs("INPUT");
8685
std::string key;
87-
bool gamma_only_local = false;
88-
ifs >> key >> gamma_only_local;
89-
return gamma_only_local;
86+
ASSERT_TRUE(ifs.is_open()) << "Cannot open DeePKS unit-test INPUT";
87+
ASSERT_TRUE(ifs >> key) << "Cannot read gamma_only_local key from DeePKS unit-test INPUT";
88+
ASSERT_EQ(key, "gamma_only_local") << "Unexpected first entry in DeePKS unit-test INPUT";
89+
ASSERT_TRUE(ifs >> *gamma_only_local) << "Cannot read gamma_only_local value from DeePKS unit-test INPUT";
9090
}
9191
} // namespace
9292

9393
TEST(DeePKSUnitTest, ConfiguredCheck)
9494
{
9595
prepare_workdir();
9696

97-
if (gamma_only_case())
97+
bool gamma_only_local = false;
98+
ASSERT_NO_FATAL_FAILURE(gamma_only_case(&gamma_only_local));
99+
100+
if (gamma_only_local)
98101
{
99102
run_typed_check<double>();
100103
}

source/source_lcao/module_deepks/test/support/NO_GO_deepks_UT/STRU

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
ATOMIC_SPECIES
2-
C 12.000 ../PP_ORB/C_ONCV_PBE-1.0.upf #Element, Mass, Pseudopotential
3-
H 1.008 ../PP_ORB/H_ONCV_PBE-1.0.upf
2+
C 12.000 @DEEPKS_UT_PP_ORB_DIR@/C_ONCV_PBE-1.0.upf #Element, Mass, Pseudopotential
3+
H 1.008 @DEEPKS_UT_PP_ORB_DIR@/H_ONCV_PBE-1.0.upf
44

55
NUMERICAL_ORBITAL
6-
../PP_ORB/C_gga_8au_100Ry_1s1p.orb
7-
../PP_ORB/H_gga_6au_60Ry_1s.orb
6+
@DEEPKS_UT_PP_ORB_DIR@/C_gga_8au_100Ry_1s1p.orb
7+
@DEEPKS_UT_PP_ORB_DIR@/H_gga_6au_60Ry_1s.orb
88

99
NUMERICAL_DESCRIPTOR
1010
jle.orb

source/source_lcao/module_deepks/test/support/NO_KP_deepks_UT/STRU

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ATOMIC_SPECIES
2-
O 1.00 ../PP_ORB/O_ONCV_PBE-1.0.upf
3-
H 1.00 ../PP_ORB/H_ONCV_PBE-1.0.upf
2+
O 1.00 @DEEPKS_UT_PP_ORB_DIR@/O_ONCV_PBE-1.0.upf
3+
H 1.00 @DEEPKS_UT_PP_ORB_DIR@/H_ONCV_PBE-1.0.upf
44

55
LATTICE_CONSTANT
66
1
@@ -24,8 +24,8 @@ H
2424
-0.2473714095935 -0.0346105497687 0.63538026574395 1 1 1
2525

2626
NUMERICAL_ORBITAL
27-
../PP_ORB/O_gga_6au_60Ry_1s1p.orb
28-
../PP_ORB/H_gga_6au_60Ry_1s.orb
27+
@DEEPKS_UT_PP_ORB_DIR@/O_gga_6au_60Ry_1s1p.orb
28+
@DEEPKS_UT_PP_ORB_DIR@/H_gga_6au_60Ry_1s.orb
2929

3030
NUMERICAL_DESCRIPTOR
3131
jle.orb

0 commit comments

Comments
 (0)