ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
input_conv.h
Go to the documentation of this file.
1//==========================================================
2// Author: Lixin He,mohan
3// DATE : 2008-12-24
4//==========================================================
5#ifndef INPUT_CONVERT_H
6#define INPUT_CONVERT_H
7
10
11#include <fstream>
12#include <iomanip>
13#include <iostream>
14#include <regex.h>
15#include <cstdio>
16#include <cstring>
17#include <string>
18#include <vector>
19#include <algorithm>
20
21namespace Input_Conv
22{
23
29
34void Convert();
35
45template <typename T>
46void parse_expression(const std::string& fn, std::vector<T>& vec)
47{
48 ModuleBase::TITLE("Input_Conv", "parse_expression");
49 int count = 0;
50
51 // Update the regex pattern to handle scientific notation
52 std::string pattern("([-+]?[0-9]+\\*[-+]?[0-9.eE+-]+|[-+]?[0-9,.eE+-]+)");
53
54 std::vector<std::string> str;
55 std::stringstream ss(fn);
56 std::string section;
57
58 // Split the input string into substrings by spaces
59 while (ss >> section)
60 {
61 int index = 0;
62 if (str.empty())
63 {
64 while (index < section.size() && std::isspace(section[index]))
65 {
66 index++;
67 }
68 }
69 section.erase(0, index);
70 str.push_back(section);
71 }
72
73 // Compile the regular expression
74 regex_t reg;
75 regcomp(&reg, pattern.c_str(), REG_EXTENDED);
76 regmatch_t pmatch[1];
77 const size_t nmatch = 1;
78
79 // Loop over each section and apply regex to extract numbers
80 for (size_t i = 0; i < str.size(); ++i)
81 {
82 if (str[i] == "")
83 {
84 continue;
85 }
86 int status = regexec(&reg, str[i].c_str(), nmatch, pmatch, 0);
87 std::string sub_str = "";
88
89 // Extract the matched substring
90 for (size_t j = pmatch[0].rm_so; j != pmatch[0].rm_eo; ++j)
91 {
92 sub_str += str[i][j];
93 }
94
95 // Check if the substring contains multiplication (e.g., "2*3.14")
96 std::string sub_pattern("\\*");
97 regex_t sub_reg;
98 regcomp(&sub_reg, sub_pattern.c_str(), REG_EXTENDED);
99 regmatch_t sub_pmatch[1];
100 const size_t sub_nmatch = 1;
101
102 if (regexec(&sub_reg, sub_str.c_str(), sub_nmatch, sub_pmatch, 0) == 0)
103 {
104 size_t pos = sub_str.find("*");
105 int num = stoi(sub_str.substr(0, pos));
106 assert(num >= 0);
107 T occ = stof(sub_str.substr(pos + 1, sub_str.size()));
108
109 // Add the value to the vector `num` times
110 for (size_t k = 0; k != num; k++)
111 {
112 vec.emplace_back(occ);
113 }
114 }
115 else
116 {
117 // Handle scientific notation and convert to T
118 std::stringstream convert;
119 convert << sub_str;
120 T occ;
121 convert >> occ;
122 vec.emplace_back(occ);
123 }
124
125 regfree(&sub_reg);
126 }
127
128 regfree(&reg);
129}
130
131#ifdef __LCAO
139std::vector<double> convert_units(std::string params, double c);
140
144void read_td_efield();
145#endif
146
147} // namespace Input_Conv
148
149#endif // Input_Convert
#define T
Definition exp.cpp:237
Definition input_conv.h:22
void parse_expression(const std::string &fn, std::vector< T > &vec)
To parse input parameters as expressions into vectors.
Definition input_conv.h:46
void tmp_convert()
template bridge codes for converting string to other types
void Convert()
Pass the data members from the INPUT instance(defined in source_io/input.cpp) to GlobalV and GlobalC.
Definition input_conv.cpp:163
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18