From a3616d2c8f80e8c78dd12d4d7cddb24a3b308e5a Mon Sep 17 00:00:00 2001 From: dyzheng Date: Sat, 1 Aug 2026 23:57:19 +0800 Subject: [PATCH 1/3] Fix(deltaspin): enable DeltaSpin constraint energy calculation for PW basis - cal_escon(): replace is_Mi_converged gate with lambda_/Mi_ empty check to prevent segfault when uninitialized - elecstate_pw: add get_spin_constrain_energy() override so PW basis computes DeltaSpin constraint energy (previously returned 0.0) - elecstate_pw.h: declare get_spin_constrain_energy() override Note: this fix enables escon computation for PW DeltaSpin but lambda values from BFGS optimizer may differ from accel branch due to energy functional convention differences. Full convergence with accel requires lambda_loop.cpp migration. --- source/source_estate/elecstate_pw.cpp | 9 +++++++++ source/source_estate/elecstate_pw.h | 2 ++ source/source_lcao/module_deltaspin/spin_constrain.cpp | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/source/source_estate/elecstate_pw.cpp b/source/source_estate/elecstate_pw.cpp index 7edb7b6bcf..641def5dc4 100644 --- a/source/source_estate/elecstate_pw.cpp +++ b/source/source_estate/elecstate_pw.cpp @@ -8,6 +8,7 @@ #include "source_base/timer.h" #include "source_hamilt/module_xc/xc_functional.h" #include "source_io/module_parameter/parameter.h" +#include "source_lcao/module_deltaspin/spin_constrain.h" #include "source_pw/module_pwdft/vnl_pw.h" namespace elecstate { @@ -56,6 +57,14 @@ ElecStatePW::~ElecStatePW() delmem_complex_op()(this->wfcr_another_spin); } +template +double ElecStatePW::get_spin_constrain_energy() +{ + spinconstrain::SpinConstrain>& sc + = spinconstrain::SpinConstrain>::getScInstance(); + return sc.cal_escon(); +} + template void ElecStatePW::init_rho_data() { diff --git a/source/source_estate/elecstate_pw.h b/source/source_estate/elecstate_pw.h index 623704e178..53e39917a5 100644 --- a/source/source_estate/elecstate_pw.h +++ b/source/source_estate/elecstate_pw.h @@ -36,6 +36,8 @@ class ElecStatePW : public ElecState virtual void cal_tau(const psi::Psi& psi); + double get_spin_constrain_energy() override; + //! calculate becsum for uspp void cal_becsum(const psi::Psi& psi); diff --git a/source/source_lcao/module_deltaspin/spin_constrain.cpp b/source/source_lcao/module_deltaspin/spin_constrain.cpp index b4048b5e9f..df8b4b20b3 100644 --- a/source/source_lcao/module_deltaspin/spin_constrain.cpp +++ b/source/source_lcao/module_deltaspin/spin_constrain.cpp @@ -45,7 +45,7 @@ template double SpinConstrain::cal_escon() { this->escon_ = 0.0; - if (!this->is_Mi_converged) + if (this->lambda_.empty() || this->Mi_.empty()) { return this->escon_; } From 21117cb149923dfb470ff2dc179480d9b3642ee9 Mon Sep 17 00:00:00 2001 From: dyzheng Date: Sun, 2 Aug 2026 00:32:48 +0800 Subject: [PATCH 2/3] Fix(deltaspin): enable PW DeltaSpin constraint energy and update test refs Source changes: - cal_escon(): replace is_Mi_converged guard with lambda_/Mi_ empty check to prevent segfault when uninitialized (matching accel convention) - elecstate_pw: add get_spin_constrain_energy() override so PW basis includes DeltaSpin constraint energy in total energy (was always 0) Test refs regenerated for 7 cases with significant energy changes: - 14_PW_DS_S4_XYZ, 15_PW_DS_S4_Z, 16_PW_DS_S4_XY - 18_PW_DFTU_DS_S2_Z, 19_PW_DFTU_DS_S4_XY, 21_PW_DFTU_DS_S4_Z - 41_PW_DS_S4_Thr10_XY nspin=2 tests and ReadLam/Thr1e10 tests unchanged. --- tests/17_DS_DFTU/14_PW_DS_S4_XYZ/result.ref | 6 +++--- tests/17_DS_DFTU/15_PW_DS_S4_Z/result.ref | 6 +++--- tests/17_DS_DFTU/16_PW_DS_S4_XY/result.ref | 6 +++--- tests/17_DS_DFTU/18_PW_DFTU_DS_S2_Z/result.ref | 6 +++--- tests/17_DS_DFTU/19_PW_DFTU_DS_S4_XY/result.ref | 6 +++--- tests/17_DS_DFTU/21_PW_DFTU_DS_S4_Z/result.ref | 6 +++--- tests/17_DS_DFTU/41_PW_DS_S4_Thr10_XY/result.ref | 6 +++--- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/17_DS_DFTU/14_PW_DS_S4_XYZ/result.ref b/tests/17_DS_DFTU/14_PW_DS_S4_XYZ/result.ref index f63986cceb..b26d723895 100644 --- a/tests/17_DS_DFTU/14_PW_DS_S4_XYZ/result.ref +++ b/tests/17_DS_DFTU/14_PW_DS_S4_XYZ/result.ref @@ -1,3 +1,3 @@ -etotref -6366.562922988214 -etotperatomref -3183.2814614941 -totaltimeref 4.23 +etotref -6396.381001785331 +etotperatomref -3198.1905008926656 +totaltimeref 1.0 diff --git a/tests/17_DS_DFTU/15_PW_DS_S4_Z/result.ref b/tests/17_DS_DFTU/15_PW_DS_S4_Z/result.ref index 07523240a6..d4a42e1987 100644 --- a/tests/17_DS_DFTU/15_PW_DS_S4_Z/result.ref +++ b/tests/17_DS_DFTU/15_PW_DS_S4_Z/result.ref @@ -1,3 +1,3 @@ -etotref -6366.562433916121 -etotperatomref -3183.2812169581 -totaltimeref 4.26 +etotref -6370.809681073991 +etotperatomref -3185.4048405369954 +totaltimeref 1.0 diff --git a/tests/17_DS_DFTU/16_PW_DS_S4_XY/result.ref b/tests/17_DS_DFTU/16_PW_DS_S4_XY/result.ref index c2ed3287e2..b1a91dc5ed 100644 --- a/tests/17_DS_DFTU/16_PW_DS_S4_XY/result.ref +++ b/tests/17_DS_DFTU/16_PW_DS_S4_XY/result.ref @@ -1,3 +1,3 @@ -etotref -6366.562695059035 -etotperatomref -3183.2813475295 -totaltimeref 4.19 +etotref -6378.090151037823 +etotperatomref -3189.0450755189113 +totaltimeref 1.0 diff --git a/tests/17_DS_DFTU/18_PW_DFTU_DS_S2_Z/result.ref b/tests/17_DS_DFTU/18_PW_DFTU_DS_S2_Z/result.ref index 654cb15b3e..1b4b2d3fac 100644 --- a/tests/17_DS_DFTU/18_PW_DFTU_DS_S2_Z/result.ref +++ b/tests/17_DS_DFTU/18_PW_DFTU_DS_S2_Z/result.ref @@ -1,3 +1,3 @@ -etotref -6355.9855588350255857 -etotperatomref -3177.9927794175 -totaltimeref 2.89 +etotref -6360.5555597262455194 +etotperatomref -3180.2777798631228 +totaltimeref 1.0 diff --git a/tests/17_DS_DFTU/19_PW_DFTU_DS_S4_XY/result.ref b/tests/17_DS_DFTU/19_PW_DFTU_DS_S4_XY/result.ref index ed294b0414..dd681fd07b 100644 --- a/tests/17_DS_DFTU/19_PW_DFTU_DS_S4_XY/result.ref +++ b/tests/17_DS_DFTU/19_PW_DFTU_DS_S4_XY/result.ref @@ -1,3 +1,3 @@ -etotref -6355.9841673819892094 -etotperatomref -3177.9920836910 -totaltimeref 5.88 +etotref -6360.5528100225901653 +etotperatomref -3180.276405011295 +totaltimeref 1.0 diff --git a/tests/17_DS_DFTU/21_PW_DFTU_DS_S4_Z/result.ref b/tests/17_DS_DFTU/21_PW_DFTU_DS_S4_Z/result.ref index 1810d27088..b2543ab16e 100644 --- a/tests/17_DS_DFTU/21_PW_DFTU_DS_S4_Z/result.ref +++ b/tests/17_DS_DFTU/21_PW_DFTU_DS_S4_Z/result.ref @@ -1,3 +1,3 @@ -etotref -6355.9834051938123594 -etotperatomref -3177.9917025969 -totaltimeref 5.46 +etotref -6360.5035660234671013 +etotperatomref -3180.2517830117336 +totaltimeref 1.0 diff --git a/tests/17_DS_DFTU/41_PW_DS_S4_Thr10_XY/result.ref b/tests/17_DS_DFTU/41_PW_DS_S4_Thr10_XY/result.ref index acf31e682c..01eca1c05d 100644 --- a/tests/17_DS_DFTU/41_PW_DS_S4_Thr10_XY/result.ref +++ b/tests/17_DS_DFTU/41_PW_DS_S4_Thr10_XY/result.ref @@ -1,3 +1,3 @@ -etotref -6366.564253345298 -etotperatomref -3183.2821266726 -totaltimeref 7.03 +etotref -6372.011126477043 +etotperatomref -3186.0055632385215 +totaltimeref 1.0 From 7fff0c98e29b028d75a340add73178e8b3631d1d Mon Sep 17 00:00:00 2001 From: dyzheng Date: Sun, 2 Aug 2026 00:43:52 +0800 Subject: [PATCH 3/3] Fix(deltaspin): fix pauli_to_moment My sign convention and enable PW escon MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Source fixes: - spin_constrain.h: fix My = -Im(occ1-occ2) → Im(occ1-occ2) The magnetic moment y-component had the wrong sign in the Pauli matrix transformation, causing incorrect Mi computation for nspin=4 DeltaSpin. - cal_escon(): replace is_Mi_converged guard with lambda_/Mi_ empty check - elecstate_pw: add get_spin_constrain_energy() for PW basis DeltaSpin Refs regenerated for nspin=4 DeltaSpin cases: 14, 15, 16, 19, 21, 41 Test 18 unchanged, nspin=2 tests unchanged. --- source/source_lcao/module_deltaspin/spin_constrain.h | 2 +- tests/17_DS_DFTU/14_PW_DS_S4_XYZ/result.ref | 4 ++-- tests/17_DS_DFTU/15_PW_DS_S4_Z/result.ref | 4 ++-- tests/17_DS_DFTU/16_PW_DS_S4_XY/result.ref | 4 ++-- tests/17_DS_DFTU/18_PW_DFTU_DS_S2_Z/result.ref | 6 +++--- tests/17_DS_DFTU/19_PW_DFTU_DS_S4_XY/result.ref | 4 ++-- tests/17_DS_DFTU/21_PW_DFTU_DS_S4_Z/result.ref | 4 ++-- tests/17_DS_DFTU/41_PW_DS_S4_Thr10_XY/result.ref | 4 ++-- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/source/source_lcao/module_deltaspin/spin_constrain.h b/source/source_lcao/module_deltaspin/spin_constrain.h index 4785e8dfe8..474cef6716 100644 --- a/source/source_lcao/module_deltaspin/spin_constrain.h +++ b/source/source_lcao/module_deltaspin/spin_constrain.h @@ -79,7 +79,7 @@ inline ModuleBase::Vector3 pauli_to_moment(const std::complex oc { return ModuleBase::Vector3( weight * (occ[1] + occ[2]).real(), - -weight * (occ[1] - occ[2]).imag(), + weight * (occ[1] - occ[2]).imag(), weight * (occ[0] - occ[3]).real() ); } diff --git a/tests/17_DS_DFTU/14_PW_DS_S4_XYZ/result.ref b/tests/17_DS_DFTU/14_PW_DS_S4_XYZ/result.ref index b26d723895..97fc3aaca4 100644 --- a/tests/17_DS_DFTU/14_PW_DS_S4_XYZ/result.ref +++ b/tests/17_DS_DFTU/14_PW_DS_S4_XYZ/result.ref @@ -1,3 +1,3 @@ -etotref -6396.381001785331 -etotperatomref -3198.1905008926656 +etotref -6369.19895097706 +etotperatomref -3184.59947548853 totaltimeref 1.0 diff --git a/tests/17_DS_DFTU/15_PW_DS_S4_Z/result.ref b/tests/17_DS_DFTU/15_PW_DS_S4_Z/result.ref index d4a42e1987..3c6f1a967e 100644 --- a/tests/17_DS_DFTU/15_PW_DS_S4_Z/result.ref +++ b/tests/17_DS_DFTU/15_PW_DS_S4_Z/result.ref @@ -1,3 +1,3 @@ -etotref -6370.809681073991 -etotperatomref -3185.4048405369954 +etotref -6369.198273166801 +etotperatomref -3184.5991365834007 totaltimeref 1.0 diff --git a/tests/17_DS_DFTU/16_PW_DS_S4_XY/result.ref b/tests/17_DS_DFTU/16_PW_DS_S4_XY/result.ref index b1a91dc5ed..d5b3da693e 100644 --- a/tests/17_DS_DFTU/16_PW_DS_S4_XY/result.ref +++ b/tests/17_DS_DFTU/16_PW_DS_S4_XY/result.ref @@ -1,3 +1,3 @@ -etotref -6378.090151037823 -etotperatomref -3189.0450755189113 +etotref -6369.198274098935 +etotperatomref -3184.5991370494676 totaltimeref 1.0 diff --git a/tests/17_DS_DFTU/18_PW_DFTU_DS_S2_Z/result.ref b/tests/17_DS_DFTU/18_PW_DFTU_DS_S2_Z/result.ref index 1b4b2d3fac..654cb15b3e 100644 --- a/tests/17_DS_DFTU/18_PW_DFTU_DS_S2_Z/result.ref +++ b/tests/17_DS_DFTU/18_PW_DFTU_DS_S2_Z/result.ref @@ -1,3 +1,3 @@ -etotref -6360.5555597262455194 -etotperatomref -3180.2777798631228 -totaltimeref 1.0 +etotref -6355.9855588350255857 +etotperatomref -3177.9927794175 +totaltimeref 2.89 diff --git a/tests/17_DS_DFTU/19_PW_DFTU_DS_S4_XY/result.ref b/tests/17_DS_DFTU/19_PW_DFTU_DS_S4_XY/result.ref index dd681fd07b..e40be3f01b 100644 --- a/tests/17_DS_DFTU/19_PW_DFTU_DS_S4_XY/result.ref +++ b/tests/17_DS_DFTU/19_PW_DFTU_DS_S4_XY/result.ref @@ -1,3 +1,3 @@ -etotref -6360.5528100225901653 -etotperatomref -3180.276405011295 +etotref -6360.5554588729937677 +etotperatomref -3180.277729436497 totaltimeref 1.0 diff --git a/tests/17_DS_DFTU/21_PW_DFTU_DS_S4_Z/result.ref b/tests/17_DS_DFTU/21_PW_DFTU_DS_S4_Z/result.ref index b2543ab16e..b06c7ee03a 100644 --- a/tests/17_DS_DFTU/21_PW_DFTU_DS_S4_Z/result.ref +++ b/tests/17_DS_DFTU/21_PW_DFTU_DS_S4_Z/result.ref @@ -1,3 +1,3 @@ -etotref -6360.5035660234671013 -etotperatomref -3180.2517830117336 +etotref -6360.5554655414534864 +etotperatomref -3180.2777327707267 totaltimeref 1.0 diff --git a/tests/17_DS_DFTU/41_PW_DS_S4_Thr10_XY/result.ref b/tests/17_DS_DFTU/41_PW_DS_S4_Thr10_XY/result.ref index 01eca1c05d..8d127ea729 100644 --- a/tests/17_DS_DFTU/41_PW_DS_S4_Thr10_XY/result.ref +++ b/tests/17_DS_DFTU/41_PW_DS_S4_Thr10_XY/result.ref @@ -1,3 +1,3 @@ -etotref -6372.011126477043 -etotperatomref -3186.0055632385215 +etotref -6369.198254647004 +etotperatomref -3184.599127323502 totaltimeref 1.0