Skip to content

Commit c4e9da6

Browse files
author
abacus_fixer
committed
Refactor DeltaSpin, steps 1-2: extract ScState and solver-independent init
Split the SpinConstrain god class along functional boundaries as the first steps of separating PW and LCAO code paths: - New deltaspin_state.{h,cpp}: non-template ScState owns all basis-set-independent constraint data (lambda, Mi, target_mag, constrain, atom/orbital indexing maps, lambda-loop parameters) and the ~45 setter/getter implementations moved out of spin_constrain.cpp. SpinConstrain keeps a ScState value member and its public interface becomes thin forwarding shells, so all existing call sites (esolvers, operators, tests) are unchanged. - New deltaspin_init.{h,cpp}: free function init_sc_state() performs the UnitCell/STRU-driven state initialization (count maps, nspin=2 x/y constraint fix, Ry unit conversion) with no dependency on solver-side objects. SpinConstrain::init_sc() is now a shell that calls init_sc_state() and stores external pointers. init_sc.cpp is replaced by deltaspin_init.cpp in CMakeLists. - Internal implementations (lambda_loop, cal_mw*, deltaspin_pw_impl) now access state through the state_ member. Scalars directly mutated by the lambda loop are transitional public fields on ScState, to be收敛ed to accessors when the loop is extracted. No INPUT parameter behavior changes; docs update not required. Verification: - cmake --build build -j 16 (Release, ENABLE_LCAO=ON): success - OMP_NUM_THREADS=1 ctest --test-dir build -R deltaspin: 5/5 passed (MODULE_LCAO_deltaspin_basic_func_test, spin_constrain_test, template_helpers, deltaspin_pw_test, deltaspin_core_test) - python3 tools/03_code_analysis/agent_governance_check.py --staged: no findings
1 parent 3a96cd7 commit c4e9da6

14 files changed

Lines changed: 1101 additions & 924 deletions

File tree

source/source_estate/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ AddTest(
5252
../occupy.cpp
5353
../module_charge/charge_mpi.cpp
5454
../../source_lcao/module_deltaspin/spin_constrain.cpp
55+
../../source_lcao/module_deltaspin/deltaspin_state.cpp
5556
../../source_psi/psi.cpp
5657
../../source_base/module_device/memory_op.cpp
5758
)

