Skip to content

Commit 2a904aa

Browse files
committed
naming nodes
1 parent 50dd804 commit 2a904aa

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

pygridsim/core.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,23 @@ def __init__(self):
2727
self.num_transformers = 0
2828
self.num_pv = 0
2929
self.num_generators = 0
30+
self.nickname_to_name = {}
3031
altdss.ClearAll()
3132
altdss('new circuit.MyCircuit')
3233

34+
def _check_naming(self, name):
35+
if name in self.nickname_to_name:
36+
raise NameError("Provided name already assigned to a node")
37+
if name.startswith("load") or name.startswith(
38+
"generator") or name == "source" or name.startswith("pv"):
39+
raise NameError(
40+
"Cannot name nodes of the format 'component + __', ambiguity with internal names")
41+
3342
def add_load_nodes(self,
3443
load_type: str = "house",
3544
params: dict[str, int] = None,
36-
num: int = 1):
45+
num: int = 1,
46+
names: list[str] = None):
3747
"""Adds Load Node(s) to circuit.
3848
3949
Allows the user to add num load nodes,
@@ -47,14 +57,24 @@ def add_load_nodes(self,
4757
Load parameters for these manual additions. Defaults to empty dictionary.
4858
num (int, optional):
4959
The number of loads to create with these parameters. Defaults to 1.
60+
names (list(str), optional):
61+
Up to num names to assign as shortcuts to the loads
5062
5163
Returns:
5264
list[OpenDSS object]:
5365
A list of OpenDSS objects representing the load nodes created.
5466
"""
5567
params = params or dict()
68+
names = names or list()
69+
if len(names) > num:
70+
raise ValueError("Specified more names of loads than number of nodes")
71+
5672
load_nodes = []
57-
for _ in range(num):
73+
for i in range(num):
74+
if (len(names) > i):
75+
self._check_naming(names[i])
76+
self.nickname_to_name[names[i]] = "load" + str(self.num_loads)
77+
5878
_make_load_node(params, load_type, self.num_loads)
5979
self.num_loads += 1
6080

@@ -106,12 +126,19 @@ def add_PVSystems(self, load_nodes: list[str],
106126

107127
PV_nodes = []
108128
for load in load_nodes:
129+
if (load in self.nickname_to_name):
130+
self.nickname_to_name[load]
131+
109132
PV_nodes.append(_make_pv(load, params, num_panels, self.num_pv))
110133
self.num_pv += 1
111134

112135
return PV_nodes
113136

114-
def add_generators(self, num: int = 1, gen_type: str = "small", params: dict[str, int] = None):
137+
def add_generators(self,
138+
num: int = 1,
139+
gen_type: str = "small",
140+
params: dict[str, int] = None,
141+
names: list[str] = None):
115142
"""Adds generator(s) to the system.
116143
117144
Args:
@@ -121,14 +148,24 @@ def add_generators(self, num: int = 1, gen_type: str = "small", params: dict[str
121148
The type of generator (one of "small", "large", "industrial"). Defaults to "small".
122149
params (dict[str, int], optional):
123150
A dictionary of parameters to configure the generator. Defaults to None.
151+
names (list(str), optional):
152+
Up to num names to assign as shortcuts to the generators
124153
125154
Returns:
126155
list[DSS objects]:
127156
A list of OpenDSS objects representing the generators created.
128157
"""
129158
params = params or dict()
159+
names = names or list()
160+
if len(names) > num:
161+
raise ValueError("Specified more names of generators than number of nodes")
162+
130163
generators = []
131-
for _ in range(num):
164+
for i in range(num):
165+
if (len(names) > i):
166+
self._check_naming(names[i])
167+
self.nickname_to_name[names[i]] = "generator" + str(self.num_generators)
168+
132169
generators.append(_make_generator(params, gen_type, count=self.num_generators))
133170
self.num_generators += 1
134171

@@ -159,6 +196,13 @@ def add_lines(self,
159196
"""
160197
params = params or dict()
161198
for src, dst in connections:
199+
if (src in self.nickname_to_name):
200+
src = self.nickname_to_name[src]
201+
if (dst in self.nickname_to_name):
202+
dst = self.nickname_to_name[dst]
203+
if (src == dst):
204+
raise ValueError("Tried to make a line between equivalent src and dst")
205+
162206
_make_line(src, dst, line_type, self.num_lines, params, transformer)
163207
self.num_lines += 1
164208

tests/test_circuit.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,37 @@ def test_011_configs(self):
162162
with self.assertRaises(Exception):
163163
circuit.update_source(source_type=SourceType.TURBINE)
164164

165+
def test_012_node_naming(self):
166+
circuit = PyGridSim()
167+
circuit.update_source()
168+
# Create 4 nodes, only name 2 of them
169+
circuit.add_load_nodes(num=4, load_type="house", names=["0", "1"])
170+
circuit.add_generators(num=2, gen_type="small", names=["G0", "G1"])
171+
172+
# Add PVSystems to one of the nodes with shortcut
173+
circuit.add_PVSystems(load_nodes=["load2", "0"])
174+
# Can use original or abbreviated name
175+
circuit.add_lines(connections=[("G0", "0"), ("generator1", "load1")])
176+
177+
with self.assertRaises(NameError):
178+
# Tries to assign an already assigned name
179+
circuit.add_load_nodes(num=1, names=["G1"])
180+
181+
with self.assertRaises(NameError):
182+
# Tries to assign name to internal name load1, errors because adds ambiguity
183+
circuit.add_load_nodes(num=1, names=["load1"])
184+
185+
with self.assertRaises(ValueError):
186+
# Attempt to name 2 load nodes, but only initiating 1
187+
circuit.add_load_nodes(names=["640", "641"])
188+
189+
with self.assertRaises(ValueError):
190+
# Trying to create a line between a node and itself (nickname)
191+
circuit.add_lines(connections=[("load0", "0")])
192+
193+
circuit.solve()
194+
print(circuit.results(["Voltages", "Losses"]))
195+
165196

166197
class TestCustomizedCircuit(unittest.TestCase):
167198
"""

0 commit comments

Comments
 (0)