From 0ed410b4433dd62e268e74ffe317309d811fe261 Mon Sep 17 00:00:00 2001 From: Angela Zhao Date: Wed, 9 Apr 2025 16:31:41 -0400 Subject: [PATCH 1/6] change init parameters to dictionary, get_load_types --- pygridsim/core.py | 61 ++++++++++++++++++++++++++++--------------- sim.json | 10 +++++++ tests/test_circuit.py | 15 +++++++++++ 3 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 sim.json diff --git a/pygridsim/core.py b/pygridsim/core.py index 7444482..b6ac117 100644 --- a/pygridsim/core.py +++ b/pygridsim/core.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- from altdss import altdss +from pygridsim.configs import LOAD_CONFIGURATIONS +from pygridsim.enums import LoadType 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 @@ -16,17 +18,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') @@ -52,11 +49,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 @@ -108,8 +106,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 @@ -131,8 +129,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 @@ -161,8 +159,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. @@ -206,6 +204,27 @@ 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_load_types(self, show_ranges: bool = False): + """Provides list of all supported Load Types + + Args: + 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. + """ + if not show_ranges: + return [load_type.value for load_type in LoadType] + + load_types = [] + for load_type in LoadType: + config_dict = LOAD_CONFIGURATIONS[load_type] + load_types.append((load_type.value, config_dict)) + + return load_types diff --git a/sim.json b/sim.json new file mode 100644 index 0000000..c5bbff8 --- /dev/null +++ b/sim.json @@ -0,0 +1,10 @@ +{ + "Voltages": { + "source": 1795.8120400680568, + "load0": 140.73274057854667 + }, + "Losses": { + "Active Power Loss": 242995.00938751808, + "Reactive Power Loss": 505212.2611395146 + } +} \ No newline at end of file diff --git a/tests/test_circuit.py b/tests/test_circuit.py index 8460d5f..4df9e7c 100644 --- a/tests/test_circuit.py +++ b/tests/test_circuit.py @@ -243,3 +243,18 @@ 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() + print(circuit.get_load_types()) + print(circuit.get_load_types(show_ranges=True)) From 3b4b788700dc0b9137f6b80a301d8196b53ec33b Mon Sep 17 00:00:00 2001 From: Angela Zhao Date: Wed, 9 Apr 2025 16:32:06 -0400 Subject: [PATCH 2/6] dont include sim.json --- sim.json | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 sim.json diff --git a/sim.json b/sim.json deleted file mode 100644 index c5bbff8..0000000 --- a/sim.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "Voltages": { - "source": 1795.8120400680568, - "load0": 140.73274057854667 - }, - "Losses": { - "Active Power Loss": 242995.00938751808, - "Reactive Power Loss": 505212.2611395146 - } -} \ No newline at end of file From 9ddc517eb917efd6f2d0b476b7ed6a176cec2345 Mon Sep 17 00:00:00 2001 From: Angela Zhao Date: Wed, 9 Apr 2025 16:45:41 -0400 Subject: [PATCH 3/6] fixed from load types to general get_types function --- pygridsim/configs.py | 7 +++++++ pygridsim/core.py | 27 +++++++++++++++++++-------- tests/test_circuit.py | 11 +++++++++-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/pygridsim/configs.py b/pygridsim/configs.py index cc8bd3e..d7c23ff 100644 --- a/pygridsim/configs.py +++ b/pygridsim/configs.py @@ -66,3 +66,10 @@ "kW": defaults.INDUSTRIAL_GEN_KW, } } + +NAME_TO_CONFIG = { + "load": LOAD_CONFIGURATIONS, + "source": SOURCE_CONFIGURATIONS, + "generator": GENERATOR_CONFIGURATIONS, + "line": LINE_CONFIGURATIONS +} \ No newline at end of file diff --git a/pygridsim/core.py b/pygridsim/core.py index b6ac117..7ea5312 100644 --- a/pygridsim/core.py +++ b/pygridsim/core.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from altdss import altdss -from pygridsim.configs import LOAD_CONFIGURATIONS +from pygridsim.configs import NAME_TO_CONFIG from pygridsim.enums import LoadType from pygridsim.lines import _make_line from pygridsim.parameters import _make_generator, _make_load_node, _make_pv, _make_source_node @@ -207,10 +207,12 @@ def clear(self): for key in self.counts: self.counts[key] = 0 - def get_load_types(self, show_ranges: bool = False): + 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. @@ -219,12 +221,21 @@ def get_load_types(self, show_ranges: bool = False): 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 [load_type.value for load_type in LoadType] + return [component_type.value for component_type in configuration] - load_types = [] - for load_type in LoadType: - config_dict = LOAD_CONFIGURATIONS[load_type] - load_types.append((load_type.value, config_dict)) + component_types = [] + for component_type in configuration: + config_dict = configuration[component_type] + component_types.append((component_type.value, config_dict)) - return load_types + return component_types diff --git a/tests/test_circuit.py b/tests/test_circuit.py index 4df9e7c..3f5147c 100644 --- a/tests/test_circuit.py +++ b/tests/test_circuit.py @@ -256,5 +256,12 @@ def tearDown(self): def test_200_type_queries(self): circuit = PyGridSim() - print(circuit.get_load_types()) - print(circuit.get_load_types(show_ranges=True)) + # 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") From 748b9e6634c41d80f33f1f0cf1a9550976c316a2 Mon Sep 17 00:00:00 2001 From: Angela Zhao Date: Wed, 9 Apr 2025 16:47:20 -0400 Subject: [PATCH 4/6] lint fix --- pygridsim/configs.py | 2 +- pygridsim/core.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pygridsim/configs.py b/pygridsim/configs.py index d7c23ff..b31ae52 100644 --- a/pygridsim/configs.py +++ b/pygridsim/configs.py @@ -72,4 +72,4 @@ "source": SOURCE_CONFIGURATIONS, "generator": GENERATOR_CONFIGURATIONS, "line": LINE_CONFIGURATIONS -} \ No newline at end of file +} diff --git a/pygridsim/core.py b/pygridsim/core.py index 7ea5312..724485f 100644 --- a/pygridsim/core.py +++ b/pygridsim/core.py @@ -2,7 +2,6 @@ from altdss import altdss from pygridsim.configs import NAME_TO_CONFIG -from pygridsim.enums import LoadType 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 @@ -209,7 +208,7 @@ def clear(self): 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") @@ -228,7 +227,8 @@ def get_types(self, component: str, show_ranges: bool = False): 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]}") + 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] From 49d97fc8f37a5354923474ad2b0ab621dec4d8b8 Mon Sep 17 00:00:00 2001 From: Angela Zhao Date: Tue, 15 Apr 2025 20:15:42 -0400 Subject: [PATCH 5/6] revert the self.counts change (deleting transformers because unused, alphabetized on init --- pygridsim/core.py | 30 ++++++++++++++++-------------- sim.json | 10 ++++++++++ 2 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 sim.json diff --git a/pygridsim/core.py b/pygridsim/core.py index 724485f..07a6517 100644 --- a/pygridsim/core.py +++ b/pygridsim/core.py @@ -17,11 +17,15 @@ def __init__(self): Stores numbers of circuit components to ensure unique naming of repeat circuit components. Attributes: - counts (dict[str, int]): Map of each type to the number seen of that type so far + num_generators (int): Number of generators created so far. + num_lines (int): Number of lines created so far. + num_loads (int): Number of load nodes created so far. + num_pv (int): Number of PVSystems create so far. """ - self.counts = {} - for count_type in ["loads", "lines", "transformers", "pv", "generators"]: - self.counts[count_type] = 0 + self.num_generators = 0 + self.num_lines = 0 + self.num_loads = 0 + self.num_pv = 0 altdss.ClearAll() altdss('new circuit.MyCircuit') @@ -52,8 +56,8 @@ def add_load_nodes(self, params = params or dict() load_nodes = [] for _ in range(num): - _make_load_node(params, load_type, self.counts["loads"]) - self.counts["loads"] += 1 + _make_load_node(params, load_type, self.num_loads) + self.num_loads += 1 return load_nodes @@ -105,8 +109,8 @@ def add_PVSystem(self, PV_nodes = [] for load in load_nodes: - PV_nodes.append(_make_pv(load, params, num_panels, self.counts["pv"])) - self.counts["pv"] += 1 + PV_nodes.append(_make_pv(load, params, num_panels, self.num_pv)) + self.num_pv += 1 return PV_nodes @@ -128,8 +132,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.counts["generators"])) - self.counts["generators"] += 1 + generators.append(_make_generator(params, gen_type, count=self.num_generators)) + self.num_generators += 1 return generators @@ -158,8 +162,8 @@ def add_lines(self, """ params = params or dict() for src, dst in connections: - _make_line(src, dst, line_type, self.counts["lines"], params, transformer) - self.counts["lines"] += 1 + _make_line(src, dst, line_type, self.num_lines, params, transformer) + self.num_lines += 1 def solve(self): """Solves the OpenDSS circuit. @@ -203,8 +207,6 @@ def clear(self): None """ altdss.ClearAll() - 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 diff --git a/sim.json b/sim.json new file mode 100644 index 0000000..4fd2254 --- /dev/null +++ b/sim.json @@ -0,0 +1,10 @@ +{ + "Voltages": { + "source": 9999.598243195385, + "load0": 8294.334838925915 + }, + "Losses": { + "Active Power Loss": 9059444.425434513, + "Reactive Power Loss": 31084427.834760502 + } +} \ No newline at end of file From b6c992f83b80e5a055692430c85830f48e4db672 Mon Sep 17 00:00:00 2001 From: Angela Zhao Date: Tue, 15 Apr 2025 20:16:11 -0400 Subject: [PATCH 6/6] change to sim.json ? --- sim.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sim.json b/sim.json index 4fd2254..ed80665 100644 --- a/sim.json +++ b/sim.json @@ -1,10 +1,10 @@ { "Voltages": { - "source": 9999.598243195385, - "load0": 8294.334838925915 + "source": 1912.250959225854, + "load0": 164.87512862167387 }, "Losses": { - "Active Power Loss": 9059444.425434513, - "Reactive Power Loss": 31084427.834760502 + "Active Power Loss": 181552.68984510849, + "Reactive Power Loss": 377394.0863595363 } } \ No newline at end of file