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 <algorithm>
2#include <sstream>
3#include <stdexcept>
4#include <string>
5#include <vector>
6
7#ifdef __MPI
9#endif
10
11#define strvalue item.str_values[0]
12#define intvalue std::stoi(item.str_values[0])
13#define doublevalue std::stod(item.str_values[0])
14#define boolvalue assume_as_boolean(item.str_values[0])
15
16#ifdef __MPI
17#define add_double_bcast(PARAMETER) \
18 { \
19 bcastfuncs.push_back([](Parameter& para) { Parallel_Common::bcast_double(para.PARAMETER); }); \
20 }
21#define add_int_bcast(PARAMETER) \
22 { \
23 bcastfuncs.push_back([](Parameter& para) { Parallel_Common::bcast_int(para.PARAMETER); }); \
24 }
25#define add_bool_bcast(PARAMETER) \
26 { \
27 bcastfuncs.push_back([](Parameter& para) { Parallel_Common::bcast_bool(para.PARAMETER); }); \
28 }
29#define add_string_bcast(PARAMETER) \
30 { \
31 bcastfuncs.push_back([](Parameter& para) { Parallel_Common::bcast_string(para.PARAMETER); }); \
32 }
33#define add_doublevec_bcast(PARAMETER, N, FILL) \
34 { \
35 bcastfuncs.push_back([](Parameter& para) { \
36 int _vec_size = N; \
37 Parallel_Common::bcast_int(_vec_size); \
38 if (para.PARAMETER.size() != _vec_size) \
39 para.PARAMETER.resize(_vec_size, FILL); \
40 Parallel_Common::bcast_double(para.PARAMETER.data(), _vec_size); \
41 }); \
42 }
43#define add_intvec_bcast(PARAMETER, N, FILL) \
44 { \
45 bcastfuncs.push_back([](Parameter& para) { \
46 int _vec_size = N; \
47 Parallel_Common::bcast_int(_vec_size); \
48 if (para.PARAMETER.size() != _vec_size) \
49 para.PARAMETER.resize(_vec_size, FILL); \
50 Parallel_Common::bcast_int(para.PARAMETER.data(), _vec_size); \
51 }); \
52 }
53#define add_stringvec_bcast(PARAMETER, N, FILL) \
54 { \
55 bcastfuncs.push_back([](Parameter& para) { \
56 int _vec_size = N; \
57 Parallel_Common::bcast_int(_vec_size); \
58 if (para.PARAMETER.size() != _vec_size) \
59 para.PARAMETER.resize(_vec_size, FILL); \
60 Parallel_Common::bcast_string(para.PARAMETER.data(), _vec_size); \
61 }); \
62 }
63
64#else
65#define add_double_bcast(PARAMETER)
66#define add_int_bcast(PARAMETER)
67#define add_bool_bcast(PARAMETER)
68#define add_string_bcast(PARAMETER)
69#define add_doublevec_bcast(PARAMETER, N, FILL) \
70 { \
71 bcastfuncs.push_back([](Parameter& para) { \
72 if (para.PARAMETER.size() != N) \
73 para.PARAMETER.resize(N, FILL); \
74 }); \
75 }
76
77#define add_intvec_bcast(PARAMETER, N, FILL) add_doublevec_bcast(PARAMETER, N, FILL)
78#define add_stringvec_bcast(PARAMETER, N, FILL) add_doublevec_bcast(PARAMETER, N, FILL)
79
80#endif
81
82#define sync_string(PARAMETER) \
83 { \
84 item.domain.bind(AvailabilityValueKind::String, [](const Parameter& para) { return InputValue(para.PARAMETER); }); \
85 item.get_final_value = [](Input_Item& item, const Parameter& para) { item.final_value << para.PARAMETER; }; \
86 add_string_bcast(PARAMETER); \
87 }
88#define sync_int(PARAMETER) \
89 { \
90 item.domain.bind(AvailabilityValueKind::Integer, [](const Parameter& para) { return InputValue(para.PARAMETER); }); \
91 item.get_final_value = [](Input_Item& item, const Parameter& para) { item.final_value << para.PARAMETER; }; \
92 add_int_bcast(PARAMETER); \
93 }
94#define sync_double(PARAMETER) \
95 { \
96 item.domain.bind(AvailabilityValueKind::Real, [](const Parameter& para) { return InputValue(para.PARAMETER); }); \
97 item.get_final_value = [](Input_Item& item, const Parameter& para) { item.final_value << para.PARAMETER; }; \
98 add_double_bcast(PARAMETER); \
99 }
100#define sync_bool(PARAMETER) \
101 { \
102 item.domain.bind(AvailabilityValueKind::Boolean, [](const Parameter& para) { return InputValue(para.PARAMETER); }); \
103 item.get_final_value = [](Input_Item& item, const Parameter& para) { item.final_value << para.PARAMETER; }; \
104 add_bool_bcast(PARAMETER); \
105 }
106#define sync_doublevec(PARAMETER, N, FILL) \
107 { \
108 item.domain.bind(AvailabilityValueKind::RealVector, [](const Parameter& para) { return InputValue(para.PARAMETER); }); \
109 item.get_final_value = [](Input_Item& item, const Parameter& para) { \
110 for (int i = 0; i < N; i++) \
111 { \
112 item.final_value << para.PARAMETER[i] << " "; \
113 } \
114 }; \
115 add_doublevec_bcast(PARAMETER, N, FILL); \
116 }
117#define sync_intvec(PARAMETER, N, FILL) \
118 { \
119 item.domain.bind(AvailabilityValueKind::IntegerVector, [](const Parameter& para) { return InputValue(para.PARAMETER); }); \
120 item.get_final_value = [](Input_Item& item, const Parameter& para) { \
121 for (int i = 0; i < N; i++) \
122 { \
123 item.final_value << para.PARAMETER[i] << " "; \
124 } \
125 }; \
126 add_intvec_bcast(PARAMETER, N, FILL); \
127 }
128#define sync_stringvec(PARAMETER, N, FILL) \
129 { \
130 item.domain.bind(AvailabilityValueKind::StringVector, [](const Parameter& para) { return InputValue(para.PARAMETER); }); \
131 item.get_final_value = [](Input_Item& item, const Parameter& para) { \
132 for (int i = 0; i < N; i++) \
133 { \
134 item.final_value << para.PARAMETER[i] << " "; \
135 } \
136 }; \
137 add_stringvec_bcast(PARAMETER, N, FILL); \
138 }
139
140#define read_sync_string(PARAMETER) \
141 { \
142 item.domain.scalar(); \
143 item.read_value = [](const Input_Item& item, Parameter& para) { para.PARAMETER = strvalue; }; \
144 sync_string(PARAMETER); \
145 }
146#define read_sync_int(PARAMETER) \
147 { \
148 item.domain.scalar(); \
149 item.read_value = [](const Input_Item& item, Parameter& para) { para.PARAMETER = intvalue; }; \
150 sync_int(PARAMETER); \
151 }
152#define read_sync_double(PARAMETER) \
153 { \
154 item.domain.scalar(); \
155 item.read_value = [](const Input_Item& item, Parameter& para) { para.PARAMETER = doublevalue; }; \
156 sync_double(PARAMETER); \
157 }
158#define read_sync_bool(PARAMETER) \
159 { \
160 item.domain.scalar(); \
161 item.read_value = [](const Input_Item& item, Parameter& para) { para.PARAMETER = boolvalue; }; \
162 sync_bool(PARAMETER); \
163 }
164
174template <typename T>
175void parse_expression(const std::vector<std::string>& expressions, std::vector<T>& result)
176{
177 result.clear(); // Clear the output vector to prepare for new entries
178 if (expressions.empty())
179 {
180 return;
181 }
182 else if (expressions[0].empty())
183 {
184 return;
185 }
186
187 for (const std::string& expr: expressions)
188 {
189 size_t first_star_pos = expr.find('*');
190 size_t second_star_pos = expr.rfind('*'); // rfind finds the rightmost '*'
191
192 // e.g. "3", "3.5"
193 // If no '*' found, convert the whole expression to double/int and add to result
194 if (first_star_pos == std::string::npos)
195 {
196 std::istringstream value_stream(expr);
197 T T_value{};
198 if (!(value_stream >> T_value) || !value_stream.eof())
199 {
200 throw std::runtime_error("Invalid expression value: " + expr);
201 }
202 result.push_back(T_value);
203 }
204 // e.g. "2*3", "2*3.5"
205 // If only one '*' found, split the expression into two parts and convert them to double/int
206 else if (first_star_pos == second_star_pos)
207 {
208 std::string int_part = expr.substr(0, first_star_pos);
209 std::string T_part = expr.substr(first_star_pos + 1);
210
211 std::istringstream count_stream(int_part);
212 int num = 0;
213 if (!(count_stream >> num) || !count_stream.eof() || num < 0)
214 {
215 throw std::runtime_error("Invalid expression count: " + int_part);
216 }
217 std::istringstream value_stream(T_part);
218 T T_value{};
219 if (!(value_stream >> T_value) || !value_stream.eof())
220 {
221 throw std::runtime_error("Invalid expression value: " + T_part);
222 }
223 for (int i = 0; i < num; ++i)
224 {
225 result.push_back(T_value);
226 }
227 }
228 // e.g. "2*3*3"
229 // If more than one '*' found, output an error message
230 else
231 {
232 throw std::runtime_error("Invalid expression: " + expr + " - More than one '*' found.");
233 }
234 }
235}
236
237template <typename T>
238void reset_vector(std::vector<T>& vec, int size, T default_value)
239{
240 if (vec.size() != size)
241 {
242 vec.resize(size, default_value);
243 }
244}
245
246namespace ModuleIO
247{
248
258inline bool dsp_layout_check_needed(const std::string& esolver_type)
259{
260 std::string lower = esolver_type;
261 std::transform(lower.begin(), lower.end(), lower.begin(),
262 [](unsigned char c) { return std::tolower(c); });
263 return !(lower == "dp" || lower == "nep");
264}
265
273inline int dsp_effective_kpar(const std::string& basis_type,
274 const int kpar,
275 const int kpar_lcao)
276{
277 return (basis_type == "lcao") ? kpar_lcao : kpar;
278}
279
290inline bool dsp_kpar_layout_valid(const int nproc,
291 const std::string& basis_type,
292 const int kpar,
293 const int kpar_lcao,
294 const int bndpar,
295 const std::string& esolver_type)
296{
297 if (!dsp_layout_check_needed(esolver_type))
298 {
299 return true;
300 }
301 if (nproc <= 0)
302 {
303 return false;
304 }
305 const int kpar_eff = dsp_effective_kpar(basis_type, kpar, kpar_lcao);
306 if (kpar_eff <= 0 || bndpar <= 0)
307 {
308 return false;
309 }
310 return nproc == kpar_eff * bndpar;
311}
312
313} // namespace ModuleIO
#define T
Definition exp.cpp:237
Internal helpers shared by the R-space sparse text writers. Declared here (instead of duplicated in e...
Definition csr_reader.cpp:8
bool dsp_kpar_layout_valid(const int nproc, const std::string &basis_type, const int kpar, const int kpar_lcao, const int bndpar, const std::string &esolver_type)
Check whether a k-point/band parallel layout satisfies the DSP vendor library constraint.
Definition read_input_tool.h:290
bool dsp_layout_check_needed(const std::string &esolver_type)
Whether the DSP kpar/bndpar layout constraint applies.
Definition read_input_tool.h:258
int dsp_effective_kpar(const std::string &basis_type, const int kpar, const int kpar_lcao)
The k-point pool count actually consumed by the current basis.
Definition read_input_tool.h:273
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:175
void reset_vector(std::vector< T > &vec, int size, T default_value)
Definition read_input_tool.h:238