Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion source/source_estate/elecstate_energy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ void ElecState::cal_bandgap()
}
}
}

// Assign fermi level to CBM if it's still infinity
if(cbm == std::numeric_limits<double>::infinity())
{
cbm =this->eferm.ef;
}
// Assign fermi level to VBM if it's still negative infinity
if(vbm ==-std::numeric_limits<double>::infinity())
{
vbm =this->eferm.ef;
}
#ifdef __MPI
Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, vbm);
Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, cbm);
Expand Down Expand Up @@ -92,6 +101,24 @@ void ElecState::cal_bandgap_updw()
}
}
}
// Assign fermi level to CBM if it's still infinity
if (cbm_up == std::numeric_limits<double>::infinity())
{
cbm_up =this->eferm.ef_up;
}
if (cbm_dw == std::numeric_limits<double>::infinity())
{
cbm_dw =this->eferm.ef_dw;
}
// Assign fermi level to VBM if it's still negative infinity
if(vbm_up ==-std::numeric_limits<double>::infinity())
{
vbm_up =this->eferm.ef_up;
}
if(vbm_dw ==-std::numeric_limits<double>::infinity())
{
vbm_dw =this->eferm.ef_dw;
}

#ifdef __MPI
Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, vbm_up);
Expand Down
46 changes: 45 additions & 1 deletion source/source_estate/test/elecstate_energy_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#define private public
Expand Down Expand Up @@ -242,3 +241,48 @@ TEST_F(ElecStateEnergyTest, CalBandgapUpDw)
EXPECT_DOUBLE_EQ(elecstate->bandgap_up, 1.0);
EXPECT_DOUBLE_EQ(elecstate->bandgap_dw, 0.5);
}

TEST_F(ElecStateEnergyTest, CalBandgapBoundaryConditions)
{
K_Vectors* klist = new K_Vectors;
klist->set_nks(1);
elecstate->klist = klist;
elecstate->ekb.create(1, 1);

// Case 1: Only VBM found (all bands below Fermi level)
elecstate->ekb(0, 0) = -5.0;
elecstate->eferm.ef = 0.0;
elecstate->cal_bandgap();
// Only VBM found, CBM is set to eferm.ef, so bandgap should be eferm.ef - vbm
EXPECT_DOUBLE_EQ(elecstate->bandgap, 5.0);

// Case 2: Only CBM found (all bands above Fermi level)
elecstate->ekb(0, 0) = 5.0;
elecstate->eferm.ef = 0.0;
elecstate->cal_bandgap();
// Only CBM found, VBM is set to eferm.ef, so bandgap should be cbm - eferm.ef
EXPECT_DOUBLE_EQ(elecstate->bandgap, 5.0);
}

TEST_F(ElecStateEnergyTest, CalBandgapUpDwBoundaryConditions)
{
K_Vectors* klist = new K_Vectors;
klist->set_nks(2);
klist->isk.resize(2);
klist->isk[0] = 0; // spin up
klist->isk[1] = 1; // spin down
elecstate->klist = klist;
elecstate->ekb.create(2, 1); // 2 k-points, 1 band

// Spin UP: Only VBM (band < ef)
elecstate->ekb(0, 0) = -5.0;
elecstate->eferm.ef_up = 0.0;
// Spin DW: Only CBM (band > ef)
elecstate->ekb(1, 0) = 5.0;
elecstate->eferm.ef_dw = 0.0;
elecstate->cal_bandgap_updw();
// up: Only VBM found, CBM is set to eferm.ef_up, so gap should be eferm.ef_up - vbm_up
// dw: Only CBM found, VBM is set to eferm.ef_dw, so gap should be cbm_dw - eferm.ef_dw
EXPECT_DOUBLE_EQ(elecstate->bandgap_up, 5.0);
EXPECT_DOUBLE_EQ(elecstate->bandgap_dw, 5.0);
}
Loading