source/source_lcao/module_deltaspin/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
list(APPEND objects
22
spin_constrain.cpp
3-
init_sc.cpp
3+
deltaspin_init.cpp
44
cal_mw.cpp
55
basic_funcs.cpp
66
lambda_loop_helper.cpp
@@ -9,6 +9,7 @@ list(APPEND objects
99
template_helpers.cpp
1010
deltaspin_lcao.cpp
1111
cal_mw_helper.cpp
12+
deltaspin_state.cpp
1213
mi_tools.cpp
1314
)
1415

source/source_lcao/module_deltaspin/cal_mw.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mi_lcao(const int&
4747
this->zero_Mi();
4848
const hamilt::HContainer<double>* dmr = this->dm_->get_DMR_pointer(1);
4949
std::vector<double> moments;
50-
if(this->nspin_==2)
50+
if(this->state_.nspin_==2)
5151
{
5252
// Switch to spin-difference density matrix (rho_up - rho_dn)
5353
this->dm_->switch_dmr(2);
@@ -59,22 +59,22 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mi_lcao(const int&
5959
this->dm_->switch_dmr(0);
6060

6161
// For nspin=2, only z-component is meaningful
62-
for(int iat=0;iat<this->Mi_.size();iat++)
62+
for(int iat=0;iat<this->state_.Mi_.size();iat++)
6363
{
64-
this->Mi_[iat].x = 0.0;
65-
this->Mi_[iat].y = 0.0;
66-
this->Mi_[iat].z = moments[iat];
64+
this->state_.Mi_[iat].x = 0.0;
65+
this->state_.Mi_[iat].y = 0.0;
66+
this->state_.Mi_[iat].z = moments[iat];
6767
}
6868
}
69-
else if(this->nspin_==4)
69+
else if(this->state_.nspin_==4)
7070
{
7171
// For nspin=4, moments array contains interleaved [Mx, My, Mz] per atom
7272
moments = static_cast<hamilt::DeltaSpin<hamilt::OperatorLCAO<std::complex<double>, std::complex<double>>>*>(this->p_operator)->cal_moment(dmr, this->get_constrain());
73-
for(int iat=0;iat<this->Mi_.size();iat++)
73+
for(int iat=0;iat<this->state_.Mi_.size();iat++)
7474
{
75-
this->Mi_[iat].x = moments[iat*3];
76-
this->Mi_[iat].y = moments[iat*3+1];
77-
this->Mi_[iat].z = moments[iat*3+2];
75+
this->state_.Mi_[iat].x = moments[iat*3];
76+
this->state_.Mi_[iat].y = moments[iat*3+1];
77+
this->state_.Mi_[iat].z = moments[iat*3+2];
7878
}
7979
}
8080

source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
105105
PARAM.inp.nbands,
106106
PARAM.inp.nelec,
107107
PARAM.inp.device == "gpu");
108-
if (this->nspin_ == 2)
108+
if (this->state_.nspin_ == 2)
109109
{
110110
dynamic_cast<hamilt::DeltaSpin<hamilt::OperatorLCAO<std::complex<double>, double>>*>(this->p_operator)
111111
->update_lambda();
112112
}
113-
else if (this->nspin_ == 4)
113+
else if (this->state_.nspin_ == 4)
114114
{
115115
dynamic_cast<hamilt::DeltaSpin<hamilt::OperatorLCAO<std::complex<double>, std::complex<double>>>*>(
116116
this->p_operator)
117117
->update_lambda();
118118
}
119119
// Diagonalization without updating charge density (last param = true means skip charge update)
120-
hsolver_t.solve(hamilt_t, psi_t[0], this->pelec, *this->dm_, *this->pelec->charge, this->nspin_, true);
120+
hsolver_t.solve(hamilt_t, psi_t[0], this->pelec, *this->dm_, *this->pelec->charge, this->state_.nspin_, true);
121121
elecstate::calculate_weights(this->pelec->ekb,
122122
this->pelec->wg,
123123
this->pelec->klist,
@@ -171,7 +171,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
171171
this->sub_h_save = new std::complex<double>[nbands * nbands * nk];
172172
this->sub_s_save = new std::complex<double>[nbands * nbands * nk];
173173
this->becp_save = new std::complex<double>[size_becp * nk];
174-
this->lambda_in_sub_ = this->lambda_;
174+
this->lambda_in_sub_ = this->state_.lambda_;
175175
}
176176
for (int ik = 0; ik < nk; ++ik)
177177
{
@@ -191,7 +191,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
191191
memcpy(h_tmp.data(), h_k, sizeof(std::complex<double>) * nbands * nbands);
192192
memcpy(s_tmp.data(), s_k, sizeof(std::complex<double>) * nbands * nbands);
193193
// Apply DeltaSpin correction (skip for initialization step i_step=-1)
194-
if (i_step != -1) this->calculate_delta_hcc(h_tmp.data(), becp_k, this->lambda_.data(), nbands, nkb, nh_iat, ik, true);
194+
if (i_step != -1) this->calculate_delta_hcc(h_tmp.data(), becp_k, this->state_.lambda_.data(), nbands, nkb, nh_iat, ik, true);
195195

196196
// Diagonalize in subspace, update becp (response wavefunctions)
197197
hsolver::DiagoIterAssist<std::complex<double>>::diag_responce(h_tmp.data(),
@@ -230,7 +230,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
230230
base_device::memory::resize_memory_op<std::complex<double>, base_device::DEVICE_GPU>()(this->sub_h_save, nbands * nbands * nk);
231231
base_device::memory::resize_memory_op<std::complex<double>, base_device::DEVICE_GPU>()(this->sub_s_save, nbands * nbands * nk);
232232
base_device::memory::resize_memory_op<std::complex<double>, base_device::DEVICE_GPU>()(this->becp_save, size_becp * nk);
233-
this->lambda_in_sub_ = this->lambda_;
233+
this->lambda_in_sub_ = this->state_.lambda_;
234234
}
235235
std::complex<double>* becp_pointer = nullptr;
236236
base_device::memory::resize_memory_op<std::complex<double>, base_device::DEVICE_GPU>()(becp_pointer, size_becp);
@@ -249,7 +249,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
249249
}
250250
base_device::memory::synchronize_memory_op<std::complex<double>, base_device::DEVICE_GPU, base_device::DEVICE_GPU>()(h_tmp, h_k, nbands * nbands);
251251
base_device::memory::synchronize_memory_op<std::complex<double>, base_device::DEVICE_GPU, base_device::DEVICE_GPU>()(s_tmp, s_k, nbands * nbands);
252-
if (i_step != -1) this->calculate_delta_hcc(h_tmp, becp_k, this->lambda_.data(), nbands, nkb, nh_iat, ik, true);
252+
if (i_step != -1) this->calculate_delta_hcc(h_tmp, becp_k, this->state_.lambda_.data(), nbands, nkb, nh_iat, ik, true);
253253

254254
hsolver::DiagoIterAssist<std::complex<double>, base_device::DEVICE_GPU>::diag_responce(h_tmp,
255255
s_tmp,
@@ -280,15 +280,15 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
280280
for (int ik = 0; ik < nk; ik++)
281281
{
282282
const std::complex<double>* becp = &becp_tmp[ik * size_becp];
283-
const int spin_sign = (this->npol_ == 2) ? 1 : this->get_spin_sign(ik);
284-
accumulate_Mi_from_becp(becp, nkb, nbands, this->npol_, spin_sign,
285-
&this->pelec->wg(ik, 0), nh_iat, this->Mi_);
283+
const int spin_sign = (this->state_.npol_ == 2) ? 1 : this->get_spin_sign(ik);
284+
accumulate_Mi_from_becp(becp, nkb, nbands, this->state_.npol_, spin_sign,
285+
&this->pelec->wg(ik, 0), nh_iat, this->state_.Mi_);
286286
}
287287
// MPI reduction: sum Mi across all k-pool ranks
288288
Parallel_Reduce::reduce_double_allpool(PARAM.inp.kpar,
289289
GlobalV::NPROC_IN_POOL,
290-
&(this->Mi_[0][0]),
291-
3 * this->Mi_.size());
290+
&(this->state_.Mi_[0][0]),
291+
3 * this->state_.Mi_.size());
292292
}
293293
}
294294
ModuleBase::timer::end("spinconstrain::SpinConstrain", "cal_mw_from_lambda");

