Skip to content

Commit cf5516c

Browse files
author
abacus_fixer
committed
refactor(rhog_io): inject warning stream, remove GlobalV dependency
- Add std::ostream* os_warning parameter to read_rhog() and write_rhog() (no default value; callers must pass explicitly) - Replace ModuleBase::WARNING/WARNING_QUIT with a local warn() helper that writes to the injected stream on rank 0 - Remove indirect GlobalV dependencies: drop TITLE, timer, WARNING, WARNING_QUIT calls; drop timer.h include - write_rhog: replace WARNING_QUIT with warn() + return false (same flow) - Update all call sites to pass &GlobalV::ofs_warning: - charge_init.cpp (2 read_rhog calls) - esolver_fp.cpp (2 write_rhog calls) - read_rhog_test.cpp (4 read_rhog calls) - read_rhog_test.cpp main(): replace GlobalV parallel vars with locals
1 parent 5dcb8d5 commit cf5516c

5 files changed

Lines changed: 47 additions & 37 deletions

File tree

source/source_esolver/esolver_fp.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter, bool&
249249
this->inp_->nspin,
250250
ucell.GT,
251251
this->chr.rhog_save,
252-
pw_world);
252+
pw_world,
253+
&GlobalV::ofs_warning);
253254
}
254255

255256
if (XC_Functional::get_ked_flag())
@@ -269,7 +270,8 @@ void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter, bool&
269270
this->inp_->nspin,
270271
ucell.GT,
271272
kin_g.data(),
272-
pw_world);
273+
pw_world,
274+
&GlobalV::ofs_warning);
273275
}
274276
}
275277
}

