-
Notifications
You must be signed in to change notification settings - Fork 835
/
pwscf.py
580 lines (518 loc) · 18.9 KB
/
pwscf.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
"""This module implements input and output processing from PWSCF."""
from __future__ import annotations
import re
from collections import defaultdict
from monty.io import zopen
from monty.re import regrep
from pymatgen.core import Element, Lattice, Structure
from pymatgen.util.io_utils import clean_lines
class PWInput:
"""
Base input file class. Right now, only supports no symmetry and is
very basic.
"""
def __init__(
self,
structure,
pseudo=None,
control=None,
system=None,
electrons=None,
ions=None,
cell=None,
kpoints_mode="automatic",
kpoints_grid=(1, 1, 1),
kpoints_shift=(0, 0, 0),
):
"""
Initializes a PWSCF input file.
Args:
structure (Structure): Input structure. For spin-polarized calculation,
properties (e.g. {"starting_magnetization": -0.5,
"pseudo": "Mn.pbe-sp-van.UPF"}) on each site is needed instead of
pseudo (dict).
pseudo (dict): A dict of the pseudopotentials to use. Default to None.
control (dict): Control parameters. Refer to official PWSCF doc
on supported parameters. Default to {"calculation": "scf"}
system (dict): System parameters. Refer to official PWSCF doc
on supported parameters. Default to None, which means {}.
electrons (dict): Electron parameters. Refer to official PWSCF doc
on supported parameters. Default to None, which means {}.
ions (dict): Ions parameters. Refer to official PWSCF doc
on supported parameters. Default to None, which means {}.
cell (dict): Cell parameters. Refer to official PWSCF doc
on supported parameters. Default to None, which means {}.
kpoints_mode (str): Kpoints generation mode. Default to automatic.
kpoints_grid (sequence): The kpoint grid. Default to (1, 1, 1).
kpoints_shift (sequence): The shift for the kpoints. Defaults to
(0, 0, 0).
"""
self.structure = structure
sections = {}
sections["control"] = control or {"calculation": "scf"}
sections["system"] = system or {}
sections["electrons"] = electrons or {}
sections["ions"] = ions or {}
sections["cell"] = cell or {}
if pseudo is None:
for site in structure:
try:
site.properties["pseudo"]
except KeyError:
raise PWInputError(f"Missing {site} in pseudo specification!")
else:
for species in self.structure.composition:
if str(species) not in pseudo:
raise PWInputError(f"Missing {species} in pseudo specification!")
self.pseudo = pseudo
self.sections = sections
self.kpoints_mode = kpoints_mode
self.kpoints_grid = kpoints_grid
self.kpoints_shift = kpoints_shift
def __str__(self):
out = []
site_descriptions = {}
if self.pseudo is not None:
site_descriptions = self.pseudo
else:
c = 1
for site in self.structure:
name = None
for k, v in site_descriptions.items():
if site.properties == v:
name = k
if name is None:
name = f"{site.specie.symbol}{c}"
site_descriptions[name] = site.properties
c += 1
def to_str(v):
if isinstance(v, str):
return f"{v!r}"
if isinstance(v, float):
return f"{str(v).replace('e', 'd')}"
if isinstance(v, bool):
if v:
return ".TRUE."
return ".FALSE."
return v
for k1 in ["control", "system", "electrons", "ions", "cell"]:
v1 = self.sections[k1]
out.append(f"&{k1.upper()}")
sub = []
for k2 in sorted(v1):
if isinstance(v1[k2], list):
n = 1
for _ in v1[k2][: len(site_descriptions)]:
sub.append(f" {k2}({n}) = {to_str(v1[k2][n - 1])}")
n += 1
else:
sub.append(f" {k2} = {to_str(v1[k2])}")
if k1 == "system":
if "ibrav" not in self.sections[k1]:
sub.append(" ibrav = 0")
if "nat" not in self.sections[k1]:
sub.append(f" nat = {len(self.structure)}")
if "ntyp" not in self.sections[k1]:
sub.append(f" ntyp = {len(site_descriptions)}")
sub.append("/")
out.append(",\n".join(sub))
out.append("ATOMIC_SPECIES")
for k, v in sorted(site_descriptions.items(), key=lambda i: i[0]):
e = re.match(r"[A-Z][a-z]?", k).group(0)
p = v if self.pseudo is not None else v["pseudo"]
out.append(f" {k} {Element(e).atomic_mass:.4f} {p}")
out.append("ATOMIC_POSITIONS crystal")
if self.pseudo is not None:
for site in self.structure:
out.append(f" {site.specie} {site.a:.6f} {site.b:.6f} {site.c:.6f}")
else:
for site in self.structure:
name = None
for k, v in sorted(site_descriptions.items(), key=lambda i: i[0]):
if v == site.properties:
name = k
out.append(f" {name} {site.a:.6f} {site.b:.6f} {site.c:.6f}")
out.append(f"K_POINTS {self.kpoints_mode}")
if self.kpoints_mode == "automatic":
kpt_str = [f"{i}" for i in self.kpoints_grid]
kpt_str.extend([f"{i}" for i in self.kpoints_shift])
out.append(f" {' '.join(kpt_str)}")
elif self.kpoints_mode == "crystal_b":
out.append(f" {len(self.kpoints_grid)}")
for i in range(len(self.kpoints_grid)):
kpt_str = [f"{entry:.4f}" for entry in self.kpoints_grid[i]]
out.append(f" {' '.join(kpt_str)}")
elif self.kpoints_mode == "gamma":
pass
out.append("CELL_PARAMETERS angstrom")
for vec in self.structure.lattice.matrix:
out.append(f" {vec[0]:f} {vec[1]:f} {vec[2]:f}")
return "\n".join(out)
def as_dict(self):
"""
Create a dictionary representation of a PWInput object.
Returns:
dict
"""
return {
"structure": self.structure.as_dict(),
"pseudo": self.pseudo,
"sections": self.sections,
"kpoints_mode": self.kpoints_mode,
"kpoints_grid": self.kpoints_grid,
"kpoints_shift": self.kpoints_shift,
}
@classmethod
def from_dict(cls, pwinput_dict):
"""
Load a PWInput object from a dictionary.
Args:
pwinput_dict (dict): dictionary with PWInput data
Returns:
PWInput object
"""
return cls(
structure=Structure.from_dict(pwinput_dict["structure"]),
pseudo=pwinput_dict["pseudo"],
control=pwinput_dict["sections"]["control"],
system=pwinput_dict["sections"]["system"],
electrons=pwinput_dict["sections"]["electrons"],
ions=pwinput_dict["sections"]["ions"],
cell=pwinput_dict["sections"]["cell"],
kpoints_mode=pwinput_dict["kpoints_mode"],
kpoints_grid=pwinput_dict["kpoints_grid"],
kpoints_shift=pwinput_dict["kpoints_shift"],
)
def write_file(self, filename):
"""
Write the PWSCF input file.
Args:
filename (str): The string filename to output to.
"""
with open(filename, mode="w") as file:
file.write(str(self))
@classmethod
def from_file(cls, filename):
"""
Reads an PWInput object from a file.
Args:
filename (str): Filename for file
Returns:
PWInput object
"""
with zopen(filename, mode="rt") as file:
return cls.from_str(file.read())
@classmethod
def from_str(cls, string):
"""
Reads an PWInput object from a string.
Args:
string (str): PWInput string
Returns:
PWInput object
"""
lines = list(clean_lines(string.splitlines()))
def input_mode(line):
if line[0] == "&":
return ("sections", line[1:].lower())
if "ATOMIC_SPECIES" in line:
return ("pseudo",)
if "K_POINTS" in line:
return "kpoints", line.split()[1]
if "OCCUPATIONS" in line:
return "occupations"
if "CELL_PARAMETERS" in line or "ATOMIC_POSITIONS" in line:
return "structure", line.split()[1]
if line == "/":
return None
return mode
sections = {
"control": {},
"system": {},
"electrons": {},
"ions": {},
"cell": {},
}
pseudo = {}
lattice = []
species = []
coords = []
structure = None
site_properties = {"pseudo": []}
mode = None
for line in lines:
mode = input_mode(line)
if mode is None:
pass
elif mode[0] == "sections":
section = mode[1]
m = re.match(r"(\w+)\(?(\d*?)\)?\s*=\s*(.*)", line)
if m:
key = m.group(1).strip()
key_ = m.group(2).strip()
val = m.group(3).strip()
if key_ != "":
if sections[section].get(key) is None:
val_ = [0.0] * 20 # MAX NTYP DEFINITION
val_[int(key_) - 1] = PWInput.proc_val(key, val)
sections[section][key] = val_
site_properties[key] = []
else:
sections[section][key][int(key_) - 1] = PWInput.proc_val(key, val)
else:
sections[section][key] = PWInput.proc_val(key, val)
elif mode[0] == "pseudo":
m = re.match(r"(\w+)\s+(\d*.\d*)\s+(.*)", line)
if m:
pseudo[m.group(1).strip()] = m.group(3).strip()
elif mode[0] == "kpoints":
m = re.match(r"(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)", line)
if m:
kpoints_grid = (int(m.group(1)), int(m.group(2)), int(m.group(3)))
kpoints_shift = (int(m.group(4)), int(m.group(5)), int(m.group(6)))
else:
kpoints_mode = mode[1]
kpoints_grid = (1, 1, 1)
kpoints_shift = (0, 0, 0)
elif mode[0] == "structure":
m_l = re.match(r"(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)", line)
m_p = re.match(r"(\w+)\s+(-?\d+\.\d*)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)", line)
if m_l:
lattice += [
float(m_l.group(1)),
float(m_l.group(2)),
float(m_l.group(3)),
]
elif m_p:
site_properties["pseudo"].append(pseudo[m_p.group(1)])
species.append(m_p.group(1))
coords += [[float(m_p.group(2)), float(m_p.group(3)), float(m_p.group(4))]]
if mode[1] == "angstrom":
coords_are_cartesian = True
elif mode[1] == "crystal":
coords_are_cartesian = False
structure = Structure(
Lattice(lattice),
species,
coords,
coords_are_cartesian=coords_are_cartesian,
site_properties=site_properties,
)
return cls(
structure=structure,
control=sections["control"],
pseudo=pseudo,
system=sections["system"],
electrons=sections["electrons"],
ions=sections["ions"],
cell=sections["cell"],
kpoints_mode=kpoints_mode,
kpoints_grid=kpoints_grid,
kpoints_shift=kpoints_shift,
)
@staticmethod
def proc_val(key, val):
"""
Static helper method to convert PWINPUT parameters to proper type, e.g.,
integers, floats, etc.
Args:
key: PWINPUT parameter key
val: Actual value of PWINPUT parameter.
"""
float_keys = (
"etot_conv_thr",
"forc_conv_thr",
"conv_thr",
"Hubbard_U",
"Hubbard_J0",
"degauss",
"starting_magnetization",
)
int_keys = (
"nstep",
"iprint",
"nberrycyc",
"gdir",
"nppstr",
"ibrav",
"nat",
"ntyp",
"nbnd",
"nr1",
"nr2",
"nr3",
"nr1s",
"nr2s",
"nr3s",
"nspin",
"nqx1",
"nqx2",
"nqx3",
"lda_plus_u_kind",
"edir",
"report",
"esm_nfit",
"space_group",
"origin_choice",
"electron_maxstep",
"mixing_ndim",
"mixing_fixed_ns",
"ortho_para",
"diago_cg_maxiter",
"diago_david_ndim",
"nraise",
"bfgs_ndim",
"if_pos",
"nks",
"nk1",
"nk2",
"nk3",
"sk1",
"sk2",
"sk3",
"nconstr",
)
bool_keys = (
"wf_collect",
"tstress",
"tprnfor",
"lkpoint_dir",
"tefield",
"dipfield",
"lelfield",
"lorbm",
"lberry",
"lfcpopt",
"monopole",
"nosym",
"nosym_evc",
"noinv",
"no_t_rev",
"force_symmorphic",
"use_all_frac",
"one_atom_occupations",
"starting_spin_angle",
"noncolin",
"x_gamma_extrapolation",
"lda_plus_u",
"lspinorb",
"london",
"ts_vdw_isolated",
"xdm",
"uniqueb",
"rhombohedral",
"realxz",
"block",
"scf_must_converge",
"adaptive_thr",
"diago_full_acc",
"tqr",
"remove_rigid_rot",
"refold_pos",
)
def smart_int_or_float(numstr):
if numstr.find(".") != -1 or numstr.lower().find("e") != -1:
return float(numstr)
return int(numstr)
try:
if key in bool_keys:
if val.lower() == ".true.":
return True
if val.lower() == ".false.":
return False
raise ValueError(key + " should be a boolean type!")
if key in float_keys:
return float(re.search(r"^-?\d*\.?\d*d?-?\d*", val.lower()).group(0).replace("d", "e"))
if key in int_keys:
return int(re.match(r"^-?[0-9]+", val).group(0))
except ValueError:
pass
try:
return smart_int_or_float(val.replace("d", "e"))
except ValueError:
pass
if "true" in val.lower():
return True
if "false" in val.lower():
return False
m = re.match(r"^[\"|'](.+)[\"|']$", val)
if m:
return m.group(1)
return None
class PWInputError(BaseException):
"""Error for PWInput."""
class PWOutput:
"""Parser for PWSCF output file."""
patterns = dict(
energies=r"total energy\s+=\s+([\d\.\-]+)\sRy",
ecut=r"kinetic\-energy cutoff\s+=\s+([\d\.\-]+)\s+Ry",
lattice_type=r"bravais\-lattice index\s+=\s+(\d+)",
celldm1=r"celldm\(1\)=\s+([\d\.]+)\s",
celldm2=r"celldm\(2\)=\s+([\d\.]+)\s",
celldm3=r"celldm\(3\)=\s+([\d\.]+)\s",
celldm4=r"celldm\(4\)=\s+([\d\.]+)\s",
celldm5=r"celldm\(5\)=\s+([\d\.]+)\s",
celldm6=r"celldm\(6\)=\s+([\d\.]+)\s",
nkpts=r"number of k points=\s+([\d]+)",
)
def __init__(self, filename):
"""
Args:
filename (str): Filename.
"""
self.filename = filename
self.data = defaultdict(list)
self.read_pattern(PWOutput.patterns)
for k, v in self.data.items():
if k == "energies":
self.data[k] = [float(i[0][0]) for i in v]
elif k in ["lattice_type", "nkpts"]:
self.data[k] = int(v[0][0][0])
else:
self.data[k] = float(v[0][0][0])
def read_pattern(self, patterns, reverse=False, terminate_on_match=False, postprocess=str):
r"""
General pattern reading. Uses monty's regrep method. Takes the same
arguments.
Args:
patterns (dict): A dict of patterns, e.g.,
{"energy": r"energy\\(sigma->0\\)\\s+=\\s+([\\d\\-.]+)"}.
reverse (bool): Read files in reverse. Defaults to false. Useful for
large files, esp OUTCARs, especially when used with
terminate_on_match.
terminate_on_match (bool): Whether to terminate when there is at
least one match in each key in pattern.
postprocess (callable): A post processing function to convert all
matches. Defaults to str, i.e., no change.
Renders accessible:
Any attribute in patterns. For example,
{"energy": r"energy\\(sigma->0\\)\\s+=\\s+([\\d\\-.]+)"} will set the
value of self.data["energy"] = [[-1234], [-3453], ...], to the
results from regex and postprocess. Note that the returned
values are lists of lists, because you can grep multiple
items on one line.
"""
matches = regrep(
self.filename,
patterns,
reverse=reverse,
terminate_on_match=terminate_on_match,
postprocess=postprocess,
)
self.data.update(matches)
def get_celldm(self, idx: int):
"""
Args:
idx (int): index.
Returns:
Cell dimension along index
"""
return self.data[f"celldm{idx}"]
@property
def final_energy(self):
"""Returns: Final energy."""
return self.data["energies"][-1]
@property
def lattice_type(self):
"""Returns: Lattice type."""
return self.data["lattice_type"]