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> vct;
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 sst >> data_type;
30 if (data_type == "str") {
31 string txt;
32 while (sst >> txt) {
33 str.push_back(txt);
34 }
35 }
36 if (data_type == "num") {
37 double val;
38 while (sst >> val) {
39 vct.push_back(val);
40 }
41 }
42
43 pos = 0;
44}
45
46struct object {
47 public:
48 string sys_type, sys_def, sys_file;
49 vector<object> c;
50 vector<parameter> p;
51 object(string type, string def, string file);
52 object(string type, string def);
53 object(){};
54 int ic(string type, string def);
55 int ip(string symb);
56 bool bp(string symb);
57 double fp(string symb);
58 vector<double> vctp(string symb);
59 string sp(string symb);
60 vector<string> svct(string symb);
61 void fval_p(string symb, double val);
62 void sval_p(string symb, string val);
63 void vct_fp(string symb, vector<double> vct);
64 void vct_sp(string symb, vector<string> vct);
65};
66
67// **********************************************************
68
69string get_str_parameter(vector<parameter> &par, string sys_type,
70 string sys_def, string data_id) {
71 bool found = false;
72 for (size_t np = 0; np < par.size(); np++) {
73 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
74 par[np].data_id == data_id) {
75 string val = par[np].str[par[np].pos];
76 // cout << "value : " << val << endl;
77 if (par[np].pos < par[np].str.size() - 1) {
78 par[np].pos = par[np].pos + 1;
79 }
80 found = true;
81 return val;
82 }
83 }
84 if (found == false) {
85 return "null";
86 }
87 return "null";
88}
89
90double get_num_parameter(vector<parameter> &par, string sys_type,
91 string sys_def, string data_id) {
92 bool found = false;
93 for (size_t np = 0; np < par.size(); np++) {
94 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
95 par[np].data_id == data_id) {
96 double val = par[np].vct[par[np].pos];
97 // cout << "value : " << val << endl;
98 if (par[np].pos < par[np].vct.size() - 1) {
99 par[np].pos = par[np].pos + 1;
100 }
101 found = true;
102 return val;
103 }
104 }
105 return -1;
106}
107
108// **********************************************************
109
110void print_parameter_value(vector<parameter> &p, string sys_type,
111 string sys_def, string data_id) {
112 bool found = false;
113 for (size_t np = 0; np < p.size(); np++) {
114 if (p[np].sys_type == sys_type && p[np].sys_def == sys_def &&
115 p[np].data_id == data_id) {
116 found = true;
117 cout << p[np].sys_type << " " << p[np].sys_def << " " << p[np].data_def
118 << " " << p[np].data_id << " " << p[np].str[p[np].pos] << endl;
119 }
120 }
121 if (found == false) {
122 cout << "none found" << endl;
123 }
124}
125
126// **********************************************************
127
128/*
129void export_parameter(vector<parameter> &par, string data_def, string sys_type,
130 string sys_def, string data_id, string data_type, string
131val) {
132 // cout << "exporting parameter" << endl;
133 bool found = false;
134 for (size_t np = 0; np < par.size(); np++) {
135 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
136 par[np].data_id == data_id) {
137 if( data_type == "str" ) { par[np].str.push_back(val); }
138 if( data_type == "num" ) { par[np].vct.push_back(stod(val)); }
139 par[np].pos = par[np].str.size() - 1;
140 found = true;
141 }
142 }
143 if (found == false) {
144 // cout << "not found in the list... creating" << endl;
145 parameter p;
146 p.data_def = data_def;
147 p.sys_type = sys_type;
148 p.sys_def = sys_def;
149 p.data_id = data_id;
150 p.data_type = data_type;
151 if( data_type == "str" ) { p.str.push_back(val); }
152 if( data_type == "num" ) { p.vct.push_back(stod(val)); }
153 p.pos = 0;
154 par.push_back(p);
155 }
156}
157*/
158
159// **********************************************************
160
161void get_parameters(vector<parameter> &par, string sys_type, string sys_def,
162 string input_file) {
163 // cout << "getting parameter for " << sys_type << " " << sys_def << endl;
164 ifstream p_file;
165 p_file.open(input_file);
166 if (!p_file.good()) {
167 cout << "input file not found " << endl;
168 p_file.close();
169 return;
170 }
171
172 parameter p;
173 string line_txt, type, def, txt, str;
174 double num;
175 bool par_set_found = false, sys_found = false;
176
177 while (!sys_found) {
178 getline(p_file, line_txt);
179 stringstream sst(line_txt);
180 sst >> type;
181 sst >> def;
182 if (type == sys_type && def == sys_def) {
183 // cout << sys_type << " " << sys_def << endl;
184 sys_found = true;
185
186 while (!par_set_found) {
187 getline(p_file, line_txt); // cout << "line_txt: " << line_txt << endl;
188 stringstream sst(line_txt);
189 sst >> txt;
190
191 if (txt == "input" || txt == "prop" || txt == "output") {
192 p.sys_type = sys_type;
193 p.sys_def = sys_def;
194 p.data_def = txt;
195 sst >> p.data_id;
196 sst >> p.data_type;
197 // cout << "data_def: " << txt << " data_id: " << p.data_id << "
198 // data_type: " << p.data_type << endl;
199
200 bool str_complete = false;
201
202 p.data_info = "";
203 while (sst >> str) {
204 vector<char> cstr(str.begin(), str.end());
205 if (!str_complete && cstr[0] != '#') {
206 if (p.data_type == "str") {
207 p.str.push_back(str);
208 }
209 if (p.data_type == "num") {
210 p.vct.push_back(stod(str));
211 }
212 }
213 if (cstr[0] == '#') {
214 str_complete = true;
215 }
216 if (str_complete && cstr[0] != '#') {
217 p.data_info = str + " ";
218 }
219 }
220
221 p.pos = 0;
222 par.push_back(p);
223 p = parameter();
224 p.str.clear();
225 p.vct.clear();
226 }
227
228 if (txt != "input" && txt != "output" && txt != "prop") {
229 par_set_found = true;
230 p_file.close();
231 return;
232 }
233 }
234 }
235 if (p_file.eof()) {
236 p_file.close();
237 return;
238 }
239 }
240}
241
242// **********************************************************
243
244object::object(string type, string def, string file) {
245 sys_type = type;
246 sys_def = def;
247 sys_file = file;
248 get_parameters(p, type, def, file);
249}
250
251object::object(string type, string def) {
252 sys_type = type;
253 sys_def = def;
254 if (type == "equipment") {
255 get_parameters(p, type, def, DIR + "Database/Equipment_database");
256 }
257 if (type == "consumable") {
258 get_parameters(p, type, def, DIR + "Database/Consumables_database");
259 }
260 if (type == "solid_residue") {
261 get_parameters(p, type, def, DIR + "Database/Consumables_database");
262 }
263}
264
265// **********************************************************
266
267void export_output_parameters(object &obj, string file) {
268 ofstream output_parameters(file);
269
270 for (size_t np = 0; np < obj.p.size(); np++) {
271 if (obj.p[np].data_def == "output") {
272 output_parameters << obj.p[np].sys_type << " " << obj.p[np].sys_def << " "
273 << obj.p[np].data_id << " " << obj.p[np].data_type;
274
275 if (obj.p[np].data_type == "str") {
276 for (size_t ns = 0; ns < obj.p[np].str.size(); ns++) {
277 output_parameters << " " << obj.p[np].str[ns];
278 }
279 }
280
281 if (obj.p[np].data_type == "num") {
282 for (size_t ns = 0; ns < obj.p[np].vct.size(); ns++) {
283 output_parameters << " " << obj.p[np].vct[ns];
284 }
285 }
286
287 output_parameters << endl;
288 }
289 }
290
291 output_parameters.close();
292}
293
294// **********************************************************
295
296void print_parameter(parameter &p) {
297 cout << p.data_id << " " << p.data_type;
298
299 if (p.data_type == "str") {
300 for (size_t ns = 0; ns < p.str.size(); ns++) {
301 cout << " " << p.str[ns];
302 }
303 cout << endl;
304 }
305
306 if (p.data_type == "num") {
307 for (size_t ns = 0; ns < p.vct.size(); ns++) {
308 cout << " " << p.vct[ns];
309 }
310 cout << endl;
311 }
312}
313
314// **********************************************************
315
316void print_parameters(object &obj) {
317 cout << " -------------------------------------- " << endl;
318 cout << obj.sys_type << " " << obj.sys_def << " parameters: " << endl;
319 cout << " -------------------------------------- " << endl;
320
321 for (size_t np = 0; np < obj.p.size(); np++) {
322 print_parameter(obj.p[np]);
323 /*
324 cout << obj.p[np].data_id << " " << obj.p[np].data_type;
325
326 if( obj.p[np].data_type == "str" ){
327 for (size_t ns = 0; ns < obj.p[np].str.size(); ns++) {
328 cout << " " << obj.p[np].str[ns];
329 } cout << endl;
330 }
331
332 if( obj.p[np].data_type == "num" ){
333 for (size_t ns = 0; ns < obj.p[np].vct.size(); ns++) {
334 cout << " " << obj.p[np].vct[ns];
335 } cout << endl;
336 }
337 */
338 }
339 cout << " -------------------------------------- " << endl;
340
341 if (obj.c.size() > 0) {
342 for (size_t nc = 0; nc < obj.c.size(); nc++) {
343 print_parameters(obj.c[nc]);
344 }
345 }
346}
347
348// **********************************************************
349
350double fp(vector<parameter> &par, string sys_type, string sys_def,
351 string data_id) {
352 return get_num_parameter(par, sys_type, sys_def, data_id);
353}
354
355// **********************************************************
356
357string sp(vector<parameter> &par, string sys_type, string sys_def,
358 string data_id) {
359 return get_str_parameter(par, sys_type, sys_def, data_id);
360}
361
362// **********************************************************
363
364vector<string> sp_vct(vector<parameter> &par, string sys_type, string sys_def,
365 string data_id) {
366 bool found = false;
367 vector<string> vct;
368 for (size_t np = 0; np < par.size(); np++) {
369 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
370 par[np].data_id == data_id) {
371 for (size_t n = 0; n < par[np].str.size(); n++) {
372 vct.push_back(par[np].str[n]);
373 }
374 found = true;
375 return vct;
376 }
377 }
378 if (found == false) {
379 return vct;
380 }
381 return vct;
382}
383
384// **********************************************************
385
386int object::ic(string type, string def) {
387 for (size_t n = 0; n < c.size(); n++) {
388 if (c[n].sys_type == type && c[n].sys_def == def) {
389 return n;
390 }
391 }
392 return -1;
393}
394
395int object::ip(string symb) {
396 bool found = false;
397 for (size_t np = 0; np < p.size(); np++) {
398 if (p[np].data_id == symb) {
399 return np;
400 }
401 }
402 // if( found == false ) { return -1; }
403 return -1;
404}
405
406bool object::bp(string symb) {
407 bool found = false;
408 for (size_t np = 0; np < p.size(); np++) {
409 if (p[np].data_id == symb) {
410 return true;
411 }
412 }
413 // if( found == false ) { return false; }
414 return false;
415}
416
417// **********************************************************
418
419double object::fp(string symb) {
420 return get_num_parameter(p, sys_type, sys_def, symb);
421}
422
423// **********************************************************
424
425string object::sp(string symb) {
426 return get_str_parameter(p, sys_type, sys_def, symb);
427}
428
429// **********************************************************
430
431vector<string> object::svct(string symb) {
432 return sp_vct(p, sys_type, sys_def, symb);
433}
434
435// **********************************************************
436
437void val_p(vector<parameter> &par, string data_def, string sys_type,
438 string sys_def, string data_id, double val) {
439 // export_parameter(par, data_def, sys_type, sys_def, data_id, "num", val);
440
441 bool found = false;
442 for (size_t np = 0; np < par.size(); np++) {
443 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
444 par[np].data_id == data_id) {
445 par[np].vct.push_back(val);
446 par[np].pos = par[np].str.size() - 1;
447 found = true;
448 }
449 }
450 if (found == false) {
451 // cout << "not found in the list... creating" << endl;
452 parameter p;
453 p.data_def = data_def;
454 p.sys_type = sys_type;
455 p.sys_def = sys_def;
456 p.data_id = data_id;
457 p.data_type = "num";
458 p.vct.push_back(val);
459 p.pos = 0;
460 par.push_back(p);
461 }
462}
463
464// **********************************************************
465
466void object::fval_p(string symb, double val) {
467 if (divide_string(symb, '-').size() == 1) {
468 val_p(p, "prop", sys_type, sys_def, symb, val);
469 }
470 if (divide_string(symb, '-').size() == 2) {
471 val_p(p, divide_string(symb, '-')[0], sys_type, sys_def,
472 divide_string(symb, '-')[1], val);
473 }
474}
475
476// **********************************************************
477
478void str_p(vector<parameter> &par, string data_def, string sys_type,
479 string sys_def, string data_id, string val) {
480 // export_parameter(par, data_def, sys_type, sys_def, data_id, "str", val);
481
482 bool found = false;
483 for (size_t np = 0; np < par.size(); np++) {
484 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
485 par[np].data_id == data_id) {
486 par[np].str.push_back(val);
487 par[np].pos = par[np].str.size() - 1;
488 found = true;
489 }
490 }
491 if (found == false) {
492 // cout << "not found in the list... creating" << endl;
493 parameter p;
494 p.data_def = data_def;
495 p.sys_type = sys_type;
496 p.sys_def = sys_def;
497 p.data_id = data_id;
498 p.data_type = "str";
499 p.str.push_back(val);
500 p.pos = 0;
501 par.push_back(p);
502 }
503}
504
505// **********************************************************
506
507void object::sval_p(string symb, string val) {
508 if (divide_string(symb, '-').size() == 1) {
509 str_p(p, "prop", sys_type, sys_def, symb, val);
510 }
511 if (divide_string(symb, '-').size() == 2) {
512 str_p(p, divide_string(symb, '-')[0], sys_type, sys_def,
513 divide_string(symb, '-')[1], val);
514 }
515}
516
517// **********************************************************
518
519vector<double> fp_vct(vector<parameter> &par, string sys_type, string sys_def,
520 string data_id) {
521 bool found = false;
522 vector<double> vct;
523 for (size_t np = 0; np < par.size(); np++) {
524 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
525 par[np].data_id == data_id) {
526 for (size_t n = 0; n < par[np].vct.size(); n++) {
527 vct.push_back(par[np].vct[n]);
528 }
529 found = true;
530 return vct;
531 }
532 }
533 return vct;
534}
535
536// **********************************************************
537
538vector<double> object::vctp(string symb) {
539 return fp_vct(p, sys_type, sys_def, symb);
540}
541
542// **********************************************************
543
544void fvct_p(vector<parameter> &par, string data_def, string sys_type,
545 string sys_def, string data_id, vector<double> val) {
546 // cout << "exporting parameter" << endl;
547 bool found = false;
548 for (size_t np = 0; np < par.size(); np++) {
549 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
550 par[np].data_id == data_id) {
551 par[np].vct.clear();
552 for (size_t n = 0; n < val.size(); n++) {
553 par[np].vct.push_back(val[n]);
554 }
555 par[np].pos = 0;
556 found = true;
557 }
558 }
559 if (found == false) {
560 // cout << "not found in the list... creating" << endl;
561 parameter p;
562 p.data_def = data_def;
563 p.sys_type = sys_type;
564 p.sys_def = sys_def;
565 p.data_id = data_id;
566 p.data_type = "num";
567 p.pos = 0;
568 for (size_t n = 0; n < val.size(); n++) {
569 p.vct.push_back(val[n]);
570 }
571 par.push_back(p);
572 // print_parameter_value(par,par[par.size()-1].sys_type,
573 // par[par.size()-1].sys_def, par[par.size()-1].data_id);
574 }
575}
576
577// **********************************************************
578
579void object::vct_fp(string symb, vector<double> vct) {
580 if (divide_string(symb, '-').size() == 1) {
581 fvct_p(p, "prop", sys_type, sys_def, symb, vct);
582 }
583 if (divide_string(symb, '-').size() == 2) {
584 fvct_p(p, divide_string(symb, '-')[0], sys_type, sys_def,
585 divide_string(symb, '-')[1], vct);
586 }
587}
588
589// **********************************************************
590
591void svct_p(vector<parameter> &par, string data_def, string sys_type,
592 string sys_def, string data_id, vector<string> val) {
593 // cout << "exporting parameter" << endl;
594 bool found = false;
595 for (size_t np = 0; np < par.size(); np++) {
596 if (par[np].sys_type == sys_type && par[np].sys_def == sys_def &&
597 par[np].data_id == data_id) {
598 par[np].str.clear();
599 for (size_t n = 0; n < val.size(); n++) {
600 par[np].str.push_back(val[n]);
601 }
602 par[np].pos = 0;
603 found = true;
604 }
605 }
606 if (found == false) {
607 // cout << "not found in the list... creating" << endl;
608 parameter p;
609 p.data_def = data_def;
610 p.sys_type = sys_type;
611 p.sys_def = sys_def;
612 p.data_id = data_id;
613 p.data_type = "str";
614 p.pos = 0;
615 for (size_t n = 0; n < val.size(); n++) {
616 p.str.push_back(val[n]);
617 }
618 par.push_back(p);
619 // print_parameter_value(par,par[par.size()-1].sys_type,
620 // par[par.size()-1].sys_def, par[par.size()-1].data_id);
621 }
622}
623
624void object::vct_sp(string symb, vector<string> vct) {
625 if (divide_string(symb, '-').size() == 1) {
626 svct_p(p, "prop", sys_type, sys_def, symb, vct);
627 }
628 if (divide_string(symb, '-').size() == 2) {
629 svct_p(p, divide_string(symb, '-')[0], sys_type, sys_def,
630 divide_string(symb, '-')[1], vct);
631 }
632}
633
634void transfer_parameter(string symb, object from, object &to) {
635 to.p.push_back(from.p[from.ip(symb)]);
636}
Definition Parameters.cpp:46
Definition Parameters.cpp:12