source/source_estate/module_charge/charge_init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void Charge::init_rho(const UnitCell& ucell,
5555
binary << PARAM.globalv.global_readin_dir << PARAM.inp.suffix + "-CHARGE-DENSITY.restart";
5656
// Temporary bridge: use factory until ParaCollection is wired into driver.
5757
Parallel::ParaWorld pw_world = Parallel::make_pw_world();
58-
if (ModuleIO::read_rhog(binary.str(), rhopw, nspin, rhog, pw_world))
58+
if (ModuleIO::read_rhog(binary.str(), rhopw, nspin, rhog, pw_world, &GlobalV::ofs_warning))
5959
{
6060
GlobalV::ofs_running << " Read electron density from file: " << binary.str() << std::endl;
6161
for (int is = 0; is < nspin; ++is)
@@ -152,7 +152,7 @@ void Charge::init_rho(const UnitCell& ucell,
152152

153153
std::stringstream binary;
154154
binary << PARAM.globalv.global_readin_dir << PARAM.inp.suffix + "-TAU-DENSITY.restart";
155-
if (ModuleIO::read_rhog(binary.str(), rhopw, nspin, kin_g.data(), pw_world))
155+
if (ModuleIO::read_rhog(binary.str(), rhopw, nspin, kin_g.data(), pw_world, &GlobalV::ofs_warning))
156156
{
157157
GlobalV::ofs_running << " Read in the kinetic energy density: " << binary.str() << std::endl;
158158
for (int is = 0; is < nspin; ++is)

source/source_io/module_chgpot/rhog_io.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
#include "source_base/module_out/binstream.h"
22
#include "source_base/global_function.h"
3-
#include "source_base/timer.h"
43
#include "source_base/vector3.h"
54
#include "source_base/module_parallel/para_mpi_func.h"
65
#include "rhog_io.h"
76
#include <numeric>
87
#include <unistd.h>
98

9+
namespace
10+
{
11+
inline void warn(std::ostream* os,
12+
const Parallel::ParaWorld& pw_world,
13+
const std::string& file,
14+
const std::string& desc)
15+
{
16+
if (pw_world.rank() == 0 && os != nullptr)
17+
{
18+
*os << " " << file << " warning : " << desc << std::endl;
19+
}
20+
}
21+
} // namespace
22+
1023
bool ModuleIO::read_rhog(const std::string& filename,
1124
const ModulePW::PW_Basis* pw_rhod,
1225
const int nspin,
1326
std::complex<double>** rhog,
14-
const Parallel::ParaWorld& pw_world)
27+
const Parallel::ParaWorld& pw_world,
28+
std::ostream* os_warning)
1529
{
16-
ModuleBase::TITLE("ModuleIO", "read_rhog");
17-
ModuleBase::timer::start("ModuleIO", "read_rhog");
18-
1930
const int nx = pw_rhod->nx;
2031
const int ny = pw_rhod->ny;
2132
const int nz = pw_rhod->nz;
@@ -41,8 +52,7 @@ bool ModuleIO::read_rhog(const std::string& filename,
4152

4253
if (error)
4354
{
44-
ModuleBase::WARNING("ModuleIO::read_rhog", "Can't open file " + filename);
45-
ModuleBase::timer::end("ModuleIO", "read_rhog");
55+
warn(os_warning, pw_world, "ModuleIO::read_rhog", "Can't open file " + filename);
4656
return false;
4757
}
4858

@@ -59,24 +69,23 @@ bool ModuleIO::read_rhog(const std::string& filename,
5969
}
6070
if (npwtot_in > pw_rhod->npwtot)
6171
{
62-
ModuleBase::WARNING("ModuleIO::read_rhog", "some planewaves in file are not used");
72+
warn(os_warning, pw_world, "ModuleIO::read_rhog", "some planewaves in file are not used");
6373
}
6474
else if (npwtot_in < pw_rhod->npwtot)
6575
{
66-
ModuleBase::WARNING("ModuleIO::read_rhog", "some planewaves in file are missing");
76+
warn(os_warning, pw_world, "ModuleIO::read_rhog", "some planewaves in file are missing");
6777
}
6878
if (nspin_in < nspin)
6979
{
70-
ModuleBase::WARNING("ModuleIO::read_rhog", "some spin channels in file are missing");
80+
warn(os_warning, pw_world, "ModuleIO::read_rhog", "some spin channels in file are missing");
7181
}
7282
}
7383

7484
Parallel::bcast_bool(error, pw_world);
7585

7686
if (error)
7787
{
78-
ModuleBase::WARNING("ModuleIO::read_rhog", "gamma_only read from file is inconsistent with INPUT");
79-
ModuleBase::timer::end("ModuleIO", "read_rhog");
88+
warn(os_warning, pw_world, "ModuleIO::read_rhog", "gamma_only read from file is inconsistent with INPUT");
8089
return false;
8190
}
8291

@@ -176,7 +185,6 @@ bool ModuleIO::read_rhog(const std::string& filename,
176185
{
177186
ifs.close();
178187
}
179-
ModuleBase::timer::end("ModuleIO", "read_rhog");
180188
return true;
181189
}
182190

@@ -186,11 +194,9 @@ bool ModuleIO::write_rhog(const std::string& fchg,
186194
const int nspin,
187195
const ModuleBase::Matrix3& GT,
188196
std::complex<double>** rhog,
189-
const Parallel::ParaWorld& pw_world)
197+
const Parallel::ParaWorld& pw_world,
198+
std::ostream* os_warning)
190199
{
191-
ModuleBase::TITLE("ModuleIO", "write_rhog");
192-
ModuleBase::timer::start("ModuleIO", "write_rhog");
193-
194200
// only rank 0 in the domain writes the header; all ranks cooperate
195201
// on sequential writes synchronized by barriers.
196202
const int irank = pw_world.rank();
@@ -210,8 +216,7 @@ bool ModuleIO::write_rhog(const std::string& fchg,
210216
ofs.open(fchg, std::ios::binary);
211217
if (!ofs)
212218
{
213-
ModuleBase::WARNING_QUIT("ModuleIO::write_rhog", "File I/O failure: cannot open file " + fchg);
214-
ModuleBase::timer::end("ModuleIO", "write_rhog");
219+
warn(os_warning, pw_world, "ModuleIO::write_rhog", "File I/O failure: cannot open file " + fchg);
215220
return false;
216221
}
217222
ofs.write(reinterpret_cast<char*>(&size), sizeof(size));
@@ -306,7 +311,6 @@ bool ModuleIO::write_rhog(const std::string& fchg,
306311
}
307312
Parallel::barrier(pw_world);
308313
}
309-
ModuleBase::timer::end("ModuleIO", "write_rhog");
310314
return true;
311315
}
312316

source/source_io/module_chgpot/rhog_io.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <string>
55
#include <cassert>
6+
#include <ostream>
67
#include "source_basis/module_pw/pw_basis.h"
78
#include "source_base/module_parallel/para_world.h"
89
/**
@@ -48,15 +49,17 @@ bool read_rhog(const std::string& filename,
4849
const ModulePW::PW_Basis* pw_rhod,
4950
const int nspin,
5051
std::complex<double>** rhog,
51-
const Parallel::ParaWorld& pw_world);
52+
const Parallel::ParaWorld& pw_world,
53+
std::ostream* os_warning);
5254

5355
bool write_rhog(const std::string& fchg,
5456
const bool gamma_only,
5557
const ModulePW::PW_Basis* pw_rho,
5658
const int nspin,
5759
const ModuleBase::Matrix3& GT,
5860
std::complex<double>** rhog,
59-
const Parallel::ParaWorld& pw_world);
61+
const Parallel::ParaWorld& pw_world,
62+
std::ostream* os_warning);
6063

6164
} // namespace ModuleIO
6265

source/source_io/test/read_rhog_test.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ TEST_F(ReadRhogTest, ReadRhog)
4141
rhopw.setuptransform();
4242
rhopw.collect_local_pw();
4343

44-
bool result = ModuleIO::read_rhog(filename, &rhopw, 1, rhog.data(), pw_world);
44+
bool result = ModuleIO::read_rhog(filename, &rhopw, 1, rhog.data(), pw_world, &GlobalV::ofs_warning)
4545

4646
EXPECT_TRUE(result);
4747
EXPECT_DOUBLE_EQ(rhog[0][0].real(), -1.0304462993299456e-05);
@@ -58,7 +58,7 @@ TEST_F(ReadRhogTest, NotFoundFile)
5858
std::string filename = "notfound.txt";
5959

6060
GlobalV::ofs_warning.open("test_read_rhog.txt");
61-
bool result = ModuleIO::read_rhog(filename, &rhopw, 1, rhog.data(), pw_world);
61+
bool result = ModuleIO::read_rhog(filename, &rhopw, 1, rhog.data(), pw_world, &GlobalV::ofs_warning)
6262
GlobalV::ofs_warning.close();
6363

6464
std::ifstream ifs_running("test_read_rhog.txt");
@@ -81,7 +81,7 @@ TEST_F(ReadRhogTest, InconsistentGammaOnly)
8181
rhopw.gamma_only = true;
8282

8383
GlobalV::ofs_warning.open("test_read_rhog.txt");
84-
bool result = ModuleIO::read_rhog(filename, &rhopw, 2, rhog.data(), pw_world);
84+
bool result = ModuleIO::read_rhog(filename, &rhopw, 2, rhog.data(), pw_world, &GlobalV::ofs_warning);
8585
GlobalV::ofs_warning.close();
8686

8787
std::ifstream ifs_running("test_read_rhog.txt");
@@ -107,7 +107,7 @@ TEST_F(ReadRhogTest, SomePWMissing)
107107
rhopw.npwtot = 2000;
108108

109109
GlobalV::ofs_warning.open("test_read_rhog.txt");
110-
bool result = ModuleIO::read_rhog(filename, &rhopw, 1, rhog.data(), pw_world);
110+
bool result = ModuleIO::read_rhog(filename, &rhopw, 1, rhog.data(), pw_world, &GlobalV::ofs_warning)
111111
GlobalV::ofs_warning.close();
112112

113113
std::ifstream ifs_running("test_read_rhog.txt");
@@ -126,13 +126,14 @@ TEST_F(ReadRhogTest, SomePWMissing)
126126
int main(int argc, char** argv)
127127
{
128128
#ifdef __MPI
129-
setupmpi(argc, argv, GlobalV::NPROC, GlobalV::MY_RANK);
130-
divide_pools(GlobalV::NPROC,
131-
GlobalV::MY_RANK,
132-
GlobalV::NPROC_IN_POOL,
133-
GlobalV::KPAR,
134-
GlobalV::MY_POOL,
135-
GlobalV::RANK_IN_POOL);
129+
int nproc = 1;
130+
int myrank = 0;
131+
int nproc_in_pool = 1;
132+
const int kpar = 1;
133+
int mypool = 0;
134+
int rank_in_pool = 0;
135+
setupmpi(argc, argv, nproc, myrank);
136+
divide_pools(nproc, myrank, nproc_in_pool, kpar, mypool, rank_in_pool);
136137
#endif
137138

138139
testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)