Skip to content

Commit 7ddd3d1

Browse files
rootroot
authored andcommitted
UT for global_function in module_base
1 parent dca93ce commit 7ddd3d1

File tree

3 files changed

+436
-129
lines changed

3 files changed

+436
-129
lines changed

source/module_base/global_function.cpp

Lines changed: 103 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,55 @@
22
// AUTHOR : mohan
33
//==========================================================
44
#include "global_function.h"
5-
#include "global_file.h"
5+
66
#include "../src_parallel/parallel_common.h"
7+
#include "global_file.h"
78

89
//==========================================================
910
// USE FILE timer.h
1011
// ONLY : output time after quit.
1112
//==========================================================
12-
#include "timer.h"
1313
#include "memory.h"
14+
#include "timer.h"
1415

15-
#include <string>
1616
#include <fstream>
1717
#include <iostream>
18+
#include <string>
1819
namespace ModuleBase
1920
{
2021
namespace GlobalFunc
2122
{
2223

2324
void NOTE(const std::string &words)
2425
{
25-
return;
26-
if(GlobalV::ofs_running)
27-
{
28-
//GlobalV::ofs_running << " *********************************************************************************" << std::endl;
29-
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
30-
GlobalV::ofs_running << " " << words << std::endl;
31-
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
32-
}
26+
if (GlobalV::ofs_running)
27+
{
28+
// GlobalV::ofs_running << " *********************************************************************************"
29+
// << std::endl;
30+
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
31+
<< std::endl;
32+
GlobalV::ofs_running << " " << words << std::endl;
33+
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
34+
<< std::endl;
35+
}
36+
return;
3337
}
3438

3539
void NEW_PART(const std::string &words)
3640
{
37-
GlobalV::ofs_running << "\n ><><><><><><><><><><><><><><><><><><><><><><" << std::endl;
38-
GlobalV::ofs_running << "\n " << words << std::endl;
39-
GlobalV::ofs_running << "\n ><><><><><><><><><><><><><><><><><><><><><><\n" << std::endl;
40-
return;
41+
GlobalV::ofs_running << "\n ><><><><><><><><><><><><><><><><><><><><><><" << std::endl;
42+
GlobalV::ofs_running << "\n " << words << std::endl;
43+
GlobalV::ofs_running << "\n ><><><><><><><><><><><><><><><><><><><><><><\n" << std::endl;
44+
return;
4145
}
4246

43-
4447
//==========================================================
4548
// GLOBAL FUNCTION :
4649
// NAME : OUT( output date for checking )
4750
//==========================================================
48-
void OUT(std::ofstream &ofs,const std::string &name)
51+
void OUT(std::ofstream &ofs, const std::string &name)
4952
{
50-
ofs<<"\n"<<std::setw(18)<<name<<std::endl;
53+
ofs << "\n" << std::setw(18) << name << std::endl;
5154
return;
5255
}
5356

@@ -57,17 +60,17 @@ void OUT(std::ofstream &ofs,const std::string &name)
5760
//==========================================================
5861
void MAKE_DIR(const std::string &fn)
5962
{
60-
// ModuleBase::TITLE("global_function","MAKE_DIR");
61-
if (GlobalV::MY_RANK==0)
63+
// ModuleBase::TITLE("global_function","MAKE_DIR");
64+
if (GlobalV::MY_RANK == 0)
6265
{
6366
std::stringstream ss;
64-
ss << " test -d " << fn << " || mkdir " << fn ;
65-
//----------------------------------------------------------
66-
// EXPLAIN : 'system' function return '0' if success
67-
//----------------------------------------------------------
68-
if ( system( ss.str().c_str() ) )
67+
ss << " test -d " << fn << " || mkdir " << fn;
68+
//----------------------------------------------------------
69+
// EXPLAIN : 'system' function return '0' if success
70+
//----------------------------------------------------------
71+
if (system(ss.str().c_str()))
6972
{
70-
ModuleBase::WARNING_QUIT( "MAKE_DIR", fn );
73+
ModuleBase::WARNING_QUIT("MAKE_DIR", fn);
7174
}
7275
}
7376
#ifdef __MPI
@@ -76,31 +79,31 @@ void MAKE_DIR(const std::string &fn)
7679
return;
7780
}
7881

79-
80-
void DONE(std::ofstream &ofs,const std::string &description, const bool only_rank0)
82+
void DONE(std::ofstream &ofs, const std::string &description, const bool only_rank0)
8183
{
8284
if (only_rank0)
8385
{
84-
if (GlobalV::MY_RANK==0)
86+
if (GlobalV::MY_RANK == 0)
8587
{
86-
// ofs << " ---------------------------------------------------------------------------------\n";
88+
// ofs << " ---------------------------------------------------------------------------------\n";
8789
ofs << " DONE : " << description;
88-
ofs << " Time : "<< ModuleBase::timer::print_until_now() << " (SEC)" ;
89-
ofs << std::endl << std::endl;
90-
// ofs << "\n ---------------------------------------------------------------------------------\n";
90+
ofs << " Time : " << ModuleBase::timer::print_until_now() << " (SEC)";
91+
ofs << std::endl << std::endl;
92+
// ofs << "\n ---------------------------------------------------------------------------------\n";
9193
}
9294
}
9395
else
9496
{
95-
// ofs << " ---------------------------------------------------------------------------------\n";
97+
// ofs << " ---------------------------------------------------------------------------------\n";
9698
ofs << " DONE : " << description;
97-
ofs << " Time : "<< ModuleBase::timer::print_until_now() << " (SEC)" ;
98-
ofs << std::endl << std::endl;
99-
// ofs << "\n ---------------------------------------------------------------------------------\n";
99+
ofs << " Time : " << ModuleBase::timer::print_until_now() << " (SEC)";
100+
ofs << std::endl << std::endl;
101+
// ofs << "\n ---------------------------------------------------------------------------------\n";
100102
}
101-
// std::cout << "\n---------------------------------------------------------------------------------\n";
102-
std::cout << " DONE(" << std::setw(10) << ModuleBase::timer::print_until_now() <<" SEC) : "<< description << std::endl;
103-
// std::cout << "\n---------------------------------------------------------------------------------\n";
103+
// std::cout << "\n---------------------------------------------------------------------------------\n";
104+
std::cout << " DONE(" << std::setw(10) << ModuleBase::timer::print_until_now() << " SEC) : " << description
105+
<< std::endl;
106+
// std::cout << "\n---------------------------------------------------------------------------------\n";
104107
return;
105108
}
106109

@@ -112,7 +115,8 @@ void DONE(std::ofstream &ofs,const std::string &description, const bool only_ran
112115
void TEST_LEVEL(const std::string &name)
113116
{
114117
bool disable = true;
115-
if (disable) return;
118+
if (disable)
119+
return;
116120

117121
if (name == "none")
118122
{
@@ -142,13 +146,11 @@ void TEST_LEVEL(const std::string &name)
142146
else if (name == "pw_init")
143147
{
144148
GlobalV::test_pw = 1;
145-
146149
}
147150

148151
return;
149152
}
150153

151-
152154
bool SCAN_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool restart)
153155
{
154156
std::string SearchName;
@@ -162,97 +164,100 @@ bool SCAN_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool re
162164
while (ifs.good())
163165
{
164166
ifs >> SearchName;
165-
if ( SearchName == TargetName)
167+
if (SearchName == TargetName)
166168
{
167169
find = true;
168170
break;
169171
}
170172
}
171173
if (!find)
172174
{
173-
GlobalV::ofs_warning<<" In SCAN_BEGIN, can't find: "<<TargetName<<" block."<<std::endl;
175+
GlobalV::ofs_warning << " In SCAN_BEGIN, can't find: " << TargetName << " block." << std::endl;
174176
}
175177
return find;
176178
}
177179

178-
179180
void SCAN_END(std::ifstream &ifs, const std::string &TargetName)
180181
{
181182
std::string SearchName;
182183
ifs >> SearchName;
183-
if ( SearchName != TargetName)
184+
if (SearchName != TargetName)
184185
{
185-
GlobalV::ofs_warning<<" In SCAN_END, can't find: "<<TargetName<<" block."<<std::endl;
186+
GlobalV::ofs_warning << " In SCAN_END, can't find: " << TargetName << " block." << std::endl;
186187
}
187188
return;
188189
}
189190

190-
void BLOCK_HERE( const std::string &description)
191+
void BLOCK_HERE(const std::string &description)
191192
{
192-
// return;
193-
std::cout << "\n********************************************";
193+
// return;
194+
std::cout << "\n********************************************";
194195
std::cout << "\n Here is a Block, 1: go on 0: quit";
195196
std::cout << "\n " << description;
196-
std::cout << "\n********************************************" << std::endl;
197+
std::cout << "\n********************************************" << std::endl;
197198
bool go_on = false;
198-
if(GlobalV::MY_RANK==0)
199-
{
200-
std::cin >> go_on;
201-
}
199+
if (GlobalV::MY_RANK == 0)
200+
{
201+
std::cin >> go_on;
202+
}
202203

203204
#ifdef __MPI
204-
int swap = go_on;
205-
if(GlobalV::MY_RANK == 0)swap = go_on;
206-
MPI_Bcast(&swap, 1, MPI_INT, 0, MPI_COMM_WORLD);
207-
if(GlobalV::MY_RANK != 0)go_on = static_cast<bool>( swap );
205+
int swap = go_on;
206+
if (GlobalV::MY_RANK == 0)
207+
swap = go_on;
208+
MPI_Bcast(&swap, 1, MPI_INT, 0, MPI_COMM_WORLD);
209+
if (GlobalV::MY_RANK != 0)
210+
go_on = static_cast<bool>(swap);
208211
#endif
209-
if(go_on)
210-
{
211-
return;
212-
}
213-
else
214-
{
215-
ModuleBase::QUIT();
216-
}
212+
if (go_on)
213+
{
214+
return;
215+
}
216+
else
217+
{
218+
ModuleBase::QUIT();
219+
}
217220
}
218221

219-
220222
void OUT_TIME(const std::string &name, time_t &start, time_t &end)
221223
{
222-
double mini = difftime(end, start)/60.0;
223-
if(mini>0.1)
224+
double mini = difftime(end, start) / 60.0;
225+
if (mini > 0.1)
226+
{
227+
if(GlobalV::ofs_warning)
224228
{
225-
GlobalV::ofs_warning << std::setprecision(2);
226-
GlobalV::ofs_warning << " -------------------------------------------------------" << std::endl;
227-
GlobalV::ofs_warning << " NAME < " << name << " > = " << std::endl;
228-
GlobalV::ofs_warning << " -> " << ctime(&start) << " -> " << ctime(&end);
229-
GlobalV::ofs_warning << " TIME = " << mini << " [Minutes]" << std::endl;
230-
GlobalV::ofs_warning << " -------------------------------------------------------" << std::endl;
231-
GlobalV::ofs_warning << std::setprecision(6);
229+
GlobalV::ofs_warning << std::setprecision(2);
230+
GlobalV::ofs_warning << " -------------------------------------------------------" << std::endl;
231+
GlobalV::ofs_warning << " NAME < " << name << " > = " << std::endl;
232+
GlobalV::ofs_warning << " -> " << ctime(&start) << " -> " << ctime(&end);
233+
GlobalV::ofs_warning << " TIME = " << mini << " [Minutes]" << std::endl;
234+
GlobalV::ofs_warning << " -------------------------------------------------------" << std::endl;
235+
GlobalV::ofs_warning << std::setprecision(6);
232236
}
237+
}
233238
}
234239

235240
size_t MemAvailable()
236241
{
237-
size_t mem_sum = 0;
238-
int i=0;
239-
std::ifstream ifs("/proc/meminfo");
240-
while(ifs.good())
241-
{
242-
std::string label, size, kB;
243-
ifs>>label>>size>>kB;
244-
if(label=="MemAvailable:")
245-
return std::stol(size);
246-
else if(label=="MemFree:" || label=="Buffers:" || label=="Cached:")
247-
{
248-
mem_sum += std::stol(size);
249-
++i;
250-
}
251-
if(i==3)
252-
return mem_sum;
253-
}
254-
throw std::runtime_error("read /proc/meminfo error in "+TO_STRING(__FILE__)+" line "+TO_STRING(__LINE__));
242+
size_t mem_sum = 0;
243+
int i = 0;
244+
std::ifstream ifs("/proc/meminfo");
245+
while (ifs.good())
246+
{
247+
std::string label, size, kB;
248+
ifs >> label >> size >> kB;
249+
if (label == "MemAvailable:")
250+
return std::stol(size);
251+
else if (label == "MemFree:" || label == "Buffers:" || label == "Cached:")
252+
{
253+
mem_sum += std::stol(size);
254+
++i;
255+
}
256+
if (i == 3)
257+
return mem_sum;
258+
}
259+
throw std::runtime_error("read /proc/meminfo error in " + TO_STRING(__FILE__) + " line " + TO_STRING(__LINE__));
255260
}
256261

257-
}
258-
}
262+
} // namespace GlobalFunc
263+
} // namespace ModuleBase

0 commit comments

Comments
 (0)