-
Notifications
You must be signed in to change notification settings - Fork 874
/
Copy pathinputs.py
897 lines (763 loc) · 33 KB
/
inputs.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
"""Module for reading LOBSTER input files.
For more information on LOBSTER see www.cohp.de.
If you use this module, please cite:
J. George, G. Petretto, A. Naik, M. Esters, A. J. Jackson, R. Nelson, R. Dronskowski, G.-M. Rignanese, G. Hautier,
"Automated Bonding Analysis with Crystal Orbital Hamilton Populations",
ChemPlusChem 2022, e202200123,
DOI: 10.1002/cplu.202200123.
"""
from __future__ import annotations
import itertools
import os
import re
import warnings
from collections import UserDict
from typing import TYPE_CHECKING
import numpy as np
import spglib
from monty.io import zopen
from monty.json import MSONable
from monty.serialization import loadfn
from pymatgen.core.structure import Structure
from pymatgen.io.vasp import Vasprun
from pymatgen.io.vasp.inputs import Incar, Kpoints, Potcar
from pymatgen.symmetry.bandstructure import HighSymmKpath
from pymatgen.util.due import Doi, due
if TYPE_CHECKING:
from typing import Any, ClassVar, Literal
from typing_extensions import Self
from pymatgen.core.composition import Composition
from pymatgen.util.typing import PathLike, Tuple3Ints
MODULE_DIR = os.path.dirname(os.path.abspath(__file__))
__author__ = "Janine George, Marco Esters"
__copyright__ = "Copyright 2017, The Materials Project"
__version__ = "0.2"
__maintainer__ = "Janine George"
__email__ = "[email protected]"
__date__ = "Dec 13, 2017"
due.cite(
Doi("10.1002/cplu.202200123"),
description="Automated Bonding Analysis with Crystal Orbital Hamilton Populations",
)
class Lobsterin(UserDict, MSONable):
"""Handle and generate lobsterin files.
Furthermore, it can also modify INCAR files for LOBSTER, generate KPOINTS files for fatband calculations in LOBSTER,
and generate the standard primitive cells in a POSCAR file that are needed for the fatband calculations.
There are also several standard lobsterin files that can be easily generated.
Reminder: lobsterin keywords are not case sensitive.
"""
# These keywords need an additional float suffix
_FLOAT_KEYWORDS: tuple[str, ...] = (
"COHPstartEnergy",
"COHPendEnergy",
"gaussianSmearingWidth",
"useDecimalPlaces",
"COHPSteps",
"basisRotation",
"gridDensityForPrinting",
"gridBufferForPrinting",
)
# These keywords need an additional string suffix
_STRING_KEYWORDS: tuple[str, ...] = (
"basisSet",
"cohpGenerator",
"realspaceHamiltonian",
"realspaceOverlap",
"printPAWRealSpaceWavefunction",
"printLCAORealSpaceWavefunction",
"kSpaceCOHP",
"EwaldSum",
)
# The keywords themselves (without suffix) can trigger additional functionalities
_BOOLEAN_KEYWORDS: tuple[str, ...] = (
"saveProjectionToFile",
"skipCar",
"skipdos",
"skipcohp",
"skipcoop",
"skipcobi",
"skipMOFE",
"skipMolecularOrbitals",
"skipMadelungEnergy",
"loadProjectionFromFile",
"printTotalSpilling",
"forceEnergyRange",
"DensityOfEnergy",
"BWDF",
"BWDFCOHP",
"skipPopulationAnalysis",
"skipGrossPopulation",
"userecommendedbasisfunctions",
"skipProjection",
"printLmosOnAtoms",
"printMofeAtomWise",
"printMofeMoleculeWise",
"writeAtomicOrbitals",
"writeBasisFunctions",
"writeMatricesToFile",
"noFFTforVisualization",
"RMSp",
"onlyReadVasprun.xml",
"noMemoryMappedFiles",
"skipPAWOrthonormalityTest",
"doNotIgnoreExcessiveBands",
"doNotUseAbsoluteSpilling",
"skipReOrthonormalization",
"forceV1HMatrix",
"useOriginalTetrahedronMethod",
"forceEnergyRange",
"bandwiseSpilling",
"kpointwiseSpilling",
"LSODOS",
"autoRotate",
"doNotOrthogonalizeBasis",
)
# These keywords need additional string suffixes.
# They could be used multiple times within one lobsterin.
_LIST_KEYWORDS: tuple[str, ...] = (
"basisfunctions",
"cohpbetween",
"createFatband",
"customSTOforAtom",
"cobiBetween",
"printLmosOnAtomswriteAtomicDensities",
)
# Generate {lowered: original} mappings
FLOAT_KEYWORDS: ClassVar[dict[str, str]] = {key.lower(): key for key in _FLOAT_KEYWORDS}
STRING_KEYWORDS: ClassVar[dict[str, str]] = {key.lower(): key for key in _STRING_KEYWORDS}
BOOLEAN_KEYWORDS: ClassVar[dict[str, str]] = {key.lower(): key for key in _BOOLEAN_KEYWORDS}
LIST_KEYWORDS: ClassVar[dict[str, str]] = {key.lower(): key for key in _LIST_KEYWORDS}
# All known keywords
AVAILABLE_KEYWORDS: ClassVar[dict[str, str]] = {
**FLOAT_KEYWORDS,
**STRING_KEYWORDS,
**BOOLEAN_KEYWORDS,
**LIST_KEYWORDS,
}
def __init__(self, settingsdict: dict) -> None:
"""
Args:
settingsdict: dict to initialize Lobsterin.
"""
super().__init__()
# Check for duplicates from case sensitivity
keys = tuple(map(str.lower, settingsdict.keys()))
if len(keys) != len(set(keys)):
raise KeyError("There are duplicates for the keywords!")
self.update(settingsdict)
def __setitem__(self, key: str, val: Any) -> None:
"""
Necessary due to the missing case sensitivity of lobsterin
keywords. Also clean the keys and values by stripping white spaces.
Raises:
KeyError: if keyword is not available.
"""
key = key.strip().lower()
if key not in type(self).AVAILABLE_KEYWORDS:
raise KeyError(f"Key {key} is currently not available")
super().__setitem__(key, val.strip() if isinstance(val, str) else val)
def __getitem__(self, key: str) -> Any:
"""To avoid cases sensitivity problems."""
try:
return super().__getitem__(key.strip().lower())
except KeyError as exc:
raise KeyError(f"{key=} is not available") from exc
def __contains__(self, key: str) -> bool:
"""To avoid cases sensitivity problems."""
return super().__contains__(key.lower().strip())
def __delitem__(self, key: str) -> None:
"""To avoid cases sensitivity problems."""
super().__delitem__(key.lower().strip())
def diff(self, other: Self) -> dict[str, dict[str, Any]]:
"""Compare two Lobsterin and find which parameters are the same.
Similar to the diff method of Incar.
Args:
other (Lobsterin): Lobsterin object to compare to.
Returns:
dict: {"Same": same_params, "Different": diff_params}
"""
same_params = {}
diff_params = {}
# Check self
for k1, v1 in self.items():
if k1 not in other:
diff_params[k1] = {"lobsterin1": v1, "lobsterin2": None}
# String keywords
elif isinstance(v1, str):
if v1 != other[k1]:
diff_params[k1] = {"lobsterin1": v1, "lobsterin2": other[k1]}
else:
same_params[k1] = v1
# List keywords
elif isinstance(v1, list):
new_set1 = {value.strip().lower() for value in v1}
new_set2 = {value.strip().lower() for value in other[k1]}
if new_set1 != new_set2:
diff_params[k1] = {"lobsterin1": v1, "lobsterin2": other[k1]}
# Float/boolean keywords
elif v1 != other[k1]:
diff_params[k1] = {"lobsterin1": v1, "lobsterin2": other[k1]}
else:
same_params[k1] = v1
# Check other
for k2, v2 in other.items():
if k2 not in self and k2 not in diff_params:
diff_params[k2] = {"lobsterin1": None, "lobsterin2": v2}
return {"Same": same_params, "Different": diff_params}
def write_lobsterin(
self,
path: PathLike = "lobsterin",
overwritedict: dict | None = None,
) -> None:
"""Write a lobsterin file, and recover keys to Camel case.
Args:
path (str): filename of the output lobsterin file
overwritedict (dict): dict that can be used to update lobsterin, e.g. {"skipdos": True}
"""
# Update previous entries
if overwritedict is not None:
self.update(overwritedict)
with open(path, mode="w", encoding="utf-8") as file:
for key in self:
if key in type(self).FLOAT_KEYWORDS or key in type(self).STRING_KEYWORDS:
file.write(f"{type(self).AVAILABLE_KEYWORDS[key]} {self.get(key)}\n")
elif key in type(self).BOOLEAN_KEYWORDS:
file.write(f"{type(self).BOOLEAN_KEYWORDS[key]}\n")
elif key in type(self).LIST_KEYWORDS:
for value in self.get(key): # type: ignore[union-attr]
file.write(f"{type(self).LIST_KEYWORDS[key]} {value}\n")
def as_dict(self) -> dict:
"""MSONable dict."""
dct = dict(self)
dct["@module"] = type(self).__module__
dct["@class"] = type(self).__name__
return dct
@classmethod
def from_dict(cls, dct: dict) -> Self:
"""
Args:
dct (dict): Dict representation.
Returns:
Lobsterin
"""
return cls({key: val for key, val in dct.items() if key not in {"@module", "@class"}})
def _get_nbands(self, structure: Structure) -> int:
"""Get number of bands."""
if self.get("basisfunctions") is None:
raise ValueError("No basis functions are provided. The program cannot calculate nbands.")
basis_functions: list[str] = []
for string_basis in self["basisfunctions"]:
string_basis_raw = string_basis.strip().split(" ")
while "" in string_basis_raw:
string_basis_raw.remove("")
for _idx in range(int(structure.composition.element_composition[string_basis_raw[0]])):
basis_functions.extend(string_basis_raw[1:])
num_basis_functions = 0
for basis in basis_functions:
if "s" in basis:
num_basis_functions += 1
elif "p" in basis:
num_basis_functions += 3
elif "d" in basis:
num_basis_functions += 5
elif "f" in basis:
num_basis_functions += 7
return int(num_basis_functions)
def write_INCAR(
self,
incar_input: PathLike = "INCAR",
incar_output: PathLike = "INCAR.lobster",
poscar_input: PathLike = "POSCAR",
isym: Literal[-1, 0] = 0,
further_settings: dict | None = None,
) -> None:
"""Write INCAR file. Will only make the run static, insert NBANDS,
set ISYM=0, LWAVE=True and you have to check for the rest.
Args:
incar_input (PathLike): path to input INCAR
incar_output (PathLike): path to output INCAR
poscar_input (PathLike): path to input POSCAR
isym (-1 | 0): ISYM value.
further_settings (dict): A dict can be used to include further settings, e.g. {"ISMEAR":-5}
"""
# Read INCAR from file, which will be modified
incar = Incar.from_file(incar_input)
warnings.warn("Please check your incar_input before using it. This method only changes three settings!")
if isym in {-1, 0}:
incar["ISYM"] = isym
else:
raise ValueError(f"Got {isym=}, must be -1 or 0")
incar["NSW"] = 0
incar["LWAVE"] = True
# Get NBANDS from _get_nbands (use basis set that is inserted)
incar["NBANDS"] = self._get_nbands(Structure.from_file(poscar_input))
if further_settings is not None:
for key, item in further_settings.items():
incar[key] = item
incar.write_file(incar_output)
@staticmethod
def get_basis(
structure: Structure,
potcar_symbols: list[str],
address_basis_file: PathLike | None = None,
) -> list[str]:
"""Get the basis functions from given potcar_symbols, e.g., ["Fe_pv", "Si"].
Args:
structure (Structure): Structure object
potcar_symbols: list of potcar symbols
address_basis_file (PathLike): path to the basis file
Returns:
basis
"""
if address_basis_file is None:
address_basis_file = f"{MODULE_DIR}/lobster_basis/BASIS_PBE_54_standard.yaml"
atom_types_potcar = [name.split("_")[0] for name in potcar_symbols]
if set(structure.symbol_set) != set(atom_types_potcar):
raise ValueError("Your POSCAR does not correspond to your POTCAR!")
basis = loadfn(address_basis_file)["BASIS"]
basis_functions = []
list_forin = []
for idx, name in enumerate(potcar_symbols):
if name not in basis:
raise ValueError(
f"Missing basis information for POTCAR symbol: {name}. Please provide the basis manually."
)
basis_functions.append(basis[name].split())
list_forin.append(f"{atom_types_potcar[idx]} {basis[name]}")
return list_forin
@staticmethod
def get_all_possible_basis_functions(
structure: Structure,
potcar_symbols: list[str],
address_basis_file_min: PathLike | None = None,
address_basis_file_max: PathLike | None = None,
) -> list[dict]:
"""
Args:
structure: Structure object
potcar_symbols: list of the potcar symbols
address_basis_file_min: path to file with the minimum required basis by the POTCAR
address_basis_file_max: path to file with the largest possible basis of the POTCAR.
Returns:
list[dict]: Can be used to create new Lobsterin objects in
standard_calculations_from_vasp_files as dict_for_basis
"""
max_basis = Lobsterin.get_basis(
structure=structure,
potcar_symbols=potcar_symbols,
address_basis_file=address_basis_file_max or f"{MODULE_DIR}/lobster_basis/BASIS_PBE_54_max.yaml",
)
min_basis = Lobsterin.get_basis(
structure=structure,
potcar_symbols=potcar_symbols,
address_basis_file=address_basis_file_min or f"{MODULE_DIR}/lobster_basis/BASIS_PBE_54_min.yaml",
)
all_basis = get_all_possible_basis_combinations(min_basis=min_basis, max_basis=max_basis)
list_basis_dict = []
for basis in all_basis:
basis_dict = {}
for elba in basis:
basplit = elba.split()
basis_dict[basplit[0]] = " ".join(basplit[1:])
list_basis_dict.append(basis_dict)
return list_basis_dict
@staticmethod
def write_POSCAR_with_standard_primitive(
POSCAR_input: PathLike = "POSCAR",
POSCAR_output: PathLike = "POSCAR.lobster",
symprec: float = 0.01,
) -> None:
"""Write a POSCAR with the standard primitive cell.
This is needed to arrive at the correct kpath.
Args:
POSCAR_input (PathLike): Input POSCAR file
POSCAR_output (PathLike): Output POSCAR file
symprec (float): precision to find symmetry
"""
structure = Structure.from_file(POSCAR_input)
kpath = HighSymmKpath(structure, symprec=symprec)
new_structure = kpath.prim
new_structure.to(fmt="POSCAR", filename=POSCAR_output)
@staticmethod
def write_KPOINTS(
POSCAR_input: PathLike = "POSCAR",
KPOINTS_output: PathLike = "KPOINTS.lobster",
reciprocal_density: int = 100,
isym: Literal[-1, 0] = 0,
from_grid: bool = False,
input_grid: Tuple3Ints = (5, 5, 5),
line_mode: bool = True,
kpoints_line_density: int = 20,
symprec: float = 0.01,
) -> None:
"""Write a gamma-centered KPOINTS file for LOBSTER.
Args:
POSCAR_input (PathLike): path to POSCAR
KPOINTS_output (PathLike): path to output KPOINTS
reciprocal_density (int): Grid density
isym (-1 | 0): ISYM value.
from_grid (bool): If True KPOINTS will be generated with the help of a grid given in input_grid.
Otherwise, they will be generated from the reciprocal_density
input_grid (tuple): grid to generate the KPOINTS file
line_mode (bool): If True, band structure will be generated
kpoints_line_density (int): density of the lines in the band structure
symprec (float): precision to determine symmetry
"""
structure = Structure.from_file(POSCAR_input)
if not from_grid:
kpoint_grid = Kpoints.automatic_density_by_vol(structure, reciprocal_density).kpts
mesh = kpoint_grid[0]
else:
mesh = input_grid
# The following code is taken from SpacegroupAnalyzer
# We need to switch off symmetry here
matrix = structure.lattice.matrix
positions = structure.frac_coords
unique_species: list[Composition] = []
zs = []
magmoms = []
for species, group in itertools.groupby(structure, key=lambda s: s.species):
if species in unique_species:
ind = unique_species.index(species)
zs.extend([ind + 1] * len(tuple(group)))
else:
unique_species.append(species)
zs.extend([len(unique_species)] * len(tuple(group)))
for site in structure:
if hasattr(site, "magmom"):
magmoms.append(site.magmom)
elif site.is_ordered and hasattr(site.specie, "spin"):
magmoms.append(site.specie.spin)
else:
magmoms.append(0)
# For now, we are setting MAGMOM to zero. (Taken from INCAR class)
cell = matrix, positions, zs, magmoms
# TODO: what about this shift?
mapping, grid = spglib.get_ir_reciprocal_mesh(mesh, cell, is_shift=[0, 0, 0])
# Get the KPOINTS for the grid
if isym == -1:
kpts = []
weights = []
all_labels = []
for gp in grid:
kpts.append(gp.astype(float) / mesh)
weights.append(float(1))
all_labels.append("")
# Time reversal symmetry: k and -k are equivalent
elif isym == 0:
kpts = []
weights = []
all_labels = []
newlist = [list(gp) for gp in list(grid)]
mapping = []
for gp in newlist:
minus_gp = [-k for k in gp]
if minus_gp in newlist and minus_gp != [0, 0, 0]:
mapping.append(newlist.index(minus_gp))
else:
mapping.append(newlist.index(gp))
for igp, gp in enumerate(newlist):
if mapping[igp] > igp:
kpts.append(np.array(gp).astype(float) / mesh)
weights.append(float(2))
all_labels.append("")
elif mapping[igp] == igp:
kpts.append(np.array(gp).astype(float) / mesh)
weights.append(float(1))
all_labels.append("")
else:
raise ValueError(f"Got {isym=}, must be -1 or 0")
# Line mode
if line_mode:
kpath = HighSymmKpath(structure, symprec=symprec)
if not np.allclose(kpath.prim.lattice.matrix, structure.lattice.matrix):
raise ValueError(
"You are not using the standard primitive cell. The k-path is not correct. Please generate a "
"standard primitive cell first."
)
frac_k_points, labels = kpath.get_kpoints(line_density=kpoints_line_density, coords_are_cartesian=False)
for k, f in enumerate(frac_k_points):
kpts.append(f)
weights.append(0.0)
all_labels.append(labels[k])
comment = f"{isym=}, grid: {mesh} plus kpoint path" if line_mode else f"{isym=}, grid: {mesh}"
kpoints_instance = Kpoints(
comment=comment,
style=Kpoints.supported_modes.Reciprocal,
num_kpts=len(kpts),
kpts=tuple(kpts),
kpts_weights=weights,
labels=all_labels,
)
kpoints_instance.write_file(filename=KPOINTS_output)
@classmethod
def from_file(cls, lobsterin: PathLike) -> Self:
"""Create Lobsterin from lobsterin file.
Args:
lobsterin (PathLike): path to lobsterin.
Returns:
Lobsterin object
"""
with zopen(lobsterin, mode="rt") as file:
lines = file.read().split("\n")
if not lines:
raise RuntimeError("lobsterin file contains no data.")
lobsterin_dict: dict[str, Any] = {}
for line in lines:
# Remove comment lines and in-line comments
if line := re.split(r"[!#//]", line)[0].strip():
# Extract keywords
line_parts = line.replace("\t", " ").strip().split()
if line_parts:
key = line_parts[0].lower()
else:
continue
# Avoid duplicates for float/string keywords
if (key in cls.FLOAT_KEYWORDS or key in cls.STRING_KEYWORDS) and key in lobsterin_dict:
raise ValueError(f"Same keyword {key} twice!")
# Parse by keyword type
if key in cls.BOOLEAN_KEYWORDS:
lobsterin_dict[key] = True
elif key in cls.FLOAT_KEYWORDS:
lobsterin_dict[key] = float(line_parts[1])
elif key in cls.STRING_KEYWORDS:
lobsterin_dict[key] = " ".join(line_parts[1:])
elif key in cls.LIST_KEYWORDS:
if key in lobsterin_dict:
lobsterin_dict[key].append(" ".join(line_parts[1:]))
else:
lobsterin_dict[key] = [" ".join(line_parts[1:])]
else:
raise ValueError(f"Invalid {key=}.")
return cls(lobsterin_dict)
@staticmethod
def _get_potcar_symbols(POTCAR_input: PathLike) -> list[str]:
"""Get the name of the species in the POTCAR.
Args:
POTCAR_input (PathLike): path to potcar file
Returns:
list[str]: names of the species
"""
potcar = Potcar.from_file(POTCAR_input)
for pot in potcar:
if pot.potential_type != "PAW":
raise ValueError("Lobster only works with PAW! Use different POTCARs")
# Warning about a bug in LOBSTER-4.1.0
with zopen(POTCAR_input, mode="r") as file:
data = file.read()
if isinstance(data, bytes):
data = data.decode("utf-8")
if "SHA256" in data or "COPYR" in data:
warnings.warn(
"These POTCARs are not compatible with "
"Lobster up to version 4.1.0."
"\n The keywords SHA256 and COPYR "
"cannot be handled by Lobster"
" \n and will lead to wrong results."
)
if potcar.functional != "PBE":
raise RuntimeError("We only have BASIS options for PBE so far")
return [name["symbol"] for name in potcar.spec]
@classmethod
def standard_calculations_from_vasp_files(
cls,
POSCAR_input: PathLike = "POSCAR",
INCAR_input: PathLike = "INCAR",
POTCAR_input: PathLike | None = None,
Vasprun_output: PathLike = "vasprun.xml",
dict_for_basis: dict | None = None,
option: str = "standard",
) -> Self:
"""Generate lobsterin with standard settings.
Args:
POSCAR_input (PathLike): path to POSCAR
INCAR_input (PathLike): path to INCAR
POTCAR_input (PathLike): path to POTCAR
Vasprun_output (PathLike): path to vasprun.xml
dict_for_basis (dict): can be provided: it should look the following:
dict_for_basis={"Fe":'3p 3d 4s 4f', "C": '2s 2p'} and will overwrite all settings from POTCAR_input
option (str): 'standard' will start a normal LOBSTER run where COHPs, COOPs, DOS, CHARGE etc. will be
calculated
'standard_with_energy_range_from_vasprun' will start a normal LOBSTER run for entire energy range
of VASP static run. vasprun.xml file needs to be in current directory.
'standard_from_projection' will start a normal LOBSTER run from a projection
'standard_with_fatband' will do a fatband calculation, run over all orbitals
'onlyprojection' will only do a projection
'onlydos' will only calculate a projected dos
'onlycohp' will only calculate cohp
'onlycoop' will only calculate coop
'onlycohpcoop' will only calculate cohp and coop
Returns:
Lobsterin with standard settings
"""
warnings.warn(
"Always check and test the provided basis functions. The spilling of your Lobster calculation might help"
)
if option not in {
"standard",
"standard_from_projection",
"standard_with_fatband",
"standard_with_energy_range_from_vasprun",
"onlyprojection",
"onlydos",
"onlycohp",
"onlycoop",
"onlycobi",
"onlycohpcoop",
"onlycohpcoopcobi",
"onlymadelung",
}:
raise ValueError("The option is not valid!")
lobsterin_dict: dict[str, Any] = {
# This basis set covers elements up to Lr (Z = 103)
"basisSet": "pbeVaspFit2015",
# Energies around e-fermi
"COHPstartEnergy": -35.0,
"COHPendEnergy": 5.0,
}
if option in {
"standard",
"standard_with_energy_range_from_vasprun",
"onlycohp",
"onlycoop",
"onlycobi",
"onlycohpcoop",
"onlycohpcoopcobi",
"standard_with_fatband",
}:
# Every interaction with a distance of 6.0 Å is checked
lobsterin_dict["cohpGenerator"] = "from 0.1 to 6.0 orbitalwise"
# Save the projection
lobsterin_dict["saveProjectionToFile"] = True
if option == "standard_from_projection":
lobsterin_dict["cohpGenerator"] = "from 0.1 to 6.0 orbitalwise"
lobsterin_dict["loadProjectionFromFile"] = True
elif option == "standard_with_energy_range_from_vasprun":
vasp_run = Vasprun(Vasprun_output)
lobsterin_dict["COHPstartEnergy"] = round(
min(vasp_run.complete_dos.energies - vasp_run.complete_dos.efermi), 4
)
lobsterin_dict["COHPendEnergy"] = round(
max(vasp_run.complete_dos.energies - vasp_run.complete_dos.efermi), 4
)
lobsterin_dict["COHPSteps"] = len(vasp_run.complete_dos.energies)
# TODO: add COBI here! might be relevant LOBSTER version
elif option == "onlycohp":
lobsterin_dict["skipdos"] = True
lobsterin_dict["skipcoop"] = True
lobsterin_dict["skipPopulationAnalysis"] = True
lobsterin_dict["skipGrossPopulation"] = True
# LOBSTER-4.1.0
lobsterin_dict["skipcobi"] = True
lobsterin_dict["skipMadelungEnergy"] = True
elif option == "onlycoop":
lobsterin_dict["skipdos"] = True
lobsterin_dict["skipcohp"] = True
lobsterin_dict["skipPopulationAnalysis"] = True
lobsterin_dict["skipGrossPopulation"] = True
# LOBSTER-4.1.0
lobsterin_dict["skipcobi"] = True
lobsterin_dict["skipMadelungEnergy"] = True
elif option == "onlycohpcoop":
lobsterin_dict["skipdos"] = True
lobsterin_dict["skipPopulationAnalysis"] = True
lobsterin_dict["skipGrossPopulation"] = True
# LOBSTER-4.1.0
lobsterin_dict["skipcobi"] = True
lobsterin_dict["skipMadelungEnergy"] = True
elif option == "onlycohpcoopcobi":
lobsterin_dict["skipdos"] = True
lobsterin_dict["skipPopulationAnalysis"] = True
lobsterin_dict["skipGrossPopulation"] = True
lobsterin_dict["skipMadelungEnergy"] = True
elif option == "onlydos":
lobsterin_dict["skipcohp"] = True
lobsterin_dict["skipcoop"] = True
lobsterin_dict["skipPopulationAnalysis"] = True
lobsterin_dict["skipGrossPopulation"] = True
# LOBSTER-4.1.0
lobsterin_dict["skipcobi"] = True
lobsterin_dict["skipMadelungEnergy"] = True
elif option == "onlyprojection":
lobsterin_dict["skipdos"] = True
lobsterin_dict["skipcohp"] = True
lobsterin_dict["skipcoop"] = True
lobsterin_dict["skipPopulationAnalysis"] = True
lobsterin_dict["skipGrossPopulation"] = True
lobsterin_dict["saveProjectionToFile"] = True
# LOBSTER-4.1.0
lobsterin_dict["skipcobi"] = True
lobsterin_dict["skipMadelungEnergy"] = True
elif option == "onlycobi":
lobsterin_dict["skipdos"] = True
lobsterin_dict["skipcohp"] = True
lobsterin_dict["skipPopulationAnalysis"] = True
lobsterin_dict["skipGrossPopulation"] = True
# LOBSTER-4.1.0
lobsterin_dict["skipcobi"] = True
lobsterin_dict["skipMadelungEnergy"] = True
elif option == "onlymadelung":
lobsterin_dict["skipdos"] = True
lobsterin_dict["skipcohp"] = True
lobsterin_dict["skipcoop"] = True
lobsterin_dict["skipPopulationAnalysis"] = True
lobsterin_dict["skipGrossPopulation"] = True
lobsterin_dict["saveProjectionToFile"] = True
# LOBSTER-4.1.0
lobsterin_dict["skipcobi"] = True
incar = Incar.from_file(INCAR_input)
if incar["ISMEAR"] == 0:
lobsterin_dict["gaussianSmearingWidth"] = incar["SIGMA"]
if incar["ISMEAR"] != 0 and option == "standard_with_fatband":
raise ValueError("ISMEAR has to be 0 for a fatband calculation with Lobster")
if dict_for_basis is not None:
# dict_for_basis = {"Fe":"3p 3d 4s 4f", "C": "2s 2p"}
# Will just insert this basis and not check with poscar
basis = [f"{key} {value}" for key, value in dict_for_basis.items()]
elif POTCAR_input is not None:
# Get basis functions from POTCAR
potcar_names = cls._get_potcar_symbols(POTCAR_input=POTCAR_input)
basis = cls.get_basis(structure=Structure.from_file(POSCAR_input), potcar_symbols=potcar_names)
else:
raise ValueError("basis cannot be generated")
lobsterin_dict["basisfunctions"] = basis
if option == "standard_with_fatband":
lobsterin_dict["createFatband"] = basis
return cls(lobsterin_dict)
def get_all_possible_basis_combinations(min_basis: list, max_basis: list) -> list[list[str]]:
"""Get all possible basis combinations.
Args:
min_basis: list of basis entries: e.g., ["Si 3p 3s"]
max_basis: list of basis entries: e.g., ["Si 3p 3s"].
Returns:
list[list[str]]: all possible combinations of basis functions, e.g. [["Si 3p 3s"]]
"""
max_basis_lists = [x.split() for x in max_basis]
min_basis_lists = [x.split() for x in min_basis]
# Get all possible basis functions
basis_dict: dict[str, dict] = {}
for iel, el in enumerate(max_basis_lists):
basis_dict[el[0]] = {"fixed": [], "variable": [], "combinations": []}
for basis in el[1:]:
if basis in min_basis_lists[iel]:
basis_dict[el[0]]["fixed"].append(basis)
if basis not in min_basis_lists[iel]:
basis_dict[el[0]]["variable"].append(basis)
for L in range(len(basis_dict[el[0]]["variable"]) + 1):
for subset in itertools.combinations(basis_dict[el[0]]["variable"], L):
basis_dict[el[0]]["combinations"].append(" ".join([el[0]] + basis_dict[el[0]]["fixed"] + list(subset)))
list_basis = [item["combinations"] for item in basis_dict.values()]
# Get all combinations
start_basis = list_basis[0]
if len(list_basis) > 1:
for el in list_basis[1:]:
new_start_basis = []
for elbasis in start_basis:
for elbasis2 in el:
if not isinstance(elbasis, list):
new_start_basis.append([elbasis, elbasis2])
else:
new_start_basis.append([*elbasis.copy(), elbasis2])
start_basis = new_start_basis
return start_basis
return [[basis] for basis in start_basis]