Skip to content

Commit d5b6acd

Browse files
committed
add SCAN_LINE_BEGIN
1 parent a13470f commit d5b6acd

File tree

4 files changed

+132
-44
lines changed

4 files changed

+132
-44
lines changed

source/module_base/global_function.cpp

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

107+
107108
bool SCAN_BEGIN(std::ifstream &ifs,
108109
const std::string &TargetName,
109110
const bool restart,
110111
const bool ifwarn)
112+
{
113+
std::string SearchName;
114+
bool find = false;
115+
if (restart)
116+
{
117+
ifs.clear();
118+
ifs.seekg(0);
119+
}
120+
ifs.rdstate();
121+
while (ifs.good())
122+
{
123+
ifs >> SearchName;
124+
if (SearchName == TargetName)
125+
{
126+
find = true;
127+
break;
128+
}
129+
}
130+
if (!find && ifwarn)
131+
{
132+
GlobalV::ofs_warning << " In SCAN_BEGIN, can't find: " << TargetName << " block." << std::endl;
133+
}
134+
return find;
135+
}
136+
137+
138+
bool SCAN_LINE_BEGIN(std::ifstream &ifs,
139+
const std::string &TargetName,
140+
const bool restart,
141+
const bool ifwarn)
111142
{
112143
bool find = false;
113144
if (restart)
@@ -143,7 +174,7 @@ bool SCAN_BEGIN(std::ifstream &ifs,
143174

144175
if (!find && ifwarn)
145176
{
146-
GlobalV::ofs_warning << " In SCAN_BEGIN, can't find: " << TargetName << " block." << std::endl;
177+
GlobalV::ofs_warning << " In SCAN_LINE_BEGIN, can't find: " << TargetName << " block." << std::endl;
147178
}
148179
return find;
149180
}

source/module_base/global_function.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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_pp_upf201.cpp

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ int Pseudopot_upf::read_pseudo_upf201(std::ifstream &ifs, Atom_pseudo& pp)
109109

110110
void Pseudopot_upf::getnameval(std::ifstream& ifs, int& n, std::string* name, std::string* val)
111111
{
112-
std::string txt, word;
112+
std::string txt;
113+
std::string word;
114+
113115
// get long txt
114116
ifs >> txt;
115117
while (ifs >> word)
@@ -128,26 +130,32 @@ void Pseudopot_upf::getnameval(std::ifstream& ifs, int& n, std::string* name, st
128130
while (1)
129131
{
130132
pos = txt.find("=", pos);
131-
if (pos == std::string::npos)
132-
break;
133-
pos++;
133+
if (pos == std::string::npos)
134+
{
135+
break;
136+
}
137+
pos++;
134138
n++;
135139
}
136140

141+
std::cout << "n=" << n << std::endl;
142+
137143
// get name & value
138144
pos = 0;
139-
size_t pos2, ll;
145+
size_t pos2=0;
146+
size_t ll=0;
140147
for (int i = 0; i < n; ++i)
141148
{
142149
pos2 = txt.find("=", pos);
143150
for (; pos2 > pos; --pos2) // There may be a space before "=";
144151
{
145-
if (txt.substr(pos2 - 1, 1) != " ")
146-
break;
152+
if (txt.substr(pos2 - 1, 1) != " ")
153+
{
154+
break;
155+
}
147156
}
148157
ll = pos2 - pos;
149158
name[i] = txt.substr(pos, ll);
150-
// std::cout<<i<<" "<<name[i]<<std::endl;
151159
std::string mark;
152160
bool findmark = false;
153161
for (int j = 0; j < 100; ++j) // The mark can be ' or " or .
@@ -160,10 +168,12 @@ void Pseudopot_upf::getnameval(std::ifstream& ifs, int& n, std::string* name, st
160168
break;
161169
}
162170
}
163-
if (!findmark)
164-
ModuleBase::WARNING_QUIT(
165-
"Pseudopot_upf::getnameval",
166-
"The values are not in \' or \". Please improve the program in read_pp_upf201.cpp");
171+
if (!findmark)
172+
{
173+
ModuleBase::WARNING_QUIT(
174+
"Pseudopot_upf::getnameval",
175+
"The values are not in \' or \". Please improve the program in read_pp_upf201.cpp");
176+
}
167177
pos = pos2;
168178
pos2 = txt.find(mark, pos);
169179
ll = pos2 - pos;
@@ -172,24 +182,33 @@ void Pseudopot_upf::getnameval(std::ifstream& ifs, int& n, std::string* name, st
172182
val[i] = tmpval;
173183
pos = pos2 + 1;
174184
for (int j = 0; j < 100; ++j)
175-
{
176-
if (txt.substr(pos, 1) == " " || txt.substr(pos, 1) == ",")
177-
pos++;
178-
else
179-
break;
180-
}
181-
// std::cout<<name[i]<<"=\""<<val[i]<<"\""<<std::endl;
185+
{
186+
if (txt.substr(pos, 1) == " " || txt.substr(pos, 1) == ",")
187+
{
188+
pos++;
189+
}
190+
else
191+
{
192+
break;
193+
}
194+
}
195+
//std::cout<<name[i]<<"=\""<<val[i]<<"\""<<std::endl;
182196
}
183197
return;
184198
}
185199

186200
void Pseudopot_upf::read_pseudo_upf201_header(std::ifstream& ifs, Atom_pseudo& pp)
187201
{
188-
if (!ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_HEADER"))
189-
ModuleBase::WARNING_QUIT("Pseudopot_upf::read_pseudo_upf201_header", "Found no PP_HEADER");
190-
std::string name[50];
191-
std::string val[50];
192-
int nparameter;
202+
if (!ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_HEADER"))
203+
{
204+
ModuleBase::WARNING_QUIT("Pseudopot_upf::read_pseudo_upf201_header", "Found no PP_HEADER");
205+
}
206+
207+
const int max_n = 100;
208+
std::string name[max_n];
209+
std::string val[max_n];
210+
int nparameter=0;
211+
193212
this->getnameval(ifs, nparameter, name, val);
194213

195214
for (int ip = 0; ip < nparameter; ++ip)
@@ -347,12 +366,17 @@ void Pseudopot_upf::read_pseudo_upf201_header(std::ifstream& ifs, Atom_pseudo& p
347366

348367
void Pseudopot_upf::read_pseudo_upf201_mesh(std::ifstream& ifs, Atom_pseudo& pp)
349368
{
350-
std::string name[50];
351-
std::string val[50];
352-
int nparameter;
369+
const int max_n = 100;
370+
std::string name[max_n];
371+
std::string val[max_n];
372+
int nparameter=0;
373+
353374
if (ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_MESH", true, false))
354375
{
355376
this->getnameval(ifs, nparameter, name, val);
377+
378+
std::cout << "read mesh, nparameter=" << nparameter << std::endl;
379+
356380
for (int ip = 0; ip < nparameter; ++ip)
357381
{
358382
if (name[ip] == "dx")
@@ -362,6 +386,9 @@ void Pseudopot_upf::read_pseudo_upf201_mesh(std::ifstream& ifs, Atom_pseudo& pp)
362386
else if (name[ip] == "mesh")
363387
{
364388
pp.mesh = atoi(val[ip].c_str());
389+
390+
std::cout << "mesh=" << pp.mesh << std::endl;
391+
365392
this->mesh_changed = false;
366393
if (pp.mesh % 2 == 0)
367394
{
@@ -437,9 +464,12 @@ void Pseudopot_upf::read_pseudo_upf201_nonlocal(std::ifstream& ifs, Atom_pseudo&
437464
return;
438465
}
439466
std::string word;
440-
std::string name[50];
441-
std::string val[50];
442-
int nparameter;
467+
468+
const int max_n = 100;
469+
std::string name[max_n];
470+
std::string val[max_n];
471+
int nparameter=0;
472+
443473
this->kbeta = std::vector<int>(pp.nbeta);
444474
pp.lll = std::vector<int>(pp.nbeta);
445475
this->els_beta = std::vector<std::string>(pp.nbeta);
@@ -680,9 +710,12 @@ void Pseudopot_upf::read_pseudo_upf201_nonlocal(std::ifstream& ifs, Atom_pseudo&
680710
void Pseudopot_upf::read_pseudo_upf201_pswfc(std::ifstream& ifs, Atom_pseudo& pp)
681711
{
682712
std::string word;
683-
std::string name[50];
684-
std::string val[50];
685-
int nparameter;
713+
714+
const int max_n = 100;
715+
std::string name[max_n];
716+
std::string val[max_n];
717+
int nparameter=0;
718+
686719
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_PSWFC>");
687720
pp.els = std::vector<std::string>(pp.nchi, "");
688721
pp.lchi = std::vector<int>(pp.nchi, 0);
@@ -815,9 +848,12 @@ void Pseudopot_upf::read_pseudo_upf201_fullwfc(std::ifstream& ifs)
815848
void Pseudopot_upf::read_pseudo_upf201_so(std::ifstream& ifs, Atom_pseudo& pp)
816849
{
817850
std::string word;
818-
std::string name[50];
819-
std::string val[50];
820-
int nparameter;
851+
852+
const int max_n = 100;
853+
std::string name[max_n];
854+
std::string val[max_n];
855+
int nparameter=0;
856+
821857
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_SPIN_ORB>");
822858
pp.jchi = std::vector<double>(pp.nchi, 0.0);
823859
pp.jjj = std::vector<double>(pp.nbeta, 0.0);
@@ -893,4 +929,4 @@ void Pseudopot_upf::read_pseudo_upf201_so(std::ifstream& ifs, Atom_pseudo& pp)
893929
ModuleBase::GlobalFunc::SCAN_END(ifs, word);
894930
}
895931
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_SPIN_ORB>");
896-
}
932+
}

source/module_cell/test/support/mock_unitcell.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@ UnitCell::~UnitCell() {
1717
delete[] atoms;
1818
}
1919
}
20+
2021
void UnitCell::print_cell(std::ofstream& ofs) const {}
22+
2123
void UnitCell::set_iat2itia() {}
24+
2225
void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) {}
26+
2327
bool UnitCell::if_atoms_can_move() const { return true; }
28+
2429
bool UnitCell::if_cell_can_change() const { return true; }
30+
2531
void UnitCell::setup(const std::string& latname_in,
2632
const int& ntype_in,
2733
const int& lmaxmax_in,
2834
const bool& init_vel_in,
2935
const std::string& fixed_axes_in) {}
36+
3037
void cal_nelec(const Atom* atoms, const int& ntype, double& nelec) {}
31-
void UnitCell::compare_atom_labels(std::string label1, std::string label2) {}
38+
39+
void UnitCell::compare_atom_labels(const std::string &label1, const std::string &label2) {}

0 commit comments

Comments
 (0)