ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
read_input_tool.h
Go to the documentation of this file.
1#include <string>
2#include <vector>
3#include <stdexcept>
4#ifdef __MPI
6#endif
7
8#define strvalue item.str_values[0]
9#define intvalue std::stoi(item.str_values[0])
10#define doublevalue std::stod(item.str_values[0])
11#define boolvalue assume_as_boolean(item.str_values[0])
12
13#ifdef __MPI
14#define add_double_bcast(PARAMETER) \
15 { \
16 bcastfuncs.push_back([](Parameter& para) { Parallel_Common::bcast_double(para.PARAMETER); }); \
17 }
18#define add_int_bcast(PARAMETER) \
19 { \
20 bcastfuncs.push_back([](Parameter& para) { Parallel_Common::bcast_int(para.PARAMETER); }); \
21 }
22#define add_bool_bcast(PARAMETER) \
23 { \
24 bcastfuncs.push_back([](Parameter& para) { Parallel_Common::bcast_bool(para.PARAMETER); }); \
25 }
26#define add_string_bcast(PARAMETER) \
27 { \
28 bcastfuncs.push_back([](Parameter& para) { Parallel_Common::bcast_string(para.PARAMETER); }); \
29 }
30#define add_doublevec_bcast(PARAMETER, N, FILL) \
31 { \
32 bcastfuncs.push_back([](Parameter& para) { \
33 int _vec_size = N; \
34 Parallel_Common::bcast_int(_vec_size); \
35 if (para.PARAMETER.size() != _vec_size) \
36 para.PARAMETER.resize(_vec_size, FILL); \
37 Parallel_Common::bcast_double(para.PARAMETER.data(), _vec_size); \
38 }); \
39 }
40#define add_intvec_bcast(PARAMETER, N, FILL) \
41 { \
42 bcastfuncs.push_back([](Parameter& para) { \
43 int _vec_size = N; \
44 Parallel_Common::bcast_int(_vec_size); \
45 if (para.PARAMETER.size() != _vec_size) \
46 para.PARAMETER.resize(_vec_size, FILL); \
47 Parallel_Common::bcast_int(para.PARAMETER.data(), _vec_size); \
48 }); \
49 }
50#define add_stringvec_bcast(PARAMETER, N, FILL) \
51 { \
52 bcastfuncs.push_back([](Parameter& para) { \
53 int _vec_size = N; \
54 Parallel_Common::bcast_int(_vec_size); \
55 if (para.PARAMETER.size() != _vec_size) \
56 para.PARAMETER.resize(_vec_size, FILL); \
57 Parallel_Common::bcast_string(para.PARAMETER.data(), _vec_size); \
58 }); \
59 }
60
61#else
62#define add_double_bcast(PARAMETER)
63#define add_int_bcast(PARAMETER)
64#define add_bool_bcast(PARAMETER)
65#define add_string_bcast(PARAMETER)
66#define add_doublevec_bcast(PARAMETER, N, FILL) \
67 { \
68 bcastfuncs.push_back([](Parameter& para) { \
69 if (para.PARAMETER.size() != N) \
70 para.PARAMETER.resize(N, FILL); \
71 }); \
72 }
73
74#define add_intvec_bcast(PARAMETER, N, FILL) add_doublevec_bcast(PARAMETER, N, FILL)
75#define add_stringvec_bcast(PARAMETER, N, FILL) add_doublevec_bcast(PARAMETER, N, FILL)
76
77#endif
78
79#define sync_string(PARAMETER) \
80 { \
81 item.get_final_value = [](Input_Item& item, const Parameter& para) { item.final_value << para.PARAMETER; }; \
82 add_string_bcast(PARAMETER); \
83 }
84#define sync_int(PARAMETER) \
85 { \
86 item.get_final_value = [](Input_Item& item, const Parameter& para) { item.final_value << para.PARAMETER; }; \
87 add_int_bcast(PARAMETER); \
88 }
89#define sync_double(PARAMETER) \
90 { \
91 item.get_final_value = [](Input_Item& item, const Parameter& para) { item.final_value << para.PARAMETER; }; \
92 add_double_bcast(PARAMETER); \
93 }
94#define sync_bool(PARAMETER) \
95 { \
96 item.get_final_value = [](Input_Item& item, const Parameter& para) { item.final_value << para.PARAMETER; }; \
97 add_bool_bcast(PARAMETER); \
98 }
99#define sync_doublevec(PARAMETER, N, FILL) \
100 { \
101 item.get_final_value = [](Input_Item& item, const Parameter& para) { \
102 for (int i = 0; i < N; i++) \
103 { \
104 item.final_value << para.PARAMETER[i] << " "; \
105 } \
106 }; \
107 add_doublevec_bcast(PARAMETER, N, FILL); \
108 }
109#define sync_intvec(PARAMETER, N, FILL) \
110 { \
111 item.get_final_value = [](Input_Item& item, const Parameter& para) { \
112 for (int i = 0; i < N; i++) \
113 { \
114 item.final_value << para.PARAMETER[i] << " "; \
115 } \
116 }; \
117 add_intvec_bcast(PARAMETER, N, FILL); \
118 }
119#define sync_stringvec(PARAMETER, N, FILL) \
120 { \
121 item.get_final_value = [](Input_Item& item, const Parameter& para) { \
122 for (int i = 0; i < N; i++) \
123 { \
124 item.final_value << para.PARAMETER[i] << " "; \
125 } \
126 }; \
127 add_stringvec_bcast(PARAMETER, N, FILL); \
128 }
129
130#define read_sync_string(PARAMETER) \
131 { \
132 item.read_value = [](const Input_Item& item, Parameter& para) { para.PARAMETER = strvalue; }; \
133 sync_string(PARAMETER); \
134 }
135#define read_sync_int(PARAMETER) \
136 { \
137 item.read_value = [](const Input_Item& item, Parameter& para) { para.PARAMETER = intvalue; }; \
138 sync_int(PARAMETER); \
139 }
140#define read_sync_double(PARAMETER) \
141 { \
142 item.read_value = [](const Input_Item& item, Parameter& para) { para.PARAMETER = doublevalue; }; \
143 sync_double(PARAMETER); \
144 }
145#define read_sync_bool(PARAMETER) \
146 { \
147 item.read_value = [](const Input_Item& item, Parameter& para) { para.PARAMETER = boolvalue; }; \
148 sync_bool(PARAMETER); \
149 }
150
160template <typename T>
161void parse_expression(const std::vector<std::string>& expressions, std::vector<T>& result)
162{
163 result.clear(); // Clear the output vector to prepare for new entries
164 if (expressions.empty())
165 {
166 return;
167 }
168 else if (expressions[0].empty())
169 {
170 return;
171 }
172
173 for (const auto& expr: expressions)
174 {
175 size_t first_star_pos = expr.find('*');
176 size_t second_star_pos = expr.rfind('*'); // rfind finds the rightmost '*'
177
178 // e.g. "3", "3.5"
179 // If no '*' found, convert the whole expression to double/int and add to result
180 if (first_star_pos == std::string::npos)
181 {
182 T T_value = static_cast<T>(std::stof(expr));
183 result.push_back(T_value);
184 }
185 // e.g. "2*3", "2*3.5"
186 // If only one '*' found, split the expression into two parts and convert them to double/int
187 else if (first_star_pos == second_star_pos)
188 {
189 std::string int_part = expr.substr(0, first_star_pos);
190 std::string T_part = expr.substr(first_star_pos + 1);
191
192 int num = std::stoi(int_part);
193 T T_value = static_cast<T>(std::stof(T_part));
194 for(int i = 0 ; i < num; ++i)
195 {
196 result.push_back(T_value);
197 }
198 }
199 // e.g. "2*3*3"
200 // If more than one '*' found, output an error message
201 else
202 {
203 throw std::runtime_error("Invalid expression: " + expr + " - More than one '*' found.");
204 }
205 }
206}
207
208template <typename T>
209void reset_vector(std::vector<T>& vec, int size, T default_value)
210{
211 if (vec.size() != size)
212 {
213 vec.resize(size, default_value);
214 }
215}
#define T
Definition exp.cpp:237
void parse_expression(const std::vector< std::string > &expressions, std::vector< T > &result)
To parse input parameters as expressions into vectors.
Definition read_input_tool.h:161
void reset_vector(std::vector< T > &vec, int size, T default_value)
Definition read_input_tool.h:209