Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For band structure, how to correctly display the symbol of the \Gamma point #4214

Closed
phyfang opened this issue Dec 3, 2024 · 13 comments · Fixed by materialsvirtuallab/monty#730 · May be fixed by #4218
Closed

For band structure, how to correctly display the symbol of the \Gamma point #4214

phyfang opened this issue Dec 3, 2024 · 13 comments · Fixed by materialsvirtuallab/monty#730 · May be fixed by #4218
Labels

Comments

@phyfang
Copy link

phyfang commented Dec 3, 2024

Python version

python--3.12

Pymatgen version

2024.11.13

Operating system version

Win-11

Current behavior

When I was drawing the band structure, I found some problems that the gamma points were not displayed correctly.
??

Expected Behavior

How should I solve this problem?

Minimal example

No response

Relevant files to reproduce this bug

No response

@phyfang phyfang added the bug label Dec 3, 2024
@QuantumChemist
Copy link
Contributor

Dear @phyfang , could you further describe how you try to plot the bandstructure?
Depending on what you do, it helps to put \Gamma in backticks (``) like `\Gamma` .

@DanielYang59
Copy link
Contributor

DanielYang59 commented Dec 3, 2024

I guess it's related to #4025 (looks like Gamma is not escaped properly somewhere). I might open a PR later and let's fix this together or you would fix this @QuantumChemist ?

@QuantumChemist
Copy link
Contributor

I guess it's related to #4025 (looks like Gamma is not escaped properly somewhere). I might open a PR later and let's fix this together or you would fix this @QuantumChemist ?

yeah, as you gave me access to your fork, let's fix this together. Or was that not intended? When it's just escaping the Gamma correctly, the PR shouldn't take long I guess.

@DanielYang59
Copy link
Contributor

DanielYang59 commented Dec 4, 2024

as you gave me access to your fork, let's fix this together. Or was that not intended?

It was 100% intended, free feel to commit to my other/future PRs as well :)

When it's just escaping the Gamma correctly, the PR shouldn't take long I guess.

Yep, I guess it wouldn't be a hard fix, but I would need to fix some data (perhaps from current tests) to recreate this as I don't have data at hand right now.


Update: I just tried the BS plotter at

def test_get_plot(self):
# zero_to_efermi = True, ylim = None, smooth = False,
# vbm_cbm_marker = False, smooth_tol = None
ax = self.plotter.get_plot()
assert ax.get_ylim() == (-4.0, 7.6348), "wrong ylim"
ax = self.plotter.get_plot(smooth=True)
ax = self.plotter.get_plot(vbm_cbm_marker=True)
self.plotter.save_plot(f"{self.tmp_path}/bsplot.png")
assert os.path.isfile(f"{self.tmp_path}/bsplot.png")
plt.close("all")
# test plotter with 2 bandstructures
ax = self.plotter_multi.get_plot()
assert len(ax.get_lines()) == 874, "wrong number of lines"
assert ax.get_ylim() == (-10.0, 10.0), "wrong ylim"
ax = self.plotter_multi.get_plot(zero_to_efermi=False)
assert ax.get_ylim() == (-15.2379, 12.67141266), "wrong ylim"
ax = self.plotter_multi.get_plot(smooth=True)
self.plotter_multi.save_plot(f"{self.tmp_path}/bsplot.png")
assert os.path.isfile(f"{self.tmp_path}/bsplot.png")
plt.close("all")

And it seems to work as expected.
bsplot

Also tried the BSDOS plotter at

def test_methods(self):
vasp_run = Vasprun(f"{VASP_OUT_DIR}/vasprun_Si_bands.xml.gz")
plotter = BSDOSPlotter()
band_struct = vasp_run.get_band_structure(kpoints_filename=f"{VASP_IN_DIR}/KPOINTS_Si_bands")
ax = plotter.get_plot(band_struct)
assert isinstance(ax, plt.Axes)
plt.close()
bs_ax, dos_ax = plotter.get_plot(band_struct, vasp_run.complete_dos)
assert isinstance(bs_ax, plt.Axes)
assert isinstance(dos_ax, plt.Axes)
plt.close("all")

And worked as expected as well.
image

Also tried the ipynb at https://github.com/materialsvirtuallab/matgenb/blob/master/notebooks/2017-09-03-Analyze%20and%20plot%20band%20structures.ipynb and not seeing any issue either (but the MPRest import seems broken, I would fix it)


I would have another try later back home with my Windows PC (not sure if this is a mpl backend issue in Windows or not) Update, tested on Windows and didn't see any issue for above code

@phyfang Yes it would really help us if you could provide the code snippet (and data if possible) to recreate this issue.

@phyfang
Copy link
Author

phyfang commented Dec 4, 2024

Dear @phyfang , could you further describe how you try to plot the bandstructure? Depending on what you do, it helps to put \Gamma in backticks (``) like \Gamma .

Thanks for your reply, I changed the 'GAMMA' in KPOINTS to '\Gamma' and it solved.

@DanielYang59
Copy link
Contributor

@phyfang If you don't mind, can you share the code you used to generate the plot?

@QuantumChemist
Copy link
Contributor

