-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathislanding.py
343 lines (261 loc) · 10.2 KB
/
islanding.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
tinyPDC will connect to pmu_ip:pmu_port and send request
for header message, configuration and eventually
to start sending measurements.
"""
import time
import logging
import numpy as np
import matplotlib.pyplot as plt
from synchrophasor.pdc import Pdc
from synchrophasor.frame import DataFrame, HeaderFrame, \
ConfigFrame1, ConfigFrame2, ConfigFrame3
from multiprocessing import Queue
from dime import DimeClient
h1, = plt.plot([], [], linewidth=6, label='Frequency Deviation')
h2, = plt.plot([], [], linewidth=6, label='Separation Threshold')
mng = plt.get_current_fig_manager()
# mng.full_screen_toggle()
ca = plt.gca()
ca.legend(fontsize=12)
ca.xaxis.set_tick_params(labelsize=12)
ca.yaxis.set_tick_params(labelsize=12)
plt.ion()
plt.show()
plt.pause(0.1)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
fh = logging.FileHandler('/var/log/minipdc.log')
fh.setFormatter(formatter)
logger.addHandler(fh)
dimec = DimeClient('tcp', '192.168.1.20', 5000)
dimec.join("ISLANDING")
ISLANDING = {'vgsvaridx': np.array([1, 2])}
ISLANDING_idx = {'fdev': np.array([1]), 'thresh': np.array([2])}
ISLANDING_vars = {'t': 0, 'vars': np.array([0, 0.4])}
ISLANDING_header = ['fdev_WECC', 'thresh_WECC']
ISLANDING_info = ''
class MiniPDC(object):
"""A MiniPDC connecting to multiple PMUs and a DiME server
"""
def __init__(self, name, protocol, dime_address, ip_list, port_list=None,
dime_port=None,
loglevel=logging.INFO):
self._name = name
self._dime_address = dime_address
self._loglevel = loglevel
self.dimec = DimeClient(protocol, dime_address, dime_port)
self.dimec.join("ISLANDING")
self.ip_list = ip_list
self.port_list = port_list # not being used now
# check if the lengths of `ip_list` and `port_list` match
self.pdc = {}
self.header = {}
self.config = {}
self.last_var = None
# state flags
self.andes_online = False
# self.pdc_started = False
@property
def npmu(self):
return len(self.ip_list)
def initialize(self):
"""
Reset or initialize, it is the same thing
Returns
-------
"""
pass
def sync_and_handle(self):
""" Sync from DiME and handle the received data
"""
self.last_var = self.dimec.sync(1)
if len(self.last_var) == 0:
return None
self.last_var = list(self.last_var)[0]
val = self.dimec.workspace[self.last_var]
if self.last_var == 'DONE' and int(val) == 1:
self.andes_online = False
self.initialize()
pass
return self.last_var
def start_dime(self):
logger.info('Connecting to DiME at {}'.format(self._dime_address))
logger.info('DiME connected')
def init_pdc(self):
for idx, item in enumerate(self.ip_list):
pmu_idx = int(item.split('.')[3])
self.pdc[idx] = Pdc(pdc_id=pmu_idx,
pmu_ip=self.ip_list[idx],
pmu_port=1410)
self.pdc[idx].logger.setLevel("INFO")
logger.info('PDC initialized')
def get_header_config(self):
for idx, item in self.pdc.items(): # each item is a PDC
item.run() # Connect to PMU
self.header[idx] = item.get_header()
self.config[idx] = item.get_config()
for idx, item in self.pdc.items(): # each item is a PDC
item.start() # Request to start sending measurements
self.pdc_started = True
logger.info('PMU Header and ConfigFrame received')
def collect_data(self):
pass
def process_data(self):
pass
def run(self):
pass
class Islanding(MiniPDC):
"""
System islanding class
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.result_queue = []
self.result_dict = {}
self.freq = {}
self.freq_diff = 0
self.freq_diff_array = np.array([0, 0.4])
self.t_array = np.array([0])
self.time_detect = 0
self.detected = False
self.islanded = False
self.islanding_delay = 7
self.event = {'id': [143, 146, 135],
'name': ['Line', 'Line', 'Line'],
'time': [-1, -1, -1],
'duration': [0, 0, 0],
'action': [0, 0, 0]
}
def initialize(self):
super(Islanding, self).initialize()
self.result_queue = [Queue() for x in range(self.npmu)]
self.result_dict = {}
self.freq = {}
self.freq_diff = 0
self.time_detect = 0
self.detected = False
self.islanded = False
def sync_and_handle(self):
super(Islanding, self).sync_and_handle()
if self.last_var == 'SysParam':
val = self.dimec.workspace[self.last_var]
if val is not None:
self.andes_online = True
self.dimec.send_r('andes', ISLANDING=ISLANDING)
# self.dimec.broadcast('ISLANDING_idx', ISLANDING_idx)
# self.dimec.broadcast('ISLANDING_header', ISLANDING_header)
self.initialize()
elif self.last_var == 'Varvgs':
print(self.dimec.workspace['Varvgs']['t'])
return self.last_var
def update_draw(self, xdata, ydata):
# TODO: remove the *2 in xdata
h1.set_data(xdata * 2, ydata[:, 0])
h2.set_data(xdata * 2, ydata[:, 1])
ca.relim()
ca.autoscale_view()
plt.draw()
plt.pause(0.0001)
plt.show()
def run(self):
super(Islanding, self).run()
self.start_dime()
self.initialize()
print('PDC and Islanding running.. Waiting for ANDES')
while True:
sf = self.sync_and_handle()
# only start if ANDES is connected
if self.andes_online is False:
continue
if len(self.config) == 0:
time.sleep(0.5)
self.init_pdc()
self.get_header_config()
# retrieve all measurements from the PDCs
for idx, item in self.pdc.items():
item.get_msg(self.result_queue[idx])
# for each PDC, retrieve the frequency
for idx, item in enumerate(self.result_queue):
self.result_dict[idx] = item.get()
if self.result_dict[idx] is None:
self.freq[idx] = 60
continue
frame = self.result_dict[idx]
if isinstance(frame, HeaderFrame):
self.header[idx] = frame
continue
elif isinstance(frame, (ConfigFrame3, ConfigFrame2, ConfigFrame1)):
self.config[idx] = frame
continue
elif isinstance(frame, DataFrame):
measurements = frame.get_measurements()
if isinstance(measurements, dict):
self.freq[idx] = (
measurements['measurements'][0]['frequency'] - 60) * 1000
# only the data received here goes to processing
else:
print('Unknown measurement type {}, continue'.format(
type(measurements)))
continue
else:
logger.info('ignored {} data'.format(type(frame)))
continue
# detect frequency deviation
if len(self.freq) == 0:
continue
self.freq_diff = max(self.freq.values()) - min(self.freq.values())
if abs(self.freq_diff) < 1:
print('Frequency difference = {}'.format(self.freq_diff))
else:
self.freq_diff = 0
continue
if self.detected is False:
if self.freq_diff >= 0.4:
# record the *initial* time when frequency divergence is detected
self.detected = True
self.time_detect = time.time()
print(
'--> Frequency divergence detected. Islanding will happen in {}s'.format(self.islanding_delay))
# impose a delay before islanding by comparing time() and time_detect
elif self.detected and (not self.islanded):
if time.time() - self.time_detect >= self.islanding_delay:
self.dimec.send_var('andes', Event=self.event)
print('--> Islanding initiated!!!')
self.islanded = True
if sf == 'Varvgs':
self.freq_diff_array = np.vstack((self.freq_diff_array,
np.array([[self.freq_diff, 0.4
]]))
)
self.t_array = np.hstack(
(self.t_array, self.dimec.workspace[sf]['t']))
print("updating draw")
self.update_draw(self.t_array, self.freq_diff_array)
ISLANDING_vars['t'] = self.dimec.workspace[sf]['t']
ISLANDING_vars['vars'][0] = self.freq_diff
# self.dimec.send_var('geovis', 'ISLANDING_vars', ISLANDING_vars)
# print('ISLANDING_vars df={df} sent to geovis at t={t}'.format(df=self.freq_diff,
# t=ISLANDING_vars['t']))
def run():
ip_list = [
# '192.168.1.1',
# '192.168.1.19',
'192.168.1.18',
'192.168.1.19',
# '192.168.1.73',
# '192.168.1.91',
# '192.168.1.109',
# '192.168.1.127',
# '192.168.1.145',
# '192.168.1.163',
]
islanding = Islanding(name='ISLANDING',
protocol='tcp',
dime_address='192.168.1.20',
dime_port=5000,
ip_list=ip_list)
islanding.run()
if __name__ == '__main__':
run()