Skip to content

Commit d6d129d

Browse files
author
abacus_fixer
committed
refactor(source_cell): thread ofs_running explicitly through K_Vectors
K_Vectors::set() already receives a running-log stream parameter, but the internal call chain fell back to the GlobalV::ofs_running global. Thread the stream through the non-virtual members so the dependency is explicit end-to-end (repo rule: pass dependencies explicitly where practical): - read_kpoints, parse_kfile, set_kup_and_kdw, update_use_ibz, set_after_vc and mpi_k now take std::ofstream& ofs_running and use the parameter instead of the global (15 global reads removed); - set() passes its existing 'ofs' argument down; its external signature is unchanged (esolver_fp.cpp and tests already pass a stream to set); - Call sites updated mechanically: esolver_fp.cpp set_after_vc (1), klist_test.cpp read_kpoints (30) / set_kup_and_kdw (5) / update_use_ibz (1) / set_after_vc (2), klist_test_para.cpp (1), print_info_test.cpp (1); tests pass GlobalV::ofs_running explicitly - Left as-is (recorded): the virtual reduce_by_symmetry keeps its global reads (adding a stream would force an unused parameter onto the QList override), as do MY_RANK guards, MPI topology reads, GlobalV::ofs_warning paths, and ReciprocalGrid::build_star_ops (QList chain has no stream to pass) Verified: make -j 30 (0 errors); ctest MODULE_CELL_{klist,reciprocal_ grid,qlist}_test, klist_test_para1/para4 and MODULE_IO_print_info 6/6 passed.
1 parent c2a101b commit d6d129d

6 files changed

Lines changed: 76 additions & 72 deletions

File tree

