Skip to content

Commit ce45c3f

Browse files
authored
Update Hamiltonian and Operator classes (deepmodeling#6084)
* update some timers * update timer * update output formats * update print_cell * add ofs in print_tau * update print_tau * update output formats * update md outputs * beging modifying the autotests * update integrate tests * update some timers * fix total force and total stress * update integrate tests * update print_band * fix print_band * update md print out information * update print_stress * update print_stress in dp, but still has problems in lj * fix dp_test.cpp * update esolver_lj * set mulliken charge accuracy from 4 to 3 * update print force and print stress * delete kv variable in hamilt_lcao * update some operators * update hamiltonian and operator * update updateHk funciton parameters * fix bug * fix bug in updateHk * update updateSk * update read atoms * update SCAN_BEGIN, now the function ignore lines starting with # * update * add SCAN_LINE_BEGIN * fix bug * fix issue
1 parent 06cffb7 commit ce45c3f

25 files changed

+722
-483
lines changed

source/module_base/global_function.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ void DONE(std::ofstream &ofs, const std::string &description, const bool only_ra
104104
return;
105105
}
106106

107-
bool SCAN_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool restart, const bool ifwarn)
107+
108+
bool SCAN_BEGIN(std::ifstream &ifs,
109+
const std::string &TargetName,
110+
const bool restart,
111+
const bool ifwarn)
108112
{
109113
std::string SearchName;
110114
bool find = false;
@@ -130,6 +134,51 @@ bool SCAN_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool re
130134
return find;
131135
}
132136

137+
138+
bool SCAN_LINE_BEGIN(std::ifstream &ifs,
139+
const std::string &TargetName,
140+
const bool restart,
141+
const bool ifwarn)
142+
{
143+
bool find = false;
144+
if (restart)
145+
{
146+
ifs.clear();
147+
ifs.seekg(0);
148+
}
149+
ifs.rdstate();
150+
151+
std::string line;
152+
while (std::getline(ifs,line))
153+
{
154+
//! obtain the first character, should not be #
155+
size_t first_char_pos = line.find_first_not_of(" \t");
156+
if (first_char_pos != std::string::npos && line[first_char_pos] == '#')
157+
{
158+
continue;
159+
}
160+
161+
//! search in each line
162+
std::istringstream iss(line);
163+
std::string SearchName;
164+
while (iss >> SearchName)
165+
{
166+
if (SearchName == TargetName)
167+
{
168+
find = true;
169+
//std::cout << " search name = " << SearchName << std::endl;
170+
return find;
171+
}
172+
}
173+
}
174+
175+
if (!find && ifwarn)
176+
{
177+
GlobalV::ofs_warning << " In SCAN_LINE_BEGIN, can't find: " << TargetName << " block." << std::endl;
178+
}
179+
return find;
180+
}
181+
133182
void SCAN_END(std::ifstream &ifs, const std::string &TargetName, const bool ifwarn)
134183
{
135184
std::string SearchName;

source/module_base/global_function.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_BASE_GLOBAL_FUNCTION_H
2-
#define W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_BASE_GLOBAL_FUNCTION_H
1+
#ifndef GLOBAL_FUNCTION_H
2+
#define GLOBAL_FUNCTION_H
33

44
#include "blas_connector.h"
55
#include "global_function-func_each_2.h" // Peize Lin add 2016-09-07
@@ -145,13 +145,26 @@ static void READ_VALUE(std::ifstream& ifs, T& v)
145145
return;
146146
}
147147

