Skip to content

Commit 4fffc2f

Browse files
author
abacus_fixer
committed
refactor(klist): rename nspin to spin_mult and fix set_after_vc bug
Rename the private member K_Vectors::nspin -> spin_mult (together with get_nspin/set_nspin -> get_spin_mult/set_spin_mult). The old name collided semantically with PARAM.inp.nspin: the member holds the k-point spin multiplicity (1 or 2; nspin=4 is mapped to 1 at set() time), not the physical spin index (1/2/4). The new name matches the inherited ReciprocalGrid::spin_factor() override and the member's actual role as a k-point doubling factor. Fix two bugs surfaced by the rename: - K_Vectors::set() now maps spin_mult directly from the input nspin_in (print -> validate -> map) instead of the previous "assign first, then remap itself" flow that also left the member written before validation. - K_Vectors::set_after_vc() no longer accepts an nspin argument and no longer calls set_spin_mult(): the spin multiplicity is fixed by set() and never changes during a run. The old code passed the raw, unmapped input nspin (e.g. 4 for non-collinear) and wrote it into spin_mult, silently overwriting the correct mapped value in nspin=4 vc-relax calculations. The remaining work in set_after_vc is purely a Cartesian coordinate refresh (kvec_d2c) plus logging. Remove the now-unused set_spin_mult() setter (the only external caller was set_after_vc itself). Update production and test call sites (esolver_fp, klist_test, klist_test_para, print_info_test, write_dmk_test) accordingly. API surface: K_Vectors::set() and the data members exposed through isk/nks/ nkstot remain unchanged. No INPUT or PARAM behavior is altered.
1 parent 3a9a2a8 commit 4fffc2f

7 files changed

Lines changed: 93 additions & 93 deletions

File tree