source/source_cell/klist.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void K_Vectors::set(const UnitCell& ucell,
8383

8484
this->nspin = (this->nspin == 4) ? 1 : this->nspin;
8585

86-
bool read_succesfully = this->read_kpoints(ucell, k_file_name, gamma_only_local_, kspacing, kmesh_type_, koffset);
86+
bool read_succesfully = this->read_kpoints(ucell, k_file_name, gamma_only_local_, kspacing, kmesh_type_, koffset, ofs);
8787
#ifdef __MPI
8888
Parallel_Common::bcast_bool(read_succesfully);
8989
#endif
@@ -172,11 +172,11 @@ void K_Vectors::set(const UnitCell& ucell,
172172
nspin_in); // assign k points to several process pools
173173
#ifdef __MPI
174174
// distribute K point data to the corresponding process
175-
this->mpi_k();
175+
this->mpi_k(ofs);
176176
#endif
177177

178178
// set the k vectors for the up and down spin
179-
this->set_kup_and_kdw();
179+
this->set_kup_and_kdw(ofs);
180180

181181
// initialize ibz_index
182182
this->ibz_index.resize(this->nkstot_full);
@@ -212,7 +212,8 @@ bool K_Vectors::read_kpoints(const UnitCell& ucell,
212212
const bool gamma_only_local,
213213
const double kspacing[3],
214214
const std::string& kmesh_type,
215-
const double koffset[3])
215+
const double koffset[3],
216+
std::ofstream& ofs_running)
216217
{
217218
ModuleBase::TITLE("K_Vectors", "read_kpoints");
218219
if (GlobalV::MY_RANK != 0)
@@ -225,7 +226,7 @@ bool K_Vectors::read_kpoints(const UnitCell& ucell,
225226
this->generate_kfile(ucell, fn, gamma_only_local, kspacing, kmesh_type, koffset);
226227

227228
// 2. Read the KPT file and build the k-point list
228-
return this->parse_kfile(fn);
229+
return this->parse_kfile(fn, ofs_running);
229230
}
230231

231232
void K_Vectors::generate_kfile(const UnitCell& ucell,
@@ -279,7 +280,7 @@ void K_Vectors::generate_kfile(const UnitCell& ucell,
279280
}
280281

281282
// 2. Generate the K-point grid automatically according to the KPT file
282-
bool K_Vectors::parse_kfile(const std::string& fn)
283+
bool K_Vectors::parse_kfile(const std::string& fn, std::ofstream& ofs_running)
283284
{
284285
// 2.1 read the KPT file
285286
std::ifstream ifk(fn.c_str());
@@ -346,13 +347,13 @@ bool K_Vectors::parse_kfile(const std::string& fn)
346347
{
347348
is_mp = true;
348349
k_type = 0;
349-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "Input type of k points", "Monkhorst-Pack(Gamma)");
350+
ModuleBase::GlobalFunc::OUT(ofs_running, "Input type of k points", "Monkhorst-Pack(Gamma)");
350351
}
351352
else if (kword == "Monkhorst-Pack" || kword == "MP" || kword == "mp")
352353
{
353354
is_mp = true;
354355
k_type = 1;
355-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "Input type of k points", "Monkhorst-Pack");
356+
ModuleBase::GlobalFunc::OUT(ofs_running, "Input type of k points", "Monkhorst-Pack");
356357
}
357358
else
358359
{
@@ -436,7 +437,7 @@ bool K_Vectors::parse_kfile(const std::string& fn)
436437

437438
this->nkstot_full = this->nks = this->nkstot;
438439

439-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nkstot", nkstot);
440+
ModuleBase::GlobalFunc::OUT(ofs_running, "nkstot", nkstot);
440441
return true;
441442
} // END SUBROUTINE
442443

@@ -515,7 +516,8 @@ void K_Vectors::interpolate_k_between(std::ifstream& ifk, std::vector<ModuleBase
515516

516517
void K_Vectors::update_use_ibz(const int& nkstot_ibz,
517518
const std::vector<ModuleBase::Vector3<double>>& kvec_d_ibz,
518-
const std::vector<double>& wk_ibz)
519+
const std::vector<double>& wk_ibz,
520+
std::ofstream& ofs_running)
519521
{
520522
if (GlobalV::MY_RANK != 0) {
521523
return;
@@ -526,7 +528,7 @@ void K_Vectors::update_use_ibz(const int& nkstot_ibz,
526528
// update nkstot
527529
this->nks = this->nkstot = nkstot_ibz;
528530

529-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nkstot now", nkstot);
531+
ModuleBase::GlobalFunc::OUT(ofs_running, "nkstot now", nkstot);
530532

531533
this->kvec_d.resize(this->nkstot * nspin); // qianrui fix a bug 2021-7-13 for nspin=2 in set_kup_and_kdw()
532534

@@ -547,7 +549,7 @@ void K_Vectors::update_use_ibz(const int& nkstot_ibz,
547549
// This routine sets the k vectors for the up and down spin
548550
//----------------------------------------------------------
549551
// from set_kup_and_kdw.f90
550-
void K_Vectors::set_kup_and_kdw()
552+
void K_Vectors::set_kup_and_kdw(std::ofstream& ofs_running)
551553
{
552554
ModuleBase::TITLE("K_Vectors", "setup_kup_and_kdw");
553555

@@ -583,8 +585,8 @@ void K_Vectors::set_kup_and_kdw()
583585
this->nks *= 2;
584586
this->nkstot *= 2;
585587

586-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nks(nspin=2)", nks);
587-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nkstot(nspin=2)", nkstot);
588+
ModuleBase::GlobalFunc::OUT(ofs_running, "nks(nspin=2)", nks);
589+
ModuleBase::GlobalFunc::OUT(ofs_running, "nkstot(nspin=2)", nkstot);
588590
break;
589591
}
590592

@@ -756,17 +758,17 @@ void K_Vectors::reduce_by_symmetry(const UnitCell& ucell,
756758
// resize the kpoint container according to nkstot_ibz
757759
if (use_symm || this->get_is_mp())
758760
{
759-
this->update_use_ibz(nkstot_ibz, kvec_d_ibz, wk_ibz);
761+
this->update_use_ibz(nkstot_ibz, kvec_d_ibz, wk_ibz, GlobalV::ofs_running);
760762
}
761763

762764
return;
763765
}
764766

765-
void K_Vectors::set_after_vc(const int& nspin_in, const ModuleBase::Matrix3& G)
767+
void K_Vectors::set_after_vc(const int& nspin_in, const ModuleBase::Matrix3& G, std::ofstream& ofs_running)
766768
{
767-
GlobalV::ofs_running << "\n SETUP K-POINTS" << std::endl;
769+
ofs_running << "\n SETUP K-POINTS" << std::endl;
768770
this->set_nspin(nspin_in);
769-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nspin", this->get_nspin());
771+
ModuleBase::GlobalFunc::OUT(ofs_running, "nspin", this->get_nspin());
770772

771773
// set cartesian k vectors.
772774
this->kvec_d2c(G);
@@ -783,16 +785,16 @@ void K_Vectors::set_after_vc(const int& nspin_in, const ModuleBase::Matrix3& G)
783785
this->kvec_d[i].z,
784786
this->wk[i]);
785787
}
786-
GlobalV::ofs_running << table << std::endl;
788+
ofs_running << table << std::endl;
787789

788790
this->kd_done = true;
789791
this->kc_done = true;
790792

791-
this->print_klists(GlobalV::ofs_running);
793+
this->print_klists(ofs_running);
792794
}
793795

794796
#ifdef __MPI
795-
void K_Vectors::mpi_k()
797+
void K_Vectors::mpi_k(std::ofstream& ofs_running)
796798
{
797799
ModuleBase::TITLE("K_Vectors", "mpi_k");
798800

@@ -815,8 +817,8 @@ void K_Vectors::mpi_k()
815817

816818
this->nks = this->para_k.nks_pool[GlobalV::MY_POOL];
817819

818-
GlobalV::ofs_running << std::endl;
819-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "Number of k-points in this process", this->nks);
820+
ofs_running << std::endl;
821+
ModuleBase::GlobalFunc::OUT(ofs_running, "Number of k-points in this process", this->nks);
820822
int nks_minimum = this->nks;
821823

822824
Parallel_Reduce::reduce_min(nks_minimum);
@@ -827,7 +829,7 @@ void K_Vectors::mpi_k()
827829
}
828830
else
829831
{
830-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "Minimum distributed k-point number", nks_minimum);
832+
ModuleBase::GlobalFunc::OUT(ofs_running, "Minimum distributed k-point number", nks_minimum);
831833
}
832834

833835
std::vector<int> isk_aux(this->nkstot);

source/source_cell/klist.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
149149
*/
150150
void update_use_ibz(const int& nkstot_ibz,
151151
const std::vector<ModuleBase::Vector3<double>>& kvec_d_ibz,
152-
const std::vector<double>& wk_ibz);
152+
const std::vector<double>& wk_ibz,
153+
std::ofstream& ofs_running);
153154