148-
bool SCAN_BEGIN(std::ifstream& ifs, const std::string& TargetName, const bool restart = true, const bool ifwarn = true);
149-
// ifwarn: whether to call GlobalV::ofs_warning when the TargetName is not found, used to avoid invalid warning.
150-
// Mohan warning : the last term can't be written as const bool &restart,
151-
// I don't know why.
148+
//-------------------------------------------------------------
149+
//! The `SCAN_BEGIN` function efficiently searches
150+
//! text files for specified keywords
151+
//-------------------------------------------------------------
152+
bool SCAN_BEGIN(std::ifstream& ifs,
153+
const std::string& TargetName,
154+
const bool restart = true,
155+
const bool ifwarn = true);
156+
157+
//-------------------------------------------------------------
158+
// The `SCAN_LINE_BEGIN` function efficiently searches
159+
// text files for specified keywords while ignoring comment
160+
// lines and whitespace. It skips any line starting with '#'
161+
//-------------------------------------------------------------
162+
bool SCAN_LINE_BEGIN(std::ifstream& ifs,
163+
const std::string& TargetName,
164+
const bool restart = true,
165+
const bool ifwarn = true);
152166

153167
void SCAN_END(std::ifstream& ifs, const std::string& TargetName, const bool ifwarn = true);
154-
// ifwarn: whether to call GlobalV::ofs_warning when the TargetName is not found, used to avoid invalid warning.
155168

156169
template <class T>
157170
static inline void DCOPY(const T& a, T& b, const int& dim)

