Skip to content

Commit d55dc22

Browse files
committed
Refactor: move some print func to Module_IO
1 parent c5214bf commit d55dc22

File tree

14 files changed

+71
-113
lines changed

14 files changed

+71
-113
lines changed

source/driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void Driver::init()
4141

4242
// (3) output information
4343
time_t time_finish = std::time(nullptr);
44-
Print_Info::print_time(time_start, time_finish);
44+
ModuleIO::print_time(time_start, time_finish);
4545

4646
// (4) close all of the running logs
4747
ModuleBase::Global_File::close_all_log(GlobalV::MY_RANK, PARAM.inp.out_alllog,PARAM.inp.calculation);

source/module_esolver/esolver_ks.cpp

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
#include <chrono>
88
#endif
99
#include "module_base/timer.h"
10+
#include "module_cell/cal_atoms_info.h"
1011
#include "module_io/json_output/init_info.h"
12+
#include "module_io/output_log.h"
1113
#include "module_io/print_info.h"
1214
#include "module_io/write_istate_info.h"
1315
#include "module_parameter/parameter.h"
14-
#include "module_cell/cal_atoms_info.h"
1516

1617
#include <iostream>
1718
//--------------Temporary----------------
@@ -221,7 +222,7 @@ void ESolver_KS<T, Device>::before_all_runners(const Input_para& inp, UnitCell&
221222
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS");
222223

223224
//! 7) print information
224-
Print_Info::setup_parameters(ucell, this->kv);
225+
ModuleIO::setup_parameters(ucell, this->kv);
225226

226227
//! 8) new plane wave basis, fft grids, etc.
227228
#ifdef __MPI
@@ -429,7 +430,7 @@ void ESolver_KS<T, Device>::runner(const int istep, UnitCell& ucell)
429430
for (int iter = 1; iter <= this->maxniter; ++iter)
430431
{
431432
// 5) write head
432-
this->write_head(GlobalV::ofs_running, istep, iter);
433+
ModuleIO::write_head(GlobalV::ofs_running, istep, iter, this->basisname);
433434

434435
#ifdef __MPI
435436
auto iterstart = MPI_Wtime();
@@ -687,33 +688,6 @@ void ESolver_KS<T, Device>::after_scf(const int istep)
687688
}
688689
}
689690

690-
//------------------------------------------------------------------------------
691-
//! the 8th function of ESolver_KS: print_head
692-
//! mohan add 2024-05-12
693-
//------------------------------------------------------------------------------
694-
template <typename T, typename Device>
695-
void ESolver_KS<T, Device>::print_head()
696-
{
697-
std::cout << " " << std::setw(7) << "ITER";
698-
699-
if (PARAM.inp.nspin == 2)
700-
{
701-
std::cout << std::setw(10) << "TMAG";
702-
std::cout << std::setw(10) << "AMAG";
703-
}
704-
705-
std::cout << std::setw(15) << "ETOT(eV)";
706-
std::cout << std::setw(15) << "EDIFF(eV)";
707-
std::cout << std::setw(11) << "DRHO";
708-
709-
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
710-
{
711-
std::cout << std::setw(11) << "DKIN";
712-
}
713-
714-
std::cout << std::setw(11) << "TIME(s)" << std::endl;
715-
}
716-
717691
//------------------------------------------------------------------------------
718692
//! the 8th function of ESolver_KS: print_iter
719693
//! mohan add 2024-05-12
@@ -728,17 +702,6 @@ void ESolver_KS<T, Device>::print_iter(const int iter,
728702
this->pelec->print_etot(this->conv_esolver, iter, drho, dkin, duration, PARAM.inp.printe, ethr);
729703
}
730704

731-
//------------------------------------------------------------------------------
732-
//! the 9th function of ESolver_KS: write_head
733-
//! mohan add 2024-05-12
734-
//------------------------------------------------------------------------------
735-
template <typename T, typename Device>
736-
void ESolver_KS<T, Device>::write_head(std::ofstream& ofs_running, const int istep, const int iter)
737-
{
738-
ofs_running << "\n " << this->basisname << " ALGORITHM --------------- ION=" << std::setw(4) << istep + 1
739-
<< " ELEC=" << std::setw(4) << iter << "--------------------------------\n";
740-
}
741-
742705
//------------------------------------------------------------------------------
743706
//! the 10th function of ESolver_KS: getnieter
744707
//! mohan add 2024-05-12

source/module_esolver/esolver_ks.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ class ESolver_KS : public ESolver_FP
7373
virtual void update_pot(const int istep, const int iter) {};
7474

