Skip to content

Commit 3ce7c15

Browse files
authored
Fix: incorrect append mode for out_wfc_lcao (#5914)
1 parent 339c6b3 commit 3ce7c15

File tree

2 files changed

+171
-118
lines changed

2 files changed

+171
-118
lines changed

source/module_io/write_wfc_nao.cpp

Lines changed: 154 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -46,158 +46,186 @@ std::string wfc_nao_gen_fname(const int out_type,
4646
return fn_out;
4747
}
4848

49-
void wfc_nao_write2file(const std::string &name, const double* ctot, const int nlocal, const int ik, const ModuleBase::matrix& ekb, const ModuleBase::matrix& wg, bool writeBinary)
49+
void wfc_nao_write2file(const std::string& name,
50+
const double* ctot,
51+
const int nlocal,
52+
const int ik,
53+
const ModuleBase::matrix& ekb,
54+
const ModuleBase::matrix& wg,
55+
const bool& writeBinary,
56+
const bool& append_flag)
5057
{
5158
ModuleBase::TITLE("ModuleIO", "write_wfc_nao");
5259
ModuleBase::timer::tick("ModuleIO", "write_wfc_nao");
5360

54-
//if (GlobalV::DRANK == 0)
61+
int nbands = ekb.nc;
62+
63+
if (writeBinary)
5564
{
56-
int nbands = ekb.nc;
57-
58-
if (writeBinary)
65+
Binstream ofs;
66+
if (append_flag)
5967
{
60-
Binstream ofs(name, "a");
61-
if (!ofs)
62-
{
63-
ModuleBase::WARNING("ModuleIO::write_wfc_nao", "Can't write local orbital wave functions.");
64-
}
68+
ofs.open(name, "a");
69+
}
70+
else
71+
{
72+
ofs.open(name, "w");
73+
}
74+
if (!ofs)
75+
{
76+
ModuleBase::WARNING("ModuleIO::write_wfc_nao", "Can't write local orbital wave functions.");
77+
}
6578

66-
ofs << nbands;
67-
ofs << nlocal;
79+
ofs << nbands;
80+
ofs << nlocal;
6881

69-
for (int i = 0; i < nbands; i++)
70-
{
71-
ofs << i+1;
72-
ofs << ekb(ik, i);
73-
ofs << wg(ik, i);
82+
for (int i = 0; i < nbands; i++)
83+
{
84+
ofs << i + 1;
85+
ofs << ekb(ik, i);
86+
ofs << wg(ik, i);
7487

75-
for (int j = 0; j < nlocal; j++)
76-
{
77-
ofs << ctot[i*nlocal + j];
78-
}
88+
for (int j = 0; j < nlocal; j++)
89+
{
90+
ofs << ctot[i * nlocal + j];
7991
}
80-
ofs.close();
92+
}
93+
ofs.close();
94+
}
95+
else
96+
{
97+
std::ofstream ofs;
98+
if (append_flag)
99+
{
100+
ofs.open(name.c_str(), std::ofstream::app);
81101
}
82102
else
83103
{
84-
std::ofstream ofs;
85-
// if (PARAM.inp.out_app_flag)
86-
// {
87-
// ofs.open(name.c_str(), std::ofstream::app);
88-
// }
89-
// else
90-
{ // the default value of `out_app_flag`is true, but usually there's no use to save each step's LCAO wave function.
91-
ofs.open(name.c_str());
92-
}
93-
if (!ofs)
94-
{
95-
ModuleBase::WARNING("ModuleIO::write_wfc_nao", "Can't write local orbital wave functions.");
96-
}
97-
ofs << nbands << " (number of bands)" << std::endl;
98-
ofs << nlocal << " (number of orbitals)";
99-
ofs << std::setprecision(8);
100-
ofs << std::scientific;
104+
ofs.open(name.c_str());
105+
}
106+
if (!ofs)
107+
{
108+
ModuleBase::WARNING("ModuleIO::write_wfc_nao", "Can't write local orbital wave functions.");
109+
}
110+
ofs << nbands << " (number of bands)" << std::endl;
111+
ofs << nlocal << " (number of orbitals)";
112+
ofs << std::setprecision(8);
113+
ofs << std::scientific;
101114

102-
for (int i=0; i<nbands; i++)
115+
for (int i = 0; i < nbands; i++)
116+
{
117+
// +1 to mean more clearly.
118+
// band index start from 1.
119+
ofs << "\n" << i + 1 << " (band)";
120+
ofs << "\n" << ekb(ik, i) << " (Ry)";
121+
ofs << "\n" << wg(ik, i) << " (Occupations)";
122+
for (int j = 0; j < nlocal; j++)
103123
{
104-
// +1 to mean more clearly.
105-
// band index start from 1.
106-
ofs << "\n" << i+1 << " (band)";
107-
ofs << "\n" << ekb(ik, i) << " (Ry)";
108-
ofs << "\n" << wg(ik,i) << " (Occupations)";
109-
for (int j=0; j<nlocal; j++)
124+
if (j % 5 == 0)
110125
{
111-
if (j % 5 == 0) { ofs << "\n";
112-
}
113-
ofs << ctot[i*nlocal + j] << " ";
126+
ofs << "\n";
114127
}
128+
ofs << ctot[i * nlocal + j] << " ";
115129
}
116-
ofs << std::endl;
117-
ofs.close();
118130
}
131+
ofs << std::endl;
132+
ofs.close();
119133
}
120134

121135
ModuleBase::timer::tick("ModuleIO", "write_wfc_nao");
122136
return;
123137
}
124138

125-
void wfc_nao_write2file_complex(const std::string &name, const std::complex<double>* ctot, const int nlocal,const int &ik, const ModuleBase::Vector3<double> &kvec_c, const ModuleBase::matrix& ekb, const ModuleBase::matrix& wg, bool writeBinary)
139+
void wfc_nao_write2file_complex(const std::string& name,
140+
const std::complex<double>* ctot,
141+
const int nlocal,
142+
const int& ik,
143+
const ModuleBase::Vector3<double>& kvec_c,
144+
const ModuleBase::matrix& ekb,
145+
const ModuleBase::matrix& wg,
146+
const bool& writeBinary,
147+
const bool& append_flag)
126148
{
127149
ModuleBase::TITLE("ModuleIO","write_wfc_nao_complex");
128150
ModuleBase::timer::tick("ModuleIO","write_wfc_nao_complex");
129151

130-
131-
//if (GlobalV::DRANK==0)
152+
int nbands = ekb.nc;
153+
154+
if (writeBinary)
132155
{
133-
int nbands = ekb.nc;
156+
Binstream ofs;
157+
if (append_flag)
158+
{
159+
ofs.open(name, "a");
160+
}
161+
else
162+
{
163+
ofs.open(name, "w");
164+
}
165+
if (!ofs)
166+
{
167+
ModuleBase::WARNING("ModuleIO::write_wfc_nao", "Can't write local orbital wave functions.");
168+
}
169+
ofs << ik + 1;
170+
ofs << kvec_c.x;
171+
ofs << kvec_c.y;
172+
ofs << kvec_c.z;
173+
ofs << nbands;
174+
ofs << nlocal;
134175

135-
if (writeBinary)
176+
for (int i = 0; i < nbands; i++)
136177
{
137-
Binstream ofs(name, "a");
138-
if (!ofs)
139-
{
140-
ModuleBase::WARNING("ModuleIO::write_wfc_nao", "Can't write local orbital wave functions.");
141-
}
142-
ofs << ik+1;
143-
ofs << kvec_c.x;
144-
ofs << kvec_c.y;
145-
ofs << kvec_c.z;
146-
ofs << nbands;
147-
ofs << nlocal;
178+
ofs << i + 1;
179+
ofs << ekb(ik, i);
180+
ofs << wg(ik, i);
148181

149-
for (int i = 0; i < nbands; i++)
182+
for (int j = 0; j < nlocal; j++)
150183
{
151-
ofs << i+1;
152-
ofs << ekb(ik, i);
153-
ofs << wg(ik, i);
154-
155-
for (int j = 0; j < nlocal; j++)
156-
{
157-
ofs << ctot[i*nlocal + j].real() << ctot[i*nlocal + j].imag();
158-
}
184+
ofs << ctot[i * nlocal + j].real() << ctot[i * nlocal + j].imag();
159185
}
160-
ofs.close();
186+
}
187+
ofs.close();
188+
}
189+
else
190+
{
191+
std::ofstream ofs;
192+
if (append_flag)
193+
{
194+
ofs.open(name.c_str(), std::ofstream::app);
161195
}
162196
else
163197
{
164-
std::ofstream ofs;
165-
// if (PARAM.inp.out_app_flag)
166-
// {
167-
// ofs.open(name.c_str(), std::ofstream::app);
168-
// }
169-
// else
170-
{ // the default value of `out_app_flag`is true, but usually there's no use to save each step's LCAO wave function.
171-
ofs.open(name.c_str());
172-
}
173-
if (!ofs)
174-
{
175-
ModuleBase::WARNING("ModuleIO::write_wfc_nao","Can't write local orbital wave functions.");
176-
}
177-
ofs << std::setprecision(8);
178-
ofs << ik+1 << " (index of k points)" << std::endl;
179-
ofs << kvec_c.x << " " << kvec_c.y << " " << kvec_c.z << std::endl;
180-
ofs << nbands << " (number of bands)" << std::endl;
181-
ofs << nlocal << " (number of orbitals)";
182-
ofs << std::scientific;
198+
ofs.open(name.c_str());
199+
}
200+
if (!ofs)
201+
{
202+
ModuleBase::WARNING("ModuleIO::write_wfc_nao", "Can't write local orbital wave functions.");
203+
}
204+
ofs << std::setprecision(8);
205+
ofs << ik + 1 << " (index of k points)" << std::endl;
206+
ofs << kvec_c.x << " " << kvec_c.y << " " << kvec_c.z << std::endl;
207+
ofs << nbands << " (number of bands)" << std::endl;
208+
ofs << nlocal << " (number of orbitals)";
209+
ofs << std::scientific;
183210

184-
for (int i=0; i<nbands; i++)
211+
for (int i = 0; i < nbands; i++)
212+
{
213+
// +1 to mean more clearly.
214+
// band index start from 1.
215+
ofs << "\n" << i + 1 << " (band)";
216+
ofs << "\n" << ekb(ik, i) << " (Ry)";
217+
ofs << "\n" << wg(ik, i) << " (Occupations)";
218+
for (int j = 0; j < nlocal; j++)
185219
{
186-
// +1 to mean more clearly.
187-
// band index start from 1.
188-
ofs << "\n" << i+1 << " (band)";
189-
ofs << "\n" << ekb(ik, i) << " (Ry)";
190-
ofs << "\n" << wg(ik,i) << " (Occupations)";
191-
for (int j=0; j<nlocal; j++)
220+
if (j % 5 == 0)
192221
{
193-
if (j % 5 == 0) { ofs << "\n";
194-
}
195-
ofs << ctot[i*nlocal + j].real() << " " << ctot[i*nlocal + j].imag() << " ";
222+
ofs << "\n";
196223
}
224+
ofs << ctot[i * nlocal + j].real() << " " << ctot[i * nlocal + j].imag() << " ";
197225
}
198-
ofs << std::endl;
199-
ofs.close();
200226
}
227+
ofs << std::endl;
228+
ofs.close();
201229
}
202230

203231
ModuleBase::timer::tick("ModuleIO","write_wfc_nao_complex");
@@ -269,20 +297,30 @@ void write_wfc_nao(const int out_type,
269297
if (myid == 0)
270298
{
271299
std::string fn = PARAM.globalv.global_out_dir + wfc_nao_gen_fname(out_type, gamma_only, PARAM.inp.out_app_flag, ik, istep);
300+
bool append_flag = (istep > 0 && PARAM.inp.out_app_flag);
301+
std::cout << "append_flag = " << append_flag << std::endl;
272302
if (std::is_same<double, T>::value)
273303
{
274-
wfc_nao_write2file(fn, reinterpret_cast<double*>(ctot.data()), nlocal, ik, ekb, wg, writeBinary);
304+
wfc_nao_write2file(fn,
305+
reinterpret_cast<double*>(ctot.data()),
306+
nlocal,
307+
ik,
308+
ekb,
309+
wg,
310+
writeBinary,
311+
append_flag);
275312
}
276313
else
277314
{
278315
wfc_nao_write2file_complex(fn,
279-
reinterpret_cast<std::complex<double>*>(ctot.data()),
280-
nlocal,
281-
ik,
282-
kvec_c[ik],
283-
ekb,
284-
wg,
285-
writeBinary);
316+
reinterpret_cast<std::complex<double>*>(ctot.data()),
317+
nlocal,
318+
ik,
319+
kvec_c[ik],
320+
ekb,
321+
wg,
322+
writeBinary,
323+
append_flag);
286324
}
287325
}
288326
}

source/module_io/write_wfc_nao.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,22 @@ void write_wfc_nao(const int out_type,
4848
const Parallel_Orbitals& pv,
4949
const int istep=-1) ;
5050

51-
void wfc_nao_write2file(const std::string &name, const double* ctot, const int nlocal, const int ik, const ModuleBase::matrix& ekb, const ModuleBase::matrix& wg, bool writeBinary);
52-
void wfc_nao_write2file_complex(const std::string &name, const std::complex<double>* ctot, const int nlocal,const int &ik, const ModuleBase::Vector3<double> &kvec_c, const ModuleBase::matrix& ekb, const ModuleBase::matrix& wg, bool writeBinary=false);
51+
void wfc_nao_write2file(const std::string& name,
52+
const double* ctot,
53+
const int nlocal,
54+
const int ik,
55+
const ModuleBase::matrix& ekb,
56+
const ModuleBase::matrix& wg,
57+
const bool& writeBinary,
58+
const bool& append_flag = false);
59+
void wfc_nao_write2file_complex(const std::string& name,
60+
const std::complex<double>* ctot,
61+
const int nlocal,
62+
const int& ik,
63+
const ModuleBase::Vector3<double>& kvec_c,
64+
const ModuleBase::matrix& ekb,
65+
const ModuleBase::matrix& wg,
66+
const bool& writeBinary = false,
67+
const bool& append_flag = false);
5368
}// namespace ModuleIO
5469
#endif

0 commit comments

Comments
 (0)