← Documentation
ADS-IDAC Engine 5.0.0
KeyParameterHistory.cpp
Go to the documentation of this file.
1// KeyParameterHistory.cpp: implementation of the KeyParameterHistory class.
2//
4#if defined(_WIN32)
5#include <direct.h>
6#include <boost/serialization/export.hpp>
7#else
8#include <sys/stat.h>
9#include <sys/types.h>
10#endif
11
12#include "KeyParameterHistory.h"
13#include "../Branch/BranchRecord.h"
14#include "../Scheduler/Scheduler.h"
15#include <filesystem>
16#include "SimulationContext.h"
17#include "CheckedIO.h"
18#include <boost/archive/binary_oarchive.hpp>
19#include <boost/archive/binary_iarchive.hpp>
20BOOST_CLASS_EXPORT_IMPLEMENT(KeyParameterHistory)
21using namespace std;
22namespace fs = std::filesystem;
23
24
26// Construction/Destruction
28
30{
31 this->m_timer = 0;
32}
33
35{
37}
38
39void KeyParameterHistory::addDataSet(){ //Modified by Yuandan
40 if (this->m_timer < 1)
41 {
42 auto kpu_pt = make_unique<KeyParameterHistoryUnit>();
43 kpu_pt->addDataSet();
44 kps_history.push_back(std::move(kpu_pt));
45 this->m_timer = 2;
46 }
47 else
48 {this->m_timer = this->m_timer -1;}
49
50}
51
52void KeyParameterHistory::outputHistoryToFile(int iESID, string strKPSDir)
53{
54 if (kps_history.empty()) return;
55 string full_file_name, full_file_name_diagnosis, full_file_name_PIF;
56 int num_subfolder, size_of_subfolder, es_id;
57 int num_column, num_row, num_column_diagnosis, num_column_PIF;
58 decltype(kps_history)::iterator kps_history_itr;
59//
60 es_id = iESID;
61
62 size_of_subfolder = g_sim.Branch_center.getSizeOfSubFolder();
63 full_file_name = strKPSDir;
64 full_file_name += "KPSFolder_";
65 num_subfolder = es_id / size_of_subfolder;
66 // itoa(num_subfolder, numtext, 10);
67 full_file_name += to_string(num_subfolder);
68 fs::create_directories(full_file_name);
69 full_file_name_diagnosis = full_file_name;
70 full_file_name_PIF= full_file_name;
71
72 full_file_name += "/ZKPS_";
73 full_file_name_diagnosis += "/ZDIA_";
74 full_file_name_PIF += "/ZPIF_";
75
76
77 // itoa(es_id, numtext, 10);
78 full_file_name += to_string(es_id);
79 full_file_name += ".txt";
80 full_file_name_diagnosis += to_string(es_id);
81 full_file_name_diagnosis += ".txt";
82 full_file_name_PIF += to_string(es_id);
83 full_file_name_PIF += ".txt";
84
85
86 kps_history_itr = kps_history.begin();
87 num_column = (*kps_history_itr)->getNumOfData();
88 num_column_diagnosis = (*kps_history_itr)->getDiagnosisNumOfData();
89 num_column_PIF = (*kps_history_itr)->getPIFNumOfData();
90 num_row = kps_history.size() + 2;
91 //
92 // output keyparameter data
93 ofstream fout(full_file_name.c_str(), ios::out);
94 // output column and row
95 fout << num_column << '\t' << num_row << '\n';
96 (*kps_history_itr)->outputLabelToFile(fout);
97 (*kps_history_itr)->outputUnitToFile(fout);
98 for(kps_history_itr = this->kps_history.begin(); kps_history_itr != this->kps_history.end(); kps_history_itr++){
99 (*kps_history_itr)->outputAllDataToFile(fout);
100 }
101 checked_close(fout, full_file_name);
102 //
103 // output diagnosis data
104 ofstream fout1(full_file_name_diagnosis.c_str(), ios::out);
105 fout1 << num_column_diagnosis << '\t' << num_row << '\n';
106 // the data loop above left the iterator at end() — re-anchor before the
107 // label/unit rows (dereferencing end() read one slot past the buffer)
108 kps_history_itr = this->kps_history.begin();
109 (*kps_history_itr)->outputDiagnosisLabelToFile(fout1);
110 (*kps_history_itr)->outputDiagnosisUnitToFile(fout1);
111 for(kps_history_itr = this->kps_history.begin(); kps_history_itr != this->kps_history.end(); kps_history_itr++){
112 (*kps_history_itr)->outputAllDiagnosisDataToFile(fout1);
113 }
114 checked_close(fout1, full_file_name_diagnosis);
115 //
116 // output PIF (SPDS & Time Constrained) data
117 ofstream fout2(full_file_name_PIF.c_str(), ios::out);
118 fout2 << num_column_PIF << '\t' << num_row << '\n';
119 kps_history_itr = this->kps_history.begin();
120 (*kps_history_itr)->outputPIFLabelToFile(fout2);
121 (*kps_history_itr)->outputPIFUnitToFile(fout2);
122 for(kps_history_itr = this->kps_history.begin(); kps_history_itr != this->kps_history.end(); kps_history_itr++){
123 (*kps_history_itr)->outputAllPIFDataToFile(fout2);
124 }
125 checked_close(fout2, full_file_name_PIF);
126}
127
129 if (kps_history.empty()) return;
130 string full_file_name;
131
133 full_file_name = g_sim.SScheduler.getKPSDirectory();
134
135 outputHistoryToFile(es_id, full_file_name);
136}
138 kps_history.clear();
139}
141 this->kps_history.reserve(this->kps_history.size() + kph.kps_history.size());
142 for(auto& src : kph.kps_history){
143 auto ptr = make_unique<KeyParameterHistoryUnit>();
144 ptr->copyKeyParameterHistoryUnit(*src);
145 this->kps_history.push_back(std::move(ptr));
146 }
147}
149 int i, num;
150 num = kps_history.size();
151 fout << "KeyParameterHistory\n" << num << '\n';
152 for(i = 0; i < num; i++){
153 kps_history.at(i)->saveKeyParameterHistoryUnitToFile(fout);
154 }
155}
157 int i, num;
158 string str;
159 fin >> str >> num;
160 if (str != "KeyParameterHistory"){
161 ofstream fout("err messages.txt", ios::app);
162 cout << "KeyParameterHistory::retrieveKeyParameterHistoryFromFile retrieval error in previous function" << endl;
163 fout << r5pars_.timesno << " KeyParameterHistory::retrieveKeyParameterHistoryFromFile retrieval error in previous function\n";
164 fout.close();
165 r5pars_.sim_err = true;
166 g_sim.Branch_center.setTempESMessage("KeyParameterHistory::retrieveKeyParameterHistoryFromFile Prior error");
168 return;
169 }
170 for(i = 0; i < num; i++){
171 auto ptr = make_unique<KeyParameterHistoryUnit>();
172 ptr->retrieveKeyParameterHistoryUnitFromFile(fin);
173 kps_history.push_back(std::move(ptr));
174 }
175}
bool checked_close(std::ofstream &ofs, const std::string &path)
Flush and close an output stream and report a failed write.
Definition: CheckedIO.h:19
SimulationContext g_sim
Global simulation context: the single instance, defined in SimulationContext.cpp.
Single struct holding all mutable simulation state.
void setTempTypeOfConsequence(Consequence)
Stores the consequence of the end state the running sequence will get.
int getSizeOfSubFolder()
int getIDOfRecentGeneratedES()
void setTempESMessage(std::string)
Stores the description of the end state the running sequence will get if it is terminated (set by mod...
std::vector< std::unique_ptr< KeyParameterHistoryUnit > > kps_history
void saveKeyParameterHistoryToFile(std::ofstream &)
void copyKeyParameterHistory(KeyParameterHistory &)
void retrieveKeyParameterHistoryFromFile(std::ifstream &)
std::string getKPSDirectory()
Definition: Scheduler.cpp:607
struct @0 r5pars_
Scheduler SScheduler
Simulation scheduling, parses CLI options.
BranchRecord Branch_center
Dynamic event tree state.