forked from aiidateam/aiida-wannier90-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_automated_wannier.py
executable file
·253 lines (225 loc) · 7.27 KB
/
run_automated_wannier.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env runaiida
import argparse
from aiida import orm
from aiida.engine import submit
from aiida.common.exceptions import NotExistent
# from ase.io import read as aseread
from pymatgen.core import Structure
from aiida_wannier90_workflows.workflows import Wannier90BandsWorkChain
from aiida_quantumespresso.common.types import SpinType
import numpy as np
import warnings
warnings.filterwarnings("ignore")
# Please modify these according to your machine
str_pw = 'qe-pw@localhost'
str_pw2wan = 'qe-pw2wannier90@localhost'
str_projwfc = 'qe-projwfc@localhost'
str_wan = 'wannier90@localhost'
group_name = 'scdm_workflow'
def check_codes():
# will raise NotExistent error
try:
codes = dict(
pw=orm.load_code(str_pw),
pw2wannier90=orm.load_code(str_pw2wan),
projwfc=orm.load_code(str_projwfc),
wannier90=orm.load_code(str_wan),
)
except NotExistent as e:
print(e)
print(
'Please modify the code labels in this script according to your machine'
)
exit(1)
return codes
def parse_arugments():
parser = argparse.ArgumentParser(
description=
"A script to run the AiiDA workflows to automatically compute the MLWF using the SCDM method and the automated protocol described in the Vitale et al. paper"
)
parser.add_argument(
"cif", metavar="cif_fileNAME", help="path to an input structure file (CIF, MCIF, XSF,...)"
)
parser.add_argument(
'-p',
"--protocol",
help="available protocols are 'moderate', 'precise', and 'fast'",
default="fast"
)
parser.add_argument(
'-m',
"--do-mlwf",
help="do maximal localization of Wannier functions",
action="store_false"
)
parser.add_argument(
'-d',
"--do-disentanglement",
help=
"do disentanglement in Wanner90 step (This should be False, otherwise band structure is not optimal!)",
action="store_true"
)
parser.add_argument(
'-v',
"--only-valence",
help=
"Compute only for valence bands (you must be careful to apply this only for insulators!)",
action="store_true"
)
parser.add_argument(
'-r',
"--retrieve-hamiltonian",
help="Retrieve Wannier Hamiltonian after the workflow finished",
action="store_true"
)
parser.add_argument(
"--soi",
help="Consider Spin-Orbit Interaction",
action="store_true"
)
args = parser.parse_args()
return args
def get_initial_moment(magmoms, threshold=1e-6):
"""
Return a dictionary with the magnetic moments in the QuantumEspresso format
"""
# Check collinearity
collinear = True
for mom in magmoms:
if mom[0] > threshold or mom[1] > threshold:
collinear = False
break
init_mom={}
for i, mom in enumerate(magmoms):
num=str(i+1)
m = np.linalg.norm(mom[:3],ord=2)
if m > threshold:
mtheta=np.arccos(mom[2]/m)
mphi=np.arctan2(mom[1],mom[0])
else:
mtheta=0
mphi=0
init_mom[f'starting_magnetization({num})'] = m
if not collinear:
init_mom[f'angle1({num})'] = mtheta
init_mom[f'angle2({num})'] = mphi
return init_mom, collinear
def read_structure(cif_file):
pmg=Structure.from_file(cif_file)
structure = orm.StructureData(pymatgen=pmg)
if pmg.site_properties.get('magmom',None) is not None:
init_mom, collinear = get_initial_moment(pmg.site_properties['magmom'])
structure.base.extras.set('magmom',init_mom)
structure.base.extras.set('collinear',collinear)
structure.store()
print(
'Structure {} read and stored with pk {}.'.format(
structure.get_formula(), structure.pk
)
)
return structure
def update_group_name(
group_name, only_valence, do_disen, do_mlwf, exclude_bands=None
):
if only_valence:
group_name += "_onlyvalence"
else:
group_name += "_withconduction"
if do_disen:
group_name += '_disentangle'
if do_mlwf:
group_name += '_mlwf'
if exclude_bands is not None:
group_name += '_excluded{}'.format(len(exclude_bands))
return group_name
def add_to_group(node, group_name):
if group_name is not None:
try:
g = orm.Group.get(label=group_name)
group_statistics = "that already contains {} nodes".format(
len(g.nodes)
)
except NotExistent:
g = orm.Group(label=group_name)
group_statistics = "that does not exist yet"
g.store()
g.add_nodes(node)
print(
"Wannier90BandsWorkChain<{}> will be added to the group {} {}".
format(node.pk, group_name, group_statistics)
)
def print_help(workchain, structure):
print(
'launched Wannier90BandsWorkChain pk {} for structure {}'.format(
workchain.pk, structure.get_formula()
)
)
print('')
print('# To get a detailed state of the workflow, run:')
print('verdi process report {}'.format(workchain.pk))
def submit_workchain(
cif_file, protocol, only_valence, do_disentanglement, do_mlwf,
retrieve_hamiltonian, group_name, soi
):
codes = check_codes()
group_name = update_group_name(
group_name, only_valence, do_disentanglement, do_mlwf
)
if isinstance(cif_file, orm.StructureData):
structure = cif_file
else:
structure = read_structure(cif_file)
controls = {
'retrieve_hamiltonian': orm.Bool(retrieve_hamiltonian),
'only_valence': orm.Bool(only_valence),
'do_disentanglement': orm.Bool(do_disentanglement),
'do_mlwf': orm.Bool(do_mlwf)
}
if only_valence:
print(
"Running only_valence/insulating for {}".format(
structure.get_formula()
)
)
else:
print(
"Running with conduction bands for {}".format(
structure.get_formula()
)
)
spintype = SpinType.NONE
if soi:
spintype = SpinType.SPIN_ORBIT
# wannier90_workchain_parameters = {
# "code": {
# 'pw': codes['pw_code'],
# 'pw2wannier90': codes['pw2wannier90_code'],
# 'projwfc': codes['projwfc_code'],
# 'wannier90': codes['wannier90_code']
# },
# "protocol": orm.Dict(dict={'name': protocol}),
# "structure": structure,
# "controls": controls,
# "spin_type": spintype
# }
# workchain = submit(
# Wannier90BandsWorkChain, **wannier90_workchain_parameters
# )
builder = Wannier90BandsWorkChain.get_builder_from_protocol(
codes,
structure,
protocol=protocol,
retrieve_hamiltonian=retrieve_hamiltonian,
# controls=controls,
spin_type=spintype
)
workchain = submit(builder)
add_to_group(workchain, group_name)
print_help(workchain, structure)
return workchain.pk
if __name__ == "__main__":
args = parse_arugments()
submit_workchain(
args.cif, args.protocol, args.only_valence, args.do_disentanglement,
args.do_mlwf, args.retrieve_hamiltonian, group_name, args.soi
)