From e73b206c356f1b7a0b76b31d2dfad0422db6bee3 Mon Sep 17 00:00:00 2001 From: Bonan Zhu <33688599+zhubonan@users.noreply.github.com> Date: Wed, 28 Jun 2023 19:06:08 +0100 Subject: [PATCH] Fix bugs handling time-reversal symmetry (#25) 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. --- easyunfold/__init__.py | 2 +- easyunfold/cli.py | 5 +- easyunfold/unfold.py | 46 ++++++++++++++----- .../Si_super_deformed/KPOINTS_easyunfold | 34 +++++++------- .../Si_211_unfold/easyunfold_sc_kpoints.cell | 32 ++++++------- .../easyunfold_sc_kpoints.cell | 32 ++++++------- .../Si_super_deformed_soc/KPOINTS_easyunfold | 34 +++++++------- .../Si_super_deformed_spin/KPOINTS_easyunfold | 34 +++++++------- 8 files changed, 121 insertions(+), 98 deletions(-) diff --git a/easyunfold/__init__.py b/easyunfold/__init__.py index 616d3c1..2b88a02 100644 --- a/easyunfold/__init__.py +++ b/easyunfold/__init__.py @@ -2,4 +2,4 @@ Collection of code for band unfolding """ -__version__ = '0.1.4' +__version__ = '0.1.5' diff --git a/easyunfold/cli.py b/easyunfold/cli.py index 0d7220b..678d6cb 100644 --- a/easyunfold/cli.py +++ b/easyunfold/cli.py @@ -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}') diff --git a/easyunfold/unfold.py b/easyunfold/unfold.py index 2acf0c8..74fc4ef 100644 --- a/easyunfold/unfold.py +++ b/easyunfold/unfold.py @@ -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 @@ -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 @@ -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""" @@ -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 @@ -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: @@ -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$: @@ -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, :] diff --git a/tests/test_data/Si-project/Si_super_deformed/KPOINTS_easyunfold b/tests/test_data/Si-project/Si_super_deformed/KPOINTS_easyunfold index 20051de..4a155eb 100644 --- a/tests/test_data/Si-project/Si_super_deformed/KPOINTS_easyunfold +++ b/tests/test_data/Si-project/Si_super_deformed/KPOINTS_easyunfold @@ -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 diff --git a/tests/test_data/Si-project/Si_super_deformed_castep/Si_211_unfold/easyunfold_sc_kpoints.cell b/tests/test_data/Si-project/Si_super_deformed_castep/Si_211_unfold/easyunfold_sc_kpoints.cell index 9a628ee..ce1ca46 100644 --- a/tests/test_data/Si-project/Si_super_deformed_castep/Si_211_unfold/easyunfold_sc_kpoints.cell +++ b/tests/test_data/Si-project/Si_super_deformed_castep/Si_211_unfold/easyunfold_sc_kpoints.cell @@ -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 diff --git a/tests/test_data/Si-project/Si_super_deformed_castep/easyunfold_sc_kpoints.cell b/tests/test_data/Si-project/Si_super_deformed_castep/easyunfold_sc_kpoints.cell index 9a628ee..ce1ca46 100644 --- a/tests/test_data/Si-project/Si_super_deformed_castep/easyunfold_sc_kpoints.cell +++ b/tests/test_data/Si-project/Si_super_deformed_castep/easyunfold_sc_kpoints.cell @@ -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 diff --git a/tests/test_data/Si-project/Si_super_deformed_soc/KPOINTS_easyunfold b/tests/test_data/Si-project/Si_super_deformed_soc/KPOINTS_easyunfold index 20051de..4a155eb 100755 --- a/tests/test_data/Si-project/Si_super_deformed_soc/KPOINTS_easyunfold +++ b/tests/test_data/Si-project/Si_super_deformed_soc/KPOINTS_easyunfold @@ -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 diff --git a/tests/test_data/Si-project/Si_super_deformed_spin/KPOINTS_easyunfold b/tests/test_data/Si-project/Si_super_deformed_spin/KPOINTS_easyunfold index 20051de..4a155eb 100644 --- a/tests/test_data/Si-project/Si_super_deformed_spin/KPOINTS_easyunfold +++ b/tests/test_data/Si-project/Si_super_deformed_spin/KPOINTS_easyunfold @@ -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