Skip to content

Commit 1f86285

Browse files
authored
Fix: Fix cal_bandgap for parallelism of k-points and band. (#6602)
* Fix: Fix cal_bandgap for parallelism of k-points and band. * Fix: Update cal_bandgap to use VBM and CBM terminology for clarity
1 parent b2a0c8a commit 1f86285

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

source/source_estate/elecstate_energy.cpp

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,86 +5,101 @@
55
#include "source_io/module_parameter/parameter.h"
66

77
#include <cmath>
8+
#include <limits>
89

910
namespace elecstate
1011
{
1112
/// @brief calculate band gap
1213
void ElecState::cal_bandgap()
1314
{
1415
if (this->ekb.nr == 0 || this->ekb.nc == 0)
15-
{ // which means no homo and no lumo
16+
{ // which means no vbm and no cbm
1617
this->bandgap = 0.0;
1718
return;
1819
}
19-
// int nbands = PARAM.inp.nbands;
20+
2021
int nbands = this->ekb.nc;
2122
int nks = this->klist->get_nks();
22-
double homo = this->ekb(0, 0);
23-
double lumo = this->ekb(0, nbands - 1);
23+
double vbm = -std::numeric_limits<double>::infinity(); // Valence Band Maximum
24+
double cbm = std::numeric_limits<double>::infinity(); // Conduction Band Minimum
2425
for (int ib = 0; ib < nbands; ib++)
2526
{
2627
for (int ik = 0; ik < nks; ik++)
2728
{
28-
if (!(this->ekb(ik, ib) - this->eferm.ef > 1e-5) && homo < this->ekb(ik, ib))
29+
if (this->ekb(ik, ib) <= this->eferm.ef && this->ekb(ik, ib) > vbm)
2930
{
30-
homo = this->ekb(ik, ib);
31+
vbm = this->ekb(ik, ib);
3132
}
32-
if (this->ekb(ik, ib) - this->eferm.ef > 1e-5 && lumo > this->ekb(ik, ib))
33+
if (this->ekb(ik, ib) >= this->eferm.ef && this->ekb(ik, ib) < cbm)
3334
{
34-
lumo = this->ekb(ik, ib);
35+
cbm = this->ekb(ik, ib);
3536
}
3637
}
3738
}
38-
this->bandgap = lumo - homo;
39+
40+
#ifdef __MPI
41+
Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, vbm);
42+
Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, cbm);
43+
#endif
44+
45+
this->bandgap = cbm - vbm;
3946
}
4047

4148
/// @brief calculate spin up & down band gap
4249
/// @todo add isk[ik] so as to discriminate different spins
4350
void ElecState::cal_bandgap_updw()
4451
{
4552
if (this->ekb.nr == 0 || this->ekb.nc == 0)
46-
{ // which means no homo and no lumo
53+
{ // which means no vbm and no cbm
4754
this->bandgap_up = 0.0;
4855
this->bandgap_dw = 0.0;
4956
return;
5057
}
5158
// int nbands = PARAM.inp.nbands;
5259
int nbands = this->ekb.nc;
5360
int nks = this->klist->get_nks();
54-
double homo_up = this->ekb(0, 0);
55-
double lumo_up = this->ekb(0, nbands - 1);
56-
double homo_dw = this->ekb(0, 0);
57-
double lumo_dw = this->ekb(0, nbands - 1);
61+
double vbm_up = -std::numeric_limits<double>::infinity();
62+
double cbm_up = std::numeric_limits<double>::infinity();
63+
double vbm_dw = -std::numeric_limits<double>::infinity();
64+
double cbm_dw = std::numeric_limits<double>::infinity();
5865
for (int ib = 0; ib < nbands; ib++)
5966
{
6067
for (int ik = 0; ik < nks; ik++)
6168
{
6269
if (this->klist->isk[ik] == 0)
6370
{
64-
if (!(this->ekb(ik, ib) - this->eferm.ef_up > 1e-5) && homo_up < this->ekb(ik, ib))
71+
if (this->ekb(ik, ib) <= this->eferm.ef_up && this->ekb(ik, ib) > vbm_up)
6572
{
66-
homo_up = this->ekb(ik, ib);
73+
vbm_up = this->ekb(ik, ib);
6774
}
68-
if (this->ekb(ik, ib) - this->eferm.ef_up > 1e-5 && lumo_up > this->ekb(ik, ib))
75+
if (this->ekb(ik, ib) >= this->eferm.ef_up && this->ekb(ik, ib) < cbm_up)
6976
{
70-
lumo_up = this->ekb(ik, ib);
77+
cbm_up = this->ekb(ik, ib);
7178
}
7279
}
7380
if (this->klist->isk[ik] == 1)
7481
{
75-
if (!(this->ekb(ik, ib) - this->eferm.ef_dw > 1e-5) && homo_dw < this->ekb(ik, ib))
82+
if (this->ekb(ik, ib) <= this->eferm.ef_dw && this->ekb(ik, ib) > vbm_dw)
7683
{
77-
homo_dw = this->ekb(ik, ib);
84+
vbm_dw = this->ekb(ik, ib);
7885
}
79-
if (this->ekb(ik, ib) - this->eferm.ef_dw > 1e-5 && lumo_dw > this->ekb(ik, ib))
86+
if (this->ekb(ik, ib) >= this->eferm.ef_dw && this->ekb(ik, ib) < cbm_dw)
8087
{
81-
lumo_dw = this->ekb(ik, ib);
88+
cbm_dw = this->ekb(ik, ib);
8289
}
8390
}
8491
}
8592
}
86-
this->bandgap_up = lumo_up - homo_up;
87-
this->bandgap_dw = lumo_dw - homo_dw;
93+
94+
#ifdef __MPI
95+
Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, vbm_up);
96+
Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, cbm_up);
97+
Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, vbm_dw);
98+
Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, cbm_dw);
99+
#endif
100+
101+
this->bandgap_up = cbm_up - vbm_up;
102+
this->bandgap_dw = cbm_dw - vbm_dw;
88103
}
89104

90105
/// @brief calculate deband

0 commit comments

Comments
 (0)