Skip to content

Commit f7ea1cc

Browse files
committed
starter error handling
1 parent fa4d386 commit f7ea1cc

File tree

12 files changed

+115
-17
lines changed

12 files changed

+115
-17
lines changed
-31 Bytes
Binary file not shown.
191 Bytes
Binary file not shown.
258 Bytes
Binary file not shown.
701 Bytes
Binary file not shown.
188 Bytes
Binary file not shown.

pygridsim/core.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def __init__(self):
2020
self.num_lines = 0
2121
self.num_transformers = 0
2222
altdss.ClearAll()
23-
#altdss('new circuit.IEEE13Nodeckt')
2423
altdss('new circuit.MyCircuit')
2524

2625
def add_load_nodes(self, params = {}, load_type: LoadType = LoadType.HOUSE, num = 1):

pygridsim/defaults.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,11 @@
6767
NUM_WINDINGS = 2
6868
XHL = 2
6969
PRIMARY_CONN = Connection.delta
70-
SECONDARY_CONN = Connection.wye
70+
SECONDARY_CONN = Connection.wye
71+
72+
"""
73+
Valid parameter lists
74+
"""
75+
VALID_LOAD_PARAMS = ["kV", "kW", "kVar", "phases"]
76+
VALID_SOURCE_PARAMS = ["kV", "phases", "frequency"]
77+
VALID_LINE_TRANSFORMER_PARAMS = ["length", "XHL", "Conns"]

pygridsim/lines.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from altdss import altdss
22
from altdss import Transformer, Connection
33
import pygridsim.defaults as defaults
4-
from pygridsim.parameters import get_param, random_param
4+
from pygridsim.parameters import get_param, random_param, check_valid_params
55
from dss.enums import LineUnits
66

77
def make_line(src, dst, line_type, count, params = {}, transformer = True):
@@ -15,13 +15,17 @@ def make_line(src, dst, line_type, count, params = {}, transformer = True):
1515
Returns:
1616
Line object that was created
1717
"""
18+
check_valid_params(params, defaults.VALID_LINE_TRANSFORMER_PARAMS)
1819
line = altdss.Line.new('line' + str(count))
1920
line.Phases = defaults.PHASES
2021
line.Length = get_param(params, "length", random_param(line_type.value))
2122
line.Bus1 = src
2223
line.Bus2 = dst
2324
line.Units = LineUnits.km
2425

26+
if (line.Length) < 0:
27+
raise ValueError("Cannot have negative length")
28+
2529
if not transformer:
2630
return
2731

@@ -33,4 +37,4 @@ def make_line(src, dst, line_type, count, params = {}, transformer = True):
3337
transformer.Buses = [src, dst]
3438
transformer.Conns = get_param(params, "Conns", [defaults.PRIMARY_CONN, defaults.SECONDARY_CONN])
3539
transformer.kVs = [altdss.Vsource[src].BasekV, altdss.Load[dst].kV]
36-
transformer.end_edit()
40+
transformer.end_edit()

pygridsim/parameters.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def get_param(params, name, default):
3131
else:
3232
return default
3333

34+
def check_valid_params(params, valid_params):
35+
# Invalid parameter handling
36+
for param in params:
37+
if param not in valid_params:
38+
raise KeyError(f"Parameter {param} is not supported")
39+
3440
def make_load_node(load_params, load_type, count):
3541
"""
3642
Make a load node with the parmeters given, filling in with defaults for
@@ -44,13 +50,18 @@ def make_load_node(load_params, load_type, count):
4450
Return:
4551
load object
4652
"""
53+
check_valid_params(load_params, defaults.VALID_LOAD_PARAMS)
54+
4755
load : Load = altdss.Load.new('load' + str(count))
4856
load.Bus1 = 'load' + str(count)
4957
load.Phases = get_param(load_params, "phases", defaults.PHASES)
5058
load.kV = get_param(load_params, "kV", random_param(load_type.value["kV"]))
5159
load.kW = get_param(load_params, "kW", random_param(load_type.value["kW"]))
5260
load.kvar = get_param(load_params, "kVar", random_param(load_type.value["kVar"]))
5361
load.Daily = 'default'
62+
63+
if (load.kV) < 0:
64+
raise ValueError("Cannot have negative voltage in load")
5465
return load
5566

5667
def make_source_node(source_params, source_type, count, num_in_batch = 1):
@@ -70,11 +81,15 @@ def make_source_node(source_params, source_type, count, num_in_batch = 1):
7081
TODO: There is a whole set of other vsource properties to set, like impedance and resistance
7182
https://github.com/dss-extensions/AltDSS-Python/blob/2b6fa7e5961cedaf8482c07d377b20bdab4a1bee/altdss/Vsource.py#L694
7283
"""
84+
check_valid_params(source_params, defaults.VALID_SOURCE_PARAMS)
85+
7386
source = altdss.Vsource[0]
74-
#source = altdss.Vsource['source' + str(count)]
75-
#source: Vsource = altdss.Vsource.new('source' + str(count))
7687
source.Bus1 = 'source'
7788
source.Phases = get_param(source_params, "phases", defaults.PHASES)
7889
source.BasekV = get_param(source_params, "kV", num_in_batch*random_param(source_type.value))
7990
source.Frequency = get_param(source_params, "frequency", defaults.FREQUENCY)
91+
92+
if (source.BasekV) < 0:
93+
raise ValueError("Cannot have negative voltage in source")
94+
8095
return source

pygridsim/results.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ def query_solution(query):
2121
bus_vmags[bus_name] = float(bus_vmag)
2222
return bus_vmags
2323
case "Losses":
24-
# Currently has some minimal value like e-13, because there is no transformer
25-
return altdss.Losses()
24+
# Parse it to output active power loss, reactive power loss, instead of just complex number.
25+
vector_losses = altdss.Losses()
26+
losses = {}
27+
losses["Active Power Loss"] = vector_losses.real
28+
losses["Reactive Power Loss"] = vector_losses.imag
29+
return losses
2630
case "TotalPower":
2731
return altdss.TotalPower()
2832
case _:

0 commit comments

Comments
 (0)