← Documentation
ADS-IDAC Engine 5.0.0
TimeConstrainedUnit.cpp
Go to the documentation of this file.
1// TimeConstrainedUnit.cpp: implementation of the TimeConstrainedUnit class.
2//
4
6#include "../ControlPanel/ControlPanel.h"
7#include "../Global/fmodule.h"
8#include "../Scheduler/Scheduler.h"
9#include "../ControlPanel/SystemDisplay.h"
10#include "../ControlPanel/DisplayParameterValue.h"
11#include <boost/serialization/export.hpp>
12#include "../Global/SimulationContext.h"
13#include <boost/archive/binary_oarchive.hpp>
14#include <boost/archive/binary_iarchive.hpp>
15BOOST_CLASS_EXPORT_IMPLEMENT(TimeConstrainedUnit)
16using namespace std;
17
18
20// Construction/Destruction
22
24{
27 Initial_Value = 0.0;
28 Parameter_Value = 0.0;
29 Parameter_Rate = 0.0;
39 decay_tau = 100.0;
40 buildup_tau = 50.0;
41 threshold_exceeded = false;
42}
43
45{
47}
48
72
73void TimeConstrainedUnit::updateRawData(double _Time_Lapse, InfoChunk *_perceived_info){
74 //
75 double _current_time = r5pars_.timesno;
76 int num_history_data_points = 10;
77 MentalParameterValue *mpv_ptr = 0;
78 DisplayParameterValue *dpv_ptr = 0;
79 //
80 if (time_constrained_parameter_history.size() == 0) {
81 for (int i=0; i < num_history_data_points; i++)
82 {
83 if(this->getPtrToCorrespondingIndicator() == 0){
84 this->setPtrToCorrespondingIndicator((SystemDisplay *) g_sim.Indicator.indicators.getDisplayFromMap(this->getCorrespondingIndicatorName()));
85 }
86 if (this->getPtrToCorrespondingIndicator() != 0){
89 }
90 else {
91 cout << "TimeConstrainedUnit::CalculateTimeToThreshold could not find TimeConstrainedUnit corresponding parameter " << this->getCorrespondingIndicatorName() << endl;
92 ofstream fout("err messages.txt", ios::app);
93 fout << r5pars_.timesno << " TimeConstrainedUnit::CalculateTimeToThreshold could not find TimeConstrainedUnit corresponding parameter " << this->getCorrespondingIndicatorName() << '\n';
94 fout.close();
95 }
97 time_constrained_time_history.push_back(r5pars_.timesno + (double) (i - (num_history_data_points-1)) * _Time_Lapse);
98 }
99 }
100 else
101 {
102 mpv_ptr = (*_perceived_info).checkExistanceOfPerceivedParameterValue(this->getCorrespondingIndicatorName());
103 if (mpv_ptr != 0) {
104 Parameter_Value = (*mpv_ptr).getObservedParameterValue();
105 if (mpv_ptr->getTimeAtWhichThisInfoIsPerceived() < this->time_constrained_time_history.at(num_history_data_points -1)) {
106 // no new information has been perceived - base parameter value on last value and perceived rate
107 Parameter_Value = this->time_constrained_parameter_history.at(num_history_data_points - 1)
108 + Parameter_Rate * _Time_Lapse / 60.0;
109 }
110 } else { // if information not perceived, use last data point
111 Parameter_Value = this->time_constrained_parameter_history.at(num_history_data_points-1);
112 }
113
116 time_constrained_time_history.push_back(_current_time);
118 }
119}
120
122{
123 double x1, x2, x21, x22, x3, x4;
124 double upper_limit; // maximum time to threshold
125 SystemDisplay *d_element;
126 d_element = 0;
127// Initialize variables for linear regression calculation of parameter trend
128 x1 = 0.0;
129 x21 = 0.0;
130 x22 = 0.0;
131 x3 = 0.0;
132 x4 = 0.0;
133 upper_limit = 1.0E+06;
134//
135 deque<double>::iterator time_constrained_parameter_history_itr;
136 deque<double>::iterator time_history_itr;
137//
138// calculate parameter trend
139
140 time_history_itr = time_constrained_time_history.begin();
141
142 for(time_constrained_parameter_history_itr = time_constrained_parameter_history.begin();
143 time_constrained_parameter_history_itr != time_constrained_parameter_history.end(); time_constrained_parameter_history_itr++){
144 x1 += (*time_constrained_parameter_history_itr) * (*time_history_itr);
145 x21 += (*time_constrained_parameter_history_itr);
146 x22 += (*time_history_itr);
147 x3 += (*time_history_itr) * (*time_history_itr);
148 time_history_itr++;
149 }
150 x1 *= (double)time_constrained_parameter_history.size();
151 x2 = x21 * x22;
152 x3 *= (double) time_constrained_parameter_history.size();
153 x4 = x22 * x22;
154 if (x3 != x4)
155 {
156 Parameter_Rate = (x1 - x2)/(x3 - x4) * 60.0; // units per minute
157 //
158 // Calculate Time to Normal threshold
160 {
162 {
164 }
165 else
166 {
167 if(Parameter_Rate < 0.0)
168 {
170 }
171 else
172 {
173 Time_To_Normal_Threshold = upper_limit;
174 }
175 }
176 }
177 else
178 {
180 {
182 }
183 else
184 {
185 if(Parameter_Rate > 0.0)
186 {
188 }
189 else
190 {
191 Time_To_Normal_Threshold = upper_limit;
192 }
193 }
194 }
195 // Calculate Time to Accident Threshold
197 {
199 {
201 if (!threshold_exceeded)
202 {
203 threshold_exceeded = true;
205 }
206 }
207 else
208 {
210 {
211 threshold_exceeded = false;
212 }
213 if(Parameter_Rate<0.0)
214 {
216 }
217 else
218 {
219 Time_To_Accident_Threshold = upper_limit;
220 }
221 }
222 }
223 else
224 {
226 {
228 if (!threshold_exceeded)
229 {
230 threshold_exceeded = true;
232 }
233 }
234 else
235 {
237 {
238 threshold_exceeded = false;
239 }
240 if(Parameter_Rate > 0.0)
241 {
243 }
244 else
245 { // trend is away from threshold
246 Time_To_Accident_Threshold = upper_limit;
247 }
248 }
249 }
250 }
251 else
252 { // Parameter Rate is undefined (potential division by zero error) - special handling required
254 {
255 if(Parameter_Value<=Normal_Parameter_Threshold || ((x1-x2) < 0.0) )
256 {
258 }
259 else
260 {
261 Time_To_Normal_Threshold = upper_limit;
262 }
263 }
264 else
265 {
266 if(Parameter_Value >= Normal_Parameter_Threshold || ((x1 - x2) > 0.0) )
267 {
269 }
270 else
271 {
272 Time_To_Normal_Threshold = upper_limit;
273 }
274 }
276 {
277 if(Parameter_Value <= Accident_Parameter_Threshold || ((x1-x2) < 0.0) )
278 {
280 if (!threshold_exceeded)
281 {
282 threshold_exceeded = true;
284 }
285 }
286 else
287 {
288 Time_To_Accident_Threshold= upper_limit;
290 {
291 threshold_exceeded = false;
292 }
293 }
294 }
295 else
296 {
297 if(Parameter_Value >= Accident_Parameter_Threshold || ((x1-x2) > 0.0))
298 {
300 if (!threshold_exceeded)
301 {
302 threshold_exceeded = true;
304 }
305 }
306 else
307 {
308 Time_To_Accident_Threshold = upper_limit;
310 {
311 threshold_exceeded = false;
312 }
313 }
314 }
315 }
316 //
319}
320
321void TimeConstrainedUnit::CalculateTimeConstrainedUnitPIFValue(int _goal, bool _rx_tripped, double _time_lapse)
322{
323 double dt;
324 double new_unit_pif_value = 0.0;
325 if ( _goal == sim::None || _goal== sim::Goal::NormalOp || (_goal == sim::Goal::TroubleShoot && !_rx_tripped) || _goal == sim::Goal::MonitorAbnormal)
326 { // goal is "sim::None", Normal Operation, troubleshooting, or monitoring
328 {
329 new_unit_pif_value = 10.0;
330 }
332 {
333 new_unit_pif_value = 0.0;
334 }
336 {
338 }
339 //
340 // use lag function to determine new PIF value
341 Time_Constrained_Unit_PIF_Value += (new_unit_pif_value - Time_Constrained_Unit_PIF_Value)*(fmin(1.0, (_time_lapse/buildup_tau)));
342 }
343 else if ( _goal == sim::Goal::MaintainGlobalSafetyMargin || (_goal == sim::Goal::TroubleShoot && _rx_tripped))
344 { // goal is other than normal
346 {
347 new_unit_pif_value = 10.0;
348 }
350 {
351 new_unit_pif_value = 0.0;
352 }
354 {
356 }
357 //
358 // if the threshold has been exceeded, allow PIF value to exponentially decay
359 if (threshold_exceeded) {
360 //
361 // allow time constrained load to decay
362 dt = r5pars_.timesno - time_threshold_exceeded;
363 Time_Constrained_Unit_PIF_Value = new_unit_pif_value * pow(2.718,-dt/decay_tau);
364 } else {
365 //
366 // use lag function to determine new PIF value
367 Time_Constrained_Unit_PIF_Value += (new_unit_pif_value - Time_Constrained_Unit_PIF_Value)*(fmin(1.0, (_time_lapse/buildup_tau)));
368 }
369 }
370
371}
372
374{
378}
380{
383 Time_To_Normal_Threshold = _TimeConstrainedUnit.getTimeToNormalThreshold();
386 time_threshold_exceeded = _TimeConstrainedUnit.getTimeThresholdExceeded();
387 threshold_exceeded = _TimeConstrainedUnit.getThresholdExceeded();
388 Initial_Value = _TimeConstrainedUnit.getInitialParameterValue();
389 Parameter_Value = _TimeConstrainedUnit.getParameterValue();
390 Parameter_Rate = _TimeConstrainedUnit.getParameterRate();
393 lower_time_threshold = _TimeConstrainedUnit.getLowerTimeThreshold();
394 upper_time_threshold = _TimeConstrainedUnit.getUpperTimeThreshold();
395 decay_tau = _TimeConstrainedUnit.getDecayTau();
396 buildup_tau = _TimeConstrainedUnit.getBuildupTau();
397 for(int i = 0; i < _TimeConstrainedUnit.time_constrained_parameter_history.size(); i++){
398 this->time_constrained_parameter_history.push_back(_TimeConstrainedUnit.time_constrained_parameter_history.at(i));
399 this->time_constrained_time_history.push_back(_TimeConstrainedUnit.time_constrained_time_history.at(i));
400 }
401}
407 {ptr_to_corresponding_indicator = _display_element;}
409
410
412 int i_threshold_exceeded = 0;
413 double xtemp;
414 if (threshold_exceeded) {i_threshold_exceeded = 1;}
415 deque <double>::iterator d_itr;
416 //
417 // ensure double variables do not go out of range
418 if (Parameter_Value < 1.0E-30 && Parameter_Value > -1.0E-30) {
419 Parameter_Value = 0.0;
420 }
421 if (Parameter_Rate < 1.0E-30 && Parameter_Rate > -1.0E-30) {
422 Parameter_Rate = 0.0;
423 }
424 if (Initial_Value < 1.0E-30 && Initial_Value > -1.0E-30) {
425 Initial_Value = 0.0;
426 }
427 //
428 fout << Normal_Parameter_Threshold << ' '
430 << Initial_Value << ' '
431 << Parameter_Value << ' '
432 << Parameter_Rate << ' '
434 << Time_To_Normal_Threshold << ' '
439 << i_threshold_exceeded << ' '
440 << lower_time_threshold << ' '
441 << upper_time_threshold << ' '
442 << decay_tau << ' '
443 << buildup_tau << '\n';
444 fout << time_constrained_parameter_history.size() << '\n';
445 for(d_itr = time_constrained_parameter_history.begin(); d_itr != time_constrained_parameter_history.end(); d_itr++){
446 xtemp = (*d_itr);
447 if (xtemp < 1.0E-30 && xtemp > -1.0E-30) {
448 xtemp = 0.0;
449 }
450 fout << xtemp << ' ';
451 }
452 fout << '\n';
453 fout << time_constrained_time_history.size() << '\n';
454 for(d_itr = time_constrained_time_history.begin(); d_itr != time_constrained_time_history.end(); d_itr++){
455 xtemp = (*d_itr);
456 if (xtemp < 1.0E-30 && xtemp > -1.0E-30) {
457 xtemp = 0.0;
458 }
459 fout << xtemp << ' ';
460 }
461 fout << '\n';
462}
464 int i, num_obj, i_threshold_exceeded;
465 double t_value;
468 >> Initial_Value
477 >> i_threshold_exceeded
480 >> decay_tau
481 >> buildup_tau;
482 if (i_threshold_exceeded == 0) {
483 threshold_exceeded = false;}
484 else {
485 threshold_exceeded = true;
486 }
487 fin >> num_obj;
488 for(i = 0; i < num_obj; i++){
489 fin >> t_value;
490 time_constrained_parameter_history.push_back(t_value);
491 }
492 fin >> num_obj;
493 for(i = 0; i < num_obj; i++){
494 fin >> t_value;
495 time_constrained_time_history.push_back(t_value);
496 }
498}
499
500
506 Initial_Value = 0.0;
507 Parameter_Value = 0.0;
508 Parameter_Rate = 0.0;
514}
SimulationContext g_sim
Global simulation context: the single instance, defined in SimulationContext.cpp.
SystemDisplay * getDisplayFromMap(std::string)
ControlPanelDisplay indicators
Definition: ControlPanel.h:53
double getTimeAtWhichThisInfoIsPerceived()
Definition: MentalInfo.cpp:27
std::deque< double > time_constrained_time_history
void setLowerTimeThreshold(double)
void retrieveTimeConstrainedUnitFromFile(std::ifstream &)
void setTimeThresholdExceeded(double)
std::deque< double > time_constrained_parameter_history
void copyTimeConstrainedUnit(TimeConstrainedUnit &)
void setAccidentParameterThreshold(double)
void CalculateTimeConstrainedUnitPIFValue(int, bool, double)
SystemDisplay * getPtrToCorrespondingIndicator()
void setNormalParameterThreshold(double)
void setCorrespondingIndicatorName(std::string)
SystemDisplay * ptr_to_corresponding_indicator
void setTimeConstrainedUnitName(std::string)
std::string Corresponding_Indicator_Name
void saveTimeConstrainedUnitToFile(std::ofstream &)
void setPtrToCorrespondingIndicator(SystemDisplay *)
void setUpperTimeThreshold(double)
std::string getCorrespondingIndicatorName()
std::string getTimeConstrainedUnitName()
void updateRawData(double, InfoChunk *)
std::string Time_Constrained_Unit_Name
void readInfoFromFile(std::ifstream &)
struct @0 r5pars_
constexpr int MonitorAbnormal
Definition: SimConstants.h:161
constexpr int NormalOp
Definition: SimConstants.h:158
constexpr int MaintainGlobalSafetyMargin
Definition: SimConstants.h:160
constexpr int TroubleShoot
Definition: SimConstants.h:159
constexpr int None
Definition: SimConstants.h:75
ControlPanel Indicator
Control room panel state.