7575
protected:
76-
77-
// Print the headline on the screen:
78-
// ITER ETOT(eV) EDIFF(eV) DRHO TIME(s)
79-
void print_head();
80-
81-
// Print inforamtion in each iter
76+
// Print inforamtion in each iter
8277
// G1 -3.435545e+03 0.000000e+00 3.607e-01 2.862e-01
8378
// for metaGGA
8479
// ITER ETOT(eV) EDIFF(eV) DRHO DKIN TIME(s)
@@ -90,13 +85,6 @@ class ESolver_KS : public ESolver_FP
9085
const double duration,
9186
const double ethr);
9287

93-
// Write the headline in the running_log file
94-
// "PW/LCAO" ALGORITHM --------------- ION= 1 ELEC= 1--------------------------------
95-
void write_head(
96-
std::ofstream& ofs_running,
97-
const int istep,
98-
const int iter);
99-
10088
//! Hamiltonian
10189
hamilt::Hamilt<T, Device>* p_hamilt = nullptr;
10290

source/module_esolver/esolver_ks_lcao.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void ESolver_KS_LCAO<TK, TR>::before_all_runners(const Input_para& inp, UnitCell
136136
this->kv.set(ucell.symm, PARAM.inp.kpoint_file, PARAM.inp.nspin, ucell.G, ucell.latvec, GlobalV::ofs_running);
137137
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS");
138138

139-
Print_Info::setup_parameters(ucell, this->kv);
139+
ModuleIO::setup_parameters(ucell, this->kv);
140140
}
141141
else
142142
{

source/module_esolver/esolver_of.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void ESolver_OF::before_all_runners(const Input_para& inp, UnitCell& ucell)
9292

9393
// print information
9494
// mohan add 2021-01-30
95-
Print_Info::setup_parameters(ucell, kv);
95+
ModuleIO::setup_parameters(ucell, kv);
9696

9797
// initialize the real-space uniform grid for FFT and parallel
9898
// distribution of plane waves

source/module_io/output_log.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,10 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs, const
304304
return;
305305
}
306306

307+
void write_head(std::ofstream& ofs_running, const int& istep, const int& iter, const std::string& basisname)
308+
{
309+
ofs_running << "\n " << basisname << " ALGORITHM --------------- ION=" << std::setw(4) << istep + 1
310+
<< " ELEC=" << std::setw(4) << iter << "--------------------------------\n";
311+
}
312+
307313
}// namespace ModuleIO

source/module_io/output_log.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ void print_force(std::ofstream& ofs_running,
6666
/// @param ry true if the unit of force is a.u.
6767
void print_stress(const std::string& name, const ModuleBase::matrix& scs, const bool screen, const bool ry);
6868

69+
/// @brief write head for scf iteration
70+
/// @param ofs_running output stream
71+
/// @param istep the ion step
72+
/// @param iter the scf iteration step
73+
/// @param basisname basis set name
74+
void write_head(std::ofstream& ofs_running, const int& istep, const int& iter, const std::string& basisname);
75+
6976
} // namespace ModuleIO
7077

7178
#endif

source/module_io/print_info.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
#include "module_base/global_variable.h"
44
#include "module_parameter/parameter.h"
55

