CHP_modelling
Loading...
Searching...
No Matches
Parameters.cpp
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include <fstream>
6#include <iostream>
7#include <sstream>
8#include <vector>
9
10using namespace std;
11
12struct parameter {
13 public:
14 string sys_type, sys_def;
15 string data_def, data_id, data_type, data_info;
16 size_t pos;
17 vector<string> str;
18 vector<double> num;
19 parameter(string line);
20 parameter(){};
21};
22
23parameter::parameter(string line) {
24 stringstream sst(line);
25 sst >> data_def;
26 sst >> sys_type;
27 sst >> sys_def;
28 sst >> data_id;
29 string txt;
30 while (sst >> txt) {
31 str.push_back(txt);
32 }
33 pos = 0;
34}
35
36struct object {
37 public:
38 string sys_type, sys_def, sys_file;
39 vector<object> c;
40 vector<parameter> p;
41 object(string type, string def, string file);
42 object(string type, string def);
43 object(){};
44 int ic(string type, string def);
45 int ip(string symb);
46 bool bp(string symb);
47 double fp(string symb);
48 vector<double> vctp(string symb);
49 string sp(string symb);
50 vector<string> svct(string symb);
51 void fval_p(string symb, double val);
52 void sval_p(string symb, string val);
53 void vct_fp(string symb, vector<double> vct);
54 void vct_sp(string symb, vector<string> vct);
55};
56
57string get_parameter_value(vector<parameter> &par, string sys_type, string sys_def,
58 string data_id) {
59 // cout << "getting value for " << data_id << " in " << sys_type << " " << sys_def <<
60 // endl;
61 bool found = false;
62 for (size_t np = 0; np < par.size(); np++) {
63 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
64 par[np].data_id == data_id) {
65 string val = par[np].str[par[np].pos];
66 // cout << "value : " << val << endl;
67 if (par[np].pos < par[np].str.size() - 1) {
68 par[np].pos = par[np].pos + 1;
69 }
70 found = true;
71 return val;
72 }
73 }
74 if (found == false) {
75 return "null";
76 }
77 return "null";
78}
79
80void print_parameter_value(vector<parameter> &p, string sys_type, string sys_def,
81 string data_id) {
82 bool found = false;
83 for (size_t np = 0; np < p.size(); np++) {
84 if (p[np].sys_type == sys_type && p[np].sys_def == sys_def &&
85 p[np].data_id == data_id) {
86 found = true;
87 cout << p[np].sys_type << " " << p[np].sys_def << " " << p[np].data_def << " "
88 << p[np].data_id << " " << p[np].str[p[np].pos] << endl;
89 }
90 }
91 if (found == false) {
92 cout << "none found" << endl;
93 }
94}
95
96void export_parameter(vector<parameter> &par, string data_def, string sys_type,
97 string sys_def, string data_id, string val) {
98 // cout << "exporting parameter" << endl;
99 bool found = false;
100 for (size_t np = 0; np < par.size(); np++) {
101 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
102 par[np].data_id == data_id) {
103 par[np].str.push_back(val);
104 par[np].pos = par[np].str.size() - 1;
105 found = true;
106 }
107 }
108 if (found == false) {
109 // cout << "not found in the list... creating" << endl;
110 parameter p;
111 p.data_def = data_def;
112 p.sys_type = sys_type;
113 p.sys_def = sys_def;
114 p.data_id = data_id;
115 p.str.push_back(val);
116 p.pos = 0;
117 par.push_back(p);
118 // print_parameter_value(par,par[par.size()-1].sys_type, par[par.size()-1].sys_def,
119 // par[par.size()-1].data_id);
120 }
121}
122
123void get_parameters(vector<parameter> &par, string sys_type, string sys_def,
124 string input_file) {
125 // cout << "getting parameter for " << sys_type << " " << sys_def << endl;
126 ifstream p_file;
127 p_file.open(input_file);
128 if (!p_file.good()) {
129 cout << "input file not found " << endl;
130 p_file.close();
131 return;
132 }
133
134 parameter p;
135 string line_txt, type, def, txt, str;
136 double num;
137 bool par_set_found = false, sys_found = false;
138
139 while (!sys_found) {
140 getline(p_file, line_txt);
141 stringstream sst(line_txt);
142 sst >> type;
143 sst >> def;
144 if (type == sys_type && def == sys_def) {
145 // cout << sys_type << " " << sys_def << endl;
146 sys_found = true;
147
148 while (!par_set_found) {
149 getline(p_file, line_txt); // cout << "line_txt: " << line_txt << endl;
150 stringstream sst(line_txt);
151 sst >> txt;
152
153 if (txt == "input" || txt == "prop" || txt == "output") {
154 p.sys_type = sys_type;
155 p.sys_def = sys_def;
156 p.data_def = txt;
157 sst >> p.data_id;
158 // cout << "data_def: " << txt << " data_id: " << p.data_id << endl;
159 bool str_complete = false;
160 p.data_info = "";
161 while (sst >> str) {
162 vector<char> cstr(str.begin(), str.end());
163 if (!str_complete && cstr[0] != '#') {
164 p.str.push_back(str);
165 }
166 if (cstr[0] == '#') {
167 str_complete = true;
168 }
169 if (str_complete && cstr[0] != '#') {
170 p.data_info = str + " ";
171 }
172 }
173 p.pos = 0;
174 par.push_back(p);
175 p = parameter();
176 p.str.clear();
177 p.num.clear();
178 }
179
180 if (txt != "input" && txt != "output" && txt != "prop") {
181 par_set_found = true;
182 p_file.close();
183 return;
184 }
185 }
186 }
187 if (p_file.eof()) {
188 p_file.close();
189 return;
190 }
191 }
192}
193
194void export_output_parameters(object &obj, string file) {
195 ofstream output_parameters(file);
196
197 for (size_t np = 0; np < obj.p.size(); np++) {
198 if (obj.p[np].data_def == "output") {
199 output_parameters << obj.p[np].sys_type << " " << obj.p[np].sys_def << " "
200 << obj.p[np].data_id;
201
202 for (size_t ns = 0; ns < obj.p[np].str.size(); ns++) {
203 output_parameters << " " << obj.p[np].str[ns];
204 }
205 output_parameters << endl;
206 }
207 }
208
209 output_parameters.close();
210}
211
212void print_parameters(vector<parameter> &p) {
213 cout << "no. parameters: " << p.size() << endl;
214 for (size_t np = 0; np < p.size(); np++) {
215 cout << p[np].sys_type << " " << p[np].sys_def << " " << p[np].data_def << " "
216 << p[np].data_type;
217
218 for (size_t ns = 0; ns < p[np].str.size(); ns++) {
219 cout << " " << p[np].str[ns];
220 }
221 cout << endl;
222 }
223}
224
225double fp(vector<parameter> &par, string sys_type, string sys_def, string data_id) {
226 if (get_parameter_value(par, sys_type, sys_def, data_id) == "null") {
227 return -1;
228 }
229 return atof(get_parameter_value(par, sys_type, sys_def, data_id).c_str());
230}
231
232string sp(vector<parameter> &par, string sys_type, string sys_def, string data_id) {
233 return get_parameter_value(par, sys_type, sys_def, data_id);
234}
235
236vector<double> fp_vct(vector<parameter> &par, string sys_type, string sys_def,
237 string data_id) {
238 bool found = false;
239 vector<double> vct;
240 for (size_t np = 0; np < par.size(); np++) {
241 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
242 par[np].data_id == data_id) {
243 for (size_t n = 0; n < par[np].str.size(); n++) {
244 vct.push_back(atof(par[np].str[n].c_str()));
245 }
246 found = true;
247 return vct;
248 }
249 }
250 // if( found == false ) { return vct; }
251 return vct;
252}
253
254vector<string> sp_vct(vector<parameter> &par, string sys_type, string sys_def,
255 string data_id) {
256 bool found = false;
257 vector<string> vct;
258 for (size_t np = 0; np < par.size(); np++) {
259 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
260 par[np].data_id == data_id) {
261 for (size_t n = 0; n < par[np].str.size(); n++) {
262 vct.push_back(par[np].str[n]);
263 }
264 found = true;
265 return vct;
266 }
267 }
268 if (found == false) {
269 return vct;
270 }
271 return vct;
272}
273
274void fval_p(vector<parameter> &par, string data_def, string sys_type, string sys_def,
275 string data_id, double val) {
276 export_parameter(par, data_def, sys_type, sys_def, data_id, to_string(val));
277}
278
279void sval_p(vector<parameter> &par, string data_def, string sys_type, string sys_def,
280 string data_id, string val) {
281 export_parameter(par, data_def, sys_type, sys_def, data_id, val);
282}
283
284void fvct_p(vector<parameter> &par, string data_def, string sys_type, string sys_def,
285 string data_id, vector<double> val) {
286 // cout << "exporting parameter" << endl;
287 bool found = false;
288 for (size_t np = 0; np < par.size(); np++) {
289 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
290 par[np].data_id == data_id) {
291 par[np].str.clear();
292 for (size_t n = 0; n < val.size(); n++) {
293 par[np].str.push_back(to_string(val[n]));
294 }
295 par[np].pos = 0;
296 found = true;
297 }
298 }
299 if (found == false) {
300 // cout << "not found in the list... creating" << endl;
301 parameter p;
302 p.data_def = data_def;
303 p.sys_type = sys_type;
304 p.sys_def = sys_def;
305 p.data_id = data_id;
306 p.pos = 0;
307 for (size_t n = 0; n < val.size(); n++) {
308 p.str.push_back(to_string(val[n]));
309 }
310 par.push_back(p);
311 // print_parameter_value(par,par[par.size()-1].sys_type, par[par.size()-1].sys_def,
312 // par[par.size()-1].data_id);
313 }
314}
315
316void svct_p(vector<parameter> &par, string data_def, string sys_type, string sys_def,
317 string data_id, vector<string> val) {
318 // cout << "exporting parameter" << endl;
319 bool found = false;
320 for (size_t np = 0; np < par.size(); np++) {
321 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
322 par[np].data_id == data_id) {
323 par[np].str.clear();
324 for (size_t n = 0; n < val.size(); n++) {
325 par[np].str.push_back(val[n]);
326 }
327 par[np].pos = 0;
328 found = true;
329 }
330 }
331 if (found == false) {
332 // cout << "not found in the list... creating" << endl;
333 parameter p;
334 p.data_def = data_def;
335 p.sys_type = sys_type;
336 p.sys_def = sys_def;
337 p.data_id = data_id;
338 p.pos = 0;
339 for (size_t n = 0; n < val.size(); n++) {
340 p.str.push_back(val[n]);
341 }
342 par.push_back(p);
343 // print_parameter_value(par,par[par.size()-1].sys_type, par[par.size()-1].sys_def,
344 // par[par.size()-1].data_id);
345 }
346}
347
348object::object(string type, string def, string file) {
349 sys_type = type;
350 sys_def = def;
351 sys_file = file;
352 get_parameters(p, type, def, file);
353}
354
355object::object(string type, string def) {
356 sys_type = type;
357 sys_def = def;
358 if (type == "equipment") {
359 get_parameters(p, type, def, DIR + "Database/Equipment_database");
360 }
361 if (type == "consumable") {
362 get_parameters(p, type, def, DIR + "Database/Consumables_database");
363 }
364 if (type == "solid_residue") {
365 get_parameters(p, type, def, DIR + "Database/Consumables_database");
366 }
367}
368
369int object::ic(string type, string def) {
370 for (size_t n = 0; n < c.size(); n++) {
371 if (c[n].sys_type == type && c[n].sys_def == def) {
372 return n;
373 }
374 }
375 return -1;
376}
377
378int object::ip(string symb) {
379 bool found = false;
380 for (size_t np = 0; np < p.size(); np++) {
381 if (p[np].data_id == symb) {
382 return np;
383 }
384 }
385 // if( found == false ) { return -1; }
386 return -1;
387}
388
389bool object::bp(string symb) {
390 bool found = false;
391 for (size_t np = 0; np < p.size(); np++) {
392 if (p[np].data_id == symb) {
393 return true;
394 }
395 }
396 // if( found == false ) { return false; }
397 return false;
398}
399
400double object::fp(string symb) {
401 string val = get_parameter_value(p, sys_type, sys_def, symb);
402 if (val == "null") {
403 return -1;
404 }
405 return atof(val.c_str());
406}
407string object::sp(string symb) { return get_parameter_value(p, sys_type, sys_def, symb); }
408
409vector<double> object::vctp(string symb) { return fp_vct(p, sys_type, sys_def, symb); }
410
411vector<string> object::svct(string symb) { return sp_vct(p, sys_type, sys_def, symb); }
412
413void object::fval_p(string symb, double val) {
414 if (divide_string(symb, '-').size() == 1) {
415 export_parameter(p, "prop", sys_type, sys_def, symb, to_string(val));
416 }
417 if (divide_string(symb, '-').size() == 2) {
418 export_parameter(p, divide_string(symb, '-')[0], sys_type, sys_def,
419 divide_string(symb, '-')[1], to_string(val));
420 }
421}
422
423void object::sval_p(string symb, string val) {
424 if (divide_string(symb, '-').size() == 1) {
425 export_parameter(p, "prop", sys_type, sys_def, symb, val);
426 }
427 if (divide_string(symb, '-').size() == 2) {
428 export_parameter(p, divide_string(symb, '-')[0], sys_type, sys_def,
429 divide_string(symb, '-')[1], val);
430 }
431}
432
433void object::vct_fp(string symb, vector<double> vct) {
434 if (divide_string(symb, '-').size() == 1) {
435 fvct_p(p, "prop", sys_type, sys_def, symb, vct);
436 }
437 if (divide_string(symb, '-').size() == 2) {
438 fvct_p(p, divide_string(symb, '-')[0], sys_type, sys_def, divide_string(symb, '-')[1],
439 vct);
440 }
441}
442
443void object::vct_sp(string symb, vector<string> vct) {
444 if (divide_string(symb, '-').size() == 1) {
445 svct_p(p, "prop", sys_type, sys_def, symb, vct);
446 }
447 if (divide_string(symb, '-').size() == 2) {
448 svct_p(p, divide_string(symb, '-')[0], sys_type, sys_def, divide_string(symb, '-')[1],
449 vct);
450 }
451}
452
453void transfer_parameter(string symb, object from, object &to) {
454 to.p.push_back(from.p[from.ip(symb)]);
455}
Definition Parameters.cpp:36
Definition Parameters.cpp:12