154155
/**
155156
* @brief Sets up the k-points after a volume change.
@@ -163,7 +164,7 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
163164
* calculations and 2 for spin-polarized calculations.
164165
* @param G The new reciprocal lattice matrix.
165166
*/
166-
void set_after_vc(const int& nspin_in, const ModuleBase::Matrix3& G);
167+
void set_after_vc(const int& nspin_in, const ModuleBase::Matrix3& G, std::ofstream& ofs_running);
167168

168169
private:
169170
int nspin = 0; ///< number of spin states
@@ -234,7 +235,8 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
234235
const bool gamma_only_local,
235236
const double kspacing[3],
236237
const std::string& kmesh_type,
237-
const double koffset[3]); // return 0: something wrong.
238+
const double koffset[3],
239+
std::ofstream& ofs_running); // return 0: something wrong.
238240

239241
/**
240242
* @brief Overwrite the KPT file with an auto-generated mesh when requested.
@@ -269,7 +271,7 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
269271
* @return bool Returns true if the k-points are successfully read,
270272
* false otherwise.
271273
*/
272-
bool parse_kfile(const std::string& fn);
274+
bool parse_kfile(const std::string& fn, std::ofstream& ofs_running);
273275

274276
/**
275277
* @brief Adds k-points linearly between special points.
@@ -315,7 +317,7 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
315317
* @note The function also doubles the total number of k-points (nks and nkstot) for spin-polarized calculations.
316318
* @note The function prints the total number of k-points for spin-polarized calculations.
317319
*/
318-
void set_kup_and_kdw();
320+
void set_kup_and_kdw(std::ofstream& ofs_running);
319321

320322
/**
321323
* @brief Gets the global index of a k-point.
@@ -334,7 +336,7 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
334336
* @note Assumes nkstot > 0 and quits if some process ends up with
335337
* no k-points.
336338
*/
337-
void mpi_k();
339+
void mpi_k(std::ofstream& ofs_running);
338340
#endif
339341
};
340342
#endif // KVECT_H

0 commit comments

Comments
 (0)