6-
Print_Info::Print_Info(){}
7-
8-
Print_Info::~Print_Info(){}
9-
6+
namespace ModuleIO
7+
{
108

11-
void Print_Info::setup_parameters(UnitCell &ucell, K_Vectors &kv)
9+
void setup_parameters(UnitCell& ucell, K_Vectors& kv)
1210
{
1311
ModuleBase::TITLE("Print_Info","setup_parameters");
1412

@@ -168,7 +166,7 @@ void Print_Info::setup_parameters(UnitCell &ucell, K_Vectors &kv)
168166
return;
169167
}
170168

171-
void Print_Info::print_time(time_t &time_start, time_t &time_finish)
169+
void print_time(time_t& time_start, time_t& time_finish)
172170
{
173171
// print out information before ABACUS ends
174172
std::cout << "\n START Time : " << ctime(&time_start);
@@ -189,7 +187,7 @@ void Print_Info::print_time(time_t &time_start, time_t &time_finish)
189187
}
190188

191189
/*
192-
void Print_Info::print_scf(const int &istep, const int &iter)
190+
void ModuleIO::print_scf(const int &istep, const int &iter)
193191
{
194192
if(PARAM.inp.basis_type=="pw")
195193
{
@@ -205,21 +203,21 @@ void Print_Info::print_scf(const int &istep, const int &iter)
205203
GlobalV::ofs_running << "ELEC = " << std::setw(4) << unsigned(iter);
206204
}
207205
else if(PARAM.inp.calculation=="relax" || PARAM.inp.calculation=="cell-relax")
208-
{
209-
GlobalV::ofs_running << "ION = " << std::setw(4) << unsigned(istep+1)
210-
<< " ELEC = " << std::setw(4) << unsigned(iter);
211-
}
212-
else if(PARAM.inp.calculation=="md")
213-
{
214-
GlobalV::ofs_running << "MD = " << std::setw(4) << unsigned(istep+1)
215-
<< " ELEC = " << std::setw(4) << unsigned(iter);
216-
}
206+
{
207+
GlobalV::ofs_running << "ION = " << std::setw(4) << unsigned(istep+1)
208+
<< " ELEC = " << std::setw(4) << unsigned(iter);
209+
}
210+
else if(PARAM.inp.calculation=="md")
211+
{
212+
GlobalV::ofs_running << "MD = " << std::setw(4) << unsigned(istep+1)
213+
<< " ELEC = " << std::setw(4) << unsigned(iter);
214+
}
217215
218216
GlobalV::ofs_running << " --------------------------------\n";
219217
}
220218
*/
221219

222-
void Print_Info::print_screen(const int &stress_step, const int &force_step, const int &istep)
220+
void print_screen(const int& stress_step, const int& force_step, const int& istep)
223221
{
224222
std::cout << " -------------------------------------------" << std::endl;
225223
GlobalV::ofs_running << "\n -------------------------------------------" << std::endl;
@@ -263,3 +261,5 @@ void Print_Info::print_screen(const int &stress_step, const int &force_step, con
263261
std::cout << " -------------------------------------------" << std::endl;
264262
GlobalV::ofs_running << " -------------------------------------------" << std::endl;
265263
}
264+
265+
} // namespace ModuleIO

source/module_io/print_info.h

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,22 @@
22
// AUTHOR : mohan
33
// DATE : 2021-01-30
44
//==========================================================
5-
#ifndef PRINT_INFO_H
6-
#define PRINT_INFO_H
5+
#ifndef PRINT_INFO
6+
#define PRINT_INFO
77

88
#include "module_base/timer.h"
9-
#include "module_cell/unitcell.h"
109
#include "module_cell/klist.h"
11-
//#include "../module_cell/klist.h"
10+
#include "module_cell/unitcell.h"
1211

13-
class Print_Info
12+
namespace ModuleIO
1413
{
15-
public:
16-
17-
Print_Info();
18-
~Print_Info();
19-
20-
// print out to screen about the readin parameters
21-
static void setup_parameters(UnitCell &ucell, K_Vectors &kv);
2214

23-
static void print_time(time_t &time_start, time_t &time_finish);
24-
static void print_scf(const int &istep, const int &iter);
25-
static void print_screen(const int &stress_step, const int &force_step, const int &istep);
15+
// print out to screen about the readin parameters
16+
void setup_parameters(UnitCell& ucell, K_Vectors& kv);
17+
void print_time(time_t& time_start, time_t& time_finish);
18+
void print_scf(const int& istep, const int& iter);
19+
void print_screen(const int& stress_step, const int& force_step, const int& istep);
2620

27-
};
21+
} // namespace ModuleIO
2822

2923
#endif

