Skip to content

Commit 7494fd7

Browse files
author
abacus_fixer
committed
refactor(klist): rename nkstot_full to nkstot_nospin and thread ofs_warning through klist API
Three intertwined changes that must ship together to keep the build green: 1. Rename `nkstot_full` -> `nkstot_nospin` (member, getter, setter) across all consumers (source_cell, source_lcao/module_ri, module_bse, module_lr, module_rdmft, source_pw/module_pwdft, source_io). The old name was ambiguous: EXX/RI/LR code already treats it as "physical k-point count WITHOUT spin multiplicity" (see e.g. ri_2d_comm.hpp: `ik_full + is_k * nkstot_full`), so the new name makes the convention explicit. Comments in reciprocal_grid.h now document both: - nkstot: INCLUDING spin multiplicity (after set_kup_and_kdw) - nkstot_nospin: physical k-points, WITHOUT spin multiplicity 2. Thread `std::ostream& ofs_warning` through K_Vectors::set, setup_line_kpoints, set_both_kvec, update_use_ibz, reduce_by_symmetry, handle_symmetry_mismatch, and read_mp_mesh so they no longer read `GlobalV::ofs_warning` directly. Internal ModuleBase::WARNING calls are replaced with `ofs_warning <<` to keep behavior consistent. The klist module itself no longer references GlobalV::ofs_warning; outer callers (esolver_fp, esolver_gets, esolver_lr_lcao_tddft, deepks_test_prep) pass `GlobalV::ofs_warning` at the boundary. 3. Thread `int my_rank` through update_use_ibz, reduce_by_symmetry, handle_symmetry_mismatch to remove `GlobalV::MY_RANK` reads inside the klist module. Also included: dftu_base static-member cleanup (mark_occ_mat_initialized -> this->occ_mat_initialized = true) which was already in the working tree and is consistent with the project rule "no static members in dftu_base.h". Behavior is unchanged. Build passes with `cmake --build build_max_para_test -j 30`. All 12 previously-failing 01_PW tests (003, 007, 028, 030, 034, 036, 037, 050, 055, 063, 078, 079, 097x2, 206, 815, scf_deltaspin2) pass with identical etot; valgrind reports no ABACUS-side Invalid write.
1 parent cab4bca commit 7494fd7

35 files changed

Lines changed: 224 additions & 186 deletions

