-
Notifications
You must be signed in to change notification settings - Fork 0
/
stb.h
71 lines (61 loc) · 1.63 KB
/
stb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once
#include <Eigen/Dense>
#include <ctre.hpp>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
namespace stb {
struct Equil {
Eigen::VectorXd lgk;
Eigen::MatrixXd matrix;
};
Equil readEquil(const std::string &filename) {
std::ifstream ifs(filename);
std::map<std::string, std::vector<std::string>> stb_blocks;
std::string line;
while (std::getline(ifs, line)) {
if (auto m = ctre::match<"^\\s+\\$(\\w+)\\s+$">(line)) {
std::string block = m.get<1>().to_string();
while (std::getline(ifs, line) &&
!ctre::match<"^\\s+\\$end\\s+$">(line)) {
stb_blocks[block].push_back(line);
}
}
}
Equil res;
if (auto it = stb_blocks.find("matrix"); it != stb_blocks.end()) {
bool is_matrix_read = false;
unsigned basis = -1;
size_t size = it->second.size();
Eigen::MatrixXd &matrix = res.matrix;
Eigen::VectorXd &lnk = res.lgk;
for (const auto &l : it->second) {
if (!is_matrix_read) {
--size;
if (auto m = ctre::match<"^\\s*(\\w+)=(\\d+)\\s+$">(l)) {
if (m.get<1>() == "basis") {
basis = std::stoi(m.get<2>().to_string());
}
}
else if (auto m = ctre::match<"^\\s*([A-Z]\\s+)+$">(l)) {
is_matrix_read = true;
matrix.resize(size, basis);
lnk.resize(size);
size = 0;
continue;
}
}
else {
std::stringstream ss(l);
for (unsigned i = 0; i < basis; ++i) {
ss >> matrix.coeffRef(size, i);
}
ss >> lnk(size++);
}
}
}
return res;
}
} // namespace stb