source/module_cell/read_atom_species.cpp

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ bool read_atom_species(std::ifstream& ifa,
1313
UnitCell& ucell)
1414
{
1515
ModuleBase::TITLE("UnitCell","read_atom_species");
16+
1617
const int ntype = ucell.ntype;
1718
std::string word;
1819

1920
//==========================================
2021
// read in information of each type of atom
2122
//==========================================
22-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "ATOMIC_SPECIES") )
23+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "ATOMIC_SPECIES") )
2324
{
24-
ifa.ignore(300, '\n');
2525
ModuleBase::GlobalFunc::OUT(ofs_running,"ntype",ntype);
2626
for (int i = 0;i < ntype;i++)
2727
{
@@ -51,7 +51,8 @@ bool read_atom_species(std::ifstream& ifa,
5151

5252
if (!end && ss >> one_string && one_string[0] != '#')
5353
{
54-
if (one_string == "auto" || one_string == "upf" || one_string == "vwr" || one_string == "upf201" || one_string == "blps")
54+
if (one_string == "auto" || one_string == "upf"
55+
|| one_string == "vwr" || one_string == "upf201" || one_string == "blps")
5556
{
5657
ucell.pseudo_type[i] = one_string;
5758
}
@@ -61,15 +62,17 @@ bool read_atom_species(std::ifstream& ifa,
6162
}
6263
else
6364
{
64-
GlobalV::ofs_warning << "unrecongnized pseudopotential type: " << one_string << ", check your STRU file." << std::endl;
65+
GlobalV::ofs_warning << "unrecongnized pseudopotential type: "
66+
<< one_string << ", check your STRU file." << std::endl;
6567
ModuleBase::WARNING_QUIT("read_atom_species", "unrecongnized pseudo type.");
6668
}
6769
}
6870

6971
// Peize Lin test for bsse 2021.04.07
7072
const std::string bsse_label = "empty";
7173
ucell.atoms[i].flag_empty_element =
72-
(search( ucell.atom_label[i].begin(), ucell.atom_label[i].end(), bsse_label.begin(), bsse_label.end() ) != ucell.atom_label[i].end())
74+
(search( ucell.atom_label[i].begin(), ucell.atom_label[i].end(),
75+
bsse_label.begin(), bsse_label.end() ) != ucell.atom_label[i].end())
7376
? true : false;
7477
}
7578
}
@@ -80,7 +83,7 @@ bool read_atom_species(std::ifstream& ifa,
8083
||((PARAM.inp.basis_type == "pw")&&(PARAM.inp.init_wfc.substr(0, 3) == "nao"))
8184
|| PARAM.inp.onsite_radius > 0.0)
8285
{
83-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "NUMERICAL_ORBITAL") )
86+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "NUMERICAL_ORBITAL") )
8487
{
8588
for(int i=0; i<ntype; i++)
8689
{
@@ -90,7 +93,7 @@ bool read_atom_species(std::ifstream& ifa,
9093
// caoyu add 2021-03-16
9194
if(PARAM.globalv.deepks_setorb)
9295
{
93-
if (ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "NUMERICAL_DESCRIPTOR")) {
96+
if (ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "NUMERICAL_DESCRIPTOR")) {
9497
ifa >> ucell.descriptor_file;
9598
}
9699
}
@@ -105,7 +108,7 @@ bool read_atom_species(std::ifstream& ifa,
105108
#ifdef __EXX
106109
if( GlobalC::exx_info.info_global.cal_exx || PARAM.inp.rpa )
107110
{
108-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "ABFS_ORBITAL") )
111+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "ABFS_ORBITAL") )
109112
{
110113
for(int i=0; i<ntype; i++)
111114
{
@@ -133,7 +136,7 @@ bool read_lattice_constant(std::ifstream& ifa,
133136
double& lat0_angstrom =lat.lat0_angstrom;
134137
std::string& latName = lat.latName;
135138
ModuleBase::Matrix3& latvec = lat.latvec;
136-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_CONSTANT") )
139+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_CONSTANT") )
137140
{
138141
ModuleBase::GlobalFunc::READ_VALUE(ifa, lat0);
139142
if(lat0<=0.0)
@@ -153,18 +156,23 @@ bool read_lattice_constant(std::ifstream& ifa,
153156

154157
if(latName=="none")
155158
{
156-
if (ModuleBase::GlobalFunc::SCAN_BEGIN(ifa,
159+
// check the existence of keyword "LATTICE_PARAMETERS"
160+
if (ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa,
157161
"LATTICE_PARAMETERS",
158162
true,
159163
false))
160164
{
161-
ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant","do not use LATTICE_PARAMETERS without explicit specification of lattice type");
165+
ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant",
166+
"do not use LATTICE_PARAMETERS without explicit specification of lattice type");
162167
}
163-
if( !ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_VECTORS") )
168+
169+
// check the existence of keyword "LATTICE_VECTORS"
170+
if( !ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_VECTORS") )
164171
{
165-
ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant","Please set LATTICE_VECTORS in STRU file");
172+
ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant",
173+
"Please set LATTICE_VECTORS in STRU file");
166174
}
167-
else if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_VECTORS") )
175+
else if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_VECTORS") )
168176
{
169177
// Reading lattice vectors. notice
170178
// here that only one cpu read these
@@ -179,12 +187,13 @@ bool read_lattice_constant(std::ifstream& ifa,
179187
}//supply lattice vectors
180188
else
181189
{
182-
if (ModuleBase::GlobalFunc::SCAN_BEGIN(ifa,
190+
if (ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa,
183191
"LATTICE_VECTORS",
184192
true,
185193
false))
186194
{
187-
ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant","do not use LATTICE_VECTORS along with explicit specification of lattice type");
195+
ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant",
196+
"do not use LATTICE_VECTORS along with explicit specification of lattice type");
188197
}
189198
if(latName=="sc")
190199
{//simple-cubic, ibrav = 1
@@ -210,7 +219,7 @@ bool read_lattice_constant(std::ifstream& ifa,
210219
latvec.e11 = 1.0; latvec.e12 = 0.0; latvec.e13 = 0.0;
211220
latvec.e21 =-0.5; latvec.e22 = e22; latvec.e23 = 0.0;
212221
latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0;
213-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") )
222+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") )
214223
{
215224
ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33);
216225
}
@@ -219,7 +228,7 @@ bool read_lattice_constant(std::ifstream& ifa,
219228
{//trigonal, ibrav = 5
220229
double t1 = 0.0;
221230
double t2 = 0.0;
222-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") )
231+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") )
223232
{
224233
double cosab=0.0;
225234
ModuleBase::GlobalFunc::READ_VALUE(ifa, cosab);
@@ -239,15 +248,15 @@ bool read_lattice_constant(std::ifstream& ifa,
239248
latvec.e11 = 1.0; latvec.e12 = 0.0; latvec.e13 = 0.0;
240249
latvec.e21 = 0.0; latvec.e22 = 1.0; latvec.e23 = 0.0;
241250
latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0;
242-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") )
251+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") )
243252
{
244253
ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33);
245254
}
246255
}
247256
else if(latName=="bct")
248257
{//body-centered tetragonal, ibrav = 7
249258
double cba = 0.0;
250-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") )
259+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") )
251260
{
252261
ModuleBase::GlobalFunc::READ_VALUE(ifa, cba);
253262
cba = cba / 2.0;
@@ -261,7 +270,7 @@ bool read_lattice_constant(std::ifstream& ifa,
261270
latvec.e11 = 1.0; latvec.e12 = 0.0; latvec.e13 = 0.0;
262271
latvec.e21 = 0.0; latvec.e22 = 0.0; latvec.e23 = 0.0;
263272
latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0;
264-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") )
273+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") )
265274
{
266275
ifa >> latvec.e22;
267276
ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33);
@@ -272,7 +281,7 @@ bool read_lattice_constant(std::ifstream& ifa,
272281
latvec.e11 = 0.5; latvec.e12 = 0.0; latvec.e13 = 0.0;
273282
latvec.e21 =-0.5; latvec.e22 = 0.0; latvec.e23 = 0.0;
274283
latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0;
275-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") )
284+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") )
276285
{
277286
ifa >> latvec.e12;
278287
latvec.e12 = latvec.e12 / 2.0;
@@ -283,7 +292,7 @@ bool read_lattice_constant(std::ifstream& ifa,
283292
else if(latName=="fco")
284293
{//face-centered orthorhombic, ibrav = 10
285294
double bba = 0.0; double cba = 0.0;
286-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") )
295+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") )
287296
{
288297
ifa >> bba;
289298
ModuleBase::GlobalFunc::READ_VALUE(ifa, cba);
@@ -296,7 +305,7 @@ bool read_lattice_constant(std::ifstream& ifa,
296305
else if(latName=="bco")
297306
{//body-centered orthorhombic, ibrav = 11
298307
double bba = 0.0; double cba = 0.0;
299-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") )
308+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") )
300309
{
301310
ifa >> bba;
302311
ModuleBase::GlobalFunc::READ_VALUE(ifa, cba);
@@ -311,7 +320,7 @@ bool read_lattice_constant(std::ifstream& ifa,
311320
double bba = 0.0; double cba = 0.0;
312321
double cosab = 0.0;
313322
double e21 = 0.0; double e22 = 0.0;
314-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") )
323+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") )
315324
{
316325
ifa >> bba >> cba;
317326
ModuleBase::GlobalFunc::READ_VALUE(ifa, cosab);
@@ -327,7 +336,7 @@ bool read_lattice_constant(std::ifstream& ifa,
327336
double bba = 0.0; double cba = 0.0;
328337
double cosab = 0.0;
329338
double e21 = 0.0; double e22 = 0.0;
330-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") )
339+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") )
331340
{
332341
ifa >> bba >> cba;
333342
ModuleBase::GlobalFunc::READ_VALUE(ifa, cosab);
@@ -348,7 +357,7 @@ bool read_lattice_constant(std::ifstream& ifa,
348357
double cosbc = 0.0;
349358
double sinab = 0.0;
350359
double term = 0.0;
351-
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") )
360+
if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") )
352361
{
353362
ifa >> bba >> cba >> cosab >> cosac;
354363
ModuleBase::GlobalFunc::READ_VALUE(ifa, cosbc);
@@ -363,7 +372,6 @@ bool read_lattice_constant(std::ifstream& ifa,
363372
}
364373
else
365374
{
366-
std::cout << "latname is : " << latName << std::endl;
367375
ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant","latname not supported!");
368376
}
369377
}
@@ -383,4 +391,4 @@ bool read_lattice_constant(std::ifstream& ifa,
383391
return true;
384392
}
385393

386-
}
394+
}

0 commit comments

Comments
 (0)