source/source_cell/klist.cpp

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void K_Vectors::set(const UnitCell& ucell,
1818
const ModuleBase::Matrix3& reciprocal_vec,
1919
const ModuleBase::Matrix3& latvec,
2020
std::ofstream& ofs,
21+
std::ofstream& ofs_warning,
2122
const bool use_ibz,
2223
const std::string& global_out_dir,
2324
const bool gamma_only_local,
@@ -65,7 +66,7 @@ void K_Vectors::set(const UnitCell& ucell,
6566
kmesh_type_,
6667
koffset,
6768
ofs,
68-
GlobalV::ofs_warning,
69+
ofs_warning,
6970
my_rank);
7071
#ifdef __MPI
7172
Parallel_Common::bcast_bool(read_succesfully);
@@ -82,7 +83,7 @@ void K_Vectors::set(const UnitCell& ucell,
8283
// complement the Cartesian coordinates of the full k-point list
8384
KListIO::fill_full_kvec(this->kc_done,
8485
this->kd_done,
85-
this->nkstot_full,
86+
this->nkstot_nospin,
8687
reciprocal_vec,
8788
this->kvec_c,
8889
this->kvec_d,
@@ -95,21 +96,21 @@ void K_Vectors::set(const UnitCell& ucell,
9596
{
9697
bool match = true;
9798
// calculate kpoints in IBZ and reduce kpoints according to symmetry
98-
this->reduce_by_symmetry(ucell, symm, ModuleSymmetry::Symmetry::symm_flag, skpt1, match);
99+
this->reduce_by_symmetry(ucell, symm, ModuleSymmetry::Symmetry::symm_flag, skpt1, match, my_rank, ofs);
99100
#ifdef __MPI
100101
Parallel_Common::bcast_bool(match);
101102
#endif
102103
if (!match)
103104
{
104-
this->handle_symmetry_mismatch(ucell, symm, skpt1, match);
105+
this->handle_symmetry_mismatch(ucell, symm, skpt1, match, my_rank, ofs);
105106
}
106107
}
107108

108109
// (3)
109110
// Improve k point information
110111

111112
// Complement the coordinates of k point
112-
this->set_both_kvec(reciprocal_vec, latvec, skpt2, ofs);
113+
this->set_both_kvec(reciprocal_vec, latvec, skpt2, ofs, ofs_warning);
113114

114115
if (my_rank == 0)
115116
{
@@ -143,8 +144,8 @@ void K_Vectors::set(const UnitCell& ucell,
143144
this->set_kup_and_kdw(ofs);
144145

145146
// initialize ibz_index
146-
this->ibz_index.resize(this->nkstot_full);
147-
for (int ik = 0; ik < this->nkstot_full; ik++)
147+
this->ibz_index.resize(this->nkstot_nospin);
148+
for (int ik = 0; ik < this->nkstot_nospin; ik++)
148149
{
149150
this->ibz_index[ik] = ik;
150151
}
@@ -167,7 +168,9 @@ void K_Vectors::set(const UnitCell& ucell,
167168
void K_Vectors::handle_symmetry_mismatch(const UnitCell& ucell,
168169
const ModuleSymmetry::Symmetry& symm,
169170
std::string& skpt,
170-
bool& match)
171+
bool& match,
172+
const int my_rank,
173+
std::ofstream& ofs)
171174
{
172175
std::cout << "Optimized lattice type of reciprocal lattice cannot match the optimized real lattice. "
173176
<< std::endl;
@@ -178,7 +181,7 @@ void K_Vectors::handle_symmetry_mismatch(const UnitCell& ucell,
178181
std::cout << "Automatically set symmetry to 0 and continue ..." << std::endl;
179182
ModuleSymmetry::Symmetry::symm_flag = 0;
180183
match = true;
181-
this->reduce_by_symmetry(ucell, symm, ModuleSymmetry::Symmetry::symm_flag, skpt, match);
184+
this->reduce_by_symmetry(ucell, symm, ModuleSymmetry::Symmetry::symm_flag, skpt, match, my_rank, ofs);
182185
}
183186
else
184187
{
@@ -285,7 +288,7 @@ bool K_Vectors::parse_kfile(const std::string& fn, std::ofstream& ofs_running, s
285288
return false;
286289
}
287290

288-
this->nkstot_full = this->nks = this->nkstot;
291+
this->nkstot_nospin = this->nks = this->nkstot;
289292

290293
ModuleBase::GlobalFunc::OUT(ofs_running, "nkstot", nkstot);
291294
return true;
@@ -322,7 +325,8 @@ bool K_Vectors::read_mp_mesh(std::ifstream& ifk,
322325
this->koffset[2] = 0;
323326
if (!(ifk >> this->koffset[0] >> this->koffset[1] >> this->koffset[2]))
324327
{
325-
ModuleBase::WARNING("K_Vectors::read_kpoints", "Missing k-point offsets in the k-points file.");
328+
ofs_warning << " K_Vectors::read_kpoints warning : "
329+
<< "Missing k-point offsets in the k-points file." << std::endl;
326330
}
327331

328332
this->Monkhorst_Pack(nmp, this->koffset, k_type);
@@ -347,11 +351,11 @@ bool K_Vectors::read_listed_kpoints(std::ifstream& ifk, const std::string& kword
347351
}
348352
if (kword == "Line_Cartesian")
349353
{
350-
return this->setup_line_kpoints(ifk, this->kvec_c, true);
354+
return this->setup_line_kpoints(ifk, this->kvec_c, true, ofs_warning);
351355
}
352356
if (kword == "Line_Direct" || kword == "L" || kword == "Line")
353357
{
354-
return this->setup_line_kpoints(ifk, this->kvec_d, false);
358+
return this->setup_line_kpoints(ifk, this->kvec_d, false, ofs_warning);
355359
}
356360

357361
ofs_warning << " Error : neither Cartesian nor Direct kpoint." << std::endl;
@@ -360,12 +364,14 @@ bool K_Vectors::read_listed_kpoints(std::ifstream& ifk, const std::string& kword
360364

361365
bool K_Vectors::setup_line_kpoints(std::ifstream& ifk,
362366
std::vector<ModuleBase::Vector3<double>>& kvec,
363-
const bool cartesian)
367+
const bool cartesian,
368+
std::ofstream& ofs_warning)
364369
{
365370
if (ModuleSymmetry::Symmetry::symm_flag == 1)
366371
{
367-
ModuleBase::WARNING("K_Vectors::read_kpoints",
368-
"Line mode of k-points is open, please set symmetry to 0 or -1.");
372+
ofs_warning << " K_Vectors::read_kpoints warning : "
373+
<< "Line mode of k-points is open, please set symmetry to 0 or -1."
374+
<< std::endl;
369375
return false;
370376
}
371377

@@ -403,9 +409,10 @@ void K_Vectors::interpolate_k_between(std::ifstream& ifk, std::vector<ModuleBase
403409
void K_Vectors::update_use_ibz(const int& nkstot_ibz,
404410
const std::vector<ModuleBase::Vector3<double>>& kvec_d_ibz,
405411
const std::vector<double>& wk_ibz,
406-
std::ofstream& ofs_running)
412+
std::ofstream& ofs_running,
413+
const int my_rank)
407414
{
408-
if (GlobalV::MY_RANK != 0) {
415+
if (my_rank != 0) {
409416
return;
410417
}
411418
ModuleBase::TITLE("K_Vectors", "update_use_ibz");
@@ -461,9 +468,11 @@ void K_Vectors::reduce_by_symmetry(const UnitCell& ucell,
461468
const ModuleSymmetry::Symmetry& symm,
462469
bool use_symm,
463470
std::string& skpt,
464-
bool& match)
471+
bool& match,
472+
const int my_rank,
473+
std::ofstream& ofs_running)
465474
{
466-
if (GlobalV::MY_RANK != 0)
475+
if (my_rank != 0)
467476
{
468477
return;
469478
}
@@ -532,14 +541,14 @@ void K_Vectors::reduce_by_symmetry(const UnitCell& ucell,
532541

533542
// output in kpoints file
534543
skpt = KListIO::ibz_kpt_table(this->nkstot, this->kvec_d, this->ibz_index, kvec_d_ibz);
535-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "Number of irreducible k-points", nkstot_ibz);
544+
ModuleBase::GlobalFunc::OUT(ofs_running, "Number of irreducible k-points", nkstot_ibz);
536545

537-
GlobalV::ofs_running << KListIO::ibz_wk_table(nkstot_ibz, kvec_d_ibz, wk_ibz, ibz2bz) << std::endl;
546+
ofs_running << KListIO::ibz_wk_table(nkstot_ibz, kvec_d_ibz, wk_ibz, ibz2bz) << std::endl;
538547

539548
// resize the kpoint container according to nkstot_ibz
540549
if (use_symm || this->get_is_mp())
541550
{
542-
this->update_use_ibz(nkstot_ibz, kvec_d_ibz, wk_ibz, GlobalV::ofs_running);
551+
this->update_use_ibz(nkstot_ibz, kvec_d_ibz, wk_ibz, ofs_running, my_rank);
543552
}
544553

545554
return;
@@ -588,7 +597,7 @@ void K_Vectors::mpi_k(std::ofstream& ofs_running, const int my_rank, const int m
588597

589598
Parallel_Common::bcast_int(this->nkstot);
590599

591-
Parallel_Common::bcast_int(this->nkstot_full);
600+
Parallel_Common::bcast_int(this->nkstot_nospin);
592601

593602
Parallel_Common::bcast_int(this->nmp, 3);
594603

@@ -618,7 +627,7 @@ void K_Vectors::mpi_k(std::ofstream& ofs_running, const int my_rank, const int m
618627
std::vector<double> wk_aux(this->nkstot);
619628
std::vector<double> kvec_c_aux(this->nkstot * 3);
620629
std::vector<double> kvec_d_aux(this->nkstot * 3);
621-
std::vector<double> kvec_c_full_aux(this->nkstot_full * 3);
630+
std::vector<double> kvec_c_full_aux(this->nkstot_nospin * 3);
622631

623632
// collect and process in rank 0
624633
if (my_rank == 0)
@@ -642,7 +651,7 @@ void K_Vectors::mpi_k(std::ofstream& ofs_running, const int my_rank, const int m
642651
Parallel_Common::bcast_double(wk_aux.data(), this->nkstot);
643652
Parallel_Common::bcast_double(kvec_c_aux.data(), this->nkstot * 3);
644653
Parallel_Common::bcast_double(kvec_d_aux.data(), this->nkstot * 3);
645-
Parallel_Common::bcast_double(kvec_c_full_aux.data(), this->nkstot_full * 3);
654+
Parallel_Common::bcast_double(kvec_c_full_aux.data(), this->nkstot_nospin * 3);
646655

647656
// process k point data in each processor
648657
this->renew(this->nks * this->spin_mult);

source/source_cell/klist.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
6060
const ModuleBase::Matrix3& reciprocal_vec,
6161
const ModuleBase::Matrix3& latvec,
6262
std::ofstream& ofs,
63+
std::ofstream& ofs_warning,
6364
const bool use_ibz,
6465
const std::string& global_out_dir,
6566
const bool gamma_only_local,
@@ -77,9 +78,9 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
7778
return this->nkstot;
7879
}
7980

80-
int get_nkstot_full() const
81+
int get_nkstot_nospin() const
8182
{
82-
return this->nkstot_full;
83+
return this->nkstot_nospin;
8384
}
8485

8586
double get_koffset(const int i) const
@@ -114,9 +115,9 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
114115
this->nkstot = value;
115116
}
116117

117-
void set_nkstot_full(int value)
118+
void set_nkstot_nospin(int value)
118119
{
119-
this->nkstot_full = value;
120+
this->nkstot_nospin = value;
120121
}
121122

122123
bool get_is_mp() const
@@ -147,7 +148,8 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
147148
void update_use_ibz(const int& nkstot_ibz,
148149
const std::vector<ModuleBase::Vector3<double>>& kvec_d_ibz,
149150
const std::vector<double>& wk_ibz,
150-
std::ofstream& ofs_running);
151+
std::ofstream& ofs_running,
152+
const int my_rank);
151153

152154
/**
153155
* @brief Updates the k-points after a volume change.
@@ -206,7 +208,9 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
206208
const ModuleSymmetry::Symmetry& symm,
207209
bool use_symm,
208210
std::string& skpt,
209-
bool& match) override;
211+
bool& match,
212+
const int my_rank,
213+
std::ofstream& ofs_running) override;
210214

211215
/// @brief step 1 : generate kpoints
212216

@@ -296,10 +300,12 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
296300
* @param ifk stream to read the special points from
297301
* @param kvec target coordinate container (kvec_c or kvec_d)
298302
* @param cartesian true for Line_Cartesian, false for Line_Direct
303+
* @param ofs_warning warning-log stream for error messages
299304
*/
300305
bool setup_line_kpoints(std::ifstream& ifk,
301306
std::vector<ModuleBase::Vector3<double>>& kvec,
302-
const bool cartesian);
307+
const bool cartesian,
308+
std::ofstream& ofs_warning);
303309

304310
/**
305311
* @brief Handle a reciprocal/real lattice Bravais-type mismatch after
@@ -317,7 +323,9 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
317323
void handle_symmetry_mismatch(const UnitCell& ucell,
318324
const ModuleSymmetry::Symmetry& symm,
319325
std::string& skpt,
320-
bool& match);
326+
bool& match,
327+
const int my_rank,
328+
std::ofstream& ofs);
321329

322330
/**
323331
* @brief Adds k-points linearly between special points.

source/source_cell/klist_io.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,22 +344,22 @@ void bcast_kstars(std::vector<std::map<int, ModuleBase::Vector3<double>>>& kstar
344344

345345
void fill_full_kvec(const bool kc_done,
346346
const bool kd_done,
347-
const int nkstot_full,
347+
const int nkstot_nospin,
348348
const ModuleBase::Matrix3& reciprocal_vec,
349349
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
350350
const std::vector<ModuleBase::Vector3<double>>& kvec_d,
351351
std::vector<ModuleBase::Vector3<double>>& kvec_c_full)
352352
{
353353
if (!kc_done && kd_done)
354354
{
355-
for (int ik = 0; ik < nkstot_full; ++ik)
355+
for (int ik = 0; ik < nkstot_nospin; ++ik)
356356
{
357357
kvec_c_full[ik] = kvec_d[ik] * reciprocal_vec;
358358
}
359359
}
360360
else if (kc_done && !kd_done)
361361
{
362-
for (int ik = 0; ik < nkstot_full; ++ik)
362+
for (int ik = 0; ik < nkstot_nospin; ++ik)
363363
{
364364
kvec_c_full[ik] = kvec_c[ik];
365365
}

source/source_cell/klist_io.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void unpack_kpts(const std::vector<int>& isk_aux,
127127
/// K_Vectors::set() before IBZ reduction.
128128
void fill_full_kvec(bool kc_done,
129129
bool kd_done,
130-
int nkstot_full,
130+
int nkstot_nospin,
131131
const ModuleBase::Matrix3& reciprocal_vec,
132132
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
133133
const std::vector<ModuleBase::Vector3<double>>& kvec_d,

source/source_cell/qlist.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ void QList::generate_mesh(UnitCell& ucell, ModuleSymmetry::Symmetry& symm,
3636
const double offset[3] = {0.0, 0.0, 0.0};
3737
this->Monkhorst_Pack(this->nmp, offset, 0);
3838

39-
this->nkstot_full = this->nkstot;
39+
this->nkstot_nospin = this->nkstot;
4040
this->nks = this->nkstot;
4141

4242
// Star reduction: always use symmetry, always include the -q partner.
4343
bool match = true;
4444
std::string skpt;
45-
this->reduce_by_symmetry(ucell, symm, true, skpt, match);
45+
this->reduce_by_symmetry(ucell, symm, true, skpt, match, GlobalV::MY_RANK, GlobalV::ofs_running);
4646
if (!match)
4747
{
4848
ModuleBase::WARNING("QList::generate_mesh",
4949
"Reciprocal lattice is incompatible with the real-space lattice. "
5050
"Falling back to the unreduced q-point mesh.");
51-
this->nkstot = this->nks = this->nkstot_full;
51+
this->nkstot = this->nks = this->nkstot_nospin;
5252
}
5353

5454
// weights sum to 1 (average over the full Brillouin zone)
@@ -189,7 +189,7 @@ void QList::read_from_file(const std::string& filename, UnitCell& ucell) {
189189
}
190190
}
191191

192-
this->nkstot_full = this->nks = this->nkstot;
192+
this->nkstot_nospin = this->nks = this->nkstot;
193193

194194
// complement the coordinates: fill the missing representation
195195
if (!this->kc_done && this->kd_done)
@@ -329,8 +329,12 @@ void QList::reduce_by_symmetry(const UnitCell& ucell,
329329
const ModuleSymmetry::Symmetry& symm,
330330
bool use_symm,
331331
std::string& skpt,
332-
bool& match) {
332+
bool& match,
333+
const int my_rank,
334+
std::ofstream& ofs_running) {
333335
(void)skpt;
336+
(void)my_rank;
337+
(void)ofs_running;
334338
// q-points are spin-free: build the point-group operations and always
335339
// double them by the time-reversal operation -q (no magnetic group).
336340
std::vector<ModuleBase::Matrix3> kgmatrix(48 * 2);

source/source_cell/qlist.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ class QList : public ModuleCell::ReciprocalGrid {
135135
const ModuleSymmetry::Symmetry& symm,
136136
bool use_symm,
137137
std::string& skpt,
138-
bool& match) override;
138+
bool& match,
139+
const int my_rank,
140+
std::ofstream& ofs_running) override;
139141

140142
private:
141143
std::vector<int> nirr_; ///< number of irreps for each q-point

0 commit comments

Comments
 (0)