materialsproject / pymatgen

Python Materials Genomics (pymatgen) is a robust materials analysis code that defines classes for structures and molecules with support for many electronic structure codes. It powers the Materials Project.
https://pymatgen.org
Other
1.52k stars 867 forks source link

Fix: BSPlotterProjected._Orbitals_SumOrbitals() method #4167

Open ShengLin1001 opened 1 week ago

ShengLin1001 commented 1 week ago

Fix: Ensure BSPlotterProjected._Orbitals_SumOrbitals() checks if sum_morbs is not None, as a NoneType object cannot be iterated.

Summary

Major changes:

Description

When running the following code, I encountered an error in the get_projected_plots_dots_patom_pmorb() method, which uses _Orbitals_SumOrbitals(). The error occurred because sum_morbs was None, making it non-iterable.

This fix addresses the issue by ensuring that sum_morbs is checked to confirm it is not None before any iteration, especially when plotting a single orbital. The order of statements has been adjusted accordingly.

Example Code to Reproduce:

from pymatgen.electronic_structure.plotter import DosPlotter, BSPlotter, BSPlotterProjected, BSDOSPlotter
from pymatgen.io.vasp import BSVasprun, Vasprun

v = BSVasprun("./bs/vasprun.xml", parse_projected_eigen=True)
bs = v.get_band_structure(kpoints_filename="./bs/KPOINTS", line_mode=True)
plotter = BSPlotterProjected(bs)
plotter.get_projected_plots_dots_patom_pmorb({"Au": ["dxy"]}, {"Au": [1]}, ylim=[-5, 5])

This change ensures more robust handling of cases where sum_morbs is None to prevent runtime errors.

ShengLin1001 commented 1 week ago

And I also have a question, why we need run this

elif len(dictio[elt][0]) > 1:

for example code, the len("dxy") = 3, what's meaning of this?