-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_bgw
More file actions
executable file
·134 lines (115 loc) · 3.58 KB
/
prepare_bgw
File metadata and controls
executable file
·134 lines (115 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#! /usr/bin/env python3
import os
import shutil
from pathlib import Path
import numpy as np
import typer
from ase.io import read
from ase.io.espresso import write_espresso_in
from rich import print as echo
app = typer.Typer()
def get_input_data(control: dict, system: dict, electrons: dict):
return {"control": control, "system": system, "electrons": electrons}
def remove_kpoints(file):
"""post hoc fix to remove K_POINTS from input file"""
# find index of K_POINTS line
lines = Path(file).open().readlines()
index = next(tpl[0] for tpl in enumerate(lines) if "K_POINTS" in tpl[1])
# remove that line plus blank space and write
new_lines = lines[: index - 1] + lines[index + 1 :]
Path(file).write_text("".join(new_lines))
@app.command()
def main(
file,
ecutwfc: float = 50.0,
nbnd: int = 100,
xc: str = "pbe",
tprnfor: bool = True,
tstress: bool = True,
conv_thr: float = 1e-12,
diagonalization: str = "david",
diago_full_acc: bool = True,
prefix: str = "qe",
outdir: str = "./",
wfcdir: str = "./wfc",
pseudodir: str = "./pseudopotentials",
format="aims",
):
"""Convert geometry file into pw.in template"""
atoms = read(file, format=format)
echo(atoms)
path_pseudo = Path(os.environ["ESPRESSO_PSEUDO"])
# copy pseudopoentials in folder and add to dict
_folder = Path(pseudodir)
_folder.mkdir(exist_ok=True)
pseudopotentials = {}
for elem in np.unique(atoms.symbols):
file = next(path_pseudo.glob(f"{elem}_*PBE-1.2.upf"))
# copy
_newfile = _folder / file.name
echo(f".. copy {file} to {_folder}")
shutil.copy(file, _newfile)
pseudopotentials[elem] = file.name
# default control
data_control = {
"calculation": "scf",
"prefix": prefix,
"outdir": outdir,
"wfcdir": wfcdir,
"pseudo_dir": pseudodir,
"tprnfor": tprnfor,
"tstress": tstress,
}
# default electrons
data_electrons = {
"conv_thr": conv_thr,
"diagonalization": diagonalization,
"diago_full_acc": diago_full_acc,
}
# system
data_system = {
"ecutwfc": ecutwfc,
"input_dft": xc,
}
# report
echo("Will set up following things:")
echo("Control:")
echo(data_control)
echo("System:")
echo(data_system)
echo("Electrons:")
echo(data_electrons)
# scf
outfile = "pw.in.tmp.scf"
echo(f".. write {outfile}")
with open(outfile, "w") as f:
input_data = get_input_data(data_control, data_system, data_electrons)
write_espresso_in(
f, atoms=atoms, pseudopotentials=pseudopotentials, input_data=input_data
)
# fix K_POINTS
remove_kpoints(outfile)
# occupied bands
outfile = "pw.in.tmp.bands.occ"
echo(f".. write {outfile}")
data_control["calculation"] = "bands"
with open(outfile, "w") as f:
input_data = get_input_data(data_control, data_system, data_electrons)
write_espresso_in(
f, atoms=atoms, pseudopotentials=pseudopotentials, input_data=input_data
)
# fix K_POINTS
remove_kpoints(outfile)
# occupied + unoccupied bands
outfile = "pw.in.tmp.bands.unocc"
echo(f".. write {outfile}")
data_system["nbnd"] = nbnd
with open(outfile, "w") as f:
input_data = get_input_data(data_control, data_system, data_electrons)
write_espresso_in(
f, atoms=atoms, pseudopotentials=pseudopotentials, input_data=input_data
)
# fix K_POINTS
remove_kpoints(outfile)
if __name__ == "__main__":
app()