Skip to content

issue1475: a small bug of iterdict (generator/lib/cp2k.py) #1476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions dpgen/generator/lib/cp2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ def update_dict(old_d, update_d):
old_d[k] = update_d[k]


def iterdict(d, out_list, flag=None):
""":doc: a recursive expansion of dictionary into cp2k input
def iterdict(d, out_list, flag=None, indent=0):
"""
:doc: a recursive expansion of dictionary into cp2k input
:k: current key
:v: current value
:d: current dictionary under expansion
:flag: used to record dictionary state. if flag is None,
it means we are in top level dict. flag is a string.
:indent: intent for current section.
"""
for k, v in d.items():
k = str(k) # cast key into string
Expand All @@ -64,16 +66,17 @@ def iterdict(d, out_list, flag=None):
if flag is None:
out_list.append("&" + k)
out_list.append("&END " + k)
iterdict(v, out_list, k)
iterdict(v, out_list, k, indent + 2)
# flag is not None, now it has name of section
else:
index = out_list.index("&END " + flag)
out_list.insert(index, "&" + k)
out_list.insert(index + 1, "&END " + k)
iterdict(v, out_list, k)
index = out_list.index(" " * (indent - 2) + "&END " + flag)
out_list.insert(index, " " * indent + "&" + k + " #" + flag)
out_list.insert(index + 1, " " * indent + "&END " + k + " #" + flag)
# the flag now contains its parent section name, separed by "#".
iterdict(v, out_list, k + " #" + flag, indent + 2)
elif isinstance(v, list):
# print("we have encountered the repeat section!")
index = out_list.index("&" + flag)
index = out_list.index(" " * (indent - 2) + "&" + flag)
# delete the current constructed repeat section
del out_list[index : index + 2]
# do a loop over key and corresponding list
Expand All @@ -83,14 +86,22 @@ def iterdict(d, out_list, flag=None):
k_tmp_list.append(str(k_tmp))
v_list_tmp_list.append(v_tmp)
for repeat_keyword in zip(*v_list_tmp_list):
out_list.insert(index, "&" + flag)
out_list.insert(index + 1, "&END " + flag)
out_list.insert(index, " " * (indent - 2) + "&" + flag)
out_list.insert(index + 1, " " * (indent - 2) + "&END " + flag)
for idx, k_tmp in enumerate(k_tmp_list):
if k_tmp == "_":
out_list[index] = "&" + flag + " " + repeat_keyword[idx]
out_list[index] = (
" " * (indent - 2)
+ "&"
+ flag.split(" #")[0]
+ " "
+ repeat_keyword[idx]
)
else:
out_list.insert(index + 1, k_tmp + " " + repeat_keyword[idx])

out_list.insert(
index + 1,
" " * (indent) + k_tmp + " " + repeat_keyword[idx],
)
break

else:
Expand All @@ -100,12 +111,14 @@ def iterdict(d, out_list, flag=None):
print(k, ":", v)
else:
if k == "_":
index = out_list.index("&" + flag)
out_list[index] = "&" + flag + " " + v
index = out_list.index(" " * (indent - 2) + "&" + flag)
out_list[index] = (
" " * (indent - 2) + "&" + flag.split(" #")[0] + " " + v
)

else:
index = out_list.index("&END " + flag)
out_list.insert(index, k + " " + v)
index = out_list.index(" " * (indent - 2) + "&END " + flag)
out_list.insert(index, " " * indent + k + " " + v)


def make_cp2k_input(sys_data, fp_params):
Expand Down
114 changes: 57 additions & 57 deletions tests/generator/cp2k_test_ref.inp
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
&GLOBAL
PROJECT DPGEN
PROJECT DPGEN
&END GLOBAL
&FORCE_EVAL
METHOD QS
STRESS_TENSOR ANALYTICAL
&DFT
BASIS_SET_FILE_NAME ./cp2k_basis_pp_file/BASIS_MOLOPT
POTENTIAL_FILE_NAME ./cp2k_basis_pp_file/GTH_POTENTIALS
CHARGE 0
UKS F
MULTIPLICITY 1
&MGRID
CUTOFF 400
REL_CUTOFF 50
NGRIDS 4
&END MGRID
&QS
EPS_DEFAULT 1.0E-12
&END QS
&SCF
SCF_GUESS ATOMIC
EPS_SCF 1.0E-6
MAX_SCF 50
&OT
MINIMIZER DIIS
PRECONDITIONER FULL_SINGLE_INVERSE
&END OT
&END SCF
&XC
&XC_FUNCTIONAL PBE
&END XC_FUNCTIONAL
&END XC
&END DFT
&SUBSYS
&CELL
&END CELL
&COORD
@include coord.xyz
&END COORD
&KIND H
BASIS_SET DZVP-MOLOPT-GTH
POTENTIAL GTH-PBE-q1
&END KIND
&KIND C
BASIS_SET DZVP-MOLOPT-GTH
POTENTIAL GTH-PBE-q4
&END KIND
&KIND N
BASIS_SET DZVP-MOLOPT-GTH
POTENTIAL GTH-PBE-q5
&END KIND
&END SUBSYS
&PRINT
&FORCES ON
&END FORCES
&STRESS_TENSOR ON
&END STRESS_TENSOR
&END PRINT
&END FORCE_EVAL
METHOD QS
STRESS_TENSOR ANALYTICAL
&DFT #FORCE_EVAL
BASIS_SET_FILE_NAME ./cp2k_basis_pp_file/BASIS_MOLOPT
POTENTIAL_FILE_NAME ./cp2k_basis_pp_file/GTH_POTENTIALS
CHARGE 0
UKS F
MULTIPLICITY 1
&MGRID #DFT #FORCE_EVAL
CUTOFF 400
REL_CUTOFF 50
NGRIDS 4
&END MGRID #DFT #FORCE_EVAL
&QS #DFT #FORCE_EVAL
EPS_DEFAULT 1.0E-12
&END QS #DFT #FORCE_EVAL
&SCF #DFT #FORCE_EVAL
SCF_GUESS ATOMIC
EPS_SCF 1.0E-6
MAX_SCF 50
&OT #SCF #DFT #FORCE_EVAL
MINIMIZER DIIS
PRECONDITIONER FULL_SINGLE_INVERSE
&END OT #SCF #DFT #FORCE_EVAL
&END SCF #DFT #FORCE_EVAL
&XC #DFT #FORCE_EVAL
&XC_FUNCTIONAL PBE
&END XC_FUNCTIONAL #XC #DFT #FORCE_EVAL
&END XC #DFT #FORCE_EVAL
&END DFT #FORCE_EVAL
&SUBSYS #FORCE_EVAL
&CELL #SUBSYS #FORCE_EVAL
&END CELL #SUBSYS #FORCE_EVAL
&COORD #SUBSYS #FORCE_EVAL
@include coord.xyz
&END COORD #SUBSYS #FORCE_EVAL
&KIND H
BASIS_SET DZVP-MOLOPT-GTH
POTENTIAL GTH-PBE-q1
&END KIND #SUBSYS #FORCE_EVAL
&KIND C
BASIS_SET DZVP-MOLOPT-GTH
POTENTIAL GTH-PBE-q4
&END KIND #SUBSYS #FORCE_EVAL
&KIND N
BASIS_SET DZVP-MOLOPT-GTH
POTENTIAL GTH-PBE-q5
&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