/
BaseBackendTest.py
3188 lines (2860 loc) · 120 KB
/
BaseBackendTest.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
# Copyright (c) 2019-2020, RTE (https://www.rte-france.com)
# See AUTHORS.txt
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
# you can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.
# do some generic tests that can be implemented directly to test if a backend implementation can work out of the box
# with grid2op.
# see an example of test_Pandapower for how to use this suit.
import os
import numpy as np
import copy
from abc import ABC, abstractmethod
import inspect
from grid2op.Action import CompleteAction
try:
# this is only available starting python 3.7 or 3.8... tests are with python 3.6 :-(
from math import comb
except ImportError:
def comb(n, k):
if n == k:
return 1
if n < k:
return 0
res = 1
acc = 1
for i in range(k):
res *= int((n - i))
for i in range(1, k + 1):
res /= i
return res
"""
test to check that it's working
for i in range(10):
for j in range(10):
me_ = comb(i,j)
real_ = math.comb(i,j)
assert me_ == real_, "{}, {}".format(i,j)
"""
import warnings
import grid2op
from grid2op.dtypes import dt_bool, dt_int, dt_float
from grid2op.Action import ActionSpace, CompleteAction
from grid2op.Parameters import Parameters
from grid2op.Chronics import ChronicsHandler
from grid2op.Environment import Environment
from grid2op.Exceptions import *
from grid2op.Rules import RulesChecker
from grid2op.MakeEnv import make
from grid2op.Rules import AlwaysLegal
from grid2op.Action._backendAction import _BackendAction
import pdb
class MakeBackend(ABC):
@abstractmethod
def make_backend(self, detailed_infos_for_cascading_failures=False):
pass
def get_path(self):
raise NotImplementedError(
"This function should be implemented for the test suit you are developping"
)
def get_casefile(self):
raise NotImplementedError(
"This function should be implemented for the test suit you are developping"
)
def skip_if_needed(self):
if hasattr(self, "tests_skipped"):
nm_ = inspect.currentframe().f_back.f_code.co_name
if nm_ in self.tests_skipped:
self.skipTest('the test "{}" is skipped'.format(nm_))
class BaseTestNames(MakeBackend):
def test_properNames(self):
self.skip_if_needed()
backend = self.make_backend()
path = self.get_path()
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with make(
os.path.join(path, "5bus_example_diff_name"),
backend=backend,
_add_to_name="_BaseTestNames",
) as env:
obs = env.reset()
assert np.all(type(obs).name_load == ["tutu", "toto", "tata"])
assert np.all(type(env).name_load == ["tutu", "toto", "tata"])
class BaseTestLoadingCase(MakeBackend):
def test_load_file(self):
backend = self.make_backend()
path_matpower = self.get_path()
case_file = self.get_casefile()
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
backend.load_grid(path_matpower, case_file)
type(backend).set_env_name("BaseTestLoadingCase")
backend.assert_grid_correct()
assert backend.n_line == 20
assert backend.n_gen == 5
assert backend.n_load == 11
assert backend.n_sub == 14
name_line = [
"0_1_0",
"0_4_1",
"8_9_2",
"8_13_3",
"9_10_4",
"11_12_5",
"12_13_6",
"1_2_7",
"1_3_8",
"1_4_9",
"2_3_10",
"3_4_11",
"5_10_12",
"5_11_13",
"5_12_14",
"3_6_15",
"3_8_16",
"4_5_17",
"6_7_18",
"6_8_19",
]
name_line = np.array(name_line)
assert np.all(sorted(backend.name_line) == sorted(name_line))
name_sub = [
"sub_0",
"sub_1",
"sub_2",
"sub_3",
"sub_4",
"sub_5",
"sub_6",
"sub_7",
"sub_8",
"sub_9",
"sub_10",
"sub_11",
"sub_12",
"sub_13",
]
name_sub = np.array(name_sub)
assert np.all(sorted(backend.name_sub) == sorted(name_sub))
name_gen = ["gen_0_4", "gen_1_0", "gen_2_1", "gen_5_2", "gen_7_3"]
name_gen = np.array(name_gen)
assert np.all(sorted(backend.name_gen) == sorted(name_gen))
name_load = [
"load_1_0",
"load_2_1",
"load_13_2",
"load_3_3",
"load_4_4",
"load_5_5",
"load_8_6",
"load_9_7",
"load_10_8",
"load_11_9",
"load_12_10",
]
name_load = np.array(name_load)
assert np.all(sorted(backend.name_load) == sorted(name_load))
assert np.all(backend.get_topo_vect() == np.ones(np.sum(backend.sub_info)))
conv = backend.runpf()
assert conv, "powerflow diverge it is not supposed to!"
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
p_subs, q_subs, p_bus, q_bus, v_bus = backend.check_kirchoff()
assert np.max(np.abs(p_subs)) <= self.tolvect
assert np.max(np.abs(p_bus.flatten())) <= self.tolvect
if backend.shunts_data_available:
assert np.max(np.abs(q_subs)) <= self.tolvect
assert np.max(np.abs(q_bus.flatten())) <= self.tolvect
def test_assert_grid_correct(self):
backend = self.make_backend()
path_matpower = self.get_path()
case_file = self.get_casefile()
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
backend.load_grid(path_matpower, case_file)
type(backend).set_env_name("TestLoadingCase_env2_test_assert_grid_correct")
backend.assert_grid_correct()
conv = backend.runpf()
assert conv, "powerflow diverge it is not supposed to!"
backend.assert_grid_correct_after_powerflow()
class BaseTestLoadingBackendFunc(MakeBackend):
def setUp(self):
self.backend = self.make_backend()
self.path_matpower = self.get_path()
self.case_file = self.get_casefile()
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.backend.load_grid(self.path_matpower, self.case_file)
type(self.backend).set_env_name("TestLoadingBackendFunc_env")
type(self.backend).set_no_storage()
self.backend.assert_grid_correct()
self.game_rules = RulesChecker()
self.action_env_class = ActionSpace.init_grid(self.backend)
self.action_env = self.action_env_class(
gridobj=self.backend, legal_action=self.game_rules.legal_action
)
self.bkact_class = _BackendAction.init_grid(self.backend)
self.backend.runpf()
self.backend.assert_grid_correct_after_powerflow()
def tearDown(self):
pass
def test_theta_ok(self):
self.skip_if_needed()
if self.backend.can_output_theta:
(
theta_or,
theta_ex,
load_theta,
gen_theta,
storage_theta,
) = self.backend.get_theta()
assert theta_or.shape[0] == self.backend.n_line
assert theta_ex.shape[0] == self.backend.n_line
assert load_theta.shape[0] == self.backend.n_load
assert gen_theta.shape[0] == self.backend.n_gen
assert storage_theta.shape[0] == self.backend.n_storage
assert np.all(np.isfinite(theta_or))
assert np.all(np.isfinite(theta_ex))
assert np.all(np.isfinite(load_theta))
assert np.all(np.isfinite(gen_theta))
assert np.all(np.isfinite(storage_theta))
else:
with self.assertRaises(NotImplementedError):
# if the "can_output_theta" flag is set to false, then it means the backend
# should not implement the get_theta class
self.backend.get_theta()
def test_runpf_dc(self):
self.skip_if_needed()
conv = self.backend.runpf(is_dc=True)
assert conv
true_values_dc = np.array(
[
147.83859556,
71.16140444,
5.7716542,
9.64132512,
-3.2283458,
1.50735814,
5.25867488,
70.01463596,
55.1518527,
40.9721069,
-24.18536404,
-61.74649065,
6.7283458,
7.60735814,
17.25131674,
28.36115279,
16.55182652,
42.78702069,
0.0,
28.36115279,
]
)
p_or, *_ = self.backend.lines_or_info()
assert self.compare_vect(p_or, true_values_dc)
def test_runpf(self):
self.skip_if_needed()
true_values_ac = np.array(
[
1.56882891e02,
7.55103818e01,
5.22755247e00,
9.42638103e00,
-3.78532238e00,
1.61425777e00,
5.64385098e00,
7.32375792e01,
5.61314959e01,
4.15162150e01,
-2.32856901e01,
-6.11582304e01,
7.35327698e00,
7.78606702e00,
1.77479769e01,
2.80741759e01,
1.60797576e01,
4.40873209e01,
-1.11022302e-14,
2.80741759e01,
]
)
conv = self.backend.runpf(is_dc=False)
assert conv
p_or, *_ = self.backend.lines_or_info()
assert self.compare_vect(p_or, true_values_ac)
def test_voltage_convert_powerlines(self):
self.skip_if_needed()
# i have the correct voltages in powerlines if the formula to link mw, mvar, kv and amps is correct
conv = self.backend.runpf(is_dc=False)
assert conv, "powerflow diverge at loading"
p_or, q_or, v_or, a_or = self.backend.lines_or_info()
a_th = np.sqrt(p_or**2 + q_or**2) * 1e3 / (np.sqrt(3) * v_or)
assert self.compare_vect(a_th, a_or)
p_ex, q_ex, v_ex, a_ex = self.backend.lines_ex_info()
a_th = np.sqrt(p_ex**2 + q_ex**2) * 1e3 / (np.sqrt(3) * v_ex)
assert self.compare_vect(a_th, a_ex)
def test_voltages_correct_load_gen(self):
self.skip_if_needed()
# i have the right voltages to generators and load, if it's the same as the voltage (correct from the above test)
# of the powerline connected to it.
conv = self.backend.runpf(is_dc=False)
assert conv, "powerflow diverge at loading"
load_p, load_q, load_v = self.backend.loads_info()
gen_p, gen__q, gen_v = self.backend.generators_info()
p_or, q_or, v_or, a_or = self.backend.lines_or_info()
p_ex, q_ex, v_ex, a_ex = self.backend.lines_ex_info()
for c_id, sub_id in enumerate(self.backend.load_to_subid):
l_ids = np.where(self.backend.line_or_to_subid == sub_id)[0]
if len(l_ids):
l_id = l_ids[0]
assert (
np.abs(v_or[l_id] - load_v[c_id]) <= self.tol_one
), "problem for load {}".format(c_id)
continue
l_ids = np.where(self.backend.line_ex_to_subid == sub_id)[0]
if len(l_ids):
l_id = l_ids[0]
assert (
np.abs(v_ex[l_id] - load_v[c_id]) <= self.tol_one
), "problem for load {}".format(c_id)
continue
assert False, "load {} has not been checked".format(c_id)
for g_id, sub_id in enumerate(self.backend.gen_to_subid):
l_ids = np.where(self.backend.line_or_to_subid == sub_id)[0]
if len(l_ids):
l_id = l_ids[0]
assert (
np.abs(v_or[l_id] - gen_v[g_id]) <= self.tol_one
), "problem for generator {}".format(g_id)
continue
l_ids = np.where(self.backend.line_ex_to_subid == sub_id)[0]
if len(l_ids):
l_id = l_ids[0]
assert (
np.abs(v_ex[l_id] - gen_v[g_id]) <= self.tol_one
), "problem for generator {}".format(g_id)
continue
assert False, "generator {} has not been checked".format(g_id)
def test_copy(self):
self.skip_if_needed()
conv = self.backend.runpf(is_dc=False)
assert conv, "powerflow diverge at loading"
l_id = 3
p_or_orig, *_ = self.backend.lines_or_info()
adn_backend_cpy = self.backend.copy()
self.backend._disconnect_line(l_id)
conv = self.backend.runpf(is_dc=False)
assert conv
conv2 = adn_backend_cpy.runpf(is_dc=False)
assert conv2
p_or_ref, *_ = self.backend.lines_or_info()
p_or, *_ = adn_backend_cpy.lines_or_info()
assert self.compare_vect(
p_or_orig, p_or
), "the copied object affects its original 'parent'"
assert (
np.abs(p_or_ref[l_id]) <= self.tol_one
), "powerline {} has not been disconnected".format(l_id)
def test_copy2(self):
self.skip_if_needed()
self.backend._disconnect_line(8)
conv = self.backend.runpf(is_dc=False)
p_or_orig, *_ = self.backend.lines_or_info()
adn_backend_cpy = self.backend.copy()
adn_backend_cpy._disconnect_line(11)
assert not adn_backend_cpy.get_line_status()[8]
assert not adn_backend_cpy.get_line_status()[11]
assert not self.backend.get_line_status()[8]
assert self.backend.get_line_status()[11]
def test_get_private_line_status(self):
self.skip_if_needed()
if hasattr(self.backend, "_get_line_status"):
assert np.all(self.backend._get_line_status())
else:
assert np.all(self.backend.get_line_status())
self.backend._disconnect_line(3)
if hasattr(self.backend, "_get_line_status"):
vect_ = self.backend._get_line_status()
else:
vect_ = self.backend.get_line_status()
assert np.sum(~vect_) == 1
assert not vect_[3]
def test_get_line_flow(self):
self.skip_if_needed()
self.backend.runpf(is_dc=False)
true_values_ac = np.array(
[
-20.40429168,
3.85499114,
4.2191378,
3.61000624,
-1.61506292,
0.75395917,
1.74717378,
3.56020295,
-1.5503504,
1.17099786,
4.47311562,
15.82364194,
3.56047297,
2.50341424,
7.21657539,
-9.68106571,
-0.42761118,
12.47067981,
-17.16297051,
5.77869057,
]
)
p_or_orig, q_or_orig, *_ = self.backend.lines_or_info()
assert self.compare_vect(q_or_orig, true_values_ac)
self.backend._disconnect_line(3)
a = self.backend.runpf(is_dc=False)
true_values_ac = np.array(
[
-20.40028207,
3.65600775,
3.77916284,
0.0,
-2.10761554,
1.34025308,
5.86505081,
3.58514625,
-2.28717836,
0.81979017,
3.72328838,
17.09556423,
3.9548798,
3.18389804,
11.24144925,
-11.09660174,
-1.70423701,
13.14347167,
-14.82917601,
2.276297,
]
)
p_or_orig, q_or_orig, *_ = self.backend.lines_or_info()
assert self.compare_vect(q_or_orig, true_values_ac)
def test_pf_ac_dc(self):
self.skip_if_needed()
true_values_ac = np.array(
[
-20.40429168,
3.85499114,
4.2191378,
3.61000624,
-1.61506292,
0.75395917,
1.74717378,
3.56020295,
-1.5503504,
1.17099786,
4.47311562,
15.82364194,
3.56047297,
2.50341424,
7.21657539,
-9.68106571,
-0.42761118,
12.47067981,
-17.16297051,
5.77869057,
]
)
conv = self.backend.runpf(is_dc=True)
assert conv
p_or_orig, q_or_orig, *_ = self.backend.lines_or_info()
assert np.all(q_or_orig == 0.0), "in dc mode all q must be zero"
conv = self.backend.runpf(is_dc=False)
assert conv
p_or_orig, q_or_orig, *_ = self.backend.lines_or_info()
assert self.compare_vect(q_or_orig, true_values_ac)
def test_get_thermal_limit(self):
self.skip_if_needed()
res = self.backend.get_thermal_limit()
true_values_ac = np.array(
[
42339.01974057,
42339.01974057,
27479652.23546777,
27479652.23546777,
27479652.23546777,
27479652.23546777,
27479652.23546777,
42339.01974057,
42339.01974057,
42339.01974057,
42339.01974057,
42339.01974057,
27479652.23546777,
27479652.23546777,
27479652.23546777,
42339.01974057,
42339.01974057,
42339.01974057,
408269.11892695,
408269.11892695,
],
dtype=dt_float,
)
assert self.compare_vect(res, true_values_ac)
def test_disconnect_line(self):
self.skip_if_needed()
for i in range(self.backend.n_line):
if i == 18:
# powerflow diverge if line 1 is removed, unfortunately
continue
backend_cpy = self.backend.copy()
backend_cpy._disconnect_line(i)
conv = backend_cpy.runpf()
assert (
conv
), "Power flow computation does not converge if line {} is removed".format(
i
)
flows = backend_cpy.get_line_status()
assert not flows[i]
assert np.sum(~flows) == 1
def test_donothing_action(self):
self.skip_if_needed()
conv = self.backend.runpf()
init_flow = self.backend.get_line_flow()
init_lp, *_ = self.backend.loads_info()
init_gp, *_ = self.backend.generators_info()
init_ls = self.backend.get_line_status()
action = self.action_env({}) # update the action
bk_action = self.bkact_class()
bk_action += action
self.backend.apply_action(bk_action)
after_lp, *_ = self.backend.loads_info()
after_gp, *_ = self.backend.generators_info()
after_ls = self.backend.get_line_status()
assert self.compare_vect(init_lp, after_lp) # check i didn't modify the loads
# assert self.compare_vect(init_gp, after_gp) # check i didn't modify the generators # TODO here !!! problem with steady state P=C+L
assert np.all(init_ls == after_ls) # check i didn't disconnect any powerlines
conv = self.backend.runpf()
assert conv, "Cannot perform a powerflow after doing nothing"
after_flow = self.backend.get_line_flow()
assert self.compare_vect(init_flow, after_flow)
def test_apply_action_active_value(self):
self.skip_if_needed()
# test that i can modify only the load / prod active values of the powergrid
# to do that i modify the productions and load all of a factor 0.5 and compare that the DC flows are
# also multiply by 2
# i set up the stuff to have exactly 0 losses
conv = self.backend.runpf(is_dc=True)
assert conv, "powergrid diverge after loading (even in DC)"
init_flow, *_ = self.backend.lines_or_info()
init_lp, init_l_q, *_ = self.backend.loads_info()
init_gp, *_ = self.backend.generators_info()
init_ls = self.backend.get_line_status()
ratio = 1.0
new_cp = ratio * init_lp
new_pp = ratio * init_gp * np.sum(init_lp) / np.sum(init_gp)
action = self.action_env(
{"injection": {"load_p": new_cp, "prod_p": new_pp}}
) # update the action
bk_action = self.bkact_class()
bk_action += action
self.backend.apply_action(bk_action)
conv = self.backend.runpf(is_dc=True)
# now the system has exactly 0 losses (ie sum load = sum gen)
# i check that if i divide by 2, then everything is divided by 2
assert conv
init_flow, *_ = self.backend.lines_or_info()
init_lp, init_l_q, *_ = self.backend.loads_info()
init_gp, *_ = self.backend.generators_info()
init_ls = self.backend.get_line_status()
ratio = 0.5
new_cp = ratio * init_lp
new_pp = ratio * init_gp
action = self.action_env(
{"injection": {"load_p": new_cp, "prod_p": new_pp}}
) # update the action
bk_action = self.bkact_class()
bk_action += action
self.backend.apply_action(bk_action)
conv = self.backend.runpf(is_dc=True)
assert conv, "Cannot perform a powerflow after doing nothing"
after_lp, after_lq, *_ = self.backend.loads_info()
after_gp, *_ = self.backend.generators_info()
after_ls = self.backend.get_line_status()
assert self.compare_vect(new_cp, after_lp) # check i didn't modify the loads
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
p_subs, q_subs, p_bus, q_bus, v_bus = self.backend.check_kirchoff()
# i'm in DC mode, i can't check for reactive values...
assert (
np.max(np.abs(p_subs)) <= self.tolvect
), "problem with active values, at substation"
assert (
np.max(np.abs(p_bus.flatten())) <= self.tolvect
), "problem with active values, at a bus"
assert self.compare_vect(
new_pp, after_gp
) # check i didn't modify the generators
assert np.all(init_ls == after_ls) # check i didn't disconnect any powerlines
after_flow, *_ = self.backend.lines_or_info()
assert self.compare_vect(
ratio * init_flow, after_flow
) # probably an error with the DC approx
def test_apply_action_prod_v(self):
self.skip_if_needed()
conv = self.backend.runpf(is_dc=False)
assert conv, "powergrid diverge after loading"
prod_p_init, prod_q_init, prod_v_init = self.backend.generators_info()
ratio = 1.05
action = self.action_env(
{"injection": {"prod_v": ratio * prod_v_init}}
) # update the action
bk_action = self.bkact_class()
bk_action += action
self.backend.apply_action(bk_action)
conv = self.backend.runpf(is_dc=False)
assert conv, "Cannot perform a powerflow after modifying the powergrid"
prod_p_after, prod_q_after, prod_v_after = self.backend.generators_info()
assert self.compare_vect(
ratio * prod_v_init, prod_v_after
) # check i didn't modify the generators
def test_apply_action_maintenance(self):
self.skip_if_needed()
# retrieve some initial data to be sure only a subpart of the _grid is modified
conv = self.backend.runpf()
init_lp, *_ = self.backend.loads_info()
init_gp, *_ = self.backend.generators_info()
# check that maintenance vector is properly taken into account
maintenance = np.full((self.backend.n_line,), fill_value=False, dtype=dt_bool)
maintenance[19] = True
action = self.action_env({"maintenance": maintenance}) # update the action
bk_action = self.bkact_class()
bk_action += action
# apply the action here
self.backend.apply_action(bk_action)
# compute a load flow an performs more tests
conv = self.backend.runpf()
assert conv, "Power does not converge if line {} is removed".format(19)
# performs basic check
after_lp, *_ = self.backend.loads_info()
after_gp, *_ = self.backend.generators_info()
after_ls = self.backend.get_line_status()
assert self.compare_vect(init_lp, after_lp) # check i didn't modify the loads
# assert self.compare_vect(init_gp, after_gp) # check i didn't modify the generators # TODO here problem with steady state P=C+L
assert np.all(
~maintenance == after_ls
) # check i didn't disconnect any powerlines beside the correct one
flows = self.backend.get_line_status()
assert np.sum(~flows) == 1
assert not flows[19]
def test_apply_action_hazard(self):
self.skip_if_needed()
conv = self.backend.runpf()
assert conv, "powerflow did not converge at iteration 0"
init_lp, *_ = self.backend.loads_info()
init_gp, *_ = self.backend.generators_info()
# check that maintenance vector is properly taken into account
maintenance = np.full((self.backend.n_line,), fill_value=False, dtype=dt_bool)
maintenance[17] = True
action = self.action_env({"hazards": maintenance}) # update the action
bk_action = self.bkact_class()
bk_action += action
# apply the action here
self.backend.apply_action(bk_action)
# compute a load flow an performs more tests
conv = self.backend.runpf()
assert conv, "Power does not converge if line {} is removed".format(19)
# performs basic check
after_lp, *_ = self.backend.loads_info()
after_gp, *_ = self.backend.generators_info()
after_ls = self.backend.get_line_status()
assert self.compare_vect(init_lp, after_lp) # check i didn't modify the loads
# assert self.compare_vect(init_gp, after_gp) # check i didn't modify the generators # TODO here problem with steady state P=C+L
assert np.all(
maintenance == ~after_ls
) # check i didn't disconnect any powerlines beside the correct one
def test_apply_action_disconnection(self):
self.skip_if_needed()
# retrieve some initial data to be sure only a subpart of the _grid is modified
conv = self.backend.runpf()
init_lp, *_ = self.backend.loads_info()
init_gp, *_ = self.backend.generators_info()
# check that maintenance vector is properly taken into account
maintenance = np.full((self.backend.n_line,), fill_value=False, dtype=dt_bool)
maintenance[19] = True
disc = np.full((self.backend.n_line,), fill_value=False, dtype=dt_bool)
disc[17] = True
action = self.action_env(
{"hazards": disc, "maintenance": maintenance}
) # update the action
bk_action = self.bkact_class()
bk_action += action
# apply the action here
self.backend.apply_action(bk_action)
# compute a load flow an performs more tests
conv = self.backend.runpf()
assert (
conv
), "Powerflow does not converge if lines {} and {} are removed".format(17, 19)
# performs basic check
after_lp, *_ = self.backend.loads_info()
after_gp, *_ = self.backend.generators_info()
after_ls = self.backend.get_line_status()
assert self.compare_vect(init_lp, after_lp) # check i didn't modify the loads
# assert self.compare_vect(init_gp, after_gp) # check i didn't modify the generators # TODO here problem with steady state, P=C+L
assert np.all(
disc | maintenance == ~after_ls
) # check i didn't disconnect any powerlines beside the correct one
flows = self.backend.get_line_status()
assert np.sum(~flows) == 2
assert not flows[19]
assert not flows[17]
class BaseTestTopoAction(MakeBackend):
def setUp(self):
self.backend = self.make_backend()
self.path_matpower = self.get_path()
self.case_file = self.get_casefile()
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.backend.load_grid(self.path_matpower, self.case_file)
type(self.backend).set_env_name("BaseTestTopoAction")
type(self.backend).set_no_storage()
self.backend.assert_grid_correct()
self.game_rules = RulesChecker()
as_class = ActionSpace.init_grid(self.backend)
self.helper_action = as_class(
gridobj=self.backend, legal_action=self.game_rules.legal_action
)
self.bkact_class = _BackendAction.init_grid(self.backend)
def tearDown(self):
pass
def compare_vect(self, pred, true):
return np.max(np.abs(pred - true)) <= self.tolvect
def _check_kirchoff(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
p_subs, q_subs, p_bus, q_bus, v_bus = self.backend.check_kirchoff()
assert (
np.max(np.abs(p_subs)) <= self.tolvect
), "problem with active values, at substation"
assert (
np.max(np.abs(p_bus.flatten())) <= self.tolvect
), "problem with active values, at a bus"
if self.backend.shunts_data_available:
assert (
np.max(np.abs(q_subs)) <= self.tolvect
), "problem with reactive values, at substation"
assert (
np.max(np.abs(q_bus.flatten())) <= self.tolvect
), "problem with reaactive values, at a bus"
def test_get_topo_vect_speed(self):
# retrieve some initial data to be sure only a subpart of the _grid is modified
self.skip_if_needed()
conv = self.backend.runpf()
init_amps_flow = self.backend.get_line_flow()
# check that maintenance vector is properly taken into account
arr = np.array([1, 1, 1, 2, 2, 2], dtype=dt_int)
id_ = 1
action = self.helper_action({"set_bus": {"substations_id": [(id_, arr)]}})
bk_action = self.bkact_class()
bk_action += action
# apply the action here
self.backend.apply_action(bk_action)
conv = self.backend.runpf()
assert conv
after_amps_flow = self.backend.get_line_flow()
topo_vect = self.backend.get_topo_vect()
topo_vect_old = np.array(
[
1,
1,
1,
1,
1,
1,
2,
2,
2,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
],
dtype=dt_int,
)
assert self.compare_vect(topo_vect, topo_vect_old)
def test_topo_set1sub(self):
# retrieve some initial data to be sure only a subpart of the _grid is modified
self.skip_if_needed()
conv = self.backend.runpf()
init_amps_flow = self.backend.get_line_flow()
# check that maintenance vector is properly taken into account
arr = np.array([1, 1, 1, 2, 2, 2], dtype=dt_int)
id_ = 1
action = self.helper_action({"set_bus": {"substations_id": [(id_, arr)]}})
bk_action = self.bkact_class()
bk_action += action
# apply the action here
self.backend.apply_action(bk_action)
conv = self.backend.runpf()
assert conv
after_amps_flow = self.backend.get_line_flow()
topo_vect = self.backend.get_topo_vect()
assert np.min(topo_vect) == 1, "all buses have been changed"
assert np.max(topo_vect) == 2, "no buses have been changed"
# check that the objects have been properly moved
load_ids = np.where(self.backend.load_to_subid == id_)[0]
assert np.all(
topo_vect[self.backend.load_pos_topo_vect[load_ids]]
== arr[self.backend.load_to_sub_pos[load_ids]]
)
lor_ids = np.where(self.backend.line_or_to_subid == id_)[0]
assert np.all(
topo_vect[self.backend.line_or_pos_topo_vect[lor_ids]]
== arr[self.backend.line_or_to_sub_pos[lor_ids]]
)
lex_ids = np.where(self.backend.line_ex_to_subid == id_)[0]
assert np.all(
topo_vect[self.backend.line_ex_pos_topo_vect[lex_ids]]
== arr[self.backend.line_ex_to_sub_pos[lex_ids]]
)
gen_ids = np.where(self.backend.gen_to_subid == id_)[0]
assert np.all(
topo_vect[self.backend.gen_pos_topo_vect[gen_ids]]
== arr[self.backend.gen_to_sub_pos[gen_ids]]
)
after_amps_flow_th = np.array(
[
6.38865247e02,
3.81726828e02,
1.78001287e04,
2.70742428e04,
1.06755055e04,
4.71160165e03,
1.52265925e04,
3.37755751e02,
3.00535519e02,
5.01164454e-13,
7.01900962e01,
1.73874580e02,
2.08904697e04,
2.11757439e04,
4.93863382e04,
1.31935835e02,
6.99779475e01,
1.85068609e02,
7.47283039e02,
1.14125596e03,
]
)
after_amps_flow_th = np.array(
[
596.58386539,