Skip to content

Commit cbecd99

Browse files
issue1475: a small bug of iterdict (generator/lib/cp2k.py) (#1476)
1. The flag in iterdict (generator/lib/cp2k.py) now contains the parent section name, and fix the problem that the content of multiple sections with the same name will only be inserted into the first section. 2. Add indent for sections. now the input generate by make_cp2k_input is look like this: ``` &GLOBAL PROJECT DPGEN PREFERRED_DIAG_LIBRARY SCALAPACK &END GLOBAL &FORCE_EVAL METHOD QS STRESS_TENSOR ANALYTICAL &DFT #FORCE_EVAL BASIS_SET_FILE_NAME EMSL_BASIS_SETS POTENTIAL_FILE_NAME POTENTIAL CHARGE 0 UKS TRUE &MGRID #DFT #FORCE_EVAL CUTOFF 800 REL_CUTOFF 55 NGRIDS 4 &END MGRID #DFT #FORCE_EVAL &QS #DFT #FORCE_EVAL EPS_DEFAULT 1.0E-12 METHOD GAPW &END QS #DFT #FORCE_EVAL &SCF #DFT #FORCE_EVAL SCF_GUESS ATOMIC EPS_SCF 1.0E-6 MAX_SCF 50 &MIXING #SCF #DFT #FORCE_EVAL METHOD BROYDEN_MIXING &END MIXING #SCF #DFT #FORCE_EVAL &PRINT #SCF #DFT #FORCE_EVAL &RESTART OFF &END RESTART #PRINT #SCF #DFT #FORCE_EVAL &END PRINT #SCF #DFT #FORCE_EVAL ADD_MOS -1 -1 &END SCF #DFT #FORCE_EVAL &XC #DFT #FORCE_EVAL &XC_FUNCTIONAL B3LYP &END XC_FUNCTIONAL #XC #DFT #FORCE_EVAL &HF #XC #DFT #FORCE_EVAL &MEMORY #HF #XC #DFT #FORCE_EVAL MAX_MEMORY 2000 &END MEMORY #HF #XC #DFT #FORCE_EVAL &END HF #XC #DFT #FORCE_EVAL &END XC #DFT #FORCE_EVAL &POISSON #DFT #FORCE_EVAL PERIODIC NONE POISSON_SOLVER MULTIPOLE &END POISSON #DFT #FORCE_EVAL RELAX_MULTIPLICITY 0.00001 &END DFT #FORCE_EVAL &SUBSYS #FORCE_EVAL &CELL #SUBSYS #FORCE_EVAL A 14.39252 0. 0. B 0. 20.281213 0. C 0. 0. 15.801355 PERIODIC NONE &END CELL #SUBSYS #FORCE_EVAL &COORD #SUBSYS #FORCE_EVAL @include coord.xyz &END COORD #SUBSYS #FORCE_EVAL &KIND Cl BASIS_SET 6-311G** POTENTIAL ALL &END KIND #SUBSYS #FORCE_EVAL &KIND H BASIS_SET 6-311G** POTENTIAL ALL &END KIND #SUBSYS #FORCE_EVAL &KIND C BASIS_SET 6-311G** POTENTIAL ALL &END KIND #SUBSYS #FORCE_EVAL &END SUBSYS #FORCE_EVAL &PRINT #FORCE_EVAL &FORCES ON &END FORCES #PRINT #FORCE_EVAL &STRESS_TENSOR ON &END STRESS_TENSOR #PRINT #FORCE_EVAL &END PRINT #FORCE_EVAL &END FORCE_EVAL ``` --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e7b4311 commit cbecd99

File tree

2 files changed

+87
-74
lines changed

2 files changed

+87
-74
lines changed

dpgen/generator/lib/cp2k.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ def update_dict(old_d, update_d):
4848
old_d[k] = update_d[k]
4949

5050

51-
def iterdict(d, out_list, flag=None):
52-
""":doc: a recursive expansion of dictionary into cp2k input
51+
def iterdict(d, out_list, flag=None, indent=0):
52+
"""
53+
:doc: a recursive expansion of dictionary into cp2k input
5354
:k: current key
5455
:v: current value
5556
:d: current dictionary under expansion
5657
:flag: used to record dictionary state. if flag is None,
5758
it means we are in top level dict. flag is a string.
59+
:indent: intent for current section.
5860
"""
5961
for k, v in d.items():
6062
k = str(k) # cast key into string
@@ -64,16 +66,17 @@ def iterdict(d, out_list, flag=None):
6466
if flag is None:
6567
out_list.append("&" + k)
6668
out_list.append("&END " + k)
67-
iterdict(v, out_list, k)
69+
iterdict(v, out_list, k, indent + 2)
6870
# flag is not None, now it has name of section
6971
else:
70-
index = out_list.index("&END " + flag)
71-
out_list.insert(index, "&" + k)
72-
out_list.insert(index + 1, "&END " + k)
73-
iterdict(v, out_list, k)
72+
index = out_list.index(" " * (indent - 2) + "&END " + flag)
73+
out_list.insert(index, " " * indent + "&" + k + " #" + flag)
74+
out_list.insert(index + 1, " " * indent + "&END " + k + " #" + flag)
75+
# the flag now contains its parent section name, separed by "#".
76+
iterdict(v, out_list, k + " #" + flag, indent + 2)
7477
elif isinstance(v, list):
7578
# print("we have encountered the repeat section!")
76-
index = out_list.index("&" + flag)
79+
index = out_list.index(" " * (indent - 2) + "&" + flag)
7780
# delete the current constructed repeat section
7881
del out_list[index : index + 2]
7982
# do a loop over key and corresponding list
@@ -83,14 +86,22 @@ def iterdict(d, out_list, flag=None):
8386
k_tmp_list.append(str(k_tmp))
8487
v_list_tmp_list.append(v_tmp)
8588
for repeat_keyword in zip(*v_list_tmp_list):
86-
out_list.insert(index, "&" + flag)
87-
out_list.insert(index + 1, "&END " + flag)
89+
out_list.insert(index, " " * (indent - 2) + "&" + flag)
90+
out_list.insert(index + 1, " " * (indent - 2) + "&END " + flag)
8891
for idx, k_tmp in enumerate(k_tmp_list):
8992
if k_tmp == "_":
90-
out_list[index] = "&" + flag + " " + repeat_keyword[idx]
93+
out_list[index] = (
94+
" " * (indent - 2)
95+
+ "&"
96+
+ flag.split(" #")[0]
97+
+ " "
98+
+ repeat_keyword[idx]
99+
)
91100
else:
92-
out_list.insert(index + 1, k_tmp + " " + repeat_keyword[idx])
93-
101+
out_list.insert(
102+
index + 1,
103+
" " * (indent) + k_tmp + " " + repeat_keyword[idx],
104+
)
94105
break
95106

96107
else:
@@ -100,12 +111,14 @@ def iterdict(d, out_list, flag=None):
100111
print(k, ":", v)
101112
else:
102113
if k == "_":
103-
index = out_list.index("&" + flag)
104-
out_list[index] = "&" + flag + " " + v
114+
index = out_list.index(" " * (indent - 2) + "&" + flag)
115+
out_list[index] = (
116+
" " * (indent - 2) + "&" + flag.split(" #")[0] + " " + v
117+
)
105118

106119
else:
107-
index = out_list.index("&END " + flag)
108-
out_list.insert(index, k + " " + v)
120+
index = out_list.index(" " * (indent - 2) + "&END " + flag)
121+
out_list.insert(index, " " * indent + k + " " + v)
109122

110123

111124
def make_cp2k_input(sys_data, fp_params):

tests/generator/cp2k_test_ref.inp

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
11
&GLOBAL
2-
PROJECT DPGEN
2+
PROJECT DPGEN
33
&END GLOBAL
44
&FORCE_EVAL
5-
METHOD QS
6-
STRESS_TENSOR ANALYTICAL
7-
&DFT
8-
BASIS_SET_FILE_NAME ./cp2k_basis_pp_file/BASIS_MOLOPT
9-
POTENTIAL_FILE_NAME ./cp2k_basis_pp_file/GTH_POTENTIALS
10-
CHARGE 0
11-
UKS F
12-
MULTIPLICITY 1
13-
&MGRID
14-
CUTOFF 400
15-
REL_CUTOFF 50
16-
NGRIDS 4
17-
&END MGRID
18-
&QS
19-
EPS_DEFAULT 1.0E-12
20-
&END QS
21-
&SCF
22-
SCF_GUESS ATOMIC
23-
EPS_SCF 1.0E-6
24-
MAX_SCF 50
25-
&OT
26-
MINIMIZER DIIS
27-
PRECONDITIONER FULL_SINGLE_INVERSE
28-
&END OT
29-
&END SCF
30-
&XC
31-
&XC_FUNCTIONAL PBE
32-
&END XC_FUNCTIONAL
33-
&END XC
34-
&END DFT
35-
&SUBSYS
36-
&CELL
37-
&END CELL
38-
&COORD
39-
@include coord.xyz
40-
&END COORD
41-
&KIND H
42-
BASIS_SET DZVP-MOLOPT-GTH
43-
POTENTIAL GTH-PBE-q1
44-
&END KIND
45-
&KIND C
46-
BASIS_SET DZVP-MOLOPT-GTH
47-
POTENTIAL GTH-PBE-q4
48-
&END KIND
49-
&KIND N
50-
BASIS_SET DZVP-MOLOPT-GTH
51-
POTENTIAL GTH-PBE-q5
52-
&END KIND
53-
&END SUBSYS
54-
&PRINT
55-
&FORCES ON
56-
&END FORCES
57-
&STRESS_TENSOR ON
58-
&END STRESS_TENSOR
59-
&END PRINT
60-
&END FORCE_EVAL
5+
METHOD QS
6+
STRESS_TENSOR ANALYTICAL
7+
&DFT #FORCE_EVAL
8+
BASIS_SET_FILE_NAME ./cp2k_basis_pp_file/BASIS_MOLOPT
9+
POTENTIAL_FILE_NAME ./cp2k_basis_pp_file/GTH_POTENTIALS
10+
CHARGE 0
11+
UKS F
12+
MULTIPLICITY 1
13+
&MGRID #DFT #FORCE_EVAL
14+
CUTOFF 400
15+
REL_CUTOFF 50
16+
NGRIDS 4
17+
&END MGRID #DFT #FORCE_EVAL
18+
&QS #DFT #FORCE_EVAL
19+
EPS_DEFAULT 1.0E-12
20+
&END QS #DFT #FORCE_EVAL
21+
&SCF #DFT #FORCE_EVAL
22+
SCF_GUESS ATOMIC
23+
EPS_SCF 1.0E-6
24+
MAX_SCF 50
25+
&OT #SCF #DFT #FORCE_EVAL
26+
MINIMIZER DIIS
27+
PRECONDITIONER FULL_SINGLE_INVERSE
28+
&END OT #SCF #DFT #FORCE_EVAL
29+
&END SCF #DFT #FORCE_EVAL
30+
&XC #DFT #FORCE_EVAL
31+
&XC_FUNCTIONAL PBE
32+
&END XC_FUNCTIONAL #XC #DFT #FORCE_EVAL
33+
&END XC #DFT #FORCE_EVAL
34+
&END DFT #FORCE_EVAL
35+
&SUBSYS #FORCE_EVAL
36+
&CELL #SUBSYS #FORCE_EVAL
37+
&END CELL #SUBSYS #FORCE_EVAL
38+
&COORD #SUBSYS #FORCE_EVAL
39+
@include coord.xyz
40+
&END COORD #SUBSYS #FORCE_EVAL
41+
&KIND H
42+
BASIS_SET DZVP-MOLOPT-GTH
43+
POTENTIAL GTH-PBE-q1
44+
&END KIND #SUBSYS #FORCE_EVAL
45+
&KIND C
46+
BASIS_SET DZVP-MOLOPT-GTH
47+
POTENTIAL GTH-PBE-q4
48+
&END KIND #SUBSYS #FORCE_EVAL
49+
&KIND N
50+
BASIS_SET DZVP-MOLOPT-GTH
51+
POTENTIAL GTH-PBE-q5
52+
&END KIND #SUBSYS #FORCE_EVAL
53+
&END SUBSYS #FORCE_EVAL
54+
&PRINT #FORCE_EVAL
55+
&FORCES ON
56+
&END FORCES #PRINT #FORCE_EVAL
57+
&STRESS_TENSOR ON
58+
&END STRESS_TENSOR #PRINT #FORCE_EVAL
59+
&END PRINT #FORCE_EVAL
60+
&END FORCE_EVAL

0 commit comments

Comments
 (0)