From ea3b23493677df2fb19e0d9591c0f0ec6aa178f8 Mon Sep 17 00:00:00 2001 From: Weiqing Zhou Date: Tue, 16 Dec 2025 11:34:37 +0800 Subject: [PATCH 1/4] add a warningquit if magnetism is larger than charge --- source/source_estate/module_charge/charge_mixing_rho.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/source/source_estate/module_charge/charge_mixing_rho.cpp b/source/source_estate/module_charge/charge_mixing_rho.cpp index 38cd679f94..ec95cab963 100644 --- a/source/source_estate/module_charge/charge_mixing_rho.cpp +++ b/source/source_estate/module_charge/charge_mixing_rho.cpp @@ -98,6 +98,10 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) { chr->rhog[0][ig] = 0.5 * (rhog_mag[ig] + rhog_mag[ig+npw]); chr->rhog[1][ig] = 0.5 * (rhog_mag[ig] - rhog_mag[ig+npw]); + if (chr->rhog[1][ig] < 0.0) + { + ModuleBase::WARNING_QUIT("Charge_Mixing", "Magnetism is larger than Charge at some grid, please try a smaller mixing_beta_mag!"); + } } // delete delete[] rhog_mag; @@ -391,6 +395,10 @@ void Charge_Mixing::mix_rho_real(Charge* chr) { chr->rho[0][ir] = 0.5 * (rho_mag[ir] + rho_mag[ir+nrxx]); chr->rho[1][ir] = 0.5 * (rho_mag[ir] - rho_mag[ir+nrxx]); + if (chr->rho[1][ir] < 0.0) + { + ModuleBase::WARNING_QUIT("Charge_Mixing", "Magnetism is larger than Charge at some grid, please try a smaller mixing_beta_mag!"); + } } // delete delete[] rho_mag; From 26bab5b93da486d1429ce5bb552d01fea5804a87 Mon Sep 17 00:00:00 2001 From: Weiqing Zhou Date: Tue, 6 Jan 2026 14:23:12 +0800 Subject: [PATCH 2/4] use max(0,rho) instead of warningquit --- .../module_charge/charge_mixing_rho.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/source/source_estate/module_charge/charge_mixing_rho.cpp b/source/source_estate/module_charge/charge_mixing_rho.cpp index ec95cab963..d66a094eb2 100644 --- a/source/source_estate/module_charge/charge_mixing_rho.cpp +++ b/source/source_estate/module_charge/charge_mixing_rho.cpp @@ -96,12 +96,8 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) } for (int ig = 0; ig < npw; ig++) { - chr->rhog[0][ig] = 0.5 * (rhog_mag[ig] + rhog_mag[ig+npw]); - chr->rhog[1][ig] = 0.5 * (rhog_mag[ig] - rhog_mag[ig+npw]); - if (chr->rhog[1][ig] < 0.0) - { - ModuleBase::WARNING_QUIT("Charge_Mixing", "Magnetism is larger than Charge at some grid, please try a smaller mixing_beta_mag!"); - } + chr->rhog[0][ig] = max(0.0, 0.5 * (rhog_mag[ig] + rhog_mag[ig+npw])); + chr->rhog[1][ig] = max(0.0, 0.5 * (rhog_mag[ig] - rhog_mag[ig+npw])); } // delete delete[] rhog_mag; @@ -393,12 +389,8 @@ void Charge_Mixing::mix_rho_real(Charge* chr) } for (int ir = 0; ir < nrxx; ir++) { - chr->rho[0][ir] = 0.5 * (rho_mag[ir] + rho_mag[ir+nrxx]); - chr->rho[1][ir] = 0.5 * (rho_mag[ir] - rho_mag[ir+nrxx]); - if (chr->rho[1][ir] < 0.0) - { - ModuleBase::WARNING_QUIT("Charge_Mixing", "Magnetism is larger than Charge at some grid, please try a smaller mixing_beta_mag!"); - } + chr->rho[0][ir] = max(0.0, 0.5 * (rho_mag[ir] + rho_mag[ir+nrxx])); + chr->rho[1][ir] = max(0.0, 0.5 * (rho_mag[ir] - rho_mag[ir+nrxx])); } // delete delete[] rho_mag; From 314144215d9647e30b86e17b27217230925d564a Mon Sep 17 00:00:00 2001 From: Weiqing Zhou Date: Tue, 6 Jan 2026 14:27:26 +0800 Subject: [PATCH 3/4] use std::max() --- source/source_estate/module_charge/charge_mixing_rho.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/source_estate/module_charge/charge_mixing_rho.cpp b/source/source_estate/module_charge/charge_mixing_rho.cpp index d66a094eb2..8dca80a95b 100644 --- a/source/source_estate/module_charge/charge_mixing_rho.cpp +++ b/source/source_estate/module_charge/charge_mixing_rho.cpp @@ -96,8 +96,8 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) } for (int ig = 0; ig < npw; ig++) { - chr->rhog[0][ig] = max(0.0, 0.5 * (rhog_mag[ig] + rhog_mag[ig+npw])); - chr->rhog[1][ig] = max(0.0, 0.5 * (rhog_mag[ig] - rhog_mag[ig+npw])); + chr->rhog[0][ig] = std::max(0.0, 0.5 * (rhog_mag[ig] + rhog_mag[ig+npw])); + chr->rhog[1][ig] = std::max(0.0, 0.5 * (rhog_mag[ig] - rhog_mag[ig+npw])); } // delete delete[] rhog_mag; @@ -389,8 +389,8 @@ void Charge_Mixing::mix_rho_real(Charge* chr) } for (int ir = 0; ir < nrxx; ir++) { - chr->rho[0][ir] = max(0.0, 0.5 * (rho_mag[ir] + rho_mag[ir+nrxx])); - chr->rho[1][ir] = max(0.0, 0.5 * (rho_mag[ir] - rho_mag[ir+nrxx])); + chr->rho[0][ir] = std::max(0.0, 0.5 * (rho_mag[ir] + rho_mag[ir+nrxx])); + chr->rho[1][ir] = std::max(0.0, 0.5 * (rho_mag[ir] - rho_mag[ir+nrxx])); } // delete delete[] rho_mag; From 39c75268f8ec5992b9a8f6c0158554fcad60d432 Mon Sep 17 00:00:00 2001 From: Weiqing Zhou Date: Tue, 6 Jan 2026 15:08:45 +0800 Subject: [PATCH 4/4] fix bug --- source/source_estate/module_charge/charge_mixing_rho.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/source_estate/module_charge/charge_mixing_rho.cpp b/source/source_estate/module_charge/charge_mixing_rho.cpp index 8dca80a95b..4e7a4a970c 100644 --- a/source/source_estate/module_charge/charge_mixing_rho.cpp +++ b/source/source_estate/module_charge/charge_mixing_rho.cpp @@ -96,8 +96,8 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) } for (int ig = 0; ig < npw; ig++) { - chr->rhog[0][ig] = std::max(0.0, 0.5 * (rhog_mag[ig] + rhog_mag[ig+npw])); - chr->rhog[1][ig] = std::max(0.0, 0.5 * (rhog_mag[ig] - rhog_mag[ig+npw])); + chr->rhog[0][ig] = 0.5 * (rhog_mag[ig] + rhog_mag[ig+npw]); + chr->rhog[1][ig] = 0.5 * (rhog_mag[ig] - rhog_mag[ig+npw]); } // delete delete[] rhog_mag;