From 78444e97ad4a29e0fbf04083bd5634009327bf98 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Mon, 26 Jan 2026 20:56:51 +0800 Subject: [PATCH 1/6] add change --- source/source_base/parallel_reduce.cpp | 50 +++----------------------- 1 file changed, 5 insertions(+), 45 deletions(-) diff --git a/source/source_base/parallel_reduce.cpp b/source/source_base/parallel_reduce.cpp index 03535573b7..937fd27fa2 100644 --- a/source/source_base/parallel_reduce.cpp +++ b/source/source_base/parallel_reduce.cpp @@ -236,30 +236,14 @@ void Parallel_Reduce::gather_int_all(int& v, int* all) void Parallel_Reduce::gather_min_int_all(const int& nproc, int& v) { #ifdef __MPI - std::vector all(nproc, 0); - MPI_Allgather(&v, 1, MPI_INT, all.data(), 1, MPI_INT, MPI_COMM_WORLD); - for (int i = 0; i < nproc; i++) - { - if (v > all[i]) - { - v = all[i]; - } - } + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); #endif } void Parallel_Reduce::gather_max_double_all(const int& nproc, double& v) { #ifdef __MPI - std::vector value(nproc, 0.0); - MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, MPI_COMM_WORLD); - for (int i = 0; i < nproc; i++) - { - if (v < value[i]) - { - v = value[i]; - } - } + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); #endif } @@ -270,15 +254,7 @@ void Parallel_Reduce::gather_max_double_pool(const int& nproc_in_pool, double& v { return; } - std::vector value(nproc_in_pool, 0.0); - MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, POOL_WORLD); - for (int i = 0; i < nproc_in_pool; i++) - { - if (v < value[i]) - { - v = value[i]; - } - } + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MAX, POOL_WORLD); #endif } @@ -289,29 +265,13 @@ void Parallel_Reduce::gather_min_double_pool(const int& nproc_in_pool, double& v { return; } - std::vector value(nproc_in_pool, 0.0); - MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, POOL_WORLD); - for (int i = 0; i < nproc_in_pool; i++) - { - if (v > value[i]) - { - v = value[i]; - } - } + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MIN, POOL_WORLD); #endif } void Parallel_Reduce::gather_min_double_all(const int& nproc, double& v) { #ifdef __MPI - std::vector value(nproc, 0.0); - MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, MPI_COMM_WORLD); - for (int i = 0; i < nproc; i++) - { - if (v > value[i]) - { - v = value[i]; - } - } + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); #endif } \ No newline at end of file From a09dea1baa355fe85c8036abddae601be3b2ada3 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Tue, 27 Jan 2026 16:06:13 +0800 Subject: [PATCH 2/6] add numrial fast --- source/source_estate/elecstate_energy.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/source/source_estate/elecstate_energy.cpp b/source/source_estate/elecstate_energy.cpp index ea3c88f4d9..99b59dd9fb 100644 --- a/source/source_estate/elecstate_energy.cpp +++ b/source/source_estate/elecstate_energy.cpp @@ -20,8 +20,8 @@ void ElecState::cal_bandgap() int nbands = this->ekb.nc; int nks = this->klist->get_nks(); - double vbm = -std::numeric_limits::infinity(); // Valence Band Maximum - double cbm = std::numeric_limits::infinity(); // Conduction Band Minimum + double vbm = -1.0e100; // Valence Band Maximum + double cbm = 1.0e100; // Conduction Band Minimum const double threshold = 1.0e-5; // threshold to avoid E_gap(k) = 0 for (int ib = 0; ib < nbands; ib++) { @@ -59,10 +59,10 @@ void ElecState::cal_bandgap_updw() // int nbands = PARAM.inp.nbands; int nbands = this->ekb.nc; int nks = this->klist->get_nks(); - double vbm_up = -std::numeric_limits::infinity(); - double cbm_up = std::numeric_limits::infinity(); - double vbm_dw = -std::numeric_limits::infinity(); - double cbm_dw = std::numeric_limits::infinity(); + double vbm_up = -1.0e100; + double cbm_up = 1.0e100; + double vbm_dw = -1.0e100; + double cbm_dw = 1.0e100; const double threshold = 1.0e-5; for (int ib = 0; ib < nbands; ib++) { From b92990d5c4910dccf8aac60c1ab16d6e93b51cc4 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Tue, 27 Jan 2026 21:59:16 +0800 Subject: [PATCH 3/6] add check for cal_bandgap Boundary --- source/source_estate/elecstate_energy.cpp | 45 +++++++++++++----- .../test/elecstate_energy_test.cpp | 46 ++++++++++++++++++- 2 files changed, 79 insertions(+), 12 deletions(-) diff --git a/source/source_estate/elecstate_energy.cpp b/source/source_estate/elecstate_energy.cpp index 99b59dd9fb..2e8a67669b 100644 --- a/source/source_estate/elecstate_energy.cpp +++ b/source/source_estate/elecstate_energy.cpp @@ -20,8 +20,8 @@ void ElecState::cal_bandgap() int nbands = this->ekb.nc; int nks = this->klist->get_nks(); - double vbm = -1.0e100; // Valence Band Maximum - double cbm = 1.0e100; // Conduction Band Minimum + double vbm = -std::numeric_limits::infinity(); // Valence Band Maximum + double cbm = std::numeric_limits::infinity(); // Conduction Band Minimum const double threshold = 1.0e-5; // threshold to avoid E_gap(k) = 0 for (int ib = 0; ib < nbands; ib++) { @@ -37,7 +37,16 @@ void ElecState::cal_bandgap() } } } - + // Assign fermi level to CBM if it's still infinity + if(cbm == std::numeric_limits::infinity()) + { + cbm =this->eferm.ef; + } + // Assign fermi level to VBM if it's still negative infinity + if(vbm ==-std::numeric_limits::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); @@ -59,10 +68,10 @@ void ElecState::cal_bandgap_updw() // int nbands = PARAM.inp.nbands; int nbands = this->ekb.nc; int nks = this->klist->get_nks(); - double vbm_up = -1.0e100; - double cbm_up = 1.0e100; - double vbm_dw = -1.0e100; - double cbm_dw = 1.0e100; + double vbm_up = -std::numeric_limits::infinity(); + double cbm_up = std::numeric_limits::infinity(); + double vbm_dw = -std::numeric_limits::infinity(); + double cbm_dw = std::numeric_limits::infinity(); const double threshold = 1.0e-5; for (int ib = 0; ib < nbands; ib++) { @@ -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::infinity()) + { + cbm_up =this->eferm.ef_up; + } + if (cbm_dw == std::numeric_limits::infinity()) + { + cbm_dw =this->eferm.ef_dw; + } + // Assign fermi level to VBM if it's still negative infinity + if(vbm_up ==-std::numeric_limits::infinity()) + { + vbm_up =this->eferm.ef_up; + } + if(vbm_dw ==-std::numeric_limits::infinity()) + { + vbm_dw =this->eferm.ef_dw; + } #ifdef __MPI Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, vbm_up); @@ -315,10 +342,6 @@ void ElecState::cal_energies(const int type) this->f_en.e_local_pp = get_local_pp_energy(); -#ifdef __MLALGO - this->f_en.ml_exx = this->pot->get_ml_exx_energy(); -#endif - if (type == 1) // Harris-Foulkes functional { this->f_en.calculate_harris(); diff --git a/source/source_estate/test/elecstate_energy_test.cpp b/source/source_estate/test/elecstate_energy_test.cpp index e6d0203b68..00b94bacc4 100644 --- a/source/source_estate/test/elecstate_energy_test.cpp +++ b/source/source_estate/test/elecstate_energy_test.cpp @@ -1,4 +1,3 @@ - #include "gmock/gmock.h" #include "gtest/gtest.h" #define private public @@ -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); +} From dd8feacc6cf866b87563f4c761942c51bed2ad6e Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Tue, 27 Jan 2026 21:59:29 +0800 Subject: [PATCH 4/6] Revert "add change" This reverts commit 78444e97ad4a29e0fbf04083bd5634009327bf98. --- source/source_base/parallel_reduce.cpp | 50 +++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/source/source_base/parallel_reduce.cpp b/source/source_base/parallel_reduce.cpp index 937fd27fa2..03535573b7 100644 --- a/source/source_base/parallel_reduce.cpp +++ b/source/source_base/parallel_reduce.cpp @@ -236,14 +236,30 @@ void Parallel_Reduce::gather_int_all(int& v, int* all) void Parallel_Reduce::gather_min_int_all(const int& nproc, int& v) { #ifdef __MPI - MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); + std::vector all(nproc, 0); + MPI_Allgather(&v, 1, MPI_INT, all.data(), 1, MPI_INT, MPI_COMM_WORLD); + for (int i = 0; i < nproc; i++) + { + if (v > all[i]) + { + v = all[i]; + } + } #endif } void Parallel_Reduce::gather_max_double_all(const int& nproc, double& v) { #ifdef __MPI - MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); + std::vector value(nproc, 0.0); + MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, MPI_COMM_WORLD); + for (int i = 0; i < nproc; i++) + { + if (v < value[i]) + { + v = value[i]; + } + } #endif } @@ -254,7 +270,15 @@ void Parallel_Reduce::gather_max_double_pool(const int& nproc_in_pool, double& v { return; } - MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MAX, POOL_WORLD); + std::vector value(nproc_in_pool, 0.0); + MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, POOL_WORLD); + for (int i = 0; i < nproc_in_pool; i++) + { + if (v < value[i]) + { + v = value[i]; + } + } #endif } @@ -265,13 +289,29 @@ void Parallel_Reduce::gather_min_double_pool(const int& nproc_in_pool, double& v { return; } - MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MIN, POOL_WORLD); + std::vector value(nproc_in_pool, 0.0); + MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, POOL_WORLD); + for (int i = 0; i < nproc_in_pool; i++) + { + if (v > value[i]) + { + v = value[i]; + } + } #endif } void Parallel_Reduce::gather_min_double_all(const int& nproc, double& v) { #ifdef __MPI - MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); + std::vector value(nproc, 0.0); + MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, MPI_COMM_WORLD); + for (int i = 0; i < nproc; i++) + { + if (v > value[i]) + { + v = value[i]; + } + } #endif } \ No newline at end of file From da29b10b52f477e710e7642f27a396eb63c7cc16 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Tue, 27 Jan 2026 22:02:29 +0800 Subject: [PATCH 5/6] add back mlago --- source/source_estate/elecstate_energy.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/source_estate/elecstate_energy.cpp b/source/source_estate/elecstate_energy.cpp index 2e8a67669b..49bb46b5cc 100644 --- a/source/source_estate/elecstate_energy.cpp +++ b/source/source_estate/elecstate_energy.cpp @@ -342,6 +342,10 @@ void ElecState::cal_energies(const int type) this->f_en.e_local_pp = get_local_pp_energy(); + #ifdef __MLALGO + this->f_en.ml_exx = this->pot->get_ml_exx_energy(); + #endif + if (type == 1) // Harris-Foulkes functional { this->f_en.calculate_harris(); From 167a3e2a07ab866376c94431abcf0c822792b14f Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Tue, 27 Jan 2026 22:03:23 +0800 Subject: [PATCH 6/6] add back mlago format --- source/source_estate/elecstate_energy.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/source_estate/elecstate_energy.cpp b/source/source_estate/elecstate_energy.cpp index 49bb46b5cc..5083ea4621 100644 --- a/source/source_estate/elecstate_energy.cpp +++ b/source/source_estate/elecstate_energy.cpp @@ -342,10 +342,10 @@ void ElecState::cal_energies(const int type) this->f_en.e_local_pp = get_local_pp_energy(); - #ifdef __MLALGO - this->f_en.ml_exx = this->pot->get_ml_exx_energy(); - #endif - +#ifdef __MLALGO + this->f_en.ml_exx = this->pot->get_ml_exx_energy(); +#endif + if (type == 1) // Harris-Foulkes functional { this->f_en.calculate_harris();