Skip to content

Commit 8a6080d

Browse files
author
Hugo Nie
authored
update faultTreeUtils.C Ref idaholab#126
1 parent 0c8e507 commit 8a6080d

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

src/utils/FaultTreeUtils.C

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ FTAUtils::FaultTree::FaultTree(std::string file_name, std::string root)
88
FTAUtils::Parser parser = FTAUtils::Parser(file_name, FTAUtils::Parser::FORMAT_CSV);
99
buildTree(parser);
1010

11+
// Override root node if not default
1112
if (root != "")
1213
{
14+
// ASSERT
1315
if (!(getNode(root)))
1416
{
1517
fprintf(stderr,
@@ -34,16 +36,21 @@ FTAUtils::FaultTree::FaultTree(set_string & sets_link, std::map<std::string, ft_
3436
std::string root = "";
3537
_node_d_b = _node_base;
3638

39+
// Clearing up sets vector for safety
3740
rmSets();
3841

42+
// Add root to set
3943
std::set<std::string> root_set, sub_sets;
40-
root_set.insert("TOP");
44+
root_set.insert("TOP"); // "TOP", size: 1
4145

4246
_sets.insert(root_set);
4347

48+
// Start with root node to expand on heirarchy
4449
cutSetsExpand(getNode("TOP"));
4550

46-
51+
// Check if first row is empty
52+
// NOTE: Since its std::set, only first row could
53+
// be empty
4754
if (_sets.begin()->size() == 0)
4855
{
4956
_sets.erase(_sets.begin());
@@ -64,17 +71,22 @@ FTAUtils::FaultTree::buildTree(FTAUtils::Parser parser)
6471
{
6572
line = parser.yieldLine();
6673

74+
// Stop if no new line to process
6775
if (line.size() == 0)
6876
break;
6977

78+
// Stash name, operator
7079
_node * node = new _node(line[0], str2Operator(line[1]));
7180

81+
// Add children
7282
for (int i = 2; i < line.size(); i++)
7383
node->_child.push_back(line[i]);
7484

85+
// Stash the first entry as ROOT of tree
7586
if (_node_d_b.size() == 0)
7687
_root = line[0];
7788

89+
// Add the newly created node to node lookup hashmap
7890
_node_d_b[line[0]] = node;
7991
}
8092

@@ -85,6 +97,7 @@ FTAUtils::FaultTree::_operator_t
8597
FTAUtils::FaultTree::str2Operator(std::string op)
8698
{
8799
std::string op_s = FTAUtils::str2Upper(op, true);
100+
// ASSERT
88101
if (_opDict.count(op_s) == 0)
89102
{
90103
fprintf(stderr,
@@ -101,16 +114,21 @@ FTAUtils::FaultTree::str2Operator(std::string op)
101114
set_string
102115
FTAUtils::FaultTree::computeMinimumCutSets()
103116
{
117+
// Clearing up sets vector for safety
104118
rmSets();
105119

120+
// Add root to set
106121
std::set<std::string> root_set, sub_sets;
107122
root_set.insert(getRoot());
108123

109124
_sets.insert(root_set);
110125

126+
// Start with root node to expand on heirarchy
111127
cutSetsExpand(getNode(getRoot()));
112128

113-
129+
// Check if first row is empty
130+
// NOTE: Since its std::set, only first row could
131+
// be empty
114132
if (_sets.begin()->size() == 0)
115133
{
116134
_sets.erase(_sets.begin());
@@ -136,8 +154,10 @@ FTAUtils::FaultTree::removeSubsets()
136154
includes(row2_it->begin(), row2_it->end(), row1_it->begin(), row1_it->end());
137155
bool row2_is_subset =
138156
includes(row1_it->begin(), row1_it->end(), row2_it->begin(), row2_it->end());
157+
// Remove row1 if row2 is a subset of row1 by marking it
139158
if (row2_is_subset)
140159
rm_its.insert(index1);
160+
// Remove row2 if row1 is a subset of row1 by marking it
141161
else if (row1_is_subset)
142162
rm_its.insert(index2);
143163
index2++;
@@ -146,6 +166,7 @@ FTAUtils::FaultTree::removeSubsets()
146166
}
147167

148168
uint64_t index = 0;
169+
// Remove rows marked to be removed
149170
for (std::set<uint64_t>::iterator it = rm_its.begin(); it != rm_its.end(); ++it)
150171
{
151172
_sets.erase(next(_sets.begin(), *it - index));
@@ -156,6 +177,7 @@ FTAUtils::FaultTree::removeSubsets()
156177
void
157178
FTAUtils::FaultTree::cutSetsExpand(_node * node)
158179
{
180+
// ASSERT
159181
if (!node)
160182
{
161183
fprintf(stderr,
@@ -166,6 +188,7 @@ FTAUtils::FaultTree::cutSetsExpand(_node * node)
166188
abort();
167189
}
168190

191+
// ASSERT
169192
if (node->_child.size() == 0)
170193
{
171194
fprintf(stderr,
@@ -177,40 +200,50 @@ FTAUtils::FaultTree::cutSetsExpand(_node * node)
177200
abort();
178201
}
179202

180-
203+
// New rows which have to be appended at end. Adding them might
204+
// result in data hazards
181205
set_string mk_rows;
182206

207+
// 1. Iterate through entire list and match for own node's name
183208
for (set_string::iterator row_it = _sets.begin(); row_it != _sets.end(); ++row_it)
184209
{
185210
std::set<std::string> row(*row_it);
211+
// Search for the element in row
186212
std::set<std::string>::iterator it = find(row.begin(), row.end(), node->_name);
187213
if (it != row.end())
188214
{
215+
// Erase self to add children
189216
row.erase(it);
190217

191-
218+
// 2. Replace self with children based on operation
219+
// (i) . Replace self with children in same row if AND
220+
// (ii). Replace self with child one per row if OR
192221
switch (node->_op)
193222
{
194223
case AND:
224+
// Append all children in same row
195225
for (uint64_t c_id = 0; c_id < node->_child.size(); c_id++)
196226
{
197227
row.insert(node->_child[c_id]);
198228
}
199229
break;
200230

201231
case OR:
232+
// Replicate line and append one child per row
202233
for (uint64_t c_id = 0; c_id < node->_child.size(); c_id++)
203234
{
204235
std::set<std::string> row_set(row);
205236
row_set.insert(node->_child[c_id]);
206237
mk_rows.insert(row_set);
207238
}
208239

240+
// Clear this row to be removed at end (postprocessing)
209241
row.clear();
210242
break;
211243

212244
default:
213245
{
246+
// ASSERT
214247
fprintf(stderr,
215248
"[ASSERT] In File: %s, Line: %d => "
216249
"Unknown Operator found!.\n",
@@ -222,17 +255,22 @@ FTAUtils::FaultTree::cutSetsExpand(_node * node)
222255
}
223256
}
224257

225-
258+
// The following 2 lines might result in an empty row to be pushed.
259+
// If we gate insertion with count, we might have a data hazard due
260+
// to wrong increment of iterators.
261+
// A quick fix to this is to check for first row and delete it at end
226262
_sets.erase(row_it);
227263
_sets.insert(row);
228264
}
229265

266+
// Add newly created rows
230267
for (set_string::iterator it = mk_rows.begin(); it != mk_rows.end(); ++it)
231268
{
232269
std::set<std::string> row(*it);
233270
_sets.insert(*it);
234271
}
235272

273+
// 3. Recurse on each non leaf child
236274
for (uint64_t c_id = 0; c_id < node->_child.size(); c_id++)
237275
{
238276
_node * child = getNode(node->_child[c_id]);
@@ -270,6 +308,7 @@ FTAUtils::FaultTree::getCutSets()
270308

271309
FTAUtils::Parser::Parser(std::string fileName, FTAUtils::Parser::parseFormatT format)
272310
{
311+
// Assertion on supported parsing formats
273312
if (format != FORMAT_CSV)
274313
{
275314
fprintf(stderr,
@@ -298,6 +337,7 @@ FTAUtils::Parser::yieldLines()
298337
{
299338
line = FTAUtils::Parser::yieldLine();
300339

340+
// Stop if no new line to process
301341
if (line.size() == 0)
302342
break;
303343
lines.push_back(line);
@@ -308,6 +348,7 @@ FTAUtils::Parser::yieldLines()
308348
std::vector<std::string>
309349
FTAUtils::Parser::yieldLine()
310350
{
351+
// ASSERT
311352
if (!(fileP->is_open()))
312353
{
313354
fprintf(stderr,
@@ -321,6 +362,7 @@ FTAUtils::Parser::yieldLine()
321362
std::string buffer;
322363
std::vector<std::string> line;
323364

365+
// Get a line that has something except \n
324366
if (getline(*fileP, buffer))
325367
{
326368
std::string token;
@@ -364,8 +406,10 @@ FTAUtils::interpolate(vector_double data, double x, bool extrapolate)
364406
{
365407
int size = data.size();
366408

409+
// find left end of interval for interpolation
367410
int i = 0;
368411

412+
// special case: beyond right end
369413
if (x >= data[size - 2][0])
370414
{
371415
i = size - 2;
@@ -376,8 +420,10 @@ FTAUtils::interpolate(vector_double data, double x, bool extrapolate)
376420
i++;
377421
}
378422

423+
// points on either side (unless beyond ends)
379424
double xL = data[i][0], yL = data[i][1], xR = data[i + 1][0], yR = data[i + 1][1];
380425

426+
// if beyond ends of array and not extrapolating
381427
if (!extrapolate)
382428
{
383429
if (x < xL)
@@ -386,8 +432,10 @@ FTAUtils::interpolate(vector_double data, double x, bool extrapolate)
386432
yL = yR;
387433
}
388434

435+
// gradient
389436
double dydx = (yR - yL) / (xR - xL);
390437

438+
// linear interpolation
391439
return yL + dydx * (x - xL);
392440
}
393441

0 commit comments

Comments
 (0)