source/source_cell/klist.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void K_Vectors::cal_ik_global()
1515
const int my_pool = this->para_k.my_pool;
1616
this->ik2iktot.resize(this->nks);
1717
#ifdef __MPI
18-
if(this->nspin == 2)
18+
if(this->spin_mult == 2)
1919
{
2020
for (int ik = 0; ik < this->nks / 2; ++ik)
2121
{
@@ -72,16 +72,17 @@ void K_Vectors::set(const UnitCell& ucell,
7272
const bool gamma_only_local_ = gamma_only_local;
7373
const std::string kmesh_type_ = kmesh_type;
7474

75-
// (1) set nspin, read kpoints.
76-
this->nspin = nspin_in;
77-
ModuleBase::GlobalFunc::OUT(ofs, "nspin", nspin);
75+
// (1) print nspin, set the k-point spin multiplicity, read kpoints.
76+
ModuleBase::GlobalFunc::OUT(ofs, "nspin", nspin_in);
7877

79-
if (this->nspin != 1 && this->nspin != 2 && this->nspin != 4)
78+
if (nspin_in != 1 && nspin_in != 2 && nspin_in != 4)
8079
{
8180
ModuleBase::WARNING_QUIT("K_Vectors::set", "Only available for nspin = 1 or 2 or 4");
8281
}
8382

84-
this->nspin = (this->nspin == 4) ? 1 : this->nspin;
83+
// non-collinear (nspin=4) does not double the k-point list, so its
84+
// k-point spin multiplicity is the same as for the unpolarized case.
85+
this->spin_mult = (nspin_in == 4) ? 1 : nspin_in;
8586

8687
bool read_succesfully = this->read_kpoints(ucell, k_file_name, gamma_only_local_, kspacing, kmesh_type_, koffset, ofs);
8788
#ifdef __MPI
@@ -195,8 +196,8 @@ void K_Vectors::set(const UnitCell& ucell,
195196
return;
196197
}
197198

198-
// 1.reset the size of the K-point container according to nspin and nkstot
199-
// 2.reserve space for nspin>2 (symmetry)
199+
// 1.reset the size of the K-point container according to spin_mult and nkstot
200+
// 2.reserve space for spin_mult>2 (symmetry)
200201
void K_Vectors::renew(const int& kpoint_number)
201202
{
202203
ReciprocalGrid::renew(kpoint_number);
@@ -377,7 +378,7 @@ bool K_Vectors::parse_kfile(const std::string& fn, std::ofstream& ofs_running)
377378
{
378379
if (kword == "Cartesian" || kword == "C") // Cartesian coordinates
379380
{
380-
this->renew(nkstot * nspin); // mohan fix bug 2009-09-01
381+
this->renew(nkstot * this->spin_mult); // mohan fix bug 2009-09-01
381382
for (int i = 0; i < nkstot; i++)
382383
{
383384
ifk >> kvec_c[i].x >> kvec_c[i].y >> kvec_c[i].z;
@@ -388,7 +389,7 @@ bool K_Vectors::parse_kfile(const std::string& fn, std::ofstream& ofs_running)
388389
}
389390
else if (kword == "Direct" || kword == "D") // Direct coordinates
390391
{
391-
this->renew(nkstot * nspin); // mohan fix bug 2009-09-01
392+
this->renew(nkstot * this->spin_mult); // mohan fix bug 2009-09-01
392393
for (int i = 0; i < nkstot; i++)
393394
{
394395
ifk >> kvec_d[i].x >> kvec_d[i].y >> kvec_d[i].z;
@@ -485,7 +486,7 @@ void K_Vectors::interpolate_k_between(std::ifstream& ifk, std::vector<ModuleBase
485486
}
486487

487488
// std::cout << " nkstot = " << nkstot << std::endl;
488-
this->renew(nkstot * nspin); // mohan fix bug 2009-09-01
489+
this->renew(nkstot * this->spin_mult); // mohan fix bug 2009-09-01
489490

490491
int count = 0;
491492
for (int iks = 1; iks < nks_special; iks++)
@@ -530,7 +531,7 @@ void K_Vectors::update_use_ibz(const int& nkstot_ibz,
530531

531532
ModuleBase::GlobalFunc::OUT(ofs_running, "nkstot now", nkstot);
532533

533-
this->kvec_d.resize(this->nkstot * nspin); // qianrui fix a bug 2021-7-13 for nspin=2 in set_kup_and_kdw()
534+
this->kvec_d.resize(this->nkstot * this->spin_mult); // qianrui fix a bug 2021-7-13 for spin_mult=2 in set_kup_and_kdw()
534535

535536
for (int i = 0; i < this->nkstot; ++i)
536537
{
@@ -557,10 +558,10 @@ void K_Vectors::set_kup_and_kdw(std::ofstream& ofs_running)
557558
// on output: the number of points is doubled and xk and wk in the
558559
// first (nks/2) positions correspond to up spin
559560
// those in the second (nks/2) ones correspond to down spin
560-
// nspin can only be 1 or 2 here: K_Vectors::set() maps nspin=4
561+
// spin_mult can only be 1 or 2 here: K_Vectors::set() maps nspin=4
561562
// (non-collinear) to 1 before the k-list is built.
562563
//=========================================================================
563-
switch (nspin)
564+
switch (this->spin_mult)
564565
{
565566
case 1:
566567

@@ -764,11 +765,12 @@ void K_Vectors::reduce_by_symmetry(const UnitCell& ucell,
764765
return;
765766
}
766767

767-
void K_Vectors::set_after_vc(const int& nspin_in, const ModuleBase::Matrix3& G, std::ofstream& ofs_running)
768+
void K_Vectors::set_after_vc(const ModuleBase::Matrix3& G, std::ofstream& ofs_running)
768769
{
769770
ofs_running << "\n SETUP K-POINTS" << std::endl;
770-
this->set_nspin(nspin_in);
771-
ModuleBase::GlobalFunc::OUT(ofs_running, "nspin", this->get_nspin());
771+
// spin_mult is fixed by set() and does not change during a run, so the
772+
// volume-change update only recomputes the Cartesian coordinates.
773+
ModuleBase::GlobalFunc::OUT(ofs_running, "nspin", this->get_spin_mult());
772774

773775
// set cartesian k vectors.
774776
this->kvec_d2c(G);
@@ -802,7 +804,7 @@ void K_Vectors::mpi_k(std::ofstream& ofs_running)
802804

803805
Parallel_Common::bcast_bool(this->kd_done);
804806

805-
Parallel_Common::bcast_int(this->nspin);
807+
Parallel_Common::bcast_int(this->spin_mult);
806808

807809
Parallel_Common::bcast_int(this->nkstot);
808810

@@ -866,7 +868,7 @@ void K_Vectors::mpi_k(std::ofstream& ofs_running)
866868
Parallel_Common::bcast_double(kvec_c_full_aux.data(), this->nkstot_full * 3);
867869

868870
// process k point data in each processor
869-
this->renew(this->nks * this->nspin);
871+
this->renew(this->nks * this->spin_mult);
870872

871873
// distribute
872874
int k_index = 0;

source/source_cell/klist.h

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Inherits the spin-free common reciprocal-grid functionality
1515
* (mesh generation, coordinate conversion, weights, printing, star/IBZ
1616
* reduction primitive) from ModuleCell::ReciprocalGrid and adds the
17-
* spin expansion (isk, nspin doubling) and the k-point IBZ logic.
17+
* spin expansion (isk, spin-multiplicity doubling) and the k-point IBZ logic.
1818
*/
1919
class K_Vectors : public ModuleCell::ReciprocalGrid
2020
{
@@ -92,9 +92,11 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
9292
return this->k_nkstot;
9393
}
9494

95-
int get_nspin() const
95+
/// @brief Spin multiplicity of the k-point list: 1 (no doubling, also for
96+
/// non-collinear nspin=4) or 2 (LSDA, k points split into up/down).
97+
int get_spin_mult() const
9698
{
97-
return this->nspin;
99+
return this->spin_mult;
98100
}
99101

100102
std::string get_k_kword() const
@@ -117,11 +119,6 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
117119
this->nkstot_full = value;
118120
}
119121

120-
void set_nspin(int value)
121-
{
122-
this->nspin = value;
123-
}
124-
125122
bool get_is_mp() const
126123
{
127124
return is_mp;
@@ -153,21 +150,23 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
153150
std::ofstream& ofs_running);
154151

155152
/**
156-
* @brief Sets up the k-points after a volume change.
153+
* @brief Updates the k-points after a volume change.
157154
*
158-
* Sets the number of spins, converts the direct coordinates (which are
159-
* kept across the volume change) to the new Cartesian coordinates using
160-
* the new reciprocal lattice, prints the resulting table, and marks both
161-
* coordinate sets as up to date.
155+
* Converts the direct coordinates (which are kept across the volume
156+
* change) to the new Cartesian coordinates using the new reciprocal
157+
* lattice, prints the resulting table, and marks both coordinate sets
158+
* as up to date. The spin multiplicity is not touched: it was fixed by
159+
* set() and never changes during a run.
162160
*
163-
* @param nspin_in The number of spins. 1 for non-spin-polarized
164-
* calculations and 2 for spin-polarized calculations.
165161
* @param G The new reciprocal lattice matrix.
166162
*/
167-
void set_after_vc(const int& nspin_in, const ModuleBase::Matrix3& G, std::ofstream& ofs_running);
163+
void set_after_vc(const ModuleBase::Matrix3& G, std::ofstream& ofs_running);
168164

169165
private:
170-
int nspin = 0; ///< number of spin states
166+
/// Spin multiplicity used to size the k-point list: 1 for input nspin 1
167+
/// or 4 (non-collinear k points are not doubled) and 2 for input nspin 2
168+
/// (LSDA up/down k points). This is NOT the physical nspin (1/2/4).
169+
int spin_mult = 0;
171170
double koffset[3] = {0.0}; ///< used only in automatic k-points
172171

173172
/**
@@ -182,10 +181,10 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
182181
*/
183182
void renew(const int& kpoint_number) override;
184183

185-
/// @brief Spin multiplicity used when generating the mesh (1/2 for nspin 1/2).
184+
/// @brief Spin multiplicity used when generating the mesh (1/2).
186185
int spin_factor() const override
187186
{
188-
return this->nspin;
187+
return this->spin_mult;
189188
}
190189

191190
/**

0 commit comments

Comments
 (0)