-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fault.m
183 lines (144 loc) · 5.41 KB
/
Fault.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
classdef Fault
%FAULT Summary of this class goes here
% Detailed explanation goes here
properties
id = 0;
label = '';
branch = [];
bus = [];
gen = [];
trans = [];
end
methods
function fault_obj = Fault(label,args, faultNum)
%label: text description of the object
%args: branch{ list by index}, bus {list by index }, gen { list by index }, transformer {list
%by index}
if nargin > 2,
fault_obj.id = faultNum;
end
if length(args) > 0,
fault_obj.branch = args{1};
end
if length(args)> 1,
fault_obj.bus = args{2};
end
if length(args)>2,
fault_obj.gen = args{3};
end
if length(args)>3, %transformers (not transmission lines)
fault_obj.trans = args{4};
end
fault_obj.label = label;
end
function stringRep = print(obj)
% produce a string represntation of faults - useful for printing
% table of faults
branchS = ['[', sprintf('%d ',obj.branch), ']'];
busS = ['[', sprintf('%d ', obj.bus), ']'];
genS = ['[', sprintf('%d ', obj.gen), ']'];
try transS = [ '[', sprintf('%d ', obj.trans), ']']; catch transS = ''; end
stringRep = sprintf( 'Elements: Br:%20s\tBu:%20s\tGe:%20s\tTr:%20s', branchS, busS, genS,transS);
if nargout == 0,
fprintf(stringRep);
end
end
function outStruct = tostruct(obj)
outStruct.label = obj.label;
outStruct.branch = obj.branch;
outStruct.bus = obj.bus;
outStruct.gen = obj.gen;
outStruct.trans = obj.trans;
end
function outList = tolist(obj)
outList = {};
outList{end+1} = obj.label;
outList{end+1} = obj.branch;
outList{end+1} = obj.bus;
outList{end+1} = obj.gen;
outList{end+1} = obj.trans;
end
function [branch,bus,gen,trans] = consolidate(obj,base)
nBranches = size(base.branch,1);
nGens = size(base.gen,1);
nBusses = size(base.bus,1);
bus = obj.bus;
branch = obj.branch;
gen = obj.gen;
trans = obj.trans;
try nTrans = length(base.trans); catch nTrans = 0; end
%take care of any transformer faults
if ~isempty(trans),
%from all transformer faults, gather up a list of branches,
%busses, and generators involved
tBusses = [];
tBranches = [];
tGens = [];
for transInd = obj.trans,
tBusses = [ tBusses base.trans{transInd}{2}];
tBranchBusses = base.trans{transInd}{1};
%from connecting busses get branch indices
for ind = 1:size(tBranchBusses,1),
tBranches = [tBranches find(tBranchBusses(ind,1) == base.branch(:,1) & tBranchBusses(ind,2) == base.branch(:,2))];
tBranches = [tBranches find(tBranchBusses(ind,2) == base.branch(:,1) & tBranchBusses(ind,1) == base.branch(:,2))];
end
tGens = [tGens base.trans{transInd}{3}];
end
bus = union(bus, tBusses);
branch = union(branch, tBranches);
gen = union(gen, tGens);
end
%take care of bus faults
if ~isempty(bus),
markBranch = zeros(nBranches,1);
markGen = zeros(nGens,1);
busIndices = [];
for mBus = bus(:)',%because of no 'for each in x:', you have to ensure the orientation of array
markBranch = markBranch | (base.branch(:,1) == mBus | base.branch(:,2) == mBus);
% for mBranch = 1:nBranches %mark branch if it connects to a node with id of bus
% markBranch(mBranch) = markBranch(mBranch) || ismember(base.bus(mBus,1), base.branch(mBranch, 1:2));
% end
markGen = markGen | mBus == base.gen(:,1);
% for mGen = 1:nGens
% markGen(mGen) = markGen(mGen) || ismember(base.bus(mBus,1), base.gen(mGen,1));
% end
end
gen = union(gen, find(markGen));
%faultCase.bus = base.bus( setdiff(1:nBusses, busIndices),:);
branch = union( branch, find(markBranch));
end
end
function faultCases = applyto(obj,base, verbose)
if nargin < 3, verbose = false; end
faultCase = base;
nBranches = size(base.branch,1);
nGens = size(base.gen,1);
nBusses = size(base.bus,1);
try nTrans = length(base.trans); catch nTrans = 0; end
[mBranch, mBus, mGen, mTrans] = obj.consolidate(base);
%remove busses from faultCase
if ~isempty(mBus)
faultCase.bus = base.bus( setdiff(1:nBusses, mBus),:); %remove busses
faultCase.bus_geo = base.bus_geo( setdiff(1:nBusses,mBus),:); %remove corresponding geographic entries
end
%take care of any generator faults
if ~isempty(mGen),
faultCase.gen = base.gen( setdiff(1:nGens, mGen),:);
faultCase.gencost = base.gencost( setdiff( 1:nGens, mGen),:);
end
%take care of branches
if ~isempty(mBranch) ,
faultCase.branch = base.branch( setdiff( 1:nBranches, mBranch),:);
faultCase.branch_geo = base.branch_geo( setdiff( 1:nBranches, mBranch));
end
% networks = island.find(faultCase,true,true); %get island groups
networks = island.find(faultCase, verbose, verbose);
if length(networks)>1,
faultCases = island.resolve(faultCase, networks);
else
faultCases = {faultCase};
end
if verbose, mplot.faulted(base, obj); end
end
end
end