Skip to content

Commit b3bc912

Browse files
authored
Refactor:Modify the output functions in elecstate. (#5954)
* change the func print_band * change the func print_format * change the func print_value * change print value * add change
1 parent 70b7f58 commit b3bc912

File tree

6 files changed

+126
-108
lines changed

6 files changed

+126
-108
lines changed

source/module_elecstate/elecstate.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,6 @@ class ElecState
166166
ModuleBase::matrix ekb; ///< band energy at each k point, each band.
167167
ModuleBase::matrix wg; ///< occupation weight for each k-point and band
168168

169-
public: // print something. See elecstate_print.cpp
170-
void print_etot(const Magnetism& magnet,
171-
const bool converged,
172-
const int& iter,
173-
const double& scf_thr,
174-
const double& scf_thr_kin,
175-
const double& duration,
176-
const int printe,
177-
const double& pw_diag_thr = 0,
178-
const double& avg_iter = 0,
179-
bool print = true);
180-
void print_format(const std::string& name, const double& value);
181-
182-
void print_band(const int& ik, const int& printe, const int& iter);
183-
184-
void print_eigenvalue(std::ofstream& ofs);
185-
186169
public:
187170
// calculate ebands for all k points and all occupied bands
188171
void calEBand();

source/module_elecstate/elecstate_print.cpp

Lines changed: 70 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,21 @@ void print_scf_iterinfo(const std::string& ks_solver,
149149
}
150150
/// @brief print and check for band energy and occupations
151151
/// @param ofs
152-
void ElecState::print_eigenvalue(std::ofstream& ofs)
152+
void print_eigenvalue(const ModuleBase::matrix& ekb,
153+
const ModuleBase::matrix& wg,
154+
const K_Vectors* klist,
155+
std::ofstream& ofs)
153156
{
154-
bool wrong = false;
155-
const int nks = this->klist->get_nks();
156-
const int nkstot = this->klist->get_nkstot();
157+
bool wrong = false;
158+
const int nks = klist->get_nks();
159+
const int nkstot = klist->get_nkstot();
157160
for (int ik = 0; ik < nks; ++ik)
158161
{
159-
for (int ib = 0; ib < this->ekb.nc; ++ib)
162+
for (int ib = 0; ib < ekb.nc; ++ib)
160163
{
161-
if (std::abs(this->ekb(ik, ib)) > 1.0e10)
164+
if (std::abs(ekb(ik, ib)) > 1.0e10)
162165
{
163-
GlobalV::ofs_warning << " ik=" << ik + 1 << " ib=" << ib + 1 << " " << this->ekb(ik, ib) << " Ry"
164-
<< std::endl;
166+
GlobalV::ofs_warning << " ik=" << ik + 1 << " ib=" << ib + 1 << " " << ekb(ik, ib) << " Ry" << std::endl;
165167
wrong = true;
166168
}
167169
}
@@ -175,7 +177,7 @@ void ElecState::print_eigenvalue(std::ofstream& ofs)
175177
}
176178

177179
std::string filename = PARAM.globalv.global_out_dir + PARAM.globalv.log_file;
178-
std::vector<int> ngk_tot = this->klist->ngk;
180+
std::vector<int> ngk_tot = klist->ngk;
179181

180182
#ifdef __MPI
181183
MPI_Allreduce(MPI_IN_PLACE, ngk_tot.data(), nks, MPI_INT, MPI_SUM, POOL_WORLD);
@@ -214,16 +216,16 @@ void ElecState::print_eigenvalue(std::ofstream& ofs)
214216
std::ofstream ofs_eig(filename.c_str(), std::ios::app);
215217
ofs_eig << std::setprecision(5);
216218
ofs_eig << std::setiosflags(std::ios::showpoint);
217-
ofs_eig << " " << this->klist->ik2iktot[ik] + 1 - is * nkstot_np << "/" << nkstot_np
218-
<< " kpoint (Cartesian) = " << this->klist->kvec_c[ik].x << " " << this->klist->kvec_c[ik].y
219-
<< " " << this->klist->kvec_c[ik].z << " (" << ngk_tot[ik] << " pws)" << std::endl;
219+
ofs_eig << " " << klist->ik2iktot[ik] + 1 - is * nkstot_np << "/" << nkstot_np
220+
<< " kpoint (Cartesian) = " << klist->kvec_c[ik].x << " " << klist->kvec_c[ik].y
221+
<< " " << klist->kvec_c[ik].z << " (" << ngk_tot[ik] << " pws)" << std::endl;
220222

221223
ofs_eig << std::setprecision(6);
222224
ofs_eig << std::setiosflags(std::ios::showpoint);
223-
for (int ib = 0; ib < this->ekb.nc; ib++)
225+
for (int ib = 0; ib < ekb.nc; ib++)
224226
{
225-
ofs_eig << std::setw(8) << ib + 1 << std::setw(15) << this->ekb(ik, ib) * ModuleBase::Ry_to_eV
226-
<< std::setw(15) << this->wg(ik, ib) << std::endl;
227+
ofs_eig << std::setw(8) << ib + 1 << std::setw(15) << ekb(ik, ib) * ModuleBase::Ry_to_eV
228+
<< std::setw(15) << wg(ik, ib) << std::endl;
227229
}
228230
ofs_eig << std::endl;
229231
ofs_eig.close();
@@ -242,16 +244,20 @@ void ElecState::print_eigenvalue(std::ofstream& ofs)
242244
/// @param ik: index of kpoints
243245
/// @param printe: print energy every 'printe' electron iteration.
244246
/// @param iter: index of iterations
245-
void ElecState::print_band(const int& ik, const int& printe, const int& iter)
247+
void print_band(const ModuleBase::matrix& ekb,
248+
const ModuleBase::matrix& wg,
249+
const K_Vectors* klist,
250+
const int& ik,
251+
const int& printe,
252+
const int& iter)
246253
{
247254
// check the band energy.
248255
bool wrong = false;
249256
for (int ib = 0; ib < PARAM.globalv.nbands_l; ++ib)
250257
{
251-
if (std::abs(this->ekb(ik, ib)) > 1.0e10)
258+
if (std::abs(ekb(ik, ib)) > 1.0e10)
252259
{
253-
GlobalV::ofs_warning << " ik=" << ik + 1 << " ib=" << ib + 1 << " " << this->ekb(ik, ib) << " Ry"
254-
<< std::endl;
260+
GlobalV::ofs_warning << " ik=" << ik + 1 << " ib=" << ib + 1 << " " << ekb(ik, ib) << " Ry" << std::endl;
255261
wrong = true;
256262
}
257263
}
@@ -265,16 +271,16 @@ void ElecState::print_band(const int& ik, const int& printe, const int& iter)
265271
if (printe > 0 && ((iter + 1) % printe == 0))
266272
{
267273
GlobalV::ofs_running << std::setprecision(6);
268-
GlobalV::ofs_running << " Energy (eV) & Occupations for spin=" << this->klist->isk[ik] + 1
274+
GlobalV::ofs_running << " Energy (eV) & Occupations for spin=" << klist->isk[ik] + 1
269275
<< " K-point=" << ik + 1 << std::endl;
270276
GlobalV::ofs_running << std::setiosflags(std::ios::showpoint);
271277
for (int ib = 0; ib < PARAM.globalv.nbands_l; ib++)
272278
{
273279
GlobalV::ofs_running << " " << std::setw(6) << ib + 1 << std::setw(15)
274-
<< this->ekb(ik, ib) * ModuleBase::Ry_to_eV;
280+
<< ekb(ik, ib) * ModuleBase::Ry_to_eV;
275281
// for the first electron iteration, we don't have the energy
276282
// spectrum, so we can't get the occupations.
277-
GlobalV::ofs_running << std::setw(15) << this->wg(ik, ib);
283+
GlobalV::ofs_running << std::setw(15) << wg(ik, ib);
278284
GlobalV::ofs_running << std::endl;
279285
}
280286
}
@@ -291,25 +297,25 @@ void ElecState::print_band(const int& ik, const int& printe, const int& iter)
291297
/// @param pw_diag_thr: threshold for diagonalization
292298
/// @param avg_iter: averaged diagonalization iteration of each scf iteration
293299
/// @param print: if print to screen
294-
void ElecState::print_etot(const Magnetism& magnet,
295-
const bool converged,
296-
const int& iter_in,
297-
const double& scf_thr,
298-
const double& scf_thr_kin,
299-
const double& duration,
300-
const int printe,
301-
const double& pw_diag_thr,
302-
const double& avg_iter,
303-
const bool print)
300+
void print_etot(const Magnetism& magnet,
301+
const ElecState& elec,
302+
const bool converged,
303+
const int& iter_in,
304+
const double& scf_thr,
305+
const double& scf_thr_kin,
306+
const double& duration,
307+
const int printe,
308+
const double& pw_diag_thr,
309+
const double& avg_iter,
310+
const bool print)
304311
{
305312
ModuleBase::TITLE("energy", "print_etot");
306313
const int iter = iter_in;
307-
const int nrxx = this->charge->nrxx;
308-
const int nxyz = this->charge->nxyz;
314+
const int nrxx = elec.charge->nrxx;
315+
const int nxyz = elec.charge->nxyz;
309316

310317
GlobalV::ofs_running << std::setprecision(12);
311318
GlobalV::ofs_running << std::setiosflags(std::ios::right);
312-
313319
GlobalV::ofs_running << "\n Density error is " << scf_thr << std::endl;
314320

315321
if (PARAM.inp.basis_type == "pw")
@@ -324,46 +330,46 @@ void ElecState::print_etot(const Magnetism& magnet,
324330
{
325331
int n_order = std::max(0, Occupy::gaussian_type);
326332
titles.push_back("E_KohnSham");
327-
energies_Ry.push_back(this->f_en.etot);
333+
energies_Ry.push_back(elec.f_en.etot);
328334
titles.push_back("E_KS(sigma->0)");
329-
energies_Ry.push_back(this->f_en.etot - this->f_en.demet / (2 + n_order));
335+
energies_Ry.push_back(elec.f_en.etot - elec.f_en.demet / (2 + n_order));
330336
titles.push_back("E_Harris");
331-
energies_Ry.push_back(this->f_en.etot_harris);
337+
energies_Ry.push_back(elec.f_en.etot_harris);
332338
titles.push_back("E_band");
333-
energies_Ry.push_back(this->f_en.eband);
339+
energies_Ry.push_back(elec.f_en.eband);
334340
titles.push_back("E_one_elec");
335-
energies_Ry.push_back(this->f_en.eband + this->f_en.deband);
341+
energies_Ry.push_back(elec.f_en.eband + elec.f_en.deband);
336342
titles.push_back("E_Hartree");
337-
energies_Ry.push_back(this->f_en.hartree_energy);
343+
energies_Ry.push_back(elec.f_en.hartree_energy);
338344
titles.push_back("E_xc");
339-
energies_Ry.push_back(this->f_en.etxc - this->f_en.etxcc);
345+
energies_Ry.push_back(elec.f_en.etxc - elec.f_en.etxcc);
340346
titles.push_back("E_Ewald");
341-
energies_Ry.push_back(this->f_en.ewald_energy);
347+
energies_Ry.push_back(elec.f_en.ewald_energy);
342348
titles.push_back("E_entropy(-TS)");
343-
energies_Ry.push_back(this->f_en.demet);
349+
energies_Ry.push_back(elec.f_en.demet);
344350
titles.push_back("E_descf");
345-
energies_Ry.push_back(this->f_en.descf);
351+
energies_Ry.push_back(elec.f_en.descf);
346352
titles.push_back("E_LocalPP");
347-
energies_Ry.push_back(this->f_en.e_local_pp);
353+
energies_Ry.push_back(elec.f_en.e_local_pp);
348354
std::string vdw_method = PARAM.inp.vdw_method;
349355
if (vdw_method == "d2") // Peize Lin add 2014-04, update 2021-03-09
350356
{
351357
titles.push_back("E_vdwD2");
352-
energies_Ry.push_back(this->f_en.evdw);
358+
energies_Ry.push_back(elec.f_en.evdw);
353359
}
354360
else if (vdw_method == "d3_0" || vdw_method == "d3_bj") // jiyy add 2019-05, update 2021-05-02
355361
{
356362
titles.push_back("E_vdwD3");
357-
energies_Ry.push_back(this->f_en.evdw);
363+
energies_Ry.push_back(elec.f_en.evdw);
358364
}
359365
titles.push_back("E_exx");
360-
energies_Ry.push_back(this->f_en.exx);
366+
energies_Ry.push_back(elec.f_en.exx);
361367
if (PARAM.inp.imp_sol)
362368
{
363369
titles.push_back("E_sol_el");
364-
energies_Ry.push_back(this->f_en.esol_el);
370+
energies_Ry.push_back(elec.f_en.esol_el);
365371
titles.push_back("E_sol_cav");
366-
energies_Ry.push_back(this->f_en.esol_cav);
372+
energies_Ry.push_back(elec.f_en.esol_cav);
367373
}
368374
if (PARAM.inp.efield_flag)
369375
{
@@ -380,43 +386,43 @@ void ElecState::print_etot(const Magnetism& magnet,
380386
if (PARAM.inp.deepks_scf) // caoyu add 2021-08-10
381387
{
382388
titles.push_back("E_DeePKS");
383-
energies_Ry.push_back(this->f_en.edeepks_delta);
389+
energies_Ry.push_back(elec.f_en.edeepks_delta);
384390
}
385391
#endif
386392
}
387393
else
388394
{
389395
titles.push_back("E_KohnSham");
390-
energies_Ry.push_back(this->f_en.etot);
396+
energies_Ry.push_back(elec.f_en.etot);
391397
titles.push_back("E_Harris");
392-
energies_Ry.push_back(this->f_en.etot_harris);
398+
energies_Ry.push_back(elec.f_en.etot_harris);
393399
}
394400

395401
if (PARAM.globalv.two_fermi)
396402
{
397403
titles.push_back("E_Fermi_up");
398-
energies_Ry.push_back(this->eferm.ef_up);
404+
energies_Ry.push_back(elec.eferm.ef_up);
399405
titles.push_back("E_Fermi_dw");
400-
energies_Ry.push_back(this->eferm.ef_dw);
406+
energies_Ry.push_back(elec.eferm.ef_dw);
401407
}
402408
else
403409
{
404410
titles.push_back("E_Fermi");
405-
energies_Ry.push_back(this->eferm.ef);
411+
energies_Ry.push_back(elec.eferm.ef);
406412
}
407413
if (PARAM.inp.out_bandgap)
408414
{
409415
if (!PARAM.globalv.two_fermi)
410416
{
411417
titles.push_back("E_bandgap");
412-
energies_Ry.push_back(this->bandgap);
418+
energies_Ry.push_back(elec.bandgap);
413419
}
414420
else
415421
{
416422
titles.push_back("E_bandgap_up");
417-
energies_Ry.push_back(this->bandgap_up);
423+
energies_Ry.push_back(elec.bandgap_up);
418424
titles.push_back("E_bandgap_dw");
419-
energies_Ry.push_back(this->bandgap_dw);
425+
energies_Ry.push_back(elec.bandgap_dw);
420426
}
421427
}
422428
energies_eV.resize(energies_Ry.size());
@@ -457,8 +463,8 @@ void ElecState::print_etot(const Magnetism& magnet,
457463
6,
458464
mag,
459465
10,
460-
this->f_en.etot * ModuleBase::Ry_to_eV,
461-
this->f_en.etot_delta * ModuleBase::Ry_to_eV,
466+
elec.f_en.etot * ModuleBase::Ry_to_eV,
467+
elec.f_en.etot_delta * ModuleBase::Ry_to_eV,
462468
16,
463469
drho,
464470
12,
@@ -471,12 +477,10 @@ void ElecState::print_etot(const Magnetism& magnet,
471477
/// @brief function to print name, value and value*Ry_to_eV
472478
/// @param name: name
473479
/// @param value: value
474-
void ElecState::print_format(const std::string& name, const double& value)
480+
void print_format(const std::string& name, const double& value)
475481
{
476482
GlobalV::ofs_running << std::setiosflags(std::ios::showpos);
477-
std::stringstream name2;
478-
name2 << name;
479-
GlobalV::ofs_running << " " << std::setw(16) << name2.str() << std::setw(30) << value << std::setw(30)
483+
GlobalV::ofs_running << " " << std::setw(16) << name << std::setw(30) << value << std::setw(30)
480484
<< value * ModuleBase::Ry_to_eV << std::endl;
481485
GlobalV::ofs_running << std::resetiosflags(std::ios::showpos);
482486
return;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef ELECSATE_PRINT_H
2+
#define ELECSATE_PRINT_H
3+
#include "module_elecstate/elecstate.h"
4+
namespace elecstate
5+
{
6+
void print_band(const ModuleBase::matrix& ekb,
7+
const ModuleBase::matrix& wg,
8+
const K_Vectors* klist,
9+
const int& ik,
10+
const int& printe,
11+
const int& iter);
12+
13+
void print_format(const std::string& name,
14+
const double& value);
15+
16+
void print_eigenvalue(const ModuleBase::matrix& ekb,
17+
const ModuleBase::matrix& wg,
18+
const K_Vectors* klist,
19+
std::ofstream& ofs);
20+
21+
void print_etot(const Magnetism& magnet,
22+
const ElecState& elec,
23+
const bool converged,
24+
const int& iter_in,
25+
const double& scf_thr,
26+
const double& scf_thr_kin,
27+
const double& duration,
28+
const int printe,
29+
const double& pw_diag_thr = 0,
30+
const double& avg_iter = 0,
31+
bool print = true);
32+
}
33+
#endif

0 commit comments

Comments
 (0)