Skip to content

Commit

Permalink
Fix bugs handling time-reversal symmetry (#25)
Browse files Browse the repository at this point in the history
There are two problems, first the reduced mapping must be generated
without time reversal symmetry, otherwise the k = K + G_0 does not
hold. Second, the C_k(G) = C_{-k}(-G) must to used to get the
correct spectral density when summing up coefficients.
  • Loading branch information
zhubonan authored Jun 28, 2023
1 parent e0c113b commit e73b206
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 98 deletions.
2 changes: 1 addition & 1 deletion easyunfold/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Collection of code for band unfolding
"""

__version__ = '0.1.4'
__version__ = '0.1.5'
5 changes: 3 additions & 2 deletions easyunfold/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ def unfold(ctx, data_file, mpl_style_file):
@click.pass_context
def unfold_status(ctx):
"""Print the status"""
from easyunfold.unfold import UnfoldKSet
from easyunfold.unfold import UnfoldKSet, reduce_kpoints

unfoldset: UnfoldKSet = ctx.obj['obj']
print_symmetry_data(unfoldset)
nkpts_sc = len(unfoldset.expansion_results['reduced_sckpts'])
nkpts_sc = len(reduce_kpoints(unfoldset.expansion_results['reduced_sckpts'], time_reversal=unfoldset.time_reversal)[0])
click.echo()
click.echo(f'No. of k points in the primitive cell : {unfoldset.nkpts_orig}')
click.echo(f'No. of supercell k points : {nkpts_sc}')
Expand Down
46 changes: 34 additions & 12 deletions easyunfold/unfold.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
############################################################
import re
from typing import Union, List, Tuple
from packaging import version

import numpy as np
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -162,8 +163,10 @@ def __init__(self,
self.dft_code = dft_code
if metadata is None:
metadata = {}
# Not loaded from a file so populate this field
if 'program_version' not in metadata:
metadata['program_version'] = __version__
self.metadata = metadata
self.metadata['program_version'] = __version__
self.transient_quantities = {}

# Transient properties
Expand All @@ -172,6 +175,19 @@ def __init__(self,
if self.expansion_results is None:
self.expand_pc_kpoints()

self.check_version()

def check_version(self):
"""Check the version of the program"""
vnow = version.parse(__version__)
vcreated = version.parse(self.metadata['program_version'])
if vnow > vcreated:
print(f'The data file was generated with easyunfold {vcreated}, current {vnow}.')
if vcreated == version.parse('0.1.4'):
if self.time_reversal:
print(('Version 0.1.4 is known to have a bug with supercell kpoints with time-reversal symmetry\n'
'Please regenerate the data file with a newer version.'))

@property
def is_calculated(self) -> bool:
"""Show the status of the work"""
Expand Down Expand Up @@ -290,7 +306,8 @@ def generate_sc_kpoints(self) -> None:
# We now have bunch of supercell kpoints for each set of expanded kpoints
# Try to find duplicated SC kpoints
all_sc = np.array(all_sc)
reduced_sckpts, _, sc_kpts_map = reduce_kpoints(all_sc, time_reversal=self.time_reversal)
# Do not use time-reversal symmetry for reduction here, as we need to keep k = K - G_0 valid!
reduced_sckpts, _, sc_kpts_map = reduce_kpoints(all_sc, time_reversal=False)
sc_kpts_map = list(sc_kpts_map)

# Mapping between the pckpts to the reduced sckpts
Expand Down Expand Up @@ -321,6 +338,9 @@ def write_sc_kpoints(self,
if self.expansion_results.get('reduced_sckpts') is None:
self.generate_sc_kpoints()
kpoints = np.asarray(self.expansion_results['reduced_sckpts'])
# Reduce the number of supercell kpoint via time-reversal symmetry
if self.time_reversal:
kpoints = reduce_kpoints(kpoints, time_reversal=self.time_reversal)[0]
weights = None
if nk_per_split is None:
if scf_kpoints_and_weights:
Expand Down Expand Up @@ -822,19 +842,13 @@ def find_K_index(self, K0: np.ndarray) -> int:
K0_wrapped = wrap_kpoints(K0)
for ii in range(self.wfc.nkpts):
if np.alltrue(np.abs(kpts_wrapped[ii] - K0_wrapped) < 1E-5):
return ii + 1
return ii + 1, False
# Check for kpoint related with time-reversal symmetry
if self.time_reversal and np.alltrue(np.abs(kpts_wrapped[ii] + K0_wrapped) < 1E-5):
return ii + 1
# This actually returns the index of -K0
return ii + 1, True
raise ValueError('Cannot find the corresponding K-points in WAVECAR!')

def k2K_map(self, kpath) -> List[int]:
"""
Return the map from primitive-cell k-points to supercell k-points.
"""

return [self.find_K_index(find_K_from_k(k, self.M)[0]) - 1 for k in kpath]

def spectral_weight_k(self, k0, whichspin=1):
r"""
Spectral weight for a given $k$:
Expand All @@ -857,10 +871,18 @@ def spectral_weight_k(self, k0, whichspin=1):
# k0 = G0 + K0
K0, G0 = find_K_from_k(k0, self.M)
# find index of K0
ikpt = self.find_K_index(K0)
ikpt, time_reversal = self.find_K_index(K0)

# get the overlap G-vectors
Gvalid, Gall = self.get_ovlap_G(ikpt=ikpt)

# If this kpoint is actuall -K0, use the relationship C_{k}(G) = C_{-k}*(-G)
# Since the coefficients are on a grid, we can just inverse the G vectors (unit of reciprocal lattice vector).
# There is no need to take conjugate as we only care about the norm.
if time_reversal:
Gvalid = -Gvalid
Gall = -Gall

# Gnew = Gvalid + k0 - K0
Goffset = Gvalid + G0[np.newaxis, :]

Expand Down
34 changes: 17 additions & 17 deletions tests/test_data/Si-project/Si_super_deformed/KPOINTS_easyunfold
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
supercell kpoints
25
Rec
0.00000000 0.00000000 0.00000000 1.0
-0.50000000 0.25000000 0.25000000 1.0
0.00000000 0.25000000 0.00000000 1.0
-0.50000000 -0.50000000 0.25000000 1.0
-0.50000000 -0.12500000 0.37500000 1.0
-0.50000000 0.00000000 0.00000000 1.0
0.00000000 -0.50000000 -0.50000000 1.0
0.00000000 -0.50000000 0.00000000 1.0
0.00000000 0.37500000 -0.37500000 1.0
-0.50000000 0.25000000 0.25000000 1.0
-0.33333333 0.00000000 0.33333333 1.0
-0.25000000 -0.50000000 0.12500000 1.0
-0.50000000 -0.12500000 0.37500000 1.0
-0.25000000 -0.50000000 0.37500000 1.0
-0.25000000 -0.37500000 -0.50000000 1.0
-0.25000000 -0.25000000 -0.12500000 1.0
0.00000000 -0.50000000 -0.50000000 1.0
0.00000000 -0.50000000 0.00000000 1.0
0.00000000 0.00000000 -0.50000000 1.0
0.00000000 0.00000000 0.00000000 1.0
0.00000000 0.12500000 -0.37500000 1.0
0.00000000 0.12500000 -0.12500000 1.0
0.00000000 0.16666667 0.16666667 1.0
0.00000000 0.25000000 -0.25000000 1.0
0.00000000 0.25000000 0.00000000 1.0
0.00000000 0.33333333 0.33333333 1.0
0.00000000 0.37500000 -0.37500000 1.0
0.25000000 -0.50000000 0.37500000 1.0
0.25000000 0.25000000 -0.37500000 1.0
-0.25000000 -0.37500000 -0.50000000 1.0
0.25000000 -0.12500000 -0.50000000 1.0
0.00000000 0.25000000 -0.25000000 1.0
-0.50000000 -0.50000000 0.25000000 1.0
0.00000000 0.12500000 -0.37500000 1.0
-0.25000000 -0.50000000 0.37500000 1.0
0.25000000 0.12500000 -0.50000000 1.0
0.00000000 0.00000000 -0.50000000 1.0
-0.33333333 0.00000000 0.33333333 1.0
0.00000000 0.33333333 0.33333333 1.0
0.25000000 0.25000000 -0.37500000 1.0
0.33333333 0.00000000 0.16666667 1.0
0.00000000 0.16666667 0.16666667 1.0
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ Si 2.734364 2.734364 2.734364
%ENDBLOCK positions_abs

%BLOCK SPECTRAL_KPOINTS_LIST
-0.5000000000 0.0000000000 0.0000000000
-0.5000000000 0.2500000000 0.2500000000
-0.4000000000 0.0000000000 0.0000000000
-0.4000000000 0.3000000000 0.3000000000
-0.3000000000 0.0000000000 0.0000000000
-0.3000000000 0.3500000000 0.3500000000
-0.2000000000 0.0000000000 0.0000000000
-0.2000000000 0.4000000000 0.4000000000
-0.1000000000 0.0000000000 0.0000000000
-0.1000000000 0.4500000000 0.4500000000
0.0000000000 -0.5000000000 -0.5000000000
0.0000000000 -0.5000000000 0.0000000000
0.0000000000 0.0000000000 0.0000000000
0.1000000000 0.0000000000 0.0000000000
0.1000000000 0.0500000000 0.0500000000
0.0000000000 0.0500000000 0.0000000000
0.2000000000 0.0000000000 0.0000000000
0.2000000000 0.1000000000 0.1000000000
0.0000000000 0.1000000000 0.0000000000
0.3000000000 0.0000000000 0.0000000000
0.3000000000 0.1500000000 0.1500000000
0.0000000000 0.1500000000 0.0000000000
0.4000000000 0.0000000000 0.0000000000
0.4000000000 0.2000000000 0.2000000000
0.0000000000 0.2000000000 0.0000000000
-0.5000000000 0.0000000000 0.0000000000
-0.5000000000 0.2500000000 0.2500000000
0.0000000000 0.2500000000 0.0000000000
-0.4000000000 0.3000000000 0.3000000000
0.0000000000 0.3000000000 0.0000000000
-0.3000000000 0.3500000000 0.3500000000
0.0000000000 0.3500000000 0.0000000000
-0.2000000000 0.4000000000 0.4000000000
0.0000000000 0.4000000000 0.0000000000
-0.1000000000 0.4500000000 0.4500000000
0.0000000000 0.4500000000 0.0000000000
0.0000000000 -0.5000000000 -0.5000000000
0.0000000000 -0.5000000000 0.0000000000
0.1000000000 0.0500000000 0.0500000000
0.2000000000 0.1000000000 0.1000000000
0.3000000000 0.1500000000 0.1500000000
0.4000000000 0.2000000000 0.2000000000
%ENDBLOCK SPECTRAL_KPOINTS_LIST
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ Si 2.734364 2.734364 2.734364
%ENDBLOCK positions_abs

%BLOCK SPECTRAL_KPOINTS_LIST
-0.5000000000 0.0000000000 0.0000000000
-0.5000000000 0.2500000000 0.2500000000
-0.4000000000 0.0000000000 0.0000000000
-0.4000000000 0.3000000000 0.3000000000
-0.3000000000 0.0000000000 0.0000000000
-0.3000000000 0.3500000000 0.3500000000
-0.2000000000 0.0000000000 0.0000000000
-0.2000000000 0.4000000000 0.4000000000
-0.1000000000 0.0000000000 0.0000000000
-0.1000000000 0.4500000000 0.4500000000
0.0000000000 -0.5000000000 -0.5000000000
0.0000000000 -0.5000000000 0.0000000000
0.0000000000 0.0000000000 0.0000000000
0.1000000000 0.0000000000 0.0000000000
0.1000000000 0.0500000000 0.0500000000
0.0000000000 0.0500000000 0.0000000000
0.2000000000 0.0000000000 0.0000000000
0.2000000000 0.1000000000 0.1000000000
0.0000000000 0.1000000000 0.0000000000
0.3000000000 0.0000000000 0.0000000000
0.3000000000 0.1500000000 0.1500000000
0.0000000000 0.1500000000 0.0000000000
0.4000000000 0.0000000000 0.0000000000
0.4000000000 0.2000000000 0.2000000000
0.0000000000 0.2000000000 0.0000000000
-0.5000000000 0.0000000000 0.0000000000
-0.5000000000 0.2500000000 0.2500000000
0.0000000000 0.2500000000 0.0000000000
-0.4000000000 0.3000000000 0.3000000000
0.0000000000 0.3000000000 0.0000000000
-0.3000000000 0.3500000000 0.3500000000
0.0000000000 0.3500000000 0.0000000000
-0.2000000000 0.4000000000 0.4000000000
0.0000000000 0.4000000000 0.0000000000
-0.1000000000 0.4500000000 0.4500000000
0.0000000000 0.4500000000 0.0000000000
0.0000000000 -0.5000000000 -0.5000000000
0.0000000000 -0.5000000000 0.0000000000
0.1000000000 0.0500000000 0.0500000000
0.2000000000 0.1000000000 0.1000000000
0.3000000000 0.1500000000 0.1500000000
0.4000000000 0.2000000000 0.2000000000
%ENDBLOCK SPECTRAL_KPOINTS_LIST
34 changes: 17 additions & 17 deletions tests/test_data/Si-project/Si_super_deformed_soc/KPOINTS_easyunfold
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
supercell kpoints
25
Rec
0.00000000 0.00000000 0.00000000 1.0
-0.50000000 0.25000000 0.25000000 1.0
0.00000000 0.25000000 0.00000000 1.0
-0.50000000 -0.50000000 0.25000000 1.0
-0.50000000 -0.12500000 0.37500000 1.0
-0.50000000 0.00000000 0.00000000 1.0
0.00000000 -0.50000000 -0.50000000 1.0
0.00000000 -0.50000000 0.00000000 1.0
0.00000000 0.37500000 -0.37500000 1.0
-0.50000000 0.25000000 0.25000000 1.0
-0.33333333 0.00000000 0.33333333 1.0
-0.25000000 -0.50000000 0.12500000 1.0
-0.50000000 -0.12500000 0.37500000 1.0
-0.25000000 -0.50000000 0.37500000 1.0
-0.25000000 -0.37500000 -0.50000000 1.0
-0.25000000 -0.25000000 -0.12500000 1.0
0.00000000 -0.50000000 -0.50000000 1.0
0.00000000 -0.50000000 0.00000000 1.0
0.00000000 0.00000000 -0.50000000 1.0
0.00000000 0.00000000 0.00000000 1.0
0.00000000 0.12500000 -0.37500000 1.0
0.00000000 0.12500000 -0.12500000 1.0
0.00000000 0.16666667 0.16666667 1.0
0.00000000 0.25000000 -0.25000000 1.0
0.00000000 0.25000000 0.00000000 1.0
0.00000000 0.33333333 0.33333333 1.0
0.00000000 0.37500000 -0.37500000 1.0
0.25000000 -0.50000000 0.37500000 1.0
0.25000000 0.25000000 -0.37500000 1.0
-0.25000000 -0.37500000 -0.50000000 1.0
0.25000000 -0.12500000 -0.50000000 1.0
0.00000000 0.25000000 -0.25000000 1.0
-0.50000000 -0.50000000 0.25000000 1.0
0.00000000 0.12500000 -0.37500000 1.0
-0.25000000 -0.50000000 0.37500000 1.0
0.25000000 0.12500000 -0.50000000 1.0
0.00000000 0.00000000 -0.50000000 1.0
-0.33333333 0.00000000 0.33333333 1.0
0.00000000 0.33333333 0.33333333 1.0
0.25000000 0.25000000 -0.37500000 1.0
0.33333333 0.00000000 0.16666667 1.0
0.00000000 0.16666667 0.16666667 1.0
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
supercell kpoints
25
Rec
0.00000000 0.00000000 0.00000000 1.0
-0.50000000 0.25000000 0.25000000 1.0
0.00000000 0.25000000 0.00000000 1.0
-0.50000000 -0.50000000 0.25000000 1.0
-0.50000000 -0.12500000 0.37500000 1.0
-0.50000000 0.00000000 0.00000000 1.0
0.00000000 -0.50000000 -0.50000000 1.0
0.00000000 -0.50000000 0.00000000 1.0
0.00000000 0.37500000 -0.37500000 1.0
-0.50000000 0.25000000 0.25000000 1.0
-0.33333333 0.00000000 0.33333333 1.0
-0.25000000 -0.50000000 0.12500000 1.0
-0.50000000 -0.12500000 0.37500000 1.0
-0.25000000 -0.50000000 0.37500000 1.0
-0.25000000 -0.37500000 -0.50000000 1.0
-0.25000000 -0.25000000 -0.12500000 1.0
0.00000000 -0.50000000 -0.50000000 1.0
0.00000000 -0.50000000 0.00000000 1.0
0.00000000 0.00000000 -0.50000000 1.0
0.00000000 0.00000000 0.00000000 1.0
0.00000000 0.12500000 -0.37500000 1.0
0.00000000 0.12500000 -0.12500000 1.0
0.00000000 0.16666667 0.16666667 1.0
0.00000000 0.25000000 -0.25000000 1.0
0.00000000 0.25000000 0.00000000 1.0
0.00000000 0.33333333 0.33333333 1.0
0.00000000 0.37500000 -0.37500000 1.0
0.25000000 -0.50000000 0.37500000 1.0
0.25000000 0.25000000 -0.37500000 1.0
-0.25000000 -0.37500000 -0.50000000 1.0
0.25000000 -0.12500000 -0.50000000 1.0
0.00000000 0.25000000 -0.25000000 1.0
-0.50000000 -0.50000000 0.25000000 1.0
0.00000000 0.12500000 -0.37500000 1.0
-0.25000000 -0.50000000 0.37500000 1.0
0.25000000 0.12500000 -0.50000000 1.0
0.00000000 0.00000000 -0.50000000 1.0
-0.33333333 0.00000000 0.33333333 1.0
0.00000000 0.33333333 0.33333333 1.0
0.25000000 0.25000000 -0.37500000 1.0
0.33333333 0.00000000 0.16666667 1.0
0.00000000 0.16666667 0.16666667 1.0

0 comments on commit e73b206

Please sign in to comment.