Skip to content

Commit 47beb63

Browse files
committed
fix: bug for init_wfc file when nspin = 2
1 parent bb765cc commit 47beb63

File tree

9 files changed

+54
-51
lines changed

9 files changed

+54
-51
lines changed

source/module_elecstate/elecstate_print.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,20 @@ void ElecState::print_eigenvalue(std::ofstream& ofs)
177177
std::stringstream ss;
178178
if(PARAM.inp.out_alllog)
179179
{
180-
ss << PARAM.globalv.global_out_dir << "running_" << PARAM.inp.calculation << ".log";
180+
ss << PARAM.globalv.global_out_dir << "running_" << PARAM.inp.calculation << "_" << GlobalV::MY_RANK + 1 << ".log";
181181
}
182182
else
183183
{
184-
ss << PARAM.globalv.global_out_dir << "running_" << PARAM.inp.calculation << "_" << GlobalV::MY_RANK + 1 << ".log";
184+
ss << PARAM.globalv.global_out_dir << "running_" << PARAM.inp.calculation << ".log";
185185
}
186186
std::string filename = ss.str();
187187
std::vector<int> ngk_tot = this->klist->ngk;
188188

189189
#ifdef __MPI
190-
Parallel_Common::bcast_string(filename);
190+
if(!PARAM.inp.out_alllog)
191+
{
192+
Parallel_Common::bcast_string(filename);
193+
}
191194
MPI_Allreduce(MPI_IN_PLACE, ngk_tot.data(), nks, MPI_INT, MPI_SUM, POOL_WORLD);
192195
#endif
193196

@@ -214,7 +217,8 @@ void ElecState::print_eigenvalue(std::ofstream& ofs)
214217
#ifdef __MPI
215218
MPI_Barrier(MPI_COMM_WORLD);
216219
#endif
217-
if (GlobalV::MY_POOL == ip && GlobalV::RANK_IN_POOL == 0 && GlobalV::MY_STOGROUP == 0)
220+
bool ip_flag = PARAM.inp.out_alllog || (GlobalV::RANK_IN_POOL == 0 && GlobalV::MY_STOGROUP == 0);
221+
if (GlobalV::MY_POOL == ip && ip_flag)
218222
{
219223
const int start_ik = nks_np * is;
220224
const int end_ik = nks_np * (is + 1);
@@ -223,7 +227,7 @@ void ElecState::print_eigenvalue(std::ofstream& ofs)
223227
std::ofstream ofs_eig(filename.c_str(), std::ios::app);
224228
ofs_eig << std::setprecision(5);
225229
ofs_eig << std::setiosflags(std::ios::showpoint);
226-
ofs_eig << " " << this->klist->ik2iktot[ik] + 1 - is * nkstot_np << "/" << nks_np
230+
ofs_eig << " " << this->klist->ik2iktot[ik] + 1 - is * nkstot_np << "/" << nkstot_np
227231
<< " kpoint (Cartesian) = " << this->klist->kvec_c[ik].x << " " << this->klist->kvec_c[ik].y
228232
<< " " << this->klist->kvec_c[ik].z << " (" << ngk_tot[ik] << " pws)" << std::endl;
229233

@@ -239,6 +243,9 @@ void ElecState::print_eigenvalue(std::ofstream& ofs)
239243
}
240244
}
241245
}
246+
#ifdef __MPI
247+
MPI_Barrier(MPI_COMM_WORLD);
248+
#endif
242249
ofs.seekp(0, std::ios::end);
243250
}
244251
return;

source/module_elecstate/module_charge/charge_init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ void Charge::init_rho(elecstate::efermi& eferm_iout,
248248
const K_Vectors* kv = reinterpret_cast<const K_Vectors*>(klist);
249249
const int nkstot = kv->get_nkstot();
250250
const std::vector<int>& isk = kv->isk;
251-
ModuleIO::read_wfc_to_rho(pw_wfc, symm, nkstot, isk, *this);
251+
ModuleIO::read_wfc_to_rho(pw_wfc, symm, kv->ik2iktot.data(), nkstot, isk, *this);
252252
}
253253
}
254254

