Skip to content

Commit dd67d53

Browse files
author
dyzheng
committed
Fix: correct append mode for out_wfc_lcao output (backport deepmodeling#5914)
The out_app_flag semantics were broken in write_wfc_nao: the text path always opened with truncation and the binary path always with 'a'. With out_app_flag=1 (append all MD steps to one WFC_NAO file) every step overwrote the previous text file, so only the last step survived; with out_app_flag=0 (per-step _ION files) re-running in the same directory appended new blocks to stale binary files. - Thread an explicit append_flag (istep > 0 && out_app_flag) through wfc_nao_write2file/wfc_nao_write2file_complex. - Open text output with std::ofstream::app when appending and truncate otherwise; open Binstream with 'a'/'w' accordingly. - The LTS setprecision(25) text format is kept unchanged. Verified with a 2-step LCAO MD (out_wfc_lcao=1/2): text+append produces one WFC_NAO_K1.txt containing both steps (28544 = 2 x 14272 bytes); binary+append produces WFC_NAO_K1.dat = 2 x 3524; binary without append produces one-block ION1/ION3 .dat files. Before the fix the text file kept only the last step and binary re-runs accumulated duplicate blocks. The 220_NO_KP_MD_wfc_out integrate case passes (Compare_wfc_lcao_pass 0).
1 parent 3c0fcbb commit dd67d53

2 files changed

Lines changed: 171 additions & 117 deletions

File tree

source/module_io/write_wfc_nao.cpp

Lines changed: 153 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -46,156 +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-
ofs << ctot[i*nlocal + j] << " ";
126+
ofs << "\n";
113127
}
128+
ofs << ctot[i * nlocal + j] << " ";
114129
}
115-
ofs << std::endl;
116-
ofs.close();
117130
}
131+
ofs << std::endl;
132+
ofs.close();
118133
}
119134

120135
ModuleBase::timer::tick("ModuleIO", "write_wfc_nao");
121136
return;
122137
}
123138

124-
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)
125148
{
126149
ModuleBase::TITLE("ModuleIO","write_wfc_nao_complex");
127150
ModuleBase::timer::tick("ModuleIO","write_wfc_nao_complex");
128151

129-
130-
//if (GlobalV::DRANK==0)
152+
int nbands = ekb.nc;
153+
154+
if (writeBinary)
131155
{
132-
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;
133175

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

148-
for (int i = 0; i < nbands; i++)
182+
for (int j = 0; j < nlocal; j++)
149183
{
150-
ofs << i+1;
151-
ofs << ekb(ik, i);
152-
ofs << wg(ik, i);
153-
154-
for (int j = 0; j < nlocal; j++)
155-
{
156-
ofs << ctot[i*nlocal + j].real() << ctot[i*nlocal + j].imag();
157-
}
184+
ofs << ctot[i * nlocal + j].real() << ctot[i * nlocal + j].imag();
158185
}
159-
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);
160195
}
161196
else
162197
{
163-
std::ofstream ofs;
164-
// if (PARAM.inp.out_app_flag)
165-
// {
166-
// ofs.open(name.c_str(), std::ofstream::app);
167-
// }
168-
// else
169-
{ // the default value of `out_app_flag`is true, but usually there's no use to save each step's LCAO wave function.
170-
ofs.open(name.c_str());
171-
}
172-
if (!ofs)
173-
{
174-
ModuleBase::WARNING("ModuleIO::write_wfc_nao","Can't write local orbital wave functions.");
175-
}
176-
ofs << std::setprecision(25);
177-
ofs << ik+1 << " (index of k points)" << std::endl;
178-
ofs << kvec_c.x << " " << kvec_c.y << " " << kvec_c.z << std::endl;
179-
ofs << nbands << " (number of bands)" << std::endl;
180-
ofs << nlocal << " (number of orbitals)";
181-
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(25);
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;
182210

183-
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++)
184219
{
185-
// +1 to mean more clearly.
186-
// band index start from 1.
187-
ofs << "\n" << i+1 << " (band)";
188-
ofs << "\n" << ekb(ik, i) << " (Ry)";
189-
ofs << "\n" << wg(ik,i) << " (Occupations)";
190-
for (int j=0; j<nlocal; j++)
220+
if (j % 5 == 0)
191221
{
192-
if (j % 5 == 0) ofs << "\n";
193-
ofs << ctot[i*nlocal + j].real() << " " << ctot[i*nlocal + j].imag() << " ";
222+
ofs << "\n";
194223
}
224+
ofs << ctot[i * nlocal + j].real() << " " << ctot[i * nlocal + j].imag() << " ";
195225
}
196-
ofs << std::endl;
197-
ofs.close();
198226
}
227+
ofs << std::endl;
228+
ofs.close();
199229
}
200230

201231
ModuleBase::timer::tick("ModuleIO","write_wfc_nao_complex");
@@ -267,20 +297,29 @@ void write_wfc_nao(const int out_type,
267297
if (myid == 0)
268298
{
269299
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);
270301
if (std::is_same<double, T>::value)
271302
{
272-
wfc_nao_write2file(fn, reinterpret_cast<double*>(ctot.data()), nlocal, ik, ekb, wg, writeBinary);
303+
wfc_nao_write2file(fn,
304+
reinterpret_cast<double*>(ctot.data()),
305+
nlocal,
306+
ik,
307+
ekb,
308+
wg,
309+
writeBinary,
310+
append_flag);
273311
}
274312
else
275313
{
276314
wfc_nao_write2file_complex(fn,
277-
reinterpret_cast<std::complex<double>*>(ctot.data()),
278-
nlocal,
279-
ik,
280-
kvec_c[ik],
281-
ekb,
282-
wg,
283-
writeBinary);
315+
reinterpret_cast<std::complex<double>*>(ctot.data()),
316+
nlocal,
317+
ik,
318+
kvec_c[ik],
319+
ekb,
320+
wg,
321+
writeBinary,
322+
append_flag);
284323
}
285324
}
286325
}

source/module_io/write_wfc_nao.h

Lines changed: 18 additions & 3 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
54-
#endif
69+
#endif

0 commit comments

Comments
 (0)