Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pygridsim/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@
"kW": defaults.INDUSTRIAL_GEN_KW,
}
}

NAME_TO_CONFIG = {
"load": LOAD_CONFIGURATIONS,
"source": SOURCE_CONFIGURATIONS,
"generator": GENERATOR_CONFIGURATIONS,
"line": LINE_CONFIGURATIONS
}
72 changes: 51 additions & 21 deletions pygridsim/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from altdss import altdss

from pygridsim.configs import NAME_TO_CONFIG
from pygridsim.lines import _make_line
from pygridsim.parameters import _make_generator, _make_load_node, _make_pv, _make_source_node
from pygridsim.results import _export_results, _query_solution
Expand All @@ -16,17 +17,12 @@ def __init__(self):
Stores numbers of circuit components to ensure unique naming of repeat circuit components.

Attributes:
num_loads (int): Number of loads in circuit so far.
num_lines (int): Number of lines in circuit so far.
num_transformers (int): Number of transformers in circuit so far.
num_pv (int): Number of PV systems in circuit so far.
num_generators (int): Number generators in circuit so far.
counts (dict[str, int]): Map of each type to the number seen of that type so far
"""
self.num_loads = 0
self.num_lines = 0
self.num_transformers = 0
self.num_pv = 0
self.num_generators = 0
self.counts = {}
for count_type in ["loads", "lines", "transformers", "pv", "generators"]:
self.counts[count_type] = 0

altdss.ClearAll()
altdss('new circuit.MyCircuit')

Expand All @@ -52,11 +48,12 @@ def add_load_nodes(self,
list[OpenDSS object]:
A list of OpenDSS objects representing the load nodes created.
"""

params = params or dict()
load_nodes = []
for _ in range(num):
_make_load_node(params, load_type, self.num_loads)
self.num_loads += 1
_make_load_node(params, load_type, self.counts["loads"])
self.counts["loads"] += 1

return load_nodes

Expand Down Expand Up @@ -108,8 +105,8 @@ def add_PVSystem(self,

PV_nodes = []
for load in load_nodes:
PV_nodes.append(_make_pv(load, params, num_panels, self.num_pv))
self.num_pv += 1
PV_nodes.append(_make_pv(load, params, num_panels, self.counts["pv"]))
self.counts["pv"] += 1

return PV_nodes

Expand All @@ -131,8 +128,8 @@ def add_generator(self, num: int = 1, gen_type: str = "small", params: dict[str,
params = params or dict()
generators = []
for _ in range(num):
generators.append(_make_generator(params, gen_type, count=self.num_generators))
self.num_generators += 1
generators.append(_make_generator(params, gen_type, count=self.counts["generators"]))
self.counts["generators"] += 1

return generators

Expand Down Expand Up @@ -161,8 +158,8 @@ def add_lines(self,
"""
params = params or dict()
for src, dst in connections:
_make_line(src, dst, line_type, self.num_lines, params, transformer)
self.num_lines += 1
_make_line(src, dst, line_type, self.counts["lines"], params, transformer)
self.counts["lines"] += 1

def solve(self):
"""Solves the OpenDSS circuit.
Expand Down Expand Up @@ -206,6 +203,39 @@ def clear(self):
None
"""
altdss.ClearAll()
self.num_loads = 0
self.num_lines = 0
self.num_transformers = 0
for key in self.counts:
self.counts[key] = 0

def get_types(self, component: str, show_ranges: bool = False):
"""Provides list of all supported Load Types

Args:
component (str):
Which component to get, one of (one of "load", "source", "pv", "line")
show_ranges (bool, optional):
Whether to show all configuration ranges in output.

Returns:
list:
A list containing all load types, if show_ranges False.
A list of tuples showing all load types and configurations, if show_ranges True.
"""
component_simplified = component.lower().replace(" ", "")
if (component_simplified[-1] == "s"):
component_simplified = component_simplified[:-1]
configuration = {}
if component_simplified in NAME_TO_CONFIG:
configuration = NAME_TO_CONFIG[component_simplified]
else:
raise KeyError(
f"Invalid component input: expect one of {[name for name in NAME_TO_CONFIG]}")

if not show_ranges:
return [component_type.value for component_type in configuration]

component_types = []
for component_type in configuration:
config_dict = configuration[component_type]
component_types.append((component_type.value, config_dict))

return component_types
22 changes: 22 additions & 0 deletions tests/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,25 @@ def test_104_non_int_parameters(self):
circuit = PyGridSim()
with self.assertRaises(TypeError):
circuit.add_load_nodes(params={"kV": "stringInput"})


class TestTypeQueryFunctions(unittest.TestCase):

def setUp(self):
"""Set up test fixtures, if any."""
print("\nTest", self._testMethodName)

def tearDown(self):
"""Tear down test fixtures, if any."""

def test_200_type_queries(self):
circuit = PyGridSim()
# should still work if plural, capitalized, spaces
for component in ["load ", "sources", "Line", "GENERATOR"]:
print(circuit.get_types(component))
print(circuit.get_types(component, show_ranges=True))

def test_200_invalid_type_quer(self):
circuit = PyGridSim()
with self.assertRaises(KeyError):
circuit.get_types("bad_component_name")