Skip to content

Commit 049de94

Browse files
committed
1. add values size range check in Read_Txt_Input
1 parent 571626c commit 049de94

10 files changed

+85
-27
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ read_txt_input_process.o\
253253
read_txt_input_process_global.o\
254254
read_txt_input-general.o\
255255
read_txt_input-pw.o\
256+
read_txt_input-spectrum.o\
256257
write_pot.o\
257258
write_rho.o\
258259
write_rho_cube.o\

source/src_io/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_library(
3030
read_txt_input_process_global.cpp
3131
read_txt_input-general.cpp
3232
read_txt_input-pw.cpp
33+
read_txt_input-spectrum.cpp
3334
read_txt_stru.cpp
3435
restart.cpp
3536
rwstream.cpp

source/src_io/read_txt_input-pw.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ namespace Read_Txt_Input
1919

2020
{
2121
Input_Item item("ecutwfc");
22-
item.default_1(100.0,"eV");
22+
item.default_1(100.0,"Ry");
23+
item.check_values_size(1,2);
2324
item.annotation = "energy cutoff for wave functions";
2425
item.check_transform = [](Input_Item &self)
2526
{
26-
if(self.values[1].gets()=="eV"){}
27-
else if(self.values[1].gets()=="Ry")
27+
if(self.values[0].getd()<=0)
28+
throw std::invalid_argument("ecutwfc must > 0");
29+
30+
if(self.values[1].gets()=="Ry"){}
31+
else if(self.values[1].gets()=="eV")
2832
self.values[0].setd( self.values[0].getd() / ModuleBase::Ry_to_eV );
2933
else
3034
throw std::invalid_argument(self.values[1].gets());
31-
self.values[1].sets("eV");
32-
if(self.values[0].getd()<=0)
33-
throw std::invalid_argument("ecutwfc must > 0");
35+
self.values[1].sets("Ry");
3436
};
3537
item.convert = [](const Input_Item &self)
3638
{
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//=======================
2+
// AUTHOR : Peize Lin
3+
// DATE : 2021-12-13
4+
//=======================
5+
6+
#include "read_txt_input_list.h"
7+
8+
#include <limits>
9+
10+
namespace Read_Txt_Input
11+
{
12+
void Input_List::set_items_spectrum()
13+
{
14+
this->output_labels.push_back("Parameters (15.Spectrum)");
15+
16+
{
17+
Input_Item item("ocp_set");
18+
item.annotation = "set occupation number";
19+
item.check_values_size(0, std::numeric_limits<int>::max());
20+
item.convert = [](const Input_Item &self)
21+
{
22+
// ...
23+
};
24+
this->add_item(item);
25+
}
26+
}
27+
}

source/src_io/read_txt_input_item-template.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ namespace Read_Txt_Input
1414
void Input_Item::default_1(const T &value)
1515
{
1616
set_value(value);
17+
++this->values_size_lower_limit;
18+
++this->values_size_upper_limit;
1719
}
1820

1921
template <typename T_head, typename... T_tail>
2022
void Input_Item::default_1( const T_head &value_head, const T_tail... values_tail)
2123
{
2224
set_value(value_head);
2325
default_1(values_tail...);
26+
++this->values_size_lower_limit;
27+
++this->values_size_upper_limit;
2428
}
2529

2630
template<>

source/src_io/read_txt_input_item.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ namespace Read_Txt_Input
4040

4141
std::vector<Input_Value> values;
4242

43+
void check_values_size(const int lower_limit, const int upper_limit)
44+
{ values_size_lower_limit=lower_limit; values_size_upper_limit=upper_limit; }
45+
4346
private:
4447
std::string label;
45-
//int value_read_size = -1;
4648
std::vector<std::string> values_type;
49+
50+
int values_size_read = -1;
51+
int values_size_lower_limit = 0;
52+
int values_size_upper_limit = 0;
4753

4854
friend class Input_List;
4955
friend class Input_Process;

source/src_io/read_txt_input_list.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace Read_Txt_Input
3535
{
3636
set_items_general();
3737
set_items_pw();
38+
set_items_spectrum();
3839
}
3940

4041
}

source/src_io/read_txt_input_list.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace Read_Txt_Input
2929

3030
void set_items_general();
3131
void set_items_pw();
32+
void set_items_spectrum();
3233

3334
friend class Input_Process;
3435
};

source/src_io/read_txt_input_process.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,48 @@ namespace Read_Txt_Input
3737
= Read_Txt_Tools::read_file_to_map(file_name, {"#","\\"}, true);
3838
for(const auto & input_read : inputs_read)
3939
{
40-
const auto ptr = this->input.list.find(input_read.first);
41-
if(ptr==this->input.list.end())
40+
const auto item_ptr = this->input.list.find(input_read.first);
41+
if(item_ptr==this->input.list.end())
4242
throw std::out_of_range("input_read.first");
43+
Read_Txt_Input::Input_Item &item = item_ptr->second;
4344

44-
//if(input_read.second.size() > ptr->second.values.size())
45-
if(input_read.second.size() != ptr->second.values.size())
46-
throw std::out_of_range(
47-
"size of INPUT "+ptr->second.label+" needs "+std::to_string(ptr->second.values.size())
48-
+" but reads "+std::to_string(input_read.second.size()));
45+
item.values_size_read = input_read.second.size();
46+
if(item.values.size()<input_read.second.size())
47+
{
48+
item.values.resize(input_read.second.size());
49+
item.values_type.resize(input_read.second.size(),"s");
50+
}
4951

5052
for(size_t i=0; i<input_read.second.size(); ++i)
51-
ptr->second.values[i].sets(input_read.second[i]);
52-
//ptr->second.value_read_size = input_read.second.size();
53+
item.values[i].sets(input_read.second[i]);
5354
}
5455
}
5556

