sisi4s
Loading...
Searching...
No Matches
MosParser.hpp
Go to the documentation of this file.
1#ifndef _TURBOMOLE_MOVECESP_PARSER
2#define _TURBOMOLE_MOVECESP_PARSER
3
4#include <regex>
5#include <string>
6#include <algorithm>
7#include <vector>
8#include <fstream>
9#include <assert.h>
10#include <util/Log.hpp>
11#include <util/Parsing.hpp>
12
13using namespace pars;
14namespace tmole {
15
16struct MosParser {
17 size_t Np;
18 std::vector<size_t> Nsaos;
19 std::vector<double> occupations, eigenvalues, mos;
20 std::smatch match;
23 expnumber = anyChar + "." + digit + oneOrMore + "D"
26 comment = bof + "#" + print + anyOf,
27 unper = bof + anyChar + "scfmo" + print + anyOf,
29 + capture(symm.s) + sep.s
30 + "eigenvalue=" + capture(expnumber.s) + sep.s
31 + "nsaos=" + capture(number.s) + print + anyOf,
38
39 MosParser(const std::string &fileName) {
40 std::fstream file(fileName.c_str(), std::ios::binary | std::ios::in);
41 std::vector<double> mos_buf;
42 std::string s, e;
43 const auto fortranDoubleToDouble = [](std::string &s) {
44 std::replace(s.begin(), s.end(), 'D', 'e');
45 return std::atof(s.c_str());
46 };
47 size_t buf_size;
48 if (!file.good()) throw "File " + fileName + " not found";
49 if (!file.is_open()) throw "File IO error: " + fileName;
50 std::string line;
51 while (std::getline(file, line)) {
52
53 // empty lines ro comments
54 if (std::regex_match(line, match, comment.r)
55 || std::regex_match(line, match, unper.r))
56 continue;
57
58 // eigenvalues
59 if (std::regex_match(line, match, eigenval.r)) {
60 e = std::string(match[3]);
61 mos_buf.resize(0);
62 buf_size = atoi(std::string(match[4]).c_str());
63 Nsaos.push_back(buf_size);
64 }
65
66 if (std::regex_match(line, match, mosline1.r)
67 || std::regex_match(line, match, mosline2.r)
68 || std::regex_match(line, match, mosline3.r)
69 || std::regex_match(line, match, mosline4.r))
70 for (size_t i(1); i < match.size(); i++) {
71 s = std::string(match[i]);
72 mos_buf.push_back(fortranDoubleToDouble(s));
73 }
74
75 if (mos_buf.size() > 0 && mos_buf.size() == buf_size) {
76 eigenvalues.push_back(fortranDoubleToDouble(e));
77 for (size_t i(0); i < mos_buf.size(); i++) {
78 mos.push_back(mos_buf[i]);
79 }
80 mos_buf.resize(0);
81 }
82 }
83
84 Np = eigenvalues.size();
85
86 file.close();
87 }
88};
89
90} // namespace tmole
91
92#endif
Definition Parsing.cxx:6
const Str bof("^")
const Str alnum("[[:alnum:]]")
const Str blank("[[:blank:]]")
const Str anyOf("*")
const Str oneOrMore("+")
const std::function< Str(Str)> capture([](const Str &i) { return "("+i+")";})
const Str digit("[[:digit:]]")
const Str anyChar(".")
const Str print("[[:print:]]")
Definition MosParser.hpp:14
Definition Parsing.hpp:45
const std::regex r
Definition Parsing.hpp:47
const std::string s
Definition Parsing.hpp:46
Definition MosParser.hpp:16
std::vector< double > eigenvalues
Definition MosParser.hpp:19
const pars::Regex comment
Definition MosParser.hpp:26
const pars::Regex mosline2
Definition MosParser.hpp:33
const pars::Regex mosline3
Definition MosParser.hpp:34
const pars::Regex twodigits
Definition MosParser.hpp:22
const pars::Regex symm
Definition MosParser.hpp:25
std::vector< double > mos
Definition MosParser.hpp:19
MosParser(const std::string &fileName)
Definition MosParser.hpp:39
std::vector< size_t > Nsaos
Definition MosParser.hpp:18
const pars::Regex mosline1
Definition MosParser.hpp:32
const pars::Regex expnumber
Definition MosParser.hpp:23
std::vector< double > occupations
Definition MosParser.hpp:19
std::smatch match
Definition MosParser.hpp:20
size_t Np
Definition MosParser.hpp:17
const pars::Regex unper
Definition MosParser.hpp:27
const pars::Regex eigenval
Definition MosParser.hpp:28
const pars::Regex number
Definition MosParser.hpp:21
const pars::Regex sep
Definition MosParser.hpp:21
const pars::Regex mosline4
Definition MosParser.hpp:36