Debian Codegraph @

utils/openvsp/vsp2jsbsim.cpp

raw symbols SCIP
1
2 #include <iterator>
3 #include <iostream>
4 #include <fstream>
5 #include <sstream>
6 #include <iomanip>
7 #include <vector>
8 #include <string>
9 #include <map>
10
11 #define DEG2RAD(a) (3.141592657*(a)/180.0)
12
13 // https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c
14 class CSVRow
15 {
16 public:
17 std::string_view operator[](std::size_t index) const
18 {
19 return std::string_view(&m_line[m_data[index] + 1], m_data[index + 1] - (m_data[index] + 1));
20 }
21 std::size_t size() const
22 {
23 return m_data.size() - 1;
24 }
25 void readNextRow(std::istream& str)
26 {
27 std::getline(str, m_line);
28
29 m_data.clear();
30 m_data.emplace_back(-1);
31 std::string::size_type pos = 0;
32 while((pos = m_line.find(',', pos)) != std::string::npos)
33 {
34 m_data.emplace_back(pos);
35 ++pos;
36 }
37 // This checks for a trailing comma with no data after it.
38 pos = m_line.size();
39 m_data.emplace_back(pos);
40 }
41 private:
42 std::string m_line;
43 std::vector<int> m_data;
44 };
45
46 std::istream& operator>>(std::istream& str, CSVRow& data)
47 {
48 data.readNextRow(str);
49 return str;
50 }
51
52 class CSVIterator
53 {
54 public:
55 typedef std::input_iterator_tag iterator_category;
56 typedef CSVRow value_type;
57 typedef std::size_t difference_type;
58 typedef CSVRow* pointer;
59 typedef CSVRow& reference;
60
61 CSVIterator(std::istream& str) :m_str(str.good()?&str:NULL) { ++(*this); }
62 CSVIterator() :m_str(NULL) {}
63
64 // Pre Increment
65 CSVIterator& operator++() {if (m_str) { if (!((*m_str) >> m_row)){m_str = NULL;}}return *this;}
66 // Post increment
67 CSVIterator operator++(int) {CSVIterator tmp(*this);++(*this);return tmp;}
68 CSVRow const& operator*() const {return m_row;}
69 CSVRow const* operator->() const {return &m_row;}
70
71 bool operator==(CSVIterator const& rhs) {return ((this == &rhs) || ((this->m_str == NULL) && (rhs.m_str == NULL)));}
72 bool operator!=(CSVIterator const& rhs) {return !((*this) == rhs);}
73 private:
74 std::istream* m_str;
75 CSVRow m_row;
76 };
77
78 class CSVRange
79 {
80 std::istream& stream;
81 public:
82 CSVRange(std::istream& str)
83 : stream(str)
84 {}
85 CSVIterator begin() const {return CSVIterator{stream};}
86 CSVIterator end() const {return CSVIterator{};}
87 };
88
89 // https://stackoverflow.com/questions/17642882/c-2d-map-like-a-2d-array
90 template<typename T>
91 class Graph {
92 private:
93 typedef std::map<std::pair<size_t,size_t>, T> graph_type;
94 graph_type graph;
95 class SrcVertex {
96 private:
97 graph_type& graph;
98 size_t vert_src;
99 public:
100 SrcVertex(graph_type& graph): graph(graph) {}
101 T& operator[](size_t vert_dst) {
102 return graph[std::make_pair(vert_src, vert_dst)];
103 }
104 void set_vert_src(size_t vert_src) {
105 this->vert_src = vert_src;
106 }
107 } src_vertex_proxy;
108 public:
109 Graph(): src_vertex_proxy(graph) {}
110 SrcVertex& operator[](size_t vert_src) {
111 src_vertex_proxy.set_vert_src(vert_src);
112 return src_vertex_proxy;
113 }
114 };
115
116
117 // own code by Erik Hofman
118 class JSBSim
119 {
120 public:
121 std::string name;
122 std::vector<int> date;
123 std::vector<int> time;
124 std::vector<double> a, b;
125 Graph<double> CDi, CDo, CDtot, CDtrefftz;
126 Graph<double> CFx, CFy, CFz, CL;
127 Graph<double> CMx, CMy, CMz;
128
129 JSBSim(std::istream& file)
130 {
131 // every section contains 39 lines
132 size_t alpha, beta;
133 for(auto& row: CSVRange(file))
134 {
135 if (row[0] == "Results_Name") {
136 if (row[1] != "VSPAERO_History") break;
137 name = row[1];
138 }
139
140 char *end;
141 const char *start = row[row.size()-1].data();
142 double d = std::strtod(start, &end);
143 if (row[0] == "Alpha") {
144 alpha = d*100;
145 if (!a.size() || d > a.back()) a.push_back(d);
146 } else if (row[0] == "Beta") {
147 beta = d*100;
148 if (!b.size() || d > b.back()) b.push_back(d);
149 } if (row[0] == "CDi") {
150 CDi[alpha][beta] = d;
151 } if (row[0] == "CDo") {
152 CDo[alpha][beta] = d;
153 } if (row[0] == "CDtot") {
154 CDtot[alpha][beta] = d;
155 } if (row[0] == "CDtrefftz") {
156 CDtrefftz[alpha][beta] = d;
157 } if (row[0] == "CFx") {
158 CFx[alpha][beta] = d;
159 } if (row[0] == "CFy") {
160 CFy[alpha][beta] = d;
161 } if (row[0] == "CFz") {
162 CFz[alpha][beta] = d;
163 } if (row[0] == "CL") {
164 CL[alpha][beta] = d;
165 } if (row[0] == "CMx") {
166 CMx[alpha][beta] = d;
167 } if (row[0] == "CMy") {
168 CMy[alpha][beta] = d;
169 } if (row[0] == "CMz") {
170 CMz[alpha][beta] = d;
171 }
172 }
173 }
174 ~JSBSim() = default;
175
176 void print_table(Graph<double>& type, std::string name) {
177 std::cout << name << std::endl;
178 std::cout << std::setw(10) << " ";
179 for (int j=0; j<b.size(); ++j) {
180 std::cout << std::setw(8) << DEG2RAD(b[j]);
181 }
182 std::cout << std::endl;
183
184 for (int i=0; i<a.size(); ++i) {
185 std::cout << std::setw(8) << DEG2RAD(a[i]) << " ";
186 for (int j=0; j<b.size(); ++j) {
187 std::cout << std::setw(8) << type[a[i]*100][b[j]*100];
188 }
189 std::cout << std::endl;
190 }
191 std::cout << std::endl;
192 }
193
194 private:
195 template <typename T>
196 void push_vector(std::vector<T>& dst, const CSVRow& row) {
197 for (int i=1; i<row.size(); ++i)
198 {
199 char *end;
200 const char *start = row[i].data();
201 double d = std::strtod(start, &end);
202 dst.push_back(d);
203 }
204 }
205 };
206
207 int main(int argc, char *argv[])
208 {
209 std::ifstream file(argv[1]);
210 JSBSim jsb(file);
211
212 std::cout << "Name: " << jsb.name << std::endl;
213
214 std::cout << std::fixed << std::showpoint << std::setprecision(3);
215
216 std::cout << "LIFT:" << std::endl;
217 jsb.print_table(jsb.CL, "CL");
218
219 std::cout << "DRAG:" << std::endl;
220 jsb.print_table(jsb.CDi, "CDi (Induced Drag)");
221 jsb.print_table(jsb.CDo, "CDo (Parasitic Drag)");
222 jsb.print_table(jsb.CDtot, "CDtot (CDi + CDo)");
223 jsb.print_table(jsb.CDtrefftz, "CDtrefftz (Trefftz Method)");
224
225 std::cout << "FORCES:" << std::endl;
226 jsb.print_table(jsb.CFx, "CFx (Drag)");
227 jsb.print_table(jsb.CFy, "CFy (Side)");
228 jsb.print_table(jsb.CFz, "CFz (Lift)");
229
230 std::cout << "MOMENTS:" << std::endl;
231 jsb.print_table(jsb.CMx, "CMx (Roll)");
232 jsb.print_table(jsb.CMy, "CMy (Pitch)");
233 jsb.print_table(jsb.CMz, "CMz (Yaw)");
234 }