Skip to content

Commit 8661328

Browse files
committed
1. add namespace Read_Txt_Input
1 parent 7506aba commit 8661328

File tree

9 files changed

+358
-0
lines changed

9 files changed

+358
-0
lines changed

source/Makefile.Objects

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ read_cell_pseudopots.o\
247247
read_dm.o\
248248
read_txt_tools.o\
249249
read_txt_stru.o\
250+
read_txt_input.o\
251+
read_txt_input_list.o\
252+
read_txt_input-general.o\
253+
read_txt_input-pw.o\
250254
write_pot.o\
251255
write_rho.o\
252256
write_rho_cube.o\
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "read_txt_input_list.h"
2+
3+
#include "src_io/read_txt_tools.h"
4+
#include <stdexcept>
5+
6+
// for convert
7+
#include "module_base/global_variable.h"
8+
#include "src_pw/global.h"
9+
10+
namespace Read_Txt_Input
11+
{
12+
void Input_List::set_items_general()
13+
{
14+
{
15+
Input_Item item("nspin");
16+
item.default1({"1"});
17+
item.annotation = "1: single spin; 2: up and down spin; 4: noncollinear spin";
18+
item.check_transform = [](Input_Item &self)
19+
{
20+
Input_List::check_value_size(self, 1);
21+
if(!Read_Txt_Tools::in_set(self.value[0].s, {"1","2","4"}))
22+
throw std::invalid_argument("nspin must be 1,2,4");
23+
self.value[0].i = std::stoi(self.value[0].s);
24+
};
25+
item.convert = [](const Input_Item &self)
26+
{
27+
GlobalV::NSPIN = self.value[0].i;
28+
};
29+
this->add_item(item);
30+
}
31+
32+
{
33+
Input_Item item("ntype");
34+
item.default1({"0"});
35+
item.annotation = "atom species number";
36+
item.check_transform = [](Input_Item &self)
37+
{
38+
Input_List::check_value_size(self, 1);
39+
self.value[0].i = std::stoi(self.value[0].s);
40+
if(self.value[0].i<=0)
41+
throw std::invalid_argument("ntype must > 0");
42+
};
43+
item.convert = [](const Input_Item &self)
44+
{
45+
GlobalC::ucell.ntype = self.value[0].i;
46+
};
47+
this->add_item(item);
48+
}
49+
}
50+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "read_txt_input_list.h"
2+
3+
#include "module_base/constants.h"
4+
#include <stdexcept>
5+
6+
// for convert
7+
#include "module_base/global_variable.h"
8+
9+
namespace Read_Txt_Input
10+
{
11+
void Input_List::set_items_pw()
12+
{
13+
{
14+
Input_Item item("ecutwfc");
15+
item.default1({"100","eV"});
16+
item.annotation = "energy cutoff for wave functions";
17+
item.check_transform = [](Input_Item &self)
18+
{
19+
Input_List::check_value_size(self, 1, 2);
20+
if(self.value[1].s=="eV")
21+
self.value[0].d = std::stod(self.value[0].s);
22+
else if(self.value[1].s=="Ry")
23+
{
24+
self.value[0].d = std::stod(self.value[0].s) / ModuleBase::Ry_to_eV;
25+
self.value[0].s = std::to_string(self.value[0].d);
26+
}
27+
else
28+
throw std::invalid_argument(self.value[1].s);
29+
self.value[1].s = "eV";
30+
if(self.value[0].d<=0)
31+
throw std::invalid_argument("ecutwfc must > 0");
32+
};
33+
item.convert = [](const Input_Item &self)
34+
{
35+
// ?? GlobalC::pw.set()
36+
};
37+
this->add_item(item);
38+
}
39+
}
40+
}

source/src_io/read_txt_input.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "read_txt_input.h"
2+
3+
namespace Read_Txt_Input
4+
{
5+
void Input_Item::default1(const std::vector<std::string> &value_in)
6+
{
7+
this->value.resize(value_in.size());
8+
for(size_t i=0; i<value_in.size(); ++i)
9+
this->value[i].s = value_in[i];
10+
}
11+
}

source/src_io/read_txt_input.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef READ_TXT_INPUT_H
2+
#define READ_TXT_INPUT_H
3+
4+
#include <vector>
5+
#include <map>
6+
#include <string>
7+
#include <functional>
8+
9+
#ifdef USE_CEREAL_SERIALIZATION
10+
#include <cereal/access.hpp>
11+
#endif
12+
13+
namespace Read_Txt_Input
14+
{
15+
struct Input_Value
16+
{
17+
bool b;
18+
int i;
19+
double d;
20+
std::string s;
21+
};
22+
23+
class Input_Item
24+
{
25+
public:
26+
// call these functions
27+
Input_Item(const std::string &label_in) :label(label_in) {}
28+
void default1(const std::vector<std::string> &value_in);
29+
30+
// set these variables and functions
31+
std::string annotation;
32+
std::function<void(Input_Item&)> check_transform
33+
= [](Input_Item&self){};
34+
std::function<void(const std::map<std::string, Input_Item>&)> default2
35+
= [](const std::map<std::string, Input_Item>&list){};
36+
std::function<void(const Input_Item&)> convert
37+
= [](const Input_Item&item){};
38+
39+
std::vector<Input_Value> value;
40+
41+
private:
42+
std::string label;
43+
int value_read_size = -1;
44+
friend class Input_List;
45+
46+
#ifdef USE_CEREAL_SERIALIZATION
47+
public:
48+
Input_Item() = default;
49+
private:
50+
friend class cereal::access;
51+
template <class Archive> void serialize( Archive & ar );
52+
#endif
53+
};
54+
}
55+
56+
#endif
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include "read_txt_input_list.h"
2+
3+
#include "src_io/read_txt_tools.h"
4+
#include "module_base/global_variable.h"
5+
6+
#include "src_lcao/serialization_cereal.h"
7+
#include <cereal/archives/binary.hpp>
8+
#include <mpi.h>
9+
#include <sstream>
10+
11+
namespace Read_Txt_Input
12+
{
13+
void Input_List::add_item(const Input_Item &input_item)
14+
{
15+
list.insert(make_pair(input_item.label, input_item));
16+
add_order.push_back(input_item.label);
17+
}
18+
19+
void Input_List::check_value_size(const Input_Item& input_item, const int size)
20+
{
21+
if(input_item.value_read_size==-1)
22+
return;
23+
if(input_item.value_read_size!=size)
24+
throw std::out_of_range(std::to_string(input_item.value_read_size)+std::to_string(size));
25+
}
26+
27+
void Input_List::check_value_size(const Input_Item& input_item, const int size_low, const int size_up)
28+
{
29+
if(input_item.value_read_size==-1)
30+
return;
31+
if(input_item.value_read_size<size_low || input_item.value_read_size>size_up)
32+
throw std::out_of_range(std::to_string(input_item.value_read_size)+std::to_string(size_low)+std::to_string(size_up));
33+
}
34+
35+
void Input_List::read_and_convert(const std::string &file_name)
36+
{
37+
this->set_items();
38+
if(GlobalV::MY_RANK==0)
39+
{
40+
this->read(file_name);
41+
this->check_transform();
42+
this->default2();
43+
this->out(GlobalV::global_out_dir + file_name);
44+
}
45+
this->bcast();
46+
this->convert();
47+
}
48+
49+
void Input_List::set_items()
50+
{
51+
set_items_general();
52+
set_items_pw();
53+
}
54+
55+
void Input_List::read(const std::string &file_name)
56+
{
57+
const std::map<std::string, std::vector<std::string>> inputs_read
58+
= Read_Txt_Tools::read_file_to_map(file_name, {"#","\\"});
59+
for(const auto & input_read : inputs_read)
60+
{
61+
const auto ptr = list.find(input_read.first);
62+
if(ptr==list.end())
63+
throw std::out_of_range("input_read.first");
64+
if(input_read.second.size() > ptr->second.value.size())
65+
throw std::out_of_range("size error");
66+
for(size_t i=0; i<input_read.second.size(); ++i)
67+
ptr->second.value[i].s = input_read.second[i];
68+
ptr->second.value_read_size = input_read.second.size();
69+
}
70+
}
71+
72+
void Input_List::check_transform()
73+
{
74+
for(auto &item : this->list)
75+
item.second.check_transform(item.second);
76+
}
77+
78+
void Input_List::default2()
79+
{
80+
for(auto &item : this->list)
81+
item.second.default2(this->list);
82+
}
83+
84+
void Input_List::out(const std::string &file_name) const
85+
{
86+
std::ofstream ofs(file_name);
87+
for(const std::string &label : add_order)
88+
{
89+
ofs<<label<<"\t";
90+
for(const Input_Value &value : this->list.at(label).value)
91+
ofs<<value.s<<" ";
92+
ofs<<"\t# "<<this->list.at(label).annotation<<std::endl;
93+
}
94+
}
95+
96+
void Input_List::bcast()
97+
{
98+
#ifdef USE_CEREAL_SERIALIZATION
99+
if(GlobalV::MY_RANK==0)
100+
{
101+
std::stringstream ss;
102+
{
103+
cereal::BinaryOutputArchive ar(ss);
104+
ar(this->list);
105+
}
106+
const int size = ss.str().size();
107+
MPI_Bcast( const_cast<int*>(&size), 1, MPI_INT, 0, MPI_COMM_WORLD );
108+
MPI_Bcast( const_cast<char*>(ss.str().c_str()), size, MPI_CHAR, 0, MPI_COMM_WORLD );
109+
}
110+
else
111+
{
112+
int size;
113+
MPI_Bcast( &size, 1, MPI_INT, 0, MPI_COMM_WORLD );
114+
std::vector<char> c(size);
115+
MPI_Bcast( c.data(), size, MPI_CHAR, 0, MPI_COMM_WORLD );
116+
std::stringstream ss;
117+
ss.rdbuf()->pubsetbuf(c.data(),size);
118+
{
119+
cereal::BinaryInputArchive ar(ss);
120+
ar(this->list);
121+
}
122+
}
123+
#else
124+
#error Input_List::bcast() needs cereal
125+
#endif
126+
}
127+
128+
void Input_List::convert()
129+
{
130+
for(auto &item : this->list)
131+
item.second.convert(item.second);
132+
}
133+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef READ_TXT_INIPUT_LIST_H
2+
#define READ_TXT_INIPUT_LIST_H
3+
4+
#include "read_txt_input.h"
5+
6+
#include <map>
7+
#include <string>
8+
9+
namespace Read_Txt_Input
10+
{
11+
class Input_List
12+
{
13+
public:
14+
void read_and_convert(const std::string &file_name);
15+
16+
private:
17+
std::map<std::string, Input_Item> list;
18+
void add_item(const Input_Item &input_item);
19+
static void check_value_size(const Input_Item& input_item, const int size);
20+
static void check_value_size(const Input_Item& input_item, const int size_low, const int size_up);
21+
22+
void set_items();
23+
void read(const std::string &file_name);
24+
void check_transform();
25+
void default2();
26+
void out(const std::string &file_name) const;
27+
void bcast();
28+
void convert();
29+
std::vector<std::string> add_order;
30+
31+
void set_items_general();
32+
void set_items_pw();
33+
};
34+
}
35+
36+
#endif

source/src_io/read_txt_tools.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ namespace Read_Txt_Tools
6262
for(const std::vector<T> & data_i : data_v)
6363
data_chain.insert(data_chain.end(), data_i.begin(), data_i.end());
6464
return data_chain;
65+
}
66+
67+
// check whether value in set_check
68+
template<typename T>
69+
bool in_set(const T &value, const std::set<T> &set_check)
70+
{
71+
return set_check.find(value)!=set_check.end();
6572
}
6673
}
6774

source/src_lcao/serialization_cereal.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
#include <cereal/types/vector.hpp>
77
#include <cereal/types/map.hpp>
88
#include <cereal/types/set.hpp>
9+
#include <cereal/types/string.hpp>
10+
#include <cereal/types/functional.hpp>
911

1012
#include "../module_base/vector3.h"
1113
#include "../src_ri/abfs-vector3_order.h"
1214
#include "../module_base/matrix.h"
15+
#include "src_io/read_txt_input.h"
1316

1417

1518

@@ -34,4 +37,22 @@ template<class Archive> void load( Archive & ar, matrix & m )
3437
}
3538
}
3639

40+
namespace Read_Txt_Input
41+
{
42+
template<class Archive> void serialize( Archive & ar, Input_Value & value )
43+
{
44+
ar( value.b );
45+
ar( value.i );
46+
ar( value.d );
47+
ar( value.s );
48+
}
49+
template<class Archive> void Input_Item::serialize( Archive & ar )
50+
{
51+
ar( this->annotation );
52+
ar( this->value );
53+
ar( this->label );
54+
ar( this->value_read_size );
55+
}
56+
}
57+
3758
#endif

0 commit comments

Comments
 (0)