Skip to content

Commit 974caa1

Browse files
authored
Merge pull request #7 from DAI-Lab/user_query_functions
Add support of user query functions, change in class variables
2 parents 994f941 + 295a260 commit 974caa1

File tree

4 files changed

+82
-11
lines changed

4 files changed

+82
-11
lines changed

pygridsim/configs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,10 @@
6666
"kW": defaults.INDUSTRIAL_GEN_KW,
6767
}
6868
}
69+
70+
NAME_TO_CONFIG = {
71+
"load": LOAD_CONFIGURATIONS,
72+
"source": SOURCE_CONFIGURATIONS,
73+
"generator": GENERATOR_CONFIGURATIONS,
74+
"line": LINE_CONFIGURATIONS
75+
}

pygridsim/core.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from altdss import altdss
33

4+
from pygridsim.configs import NAME_TO_CONFIG
45
from pygridsim.lines import _make_line
56
from pygridsim.parameters import _make_generator, _make_load_node, _make_pv, _make_source_node
67
from pygridsim.results import _export_results, _query_solution
@@ -16,17 +17,16 @@ def __init__(self):
1617
Stores numbers of circuit components to ensure unique naming of repeat circuit components.
1718
1819
Attributes:
19-
num_loads (int): Number of loads in circuit so far.
20-
num_lines (int): Number of lines in circuit so far.
21-
num_transformers (int): Number of transformers in circuit so far.
22-
num_pv (int): Number of PV systems in circuit so far.
23-
num_generators (int): Number generators in circuit so far.
20+
num_generators (int): Number of generators created so far.
21+
num_lines (int): Number of lines created so far.
22+
num_loads (int): Number of load nodes created so far.
23+
num_pv (int): Number of PVSystems create so far.
2424
"""
25-
self.num_loads = 0
25+
self.num_generators = 0
2626
self.num_lines = 0
27-
self.num_transformers = 0
27+
self.num_loads = 0
2828
self.num_pv = 0
29-
self.num_generators = 0
29+
3030
altdss.ClearAll()
3131
altdss('new circuit.MyCircuit')
3232

@@ -52,6 +52,7 @@ def add_load_nodes(self,
5252
list[OpenDSS object]:
5353
A list of OpenDSS objects representing the load nodes created.
5454
"""
55+
5556
params = params or dict()
5657
load_nodes = []
5758
for _ in range(num):
@@ -206,6 +207,37 @@ def clear(self):
206207
None
207208
"""
208209
altdss.ClearAll()
209-
self.num_loads = 0
210-
self.num_lines = 0
211-
self.num_transformers = 0
210+
211+
def get_types(self, component: str, show_ranges: bool = False):
212+
"""Provides list of all supported Load Types
213+
214+
Args:
215+
component (str):
216+
Which component to get, one of (one of "load", "source", "pv", "line")
217+
show_ranges (bool, optional):
218+
Whether to show all configuration ranges in output.
219+
220+
Returns:
221+
list:
222+
A list containing all load types, if show_ranges False.
223+
A list of tuples showing all load types and configurations, if show_ranges True.
224+
"""
225+
component_simplified = component.lower().replace(" ", "")
226+
if (component_simplified[-1] == "s"):
227+
component_simplified = component_simplified[:-1]
228+
configuration = {}
229+
if component_simplified in NAME_TO_CONFIG:
230+
configuration = NAME_TO_CONFIG[component_simplified]
231+
else:
232+
raise KeyError(
233+
f"Invalid component input: expect one of {[name for name in NAME_TO_CONFIG]}")
234+
235+
if not show_ranges:
236+
return [component_type.value for component_type in configuration]
237+
238+
component_types = []
239+
for component_type in configuration:
240+
config_dict = configuration[component_type]
241+
component_types.append((component_type.value, config_dict))
242+
243+
return component_types

sim.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Voltages": {
3+
"source": 1912.250959225854,
4+
"load0": 164.87512862167387
5+
},
6+
"Losses": {
7+
"Active Power Loss": 181552.68984510849,
8+
"Reactive Power Loss": 377394.0863595363
9+
}
10+
}

tests/test_circuit.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,25 @@ def test_106_transformer_parameters(self):
274274
circuit.solve()
275275
print(circuit.results(["Voltages", "Losses"]))
276276
circuit.clear()
277+
278+
279+
class TestTypeQueryFunctions(unittest.TestCase):
280+
281+
def setUp(self):
282+
"""Set up test fixtures, if any."""
283+
print("\nTest", self._testMethodName)
284+
285+
def tearDown(self):
286+
"""Tear down test fixtures, if any."""
287+
288+
def test_200_type_queries(self):
289+
circuit = PyGridSim()
290+
# should still work if plural, capitalized, spaces
291+
for component in ["load ", "sources", "Line", "GENERATOR"]:
292+
print(circuit.get_types(component))
293+
print(circuit.get_types(component, show_ranges=True))
294+
295+
def test_200_invalid_type_quer(self):
296+
circuit = PyGridSim()
297+
with self.assertRaises(KeyError):
298+
circuit.get_types("bad_component_name")

0 commit comments

Comments
 (0)