/
CtrlQueueTest.py
148 lines (117 loc) · 5.73 KB
/
CtrlQueueTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'''
2019-02-28: Ported from CtrlQueueTest.bas by Paulo Meira (@pmeira)
'''
USE_COM = False # Change to True to test with the COM DLL
if not USE_COM:
from dss import DSS as DSSobj
else:
import os
old_cd = os.getcwd()
import win32com.client
DSSobj = win32com.client.gencache.EnsureDispatch('OpenDSSengine.DSS')
os.chdir(old_cd)
DSSText = DSSobj.Text
DSSCircuit = DSSobj.ActiveCircuit
DSSSolution = DSSCircuit.Solution
DSSControlQueue = DSSCircuit.CtrlQueue
DSSCktElement = DSSCircuit.ActiveCktElement
DSSPDElement = DSSCircuit.PDElements
DSSMeters = DSSCircuit.Meters
DSSBus = DSSCircuit.ActiveBus
DSSCmath = DSSobj.CmathLib
DSSParser = DSSobj.Parser
DSSIsources = DSSCircuit.ISources
DSSMonitors = DSSCircuit.Monitors
DSSCapacitors = DSSCircuit.Capacitors
def TestCtrlQueue():
'''Example of implementing a simple voltage control for Capacitors via the COM interface'''
# Run simple capacitor interface test and execute local cap control that emulates CapControl
# with these settings:
# PT=125.09 Type=voltage onsetting=118.8 offsetting=121.2
# this test case has a four-step capacitor bank named "cap" and can be found in the Test Folder
DSSText.Command = r"Compile Master_TestCapInterface.DSS"
# Set all capacitor steps open for first capacitor
iStates = [0] * 10
iCap = DSSCapacitors.First # should check iCap for >0
for i in range(DSSCapacitors.NumSteps):
iStates[i] = 0
DSSCapacitors.States = iStates # push over the interface to OpenDSS
# check to make sure it worked
DSSText.Command = "? Capacitor.Cap.States"
strValue = "Starting Capacitor Step States=" + DSSText.Result # should be [0 0 0 0]
print(strValue)
# Base solution
DSSSolution.Solve()
# Each message we push onto the queue will get a 5 s delay
hour = 0
secDelay = 5 # delay
PTratio = 125.09 # for 26 kV system
ONsetting = 118.8
OFFsetting = 121.2
ActionCodeAdd = 201 # just an arbitrary action code
ActionCodeSub = 202 # just another arbitrary action code
DeviceHandle = 123 # arbitrary handle that signifies this control
# now, we'll crank the load up in 10% steps, checking the voltage at each step
# until all cap steps are on (no more available)
i = 0
while DSSCapacitors.AvailableSteps > 0:
print('DSSCapacitors.AvailableSteps', DSSCapacitors.AvailableSteps)
i = i + 1
DSSSolution.LoadMult = 1 + i * 0.1 # 10% more each time
DSSSolution.InitSnap()
DSSSolution.SolveNoControl()
DSSSolution.SampleControlDevices() # sample all other controls
# Emulate the cap control Sample Routine and get the bus voltage
DSSCircuit.SetActiveBus("feedbus")
V = DSSBus.VMagAngle
# check the first phase magnitude
Vreg = V[0] / PTratio
print("Step", i, "Voltage=", Vreg, "LoadMult=", DSSSolution.LoadMult)
if Vreg < ONsetting: # push a message to bump up the number of steps
DSSControlQueue.Push(hour, secDelay, ActionCodeAdd, DeviceHandle)
DSSSolution.DoControlActions() # this sends actions to the local action list
print('DSSControlQueue.NumActions', DSSControlQueue.NumActions)
if DSSControlQueue.NumActions > 0:
while DSSControlQueue.PopAction > 0:
devHandle = DSSControlQueue.DeviceHandle
if devHandle == DeviceHandle:
iCap = DSSCapacitors.First # Sets designated capacitor active
currentActionCode = DSSControlQueue.ActionCode
if currentActionCode == ActionCodeAdd:
DSSCapacitors.AddStep()
elif currentActionCode == ActionCodeSub:
DSSCapacitors.SubtractStep()
# Print result
DSSText.Command = "? Capacitor." + DSSCapacitors.Name + ".States"
print("Capacitor " + DSSCapacitors.Name + " States=" + DSSText.Result)
# Now let's reverse Direction and start removing steps
while DSSCapacitors.AvailableSteps < DSSCapacitors.NumSteps:
i = i - 1
DSSSolution.LoadMult = 1 + i * 0.1 # 10% more each time
DSSSolution.InitSnap()
DSSSolution.SolveNoControl()
DSSSolution.SampleControlDevices() # sample all other controls
# Emulate the cap control Sample Routine and get the bus voltage
DSSCircuit.SetActiveBus("feedbus")
V = DSSBus.VMagAngle
# check the first phase magnitude
Vreg = V[0] / PTratio
print("Step", i, "Voltage=", Vreg, "LoadMult=", DSSSolution.LoadMult)
if Vreg > OFFsetting: # push a message to bump down the number of steps
DSSControlQueue.Push(hour, secDelay, ActionCodeSub, DeviceHandle)
DSSSolution.DoControlActions() # this send actions to the local action list
if DSSControlQueue.NumActions > 0:
while DSSControlQueue.PopAction > 0:
devHandle = DSSControlQueue.DeviceHandle
if devHandle == DeviceHandle:
iCap = DSSCapacitors.First # Sets designated capacitor active
currentActionCode = DSSControlQueue.ActionCode
if currentActionCode == ActionCodeAdd:
DSSCapacitors.AddStep()
elif currentActionCode == ActionCodeSub:
DSSCapacitors.SubtractStep()
# Print result
DSSText.Command = "? Capacitor." + DSSCapacitors.Name + ".States"
print("Capacitor " + DSSCapacitors.Name + " States=" + DSSText.Result)
if __name__ == '__main__':
TestCtrlQueue()