-
Notifications
You must be signed in to change notification settings - Fork 850
/
boltztrap2.py
1186 lines (1007 loc) · 44.6 KB
/
boltztrap2.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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""BoltzTraP2 is a python software interpolating band structures and
computing materials properties from dft band structure using Boltzmann
semi-classical transport theory.
This module provides a pymatgen interface to BoltzTraP2.
Some of the code is written following the examples provided in BoltzTraP2.
BoltzTraP2 has been developed by Georg Madsen, Jesús Carrete, Matthieu J. Verstraete.
https://gitlab.com/sousaw/BoltzTraP2
https://www.sciencedirect.com/science/article/pii/S0010465518301632
References are:
Georg K.H.Madsen, Jesús Carrete, Matthieu J.Verstraete
BoltzTraP2, a program for interpolating band structures and
calculating semi-classical transport coefficients
Computer Physics Communications 231, 140-145, 2018
Madsen, G. K. H., and Singh, D. J. (2006).
BoltzTraP. A code for calculating band-structure dependent quantities.
Computer Physics Communications, 175, 67-71
Todo:
- DONE: spin polarized bands
- read first derivative of the eigenvalues from vasprun.xml (mommat)
- handle magnetic moments (magmom)
"""
from __future__ import annotations
import warnings
import matplotlib.pyplot as plt
import numpy as np
from monty.serialization import dumpfn, loadfn
from tqdm import tqdm
from pymatgen.electronic_structure.bandstructure import BandStructure, BandStructureSymmLine, Spin
from pymatgen.electronic_structure.boltztrap import BoltztrapError
from pymatgen.electronic_structure.dos import CompleteDos, Dos, Orbital
from pymatgen.electronic_structure.plotter import BSPlotter, DosPlotter
from pymatgen.io.ase import AseAtomsAdaptor
from pymatgen.io.vasp import Vasprun
from pymatgen.symmetry.bandstructure import HighSymmKpath
try:
from BoltzTraP2 import bandlib as BL
from BoltzTraP2 import fite, sphere, units
except ImportError:
raise BoltztrapError("BoltzTraP2 has to be installed and working")
__author__ = "Francesco Ricci"
__copyright__ = "Copyright 2018, The Materials Project"
__version__ = "1.0"
__maintainer__ = "Fracesco Ricci"
__email__ = "frankyricci@gmail.com"
__status__ = "Development"
__date__ = "August 2018"
class VasprunBSLoader:
"""Loader for Bandstructure and Vasprun pmg objects."""
def __init__(self, obj, structure=None, nelect=None) -> None:
"""
Args:
obj: Either a pmg Vasprun or a BandStructure object.
structure: Structure object in case is not included in the BandStructure object.
nelect: number of electrons in case a BandStructure obj is provided.
Example:
vrun = Vasprun('vasprun.xml')
data = VasprunBSLoader(vrun)
"""
if isinstance(obj, Vasprun):
structure = obj.final_structure
nelect = obj.parameters["NELECT"]
bs_obj = obj.get_band_structure()
elif isinstance(obj, BandStructure):
bs_obj = obj
else:
raise BoltztrapError("The object provided is neither a Bandstructure nor a Vasprun.")
self.kpoints = np.array([kp.frac_coords for kp in bs_obj.kpoints])
if bs_obj.structure:
self.structure = bs_obj.structure
elif structure:
self.structure = structure
else:
raise BoltztrapError("A structure must be given.")
self.atoms = AseAtomsAdaptor.get_atoms(self.structure)
self.proj_all = None
if bs_obj.projections:
self.proj_all = {sp: p.transpose((1, 0, 3, 2)) for sp, p in bs_obj.projections.items()}
e = np.array(list(bs_obj.bands.values()))
e = e.reshape(-1, e.shape[-1])
self.ebands_all = e * units.eV
self.is_spin_polarized = bs_obj.is_spin_polarized
if bs_obj.is_spin_polarized:
self.dosweight = 1.0
else:
self.dosweight = 2.0
self.lattvec = self.atoms.get_cell().T * units.Angstrom
self.mommat_all = None # not implemented yet
self.mommat = None # not implemented yet
self.magmom = None # not implemented yet
self.fermi = bs_obj.efermi * units.eV
self.UCvol = self.structure.volume * units.Angstrom**3
if not bs_obj.is_metal():
self.vbm_idx = max(bs_obj.get_vbm()["band_index"][Spin.up] + bs_obj.get_vbm()["band_index"][Spin.down])
self.cbm_idx = min(bs_obj.get_cbm()["band_index"][Spin.up] + bs_obj.get_cbm()["band_index"][Spin.down])
self.vbm = bs_obj.get_vbm()["energy"]
self.cbm = bs_obj.get_cbm()["energy"]
else:
self.vbm_idx = self.cbm_idx = None
self.vbm = self.fermi / units.eV
self.cbm = self.fermi / units.eV
if nelect:
self.nelect_all = nelect
elif self.vbm_idx:
self.nelect_all = self.vbm_idx + self.cbm_idx + 1
else:
raise BoltztrapError("nelect must be given.")
@classmethod
def from_file(cls, vasprun_file):
"""Get a vasprun.xml file and return a VasprunBSLoader."""
vrun_obj = Vasprun(vasprun_file, parse_projected_eigen=True)
return cls(vrun_obj)
def get_lattvec(self):
"""The lattice vectors."""
try:
return self.lattvec
except AttributeError:
self.lattvec = self.atoms.get_cell().T * units.Angstrom
return self.lattvec
def get_volume(self):
"""Volume."""
try:
return self.UCvol
except AttributeError:
lattvec = self.get_lattvec()
self.UCvol = np.abs(np.linalg.det(lattvec))
return self.UCvol
def bandana(self, emin=-np.inf, emax=np.inf):
"""Cut out bands outside the range (emin,emax)."""
bandmin = np.min(self.ebands_all, axis=1)
bandmax = np.max(self.ebands_all, axis=1)
ntoolow = np.count_nonzero(bandmax <= emin)
accepted = np.logical_and(bandmin < emax, bandmax > emin)
# self.data_bkp = np.copy(self.data.ebands)
self.ebands = self.ebands_all[accepted]
self.proj = {}
if self.proj_all:
if len(self.proj_all) == 2:
h = int(len(accepted) / 2)
self.proj[Spin.up] = self.proj_all[Spin.up][:, accepted[:h], :, :]
self.proj[Spin.down] = self.proj_all[Spin.down][:, accepted[h:], :, :]
elif len(self.proj_all) == 1:
self.proj[Spin.up] = self.proj_all[Spin.up][:, accepted, :, :]
if self.mommat_all:
self.mommat = self.mommat[:, accepted, :]
# Removing bands may change the number of valence electrons
if self.nelect_all:
self.nelect = self.nelect_all - self.dosweight * ntoolow
return accepted
class BandstructureLoader:
"""Loader for Bandstructure object."""
def __init__(self, bs_obj, structure=None, nelect=None, mommat=None, magmom=None) -> None:
"""
Args:
bs_obj: BandStructure object.
structure: Structure object. It is needed if it is not contained in the BandStructure obj.
nelect: Number of electrons in the calculation.
mommat: Matrix of derivatives of energy eigenvalues. TODO Not implemented yet.
magmom: Matrix of magnetic moments in non collinear calculations. Not implemented yet.
Example:
vrun = Vasprun('vasprun.xml')
bs = vrun.get_band_structure()
st = vrun.final_structure
ne = vrun.parameters['NELECT']
data = BandstructureLoader(bs,st,ne)
"""
warnings.warn("Deprecated Loader. Use VasprunBSLoader instead.")
self.kpoints = np.array([kp.frac_coords for kp in bs_obj.kpoints])
if structure is None:
self.structure = bs_obj.structure
else:
self.structure = structure
self.atoms = AseAtomsAdaptor.get_atoms(self.structure)
self.proj_all = None
if bs_obj.projections:
self.proj_all = {sp: p.transpose((1, 0, 3, 2)) for sp, p in bs_obj.projections.items()}
e = np.array(list(bs_obj.bands.values()))
e = e.reshape(-1, e.shape[-1])
self.ebands_all = e * units.eV
self.is_spin_polarized = bs_obj.is_spin_polarized
if bs_obj.is_spin_polarized:
self.dosweight = 1.0
else:
self.dosweight = 2.0
self.lattvec = self.atoms.get_cell().T * units.Angstrom
self.mommat_all = mommat # not implemented yet
self.mommat = mommat # not implemented yet
self.magmom = magmom # not implemented yet
self.fermi = bs_obj.efermi * units.eV
self.UCvol = self.structure.volume * units.Angstrom**3
if not bs_obj.is_metal():
self.vbm_idx = max(bs_obj.get_vbm()["band_index"][Spin.up] + bs_obj.get_vbm()["band_index"][Spin.down])
self.cbm_idx = min(bs_obj.get_cbm()["band_index"][Spin.up] + bs_obj.get_cbm()["band_index"][Spin.down])
self.vbm = bs_obj.get_vbm()["energy"]
self.cbm = bs_obj.get_cbm()["energy"]
self.nelect_all = self.vbm_idx * self.dosweight
else:
self.vbm_idx = self.cbm_idx = None
self.vbm = self.fermi
self.cbm = self.fermi
self.nelect_all = nelect
def get_lattvec(self):
"""The lattice vectors."""
try:
return self.lattvec
except AttributeError:
self.lattvec = self.atoms.get_cell().T * units.Angstrom
return self.lattvec
def bandana(self, emin=-np.inf, emax=np.inf):
"""Cut out bands outside the range (emin,emax)."""
band_min = np.min(self.ebands_all, axis=1)
band_max = np.max(self.ebands_all, axis=1)
n_too_low = np.count_nonzero(band_max <= emin)
accepted = np.logical_and(band_min < emax, band_max > emin)
# self.data_bkp = np.copy(self.data.ebands)
self.ebands = self.ebands_all[accepted]
self.proj = {}
if self.proj_all:
if len(self.proj_all) == 2:
h = int(len(accepted) / 2)
self.proj[Spin.up] = self.proj_all[Spin.up][:, accepted[:h], :, :]
self.proj[Spin.down] = self.proj_all[Spin.down][:, accepted[h:], :, :]
elif len(self.proj) == 1:
self.proj[Spin.up] = self.proj_all[Spin.up][:, accepted, :, :]
if self.mommat_all:
self.mommat = self.mommat[:, accepted, :]
# Removing bands may change the number of valence electrons
if self.nelect_all:
self.nelect = self.nelect_all - self.dosweight * n_too_low
return accepted
def set_upper_lower_bands(self, e_lower, e_upper) -> None:
"""Set fake upper/lower bands, useful to set the same energy
range in the spin up/down bands when calculating the DOS.
"""
warnings.warn(
"This method does not work anymore in case of spin polarized case due to the concatenation of bands !"
)
lower_band = e_lower * np.ones((1, self.ebands.shape[1]))
upper_band = e_upper * np.ones((1, self.ebands.shape[1]))
self.ebands = np.vstack((lower_band, self.ebands, upper_band))
if self.proj:
for sp, proj in self.proj.items():
proj_lower = proj[:, 0:1, :, :]
proj_upper = proj[:, -1:, :, :]
self.proj[sp] = np.concatenate((proj_lower, proj, proj_upper), axis=1)
def get_volume(self):
"""Volume."""
try:
return self.UCvol
except AttributeError:
lattvec = self.get_lattvec()
self.UCvol = np.abs(np.linalg.det(lattvec))
return self.UCvol
class VasprunLoader:
"""Loader for Vasprun object."""
def __init__(self, vrun_obj=None) -> None:
"""vrun_obj: Vasprun object."""
warnings.warn("Deprecated Loader. Use VasprunBSLoader instead.")
if vrun_obj:
self.kpoints = np.array(vrun_obj.actual_kpoints)
self.structure = vrun_obj.final_structure
self.atoms = AseAtomsAdaptor.get_atoms(self.structure)
self.proj = None
if len(vrun_obj.eigenvalues) == 1:
e = next(iter(vrun_obj.eigenvalues.values()))
self.ebands = e[:, :, 0].transpose() * units.eV
self.dosweight = 2.0
if vrun_obj.projected_eigenvalues:
self.proj = next(iter(vrun_obj.projected_eigenvalues.values()))
elif len(vrun_obj.eigenvalues) == 2:
raise BoltztrapError("spin bs case not implemented")
self.lattvec = self.atoms.get_cell().T * units.Angstrom
# TODO: read mommat from vasprun
self.mommat = self.magmom = self.spin = None
self.fermi = vrun_obj.efermi * units.eV
self.nelect = vrun_obj.parameters["NELECT"]
self.UCvol = self.structure.volume * units.Angstrom**3
bs_obj = vrun_obj.get_band_structure()
if not bs_obj.is_metal():
self.vbm_idx = max(bs_obj.get_vbm()["band_index"][Spin.up] + bs_obj.get_vbm()["band_index"][Spin.down])
self.cbm_idx = min(bs_obj.get_cbm()["band_index"][Spin.up] + bs_obj.get_cbm()["band_index"][Spin.down])
self.vbm = bs_obj.get_vbm()["energy"]
self.cbm = bs_obj.get_cbm()["energy"]
else:
self.vbm_idx = self.cbm_idx = None
self.vbm = self.fermi
self.cbm = self.fermi
@classmethod
def from_file(cls, vasprun_file):
"""Get a vasprun.xml file and return a VasprunLoader."""
vrun_obj = Vasprun(vasprun_file, parse_projected_eigen=True)
return VasprunLoader(vrun_obj)
def get_lattvec(self):
"""Lattice vectors."""
try:
return self.lattvec
except AttributeError:
self.lattvec = self.atoms.get_cell().T * units.Angstrom
return self.lattvec
def bandana(self, emin=-np.inf, emax=np.inf):
"""Cut out bands outside the range (emin,emax)."""
band_min = np.min(self.ebands, axis=1)
band_max = np.max(self.ebands, axis=1)
ii = np.nonzero(band_min < emax)
n_emax = ii[0][-1]
ii = np.nonzero(band_max > emin)
n_emin = ii[0][0]
# BoltzTraP2.misc.info("BANDANA output")
# for iband in range(len(self.ebands)):
# BoltzTraP2.misc.info(iband, bandmin[iband], bandmax[iband], (
# (bandmin[iband] < emax) & (bandmax[iband] > emin)))
self.ebands = self.ebands[n_emin : n_emax + 1]
if isinstance(self.proj, np.ndarray):
self.proj = self.proj[:, n_emin : n_emax + 1, :, :]
if self.mommat is not None:
self.mommat = self.mommat[:, n_emin : n_emax + 1, :]
# Removing bands may change the number of valence electrons
if self.nelect is not None:
self.nelect -= self.dosweight * n_emin
return n_emin, n_emax
def get_volume(self):
"""Volume of cell."""
try:
return self.UCvol
except AttributeError:
latt_vec = self.get_lattvec()
self.UCvol = np.abs(np.linalg.det(latt_vec))
return self.UCvol
class BztInterpolator:
"""Interpolate the dft band structures."""
def __init__(
self,
data,
lpfac=10,
energy_range=1.5,
curvature=True,
save_bztInterp=False,
load_bztInterp=False,
save_bands=False,
fname="bztInterp.json.gz",
) -> None:
"""
Args:
data: A loader
lpfac: the number of interpolation points in the real space. By
default 10 gives 10 time more points in the real space than
the number of kpoints given in reciprocal space.
energy_range: usually the interpolation is not needed on the entire energy
range but on a specific range around the Fermi level.
This energy in eV fix the range around the Fermi level
(E_fermi-energy_range,E_fermi+energy_range) of
bands that will be interpolated
and taken into account to calculate the transport properties.
curvature: boolean value to enable/disable the calculation of second
derivative related transport properties (Hall coefficient).
save_bztInterp: Default False. If True coefficients and equivalences are
saved in fname file.
load_bztInterp: Default False. If True the coefficients and equivalences
are loaded from fname file, not calculated. It can be faster than
re-calculate them in some cases.
save_bands: Default False. If True interpolated bands are also stored.
It can be slower than interpolate them. Not recommended.
fname: File path where to store/load from the coefficients and equivalences.
Example:
data = VasprunLoader().from_file('vasprun.xml')
bztInterp = BztInterpolator(data)
"""
bands_loaded = False
self.data = data
num_kpts = self.data.kpoints.shape[0]
self.efermi = self.data.fermi
middle_gap_en = (self.data.cbm + self.data.vbm) / 2
self.accepted = self.data.bandana(
emin=(middle_gap_en - energy_range) * units.eV,
emax=(middle_gap_en + energy_range) * units.eV,
)
if load_bztInterp:
bands_loaded = self.load(fname)
else:
self.equivalences = sphere.get_equivalences(self.data.atoms, self.data.magmom, num_kpts * lpfac)
self.coeffs = fite.fitde3D(self.data, self.equivalences)
if not bands_loaded:
self.eband, self.vvband, self.cband = fite.getBTPbands(
self.equivalences, self.coeffs, self.data.lattvec, curvature=curvature
)
if save_bztInterp:
self.save(fname, save_bands)
def load(self, fname="bztInterp.json.gz"):
"""Load the coefficient, equivalences, bands from fname."""
d = loadfn(fname)
if len(d) > 2:
self.equivalences, coeffs, self.eband, self.vvband, self.cband = d
bands_loaded = True
elif len(d) == 2:
self.equivalences, coeffs = loadfn(fname)
bands_loaded = False
else:
raise BoltztrapError("Something wrong reading the data file!")
self.coeffs = coeffs[0] + coeffs[1] * 1j
return bands_loaded
def save(self, fname="bztInterp.json.gz", bands=False) -> None:
"""Save the coefficient, equivalences to fname.
If bands is True, also interpolated bands are stored.
"""
if bands:
dumpfn(
[
self.equivalences,
[self.coeffs.real, self.coeffs.imag],
self.eband,
self.vvband,
self.cband,
],
fname,
)
else:
dumpfn([self.equivalences, [self.coeffs.real, self.coeffs.imag]], fname)
def get_band_structure(self, kpaths=None, kpoints_lbls_dict=None, density=20):
"""Return a BandStructureSymmLine object interpolating bands along a
High symmetry path calculated from the structure using HighSymmKpath
function. If kpaths and kpoints_lbls_dict are provided, a custom
path is interpolated.
kpaths: List of lists of following kpoints labels defining
the segments of the path. E.g. [['L','M'],['L','X']]
kpoints_lbls_dict: Dict where keys are the kpoint labels used in kpaths
and values are their fractional coordinates.
E.g. {'L':np.array(0.5,0.5,0.5)},
'M':np.array(0.5,0.,0.5),
'X':np.array(0.5,0.5,0.)}
density: Number of points in each segment.
"""
if isinstance(kpaths, list) and isinstance(kpoints_lbls_dict, dict):
kpoints = []
for kpath in kpaths:
for idx, k_pt in enumerate(kpath[:-1]):
sta = kpoints_lbls_dict[k_pt]
end = kpoints_lbls_dict[kpath[idx + 1]]
kpoints.append(np.linspace(sta, end, density))
kpoints = np.concatenate(kpoints)
else:
kpath = HighSymmKpath(self.data.structure)
kpoints = np.vstack(kpath.get_kpoints(density, coords_are_cartesian=False)[0])
kpoints_lbls_dict = kpath.kpath["kpoints"]
lattvec = self.data.get_lattvec()
egrid, _vgrid = fite.getBands(kpoints, self.equivalences, lattvec, self.coeffs)
if self.data.is_spin_polarized:
h = sum(np.array_split(self.accepted, 2)[0])
egrid = np.array_split(egrid, [h], axis=0)
bands_dict = {
Spin.up: (egrid[0] / units.eV),
Spin.down: (egrid[1] / units.eV),
}
else:
bands_dict = {Spin.up: (egrid / units.eV)}
return BandStructureSymmLine(
kpoints,
bands_dict,
self.data.structure.lattice.reciprocal_lattice,
self.efermi / units.eV,
labels_dict=kpoints_lbls_dict,
)
def get_dos(self, partial_dos=False, npts_mu=10000, T=None, progress=False):
"""Return a Dos object interpolating bands.
Args:
partial_dos: if True, projections will be interpolated as well
and partial doses will be return. Projections must be available
in the loader.
npts_mu: number of energy points of the Dos
T: parameter used to smooth the Dos
progress: Default False, If True a progress bar is shown when
partial dos are computed.
"""
dos_dict = {}
enr = (self.eband.min(), self.eband.max())
if self.data.is_spin_polarized:
h = sum(np.array_split(self.accepted, 2)[0])
eband_ud = np.array_split(self.eband, [h], axis=0)
vvband_ud = np.array_split(self.vvband, [h], axis=0)
spins = [Spin.up, Spin.down]
else:
eband_ud = [self.eband]
vvband_ud = [self.vvband]
spins = [Spin.up]
for spin, eb, vvb in zip(spins, eband_ud, vvband_ud):
energies, densities, _vvdos, _cdos = BL.BTPDOS(eb, vvb, npts=npts_mu, erange=enr)
if T:
densities = BL.smoothen_DOS(energies, densities, T)
dos_dict.setdefault(spin, densities)
tdos = Dos(self.efermi / units.eV, energies / units.eV, dos_dict)
if partial_dos:
tdos = self.get_partial_doses(tdos, eband_ud, spins, enr, npts_mu, T, progress)
return tdos
def get_partial_doses(self, tdos, eband_ud, spins, enr, npts_mu, T, progress):
"""Return a CompleteDos object interpolating the projections.
tdos: total dos previously calculated
npts_mu: number of energy points of the Dos
T: parameter used to smooth the Dos
progress: Default False, If True a progress bar is shown.
"""
if not self.data.proj:
raise BoltztrapError("No projections loaded.")
bkp_data_ebands = np.copy(self.data.ebands)
pdoss = {}
if progress:
n_iter = np.prod(np.sum([np.array(i.shape)[2:] for i in self.data.proj.values()]))
t = tqdm(total=n_iter * 2)
for spin, eb in zip(spins, eband_ud):
for idx, site in enumerate(self.data.structure):
if site not in pdoss:
pdoss[site] = {}
for iorb, orb in enumerate(Orbital):
if progress:
t.update()
if iorb == self.data.proj[spin].shape[-1]:
break
if orb not in pdoss[site]:
pdoss[site][orb] = {}
self.data.ebands = self.data.proj[spin][:, :, idx, iorb].T
coeffs = fite.fitde3D(self.data, self.equivalences)
proj, _vvproj, _cproj = fite.getBTPbands(self.equivalences, coeffs, self.data.lattvec)
edos, pdos = BL.DOS(eb, npts=npts_mu, weights=np.abs(proj.real), erange=enr)
if T:
pdos = BL.smoothen_DOS(edos, pdos, T)
pdoss[site][orb][spin] = pdos
self.data.ebands = bkp_data_ebands
return CompleteDos(self.data.structure, total_dos=tdos, pdoss=pdoss)
class BztTransportProperties:
"""Compute Seebeck, Conductivity, Electrical part of thermal conductivity
and Hall coefficient, conductivity effective mass, Power Factor tensors
w.r.t. the chemical potential and temperatures, from dft band structure via
interpolation.
"""
def __init__(
self,
BztInterpolator,
temp_r=None,
doping=None,
npts_mu=4000,
CRTA=1e-14,
margin=None,
save_bztTranspProps=False,
load_bztTranspProps=False,
fname="bztTranspProps.json.gz",
) -> None:
"""
Args:
BztInterpolator: a BztInterpolator previously generated
temp_r: numpy array of temperatures at which to calculate transport properties
doping: doping levels at which to calculate transport properties. If provided,
transport properties w.r.t. these doping levels are also computed. See
compute_properties_doping() method for details.
npts_mu: number of energy points at which to calculate transport properties
CRTA: constant value of the relaxation time
margin: The energy range of the interpolation is extended by this value on both sides.
Defaults to 9 * units.BOLTZMANN * temp_r.max().
save_bztTranspProps: Default False. If True all computed transport properties
will be stored in fname file.
load_bztTranspProps: Default False. If True all computed transport properties
will be loaded from fname file.
fname: File path where to save/load transport properties.
Upon creation, it contains properties tensors w.r.t. the chemical potential
of size (len(temp_r),npts_mu,3,3):
Conductivity_mu (S/m), Seebeck_mu (microV/K), Kappa_mu (W/(m*K)),
Power_Factor_mu (milliW/K m);
cond_Effective_mass_mu (m_e) calculated as Ref.
Also:
Carrier_conc_mu: carrier concentration of size (len(temp_r),npts_mu)
Hall_carrier_conc_trace_mu: trace of Hall carrier concentration of size
(len(temp_r),npts_mu)
mu_r_eV: array of energies in eV and with E_fermi at 0.0
where all the properties are calculated.
Example:
bztTransp = BztTransportProperties(bztInterp,temp_r = np.arange(100,1400,100))
"""
if temp_r is None:
temp_r = np.arange(100, 1400, 100)
self.dosweight = BztInterpolator.data.dosweight
self.volume = BztInterpolator.data.get_volume()
self.nelect = BztInterpolator.data.nelect
self.efermi = BztInterpolator.data.fermi / units.eV
if margin is None:
margin = 9 * units.BOLTZMANN * temp_r.max()
if load_bztTranspProps:
self.load(fname)
else:
self.CRTA = CRTA
self.temp_r = temp_r
self.doping = doping
self.epsilon, self.dos, self.vvdos, self.cdos = BL.BTPDOS(
BztInterpolator.eband,
BztInterpolator.vvband,
npts=npts_mu,
cband=BztInterpolator.cband,
)
mur_indices = np.logical_and(
self.epsilon > self.epsilon.min() + margin,
self.epsilon < self.epsilon.max() - margin,
)
self.mu_r = self.epsilon[mur_indices] # mu range
self.mu_r_eV = self.mu_r / units.eV - self.efermi
N, L0, L1, L2, Lm11 = BL.fermiintegrals(
self.epsilon,
self.dos,
self.vvdos,
mur=self.mu_r,
Tr=temp_r,
dosweight=self.dosweight,
cdos=self.cdos,
)
# Compute the Onsager coefficients from those Fermi integrals
self.Conductivity_mu, self.Seebeck_mu, self.Kappa_mu, Hall_mu = BL.calc_Onsager_coefficients(
L0, L1, L2, self.mu_r, temp_r, self.volume, Lm11=Lm11
)
# Common properties rescaling
self.Conductivity_mu *= CRTA # S / m
self.Seebeck_mu *= 1e6 # microvolt / K
self.Kappa_mu *= CRTA # W / (m K)
self.Hall_carrier_conc_trace_mu = (
units.Coulomb
* 1e-6
/ (np.abs(Hall_mu[:, :, 0, 1, 2] + Hall_mu[:, :, 2, 0, 1] + Hall_mu[:, :, 1, 2, 0]) / 3)
)
self.Carrier_conc_mu = (N + self.nelect) / (self.volume / (units.Meter / 100.0) ** 3)
# Derived properties
cond_eff_mass = np.zeros((len(self.temp_r), len(self.mu_r), 3, 3))
for temp in range(len(self.temp_r)):
for i in range(len(self.mu_r)):
try:
cond_eff_mass[temp, i] = (
np.linalg.inv(self.Conductivity_mu[temp, i])
* self.Carrier_conc_mu[temp, i]
* units.qe_SI**2
/ units.me_SI
* 1e6
)
except np.linalg.LinAlgError:
pass
self.Effective_mass_mu = cond_eff_mass * CRTA
self.Power_Factor_mu = (self.Seebeck_mu @ self.Seebeck_mu) @ self.Conductivity_mu
self.Power_Factor_mu *= 1e-9 # milliWatt / m / K**2
self.contain_props_doping = False
if isinstance(doping, np.ndarray):
self.compute_properties_doping(doping, temp_r)
if save_bztTranspProps:
self.save(fname)
def compute_properties_doping(self, doping, temp_r=None) -> None:
"""Calculate all the properties w.r.t. the doping levels in input.
Args:
doping: numpy array specifying the doping levels
temp_r: numpy array specifying the temperatures
When executed, it add the following variable at the BztTransportProperties
object:
Conductivity_doping, Seebeck_doping, Kappa_doping, Power_Factor_doping,
cond_Effective_mass_doping are dictionaries with 'n' and 'p' keys and
arrays of dim (len(temp_r),len(doping),3,3) as values.
Carriers_conc_doping: carriers concentration for each doping level and T.
mu_doping_eV: the chemical potential correspondent to each doping level.
"""
if temp_r is None:
temp_r = self.temp_r
self.Conductivity_doping, self.Seebeck_doping, self.Kappa_doping, self.Carriers_conc_doping = {}, {}, {}, {}
self.Power_Factor_doping, self.Effective_mass_doping = {}, {}
mu_doping = {}
doping_carriers = [dop * (self.volume / (units.Meter / 100.0) ** 3) for dop in doping]
for dop_type in ["n", "p"]:
sbk = np.zeros((len(temp_r), len(doping), 3, 3))
cond = np.zeros((len(temp_r), len(doping), 3, 3))
kappa = np.zeros((len(temp_r), len(doping), 3, 3))
hall = np.zeros((len(temp_r), len(doping), 3, 3, 3))
dc = np.zeros((len(temp_r), len(doping)))
if dop_type == "p":
doping_carriers = [-dop for dop in doping_carriers]
mu_doping[dop_type] = np.zeros((len(temp_r), len(doping)))
for idx_t, temp in enumerate(temp_r):
for idx_d, dop_car in enumerate(doping_carriers):
mu_doping[dop_type][idx_t, idx_d] = BL.solve_for_mu(
self.epsilon,
self.dos,
self.nelect + dop_car,
temp,
self.dosweight,
True, # noqa: FBT003
False, # noqa: FBT003
)
N, L0, L1, L2, Lm11 = BL.fermiintegrals(
self.epsilon,
self.dos,
self.vvdos,
mur=mu_doping[dop_type][idx_t],
Tr=np.array([temp]),
dosweight=self.dosweight,
)
cond[idx_t], sbk[idx_t], kappa[idx_t], hall[idx_t] = BL.calc_Onsager_coefficients(
L0, L1, L2, mu_doping[dop_type][idx_t], np.array([temp]), self.volume, Lm11
)
dc[idx_t] = self.nelect + N
self.Conductivity_doping[dop_type] = cond * self.CRTA # S / m
self.Seebeck_doping[dop_type] = sbk * 1e6 # microVolt / K
self.Kappa_doping[dop_type] = kappa * self.CRTA # W / (m K)
# self.Hall_doping[dop_type] = hall
self.Carriers_conc_doping[dop_type] = dc / (self.volume / (units.Meter / 100.0) ** 3)
self.Power_Factor_doping[dop_type] = (sbk @ sbk) @ cond * self.CRTA * 1e3
cond_eff_mass = np.zeros((len(temp_r), len(doping), 3, 3))
for idx_t in range(len(temp_r)):
for idx_d, dop in enumerate(doping):
try:
cond_eff_mass[idx_t, idx_d] = (
np.linalg.inv(cond[idx_t, idx_d]) * dop * units.qe_SI**2 / units.me_SI * 1e6
)
except np.linalg.LinAlgError:
pass
self.Effective_mass_doping[dop_type] = cond_eff_mass
self.doping = doping
self.mu_doping = mu_doping
self.mu_doping_eV = {k: v / units.eV - self.efermi for k, v in mu_doping.items()}
self.contain_props_doping = True
# def find_mu_doping(self, epsilon, dos, N0, T, dosweight=2.):
# """
# Find the mu.
# :param epsilon:
# :param dos:
# :param N0:
# :param T:
# :param dosweight:
# """
# delta = np.empty_like(epsilon)
# for i, e in enumerate(epsilon):
# delta[i] = BL.calc_N(epsilon, dos, e, T, dosweight) + N0
# delta = np.abs(delta)
# # Find the position optimizing this distance
# pos = np.abs(delta).argmin()
# return epsilon[pos]
def save(self, fname="bztTranspProps.json.gz") -> None:
"""Save the transport properties to fname file."""
lst_props = [
self.temp_r,
self.CRTA,
self.epsilon,
self.dos,
self.vvdos,
self.cdos,
self.mu_r,
self.mu_r_eV,
self.Conductivity_mu,
self.Seebeck_mu,
self.Kappa_mu,
self.Carrier_conc_mu,
self.Hall_carrier_conc_trace_mu,
self.Power_Factor_mu,
self.Effective_mass_mu,
]
if self.contain_props_doping:
props = [
self.Conductivity_doping,
self.Seebeck_doping,
self.Kappa_doping,
self.Power_Factor_doping,
self.Effective_mass_doping,
self.Carriers_conc_doping,
self.doping,
self.mu_doping,
self.mu_doping_eV,
]
lst_props.extend(props)
dumpfn(lst_props, fname)
def load(self, fname="bztTranspProps.json.gz") -> bool:
"""Load the transport properties from fname file."""
lst = loadfn(fname)
(
self.temp_r,
self.CRTA,
self.epsilon,
self.dos,
self.vvdos,
self.cdos,
self.mu_r,
self.mu_r_eV,
self.Conductivity_mu,
self.Seebeck_mu,
self.Kappa_mu,
self.Carrier_conc_mu,
self.Hall_carrier_conc_trace_mu,
self.Power_Factor_mu,
self.Effective_mass_mu,
) = lst[:15]
if len(lst) > 15:
(
self.Conductivity_doping,
self.Seebeck_doping,
self.Kappa_doping,
self.Power_Factor_doping,
self.Effective_mass_doping,
self.Carriers_conc_doping,
self.doping,
self.mu_doping,
self.mu_doping_eV,
) = lst[15:]
self.contains_doping_props = True
return True
class BztPlotter:
"""Plotter to plot transport properties, interpolated bands along some high
symmetry k-path, and DOS.
Example:
bztPlotter = BztPlotter(bztTransp,bztInterp)
fig = self.bztPlotter.plot_props('S', 'mu', 'temp', temps=[300, 500])
fig.show()
"""
def __init__(self, bzt_transP=None, bzt_interp=None) -> None:
""":param bzt_transP:
:param bzt_interp:
"""
self.bzt_transP = bzt_transP
self.bzt_interp = bzt_interp
def plot_props(
self,
prop_y,
prop_x,
prop_z="temp",
output="avg_eigs",
dop_type="n",
doping=None,
temps=None,
xlim=(-2, 2),
ax: plt.Axes = None,
):
"""Function to plot the transport properties.
Args:
prop_y: property to plot among ("Conductivity","Seebeck","Kappa","Carrier_conc",
"Hall_carrier_conc_trace"). Abbreviations are possible, like "S" for "Seebeck"
prop_x: independent variable in the x-axis among ('mu','doping','temp')
prop_z: third variable to plot multiple curves ('doping','temp')
output: 'avg_eigs' to plot the average of the eigenvalues of the properties
tensors; 'eigs' to plot the three eigenvalues of the properties
tensors.
dop_type: 'n' or 'p' to specify the doping type in plots that use doping
levels as prop_x or prop_z
doping: list of doping level to plot, useful to reduce the number of curves
when prop_z='doping'
temps: list of temperatures to plot, useful to reduce the number of curves
when prop_z='temp'
xlim: chemical potential range in eV, useful when prop_x='mu'
ax: figure.axes where to plot. If None, a new figure is produced.
Example:
bztPlotter.plot_props('S','mu','temp',temps=[600,900,1200]).show()
more example are provided in the notebook
"How to use Boltztra2 interface.ipynb".
"""
props = (
"Conductivity",
"Seebeck",
"Kappa",
"Effective_mass",
"Power_Factor",