source/module_io/read_wfc_pw.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
void ModuleIO::read_wfc_pw(const std::string& filename,
1313
const ModulePW::PW_Basis_K* pw_wfc,
1414
const int& ik,
15+
const int& ikstot,
1516
const int& nkstot,
1617
ModuleBase::ComplexMatrix& wfc)
1718
{
@@ -60,28 +61,17 @@ void ModuleIO::read_wfc_pw(const std::string& filename,
6061
const int nz = pw_wfc->nz;
6162
const int npwk_max = pw_wfc->npwk_max;
6263

63-
int npwtot, ikstot, max_dim;
64+
int npwtot = 0;
65+
int max_dim = 0;
6466

65-
// get npwtot and ikstot
67+
// get npwtot
6668
#ifdef __MPI
6769
MPI_Allreduce(&pw_wfc->npwk[ik], &npwtot, 1, MPI_INT, MPI_SUM, POOL_WORLD);
6870
MPI_Allreduce(&npwk_max, &max_dim, 1, MPI_INT, MPI_MAX, POOL_WORLD);
69-
int nkp = nkstot / GlobalV::KPAR;
70-
int rem = nkstot % GlobalV::KPAR;
71-
if (GlobalV::MY_POOL < rem)
72-
{
73-
ikstot = GlobalV::MY_POOL * nkp + GlobalV::MY_POOL + ik;
74-
}
75-
else
76-
{
77-
ikstot = GlobalV::MY_POOL * nkp + rem + ik;
78-
}
7971
#else
8072
max_dim = npwk_max;
8173
npwtot = pw_wfc->npwk[ik];
82-
ikstot = ik;
8374
#endif
84-
8575
int npwtot_npol = npwtot * PARAM.globalv.npol;
8676

8777

source/module_io/read_wfc_pw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ namespace ModuleIO
1515
* @param filename filename containing wave functions
1616
* @param pw_wfc wave function FFT grids
1717
* @param ik k index
18+
* @param ikstot total index of k points
1819
* @param nkstot total number of k points
1920
* @param wfc wave functions
2021
*/
2122
void read_wfc_pw(const std::string& filedir,
2223
const ModulePW::PW_Basis_K* pw_wfc,
2324
const int& ik,
25+
const int& ikstot,
2426
const int& nkstot,
2527
ModuleBase::ComplexMatrix& wfc);
2628
} // namespace ModuleIO

source/module_io/read_wfc_to_rho.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
void ModuleIO::read_wfc_to_rho(const ModulePW::PW_Basis_K* pw_wfc,
1111
ModuleSymmetry::Symmetry& symm,
12+
const int* ik2iktot,
1213
const int nkstot,
1314
const std::vector<int>& isk,
1415
Charge& chg)
@@ -39,13 +40,29 @@ void ModuleIO::read_wfc_to_rho(const ModulePW::PW_Basis_K* pw_wfc,
3940
std::string filename = PARAM.globalv.global_readin_dir + "istate.info";
4041
std::ifstream ifs(filename);
4142
std::string useless;
42-
for (int ik_tot = 0; ik_tot < nkstot; ++ik_tot)
43+
if (PARAM.inp.nspin == 2)
4344
{
44-
ifs >> useless;
45-
getline(ifs, useless);
46-
for(int ib = 0; ib < nbands; ++ib)
45+
const int nkstot_np = nkstot / 2;
46+
for (int iktot_np = 0; iktot_np < nkstot_np; ++iktot_np)
4747
{
48-
ifs >> useless >> useless >> wg_tmp(ik_tot, ib);
48+
ifs >> useless;
49+
getline(ifs, useless);
50+
for (int ib = 0; ib < nbands; ++ib)
51+
{
52+
ifs >> useless >> useless >> wg_tmp(iktot_np, ib) >> useless >> wg_tmp(iktot_np + nkstot_np, ib);
53+
}
54+
}
55+
}
56+
else
57+
{
58+
for (int ik_tot = 0; ik_tot < nkstot; ++ik_tot)
59+
{
60+
ifs >> useless;
61+
getline(ifs, useless);
62+
for (int ib = 0; ib < nbands; ++ib)
63+
{
64+
ifs >> useless >> useless >> wg_tmp(ik_tot, ib);
65+
}
4966
}
5067
}
5168
}
@@ -54,31 +71,17 @@ void ModuleIO::read_wfc_to_rho(const ModulePW::PW_Basis_K* pw_wfc,
5471
MPI_Bcast(wg_tmp.c, nkstot * nbands, MPI_DOUBLE, 0, MPI_COMM_WORLD);
5572
#endif
5673

57-
auto get_ikstot = [&](int ik) {
58-
int nkp = nkstot / kpar;
59-
int rem = nkstot % kpar;
60-
int ikstot;
61-
if (my_pool < rem)
62-
{
63-
ikstot = my_pool * nkp + my_pool + ik;
64-
}
65-
else
66-
{
67-
ikstot = my_pool * nkp + rem + ik;
68-
}
69-
return ikstot;
70-
};
7174
for (int ik = 0; ik < pw_wfc->nks; ++ik)
7275
{
7376
int is = 0;
7477
if (nspin == 2)
7578
{
7679
is = isk[ik];
7780
}
78-
const int ikstot = get_ikstot(ik);
7981
std::stringstream filename;
82+
const int ikstot = ik2iktot[ik];
8083
filename << PARAM.globalv.global_readin_dir << "WAVEFUNC" << ikstot + 1 << ".dat";
81-
ModuleIO::read_wfc_pw(filename.str(), pw_wfc, ik, nkstot, wfc_tmp);
84+
ModuleIO::read_wfc_pw(filename.str(), pw_wfc, ik, ikstot, nkstot, wfc_tmp);
8285
if (PARAM.inp.nspin == 4)
8386
{
8487
std::vector<std::complex<double>> rho_tmp2(nrxx);

source/module_io/read_wfc_to_rho.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace ModuleIO
1919
*/
2020
void read_wfc_to_rho(const ModulePW::PW_Basis_K* pw_wfc,
2121
ModuleSymmetry::Symmetry& symm,
22+
const int* ik2iktot,
2223
const int nkstot,
2324
const std::vector<int>& isk,
2425
Charge& chg);

source/module_io/test/read_wfc_pw_test.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ TEST_F(ReadWfcPwTest, ReadWfcPw)
5555
PARAM.input.nbands = 8;
5656
const int nbasis = wfcpw->npwk[0];
5757
ModuleBase::ComplexMatrix wfcatom(PARAM.input.nbands, nbasis);
58-
ModuleIO::read_wfc_pw(filename, wfcpw, 0, nkstot, wfcatom);
58+
ModuleIO::read_wfc_pw(filename, wfcpw, 0, 0, nkstot, wfcatom);
5959

6060
if (GlobalV::NPROC_IN_POOL == 1)
6161
{
@@ -126,22 +126,22 @@ TEST_F(ReadWfcPwTest, NotFoundFile)
126126
// dat file
127127
std::string filename = "notfound.dat";
128128
testing::internal::CaptureStdout();
129-
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
129+
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
130130
std::string output = testing::internal::GetCapturedStdout();
131131
EXPECT_THAT(output,testing::HasSubstr("Can't open file notfound.dat"));
132132

133133

134134
// txt file
135135
filename = "notfound.txt";
136136
testing::internal::CaptureStdout();
137-
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
137+
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
138138
output = testing::internal::GetCapturedStdout();
139139
EXPECT_THAT(output,testing::HasSubstr("Can't open file notfound.txt"));
140140

141141
// other file
142142
filename = "notfound";
143143
testing::internal::CaptureStdout();
144-
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
144+
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
145145
output = testing::internal::GetCapturedStdout();
146146
EXPECT_THAT(output,testing::HasSubstr("Unknown file type und"));
147147
}
@@ -166,7 +166,7 @@ TEST_F(ReadWfcPwTest, InconsistentBands)
166166
const int nbasis = wfcpw->npwk[0];
167167
ModuleBase::ComplexMatrix wfcatom(PARAM.input.nbands, nbasis);
168168
testing::internal::CaptureStdout();
169-
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
169+
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
170170
std::string output = testing::internal::GetCapturedStdout();
171171
EXPECT_THAT(output, testing::HasSubstr("nbands_in = 8"));
172172
EXPECT_THAT(output, testing::HasSubstr("nbands = 4"));
@@ -198,7 +198,7 @@ TEST_F(ReadWfcPwTest, InconsistentKvec)
198198
const int nbasis = wfcpw->npwk[0];
199199
ModuleBase::ComplexMatrix wfcatom(PARAM.input.nbands, nbasis);
200200
testing::internal::CaptureStdout();
201-
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
201+
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
202202
std::string output = testing::internal::GetCapturedStdout();
203203
EXPECT_THAT(output, testing::HasSubstr("kvec_in[0] = 0 0 0"));
204204
EXPECT_THAT(output, testing::HasSubstr("kvec[0] = -1 1 -1"));
@@ -226,7 +226,7 @@ TEST_F(ReadWfcPwTest, InconsistentLat0)
226226
const int nbasis = wfcpw->npwk[0];
227227
ModuleBase::ComplexMatrix wfcatom(PARAM.input.nbands, nbasis);
228228
testing::internal::CaptureStdout();
229-
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
229+
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
230230
std::string output = testing::internal::GetCapturedStdout();
231231
EXPECT_THAT(output, testing::HasSubstr("lat0_in = 5.3233"));
232232
EXPECT_THAT(output, testing::HasSubstr("lat0 = 5"));
@@ -254,7 +254,7 @@ TEST_F(ReadWfcPwTest, InconsistentG)
254254
const int nbasis = wfcpw->npwk[0];
255255
ModuleBase::ComplexMatrix wfcatom(PARAM.input.nbands, nbasis);
256256
testing::internal::CaptureStdout();
257-
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
257+
EXPECT_EXIT(ModuleIO::read_wfc_pw(filename, wfcpw, 0, 0, nkstot, wfcatom), ::testing::ExitedWithCode(1), "");
258258
std::string output = testing::internal::GetCapturedStdout();
259259
EXPECT_THAT(output, testing::HasSubstr("G_in[0] = -1 -1 1\nG_in[1] = 1 1 1\nG_in[2] = -1 1 -1\n"));
260260
EXPECT_THAT(

source/module_io/test/read_wfc_to_rho_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ TEST_F(ReadWfcRhoTest, ReadWfcRho)
229229
ModuleIO::write_wfc_pw("WAVEFUNC", *psi, *kv, wfcpw);
230230

231231
// Read the wave functions to charge density
232-
ModuleIO::read_wfc_to_rho(wfcpw, symm, nkstot, kv->isk, chg);
232+
ModuleIO::read_wfc_to_rho(wfcpw, symm, kv->ik2iktot.data(), nkstot, kv->isk, chg);
233233

234234
// compare the charge density
235235
for (int ir = 0; ir < rhopw->nrxx; ++ir)

source/module_psi/psi_initializer_file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void psi_initializer_file<T>::init_psig(T* psig, const int& ik)
2929
std::stringstream filename;
3030
int ik_tot = this->p_kv->ik2iktot[ik];
3131
filename << PARAM.globalv.global_readin_dir << "WAVEFUNC" << ik_tot + 1 << ".dat";
32-
ModuleIO::read_wfc_pw(filename.str(), this->pw_wfc_, ik, nkstot, wfcatom);
32+
ModuleIO::read_wfc_pw(filename.str(), this->pw_wfc_, ik, ik_tot, nkstot, wfcatom);
3333

3434
assert(this->nbands_start_ <= wfcatom.nr);
3535
for (int ib = 0; ib < this->nbands_start_; ib++)

0 commit comments

Comments
 (0)