Skip to content

Commit cdd250b

Browse files
committed
Fix: Fix cal_bandgap for parallelism of k-points and band.
1 parent 4cfd8f5 commit cdd250b

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

source/source_estate/elecstate_energy.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "source_io/module_parameter/parameter.h"
66

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

910
namespace elecstate
1011
{
@@ -16,25 +17,31 @@ void ElecState::cal_bandgap()
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 homo = -std::numeric_limits<double>::infinity();
24+
double lumo = std::numeric_limits<double>::infinity();
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) > homo)
2930
{
3031
homo = 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) < lumo)
3334
{
3435
lumo = this->ekb(ik, ib);
3536
}
3637
}
3738
}
39+
40+
#ifdef __MPI
41+
Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, homo);
42+
Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, lumo);
43+
#endif
44+
3845
this->bandgap = lumo - homo;
3946
}
4047

@@ -51,38 +58,46 @@ void ElecState::cal_bandgap_updw()
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 homo_up = -std::numeric_limits<double>::infinity();
62+
double lumo_up = std::numeric_limits<double>::infinity();
63+
double homo_dw = -std::numeric_limits<double>::infinity();
64+
double lumo_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) > homo_up)
6572
{
6673
homo_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) < lumo_up)
6976
{
7077
lumo_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) > homo_dw)
7683
{
7784
homo_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) < lumo_dw)
8087
{
8188
lumo_dw = this->ekb(ik, ib);
8289
}
8390
}
8491
}
8592
}
93+
94+
#ifdef __MPI
95+
Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, homo_up);
96+
Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, lumo_up);
97+
Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, homo_dw);
98+
Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, lumo_dw);
99+
#endif
100+
86101
this->bandgap_up = lumo_up - homo_up;
87102
this->bandgap_dw = lumo_dw - homo_dw;
88103
}

0 commit comments

Comments
 (0)