-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefineFaults.m
167 lines (125 loc) · 4.45 KB
/
defineFaults.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
function faults = defineFaults(base)
if nargin == 0,
base = loadcase('case30_mod.mat');
end
% base = loadcase('case30_mod.mat');
levels =2;
nBranches = size(base.branch,1);
nBusses = size(base.bus,1);
nGens = size(base.gen,1);
try nTrans = size(base.trans,2); catch nTrans = 0; end
% singleBranchFaults = [5,9,21,35];
% singleBranchFaults = 1:4;
singleBranchFaults = 1:nBranches;
% singleBranchFaults = [1:5, 7:nBranches];
% singleBranchFaults = [1 2 18];
singleBusFaults = 1:nBusses;
% singleBusFaults = [1:5, 7:nBusses];
% singleBusFaults = [12,15,19];
singleGenFaults = 1:nGens;
% singleGenFaults = [1,2];
singleTransFaults = 1:nTrans;
% singleTransFaults = [1];
if ~exist('singleBranchFaults', 'var'), singleBranchFaults = []; end
if ~exist('singleBusFaults', 'var'), singleBusFaults = []; end
if ~exist('singleGenFaults', 'var'), singleGenFaults = []; end
if ~exist('singleTransFaults', 'var'), singleTransFaults = []; end
% [faults, columns] = combineFaults(2, singleBranchFaults, singleBusFaults, singleGenFaults);
[faults, columns] = combineFaults(levels, singleBranchFaults, singleBusFaults, singleGenFaults, singleTransFaults);
fprintf('faults combined\n');
singleFaults = [];
for i = 1:length(faults)
fault = faults{i};
if length(fault.branch) + length(fault.bus) + length(fault.gen) + length(fault.trans) == 1,
singleFaults = [singleFaults fault];
end
end
% fprintf('%30s\t', columns{1})
% for i = 2:length(columns),
% fprintf('%20s\t', columns{i});
% end
% fprintf('\n');
% for i = 1:length(faults),
%
% faults{i}.print();
% end
end
function [faults, columns] = combineFaults(level,varargin)
fprintf('Combining Faults\n')
elements = {'branch', 'bus', 'generator', 'transformer'};
%getting boundaries for x = [ varargin{1}, varargin{2}, varargin{3}]
i = 1;
% delete empty lists from varargin, along with their associated
% elements
while i<= length(varargin),
if isempty(varargin{i}),
varargin = varargin( 1:end ~= i);
elements = elements(1:end ~= i);
else
i = i+1;
end
end
lengths = zeros(length(varargin),1);
for i = 1:length(varargin)
lengths(i) = length(varargin{i});
end
lengths = cumsum(lengths);
offset = [0; lengths(1:end-1)];
ranges = [1+offset, [lengths] ];
% offset = [0; ranges(1:end-1, 2)];
indices = ranges(1,1):ranges(end,2);
columns = ['label', elements];
% faults = cell(1,1);
faults = {};
tic;
for l = 1:level,
% lfaults = {};
combos = nchoosek(indices, l);
fprintf('\tLevel %d: building fault list - %d faults\n', l, length(combos));
ppm = ParforProgMon(sprintf('Building faults for level %d: ',l),size(combos,1), floor(size(combos,1)/100));
parfor index = 1:size(combos,1),
ppm.update(index)
combo = combos(index,:);
[gIndices, mGroups] = groupIndex(combo, offset,ranges);
if l <= 1, label = sprintf('single %s fault', elements{mGroups});
else label = 'combined fault';
end
% label = '';
% for str = elements(mGroups)
% label = sprintf('%s, %s', label, str{1});
% end
% label = sprintf('combined %s fault', label(3:end));
%arguments will be cell array of lists in the form { [list of
%branches], [list of buses], [list of generators], [list of
%transformers]
arguments = cell(length(varargin),1);
for el = 1:length(gIndices),
arguments{mGroups(el)}(end+1) = varargin{mGroups(el)}(gIndices(el));
end
% faults = [faults {}]
faultNum = level * index;
% faults{end+1} = Fault(label, arguments);
faults = [faults {Fault(label, arguments,faultNum)}];
% faults{end}.print()
end
end
pause(0.2); ppm.delete();
fprintf('\tcompleted: %3.2f\n', toc);
end
function groups = grouping(indices, ranges)
% returns grouping in varargin of each index specified in 'indices'
groups = zeros(length(indices), 1);
for index = 1:length(indices),
groups(index) = max(find(sum(indices(index) < ranges,2) <= 1));
end
end
function [gIndices, mGroups] = groupIndex(indices, offset, ranges)
% converts indices of x = [ varargin{1}, varargin{2}, varargin{3}]
% to indices of lists in varargin, accompanied by grouping number
mGroups = grouping(indices,ranges);
gIndices = zeros(length(indices),1);
for index = 1:length(indices)
% gIndices(index) = varargin{mGroups(index)}(indices(index) - offset(mGroups(index)));
gIndices(index) = indices(index) - offset(mGroups(index));
end
end