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+ }
0 commit comments