source/module_io/test/print_info_test.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ TEST_F(PrintInfoTest, SetupParameters)
8383
PARAM.sys.gamma_only_local = false;
8484
PARAM.input.calculation = cal_type[i];
8585
testing::internal::CaptureStdout();
86-
EXPECT_NO_THROW(Print_Info::setup_parameters(*ucell,*kv));
87-
output = testing::internal::GetCapturedStdout();
86+
EXPECT_NO_THROW(ModuleIO::setup_parameters(*ucell, *kv));
87+
output = testing::internal::GetCapturedStdout();
8888
if(PARAM.input.calculation == "scf")
8989
{
9090
EXPECT_THAT(output,testing::HasSubstr("Self-consistent calculations"));
@@ -106,7 +106,7 @@ TEST_F(PrintInfoTest, SetupParameters)
106106
{
107107
PARAM.input.mdp.md_type = md_types[j];
108108
testing::internal::CaptureStdout();
109-
EXPECT_NO_THROW(Print_Info::setup_parameters(*ucell,*kv));
109+
EXPECT_NO_THROW(ModuleIO::setup_parameters(*ucell, *kv));
110110
output = testing::internal::GetCapturedStdout();
111111
EXPECT_THAT(output,testing::HasSubstr("Molecular Dynamics simulations"));
112112
if (PARAM.mdp.md_type == "fire")
@@ -141,8 +141,8 @@ TEST_F(PrintInfoTest, SetupParameters)
141141
{
142142
PARAM.input.basis_type = basis_type[i];
143143
testing::internal::CaptureStdout();
144-
EXPECT_NO_THROW(Print_Info::setup_parameters(*ucell,*kv));
145-
output = testing::internal::GetCapturedStdout();
144+
EXPECT_NO_THROW(ModuleIO::setup_parameters(*ucell, *kv));
145+
output = testing::internal::GetCapturedStdout();
146146
if(PARAM.input.basis_type == "lcao")
147147
{
148148
EXPECT_THAT(output,testing::HasSubstr("Use Systematically Improvable Atomic bases"));
@@ -170,22 +170,22 @@ TEST_F(PrintInfoTest, PrintScreen)
170170
if(PARAM.input.calculation=="scf")
171171
{
172172
testing::internal::CaptureStdout();
173-
Print_Info::print_screen(stress_step,force_step,istep);
174-
output = testing::internal::GetCapturedStdout();
173+
ModuleIO::print_screen(stress_step, force_step, istep);
174+
output = testing::internal::GetCapturedStdout();
175175
EXPECT_THAT(output,testing::HasSubstr("SELF-CONSISTENT"));
176176
}
177177
else if(PARAM.input.calculation=="nscf")
178178
{
179179
testing::internal::CaptureStdout();
180-
Print_Info::print_screen(stress_step,force_step,istep);
181-
output = testing::internal::GetCapturedStdout();
180+
ModuleIO::print_screen(stress_step, force_step, istep);
181+
output = testing::internal::GetCapturedStdout();
182182
EXPECT_THAT(output,testing::HasSubstr("NONSELF-CONSISTENT"));
183183
}
184184
else if(PARAM.input.calculation=="md")
185185
{
186186
testing::internal::CaptureStdout();
187-
Print_Info::print_screen(stress_step,force_step,istep);
188-
output = testing::internal::GetCapturedStdout();
187+
ModuleIO::print_screen(stress_step, force_step, istep);
188+
output = testing::internal::GetCapturedStdout();
189189
EXPECT_THAT(output,testing::HasSubstr("STEP OF MOLECULAR DYNAMICS"));
190190
}
191191
else
@@ -194,22 +194,22 @@ TEST_F(PrintInfoTest, PrintScreen)
194194
if(PARAM.input.calculation=="relax")
195195
{
196196
testing::internal::CaptureStdout();
197-
Print_Info::print_screen(stress_step,force_step,istep);
198-
output = testing::internal::GetCapturedStdout();
197+
ModuleIO::print_screen(stress_step, force_step, istep);
198+
output = testing::internal::GetCapturedStdout();
199199
EXPECT_THAT(output,testing::HasSubstr("STEP OF ION RELAXATION"));
200200
}
201201
else if(PARAM.input.calculation=="cell-relax")
202202
{
203203
testing::internal::CaptureStdout();
204-
Print_Info::print_screen(stress_step,force_step,istep);
205-
output = testing::internal::GetCapturedStdout();
204+
ModuleIO::print_screen(stress_step, force_step, istep);
205+
output = testing::internal::GetCapturedStdout();
206206
EXPECT_THAT(output,testing::HasSubstr("RELAX CELL"));
207207
EXPECT_THAT(output,testing::HasSubstr("RELAX IONS"));
208208
}
209209
PARAM.input.relax_new = true;
210210
testing::internal::CaptureStdout();
211-
Print_Info::print_screen(stress_step,force_step,istep);
212-
output = testing::internal::GetCapturedStdout();
211+
ModuleIO::print_screen(stress_step, force_step, istep);
212+
output = testing::internal::GetCapturedStdout();
213213
EXPECT_THAT(output,testing::HasSubstr("STEP OF RELAXATION"));
214214
}
215215
}
@@ -220,8 +220,8 @@ TEST_F(PrintInfoTest, PrintTime)
220220
time_t time_start = std::time(nullptr);
221221
time_t time_finish = std::time(nullptr);
222222
testing::internal::CaptureStdout();
223-
EXPECT_NO_THROW(Print_Info::print_time(time_start,time_finish));
224-
output = testing::internal::GetCapturedStdout();
223+
EXPECT_NO_THROW(ModuleIO::print_time(time_start, time_finish));
224+
output = testing::internal::GetCapturedStdout();
225225
EXPECT_THAT(output,testing::HasSubstr("START Time"));
226226
EXPECT_THAT(output,testing::HasSubstr("FINISH Time"));
227227
EXPECT_THAT(output,testing::HasSubstr("TOTAL Time"));

0 commit comments

Comments
 (0)