Skip to content

Commit 6320309

Browse files
sunliang98Copilot
andauthored
Refactor: Improve stability of KSDFT-generated ML descriptors (#7431)
* Enhance the stability of of_ml_gene_data * Test: Add an integrate test 01_PW/103_PW_Gene_Descriptors for of_ml_gene_data * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Test: Add a unit test for of_ml_gene_data, and update Autotest.sh * docs: trigger readthedocs rebuild --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 260a811 commit 6320309

11 files changed

Lines changed: 208 additions & 3 deletions

File tree

source/source_io/module_parameter/read_input_item_ofdft.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,15 @@ Note: Even dimensions may cause slight errors in FFT. It should be ignorable in
382382
item.default_value = "False";
383383
item.unit = "";
384384
item.availability = "Used only for KSDFT with plane wave basis";
385+
item.check_value = [](const Input_Item& item, const Parameter& para) {
386+
if (para.input.of_ml_gene_data
387+
&& (para.input.esolver_type != "ksdft" || para.input.basis_type != "pw" || GlobalV::NPROC != 1))
388+
{
389+
ModuleBase::WARNING_QUIT(
390+
"ReadInput",
391+
"of_ml_gene_data is only available for KSDFT with PW basis on a single MPI rank (NPROC = 1)");
392+
}
393+
};
385394
read_sync_bool(input.of_ml_gene_data);
386395
this->add_item(item);
387396
}

