Skip to content

Commit 1be8c20

Browse files
author
abacus_fixer
committed
refactor(rhog_io): remove PARAM dependency, pass nspin as parameter
- Add explicit nspin parameter to read_rhog() signature, making it consistent with write_rhog() which already has nspin - Remove #include of parameter.h and global_variable.h from rhog_io.cpp - No longer reads PARAM.inp.nspin anywhere - Update charge_init.cpp call sites to pass the existing local nspin var - Remove dead PARAM.input.nspin assignments and #define private public hack from read_rhog_test.cpp (rhog_io no longer consumes PARAM)
1 parent 2baca53 commit 1be8c20

4 files changed

Lines changed: 11 additions & 17 deletions

File tree

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, rhog, pw_world))
58+
if (ModuleIO::read_rhog(binary.str(), rhopw, nspin, rhog, pw_world))
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, kin_g.data(), pw_world))
155+
if (ModuleIO::read_rhog(binary.str(), rhopw, nspin, kin_g.data(), pw_world))
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: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "source_base/module_out/binstream.h"
22
#include "source_base/global_function.h"
3-
#include "source_io/module_parameter/parameter.h"
4-
#include "source_base/global_variable.h"
53
#include "source_base/timer.h"
64
#include "source_base/vector3.h"
75
#include "source_base/module_parallel/para_mpi_func.h"
@@ -11,6 +9,7 @@
119

1210
bool ModuleIO::read_rhog(const std::string& filename,
1311
const ModulePW::PW_Basis* pw_rhod,
12+
const int nspin,
1413
std::complex<double>** rhog,
1514
const Parallel::ParaWorld& pw_world)
1615
{
@@ -66,7 +65,7 @@ bool ModuleIO::read_rhog(const std::string& filename,
6665
{
6766
ModuleBase::WARNING("ModuleIO::read_rhog", "some planewaves in file are missing");
6867
}
69-
if (nspin_in < PARAM.inp.nspin)
68+
if (nspin_in < nspin)
7069
{
7170
ModuleBase::WARNING("ModuleIO::read_rhog", "some spin channels in file are missing");
7271
}
@@ -105,7 +104,7 @@ bool ModuleIO::read_rhog(const std::string& filename,
105104
Parallel::bcast_int(miller.data(), miller.size(), pw_world);
106105

107106
// set to zero
108-
for (int is = 0; is < PARAM.inp.nspin; ++is)
107+
for (int is = 0; is < nspin; ++is)
109108
{
110109
ModuleBase::GlobalFunc::ZEROS(rhog[is], pw_rhod->npw);
111110
}
@@ -162,7 +161,7 @@ bool ModuleIO::read_rhog(const std::string& filename,
162161
}
163162
}
164163

165-
if (nspin_in == 2 && PARAM.inp.nspin == 4 && is == 1)
164+
if (nspin_in == 2 && nspin == 4 && is == 1)
166165
{
167166
for (int ig = 0; ig < pw_rhod->npw; ++ig)
168167
{

source/source_io/module_chgpot/rhog_io.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace ModuleIO
4646

4747
bool read_rhog(const std::string& filename,
4848
const ModulePW::PW_Basis* pw_rhod,
49+
const int nspin,
4950
std::complex<double>** rhog,
5051
const Parallel::ParaWorld& pw_world);
5152

source/source_io/test/read_rhog_test.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "gmock/gmock.h"
22
#include "gtest/gtest.h"
3-
#define private public
4-
#include "source_io/module_parameter/parameter.h"
5-
#undef private
63
#include "source_io/module_chgpot/rhog_io.h"
74
#include "source_base/module_parallel/para_world.h"
85
#include "source_base/module_parallel/para_tag.h"
@@ -48,7 +45,6 @@ class ReadRhogTest : public ::testing::Test
4845
TEST_F(ReadRhogTest, ReadRhog)
4946
{
5047
std::string filename = "./support/charge-density.dat";
51-
PARAM.input.nspin = 1;
5248
#ifdef __MPI
5349
rhopw->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, MPI_COMM_WORLD);
5450
#endif
@@ -57,7 +53,7 @@ TEST_F(ReadRhogTest, ReadRhog)
5753
rhopw->setuptransform();
5854
rhopw->collect_local_pw();
5955

60-
bool result = ModuleIO::read_rhog(filename, rhopw, rhog, pw_world);
56+
bool result = ModuleIO::read_rhog(filename, rhopw, 1, rhog, pw_world);
6157

6258
EXPECT_TRUE(result);
6359
EXPECT_DOUBLE_EQ(rhog[0][0].real(), -1.0304462993299456e-05);
@@ -74,7 +70,7 @@ TEST_F(ReadRhogTest, NotFoundFile)
7470
std::string filename = "notfound.txt";
7571

7672
GlobalV::ofs_warning.open("test_read_rhog.txt");
77-
bool result = ModuleIO::read_rhog(filename, rhopw, rhog, pw_world);
73+
bool result = ModuleIO::read_rhog(filename, rhopw, 1, rhog, pw_world);
7874
GlobalV::ofs_warning.close();
7975

8076
std::ifstream ifs_running("test_read_rhog.txt");
@@ -94,11 +90,10 @@ TEST_F(ReadRhogTest, NotFoundFile)
9490
TEST_F(ReadRhogTest, InconsistentGammaOnly)
9591
{
9692
std::string filename = "./support/charge-density.dat";
97-
PARAM.input.nspin = 2;
9893
rhopw->gamma_only = true;
9994

10095
GlobalV::ofs_warning.open("test_read_rhog.txt");
101-
bool result = ModuleIO::read_rhog(filename, rhopw, rhog, pw_world);
96+
bool result = ModuleIO::read_rhog(filename, rhopw, 2, rhog, pw_world);
10297
GlobalV::ofs_warning.close();
10398

10499
std::ifstream ifs_running("test_read_rhog.txt");
@@ -121,11 +116,10 @@ TEST_F(ReadRhogTest, InconsistentGammaOnly)
121116
TEST_F(ReadRhogTest, SomePWMissing)
122117
{
123118
std::string filename = "./support/charge-density.dat";
124-
PARAM.input.nspin = 1;
125119
rhopw->npwtot = 2000;
126120

127121
GlobalV::ofs_warning.open("test_read_rhog.txt");
128-
bool result = ModuleIO::read_rhog(filename, rhopw, rhog, pw_world);
122+
bool result = ModuleIO::read_rhog(filename, rhopw, 1, rhog, pw_world);
129123
GlobalV::ofs_warning.close();
130124

131125
std::ifstream ifs_running("test_read_rhog.txt");

0 commit comments

Comments
 (0)