Skip to content

Commit 66cb34b

Browse files
committed
1. add class Input_Process
1 parent 8661328 commit 66cb34b

File tree

6 files changed

+140
-108
lines changed

6 files changed

+140
-108
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ read_txt_tools.o\
249249
read_txt_stru.o\
250250
read_txt_input.o\
251251
read_txt_input_list.o\
252+
read_txt_input_process.o\
252253
read_txt_input-general.o\
253254
read_txt_input-pw.o\
254255
write_pot.o\

source/src_io/read_txt_input.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace Read_Txt_Input
4242
std::string label;
4343
int value_read_size = -1;
4444
friend class Input_List;
45+
friend class Input_Process;
4546

4647
#ifdef USE_CEREAL_SERIALIZATION
4748
public:
Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
#include "read_txt_input_list.h"
22

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>
103

114
namespace Read_Txt_Input
125
{
@@ -32,102 +25,10 @@ namespace Read_Txt_Input
3225
throw std::out_of_range(std::to_string(input_item.value_read_size)+std::to_string(size_low)+std::to_string(size_up));
3326
}
3427

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-
4928
void Input_List::set_items()
5029
{
5130
set_items_general();
5231
set_items_pw();
5332
}
5433

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-
}
13334
}

source/src_io/read_txt_input_list.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "read_txt_input.h"
55

6+
#include <vector>
67
#include <map>
78
#include <string>
89

@@ -11,25 +12,20 @@ namespace Read_Txt_Input
1112
class Input_List
1213
{
1314
public:
14-
void read_and_convert(const std::string &file_name);
15+
void set_items();
1516

1617
private:
17-
std::map<std::string, Input_Item> list;
1818
void add_item(const Input_Item &input_item);
1919
static void check_value_size(const Input_Item& input_item, const int size);
2020
static void check_value_size(const Input_Item& input_item, const int size_low, const int size_up);
2121

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();
22+
std::map<std::string, Input_Item> list;
2923
std::vector<std::string> add_order;
3024

3125
void set_items_general();
3226
void set_items_pw();
27+
28+
friend class Input_Process;
3329
};
3430
}
3531

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include "read_txt_input_process.h"
2+
3+
#include "src_io/read_txt_tools.h"
4+
#include "module_base/global_variable.h"
5+
6+
#include <mpi.h>
7+
#include <sstream>
8+
9+
#ifdef USE_CEREAL_SERIALIZATION
10+
#include "src_lcao/serialization_cereal.h"
11+
#include <cereal/archives/binary.hpp>
12+
#endif
13+
14+
namespace Read_Txt_Input
15+
{
16+
void Input_Process::read_and_convert(const std::string &file_name)
17+
{
18+
if(GlobalV::MY_RANK==0)
19+
{
20+
this->read(file_name);
21+
this->check_transform();
22+
this->default2();
23+
this->out(GlobalV::global_out_dir + file_name);
24+
}
25+
this->bcast();
26+
this->convert();
27+
}
28+
29+
void Input_Process::read(const std::string &file_name)
30+
{
31+
const std::map<std::string, std::vector<std::string>> inputs_read
32+
= Read_Txt_Tools::read_file_to_map(file_name, {"#","\\"});
33+
for(const auto & input_read : inputs_read)
34+
{
35+
const auto ptr = input.list.find(input_read.first);
36+
if(ptr==input.list.end())
37+
throw std::out_of_range("input_read.first");
38+
if(input_read.second.size() > ptr->second.value.size())
39+
throw std::out_of_range("size error");
40+
for(size_t i=0; i<input_read.second.size(); ++i)
41+
ptr->second.value[i].s = input_read.second[i];
42+
ptr->second.value_read_size = input_read.second.size();
43+
}
44+
}
45+
46+
void Input_Process::check_transform()
47+
{
48+
for(auto &item : input.list)
49+
item.second.check_transform(item.second);
50+
}
51+
52+
void Input_Process::default2()
53+
{
54+
for(auto &item : input.list)
55+
item.second.default2(input.list);
56+
}
57+
58+
void Input_Process::out(const std::string &file_name) const
59+
{
60+
std::ofstream ofs(file_name);
61+
for(const std::string &label : input.add_order)
62+
{
63+
ofs<<label<<"\t";
64+
for(const Input_Value &value : input.list.at(label).value)
65+
ofs<<value.s<<" ";
66+
ofs<<"\t# "<<input.list.at(label).annotation<<std::endl;
67+
}
68+
}
69+
70+
void Input_Process::bcast()
71+
{
72+
#ifdef USE_CEREAL_SERIALIZATION
73+
if(GlobalV::MY_RANK==0)
74+
{
75+
std::stringstream ss;
76+
{
77+
cereal::BinaryOutputArchive ar(ss);
78+
ar(input.list);
79+
}
80+
const int size = ss.str().size();
81+
MPI_Bcast( const_cast<int*>(&size), 1, MPI_INT, 0, MPI_COMM_WORLD );
82+
MPI_Bcast( const_cast<char*>(ss.str().c_str()), size, MPI_CHAR, 0, MPI_COMM_WORLD );
83+
}
84+
else
85+
{
86+
int size;
87+
MPI_Bcast( &size, 1, MPI_INT, 0, MPI_COMM_WORLD );
88+
std::vector<char> c(size);
89+
MPI_Bcast( c.data(), size, MPI_CHAR, 0, MPI_COMM_WORLD );
90+
std::stringstream ss;
91+
ss.rdbuf()->pubsetbuf(c.data(),size);
92+
{
93+
cereal::BinaryInputArchive ar(ss);
94+
ar(input.list);
95+
}
96+
}
97+
#else
98+
#error Input_Process::bcast() needs cereal
99+
#endif
100+
}
101+
102+
void Input_Process::convert()
103+
{
104+
for(auto &item : input.list)
105+
item.second.convert(item.second);
106+
}
107+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef READ_TXT_INIPUT_PROCESS_H
2+
#define READ_TXT_INIPUT_PROCESS_H
3+
4+
#include "read_txt_input.h"
5+
#include "read_txt_input_list.h"
6+
7+
namespace Read_Txt_Input
8+
{
9+
class Input_Process
10+
{
11+
public:
12+
Input_Process(Input_List &input_in) :input(input_in) {}
13+
void read_and_convert(const std::string &file_name);
14+
15+
private:
16+
Input_List &input;
17+
void read(const std::string &file_name);
18+
void check_transform();
19+
void default2();
20+
void out(const std::string &file_name) const;
21+
void bcast();
22+
void convert();
23+
};
24+
}
25+
26+
#endif

0 commit comments

Comments
 (0)