/
sample_generator.py
2195 lines (2083 loc) · 115 KB
/
sample_generator.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from ast import dump
from cgitb import small
import math
import pathlib
from numpy.core.arrayprint import dtype_is_implied
from numpy.random import sample
import ray
from tqdm import tqdm
import collections
import pickle
from sklearn.model_selection import train_test_split
from py_psops import Py_PSOPS
import numpy as np
import time
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import os
font1 = {'size': 12}
def plot_dots(x, y, color, lw=1):
assert x.shape[0] == y.shape[0], "x, y dimensional error"
if y.ndim == 1:
plt.scatter(x, y, alpha=0.5)
elif y.ndim == 2:
pass
else:
raise Exception(f"{y.ndim} dimensional data input!")
def plot_show():
plt.show()
def contour_plot(xx, yy, zz):
contour = plt.contour(xx, yy, zz, 100, cmap='rainbow')
plt.clabel(contour, fontsize=6, colors='k')
# 去掉坐标轴刻度
# plt.xticks(())
# plt.yticks(())
# 填充颜色,f即filled,6表示将三色分成三层,cmap那儿是放置颜色格式,hot表示热温图(红黄渐变)
# 更多颜色图参考:https://blog.csdn.net/mr_cat123/article/details/80709099
# 颜色集,6层颜色,默认的情况不用写颜色层数,
# c_set = plt.contourf(xx, yy, zz, cmap='rainbow')
# or c_map='hot'
# 设置颜色条,(显示在图片右边)
# plt.colorbar(c_set)
# 显示
plt.show()
def plot_3d(xx, yy, zz):
fig = plt.figure()
ax = Axes3D(fig)
# ax.plot_surface(xx, yy, zz, cmap='rainbow')
ax.plot_surface(xx, yy, zz, rstride=1, cstride=1,
alpha=0.75, cmap='rainbow')
ax.contour(xx, yy, zz, zdir='z', offset=zz.min(),
cmap='rainbow') # 等高线图,要设置offset,为Z的最小值
ax.contour(xx, yy, zz, zdir='y', offset=yy.max(), cmap='rainbow')
ax.contour(xx, yy, zz, zdir='x', offset=xx.min(), cmap='rainbow')
plt.show()
def read_result(f_path):
file = open(f_path, 'r', encoding='gbk')
lines = file.readlines()
file.close()
num = 0
gen1 = list()
gen2 = list()
list_loss = list()
list_180 = list()
list_500 = list()
list_d = list()
for line in lines:
if line[0] == 'g':
continue
if line.strip() != '':
num += 1
line_array = line.strip().split()
gen1.append(float(line_array[0]))
gen2.append(float(line_array[1]))
list_loss.append(-float(line_array[2]))
list_180.append(float(line_array[3]))
list_500.append(float(line_array[4]))
list_d.append(float(line_array[5]))
mesh = int(num ** 0.5)
assert mesh * mesh == num, 'Line num is not correct!'
gen1 = np.array(gen1).reshape(mesh, mesh)
gen2 = np.array(gen2).reshape(mesh, mesh)
list_loss = np.array(list_loss).reshape(mesh, mesh)
list_180 = np.array(list_180).reshape(mesh, mesh)
list_500 = np.array(list_500).reshape(mesh, mesh)
list_d = np.array(list_d).reshape(mesh, mesh)
# list_d = (360. - list_d) / (360. + list_d) * 100.
# list_d[list_d > 300.] = 300.
# list_loss = list_180
list_loss = -list_loss + list_180 - 10.
loss_min = list_loss[list_d < 180.].max()
idx = np.where(list_loss == loss_min)
print(list_loss[list_d < 180.].max(), gen1[idx], gen2[idx])
list_loss = np.array(list_loss)
# list_loss = list_d
# fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
# # ax.plot_surface(gen2, gen1, list_loss, rstride=1, cstride=1, alpha=0.5, cmap='rainbow', zorder=0)
# # ax.contour(gen2, gen1, list_loss, zdir='z', offset=list_loss.min(), cmap='rainbow') # 等高线图,要设置offset,为Z的最小值
# # ax.set_xlabel('Gen_BUS38', fontdict=font1)
# # ax.set_ylabel('Gen_BUS30', fontdict=font1)
# # ax.set_zlabel('Delta_MAX', fontdict=font1)
# ax.plot_surface(gen1, gen2, list_loss, rstride=1, cstride=1, alpha=0.5, cmap='rainbow', zorder=0)
# ax.contour(gen1, gen2, list_loss, zdir='z', offset=list_loss.min(), cmap='rainbow') # 等高线图,要设置offset,为Z的最小值
# ax.set_xlabel('Gen_BUS30', fontdict=font1)
# ax.set_ylabel('Gen_BUS38', fontdict=font1)
# ax.set_zlabel('Ts', fontdict=font1)
# plt.tick_params(labelsize=12)
# ls = np.load('./result_dist.npz', allow_pickle=True)
# x = ls['action_sequence']
# print(x.shape)
# for search_line in x:
# xd = search_line[:, 0]
# yd = search_line[:, 1]
# zd = search_line[:, 2]
# ax.scatter3D(xd, yd, zd, c='k', marker='o', s=50, linewidths=1, alpha=0.25, zorder=1)
# ax.plot(xd, yd, zd, c='k', lw=2, alpha=0.2, zorder=1)
# ax.scatter3D(xd, yd, zd, c='k', marker='o', s=50, linewidths=5, zorder=2) # 绘制散点图
# ax.plot(xd, yd, zd, c='k', lw=2, zorder=2)
contour = plt.contour(gen1, gen2, list_loss, 100, cmap='rainbow', zorder=2)
plt.clabel(contour, fontsize=8, colors='k')
plt.xlim((1.25, 3.75))
plt.ylim((3.25, 9.75))
plt.xlabel('Gen_BUS30', fontdict=font1)
plt.ylabel('Gen_BUS38', fontdict=font1)
plt.tick_params(labelsize=12)
ls = np.load('../results/result_dist.npz', allow_pickle=True)
x = ls['action_sequence']
print(x.shape)
max_re = -999999999.9
max_no = -1
max_seq = None
for seq_no in range(x.shape[0]):
if max_re < np.max(x[seq_no][:, 3]):
max_re = np.max(x[seq_no][:, 3])
max_no = seq_no
max_seq = x[seq_no]
assert max_seq is not None, 'no max reward'
print(max_re, max_no, max_seq)
xd = max_seq[:7, 1]
yd = max_seq[:7, 2]
plt.scatter(xd, yd, c='k', marker='o', s=50, linewidths=5, zorder=2)
plt.plot(xd, yd, c='k', lw=2, zorder=2)
for search_line in x:
xd = search_line[:, 1]
yd = search_line[:, 2]
plt.scatter(xd, yd, c='darkgrey', marker='o', s=50, linewidths=1, alpha=0.25, zorder=1)
plt.plot(xd, yd, c='darkgrey', lw=2, alpha=0.25, zorder=1)
# plt.scatter(xd, yd, c='r', marker='o', s=50, linewidths=5, zorder=2)
# plt.plot(xd, yd, c='r', lw=2, zorder=2)
# search_line = x[-2]
# xd = search_line[:, 1]
# yd = search_line[:, 2]
# plt.scatter(xd, yd, c='b', marker='o', s=50, linewidths=5, zorder=2)
# plt.plot(xd, yd, c='b', lw=2, zorder=2)
plt.show()
# plot_3d(gen1, gen2, np.array(list_loss))
# contour_plot(gen1, gen2, np.array(list_loss))
# plot_3d(gen1, gen2, np.array(list_180))
# contour_plot(gen1, gen2, np.array(list_180))
# plot_3d(gen1, gen2, np.array(list_500))
# contour_plot(gen1, gen2, np.array(list_500))
# plot_3d(gen2, gen1, list_d)
# contour_plot(gen2, gen1, list_d)
# plot_3d(gen2, gen1, 1. / list_d)
# contour_plot(gen2, gen1, 1. / list_d)
class SampleGenerator:
def __init__(self, worker_no, total_worker, basic_seed):
self.workerNo = worker_no
self.totalWorker = total_worker
self.seed = 42 + worker_no * 42 * basic_seed + worker_no
self.rng = np.random.default_rng(self.seed)
# self.__rng = np.random.default_rng()
self.api = Py_PSOPS(flg=self.workerNo, rng=self.rng)
# self.env = sopf_Env(rng=self.__rng, test_on=True)
# self.env.set_flg(worker_no)
def ts_sampler_basic(
self,
result_path,
total_num=1,
round_num=10,
cut_length=31,
test_per=0.2
):
api = self.api
result_path = pathlib.Path(result_path)
if not result_path.exists(): result_path.mkdir()
print(str(result_path.absolute()))
# random selection range
aclines = np.arange(api.get_acline_number())
fault_duration = 0.08 + np.arange(13) * 0.01
total_step = api.get_info_ts_max_step()
cut_length = min(total_step, cut_length)
sampled = tqdm(total=total_num)
stable_num = tqdm(total=max(int(total_num*0.25), 1))
rotor_unstable_num = tqdm(total=max(int(total_num*0.25), 1))
volt_unstable_num = tqdm(total=max(int(total_num*0.25), 1))
freq_unstable_num = tqdm(total=max(int(total_num*0.25), 1))
topo_list = list()
fault_list = list()
label_list = np.zeros([total_num], dtype=float)
system_list = np.zeros([total_num, cut_length, 6], dtype=float)
delta_list = np.zeros([total_num, cut_length, api.get_generator_number()], dtype=float)
volt_list = np.zeros([total_num, cut_length, api.get_bus_number()], dtype=float)
transfer_list = np.zeros([total_num, cut_length, api.get_acline_number()], dtype=float)
admittance_list = np.zeros([total_num, 3, 2, api.get_bus_number(), api.get_bus_number()], dtype=float)
impedance_list = np.zeros([total_num, 3, 2, api.get_bus_number(), api.get_bus_number()], dtype=float)
fac_imp_LD_list = np.zeros([total_num, 3, api.get_network_n_inverse_non_zero()+api.get_bus_number(), 6], dtype=float)
fac_imp_U_list = np.zeros([total_num, 3, api.get_network_n_inverse_non_zero()+api.get_bus_number(), 6], dtype=float)
fac_imp_Diag_list = np.zeros([total_num, 3, api.get_bus_number(), 6], dtype=float)
topo_no = 0
tmp_list = list()
cur_fault = list()
while(1):
# topo sampling
topo_sample = api.get_network_topology_sample(topo_change=int(topo_no%3))
# power flow sampling
pf_samples = api.get_power_flow_sample_stepwise(num=round_num, check_voltage=False, termination_num=500)
if len(pf_samples) == 0: continue
# ts sampling
for pf_no in range(len(pf_samples)):
# restore power flow
api.set_power_flow_initiation(pf_samples[pf_no])
api.cal_power_flow_basic_nr()
for fault_no in range(round_num):
# fault line sample, n-1 or n-2
while True:
tmp_list = self.rng.choice(aclines, int(fault_no%2)+1, False)
api.set_network_acline_all_connectivity([False]*(int(fault_no%2)+1), tmp_list)
if api.get_network_n_acsystem_check_connectivity() == 1: break
api.set_network_acline_all_connectivity([True]*(int(fault_no%2)+1), tmp_list)
api.set_network_acline_all_connectivity([True]*(int(fault_no%2)+1), tmp_list)
# line fault setting
cur_fault.clear()
api.set_fault_disturbance_clear_all()
for acline_no in tmp_list:
loc = self.rng.choice([0, 100])
f_time = self.rng.choice(fault_duration)
cur_fault.append([acline_no] + api.get_acline_info(acline_no) + [loc, f_time])
api.set_fault_disturbance_add_acline(0, loc, 0.0, f_time, acline_no)
api.set_fault_disturbance_add_acline(1, loc, f_time, 10., acline_no)
# simulation
fin_step = api.cal_transient_stability_simulation_ti_sv()
if fin_step < cut_length: continue
# label determination
label = api.get_transient_stability_check_stability()
if label == 0 and stable_num.n >= stable_num.total: continue
if label == 1 and rotor_unstable_num.n >= rotor_unstable_num.total: continue
if label == 2 and volt_unstable_num.n >= volt_unstable_num.total: continue
if label == 3 and freq_unstable_num.n >= freq_unstable_num.total: continue
sim_valid, start_topo, result_all, result_ang, result_vol, result_tran, adm_list, imp_list, fac_imp_list = api.get_transient_stability_all_relevant_data()
if sim_valid == False: continue
# save results
label_list[sampled.n] = label
system_list[sampled.n, :, :] = result_all[:cut_length]
delta_list[sampled.n, :, :] = result_ang[:cut_length, :, 0]
volt_list[sampled.n, :, :] = result_vol[:cut_length, :, 0]
transfer_list[sampled.n, :, :] = result_tran[:cut_length, :, 0]
# save topo and faults
topo_list.append(topo_sample)
fault_list.append(cur_fault.copy())
# topo matrix
admittance_list[sampled.n, :, :, :, :] = adm_list[:, :, :, :]
impedance_list[sampled.n, :, :, :, :] = imp_list[:, :, :, :]
for f_step in range(3):
fac_imp_LD_list[sampled.n, f_step, :, :] = fac_imp_list[f_step][0]
fac_imp_U_list[sampled.n, f_step, :, :] = fac_imp_list[f_step][1]
fac_imp_Diag_list[sampled.n, f_step, :, :] = fac_imp_list[f_step][2]
# sample count
sampled.update(1)
if label == 0: stable_num.update(1)
if label == 1: rotor_unstable_num.update(1)
if label == 2: volt_unstable_num.update(1)
if label == 3: freq_unstable_num.update(1)
if sampled.n >= total_num: break
if sampled.n >= total_num: break
if sampled.n >= total_num: break
topo_no += 1
total_idx = np.arange(total_num)
train_idx, test_idx = train_test_split(total_idx, test_size=test_per, random_state=42)
topo_list = np.array(topo_list, dtype=object)
fault_list = np.array(fault_list, dtype=object)
np.savez(result_path / f'training.{self.workerNo}.{self.seed}.npz',
label=label_list[train_idx],
topo=topo_list[train_idx],
fault=fault_list[train_idx],
sys=system_list[train_idx],
delta=delta_list[train_idx],
volt=volt_list[train_idx],
transfer=transfer_list[train_idx],
admittance=admittance_list[train_idx],
impedance=impedance_list[train_idx],
fipld=fac_imp_LD_list[train_idx],
fipu=fac_imp_U_list[train_idx],
fipdiag=fac_imp_Diag_list[train_idx]
)
np.savez(result_path / f'testing.{self.workerNo}.{self.seed}.npz',
label=label_list[test_idx],
topo=topo_list[test_idx],
fault=fault_list[test_idx],
sys=system_list[test_idx],
delta=delta_list[test_idx],
volt=volt_list[test_idx],
transfer=transfer_list[test_idx],
admittance=admittance_list[test_idx],
impedance=impedance_list[test_idx],
fipld=fac_imp_LD_list[test_idx],
fipu=fac_imp_U_list[test_idx],
fipdiag=fac_imp_Diag_list[test_idx]
)
def ts_sampler_simple_random_for_avr_1(self,
gen_no,
result_path,
num=1,
test_per=0.2,
cut_length=None,
check_voltage=False,
check_slack=False,
limit_3phase_short=False,
must_stable=False,
limit_angle_range = False,
balance_stability=False,
need_larger_than=None,
need_smaller_than=None):
api = self.api
assert must_stable != True or balance_stability != True, "do not need balance_stability when must_stable"
assert must_stable != True or limit_angle_range != True, "do not need balance_stability when must_stable"
assert (need_larger_than is None and need_smaller_than is None) or (need_larger_than is not None and need_smaller_than is not None), "need_larger_than and need_smaller_than must be the same type"
result_path = pathlib.Path(result_path)
if not result_path.exists(): result_path.mkdir()
print(str(result_path.absolute()))
choice_aclines = np.arange(api.get_acline_number())
choice_generators = np.arange(api.get_generator_number())
choice_generators = choice_generators[np.where(choice_generators != gen_no)]
choice_loads = np.arange(api.get_load_number())
total_step = api.get_info_ts_max_step()
total_step = min(total_step, cut_length) if cut_length is not None else total_step
t = np.ones([num, total_step, 1], dtype=np.float32) * -1
mask = np.zeros([num, total_step, 1], dtype=np.float32)
x = np.zeros([num, total_step, 1], dtype=np.float32)
z = np.zeros([num, total_step, 2], dtype=np.float32)
event_t = np.zeros([num, 3], dtype=np.float32)
z_jump = np.zeros([num, 3, 2], dtype = np.float32)
total_sim = 0
sampled = 0
sampled_stable = 0
sampled_unstable = 0
sampled_larger = 0
larger_sample = 0 if need_larger_than is None else int(num * 0.3)
sampled_smaller = 0
smaller_sample = 0 if need_smaller_than is None else int(num * 0.3)
old_sampled = -1
flg_no_change = 0
while sampled < num:
if old_sampled == sampled: flg_no_change += 1
old_sampled = sampled
samples = api.get_power_flow_sample_stepwise(num=num, check_voltage=check_voltage, check_slack=check_slack)
for sample in samples:
api.set_power_flow_initiation(sample)
api.cal_power_flow_basic_nr()
api.set_fault_disturbance_clear_all()
if limit_3phase_short:
if flg_no_change < 3: fd_idx = sampled % 5
else: fd_idx = total_sim % 5
if fd_idx == 0:
acline_no = self.rng.choice(choice_aclines)
loc = self.rng.choice([0, 100])
api.set_fault_disturbance_add_acline(0, loc, 1.0, 1.3, acline_no)
api.set_fault_disturbance_add_acline(1, loc, 1.3, 10., acline_no)
elif fd_idx == 1 or fd_idx == 2:
g_no = self.rng.choice(choice_generators)
api.set_fault_disturbance_add_generator(0, 1.0, 0.5, g_no)
api.set_fault_disturbance_add_generator(0, 1.3, 0.5, g_no)
elif fd_idx == 3 or fd_idx == 4:
l_no = self.rng.choice(choice_loads)
api.set_fault_disturbance_add_load(0, 1.0, 0.5, l_no)
api.set_fault_disturbance_add_load(0, 1.3, 0.5, l_no)
else:
acline_no = self.rng.choice(choice_aclines)
loc = self.rng.choice([0, 100])
api.set_fault_disturbance_add_acline(0, loc, 1.0, 1.3, acline_no)
api.set_fault_disturbance_add_acline(1, loc, 1.3, 10., acline_no)
real_fin_step = api.cal_transient_stability_simulation_ti_sv()
total_sim += 1
# cut length check
if cut_length is not None:
if real_fin_step > cut_length: real_fin_step = cut_length
# check e step
e_step = api.get_info_fault_step_sequence()
for flt_no in range(len(e_step)):
e_step += 1
api.set_info_ts_step_element_state(e_step[flt_no], is_real_step=False)
tmp_re = api.get_generator_exciter_ts_cur_step_result(gen_no, rt=True)
if np.any(tmp_re != tmp_re): continue
# get results
result_all = api.get_acsystem_all_ts_result()[0]
if np.any(result_all != result_all): continue
# check stability
fin_step = real_fin_step
delta_diff = result_all[:fin_step, 1]
min_vol = result_all[:fin_step, 4]
min_freq = result_all[:fin_step, 2]
max_freq = result_all[:fin_step, 3]
label = 0
vol_count = 0
for step in range(fin_step):
if delta_diff[step] > 360.:
label = 1
break
# if min_vol[step] < 0.7: vol_count += 1
# else: vol_count = 0
# if vol_count > 100:
# label = 2
# break
# if min_freq[step] < 49.:
# label = 3
# break
# if max_freq[step] > 51.:
# label = 4
# break
if label != 0:
if must_stable: continue
if balance_stability and sampled_unstable >= num / 2: continue
if limit_angle_range: fin_step = step
else:
if balance_stability and sampled_stable >= num / 2: continue
# check result
result = api.get_generator_exciter_ts_all_step_result(gen_no)
if np.any(result[:fin_step] != result[:fin_step]): continue
# check sample requirement
if need_larger_than is not None and sampled >= num - larger_sample - larger_sample:
if sampled_larger >= larger_sample and sampled_smaller >= smaller_sample: pass
else:
if result[:fin_step, -1].max() < need_larger_than and result[:fin_step, -1].min() > need_smaller_than: continue
flg = False
if sampled_larger < larger_sample and result[:fin_step, -1].max() > need_larger_than: flg = True
if sampled_smaller < smaller_sample and result[:fin_step, -1].min() < need_smaller_than: flg = True
if flg == False: continue
# time step
assert np.all(t[sampled] == -1), "this sample space has been polluted!!!!!!!!!!!!!!!!!"
t[sampled, :fin_step, 0] = result_all[:fin_step, 0]
# masks
mask[sampled, :fin_step, 0] = 1.
# event time
event_t[sampled] = api.get_info_fault_time_sequence()
# event data
e_step = api.get_info_fault_step_sequence()
for flt_no in range(len(e_step)):
e_step += 1
api.set_info_ts_step_element_state(e_step[flt_no], is_real_step=False)
tmp_re = api.get_generator_exciter_ts_cur_step_result(gen_no, rt=True)
z_jump[sampled][flt_no][0] = tmp_re[0]
z_jump[sampled][flt_no][1] = tmp_re[-2]
x[sampled, :fin_step, 0] = result[:fin_step, -1] # efd
# voltages and vs
z[sampled, :fin_step, 0] = result[:fin_step, 0] # vt
z[sampled, :fin_step, 1] = result[:fin_step, -2] # VS
# check requirements
if need_larger_than is not None and sampled >= num - larger_sample - larger_sample:
if sampled_larger >= larger_sample and sampled_smaller >= smaller_sample: pass
else:
if sampled_larger < larger_sample and result[:fin_step, -1].max() > need_larger_than: sampled_larger += 1
if sampled_smaller < smaller_sample and result[:fin_step, -1].min() < need_smaller_than: sampled_smaller += 1
# check stability balance
if balance_stability:
if label != 0: sampled_unstable += 1
else: sampled_stable += 1
# count samples
sampled += 1
if sampled >= num: break
print(f'sampled: {sampled}, stable: {sampled_stable}, unstable: {sampled_unstable}, larger: {sampled_larger}, smaller: {sampled_smaller}')
total_idx = np.arange(num)
train_idx, test_idx = train_test_split(total_idx, test_size=test_per, random_state=4242)
data_name = [
['Efd', 'p.u.']
]
np.savez(result_path / 'full.npz', name=data_name, t=t, x=x, z=z, event_t=event_t, z_jump=z_jump, mask=mask)
np.savez(result_path / 'training.npz', name=data_name, t=t[train_idx], x=x[train_idx], z=z[train_idx],
event_t=event_t[train_idx], z_jump=z_jump[train_idx], mask=mask[train_idx])
np.savez(result_path / 'testing.npz', name=data_name, t=t[test_idx], x=x[test_idx], z=z[test_idx],
event_t=event_t[test_idx], z_jump=z_jump[test_idx], mask=mask[test_idx])
def ts_sampler_simple_random_for_gen_0(self,
gen_no,
result_path,
num=1,
test_per=0.2,
cut_length=None,
check_voltage=False,
check_slack=False,
limit_3phase_short=False,
must_stable=False,
limit_angle_range = False,
balance_stability=False
):
api = self.api
assert must_stable != True or balance_stability != True, "do not need balance_stability when must_stable"
assert must_stable != True or limit_angle_range != True, "do not need balance_stability when must_stable"
result_path = pathlib.Path(result_path)
if not result_path.exists(): result_path.mkdir()
print(str(result_path.absolute()))
choice_aclines = np.arange(api.get_acline_number())
choice_generators = np.arange(api.get_generator_number())
choice_generators = choice_generators[np.where(choice_generators != gen_no)]
choice_loads = np.arange(api.get_load_number())
total_length = api.get_info_ts_max_step() if cut_length is None else cut_length
t = np.ones([num, total_length, 1], dtype=np.float32) * -1
mask = np.zeros([num, total_length, 1], dtype=np.float32)
x = np.zeros([num, total_length, 2], dtype=np.float32)
z = np.zeros([num, total_length, 0], dtype=np.float32)
v = np.zeros([num, total_length, 2], dtype=np.float32)
i = np.zeros([num, total_length, 2], dtype=np.float32)
event_t = np.zeros([num, 3], dtype=np.float32)
z_jump = np.zeros([num, 3, 0], dtype = np.float32)
v_jump = np.zeros([num, 3, 2], dtype = np.float32)
total_sim = 0
sampled = 0
sampled_stable = 0
sampled_unstable = 0
old_sampled = -1
flg_no_change = 0
while sampled < num:
if old_sampled == sampled: flg_no_change += 1
old_sampled = sampled
samples = api.get_power_flow_sample_stepwise(num=num, check_voltage=check_voltage, check_slack=check_slack)
for sample in samples:
api.set_power_flow_initiation(sample)
api.cal_power_flow_basic_nr()
api.set_fault_disturbance_clear_all()
if limit_3phase_short:
if flg_no_change < 3: fd_idx = sampled % 5
else: fd_idx = total_sim % 5
if fd_idx == 0:
acline_no = self.rng.choice(choice_aclines)
loc = self.rng.choice([0, 100])
api.set_fault_disturbance_add_acline(0, loc, 1.0, 1.3, acline_no)
api.set_fault_disturbance_add_acline(1, loc, 1.3, 10., acline_no)
elif fd_idx == 1 or fd_idx == 2:
g_no = self.rng.choice(choice_generators)
api.set_fault_disturbance_add_generator(0, 1.0, 0.5, g_no)
api.set_fault_disturbance_add_generator(0, 1.3, 0.5, g_no)
elif fd_idx == 3 or fd_idx == 4:
l_no = self.rng.choice(choice_loads)
api.set_fault_disturbance_add_load(0, 1.0, 0.5, l_no)
api.set_fault_disturbance_add_load(0, 1.3, 0.5, l_no)
else:
acline_no = self.rng.choice(choice_aclines)
loc = self.rng.choice([0, 100])
api.set_fault_disturbance_add_acline(0, loc, 1.0, 1.3, acline_no)
api.set_fault_disturbance_add_acline(1, loc, 1.3, 10., acline_no)
real_fin_step = api.cal_transient_stability_simulation_ti_sv()
total_sim += 1
# cut length check
if cut_length is not None:
if real_fin_step > cut_length: real_fin_step = cut_length
# check e step
e_step = api.get_info_fault_step_sequence()
for flt_no in range(len(e_step)):
e_step += 1
api.set_info_ts_step_element_state(e_step[flt_no], is_real_step=False)
tmp_re = api.get_generator_ts_cur_step_result(gen_no, rt=True)
if np.any(tmp_re != tmp_re): continue
# get results
result_all = api.get_acsystem_all_ts_result()[0]
if np.any(result_all != result_all): continue
# check stability
fin_step = real_fin_step
delta_diff = result_all[:fin_step, 1]
min_vol = result_all[:fin_step, 4]
min_freq = result_all[:fin_step, 2]
max_freq = result_all[:fin_step, 3]
label = 0
vol_count = 0
for step in range(fin_step):
if delta_diff[step] > 360.:
label = 1
break
if min_vol[step] < 0.7: vol_count += 1
else: vol_count = 0
if vol_count > 100:
label = 2
break
if min_freq[step] < 49.:
label = 3
break
if max_freq[step] > 51.:
label = 4
break
if label != 0:
if must_stable: continue
if balance_stability and sampled_unstable >= num / 2: continue
if limit_angle_range: fin_step = step
else:
if balance_stability and sampled_stable >= num / 2: continue
# check result
result = api.get_generator_ts_all_step_result(gen_no, need_inner_e=True)
if np.any(result[:fin_step] != result[:fin_step]): continue
# time step
assert np.all(t[sampled] == -1), "this sample space has been polluted!!!!!!!!!!!!!!!!!"
t[sampled, :fin_step, 0] = result_all[:fin_step, 0]
# masks
mask[sampled, :fin_step, 0] = 1.
# state variables
result[:, 0] /= 180. / math.pi
result[:, 1] = (result[:, 1] - 50)
result[:, 3] /= 180. / math.pi
x[sampled, :fin_step, 0] = result[:fin_step, 0]
x[sampled, :fin_step, 1] = result[:fin_step, 1]
v[sampled, :fin_step, 0] = result[:fin_step, 2] * np.cos(result[:fin_step, 3])
v[sampled, :fin_step, 1] = result[:fin_step, 2] * np.sin(result[:fin_step, 3])
vv = v[sampled, :fin_step, :2]
# inject currents
i[sampled, :fin_step, 0] = (result[:fin_step, 4] * vv[:fin_step, 0] + result[:fin_step, 5] * vv[:fin_step, 1]) / (vv[:fin_step, 0] * vv[:fin_step, 0] + vv[:fin_step, 1] * vv[:fin_step, 1])
i[sampled, :fin_step, 1] = (result[:fin_step, 4] * vv[:fin_step, 1] - result[:fin_step, 5] * vv[:fin_step, 0]) / (vv[:fin_step, 0] * vv[:fin_step, 0] + vv[:fin_step, 1] * vv[:fin_step, 1])
# event time
event_t[sampled] = api.get_info_fault_time_sequence()
# event data
e_step = api.get_info_fault_step_sequence()
for flt_no in range(len(e_step)):
e_step += 1
api.set_info_ts_step_element_state(e_step[flt_no], is_real_step=False)
tmp_re = api.get_generator_ts_cur_step_result(gen_no, rt=True)
tmp_re[3] /= 180. / math.pi
v_jump[sampled][flt_no][0] = tmp_re[2] * np.cos(tmp_re[3])
v_jump[sampled][flt_no][1] = tmp_re[2] * np.sin(tmp_re[3])
# check stability balance
if balance_stability:
if label != 0: sampled_unstable += 1
else: sampled_stable += 1
# count samples
sampled += 1
if sampled >= num: break
print(f'sampled: {sampled}, stable: {sampled_stable}, unstable: {sampled_unstable}')
total_idx = np.arange(num)
train_idx, test_idx = train_test_split(total_idx, test_size=test_per, random_state=4242)
data_name = [
['Rotor Angle', 'Rad.'],
['Omega', 'p.u.'],
['Ix', 'p.u.'],
['Iy', 'p.u.']
]
np.savez(result_path / 'full.npz', name=data_name, t=t, x=x, z=z, v=v, i=i, event_t=event_t, z_jump=z_jump, v_jump=v_jump, mask=mask)
np.savez(result_path / 'training.npz', name=data_name, t=t[train_idx], x=x[train_idx], z=z[train_idx], v=v[train_idx], i=i[train_idx],
event_t=event_t[train_idx], z_jump=z_jump[train_idx], v_jump=v_jump[train_idx], mask=mask[train_idx])
np.savez(result_path / 'testing.npz', name=data_name, t=t[test_idx], x=x[test_idx], z=z[test_idx], v=v[test_idx], i=i[test_idx],
event_t=event_t[test_idx], z_jump=z_jump[test_idx], v_jump=v_jump[test_idx], mask=mask[test_idx])
def ts_sampler_simple_random_for_custom_district(self,
result_path,
num=1,
test_per=0.2,
cut_length=None,
check_voltage=False,
check_slack=False,
limit_3phase_short=False,
must_stable=False,
limit_angle_range = False,
balance_stability=False
):
api = self.api
assert must_stable != True or balance_stability != True, "do not need balance_stability when must_stable"
assert must_stable != True or limit_angle_range != True, "do not need balance_stability when must_stable"
result_path = pathlib.Path(result_path)
if not result_path.exists(): result_path.mkdir()
print(str(result_path.absolute()))
choice_aclines = np.arange(api.get_acline_number())
choice_aclines = choice_aclines[np.where(choice_aclines != 0)]
choice_generators = np.arange(api.get_generator_number())
choice_generators = choice_generators[np.where(choice_generators != 3)]
choice_generators = choice_generators[np.where(choice_generators != 4)]
choice_loads = np.arange(api.get_load_number())
choice_loads = choice_loads[np.where(choice_loads != 4)]
total_length = api.get_info_ts_max_step() if cut_length is None else cut_length
t = np.ones([num, total_length, 1], dtype=np.float32) * -1
mask = np.zeros([num, total_length, 1], dtype=np.float32)
# x = np.zeros([num, total_length, 0], dtype=np.float32)
x = np.zeros([num, total_length, 6], dtype=np.float32)
z = np.zeros([num, total_length, 0], dtype=np.float32)
v = np.zeros([num, total_length, 2], dtype=np.float32)
i = np.zeros([num, total_length, 2], dtype=np.float32)
event_t = np.zeros([num, 3], dtype=np.float32)
z_jump = np.zeros([num, 3, 0], dtype = np.float32)
v_jump = np.zeros([num, 3, 2], dtype = np.float32)
total_sim = 0
sampled = 0
sampled_stable = 0
sampled_unstable = 0
old_sampled = -1
flg_no_change = 0
while sampled < num:
if old_sampled == sampled: flg_no_change += 1
old_sampled = sampled
samples = api.get_power_flow_sample_stepwise(num=num, check_voltage=check_voltage, check_slack=check_slack)
for sample in samples:
api.set_power_flow_initiation(sample)
api.cal_power_flow_basic_nr()
api.set_fault_disturbance_clear_all()
if limit_3phase_short:
if flg_no_change < 3: fd_idx = sampled % 5
else: fd_idx = total_sim % 5
if fd_idx == 0:
acline_no = self.rng.choice(choice_aclines)
loc = self.rng.choice([0, 100])
api.set_fault_disturbance_add_acline(0, loc, 1.0, 1.3, acline_no)
api.set_fault_disturbance_add_acline(1, loc, 1.3, 10., acline_no)
elif fd_idx == 1 or fd_idx == 2:
g_no = self.rng.choice(choice_generators)
api.set_fault_disturbance_add_generator(0, 1.0, 0.5, g_no)
api.set_fault_disturbance_add_generator(0, 1.3, 0.5, g_no)
elif fd_idx == 3 or fd_idx == 4:
l_no = self.rng.choice(choice_loads)
api.set_fault_disturbance_add_load(0, 1.0, 0.5, l_no)
api.set_fault_disturbance_add_load(0, 1.3, 0.5, l_no)
else:
acline_no = self.rng.choice(choice_aclines)
loc = self.rng.choice([0, 100])
api.set_fault_disturbance_add_acline(0, loc, 1.0, 1.3, acline_no)
api.set_fault_disturbance_add_acline(1, loc, 1.3, 10., acline_no)
real_fin_step = api.cal_transient_stability_simulation_ti_sv()
total_sim += 1
# cut length check
if cut_length is not None:
if real_fin_step < cut_length: continue #####################################################################################
if real_fin_step > cut_length: real_fin_step = cut_length
# check e step
e_step = api.get_info_fault_step_sequence()
valid = True
for flt_no in range(len(e_step)):
e_step[flt_no] += 1
if e_step[flt_no] > real_fin_step:
valid = False
break
api.set_info_ts_step_element_state(e_step[flt_no], is_real_step=False)
tmp_re = api.get_bus_ts_cur_step_result(bus_no=30, rt=True)
if np.any(tmp_re != tmp_re):
valid = False
continue
if valid == False:
continue
# get results
result_all = api.get_acsystem_all_ts_result()[0]
if np.any(result_all != result_all): continue
# check stability
fin_step = real_fin_step
delta_diff = result_all[:fin_step, 1]
min_vol = result_all[:fin_step, 4]
min_freq = result_all[:fin_step, 2]
max_freq = result_all[:fin_step, 3]
label = 0
vol_count = 0
for step in range(fin_step):
if delta_diff[step] > 360.:
label = 1
break
# if min_vol[step] < 0.7: vol_count += 1
# else: vol_count = 0
# if vol_count > 100:
# label = 2
# break
# if min_freq[step] < 49.:
# label = 3
# break
# if max_freq[step] > 51.:
# label = 4
# break
if label != 0:
if must_stable: continue
if balance_stability and sampled_unstable >= num / 2: continue
if limit_angle_range: #######################################################################################################################
# result_33 = api.get_generator_ts_all_step_result(3)
# result_34 = api.get_generator_ts_all_step_result(4)
# result_39 = api.get_generator_ts_all_step_result(9)
# d_33_39 = np.abs(result_33[:, 0] - result_39[:, 0])
# d_34_39 = np.abs(result_34[:, 0] - result_39[:, 0])
# if np.any(d_33_39 > 180.) or np.any(d_34_39 > 180.):
# continue
fin_step = step
# continue
else:
if balance_stability and sampled_stable >= num / 2: continue
# check result
result_33 = api.get_generator_ts_all_step_result(3)
result_34 = api.get_generator_ts_all_step_result(4)
result_20 = api.get_load_ts_all_step_result(3)
result_v = api.get_bus_all_ts_result(bus_list=[30])[:, 0, :]
result_pq = api.get_acline_all_ts_result(acline_list=[0])[:, 0, :]
if np.any(result_v != result_v) or np.any(result_pq != result_pq): continue
# time step
assert np.all(t[sampled] == -1), "this sample space has been polluted!!!!!!!!!!!!!!!!!"
t[sampled, :fin_step, 0] = result_all[:fin_step, 0]
# masks
mask[sampled, :fin_step, 0] = 1.
# state variables
x[sampled, :fin_step, 0:2] = result_33[:fin_step, 4:6]
x[sampled, :fin_step, 2:4] = result_34[:fin_step, 4:6]
x[sampled, :fin_step, 4:6] = result_20[:fin_step, 2:4]
# voltage
result_v[:, 1] /= 180. / math.pi
v[sampled, :fin_step, 0] = result_v[:fin_step, 0] * np.cos(result_v[:fin_step, 1])
v[sampled, :fin_step, 1] = result_v[:fin_step, 0] * np.sin(result_v[:fin_step, 1])
vv = v[sampled, :fin_step, :2]
# inject currents
i[sampled, :fin_step, 0] = -(result_pq[:fin_step, 0] * vv[:fin_step, 0] + result_pq[:fin_step, 1] * vv[:fin_step, 1]) / (vv[:fin_step, 0] * vv[:fin_step, 0] + vv[:fin_step, 1] * vv[:fin_step, 1])
i[sampled, :fin_step, 1] = -(result_pq[:fin_step, 0] * vv[:fin_step, 1] - result_pq[:fin_step, 1] * vv[:fin_step, 0]) / (vv[:fin_step, 0] * vv[:fin_step, 0] + vv[:fin_step, 1] * vv[:fin_step, 1])
# event time
event_t[sampled] = api.get_info_fault_time_sequence()
# event data
e_step = api.get_info_fault_step_sequence()
for flt_no in range(len(e_step)):
e_step += 1
api.set_info_ts_step_element_state(e_step[flt_no], is_real_step=False)
tmp_re = api.get_bus_ts_cur_step_result(bus_no=30, rt=True)
tmp_re[1] /= 180. / math.pi
v_jump[sampled][flt_no][0] = tmp_re[0] * np.cos(tmp_re[1])
v_jump[sampled][flt_no][1] = tmp_re[0] * np.sin(tmp_re[1])
# check stability balance
if balance_stability:
if label != 0: sampled_unstable += 1
else: sampled_stable += 1
# count samples
sampled += 1
if sampled >= num: break
print(f'sampled: {sampled}, stable: {sampled_stable}, unstable: {sampled_unstable}')
total_idx = np.arange(num)
train_idx, test_idx = train_test_split(total_idx, test_size=test_per, random_state=4242)
data_name = [
['P_33', 'p.u.'],
['Q_33', 'p.u.'],
['P_34', 'p.u.'],
['Q_34', 'p.u.'],
['P_20', 'p.u.'],
['Q_20', 'p.u.'],
['Ix', 'p.u.'],
['Iy', 'p.u.']
]
np.savez(result_path / 'full.npz', name=data_name, t=t, x=x, z=z, v=v, i=i, event_t=event_t, z_jump=z_jump, v_jump=v_jump, mask=mask)
np.savez(result_path / 'training.npz', name=data_name, t=t[train_idx], x=x[train_idx], z=z[train_idx], v=v[train_idx], i=i[train_idx],
event_t=event_t[train_idx], z_jump=z_jump[train_idx], v_jump=v_jump[train_idx], mask=mask[train_idx])
np.savez(result_path / 'testing.npz', name=data_name, t=t[test_idx], x=x[test_idx], z=z[test_idx], v=v[test_idx], i=i[test_idx],
event_t=event_t[test_idx], z_jump=z_jump[test_idx], v_jump=v_jump[test_idx], mask=mask[test_idx])
def ts_sampler_simple_random_for_Solar_district(self,
result_path,
num_total=1,
test_per=0.2,
cut_length=None,
check_voltage=False,
check_slack=False,
limit_3phase_short=False,
must_stable=False,
limit_angle_range = False,
balance_stability=False,
ratio_3phase=0.2
):
api = self.api
assert must_stable != True or balance_stability != True, "do not need balance_stability when must_stable"
assert must_stable != True or limit_angle_range != True, "do not need balance_stability when must_stable"
result_path = pathlib.Path(result_path)
if not result_path.exists(): result_path.mkdir()
print(str(result_path.absolute()))
choice_aclines = np.arange(api.get_acline_number())
choice_aclines = choice_aclines[np.where(choice_aclines != 0)]
choice_generators = np.arange(api.get_generator_number())
choice_generators = choice_generators[np.where(choice_generators != 3)]
choice_generators = choice_generators[np.where(choice_generators != 4)]
choice_loads = np.arange(api.get_load_number())
choice_loads = choice_loads[np.where(choice_loads != 4)]
total_length = api.get_info_ts_max_step() if cut_length is None else cut_length
t = np.ones([num_total, total_length, 1], dtype=np.float32) * -1
mask = np.zeros([num_total, total_length, 1], dtype=np.float32)
x = np.zeros([num_total, total_length, 0], dtype=np.float32)
z = np.zeros([num_total, total_length, 4], dtype=np.float32)
v = np.zeros([num_total, total_length, 2], dtype=np.float32)
i = np.zeros([num_total, total_length, 2], dtype=np.float32)
event_t = np.zeros([num_total, 3], dtype=np.float32)
z_jump = np.zeros([num_total, 3, 4], dtype = np.float32)
v_jump = np.zeros([num_total, 3, 2], dtype = np.float32)
total_sim = 0
sampled = 0
f_3phase = ratio_3phase * num_total
d_gen = (1 - ratio_3phase) / 2 * num_total
d_load = num_total - f_3phase - d_gen
sampled_stable = 0
sampled_unstable = 0
old_sampled = -1
flg_no_change = 0
while sampled < num_total:
if old_sampled == sampled: flg_no_change += 1
old_sampled = sampled
samples = api.get_power_flow_sample_stepwise(num=num_total, check_voltage=check_voltage, check_slack=check_slack)
for sample in samples:
api.set_power_flow_initiation(sample)
api.cal_power_flow_basic_nr()
api.set_fault_disturbance_clear_all()
# set fault and disturbance
if limit_3phase_short:
if flg_no_change < 3: fd_idx = sampled % 5
else: fd_idx = total_sim % 5
if fd_idx == 0:
acline_no = self.rng.choice(choice_aclines)
loc = self.rng.choice([0, 100])
api.set_fault_disturbance_add_acline(0, loc, 1.0, 1.1, acline_no)
api.set_fault_disturbance_add_acline(1, loc, 1.1, 10., acline_no)
elif fd_idx == 1 or fd_idx == 2:
g_no = self.rng.choice(choice_generators)
api.set_fault_disturbance_add_generator(0, 1.0, 0.5, g_no)
api.set_fault_disturbance_add_generator(0, 5.0, 0.5, g_no)
elif fd_idx == 3 or fd_idx == 4:
l_no = self.rng.choice(choice_loads)
api.set_fault_disturbance_add_load(0, 1.0, 0.5, l_no)
api.set_fault_disturbance_add_load(0, 5.0, 0.5, l_no)
else:
acline_no = self.rng.choice(choice_aclines)
loc = self.rng.choice([0, 100])
api.set_fault_disturbance_add_acline(0, loc, 1.0, 1.2, acline_no)
api.set_fault_disturbance_add_acline(1, loc, 1.2, 10., acline_no)
# temperature sampling, 20-35
temperature = self.rng.random([2]) * 15 + 20.
api.set_generator_all_environment_status([2, 2], temperature.tolist(), [3, 4])
# ts simulation
real_fin_step = api.cal_transient_stability_simulation_ti_sv()
total_sim += 1
# cut length check
if cut_length is not None:
if real_fin_step < cut_length: continue #####################################################################################
if real_fin_step > cut_length: real_fin_step = cut_length
# check e step
e_step = api.get_info_fault_step_sequence()
valid = True
for flt_no in range(len(e_step)):
e_step[flt_no] += 1
if e_step[flt_no] > real_fin_step:
valid = False
break
api.set_info_ts_step_element_state(e_step[flt_no], is_real_step=False)
tmp_re = api.get_bus_ts_cur_step_result(bus_no=30, rt=True)
if np.any(tmp_re != tmp_re):
valid = False
continue
if valid == False:
continue
# get results
result_all = api.get_acsystem_all_ts_result()[0]
if np.any(result_all != result_all): continue
# check stability
fin_step = real_fin_step
delta_diff = result_all[:fin_step, 1]
min_vol = result_all[:fin_step, 4]
min_freq = result_all[:fin_step, 2]
max_freq = result_all[:fin_step, 3]
label = 0
vol_count = 0