Fix: Robust handling of bandgap boundary conditions in ElecState and add unit tests #6917
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Linked Issue
Fix #6909
Description
This PR improves the robustness of bandgap calculations in
ElecState::cal_bandgapandElecState::cal_bandgap_updwby handling boundary conditions where only VBM or CBM is found. If either VBM or CBM is not found (remains ±infinity), it is now assigned the Fermi level, ensuring the bandgap is always a finite, physically meaningful value. This prevents the output of infinity or undefined values in metallic or pathological cases.Additionally, new unit tests are added to
elecstate_energy_test.cppto verify the correct behavior for these boundary conditions, including cases where only VBM or only CBM is found for both spin-unpolarized and spin-polarized scenarios.Changes
cal_bandgapandcal_bandgap_updw, assign the Fermi level to VBM/CBM if not found.CalBandgapBoundaryConditionsandCalBandgapUpDwBoundaryConditionsunit tests to cover these scenarios.Motivation
To make the bandgap calculation more robust and user-friendly, and to ensure that the code behaves correctly in all possible input cases, including metallic systems or when the number of bands is insufficient.
No changes to the physical calculation logic for normal cases; only edge case handling and test coverage are improved.