Skip to content

Commit cd6d8e4

Browse files
author
dyzheng
committed
Refactor: add Per-spin HContainer wrappers for nspin=2
1 parent 50a82cb commit cd6d8e4

3 files changed

Lines changed: 11 additions & 11 deletions

File tree

source/source_io/module_ctrl/ctrl_scf_lcao.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,6 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell,
224224

225225
ModuleIO::write_hsr(hr_vec, sr, &ucell, precision, pv,
226226
out_app_flag, ucell.get_iat2iwt(), ucell.nat, istep);
227-
228-
// nspin=2: getHR_vector() returns new'd temporary objects
229-
if (PARAM.inp.nspin == 2)
230-
{
231-
for (auto* p : hr_vec) { delete p; }
232-
}
233227
}
234228

235229
//------------------------------------------------------------------

source/source_lcao/hamilt_lcao.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,9 @@ std::vector<HContainer<TR>*> HamiltLCAO<TK, TR>::getHR_vector()
484484
if (PARAM.inp.nspin == 2)
485485
{
486486
const int nnr = this->hRS2.size() / 2;
487-
HContainer<TR>* hr_up = new HContainer<TR>(*this->hR, this->hRS2.data());
488-
HContainer<TR>* hr_dn = new HContainer<TR>(*this->hR, this->hRS2.data() + nnr);
489-
return {hr_up, hr_dn};
487+
this->hr_spin_up_ = std::make_unique<HContainer<TR>>(*this->hR, this->hRS2.data());
488+
this->hr_spin_dn_ = std::make_unique<HContainer<TR>>(*this->hR, this->hRS2.data() + nnr);
489+
return {this->hr_spin_up_.get(), this->hr_spin_dn_.get()};
490490
}
491491
else
492492
{

source/source_lcao/hamilt_lcao.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "source_lcao/hs_matrix_k.hpp"
1111
#include "source_lcao/module_hcontainer/hcontainer.h"
1212

13+
#include <memory>
1314
#include <vector>
1415

1516
#include "source_lcao/setup_deepks.h" // mohan add 20251008
@@ -127,8 +128,8 @@ class HamiltLCAO : public Hamilt<TK>
127128
std::vector<TR>& getHRS2() { return this->hRS2; }
128129

129130
/// Get HR as a vector of HContainer pointers (one per spin).
130-
/// For nspin=2, creates temporary HContainers wrapping each spin half of hRS2.
131-
/// Caller must delete returned pointers when nspin=2.
131+
/// For nspin=2, returns pointers to internally managed per-spin wrappers over hRS2.
132+
/// Returned pointers are owned by this class; caller must NOT delete them.
132133
std::vector<HContainer<TR>*> getHR_vector();
133134

134135
/// refresh the status of HR
@@ -172,6 +173,11 @@ class HamiltLCAO : public Hamilt<TK>
172173
// save them in this->hRS2;
173174
std::vector<TR> hRS2;
174175

176+
/// Per-spin HContainer wrappers for nspin=2 (owned by this class).
177+
/// Rebuilt by getHR_vector() whenever hRS2 is resized.
178+
std::unique_ptr<HContainer<TR>> hr_spin_up_;
179+
std::unique_ptr<HContainer<TR>> hr_spin_dn_;
180+
175181
int refresh_times = 1;
176182

177183
//! current_spin for NSPIN=2 case

0 commit comments

Comments
 (0)