← Documentation
ADS-IDAC Engine 5.0.0
SPDS.cpp
Go to the documentation of this file.
1// SPDS.cpp: implementation of the SPDS class.
2//
4
5#include "SPDS.h"
6#include "../Global/fmodule.h"
7#include "../Scheduler/Scheduler.h"
8#include "../Branch/BranchRecord.h"
9#include <boost/serialization/export.hpp>
10#include "../Global/SimulationContext.h"
11#include <boost/archive/binary_oarchive.hpp>
12#include <boost/archive/binary_iarchive.hpp>
13BOOST_CLASS_EXPORT_IMPLEMENT(SPDS)
14using namespace std;
15
16
17
19// Construction/Destruction
21
23{
24}
25
27{
28 this->clearSPDS();
29}
31 spds_input_array.clear();
32 spds_history.clear();
33 time.clear();
34}
36 //currently 10 data points are used to calculate the value of SPDS_rate.
37 int i;
38 this->readInfoFromFile(_str);
39 for(i = 0; i < 10; i++){
40 spds_history.push_back(0.0);
41 }
42 for(i = 0; i < 10; i++){
43 time.push_back(r5pars_.timesno + (double) (i - 10) * time_lapse_for_update);
44 }
46}
47void SPDS::updateAllSPDSUnitValues(InfoChunk * _perceived_info_ptr){
48 for(auto& elem : spds_input_array){
49 elem->calculateSPDSUnitValue(_perceived_info_ptr);
50 }
51}
52
57void SPDS::setSPDSValue(double x){SPDS_value = x;}
58void SPDS::SetSPDSRate(double x){SPDS_rate = x;}
59
60void SPDS::copySPDS(SPDS &_spds){
61 int i;
62 for(auto& src : _spds.spds_input_array){
63 auto pt = std::make_unique<SPDSUnit>();
64 pt->copySPDSUnit(*src);
65 spds_input_array.push_back(std::move(pt));
66 }
67 for(i = 0; i < _spds.spds_history.size(); i++){
68 this->spds_history.push_back(_spds.spds_history.at(i));
69 }
70 for(i = 0; i < _spds.time.size(); i++){
71 this->time.push_back(_spds.time.at(i));
72 }
74 SPDS_value = _spds.getSPDSValue();
75 SPDS_rate = _spds.getSPDSRate();
77}
78
79void SPDS::readInfoFromFile(string _str){
80 string str, file_name;
81 int i, num_spds_unit;
82 ifstream fin(_str.c_str(), ios::in);
83 if(!fin.is_open()){
84 cout << "File: " << file_name << " cound not be found" << endl;
85 ofstream fout("err messages.txt", ios::app);
86 fout << r5pars_.timesno << " File: " << file_name << " cound not be found\n";
87 fout.close();
88 r5pars_.sim_err = true;
89 g_sim.Branch_center.setTempESMessage("SPDS::readInfoFromFile err.");
91 }
92 else {
93 fin >> str >> num_spds_unit >> str >> time_lapse_for_update;
94 for (i = 0; i < num_spds_unit; i++){
95 auto pt = std::make_unique<SPDSUnit>();
96 pt->readInfoFromFile(fin);
97 spds_input_array.push_back(std::move(pt));
98 }
99 fin.close();
100 }
101}
102
105
106void SPDS::updateAllSPDSValueAndRate(InfoChunk * _perceived_info_ptr){
107 double total_weight, total_weighted_sum;
108 double x1, x2, x21, x22, x3, x4;
109 total_weight = 0.0;
110 total_weighted_sum = 0.0;
111 x1 = 0.0;
112 x21 = 0.0;
113 x22 = 0.0;
114 x3 = 0.0;
115 x4 = 0.0;
116 deque<double>::iterator spds_history_itr;
117 deque<double>::iterator time_itr;
119 last_update_time = r5pars_.timesno;
120 this->updateAllSPDSUnitValues(_perceived_info_ptr);
121 //calculate SPDS_value
122 for(auto& elem : spds_input_array){
123 total_weight += elem->getSPDSUnitWeight();
124 total_weighted_sum += elem->getSPDSUnitWeight() * elem->getSPDSUnitValue();
125 }
126 SPDS_value = total_weighted_sum/total_weight;
127 // ensure SPDS_value is in the range of 0.0 - 10.0
128 SPDS_value = fmax(0.0, SPDS_value);
129 SPDS_value = fmin(10.0,SPDS_value);
130 //calculate SPDS_rate
131 spds_history.push_back(SPDS_value);
132 spds_history.pop_front();
133 time.push_back(last_update_time);
134 time.pop_front();
135 //
136
137 time_itr = time.begin();
138 for(spds_history_itr = spds_history.begin(); spds_history_itr != spds_history.end(); spds_history_itr++){
139 x1 += (*spds_history_itr) * (*time_itr);
140 x21 += (*spds_history_itr);
141 x22 += (*time_itr);
142 x3 += (*time_itr) * (*time_itr);
143 time_itr++;
144 }
145 x1 *= (double) spds_history.size();
146 x2 = x21 * x22;
147 x3 *= (double) spds_history.size();
148 x4 = x22 * x22;
149 if (x3 != x4){
150 // find spds_rate from linear regression slope formula (units are % of full scale per minute)
151 // range for spds value is 0 to 10
152 SPDS_rate = (x1 - x2)/(x3 - x4) * 60.0 * (100. /10.);//per minute
153 }
154 else{
155 SPDS_rate = 0.0;
156 }
157 }
158}
159void SPDS::outputSPDSLabelToFile(string str, ofstream &fout){
160 int i;
161 string str1;
162 fout << "\t" << str+ "SPDS_Value"<< "\t" << str + "SPDS_Rate";
163 for(i = 0; i < spds_input_array.size(); i++){
164 str1 = str + spds_input_array.at(i)->getSPDSUnitName();
165 fout << '\t' << str1;;
166 }
167}
168void SPDS::outputSPDSUnitToFile(ofstream &fout){
169 int i;
170 fout << "\t Range:_0-10\t%_Full_Scale_per_minute";
171 for(i = 0; i < spds_input_array.size(); i++){
172 fout << "\t--";
173 }
174}
175
176void SPDS::saveSPDSToFile(ofstream &fout){
177 fout << "SPDS\n"
178 << time_lapse_for_update << ' '
179 << SPDS_value << ' '
180 << SPDS_rate << ' '
181 << last_update_time << '\n';
182 fout << this->spds_input_array.size() << '\n';
183 for(auto& elem : spds_input_array){
184 elem->saveSPDSUnitToFile(fout);
185 }
186 fout << this->spds_history.size() << '\n';
187 for(auto& val : spds_history){
188 fout << val << ' ';
189 }
190 fout << '\n';
191 fout << this->time.size() << '\n';
192 for(auto& val : time){
193 fout << val << ' ';
194 }
195 fout << '\n';
196}
197void SPDS::retrieveSPDSFromFile(ifstream &fin){
198 int i, num_obj;
199 double t_value;
200 string str;
201 fin >> str
203 >> SPDS_value
204 >> SPDS_rate
206 fin >> num_obj;
207 for(i = 0; i < num_obj; i++){
208 auto pt = std::make_unique<SPDSUnit>();
209 pt->retrieveSPDSUnitFromFile(fin);
210 this->spds_input_array.push_back(std::move(pt));
211 }
212 fin >> num_obj;
213 for(i = 0; i < num_obj; i++){
214 fin >> t_value;
215 this->spds_history.push_back(t_value);
216 }
217 fin >> num_obj;
218 for(i = 0; i < num_obj; i++){
219 fin >> t_value;
220 this->time.push_back(t_value);
221 }
222}
SimulationContext g_sim
Global simulation context: the single instance, defined in SimulationContext.cpp.
void setTempTypeOfConsequence(Consequence)
Stores the consequence of the end state the running sequence will get.
void setTempESMessage(std::string)
Stores the description of the end state the running sequence will get if it is terminated (set by mod...
Definition: SPDS.h:21
void setSPDSValue(double)
Definition: SPDS.cpp:57
double getSPDSRate()
Definition: SPDS.cpp:56
void copySPDS(SPDS &)
Definition: SPDS.cpp:60
void outputSPDSUnitToFile(std::ofstream &)
Definition: SPDS.cpp:168
virtual ~SPDS()
Definition: SPDS.cpp:26
double getSPDSValue()
Definition: SPDS.cpp:55
void retrieveSPDSFromFile(std::ifstream &)
Definition: SPDS.cpp:197
double last_update_time
Definition: SPDS.h:48
void saveSPDSToFile(std::ofstream &)
Definition: SPDS.cpp:176
double getTimeLapseForUpdate()
Definition: SPDS.cpp:54
void initiationForSimulation(std::string)
Definition: SPDS.cpp:35
void outputSPDSLabelToFile(std::string, std::ofstream &)
Definition: SPDS.cpp:159
void updateAllSPDSValueAndRate(InfoChunk *)
Definition: SPDS.cpp:106
void clearSPDS()
Definition: SPDS.cpp:30
void setTimeLapseForUpdate(double)
Definition: SPDS.cpp:53
std::deque< double > time
Definition: SPDS.h:45
SPDS()
Definition: SPDS.cpp:22
void SetSPDSRate(double)
Definition: SPDS.cpp:58
std::deque< double > spds_history
Definition: SPDS.h:44
void updateAllSPDSUnitValues(InfoChunk *)
Definition: SPDS.cpp:47
double SPDS_value
Definition: SPDS.h:48
void setLastUpdateTime(double)
Definition: SPDS.cpp:104
void readInfoFromFile(std::string)
Definition: SPDS.cpp:79
std::vector< std::unique_ptr< SPDSUnit > > spds_input_array
Definition: SPDS.h:43
double SPDS_rate
Definition: SPDS.h:48
double getLastUpdateTime()
Definition: SPDS.cpp:103
double time_lapse_for_update
Definition: SPDS.h:48
struct @0 r5pars_
BranchRecord Branch_center
Dynamic event tree state.