ltbams 1.0.12__py3-none-any.whl → 1.0.13__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ams/_version.py +3 -3
- ams/core/matprocessor.py +170 -104
- ams/io/matpower.py +4 -0
- ams/io/psse.py +2 -0
- ams/opt/exprcalc.py +11 -0
- ams/routines/grbopt.py +2 -0
- ams/routines/pypower.py +8 -0
- ams/routines/routine.py +118 -1
- ams/shared.py +30 -2
- ams/system.py +10 -0
- docs/source/index.rst +4 -3
- docs/source/release-notes.rst +8 -0
- {ltbams-1.0.12.dist-info → ltbams-1.0.13.dist-info}/METADATA +4 -2
- {ltbams-1.0.12.dist-info → ltbams-1.0.13.dist-info}/RECORD +17 -45
- {ltbams-1.0.12.dist-info → ltbams-1.0.13.dist-info}/top_level.txt +0 -1
- tests/__init__.py +0 -0
- tests/test_1st_system.py +0 -64
- tests/test_addressing.py +0 -40
- tests/test_case.py +0 -301
- tests/test_cli.py +0 -34
- tests/test_export_csv.py +0 -89
- tests/test_group.py +0 -83
- tests/test_interface.py +0 -238
- tests/test_io.py +0 -190
- tests/test_jumper.py +0 -27
- tests/test_known_good.py +0 -267
- tests/test_matp.py +0 -437
- tests/test_model.py +0 -54
- tests/test_omodel.py +0 -119
- tests/test_paths.py +0 -89
- tests/test_report.py +0 -251
- tests/test_repr.py +0 -21
- tests/test_routine.py +0 -178
- tests/test_rtn_acopf.py +0 -75
- tests/test_rtn_dcopf.py +0 -121
- tests/test_rtn_dcopf2.py +0 -103
- tests/test_rtn_ed.py +0 -279
- tests/test_rtn_opf.py +0 -142
- tests/test_rtn_pflow.py +0 -147
- tests/test_rtn_pypower.py +0 -315
- tests/test_rtn_rted.py +0 -273
- tests/test_rtn_uc.py +0 -248
- tests/test_service.py +0 -73
- {ltbams-1.0.12.dist-info → ltbams-1.0.13.dist-info}/WHEEL +0 -0
- {ltbams-1.0.12.dist-info → ltbams-1.0.13.dist-info}/entry_points.txt +0 -0
tests/test_known_good.py
DELETED
@@ -1,267 +0,0 @@
|
|
1
|
-
import logging
|
2
|
-
import unittest
|
3
|
-
|
4
|
-
import json
|
5
|
-
|
6
|
-
import numpy as np
|
7
|
-
|
8
|
-
from andes.shared import rad2deg
|
9
|
-
import ams
|
10
|
-
from ams.shared import nan
|
11
|
-
|
12
|
-
logger = logging.getLogger(__name__)
|
13
|
-
|
14
|
-
|
15
|
-
class TestKnownResults(unittest.TestCase):
|
16
|
-
|
17
|
-
def setUp(self) -> None:
|
18
|
-
with open(ams.get_case('matpower/benchmark.json'), 'r') as file:
|
19
|
-
self.mpres = json.load(file)
|
20
|
-
|
21
|
-
self.sp = ams.load(ams.get_case('matpower/case14.m'),
|
22
|
-
setup=True, no_output=True, default_config=True)
|
23
|
-
|
24
|
-
def test_DCPF_case14(self):
|
25
|
-
"""
|
26
|
-
Test DC power flow for case14.
|
27
|
-
"""
|
28
|
-
self.sp.DCPF.run()
|
29
|
-
np.testing.assert_allclose(self.sp.DCPF.aBus.v * rad2deg,
|
30
|
-
np.array(self.mpres['case14']['DCPF']['aBus']).reshape(-1),
|
31
|
-
rtol=1e-2, atol=1e-2)
|
32
|
-
|
33
|
-
np.testing.assert_allclose(self.sp.DCPF.pg.v * self.sp.config.mva,
|
34
|
-
np.array(self.mpres['case14']['DCPF']['pg']).reshape(-1),
|
35
|
-
rtol=1e-2, atol=1e-2)
|
36
|
-
|
37
|
-
def test_PFlow_case14(self):
|
38
|
-
"""
|
39
|
-
Test power flow for case14.
|
40
|
-
"""
|
41
|
-
self.sp.PFlow.run()
|
42
|
-
np.testing.assert_allclose(self.sp.PFlow.aBus.v * rad2deg,
|
43
|
-
np.array(self.mpres['case14']['PFlow']['aBus']).reshape(-1),
|
44
|
-
rtol=1e-2, atol=1e-2)
|
45
|
-
np.testing.assert_allclose(self.sp.PFlow.vBus.v,
|
46
|
-
np.array(self.mpres['case14']['PFlow']['vBus']).reshape(-1),
|
47
|
-
rtol=1e-2, atol=1e-2)
|
48
|
-
np.testing.assert_allclose(self.sp.PFlow.pg.v.sum() * self.sp.config.mva,
|
49
|
-
np.array(self.mpres['case14']['PFlow']['pg']).sum(),
|
50
|
-
rtol=1e-2, atol=1e-2)
|
51
|
-
|
52
|
-
def test_DCOPF_case14(self):
|
53
|
-
"""
|
54
|
-
Test DCOPF for case14.
|
55
|
-
"""
|
56
|
-
self.sp.DCOPF.run(solver='CLARABEL')
|
57
|
-
self.assertAlmostEqual(self.sp.DCOPF.obj.v,
|
58
|
-
self.mpres['case14']['DCOPF']['obj'],
|
59
|
-
places=4)
|
60
|
-
np.testing.assert_allclose(self.sp.DCOPF.pi.v / self.sp.config.mva,
|
61
|
-
np.array(self.mpres['case14']['DCOPF']['pi']).reshape(-1),
|
62
|
-
rtol=1e-2, atol=1e-2)
|
63
|
-
|
64
|
-
def test_Matrices_case14(self):
|
65
|
-
"""
|
66
|
-
Test matrices for case14.
|
67
|
-
"""
|
68
|
-
ptdf = self.sp.mats.build_ptdf()
|
69
|
-
lodf = self.sp.mats.build_lodf()
|
70
|
-
|
71
|
-
ptdf_mp = load_ptdf(self.mpres, 'case14')
|
72
|
-
lodf_mp = load_lodf(self.mpres, 'case14')
|
73
|
-
|
74
|
-
ptdf[np.isnan(ptdf_mp)] = nan
|
75
|
-
lodf[np.isnan(lodf_mp)] = nan
|
76
|
-
|
77
|
-
np.testing.assert_allclose(ptdf, ptdf_mp,
|
78
|
-
equal_nan=True, rtol=1e-2, atol=1e-2)
|
79
|
-
|
80
|
-
np.testing.assert_allclose(lodf, lodf_mp,
|
81
|
-
equal_nan=True, rtol=1e-2, atol=1e-2)
|
82
|
-
|
83
|
-
|
84
|
-
class TestKnownResultsIEEE39(unittest.TestCase):
|
85
|
-
|
86
|
-
def setUp(self) -> None:
|
87
|
-
with open(ams.get_case('matpower/benchmark.json'), 'r') as file:
|
88
|
-
self.mpres = json.load(file)
|
89
|
-
|
90
|
-
self.sp = ams.load(ams.get_case('matpower/case39.m'),
|
91
|
-
setup=True, no_output=True, default_config=True)
|
92
|
-
|
93
|
-
def test_DCPF_case39(self):
|
94
|
-
"""
|
95
|
-
Test DC power flow for case39.
|
96
|
-
"""
|
97
|
-
self.sp.DCPF.run()
|
98
|
-
np.testing.assert_allclose(self.sp.DCPF.aBus.v * rad2deg,
|
99
|
-
np.array(self.mpres['case39']['DCPF']['aBus']).reshape(-1),
|
100
|
-
rtol=1e-2, atol=1e-2)
|
101
|
-
|
102
|
-
np.testing.assert_allclose(self.sp.DCPF.pg.v.sum() * self.sp.config.mva,
|
103
|
-
np.array(self.mpres['case39']['DCPF']['pg']).sum(),
|
104
|
-
rtol=1e-2, atol=1e-2)
|
105
|
-
|
106
|
-
def test_PFlow_case39(self):
|
107
|
-
"""
|
108
|
-
Test power flow for case39.
|
109
|
-
"""
|
110
|
-
self.sp.PFlow.run()
|
111
|
-
np.testing.assert_allclose(self.sp.PFlow.aBus.v * rad2deg,
|
112
|
-
np.array(self.mpres['case39']['PFlow']['aBus']).reshape(-1),
|
113
|
-
rtol=1e-2, atol=1e-2)
|
114
|
-
np.testing.assert_allclose(self.sp.PFlow.vBus.v,
|
115
|
-
np.array(self.mpres['case39']['PFlow']['vBus']).reshape(-1),
|
116
|
-
rtol=1e-2, atol=1e-2)
|
117
|
-
np.testing.assert_allclose(self.sp.PFlow.pg.v.sum() * self.sp.config.mva,
|
118
|
-
np.array(self.mpres['case39']['PFlow']['pg']).sum(),
|
119
|
-
rtol=1e-2, atol=1e-2)
|
120
|
-
|
121
|
-
def test_DCOPF_case39(self):
|
122
|
-
"""
|
123
|
-
Test DCOPF for case39.
|
124
|
-
"""
|
125
|
-
self.sp.DCOPF.run(solver='CLARABEL')
|
126
|
-
self.assertAlmostEqual(self.sp.DCOPF.obj.v,
|
127
|
-
self.mpres['case39']['DCOPF']['obj'],
|
128
|
-
places=2)
|
129
|
-
np.testing.assert_allclose(self.sp.DCOPF.pi.v / self.sp.config.mva,
|
130
|
-
np.array(self.mpres['case39']['DCOPF']['pi']).reshape(-1),
|
131
|
-
rtol=1e-2, atol=1e-2)
|
132
|
-
|
133
|
-
def test_Matrices_case39(self):
|
134
|
-
"""
|
135
|
-
Test matrices for case39.
|
136
|
-
"""
|
137
|
-
ptdf = self.sp.mats.build_ptdf()
|
138
|
-
lodf = self.sp.mats.build_lodf()
|
139
|
-
|
140
|
-
ptdf_mp = load_ptdf(self.mpres, 'case39')
|
141
|
-
lodf_mp = load_lodf(self.mpres, 'case39')
|
142
|
-
|
143
|
-
ptdf[np.isnan(ptdf_mp)] = nan
|
144
|
-
lodf[np.isnan(lodf_mp)] = nan
|
145
|
-
|
146
|
-
np.testing.assert_allclose(ptdf, ptdf_mp,
|
147
|
-
equal_nan=True, rtol=1e-2, atol=1e-2)
|
148
|
-
|
149
|
-
np.testing.assert_allclose(lodf, lodf_mp,
|
150
|
-
equal_nan=True, rtol=1e-2, atol=10)
|
151
|
-
|
152
|
-
|
153
|
-
class TestKnownResultsIEEE118(unittest.TestCase):
|
154
|
-
|
155
|
-
def setUp(self) -> None:
|
156
|
-
with open(ams.get_case('matpower/benchmark.json'), 'r') as file:
|
157
|
-
self.mpres = json.load(file)
|
158
|
-
|
159
|
-
self.sp = ams.load(ams.get_case('matpower/case118.m'),
|
160
|
-
setup=True, no_output=True, default_config=True)
|
161
|
-
|
162
|
-
def test_DCPF_case118(self):
|
163
|
-
"""
|
164
|
-
Test DC power flow for case118.
|
165
|
-
"""
|
166
|
-
self.sp.DCPF.run()
|
167
|
-
aBus_mp = np.array(self.mpres['case118']['DCPF']['aBus']).reshape(-1)
|
168
|
-
aBus_mp -= aBus_mp[0]
|
169
|
-
np.testing.assert_allclose((self.sp.DCPF.aBus.v - self.sp.DCPF.aBus.v[0]) * rad2deg,
|
170
|
-
aBus_mp,
|
171
|
-
rtol=1e-2, atol=1e-2)
|
172
|
-
|
173
|
-
np.testing.assert_allclose(self.sp.DCPF.pg.v.sum() * self.sp.config.mva,
|
174
|
-
np.array(self.mpres['case118']['DCPF']['pg']).sum(),
|
175
|
-
rtol=1e-2, atol=1e-2)
|
176
|
-
|
177
|
-
def test_PFlow_case118(self):
|
178
|
-
"""
|
179
|
-
Test power flow for case118.
|
180
|
-
"""
|
181
|
-
self.sp.PFlow.run()
|
182
|
-
np.testing.assert_allclose(self.sp.PFlow.aBus.v * rad2deg,
|
183
|
-
np.array(self.mpres['case118']['PFlow']['aBus']).reshape(-1),
|
184
|
-
rtol=1e-2, atol=1e-2)
|
185
|
-
np.testing.assert_allclose(self.sp.PFlow.vBus.v,
|
186
|
-
np.array(self.mpres['case118']['PFlow']['vBus']).reshape(-1),
|
187
|
-
rtol=1e-2, atol=1e-2)
|
188
|
-
np.testing.assert_allclose(self.sp.PFlow.pg.v.sum() * self.sp.config.mva,
|
189
|
-
np.array(self.mpres['case118']['PFlow']['pg']).sum(),
|
190
|
-
rtol=1e-2, atol=1e-2)
|
191
|
-
|
192
|
-
def test_DCOPF_case118(self):
|
193
|
-
"""
|
194
|
-
Test DCOPF for case118.
|
195
|
-
"""
|
196
|
-
self.sp.DCOPF.run(solver='CLARABEL')
|
197
|
-
self.assertAlmostEqual(self.sp.DCOPF.obj.v,
|
198
|
-
self.mpres['case118']['DCOPF']['obj'],
|
199
|
-
places=2)
|
200
|
-
np.testing.assert_allclose(self.sp.DCOPF.pi.v / self.sp.config.mva,
|
201
|
-
np.array(self.mpres['case118']['DCOPF']['pi']).reshape(-1),
|
202
|
-
rtol=1e-2, atol=1e-2)
|
203
|
-
|
204
|
-
def test_Matrices_case118(self):
|
205
|
-
"""
|
206
|
-
Test matrices for case118.
|
207
|
-
"""
|
208
|
-
ptdf = self.sp.mats.build_ptdf()
|
209
|
-
lodf = self.sp.mats.build_lodf()
|
210
|
-
|
211
|
-
ptdf_mp = load_ptdf(self.mpres, 'case118')
|
212
|
-
lodf_mp = load_lodf(self.mpres, 'case118')
|
213
|
-
|
214
|
-
ptdf[np.isnan(ptdf_mp)] = nan
|
215
|
-
lodf[np.isnan(lodf_mp)] = nan
|
216
|
-
|
217
|
-
np.testing.assert_allclose(ptdf, ptdf_mp,
|
218
|
-
equal_nan=True, rtol=1e-2, atol=1e-2)
|
219
|
-
|
220
|
-
np.testing.assert_allclose(lodf, lodf_mp,
|
221
|
-
equal_nan=True, rtol=1e-2, atol=10)
|
222
|
-
|
223
|
-
|
224
|
-
def load_ptdf(mpres, case):
|
225
|
-
"""
|
226
|
-
Load PTDF from mpres.
|
227
|
-
|
228
|
-
Parameters
|
229
|
-
----------
|
230
|
-
mpres : dict
|
231
|
-
The result dictionary.
|
232
|
-
case : str
|
233
|
-
The case name.
|
234
|
-
|
235
|
-
Returns
|
236
|
-
-------
|
237
|
-
ptdf : np.ndarray
|
238
|
-
The PTDF matrix.
|
239
|
-
"""
|
240
|
-
ptdf_data = np.array(mpres[case]['PTDF'])
|
241
|
-
ptdf = np.array([[0 if val == "_NaN_" else val for val in row] for row in ptdf_data],
|
242
|
-
dtype=float)
|
243
|
-
return ptdf
|
244
|
-
|
245
|
-
|
246
|
-
def load_lodf(mpres, case):
|
247
|
-
"""
|
248
|
-
Load LODF from mpres.
|
249
|
-
|
250
|
-
Parameters
|
251
|
-
----------
|
252
|
-
mpres : dict
|
253
|
-
The result dictionary.
|
254
|
-
case : str
|
255
|
-
The case name.
|
256
|
-
|
257
|
-
Returns
|
258
|
-
-------
|
259
|
-
lodf : np.ndarray
|
260
|
-
The LODF matrix.
|
261
|
-
"""
|
262
|
-
lodf_data = np.array(mpres[case]['LODF'])
|
263
|
-
lodf = np.array([[nan if val in ["_NaN_", "-_Inf_", "_Inf_"] else val for val in row] for row in lodf_data],
|
264
|
-
dtype=float)
|
265
|
-
# NOTE: force the diagonal to be -1
|
266
|
-
np.fill_diagonal(lodf, -1)
|
267
|
-
return lodf
|
tests/test_matp.py
DELETED
@@ -1,437 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
Test module MatProcessor.
|
3
|
-
"""
|
4
|
-
|
5
|
-
import unittest
|
6
|
-
import os
|
7
|
-
|
8
|
-
import numpy as np
|
9
|
-
|
10
|
-
import ams
|
11
|
-
from ams.core.matprocessor import MatProcessor, MParam
|
12
|
-
from ams.shared import sps
|
13
|
-
|
14
|
-
|
15
|
-
class TestMatProcessorBasic(unittest.TestCase):
|
16
|
-
"""
|
17
|
-
Test basic functionality of MatProcessor.
|
18
|
-
"""
|
19
|
-
|
20
|
-
def setUp(self) -> None:
|
21
|
-
self.ss = ams.load(ams.get_case("matpower/case300.m"),
|
22
|
-
default_config=True, no_output=True)
|
23
|
-
self.nR = self.ss.Zone.n
|
24
|
-
self.nb = self.ss.Bus.n
|
25
|
-
self.nl = self.ss.Line.n
|
26
|
-
self.ng = self.ss.StaticGen.n
|
27
|
-
self.nsh = self.ss.Shunt.n
|
28
|
-
self.nD = self.ss.StaticLoad.n
|
29
|
-
|
30
|
-
self.mats = MatProcessor(self.ss)
|
31
|
-
self.mats.build()
|
32
|
-
|
33
|
-
def test_MParams_owner(self):
|
34
|
-
"""
|
35
|
-
Tesst MParams owner before system initialization.
|
36
|
-
"""
|
37
|
-
self.assertIs(self.mats.Cft.owner, self.mats)
|
38
|
-
self.assertIs(self.mats.CftT.owner, self.mats)
|
39
|
-
self.assertIs(self.mats.Cg.owner, self.mats)
|
40
|
-
self.assertIs(self.mats.Cl.owner, self.mats)
|
41
|
-
self.assertIs(self.mats.Csh.owner, self.mats)
|
42
|
-
self.assertIs(self.mats.Bbus.owner, self.mats)
|
43
|
-
self.assertIs(self.mats.Bf.owner, self.mats)
|
44
|
-
self.assertIs(self.mats.Pbusinj.owner, self.mats)
|
45
|
-
self.assertIs(self.mats.Pfinj.owner, self.mats)
|
46
|
-
self.assertIs(self.mats.PTDF.owner, self.mats)
|
47
|
-
self.assertIs(self.mats.LODF.owner, self.mats)
|
48
|
-
|
49
|
-
def test_MParam_export_csv(self):
|
50
|
-
"""
|
51
|
-
Test MParams export.
|
52
|
-
"""
|
53
|
-
# --- path is not given ---
|
54
|
-
exported_csv = self.mats.Cft.export_csv()
|
55
|
-
self.assertTrue(os.path.exists(exported_csv))
|
56
|
-
os.remove(exported_csv)
|
57
|
-
|
58
|
-
# --- path is given ---
|
59
|
-
path = 'CASE300_Cft.csv'
|
60
|
-
exported_csv = self.mats.Cft.export_csv(path)
|
61
|
-
self.assertTrue(os.path.exists(path))
|
62
|
-
os.remove(path)
|
63
|
-
|
64
|
-
def test_MParams_export(self) -> None:
|
65
|
-
"""
|
66
|
-
Test MParams export.
|
67
|
-
"""
|
68
|
-
cft = self.mats.Cft.export_csv()
|
69
|
-
self.assertTrue(os.path.exists(cft))
|
70
|
-
os.remove(cft)
|
71
|
-
|
72
|
-
cftt = self.mats.CftT.export_csv()
|
73
|
-
self.assertTrue(os.path.exists(cftt))
|
74
|
-
os.remove(cftt)
|
75
|
-
|
76
|
-
cg = self.mats.Cg.export_csv()
|
77
|
-
self.assertTrue(os.path.exists(cg))
|
78
|
-
os.remove(cg)
|
79
|
-
|
80
|
-
cl = self.mats.Cl.export_csv()
|
81
|
-
self.assertTrue(os.path.exists(cl))
|
82
|
-
os.remove(cl)
|
83
|
-
|
84
|
-
csh = self.mats.Csh.export_csv()
|
85
|
-
self.assertTrue(os.path.exists(csh))
|
86
|
-
os.remove(csh)
|
87
|
-
|
88
|
-
bbus = self.mats.Bbus.export_csv()
|
89
|
-
self.assertTrue(os.path.exists(bbus))
|
90
|
-
os.remove(bbus)
|
91
|
-
|
92
|
-
bf = self.mats.Bf.export_csv()
|
93
|
-
self.assertTrue(os.path.exists(bf))
|
94
|
-
os.remove(bf)
|
95
|
-
|
96
|
-
ptdf = self.mats.PTDF.export_csv()
|
97
|
-
self.assertTrue(os.path.exists(ptdf))
|
98
|
-
os.remove(ptdf)
|
99
|
-
|
100
|
-
lodf = self.mats.LODF.export_csv()
|
101
|
-
self.assertTrue(os.path.exists(lodf))
|
102
|
-
os.remove(lodf)
|
103
|
-
|
104
|
-
def test_MParam_instance(self):
|
105
|
-
"""
|
106
|
-
Test `MParam` instantiate.
|
107
|
-
"""
|
108
|
-
one_vec = MParam(v=sps.csr_matrix(np.ones(self.ss.Bus.n)))
|
109
|
-
# check if `_v` is `sps.csr_matrix` instance
|
110
|
-
self.assertIsInstance(one_vec._v, sps.csr_matrix)
|
111
|
-
# check if `v` is 1D-array
|
112
|
-
self.assertEqual(one_vec.v.shape, (self.ss.Bus.n,))
|
113
|
-
|
114
|
-
def test_c(self):
|
115
|
-
"""
|
116
|
-
Test connectivity matrices.
|
117
|
-
"""
|
118
|
-
# Test `Cg`
|
119
|
-
self.assertIsInstance(self.mats.Cg._v, (sps.csr_matrix, sps.lil_matrix))
|
120
|
-
self.assertIsInstance(self.mats.Cg.v, np.ndarray)
|
121
|
-
self.assertEqual(self.mats.Cg._v.max(), 1)
|
122
|
-
np.testing.assert_equal(self.mats.Cg._v.sum(axis=0), np.ones((1, self.ng)))
|
123
|
-
|
124
|
-
# Test `Cl`
|
125
|
-
self.assertIsInstance(self.mats.Cl._v, (sps.csr_matrix, sps.lil_matrix))
|
126
|
-
self.assertIsInstance(self.mats.Cl.v, np.ndarray)
|
127
|
-
self.assertEqual(self.mats.Cl._v.max(), 1)
|
128
|
-
np.testing.assert_equal(self.mats.Cl._v.sum(axis=0), np.ones((1, self.nD)))
|
129
|
-
|
130
|
-
# Test `Csh`
|
131
|
-
self.assertIsInstance(self.mats.Csh._v, (sps.csr_matrix, sps.lil_matrix))
|
132
|
-
self.assertIsInstance(self.mats.Csh.v, np.ndarray)
|
133
|
-
self.assertEqual(self.mats.Csh._v.max(), 1)
|
134
|
-
np.testing.assert_equal(self.mats.Csh._v.sum(axis=0), np.ones((1, self.nsh)))
|
135
|
-
|
136
|
-
# Test `Cft`
|
137
|
-
self.assertIsInstance(self.mats.Cft._v, (sps.csr_matrix, sps.lil_matrix))
|
138
|
-
self.assertIsInstance(self.mats.Cft.v, np.ndarray)
|
139
|
-
self.assertEqual(self.mats.Cft._v.max(), 1)
|
140
|
-
np.testing.assert_equal(self.mats.Cft._v.sum(axis=0), np.zeros((1, self.nl)))
|
141
|
-
|
142
|
-
def test_b_calc(self):
|
143
|
-
"""
|
144
|
-
Test `b_calc`.
|
145
|
-
"""
|
146
|
-
b = self.mats._calc_b()
|
147
|
-
self.assertIsInstance(b, np.ndarray)
|
148
|
-
self.assertEqual(b.shape, (self.nl,))
|
149
|
-
|
150
|
-
def test_bf(self):
|
151
|
-
"""
|
152
|
-
Test `Bf`.
|
153
|
-
"""
|
154
|
-
self.assertIsInstance(self.mats.Bf._v, (sps.csr_matrix, sps.lil_matrix))
|
155
|
-
np.testing.assert_equal(self.mats.Bf._v.shape, (self.nl, self.nb))
|
156
|
-
|
157
|
-
def test_bbus(self):
|
158
|
-
"""
|
159
|
-
Test `Bbus`.
|
160
|
-
"""
|
161
|
-
self.assertIsInstance(self.mats.Bbus._v, (sps.csr_matrix, sps.lil_matrix))
|
162
|
-
np.testing.assert_equal(self.mats.Bbus._v.shape, (self.nb, self.nb))
|
163
|
-
|
164
|
-
def test_pfinj(self):
|
165
|
-
"""
|
166
|
-
Test `Pfinj`.
|
167
|
-
"""
|
168
|
-
self.assertIsInstance(self.mats.Pfinj._v, np.ndarray)
|
169
|
-
np.testing.assert_equal(self.mats.Pfinj._v.shape, (self.nl,))
|
170
|
-
|
171
|
-
def test_pbusinj(self):
|
172
|
-
"""
|
173
|
-
Test `Pbusinj`.
|
174
|
-
"""
|
175
|
-
self.assertIsInstance(self.mats.Pbusinj._v, np.ndarray)
|
176
|
-
np.testing.assert_equal(self.mats.Pbusinj._v.shape, (self.nb,))
|
177
|
-
|
178
|
-
|
179
|
-
class TestBuildPTDF(unittest.TestCase):
|
180
|
-
"""
|
181
|
-
Test build PTDF.
|
182
|
-
"""
|
183
|
-
|
184
|
-
def setUp(self) -> None:
|
185
|
-
self.ss = ams.load(ams.get_case('matpower/case14.m'),
|
186
|
-
setup=True, default_config=True, no_output=True)
|
187
|
-
self.nl = self.ss.Line.n
|
188
|
-
self.nb = self.ss.Bus.n
|
189
|
-
|
190
|
-
self.assertFalse(self.ss.mats.initialized)
|
191
|
-
# NOTE: here we test `no_store=True` option
|
192
|
-
self.ptdf_full = self.ss.mats.build_ptdf(no_store=True)
|
193
|
-
self.assertTrue(self.ss.mats.initialized)
|
194
|
-
|
195
|
-
def test_ptdf_before_mat_init(self):
|
196
|
-
"""
|
197
|
-
Test PTDF before MatProcessor initialization `mats.init()`.
|
198
|
-
"""
|
199
|
-
self.assertIsNone(self.ss.mats.PTDF._v)
|
200
|
-
_ = self.ss.mats.build_ptdf(no_store=False)
|
201
|
-
self.assertEqual(self.ss.mats.PTDF._v.shape, (self.nl, self.nb))
|
202
|
-
|
203
|
-
def test_ptdf_lines(self):
|
204
|
-
"""
|
205
|
-
Test PTDF with `line` inputs.
|
206
|
-
"""
|
207
|
-
# NOTE: when `line` is given, `PTDF` should not be stored even with `no_store=False`
|
208
|
-
|
209
|
-
# input str
|
210
|
-
ptdf_l2 = self.ss.mats.build_ptdf(line=self.ss.Line.idx.v[2], no_store=False)
|
211
|
-
self.assertIsNone(self.ss.mats.PTDF._v)
|
212
|
-
np.testing.assert_array_almost_equal(ptdf_l2, self.ptdf_full[2, :])
|
213
|
-
|
214
|
-
# input list with single element
|
215
|
-
ptdf_l2p = self.ss.mats.build_ptdf(line=[self.ss.Line.idx.v[2]], no_store=False)
|
216
|
-
self.assertIsNone(self.ss.mats.PTDF._v)
|
217
|
-
np.testing.assert_array_almost_equal(ptdf_l2p, self.ptdf_full[[2], :])
|
218
|
-
|
219
|
-
# input list with multiple elements
|
220
|
-
ptdf_l23 = self.ss.mats.build_ptdf(line=self.ss.Line.idx.v[2:4], no_store=False)
|
221
|
-
self.assertIsNone(self.ss.mats.PTDF._v)
|
222
|
-
np.testing.assert_array_almost_equal(ptdf_l23, self.ptdf_full[2:4, :])
|
223
|
-
|
224
|
-
def test_ptdf_incremental_lines(self):
|
225
|
-
"""
|
226
|
-
Test PTDF incremental build with `line` inputs.
|
227
|
-
"""
|
228
|
-
# input str
|
229
|
-
ptdf_l2 = self.ss.mats.build_ptdf(line=self.ss.Line.idx.v[2], incremental=True, no_store=False,
|
230
|
-
no_tqdm=True)
|
231
|
-
np.testing.assert_array_almost_equal(ptdf_l2.todense(),
|
232
|
-
self.ptdf_full[[2], :])
|
233
|
-
self.assertTrue(sps.isspmatrix_lil(ptdf_l2))
|
234
|
-
self.assertIsNone(self.ss.mats.PTDF._v)
|
235
|
-
|
236
|
-
# input list with single element
|
237
|
-
ptdf_l2p = self.ss.mats.build_ptdf(line=[self.ss.Line.idx.v[2]], incremental=True, no_store=False,
|
238
|
-
no_tqdm=False)
|
239
|
-
np.testing.assert_array_almost_equal(ptdf_l2p.todense(),
|
240
|
-
self.ptdf_full[[2], :])
|
241
|
-
self.assertTrue(sps.isspmatrix_lil(ptdf_l2p))
|
242
|
-
self.assertIsNone(self.ss.mats.PTDF._v)
|
243
|
-
|
244
|
-
# input list with multiple elements
|
245
|
-
ptdf_l23 = self.ss.mats.build_ptdf(line=self.ss.Line.idx.v[2:4], incremental=True, no_store=False)
|
246
|
-
np.testing.assert_array_almost_equal(ptdf_l23.todense(),
|
247
|
-
self.ptdf_full[2:4, :])
|
248
|
-
|
249
|
-
def test_ptdf_incremental_step(self):
|
250
|
-
"""
|
251
|
-
Test PTDF incremental build with step.
|
252
|
-
"""
|
253
|
-
# step < line length
|
254
|
-
ptdf_c1 = self.ss.mats.build_ptdf(line=self.ss.Line.idx.v[2:4], incremental=True,
|
255
|
-
no_store=False, step=1)
|
256
|
-
self.assertTrue(sps.isspmatrix_lil(ptdf_c1))
|
257
|
-
self.assertIsNone(self.ss.mats.PTDF._v)
|
258
|
-
np.testing.assert_array_almost_equal(ptdf_c1.todense(),
|
259
|
-
self.ptdf_full[2:4, :],)
|
260
|
-
|
261
|
-
# step = line length
|
262
|
-
ptdf_c2 = self.ss.mats.build_ptdf(line=self.ss.Line.idx.v[2:4], incremental=True,
|
263
|
-
no_store=False, step=2)
|
264
|
-
self.assertTrue(sps.isspmatrix_lil(ptdf_c2))
|
265
|
-
self.assertIsNone(self.ss.mats.PTDF._v)
|
266
|
-
np.testing.assert_array_almost_equal(ptdf_c2.todense(),
|
267
|
-
self.ptdf_full[2:4, :],)
|
268
|
-
|
269
|
-
# step > line length
|
270
|
-
ptdf_c5 = self.ss.mats.build_ptdf(line=self.ss.Line.idx.v[2:4], incremental=True,
|
271
|
-
no_store=False, step=5)
|
272
|
-
self.assertTrue(sps.isspmatrix_lil(ptdf_c5))
|
273
|
-
self.assertIsNone(self.ss.mats.PTDF._v)
|
274
|
-
np.testing.assert_array_almost_equal(ptdf_c5.todense(),
|
275
|
-
self.ptdf_full[2:4, :],)
|
276
|
-
|
277
|
-
|
278
|
-
class TestBuildLODF(unittest.TestCase):
|
279
|
-
"""
|
280
|
-
Test build LODF.
|
281
|
-
"""
|
282
|
-
|
283
|
-
def setUp(self) -> None:
|
284
|
-
self.ss = ams.load(ams.get_case('matpower/case14.m'),
|
285
|
-
setup=True, default_config=True, no_output=True)
|
286
|
-
self.nl = self.ss.Line.n
|
287
|
-
self.nb = self.ss.Bus.n
|
288
|
-
self.dec = 4
|
289
|
-
|
290
|
-
self.assertFalse(self.ss.mats.initialized)
|
291
|
-
self.lodf_full = self.ss.mats.build_lodf(no_store=True)
|
292
|
-
self.assertTrue(self.ss.mats.initialized)
|
293
|
-
|
294
|
-
def test_lodf_before_ptdf(self):
|
295
|
-
"""
|
296
|
-
Test LODF before PTDF.
|
297
|
-
"""
|
298
|
-
self.assertIsNone(self.ss.mats.PTDF._v)
|
299
|
-
self.assertIsNone(self.ss.mats.LODF._v)
|
300
|
-
_ = self.ss.mats.build_lodf(no_store=False)
|
301
|
-
self.assertEqual(self.ss.mats.LODF._v.shape, (self.nl, self.nl))
|
302
|
-
# PTDF should not be stored for requested building
|
303
|
-
self.assertIsNone(self.ss.mats.PTDF._v)
|
304
|
-
|
305
|
-
def test_lodf_lines(self):
|
306
|
-
"""
|
307
|
-
Test LODF with `line` inputs.
|
308
|
-
"""
|
309
|
-
# input str
|
310
|
-
lodf_l2 = self.ss.mats.build_lodf(line=self.ss.Line.idx.v[2], no_store=False)
|
311
|
-
np.testing.assert_array_almost_equal(lodf_l2, self.lodf_full[:, 2], decimal=self.dec)
|
312
|
-
|
313
|
-
# input list with single element
|
314
|
-
lodf_l2p = self.ss.mats.build_lodf(line=[self.ss.Line.idx.v[2]], no_store=False)
|
315
|
-
np.testing.assert_array_almost_equal(lodf_l2p, self.lodf_full[:, [2]], decimal=self.dec)
|
316
|
-
|
317
|
-
# input list with multiple elements
|
318
|
-
lodf_l23 = self.ss.mats.build_lodf(line=self.ss.Line.idx.v[2:4], no_store=False)
|
319
|
-
np.testing.assert_array_almost_equal(lodf_l23, self.lodf_full[:, 2:4], decimal=self.dec)
|
320
|
-
|
321
|
-
def test_lodf_incremental_lines(self):
|
322
|
-
"""
|
323
|
-
Test LODF incremental build with `line` inputs.
|
324
|
-
"""
|
325
|
-
# input str
|
326
|
-
lodf_l2 = self.ss.mats.build_lodf(line=self.ss.Line.idx.v[2], incremental=True, no_store=False)
|
327
|
-
np.testing.assert_array_almost_equal(lodf_l2.todense(),
|
328
|
-
self.lodf_full[:, [2]],
|
329
|
-
decimal=self.dec)
|
330
|
-
self.assertTrue(sps.isspmatrix_lil(lodf_l2))
|
331
|
-
|
332
|
-
# input list with single element
|
333
|
-
lodf_l2p = self.ss.mats.build_lodf(line=[self.ss.Line.idx.v[2]], incremental=True, no_store=False)
|
334
|
-
np.testing.assert_array_almost_equal(lodf_l2p.todense(),
|
335
|
-
self.lodf_full[:, [2]],
|
336
|
-
decimal=self.dec)
|
337
|
-
self.assertTrue(sps.isspmatrix_lil(lodf_l2p))
|
338
|
-
|
339
|
-
# input list with multiple elements
|
340
|
-
lodf_l23 = self.ss.mats.build_lodf(line=self.ss.Line.idx.v[2:4], incremental=True, no_store=False)
|
341
|
-
np.testing.assert_array_almost_equal(lodf_l23.todense(),
|
342
|
-
self.lodf_full[:, 2:4],
|
343
|
-
decimal=self.dec)
|
344
|
-
|
345
|
-
def test_lodf_incremental_step(self):
|
346
|
-
"""
|
347
|
-
Test LODF incremental build with step.
|
348
|
-
"""
|
349
|
-
# step < line length
|
350
|
-
lodf_c1 = self.ss.mats.build_lodf(line=self.ss.Line.idx.v[2:4], incremental=True,
|
351
|
-
no_store=False, step=1)
|
352
|
-
self.assertTrue(sps.isspmatrix_lil(lodf_c1))
|
353
|
-
np.testing.assert_array_almost_equal(lodf_c1.todense(),
|
354
|
-
self.lodf_full[:, 2:4],
|
355
|
-
decimal=self.dec)
|
356
|
-
|
357
|
-
# step = line length
|
358
|
-
lodf_c2 = self.ss.mats.build_lodf(line=self.ss.Line.idx.v[2:4], incremental=True,
|
359
|
-
no_store=False, step=2)
|
360
|
-
self.assertTrue(sps.isspmatrix_lil(lodf_c2))
|
361
|
-
np.testing.assert_array_almost_equal(lodf_c2.todense(),
|
362
|
-
self.lodf_full[:, 2:4],
|
363
|
-
decimal=self.dec)
|
364
|
-
|
365
|
-
# step > line length
|
366
|
-
lodf_c5 = self.ss.mats.build_lodf(line=self.ss.Line.idx.v[2:4], incremental=True,
|
367
|
-
no_store=False, step=5)
|
368
|
-
self.assertTrue(sps.isspmatrix_lil(lodf_c5))
|
369
|
-
np.testing.assert_array_almost_equal(lodf_c5.todense(),
|
370
|
-
self.lodf_full[:, 2:4],
|
371
|
-
decimal=self.dec)
|
372
|
-
|
373
|
-
|
374
|
-
class TestBuildOTDF(unittest.TestCase):
|
375
|
-
"""
|
376
|
-
Test build OTDF.
|
377
|
-
"""
|
378
|
-
|
379
|
-
def setUp(self) -> None:
|
380
|
-
self.ss = ams.load(ams.get_case('matpower/case14.m'),
|
381
|
-
setup=True, default_config=True, no_output=True)
|
382
|
-
self.nl = self.ss.Line.n
|
383
|
-
self.nb = self.ss.Bus.n
|
384
|
-
self.dec = 4
|
385
|
-
_ = self.ss.mats.build()
|
386
|
-
|
387
|
-
def test_otdf_dense_build(self):
|
388
|
-
_ = self.ss.mats.build_ptdf(no_store=False, incremental=False)
|
389
|
-
_ = self.ss.mats.build_lodf(no_store=False, incremental=False)
|
390
|
-
self.otdf_full = self.ss.mats.build_otdf()
|
391
|
-
self.assertEqual(self.otdf_full.shape, (self.nl, self.nb))
|
392
|
-
|
393
|
-
def test_otdf_sparse_build(self):
|
394
|
-
# --- both PTDF and LODF are dense ---
|
395
|
-
_ = self.ss.mats.build_ptdf(no_store=False, incremental=False)
|
396
|
-
_ = self.ss.mats.build_lodf(no_store=False, incremental=False)
|
397
|
-
otdf_dense = self.ss.mats.build_otdf()
|
398
|
-
|
399
|
-
_ = self.ss.mats.build_ptdf(no_store=False, incremental=True)
|
400
|
-
_ = self.ss.mats.build_lodf(no_store=False, incremental=True)
|
401
|
-
otdf_sparse = self.ss.mats.build_otdf()
|
402
|
-
np.testing.assert_array_almost_equal(otdf_sparse.todense(), otdf_dense,
|
403
|
-
decimal=self.dec)
|
404
|
-
|
405
|
-
# --- PTDF is dense and LODF is sparse ---
|
406
|
-
_ = self.ss.mats.build_ptdf(no_store=False, incremental=False)
|
407
|
-
_ = self.ss.mats.build_lodf(no_store=False, incremental=True)
|
408
|
-
otdf_sps1 = self.ss.mats.build_otdf()
|
409
|
-
np.testing.assert_array_almost_equal(otdf_sps1, otdf_dense,
|
410
|
-
decimal=self.dec)
|
411
|
-
|
412
|
-
# --- PTDF is sparse and LODF is dense ---
|
413
|
-
_ = self.ss.mats.build_ptdf(no_store=False, incremental=True)
|
414
|
-
_ = self.ss.mats.build_lodf(no_store=False, incremental=False)
|
415
|
-
otdf_sps2 = self.ss.mats.build_otdf()
|
416
|
-
np.testing.assert_array_almost_equal(otdf_sps2.todense(), otdf_dense,
|
417
|
-
decimal=self.dec)
|
418
|
-
|
419
|
-
def test_otdf_lines(self):
|
420
|
-
"""
|
421
|
-
Test OTDF with `line` inputs.
|
422
|
-
"""
|
423
|
-
# --- both PTDF and LODF are dense ---
|
424
|
-
_ = self.ss.mats.build_ptdf(no_store=False, incremental=False)
|
425
|
-
_ = self.ss.mats.build_lodf(no_store=False, incremental=False)
|
426
|
-
|
427
|
-
# input str
|
428
|
-
otdf_l2 = self.ss.mats.build_otdf(line=self.ss.Line.idx.v[2])
|
429
|
-
self.assertEqual(otdf_l2.shape, (self.nl, self.nb))
|
430
|
-
|
431
|
-
# input list with single element
|
432
|
-
otdf_l2p = self.ss.mats.build_otdf(line=[self.ss.Line.idx.v[2]])
|
433
|
-
self.assertEqual(otdf_l2p.shape, (self.nl, self.nb))
|
434
|
-
|
435
|
-
# input list with multiple elements
|
436
|
-
otdf_l23 = self.ss.mats.build_otdf(line=self.ss.Line.idx.v[2:4])
|
437
|
-
self.assertEqual(otdf_l23.shape, (self.nl, self.nb))
|