-
Notifications
You must be signed in to change notification settings - Fork 1
/
Switch.m
136 lines (123 loc) · 4.63 KB
/
Switch.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
classdef Switch < handle & dynamicprops
%
% Switch class is implemented for the PowerSystem Package and it's
% used for all switches on the circuit.
%
% Its constructor is defined as following:
%
% Switch(busK, busM, status)
% busK: defines first bus connection from the switch
% busM: defines second bus connection from the switch
% status: defines switch initial status. It must be a SwitchStatus object: SwitchStatus.Open/Closed (default Open)
%
properties(GetAccess = public, SetAccess = private) % Only Switch can set these properties, but everyone can read them
busK = 0; % bus1: System first Switch connection
busM = 0; % bus2: System second Switch connection
status = SwitchStatus.Open; % status: The switch status
end
properties(Access = private)
timedChanges % timedChanges: Switch changing Times
end
properties(Access = private) % Only Source and it childs can set and see these properties
ps; % PowerSystem pointer
end
events
NewPosition
end
methods
function sw = Switch(ps, busK, busM, status)
if nargin > 0
sw.ps = ps;
if nargin == 3
status = repmat(SwitchStatus.Open,sw.ps.topology,1);
end
sw.busK = busK;
sw.busM = busM;
if isa(sw.status,'SwitchStatus')
if ( size(status,1) == 1)
sw.status = repmat(status,sw.ps.topology,1);
else
sw.status = status;
end
else
error('PowerSystemPkg:Switch', 'status must be a SwitchStatus object: SwitchStatus.Open/Closed');
end
end
end % Constructor
function bool = isOpen(sw,phase)
% function bool = isOpen(sw,phase)
%
% This function returns true if the switch is open and false otherwise.
%
if nargin == 1
phase = 1:sw.ps.topology;
end
bool = (SwitchStatus.Open == sw.status(phase));
end % function isOpen
function bool = isClosed(sw,phase)
% function bool = isClosed(sw,phase)
%
% This function returns true if the switch is closed and false otherwise.
%
if nargin == 1
phase = 1:sw.ps.topology;
end
bool = (SwitchStatus.Closed == sw.status(phase));
end % function isClosed
function changePosition(sw,position,phase)
% function changePosition(sw,position,phase)
%
% This function changes the switch position into input position.
%
% function changePosition(sw)
% Another way to use the changePosition function is not to specify the position. By using this way you just force the switch to changes it position.
%
if nargin < 3
phase = 1:sw.ps.topology;
end
if nargin < 2
if (sw.status(phase) == SwitchStatus.Open)
sw.status(phase) = repmat(SwitchStatus.Closed,sw.ps.topology,1);
else
sw.status(phase) = repmat(SwitchStatus.Open,sw.ps.topology,1);
end
notify(sw,'NewPosition');
elseif nargin > 1
if(isa(position,'SwitchStatus'))
if (sw.status(phase) ~= position)
sw.status(phase) = repmat(position,sw.ps.topology,1);
notify(sw,'NewPosition');
end
else
error('PowerSystemPkg:Switch','Position must be a SwitchStatus object (SwitchStatus.Open/Closed).');
end
end
end % function changePosition
function addTimedChange(sw, time)
if sw.ps.currentTime < time % We will change the time only if it hasnt passed
sw.timedChanges = [time, sw.timedChanges];
[sw.timedChanges, idx] = sort(sw.timedChanges);
if (isempty(sw.findprop('timeListener'))) % We need to add a listener to the PowerSystem time
sw.addprop('timeListener'); % add a property so that we can reach the listener and delete it when it is not necessary anymore
sw.timeListener = addlistener(sw.ps,'currentTime','PostSet',@sw.changeOnTime);
end
else
display(sprintf('ATTEMPTED TO SET A SWITCH CHANGE ON SYSTEM PAST TIME %f, POWER SYSTEM CURRENT TIME IS %f', time, ps.currentTime));
end
end % addTimedChange
end % methods
methods (Access = private)
function changeOnTime(sw, src, evt)
if ( sw.timedChanges(1) <= sw.ps.currentTime)
sw.changePosition
if (length(sw.timedChanges)>1) % Is there more timed changes?
sw.timedChanges = sw.timedChanges(2:end); % Update them
else % otherwise
sw.timedChanges = []; % clear timedChanges
delete(sw.timeListener); % delete listener
delete(sw.findprop('timeListener')); % delete listener property
end
end
end
end % private methods
end