source/source_lcao/module_deltaspin/cal_mw_helper.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ std::vector<std::vector<std::vector<double>>> spinconstrain::SpinConstrain<std::
3434
const ModuleBase::matrix& orbMulP)
3535
{
3636
std::vector<std::vector<std::vector<double>>> AorbMulP;
37-
AorbMulP.resize(this->nspin_);
37+
AorbMulP.resize(this->state_.nspin_);
3838
int nat = this->get_nat();
39-
for (int is = 0; is < this->nspin_; ++is)
39+
for (int is = 0; is < this->state_.nspin_; ++is)
4040
{
4141
int num = 0;
4242
AorbMulP[is].resize(nat);
@@ -86,7 +86,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::calculate_MW(
8686

8787
this->zero_Mi();
8888

89-
const int nlocal = (this->nspin_ == 4) ? nw / 2 : nw;
89+
const int nlocal = (this->state_.nspin_ == 4) ? nw / 2 : nw;
9090
for (const auto& sc_elem: this->get_atomCounts())
9191
{
9292
int it = sc_elem.first;
@@ -96,51 +96,51 @@ void spinconstrain::SpinConstrain<std::complex<double>>::calculate_MW(
9696
int num = 0;
9797
int iat = this->get_iat(it, ia);
9898
double atom_mag = 0.0;
99-
std::vector<double> total_charge_soc(this->nspin_, 0.0);
99+
std::vector<double> total_charge_soc(this->state_.nspin_, 0.0);
100100
for (const auto& lnchi: this->get_lnchiCounts().at(it))
101101
{
102-
std::vector<double> sum_l(this->nspin_, 0.0);
102+
std::vector<double> sum_l(this->state_.nspin_, 0.0);
103103
int L = lnchi.first;
104104
int nchi = lnchi.second;
105105
for (int Z = 0; Z < nchi; ++Z)
106106
{
107-
std::vector<double> sum_m(this->nspin_, 0.0);
107+
std::vector<double> sum_m(this->state_.nspin_, 0.0);
108108
for (int M = 0; M < (2 * L + 1); ++M)
109109
{
110-
for (int j = 0; j < this->nspin_; j++)
110+
for (int j = 0; j < this->state_.nspin_; j++)
111111
{
112112
sum_m[j] += AorbMulP[j][iat][num];
113113
}
114114
num++;
115115
}
116-
for (int j = 0; j < this->nspin_; j++)
116+
for (int j = 0; j < this->state_.nspin_; j++)
117117
{
118118
sum_l[j] += sum_m[j];
119119
}
120120
}
121-
if (this->nspin_ == 2)
121+
if (this->state_.nspin_ == 2)
122122
{
123123
atom_mag += sum_l[0] - sum_l[1];
124124
}
125-
else if (this->nspin_ == 4)
125+
else if (this->state_.nspin_ == 4)
126126
{
127-
for (int j = 0; j < this->nspin_; j++)
127+
for (int j = 0; j < this->state_.nspin_; j++)
128128
{
129129
total_charge_soc[j] += sum_l[j];
130130
}
131131
}
132132
}
133-
if (this->nspin_ == 2)
133+
if (this->state_.nspin_ == 2)
134134
{
135-
this->Mi_[iat].x = 0.0;
136-
this->Mi_[iat].y = 0.0;
137-
this->Mi_[iat].z = atom_mag;
135+
this->state_.Mi_[iat].x = 0.0;
136+
this->state_.Mi_[iat].y = 0.0;
137+
this->state_.Mi_[iat].z = atom_mag;
138138
}
139-
else if (this->nspin_ == 4)
139+
else if (this->state_.nspin_ == 4)
140140
{
141-
this->Mi_[iat].x = (std::abs(total_charge_soc[1]) < this->sc_thr_)? 0.0 : total_charge_soc[1];
142-
this->Mi_[iat].y = (std::abs(total_charge_soc[2]) < this->sc_thr_)? 0.0 : total_charge_soc[2];
143-
this->Mi_[iat].z = (std::abs(total_charge_soc[3]) < this->sc_thr_)? 0.0 : total_charge_soc[3];
141+
this->state_.Mi_[iat].x = (std::abs(total_charge_soc[1]) < this->state_.sc_thr_)? 0.0 : total_charge_soc[1];
142+
this->state_.Mi_[iat].y = (std::abs(total_charge_soc[2]) < this->state_.sc_thr_)? 0.0 : total_charge_soc[2];
143+
this->state_.Mi_[iat].z = (std::abs(total_charge_soc[3]) < this->state_.sc_thr_)? 0.0 : total_charge_soc[3];
144144
}
145145
}
146146
}
@@ -174,7 +174,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::collect_MW(ModuleBase::
174174
int nw,
175175
int isk)
176176
{
177-
if (this->nspin_ == 2)
177+
if (this->state_.nspin_ == 2)
178178
{
179179
for (size_t i=0; i < nw; ++i)
180180
{
@@ -186,7 +186,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::collect_MW(ModuleBase::
186186
}
187187
}
188188
}
189-
else if (this->nspin_ == 4)
189+
else if (this->state_.nspin_ == 4)
190190
{
191191
for (size_t i = 0; i < nw; ++i)
192192
{

0 commit comments

Comments
 (0)