Repository navigation
Expand file tree
/
Copy pathglobal_function.h
More file actions
380 lines (339 loc) · 11 KB
/
Copy pathglobal_function.h
File metadata and controls
380 lines (339 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#ifndef GLOBAL_FUNCTION_H
#define GLOBAL_FUNCTION_H
#include "module_external/blas_connector.h"
#include "global_function-func_each_2.h" // Peize Lin add 2016-09-07
#include "global_variable.h"
#include "tool_check.h" // mohan add 2021-05-08
#include "tool_quit.h" // mohan add 2021-05-07
#include "tool_title.h" // mohan add 2021-05-05
#include <cassert>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <valarray>
#include <vector>
namespace ModuleBase
{
namespace GlobalFunc
{
void NOTE(const std::string& words);
void NEW_PART(const std::string& words);
//==========================================================
// GLOBAL FUNCTION :
// NAME : OUT( output date for checking )
// NAME : OUT( output date into file "ofs")
// NAME : OUTP( output parameters )
//==========================================================
void OUT(std::ofstream& ofs, const std::string& name);
template <class T>
void OUT(std::ofstream& ofs, const std::string& name, const T& a)
{
std::stringstream name2;
name2 << name;
ofs << " " << std::setw(40) << name2.str() << " = " << a << std::endl;
// ofs << " " << name << a << std::endl;
return;
}
template <class T>
void OUT(std::ofstream& ofs, const std::string& name, const T& x, const T& y)
{
ofs << " " << std::setw(40) << name << " = [ " << x << ", " << y << " ]" << std::endl;
// ofs << " " << name << a << std::endl;
return;
}
template <class T>
void OUT(std::ofstream& ofs, const std::string& name, const T& x, const T& y, const T& z)
{
ofs << " " << std::setw(40) << name << " = [ " << x << ", " << y << ", " << z << " ]" << std::endl;
return;
}
// output parameters and explanations
template <class T>
void OUTP(std::ofstream& ofs, const std::string& name, const T& a, const std::string& explanation = "")
{
ofs << std::setw(30) << name << " " << a << " #" << explanation << std::endl;
}
template <class T>
void OUT(const std::string& name, const T& a)
{
std::cout << " " << std::setw(40) << name << " = " << a << std::endl;
// std::cout << " " << name << a << std::endl;
return;
}
void OUT_TIME(const std::string& name, time_t& start, time_t& end);
//==========================================================
// GLOBAL FUNCTION :
// NAME : MAKE_DIR( make dir ,using system function)
//==========================================================
void MAKE_DIR(const std::string& file);
//==========================================================
// GLOBAL FUNCTION :
// NAME : AUTO_SET( auto_set variables )
//==========================================================
template <class T>
void AUTO_SET(const std::string& name, const T& a)
{
GlobalV::ofs_warning << " AUTO_SET " << name << " to " << a << std::endl;
return;
}
//==========================================================
// GLOBAL FUNCTION :
// NAME : DONE( ouput information(time) on screen and log)
// we can regard it as a milestone.
//==========================================================
void DONE(std::ofstream& ofs, const std::string& description, bool only_rank0 = false);
//==========================================================
// GLOBAL FUNCTION :
// NAME : ZEROS
// set elements of u as zero which u is 1_d std::complex array
//==========================================================
template <class T, class TI>
inline void ZEROS(std::complex<T>* u, const TI n) // Peize Lin change int to TI at 2020.03.03
{
assert(n >= 0);
for (TI i = 0; i < n; i++)
{
u[i] = std::complex<T>(0.0, 0.0);
}
return;
}
template <class T, class TI>
inline void ZEROS(T* u, const TI n) // Peize Lin change int to TI at 2020.03.03
{
assert(n >= 0);
for (TI i = 0; i < n; i++)
{
u[i] = 0;
}
}
//==========================================================
// GLOBAL FUNCTION :
// NAME : TEST_LEVEL
// control the test_level
//==========================================================
void TEST_LEVEL(const std::string& name, bool disable);
//==========================================================
// GLOBAL FUNCTION :
//==========================================================
template <class T>
static void READ_VALUE(std::ifstream& ifs, T& v)
{
ifs >> v;
std::string line;
getline(ifs, line);
return;
}
//-------------------------------------------------------------
//! The `SCAN_BEGIN` function efficiently searches
//! text files for specified keywords
//-------------------------------------------------------------
bool SCAN_BEGIN(std::ifstream& ifs,
const std::string& TargetName,
const bool restart = true,
const bool ifwarn = true);
//-------------------------------------------------------------
// The `SCAN_LINE_BEGIN` function efficiently searches
// text files for specified keywords while ignoring comment
// lines and whitespace. It skips any line starting with '#'
//-------------------------------------------------------------
bool SCAN_LINE_BEGIN(std::ifstream& ifs,
const std::string& TargetName,
const bool restart = true,
const bool ifwarn = true);
void SCAN_END(std::ifstream& ifs, const std::string& TargetName, const bool ifwarn = true);
template <class T>
static inline void DCOPY(const T& a, T& b, const int& dim)
{
for (int i = 0; i < dim; ++i) {
b[i] = a[i];
}
}
template <typename T>
inline void DCOPY(const T* a, T* b, const int& dim) {
for (int i = 0; i < dim; ++i) {
b[i] = a[i];
}
}
template <typename T>
inline void COPYARRAY(const T* a, T* b, const long dim);
template <>
inline void COPYARRAY(const std::complex<double>* a, std::complex<double>* b, const long dim)
{
const int one = 1;
zcopy_(&dim, a, &one, b, &one);
}
template <>
inline void COPYARRAY(const double* a, double* b, const long dim)
{
const int one = 1;
dcopy_(&dim, a, &one, b, &one);
}
void BLOCK_HERE(const std::string& description);
//==========================================================
// GLOBAL FUNCTION :
// NAME : VECTOR_TO_PTR
// change std::vector to pointer
// Peize Lin add 2016-02-25
//==========================================================
template <class T>
static inline T* VECTOR_TO_PTR(std::vector<T>& v)
{
return &(v[0]);
}
template <class T>
static inline T* VECTOR_TO_PTR(std::valarray<T>& v)
{
return &(v[0]);
}
template <class T>
static inline const T* VECTOR_TO_PTR(const std::vector<T>& v)
{
return &(v[0]);
}
template <class T>
static inline const T* VECTOR_TO_PTR(const std::valarray<T>& v)
{
return &(v[0]);
}
//==========================================================
// GLOBAL FUNCTION :
// NAME : TO_STRING
// change number to std::string
// example: 233 -> "233"
// Peize Lin add 2016-07-18
//==========================================================
template <typename T>
std::string TO_STRING(const T& t, const int n=20) // n=20 since LDBL_EPSILON is 1E-16 or 1E-19
{
std::stringstream newstr;
newstr << std::setprecision(n) << t;
return newstr.str();
}
//==========================================================
// GLOBAL FUNCTION :
// NAME : MAP_EXIST
// find whether exists the std::map index
// if exist return the ptr called, else return nullptr
// example: map_exist(ms,i,j,k) -> try to find ms[i][j][k]
// Peize Lin add 2018-07-16
//==========================================================
template <typename T_map, typename T_key1>
inline void* MAP_EXIST(T_map& ms, const T_key1& key1)
{
auto ms1 = ms.find(key1);
if (ms1 == ms.end()) {
return nullptr;
}
return static_cast<void*>(&ms1->second);
}
template <typename T_map, typename T_key1, typename... T_key_tail>
inline void* MAP_EXIST(T_map& ms, const T_key1& key1, const T_key_tail&... key_tail)
{
auto ms1 = ms.find(key1);
if (ms1 == ms.end()) {
return nullptr;
}
return MAP_EXIST(ms1->second, key_tail...);
}
template <typename T_map, typename T_key1>
inline const void* MAP_EXIST(const T_map& ms, const T_key1& key1)
{
auto ms1 = ms.find(key1);
if (ms1 == ms.end()) {
return nullptr;
}
return static_cast<const void*>(&ms1->second);
}
template <typename T_map, typename T_key1, typename... T_key_tail>
inline const void* MAP_EXIST(const T_map& ms, const T_key1& key1, const T_key_tail&... key_tail)
{
auto ms1 = ms.find(key1);
if (ms1 == ms.end()) {
return nullptr;
}
return MAP_EXIST(ms1->second, key_tail...);
}
//==========================================================
// GLOBAL FUNCTION :
// NAME : MemAvailable
// read /proc/meminfo
// unit: kB
// Peize Lin add 2019-12-21
//==========================================================
size_t MemAvailable();
//==========================================================
// GLOBAL FUNCTION :
// NAME : DELETE_MUL_PTR
// delete Multi-dimensional array pointer
// example:
// int*** v;
// DELETE_MUL_PTR(v,N1,N2);
// -> for(int i1=0; i1<N1; ++i1){
// for(int i2=0; i2<N2; ++i2){
// delete[] v[i1][i2]; v[i1][i2]=nullptr; }
// delete[] v[i1]; v[i1]=nullptr; }
// delete[] v; v=nullptr;
// Peize Lin add 2021-05-09
//==========================================================
template <typename T_element>
static inline void DELETE_MUL_PTR(T_element* v)
{
delete[] v;
v = nullptr;
}
template <typename T_element, typename T_N_first, typename... T_N_tail>
static inline void DELETE_MUL_PTR(T_element* v, const T_N_first N_first, const T_N_tail... N_tail)
{
for (T_N_first i = 0; i < N_first; ++i) {
DELETE_MUL_PTR(v[i], N_tail...);
}
delete[] v;
v = nullptr;
}
//==========================================================
// GLOBAL FUNCTION :
// NAME : FREE_MUL_PTR
// delete Multi-dimensional array pointer
// example:
// int*** v;
// DELETE_MUL_PTR(v,N1,N2);
// -> for(int i1=0; i1<N1; ++i1){
// for(int i2=0; i2<N2; ++i2){
// free(v[i1][i2]); v[i1][i2]=nullptr; }
// free(v[i1]); v[i1]=nullptr; }
// free(v); v=nullptr;
// Peize Lin add 2021-05-09
//==========================================================
template <typename T_element>
static inline void FREE_MUL_PTR(T_element* v)
{
free(v);
v = nullptr;
}
template <typename T_element, typename T_N_first, typename... T_N_tail>
static inline void FREE_MUL_PTR(T_element* v, const T_N_first N_first, const T_N_tail... N_tail)
{
for (T_N_first i = 0; i < N_first; ++i) {
FREE_MUL_PTR(v[i], N_tail...);
}
free(v);
v = nullptr;
}
template <typename T>
T ddot_real(const int& dim, const std::complex<T>* psi_L, const std::complex<T>* psi_R, const bool reduce = true);
//==========================================================
// GLOBAL FUNCTION :
// NAME : IS_COLUMN_MAJOR_KS_SOLVER
// check ks_solver requires column major or not
//==========================================================
static inline bool IS_COLUMN_MAJOR_KS_SOLVER(std::string ks_solver)
{
return ks_solver == "genelpa" || ks_solver == "elpa" || ks_solver == "scalapack_gvx" || ks_solver == "cusolver"
|| ks_solver == "cusolvermp" || ks_solver == "cg_in_lcao" || ks_solver == "pexsi" || ks_solver == "lapack";
}
} // namespace GlobalFunc
} // namespace ModuleBase
#endif