source/source_io/test_serial/read_input_item_test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,36 @@ TEST_F(InputTest, Item_test2)
15281528
it->second.reset_value(it->second, param);
15291529
EXPECT_EQ(param.input.of_read_kernel, false);
15301530
}
1531+
{ // of_ml_gene_data
1532+
auto it = find_label("of_ml_gene_data", readinput.input_lists);
1533+
1534+
param.input.of_ml_gene_data = true;
1535+
param.input.esolver_type = "ofdft";
1536+
param.input.basis_type = "pw";
1537+
GlobalV::NPROC = 1;
1538+
testing::internal::CaptureStdout();
1539+
EXPECT_EXIT(it->second.check_value(it->second, param), ::testing::ExitedWithCode(1), "");
1540+
output = testing::internal::GetCapturedStdout();
1541+
EXPECT_THAT(output, testing::HasSubstr("NOTICE"));
1542+
1543+
param.input.of_ml_gene_data = true;
1544+
param.input.esolver_type = "ksdft";
1545+
param.input.basis_type = "lcao";
1546+
GlobalV::NPROC = 1;
1547+
testing::internal::CaptureStdout();
1548+
EXPECT_EXIT(it->second.check_value(it->second, param), ::testing::ExitedWithCode(1), "");
1549+
output = testing::internal::GetCapturedStdout();
1550+
EXPECT_THAT(output, testing::HasSubstr("NOTICE"));
1551+
1552+
param.input.of_ml_gene_data = true;
1553+
param.input.esolver_type = "ksdft";
1554+
param.input.basis_type = "pw";
1555+
GlobalV::NPROC = 2;
1556+
testing::internal::CaptureStdout();
1557+
EXPECT_EXIT(it->second.check_value(it->second, param), ::testing::ExitedWithCode(1), "");
1558+
output = testing::internal::GetCapturedStdout();
1559+
EXPECT_THAT(output, testing::HasSubstr("NOTICE"));
1560+
}
15311561
{ // dft_plus_u
15321562
auto it = find_label("dft_plus_u", readinput.input_lists);
15331563
param.input.dft_plus_u = 1;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
INPUT_PARAMETERS
2+
#Parameters (1.General)
3+
suffix autotest
4+
calculation scf
5+
nbands 3
6+
symmetry 1
7+
pseudo_dir ../../PP_ORB
8+
pseudo_rcut 16
9+
10+
#Parameters (2.Iteration)
11+
ecutwfc 10
12+
scf_nmax 100
13+
dft_functional XC_GGA_X_PBE+XC_GGA_C_PBE
14+
15+
16+
#Parameters (3.Basis)
17+
basis_type pw
18+
19+
#Parameters (4.Smearing)
20+
smearing_method gauss
21+
smearing_sigma 0.0002
22+
23+
#Parameters (5.Mixing)
24+
mixing_type broyden
25+
device cpu
26+
27+
#Parameters (6.Gene Data)
28+
of_full_pw 1
29+
of_full_pw_dim 1
30+
31+
of_ml_gene_data 1
32+
33+
of_ml_nkernel 2
34+
of_ml_kernel 1 1
35+
of_ml_kernel_scaling 1.5 1
36+
of_ml_chi_p 0.2
37+
of_ml_chi_q 0.1
38+
of_ml_chi_pnl 0.2 0.2
39+
of_ml_chi_qnl 0.1 0.1
40+
of_ml_chi_xi 0.8 1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
K_POINTS
2+
0
3+
Gamma
4+
20 20 20 0 0 0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tests the correctness of ML functional descriptors generated by PW KSDFT; this case supports single-rank only.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
ATOMIC_SPECIES
2+
Al 13 al.gga.psp blps
3+
4+
LATTICE_CONSTANT
5+
7.6513590200098225 // add lattice constant
6+
7+
LATTICE_VECTORS
8+
0.000000000000 0.500000000000 0.500000000000
9+
0.500000000000 0.000000000000 0.500000000000
10+
0.500000000000 0.500000000000 0.000000000000
11+
12+
ATOMIC_POSITIONS
13+
Direct
14+
15+
Al
16+
0.0
17+
1
18+
0.000000000000 0.000000000000 0.000000000000 1 1 1
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
etotref -57.04829815855825
2+
etotperatomref -57.0482981586
3+
ml_desc_mean_enhancement_npy 1.111419127928
4+
ml_desc_mean_gamma_npy 0.995847307666
5+
ml_desc_mean_gammanl_1_1_5_npy 0.077138936619
6+
ml_desc_mean_gammanl_1_1_npy 0.083331919287
7+
ml_desc_mean_nablaRhox_npy 0.003714583460
8+
ml_desc_mean_nablaRhoy_npy 0.003714583460
9+
ml_desc_mean_nablaRhoz_npy 0.003714583460
10+
ml_desc_mean_p_npy 0.095710030124
11+
ml_desc_mean_pauli_npy 0.875124481343
12+
ml_desc_mean_pnl_1_1_5_npy 0.230824971750
13+
ml_desc_mean_pnl_1_1_npy 0.224100011030
14+
ml_desc_mean_q_npy 0.310335060112
15+
ml_desc_mean_qnl_1_1_5_npy 0.598582089727
16+
ml_desc_mean_qnl_1_1_npy 0.660815190079
17+
ml_desc_mean_rho_npy 0.026789555290
18+
ml_desc_mean_tanh_pnl_1_1_5_npy 0.044695787317
19+
ml_desc_mean_tanh_pnl_1_1_npy 0.043383873620
20+
ml_desc_mean_tanh_qnl_1_1_5_npy 0.053898994944
21+
ml_desc_mean_tanh_qnl_1_1_npy 0.059951993401
22+
ml_desc_mean_tanhp_npy 0.018740571716
23+
ml_desc_mean_tanhp_nl_1_1_5_npy 0.044938608030
24+
ml_desc_mean_tanhp_nl_1_1_npy 0.043643966443
25+
ml_desc_mean_tanhq_npy 0.028985866312
26+
ml_desc_mean_tanhq_nl_1_1_5_npy 0.054446092813
27+
ml_desc_mean_tanhq_nl_1_1_npy 0.060077197039
28+
ml_desc_mean_tanhxi_1_1_5_npy 0.065230231297
29+
ml_desc_mean_tanhxi_1_1_npy 0.086762840891
30+
ml_desc_mean_tanhxi_nl_1_1_5_npy 0.113754807936
31+
ml_desc_mean_tanhxi_nl_1_1_npy 0.163063987051
32+
ml_desc_mean_veff_npy 0.431187756019
33+
ml_desc_mean_xi_1_1_5_npy 0.083625451851
34+
ml_desc_mean_xi_1_1_npy 0.090202449809
35+
pointgroupref O_h
36+
spacegroupref O_h
37+
nksibzref 256
38+
totaltimeref 1.46

tests/01_PW/CASES_CPU.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ scf_out_chg_tau
103103
100_PW_W90
104104
101_PW_MD_1O
105105
102_PW_MD_2O
106+
103_PW_Gene_Descriptors
106107
201_PW_UPF201_Ce_f
107108
202_PW_ONCV_Libxc
108109
204_PW_SY

tests/integrate/Autotest.sh

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ nt=$OMP_NUM_THREADS # number of OpenMP threads, default is $OMP_NUM_THREADS
99
threshold=0.0000001
1010
force_threshold=0.0001
1111
stress_threshold=0.001
12+
# descriptor mean threshold
13+
descriptor_threshold=0.00001
1214
# check accuracy
1315
ca=8
1416
# specify the test cases file
@@ -60,6 +62,7 @@ echo "Number of threads: $nt"
6062
echo "Test accuracy totenergy: $threshold eV"
6163
echo "Test accuracy force: $force_threshold"
6264
echo "Test accuracy stress: $stress_threshold"
65+
echo "Test accuracy descriptor mean: $descriptor_threshold"
6366
echo "Check accuaracy: $ca"
6467
echo "Test cases file: $cases_file"
6568
echo "Test cases regex: $case"
@@ -89,6 +92,7 @@ check_out(){
8992
force_thr=$3
9093
stress_thr=$4
9194
fatal_thr=$5
95+
descriptor_thr=$6
9296

9397
#------------------------------------------------------
9498
# outfile = result.out
@@ -144,7 +148,11 @@ check_out(){
144148
fatal_case_list+=$dir'\n'
145149
break
146150
else
147-
if [ $(check_deviation_pass $deviation $thr) = 0 ]; then
151+
compare_thr=$thr
152+
if [[ $key == ml_desc_mean_* ]]; then
153+
compare_thr=$descriptor_thr
154+
fi
155+
if [ $(check_deviation_pass $deviation $compare_thr) = 0 ]; then
148156
if [ $key == "totalforceref" ]; then
149157
if [ $(check_deviation_pass $deviation $force_thr) = 0 ]; then
150158
echo -e "[WARNING ] "\
@@ -208,7 +216,7 @@ get_threshold()
208216
default_value=$3
209217
if [ -e $threshold_f ]; then
210218
threshold_value=$(awk -v tn="$threshold_name" '$1==tn {print $2}' "$threshold_f")
211-
if [ -n "$threshold_value" ]; then
219+
if [ -n "$threshold_value" ]; then
212220
echo $threshold_value
213221
else
214222
echo $default_value
@@ -263,6 +271,9 @@ for dir in $testdir; do
263271
$abacus > log.txt
264272
elif [ "$case" = "282_NO_RPA" ]; then
265273
mpirun -np 1 $abacus > log.txt
274+
elif grep -qE '^[[:space:]]*of_ml_gene_data[[:space:]]+1([[:space:]]|$)' INPUT; then
275+
# of_ml_gene_data supports single-rank only.
276+
mpirun -np 1 $abacus > log.txt
266277
else
267278
mpirun -np $np $abacus > log.txt
268279
fi
@@ -289,7 +300,8 @@ for dir in $testdir; do
289300
my_force_threshold=$(get_threshold $threshold_file "force_threshold" $force_threshold)
290301
my_stress_threshold=$(get_threshold $threshold_file "stress_threshold" $stress_threshold)
291302
my_fatal_threshold=$(get_threshold $threshold_file "fatal_threshold" $fatal_threshold)
292-
check_out result.out $my_threshold $my_force_threshold $my_stress_threshold $my_fatal_threshold
303+
my_descriptor_threshold=$(get_threshold $threshold_file "descriptor_threshold" $descriptor_threshold)
304+
check_out result.out $my_threshold $my_force_threshold $my_stress_threshold $my_fatal_threshold $my_descriptor_threshold
293305
fi
294306
else
295307
bash -e ../../integrate/tools/catch_properties.sh result.ref

tests/integrate/tools/catch_properties.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
COMPARE_SCRIPT="../../integrate/tools/CompareFile.py"
66
#COMPARE_SCRIPT="../../integrate/tools/compare_file.py"
77
SUM_CUBE_EXE="python3 ../../integrate/tools/sum_cube.py"
8+
COLLECT_NPY_MEANS="../../integrate/tools/collect_npy_means.py"
89

910

1011
sum_file(){
@@ -646,6 +647,14 @@ if [ "$need_process_cube" = true ]; then
646647
fi
647648
fi
648649

650+
#--------------------------------------------
651+
# ML gene data descriptors (.npy)
652+
#--------------------------------------------
653+
descriptor_dir="OUT.autotest/MLKEDF_Descriptors"
654+
if [ -d "$descriptor_dir" ]; then
655+
python3 $COLLECT_NPY_MEANS "$descriptor_dir" >> "$1"
656+
fi
657+
649658
#--------------------------------------------
650659
# implicit solvation model
651660
#--------------------------------------------

0 commit comments

Comments
 (0)