5657
void Input_Process::check_transform()
5758
{
58-
for(auto &item : this->input.list)
59+
for(auto &tmp : this->input.list)
5960
{
60-
item.second.check_transform(item.second);
61+
Read_Txt_Input::Input_Item &item = tmp.second;
62+
63+
if( item.values_size_read>=0 &&
64+
(item.values_size_read<item.values_size_lower_limit ||
65+
item.values_size_read>item.values_size_upper_limit ))
66+
{
67+
throw std::out_of_range(
68+
"size of INPUT "+item.label+" needs in ["+std::to_string(item.values_size_lower_limit)+","+std::to_string(item.values_size_upper_limit)+"]"
69+
+" but reads "+std::to_string(item.values_size_read));
70+
}
6171

62-
for(size_t i=0; i<item.second.values.size(); ++i)
72+
item.check_transform(item);
73+
74+
for(size_t i=0; i<item.values.size(); ++i)
6375
{
64-
if(item.second.values_type[i]=="b")
76+
if(item.values_type[i]=="b")
6577
{
66-
if(!Read_Txt_Tools::in_set( item.second.values[i].gets(), Read_Txt_Tools::Preset::Bool))
67-
throw std::invalid_argument("INPUT "+item.second.label+" must be bool");
78+
if(!Read_Txt_Tools::in_set( item.values[i].gets(), Read_Txt_Tools::Preset::Bool))
79+
throw std::invalid_argument("INPUT "+item.label+" must be bool");
6880
}
69-
}
81+
}
7082
}
7183
this->check_transform_global(this->input.list);
7284
}
@@ -83,15 +95,15 @@ namespace Read_Txt_Input
8395
std::ofstream ofs(file_name);
8496
for(const std::string &label : this->input.output_labels)
8597
{
86-
const auto ptr = this->input.list.find(label);
87-
if(ptr==this->input.list.end())
98+
const auto item_ptr = this->input.list.find(label);
99+
if(item_ptr==this->input.list.end())
88100
{
89101
ofs<<std::endl<<"# "<<label<<std::endl;
90102
}
91103
else
92104
{
93105
ofs<<label<<"\t";
94-
const Read_Txt_Input::Input_Item &item = ptr->second;
106+
const Read_Txt_Input::Input_Item &item = item_ptr->second;
95107
for(size_t i=0; i<item.values.size(); ++i)
96108
{
97109
if(item.values_type[i]=="s")

source/src_lcao/serialization_cereal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ template<class Archive> void Input_Item::serialize( Archive & ar )
5151
{
5252
ar( this->annotation );
5353
ar( this->values );
54+
ar( this->values_type );
5455
ar( this->label );
55-
//ar( this->value_read_size );
56+
ar( this->values_size_read );
57+
ar( this->values_size_lower_limit );
58+
ar( this->values_size_upper_limit );
5659
}
5760
}
5861

0 commit comments

Comments
 (0)