It was 100% intended, free feel to commit to my other/future PRs as well :)

Sure 🤗 let's see how and where I can help out.

Update: I just tried the BS plotter at

I also remembered that I didn't have a problem with the BSplotter before (and there were no recent changes I guess), as you can see at the end of the following page: https://autoatml.github.io/autoplex/user/phonon/flows/flows.html

@QuantumChemist
Copy link
Contributor

QuantumChemist commented Dec 4, 2024

@phyfang If you don't mind, can you share the code you used to generate the plot?

I'm not sure if phonopy uses the KPOINTS file, but probably in the KPOINTS file, instead of using the mesh, putting the kpoint path explicitly with labels for the critical points.

@phyfang
Copy link
Author

phyfang commented Dec 4, 2024

@phyfang If you don't mind, can you share the code you used to generate the plot?

ok

import matplotlib.pyplot as plt
from pymatgen.io.vasp.outputs import Vasprun
from pymatgen.electronic_structure.plotter import BSDOSPlotter

band_structure = Vasprun(r"vasprun.xml", parse_projected_eigen=True)
band_data = band_structure.get_band_structure(line_mode=True) 

dos_vasprun = Vasprun(r"E:\DFT\dos\vasprun.xml" )
dos_data = dos_vasprun.complete_dos

banddos_fig = BSDOSPlotter(bs_projection='elements', dos_projection='orbitals', vb_energy_range=0.5, fixed_cb_energy=False, cb_energy_range=0.5)
banddos_fig.get_plot(bs=band_data, dos=dos_data)
plt.savefig('banddos.png')

original KPOINTS

K-Path Generated by VASPKIT.
   40
Line-Mode
Reciprocal
   0.0000000000   0.0000000000   0.0000000000     Γ        
   0.5000000000   0.5000000000   0.0000000000     Y              
 
   0.5000000000   0.5000000000   0.0000000000     Y              
   0.5000000000   0.5000000000   0.5000000000     M              
 
   0.5000000000   0.5000000000   0.5000000000     M              
   0.0000000000   0.0000000000   0.5000000000     A              
 
   0.0000000000   0.0000000000   0.5000000000     A              
   0.0000000000   0.0000000000   0.0000000000     Γ          
 
   0.0000000000   0.5000000000   0.5000000000     L_2            
   0.0000000000   0.0000000000   0.0000000000     Γ          
 
   0.0000000000   0.0000000000   0.0000000000     Γ          
   0.0000000000   0.5000000000   0.0000000000     V_2 

changed

K-Path Generated by VASPKIT.
   40
Line-Mode
Reciprocal
   0.0000000000   0.0000000000   0.0000000000     \Gamma        
   0.5000000000   0.5000000000   0.0000000000     Y              
 
   0.5000000000   0.5000000000   0.0000000000     Y              
   0.5000000000   0.5000000000   0.5000000000     M              
 
   0.5000000000   0.5000000000   0.5000000000     M              
   0.0000000000   0.0000000000   0.5000000000     A              
 
   0.0000000000   0.0000000000   0.5000000000     A              
   0.0000000000   0.0000000000   0.0000000000     \Gamma          
 
   0.0000000000   0.5000000000   0.5000000000     L_2            
   0.0000000000   0.0000000000   0.0000000000     \Gamma          
 
   0.0000000000   0.0000000000   0.0000000000     \Gamma          
   0.0000000000   0.5000000000   0.0000000000     V_2

@QuantumChemist
Copy link
Contributor

Thank you a lot for sharing your code @phyfang 😃

@QuantumChemist
Copy link
Contributor

I think we can safely close this issue as no PR is needed.

@shyuep shyuep closed this as not planned Won't fix, can't repro, duplicate, stale Dec 4, 2024
@DanielYang59
Copy link
Contributor

DanielYang59 commented Dec 6, 2024

Hi @QuantumChemist sorry for the delay but I don't think that really resolves the issue, as VASP docs also use Γ for labeling Gamma point (as such we cannot ask users to overwrite all Γ with \Gamma).

As far as I know, this is an encoding issue in Windows (i.e. the encoding is not explicitly set in zopen) when parsing KPOINTS from file (as text) (looks like @phyfang's system might be using GB2312 which is the default encoding for Chinese):

with zopen(filename, mode="rt") as file:
return cls.from_str(file.read())

You could check the encoding with [System.Text.Encoding]::Default from PowerShell on Windows BTW.

I know you're working on Linux, and the default encoding on Windows is a bit complicated and dependent on locale encoding:

On Unix, return the encoding of the current LC_CTYPE locale. Return "utf-8" if nl_langinfo(CODESET) returns an empty string: for example, if the current LC_CTYPE locale is not supported.

On Windows, return the ANSI code page.

So in a word, that issue is not specific to plotter but more general to file parsing where the user's default encoding is not "UTF-8" with non-ASCII char present, I guess we still need to fix it.


Explanation moved to #4218 for visibility

@QuantumChemist
Copy link
Contributor

Hi @DanielYang59 , oh I see where the issue arises from then.
I have never seen Γ in a KPOINTS file before and myself always used \Gamma, so I was not even aware that this is possible at all. 🫨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment