Skip to content

Commit 616130f

Browse files
committed
remove PARAM.inp.out_app_flag in write_wfc_nao
1 parent 5e83ae5 commit 616130f

File tree

5 files changed

+136
-90
lines changed

5 files changed

+136
-90
lines changed

source/module_esolver/lcao_after_scf.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,18 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep, const
178178
//------------------------------------------------------------------
179179
if (elecstate::ElecStateLCAO<TK>::out_wfc_lcao && (istep % PARAM.inp.out_interval == 0))
180180
{
181-
ModuleIO::write_wfc_nao(elecstate::ElecStateLCAO<TK>::out_wfc_lcao,
182-
this->psi[0],
183-
this->pelec->ekb,
184-
this->pelec->wg,
185-
this->pelec->klist->kvec_c,
186-
this->pelec->klist->ik2iktot,
187-
this->pelec->klist->get_nkstot(),
188-
this->pv,
189-
PARAM.inp.nspin,
190-
istep);
191-
}
181+
ModuleIO::write_wfc_nao(elecstate::ElecStateLCAO<TK>::out_wfc_lcao,
182+
PARAM.inp.out_app_flag,
183+
this->psi[0],
184+
this->pelec->ekb,
185+
this->pelec->wg,
186+
this->pelec->klist->kvec_c,
187+
this->pelec->klist->ik2iktot,
188+
this->pelec->klist->get_nkstot(),
189+
this->pv,
190+
PARAM.inp.nspin,
191+
istep);
192+
}
192193

193194
//------------------------------------------------------------------
194195
//! 7) write DeePKS information in LCAO basis

source/module_io/test/write_wfc_nao_test.cpp

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,61 @@
1616

1717
TEST(GenWfcLcaoFnameTest, OutType1GammaOnlyOutAppFlagTrue)
1818
{
19-
int out_type = 1;
20-
bool gamma_only = true;
21-
bool out_app_flag = true;
22-
int ik = 0;
23-
int istep = 0;
24-
25-
std::string expected_output = "WFC_NAO_GAMMA1.txt";
26-
std::string result = ModuleIO::wfc_nao_gen_fname(out_type, gamma_only, out_app_flag, ik, istep);
19+
// output .txt file when out_type=1
20+
const int out_type = 1;
21+
const bool gamma_only = true;
22+
const bool out_app_flag = true;
23+
const int ik = 0;
24+
const std::vector<int> ik2iktot = {0};
25+
const int nkstot = 1;
26+
const int nspin = 1;
27+
// if out_app_flag = true, then the 'g' label will not show up
28+
const int istep = 0;
29+
30+
std::string expected_output = "wfs1k1_nao.txt";
31+
std::string result = ModuleIO::wfc_nao_gen_fname(out_type, gamma_only, out_app_flag, ik,
32+
ik2iktot, nkstot, nspin, istep);
2733

2834
EXPECT_EQ(result, expected_output);
2935
}
3036

3137
TEST(GenWfcLcaoFnameTest, OutType2GammaOnlyOutAppFlagFalse)
3238
{
33-
int out_type = 2;
34-
bool gamma_only = true;
35-
bool out_app_flag = false;
36-
int ik = 1;
37-
int istep = 2;
38-
39-
std::string expected_output = "WFC_NAO_GAMMA2_ION3.dat";
40-
std::string result = ModuleIO::wfc_nao_gen_fname(out_type, gamma_only, out_app_flag, ik, istep);
39+
// output .dat file when out_type=2
40+
const int out_type = 2;
41+
const bool gamma_only = true;
42+
// if out_app_flag = false, then the 'g' label appears
43+
const bool out_app_flag = false;
44+
const int ik = 1;
45+
const std::vector<int> ik2iktot = {0,1};
46+
const int nkstot = 2;
47+
const int nspin = 2;
48+
const int istep = 2;
49+
50+
std::string expected_output = "wfs2g3_nao.dat";
51+
std::string result = ModuleIO::wfc_nao_gen_fname(out_type, gamma_only, out_app_flag, ik,
52+
ik2iktot, nkstot, nspin, istep);
4153

4254
EXPECT_EQ(result, expected_output);
4355
}
4456

4557
TEST(GenWfcLcaoFnameTest, OutTypeInvalid)
4658
{
47-
int out_type = 3;
48-
bool gamma_only = false;
49-
bool out_app_flag = true;
50-
int ik = 2;
51-
int istep = 3;
52-
53-
std::string expected_output = "WFC_NAO_K3.txt";
59+
// a .txt is chosen if out_type is not 1 or 2
60+
const int out_type = 3;
61+
const bool gamma_only = false;
62+
const bool out_app_flag = true;
63+
const int ik = 2;
64+
const std::vector<int> ik2iktot = {0,1,2};
65+
const int nkstot = 3;
66+
const int nspin = 1;
67+
const int istep = 3;
68+
69+
std::string expected_output = "wfs1k3_nao.txt";
5470
// catch the screen output
5571
testing::internal::CaptureStdout();
56-
std::string result = ModuleIO::wfc_nao_gen_fname(out_type, gamma_only, out_app_flag, ik, istep);
72+
std::string result = ModuleIO::wfc_nao_gen_fname(out_type, gamma_only, out_app_flag, ik,
73+
ik2iktot, nkstot, nspin, istep);
5774
std::string output = testing::internal::GetCapturedStdout();
5875

5976
EXPECT_EQ(result, expected_output);
@@ -64,7 +81,8 @@ void read_bin(const std::string& name, std::vector<T>& data)
6481
{
6582
std::ifstream ifs(name, std::ios::binary);
6683
ifs.seekg(0, std::ios::beg);
67-
int nbands, nbasis;
84+
int nbands=0;
85+
int nbasis=0;
6886
if (std::is_same<T, std::complex<double>>::value)
6987
{
7088
ifs.ignore(sizeof(int));
@@ -92,6 +110,7 @@ class WriteWfcLcaoTest : public testing::Test
92110
ModuleBase::matrix ekb;
93111
ModuleBase::matrix wg;
94112
std::vector<ModuleBase::Vector3<double>> kvec_c;
113+
95114
std::vector<double> psi_init_double;
96115
std::vector<std::complex<double>> psi_init_complex;
97116
std::vector<double> psi_local_double;
@@ -100,12 +119,16 @@ class WriteWfcLcaoTest : public testing::Test
100119
int nk = 2;
101120
int nbands = 3;
102121
int nbasis = 4;
103-
int nbands_local;
104-
int nbasis_local;
122+
int nbands_local = 0;
123+
int nbasis_local = 0;
124+
125+
// mohan add 2025-05-11
126+
std::vector<int> ik2iktot = {0,1};
127+
int nkstot = 2;
128+
bool out_app_flag = true;
105129

106130
void SetUp() override
107131
{
108-
PARAM.input.out_app_flag = true;
109132
ekb.create(nk, nbands); // in this test the value of ekb and wg is not important and not used.
110133
wg.create(nk, nbands);
111134
kvec_c.resize(nk, ModuleBase::Vector3<double>(0.0, 0.0, 0.0));
@@ -166,17 +189,26 @@ class WriteWfcLcaoTest : public testing::Test
166189

167190
TEST_F(WriteWfcLcaoTest, WriteWfcLcao)
168191
{
169-
// create a psi object
170-
psi::Psi<double> my_psi(psi_local_double.data(), nk, nbands_local, nbasis_local, nbasis_local, true);
171192
PARAM.sys.global_out_dir = "./";
172-
ModuleIO::write_wfc_nao(2, my_psi, ekb, wg, kvec_c, pv, -1);
173193

174-
// check the output
194+
const int out_type = 2;
195+
psi::Psi<double> my_psi(psi_local_double.data(), nk, nbands_local, nbasis_local, nbasis_local, true);
196+
const int nspin = 2;
197+
const int istep = -1;
198+
199+
ModuleIO::write_wfc_nao(out_type, out_app_flag, my_psi, ekb, wg, kvec_c,
200+
ik2iktot, nkstot, pv, nspin, istep);
201+
202+
const bool gamma_only = true;
203+
204+
// check the output file
175205
if (GlobalV::MY_RANK == 0)
176206
{
177207
for (int ik = 0; ik < nk; ik++)
178208
{
179-
std::string fname = ModuleIO::wfc_nao_gen_fname(2, true, PARAM.input.out_app_flag, ik, -1);
209+
std::string fname = ModuleIO::wfc_nao_gen_fname(out_type, gamma_only,
210+
out_app_flag, ik, ik2iktot, nkstot, nspin, istep);
211+
180212
std::ifstream file1(fname);
181213
EXPECT_TRUE(file1.good());
182214
std::vector<double> data;
@@ -196,16 +228,26 @@ TEST_F(WriteWfcLcaoTest, WriteWfcLcao)
196228

197229
TEST_F(WriteWfcLcaoTest, WriteWfcLcaoComplex)
198230
{
199-
psi::Psi<std::complex<double>> my_psi(psi_local_complex.data(), nk, nbands_local, nbasis_local, true);
200231
PARAM.sys.global_out_dir = "./";
201-
ModuleIO::write_wfc_nao(2, my_psi, ekb, wg, kvec_c, pv, -1);
232+
233+
const int out_type = 2;
234+
psi::Psi<std::complex<double>> my_psi(psi_local_complex.data(), nk, nbands_local, nbasis_local, true);
235+
const int nspin = 1;
236+
const int istep = -1;
237+
238+
ModuleIO::write_wfc_nao(out_type, out_app_flag, my_psi, ekb, wg, kvec_c,
239+
ik2iktot, nkstot, pv, nspin, istep);
240+
241+
const bool gamma_only = false;
202242

203243
// check the output file
204244
if (GlobalV::MY_RANK == 0)
205245
{
206246
for (int ik = 0; ik < nk; ik++)
207247
{
208-
std::string fname = ModuleIO::wfc_nao_gen_fname(2, false, PARAM.input.out_app_flag, ik, -1);
248+
std::string fname = ModuleIO::wfc_nao_gen_fname(out_type, gamma_only,
249+
out_app_flag, ik, ik2iktot, nkstot, nspin, istep);
250+
209251
std::ifstream file1(fname);
210252
EXPECT_TRUE(file1.good());
211253
std::vector<std::complex<double>> data;
@@ -231,7 +273,6 @@ TEST(ModuleIOTest, WriteWfcNao)
231273
GlobalV::DRANK = 0;
232274
PARAM.input.nbands = 2;
233275
PARAM.sys.nlocal = 2;
234-
PARAM.input.out_app_flag = true;
235276

236277
// Set up test data
237278
std::string filename = "test_wfc_nao.txt";
@@ -278,7 +319,6 @@ TEST(ModuleIOTest, WriteWfcNaoBinary)
278319
GlobalV::DRANK = 0;
279320
PARAM.input.nbands = 2;
280321
PARAM.sys.nlocal = 2;
281-
PARAM.input.out_app_flag = true;
282322

283323
// Set up test data
284324
std::string filename = "test_wfc_nao.dat";
@@ -335,7 +375,6 @@ TEST(ModuleIOTest, WriteWfcNaoComplex)
335375
// Set up GlobalV
336376
PARAM.input.nbands = 2;
337377
PARAM.sys.nlocal = 3;
338-
PARAM.input.out_app_flag = true;
339378
// set up test data
340379
std::string name = "test_wfc_nao_complex.txt";
341380
int ik = 0;
@@ -379,7 +418,6 @@ TEST(ModuleIOTest, WriteWfcNaoComplexBinary)
379418
// Set up GlobalV
380419
PARAM.input.nbands = 2;
381420
PARAM.sys.nlocal = 3;
382-
PARAM.input.out_app_flag = true;
383421
// set up test data
384422
std::string name = "test_wfc_nao_complex.dat";
385423
int ik = 0;

source/module_io/write_wfc_nao.cpp

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ std::string wfc_nao_gen_fname(const int out_type,
8787
}
8888
else
8989
{
90-
std::cout << "WARNING: the out type of wave function is not 1 or 2. Write to a txt file." << std::endl;
90+
std::cout << "WARNING: the type of output wave function is not 1 or 2, so 1 is chosen." << std::endl;
9191
suffix_block = ".txt";
9292
}
9393

@@ -286,15 +286,16 @@ void wfc_nao_write2file_complex(const std::string& name,
286286

287287
template <typename T>
288288
void write_wfc_nao(const int out_type,
289-
const psi::Psi<T>& psi,
290-
const ModuleBase::matrix& ekb,
291-
const ModuleBase::matrix& wg,
292-
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
293-
const std::vector<int> &ik2iktot,
294-
const int nkstot,
295-
const Parallel_Orbitals& pv,
296-
const int nspin,
297-
const int istep)
289+
const bool out_app_flag,
290+
const psi::Psi<T>& psi,
291+
const ModuleBase::matrix& ekb,
292+
const ModuleBase::matrix& wg,
293+
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
294+
const std::vector<int> &ik2iktot,
295+
const int nkstot,
296+
const Parallel_Orbitals& pv,
297+
const int nspin,
298+
const int istep)
298299
{
299300
if (!out_type)
300301
{
@@ -353,10 +354,10 @@ void write_wfc_nao(const int out_type,
353354
if (myid == 0)
354355
{
355356
std::string fn = PARAM.globalv.global_out_dir
356-
+ wfc_nao_gen_fname(out_type, gamma_only, PARAM.inp.out_app_flag, ik,
357+
+ wfc_nao_gen_fname(out_type, gamma_only, out_app_flag, ik,
357358
ik2iktot, nkstot, nspin, istep);
358359

359-
bool append_flag = (istep > 0 && PARAM.inp.out_app_flag);
360+
bool append_flag = (istep > 0 && out_app_flag);
360361
if (std::is_same<double, T>::value)
361362
{
362363
wfc_nao_write2file(fn,
@@ -386,25 +387,27 @@ void write_wfc_nao(const int out_type,
386387
}
387388

388389
template void write_wfc_nao<double>(const int out_type,
389-
const psi::Psi<double>& psi,
390-
const ModuleBase::matrix& ekb,
391-
const ModuleBase::matrix& wg,
392-
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
393-
const std::vector<int> &ik2iktot,
394-
const int nkstot,
395-
const Parallel_Orbitals& pv,
396-
const int nspin,
397-
const int istep);
390+
const bool out_app_flag,
391+
const psi::Psi<double>& psi,
392+
const ModuleBase::matrix& ekb,
393+
const ModuleBase::matrix& wg,
394+
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
395+
const std::vector<int> &ik2iktot,
396+
const int nkstot,
397+
const Parallel_Orbitals& pv,
398+
const int nspin,
399+
const int istep);
398400

399401
template void write_wfc_nao<std::complex<double>>(const int out_type,
400-
const psi::Psi<std::complex<double>>& psi,
401-
const ModuleBase::matrix& ekb,
402-
const ModuleBase::matrix& wg,
403-
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
404-
const std::vector<int> &ik2iktot,
405-
const int nkstot,
406-
const Parallel_Orbitals& pv,
407-
const int nspin,
408-
const int istep);
402+
const bool out_app_flag,
403+
const psi::Psi<std::complex<double>>& psi,
404+
const ModuleBase::matrix& ekb,
405+
const ModuleBase::matrix& wg,
406+
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
407+
const std::vector<int> &ik2iktot,
408+
const int nkstot,
409+
const Parallel_Orbitals& pv,
410+
const int nspin,
411+
const int istep);
409412

410413
} // namespace ModuleIO

source/module_io/write_wfc_nao.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ std::string wfc_nao_gen_fname(const int out_type,
4444
*/
4545
template <typename T>
4646
void write_wfc_nao(const int out_type,
47-
const psi::Psi<T>& psi,
48-
const ModuleBase::matrix& ekb,
49-
const ModuleBase::matrix& wg,
50-
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
51-
const std::vector<int> &ik2iktot,
52-
const int nkstot,
53-
const Parallel_Orbitals& pv,
54-
const int nspin,
55-
const int istep=-1) ;
47+
const bool out_app_flag,
48+
const psi::Psi<T>& psi,
49+
const ModuleBase::matrix& ekb,
50+
const ModuleBase::matrix& wg,
51+
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
52+
const std::vector<int> &ik2iktot,
53+
const int nkstot,
54+
const Parallel_Orbitals& pv,
55+
const int nspin,
56+
const int istep=-1) ;
5657

5758
void wfc_nao_write2file(const std::string& name,
5859
const double* ctot,

source/module_io/write_wfc_pw.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ void ModuleIO::write_wfc_pw(const std::string& fn,
1919
const int nkstot = kv.get_nkstot();
2020
const int nks = kv.get_nks();
2121

22+
assert(nkstot>0);
23+
assert(nks>0);
24+
2225
std::string* wfilename;
2326
wfilename = new std::string[nkstot];
2427
for (int ik = 0; ik < nkstot; ++ik)

0 commit comments

Comments
 (0)