/
ckt_element.jl
378 lines (320 loc) · 14.2 KB
/
ckt_element.jl
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
module CktElement
using DocStringExtensions
using ..Lib
using ..Utils
@template (FUNCTIONS, METHODS) = """
$(TYPEDSIGNATURES)
$(DOCSTRING)
"""
"""Close phase of terminal for active circuit element"""
function Close(dss::DSSContext, Term::Int, Phs::Int)
@checked Lib.CktElement_Close(dss.ctx, Term, Phs)
end
Close(Term::Int, Phs::Int) = Close(DSS_DEFAULT_CTX, Term, Phs)
"""Full name of the i-th controller attached to this element. Ex: str = Controller(2). See NumControls to determine valid index range"""
function Controller(dss::DSSContext, idx::Int)::String
return get_string(Lib.CktElement_Get_Controller(dss.ctx, idx))
end
Controller(idx::Int) = Controller(DSS_DEFAULT_CTX, idx)
"""For PCElement, get the value of a variable by name. (Getter)"""
function Variable(dss::DSSContext, MyVarName::String, Unused::Int)::Float64
Code = Ref{Int32}(-1)
result = @checked Lib.CktElement_Get_Variable(dss.ctx, MyVarName, Code)
if Code[] != 0
throw(
OpenDSSDirectException(
"[ERROR] '$MyVarName' is not a valid variable name or the current object is not a PCElement."
)
)
end
return result
end
Variable(MyVarName::String, Unused::Int) = Variable(DSS_DEFAULT_CTX, MyVarName, Unused)
"""For PCElement, set the value of a variable by name. (Setter)"""
function Variable(dss::DSSContext, MyVarName::String, Unused::Int, Value::Float64)
Code = Ref{Int32}(-1)
@checked Lib.CktElement_Set_Variable(dss.ctx, MyVarName, Code, Value)
if Code[] != 0
throw(
OpenDSSDirectException(
"[ERROR] '$MyVarName' is not a valid variable name or the current object is not a PCElement."
)
)
end
end
Variable(MyVarName::String, Unused::Int, Value::Float64) = Variable(DSS_DEFAULT_CTX, MyVarName, Unused, Value)
"""For PCElement, get the value of a variable by integer index. (Getter)"""
function Variablei(dss::DSSContext, Idx::Int, Unused::Int)::Float64
Code = Ref{Int32}(-1)
result = @checked Lib.CktElement_Get_Variablei(dss.ctx, Idx, Code)
if Code[] != 0
throw(
OpenDSSDirectException(
"[ERROR] $Idx is not a valid variable index or the current object is not a PCElement."
)
)
end
return result
end
Variablei(Idx::Int, Unused::Int) = Variablei(DSS_DEFAULT_CTX, Idx, Unused)
"""For PCElement, set the value of a variable by integer index. (Setter)"""
function Variablei(dss::DSSContext, Idx::Int, Unused::Int, Value::Float64)
Code = Ref{Int32}(-1)
@checked Lib.CktElement_Set_Variablei(dss.ctx, Idx, Code, Value)
if Code[] != 0
throw(
OpenDSSDirectException(
"[ERROR] $Idx is not a valid variable index or the current object is not a PCElement."
)
)
end
end
Variablei(Idx::Int, Unused::Int, Value::Float64) = Variablei(DSS_DEFAULT_CTX, Idx, Unused, Value)
"""Check if open phase of terminal for active circuit element"""
function IsOpen(dss::DSSContext, Term::Int, Phs::Int)::Bool
return @checked(Lib.CktElement_IsOpen(dss.ctx, Term, Phs)) != 0
end
IsOpen(Term::Int, Phs::Int) = IsOpen(DSS_DEFAULT_CTX, Term, Phs)
"""Open phase of terminal for active circuit element"""
function Open(dss::DSSContext, Term::Int, Phs::Int)
@checked Lib.CktElement_Open(dss.ctx, Term, Phs)
end
Open(Term::Int, Phs::Int) = Open(DSS_DEFAULT_CTX, Term, Phs)
"""Array containing all property names of the active device."""
function AllPropertyNames(dss::DSSContext)::Vector{String}
return get_string_array(Lib.CktElement_Get_AllPropertyNames, dss.ctx)
end
AllPropertyNames() = AllPropertyNames(DSS_DEFAULT_CTX)
"""Array of strings listing all the published variable names, if a PCElement. Otherwise, null string."""
function AllVariableNames(dss::DSSContext)::Vector{String}
return get_string_array(Lib.CktElement_Get_AllVariableNames, dss.ctx)
end
AllVariableNames() = AllVariableNames(DSS_DEFAULT_CTX)
"""Array of doubles. Values of state variables of active element if PC element."""
function AllVariableValues(dss::DSSContext)::Vector{Float64}
return get_float64_array(Lib.CktElement_Get_AllVariableValues, dss.ctx)
end
AllVariableValues() = AllVariableValues(DSS_DEFAULT_CTX)
"""
Array of strings. Get Bus definitions to which each terminal is connected. 0-based array.
"""
function BusNames(dss::DSSContext)::Vector{String}
return get_string_array(Lib.CktElement_Get_BusNames, dss.ctx)
end
BusNames() = BusNames(DSS_DEFAULT_CTX)
"""
Array of strings. Set Bus definitions for each terminal is connected.
"""
function BusNames(dss::DSSContext, Value::Vector{String})
Value, ValuePtr, ValueCount = prepare_string_array(Value)
@checked Lib.CktElement_Set_BusNames(dss.ctx, ValuePtr, ValueCount)
end
BusNames(Value::Vector{String}) = BusNames(DSS_DEFAULT_CTX, Value)
"""Complex double array of Sequence Currents for all conductors of all terminals of active circuit element."""
function CplxSeqCurrents(dss::DSSContext)::Vector{ComplexF64}
return get_complex64_array(Lib.CktElement_Get_CplxSeqCurrents, dss.ctx)
end
CplxSeqCurrents() = CplxSeqCurrents(DSS_DEFAULT_CTX)
"""Complex double array of Sequence Voltage for all terminals of active circuit element."""
function CplxSeqVoltages(dss::DSSContext)::Vector{ComplexF64}
return get_complex64_array(Lib.CktElement_Get_CplxSeqVoltages, dss.ctx)
end
CplxSeqVoltages() = CplxSeqVoltages(DSS_DEFAULT_CTX)
"""Complex array of currents into each conductor of each terminal"""
function Currents(dss::DSSContext)::Vector{ComplexF64}
return get_complex64_array(Lib.CktElement_Get_Currents, dss.ctx)
end
Currents() = Currents(DSS_DEFAULT_CTX)
"""Currents in magnitude, angle format as a array of doubles."""
function CurrentsMagAng(dss::DSSContext)::Array{Float64,2}
r = get_float64_array(Lib.CktElement_Get_CurrentsMagAng, dss.ctx)
return reshape(r, (2, Int(length(r) / 2)))
end
CurrentsMagAng() = CurrentsMagAng(DSS_DEFAULT_CTX)
"""Display name of the object (not necessarily unique) (Getter)"""
function DisplayName(dss::DSSContext)::String
return get_string(Lib.CktElement_Get_DisplayName(dss.ctx))
end
DisplayName() = DisplayName(DSS_DEFAULT_CTX)
"""Display name of the object (not necessarily unique) (Setter)"""
function DisplayName(dss::DSSContext, Value::String)
@checked Lib.CktElement_Set_DisplayName(dss.ctx, Cstring(pointer(Value)))
end
DisplayName(Value::String) = DisplayName(DSS_DEFAULT_CTX, Value)
"""Emergency Ampere Rating for PD elements (Getter)"""
function EmergAmps(dss::DSSContext)::Float64
return @checked Lib.CktElement_Get_EmergAmps(dss.ctx)
end
EmergAmps() = EmergAmps(DSS_DEFAULT_CTX)
"""Emergency Ampere Rating for PD elements (Setter)"""
function EmergAmps(dss::DSSContext, Value::Float64)
@checked Lib.CktElement_Set_EmergAmps(dss.ctx, Value)
end
EmergAmps(Value::Float64) = EmergAmps(DSS_DEFAULT_CTX, Value)
"""Boolean indicating that element is currently in the circuit. (Getter)"""
function Enabled(dss::DSSContext)::Bool
return @checked(Lib.CktElement_Get_Enabled(dss.ctx)) != 0
end
Enabled() = Enabled(DSS_DEFAULT_CTX)
"""Boolean indicating that element is currently in the circuit. (Setter)"""
function Enabled(dss::DSSContext, Value::Bool)
@checked Lib.CktElement_Set_Enabled(dss.ctx, Value ? 1 : 0)
end
Enabled(Value::Bool) = Enabled(DSS_DEFAULT_CTX, Value)
"""Name of the Energy Meter this element is assigned to."""
function EnergyMeter(dss::DSSContext)::String
return get_string(Lib.CktElement_Get_EnergyMeter(dss.ctx))
end
EnergyMeter() = EnergyMeter(DSS_DEFAULT_CTX)
"""globally unique identifier for this object"""
function GUID(dss::DSSContext)::String
return get_string(Lib.CktElement_Get_GUID(dss.ctx))
end
GUID() = GUID(DSS_DEFAULT_CTX)
"""Pointer to this object"""
function Handle(dss::DSSContext)::Int
return @checked Lib.CktElement_Get_Handle(dss.ctx)
end
Handle() = Handle(DSS_DEFAULT_CTX)
"""True if a recloser, relay, or fuse controlling this ckt element. OCP = Overcurrent Protection """
function HasOCPDevice(dss::DSSContext)::Bool
return @checked(Lib.CktElement_Get_HasOCPDevice(dss.ctx)) != 0
end
HasOCPDevice() = HasOCPDevice(DSS_DEFAULT_CTX)
"""This element has a SwtControl attached."""
function HasSwitchControl(dss::DSSContext)::Bool
return @checked(Lib.CktElement_Get_HasSwitchControl(dss.ctx)) != 0
end
HasSwitchControl() = HasSwitchControl(DSS_DEFAULT_CTX)
"""This element has a CapControl or RegControl attached."""
function HasVoltControl(dss::DSSContext)::Bool
return @checked(Lib.CktElement_Get_HasVoltControl(dss.ctx)) != 0
end
HasVoltControl() = HasVoltControl(DSS_DEFAULT_CTX)
"""Total losses in the element: two-element complex array"""
function Losses(dss::DSSContext)::Vector{ComplexF64}
return get_complex64_array(Lib.CktElement_Get_Losses, dss.ctx)
end
Losses() = Losses(DSS_DEFAULT_CTX)
"""Full Name of Active Circuit Element"""
function Name(dss::DSSContext)::String
return get_string(Lib.CktElement_Get_Name(dss.ctx))
end
Name() = Name(DSS_DEFAULT_CTX)
"""Array of integer containing the node numbers (representing phases, for example) for each conductor of each terminal. """
function NodeOrder(dss::DSSContext)::Vector{Int}
return get_int32_array(Lib.CktElement_Get_NodeOrder, dss.ctx)
end
NodeOrder() = NodeOrder(DSS_DEFAULT_CTX)
"""Normal ampere rating for PD Elements (Getter)"""
function NormalAmps(dss::DSSContext)::Float64
return @checked Lib.CktElement_Get_NormalAmps(dss.ctx)
end
NormalAmps() = NormalAmps(DSS_DEFAULT_CTX)
"""Normal ampere rating for PD Elements (Setter)"""
function NormalAmps(dss::DSSContext, Value::Float64)
@checked Lib.CktElement_Set_NormalAmps(dss.ctx, Value)
end
NormalAmps(Value::Float64) = NormalAmps(DSS_DEFAULT_CTX, Value)
"""Number of Conductors per Terminal"""
function NumConductors(dss::DSSContext)::Int
return @checked Lib.CktElement_Get_NumConductors(dss.ctx)
end
NumConductors() = NumConductors(DSS_DEFAULT_CTX)
"""Number of controls connected to this device. Use to determine valid range for index into Controller array."""
function NumControls(dss::DSSContext)::Int
return @checked Lib.CktElement_Get_NumControls(dss.ctx)
end
NumControls() = NumControls(DSS_DEFAULT_CTX)
"""Number of Phases"""
function NumPhases(dss::DSSContext)::Int
return @checked Lib.CktElement_Get_NumPhases(dss.ctx)
end
NumPhases() = NumPhases(DSS_DEFAULT_CTX)
"""Number of Properties this Circuit Element."""
function NumProperties(dss::DSSContext)::Int
return @checked Lib.CktElement_Get_NumProperties(dss.ctx)
end
NumProperties() = NumProperties(DSS_DEFAULT_CTX)
"""Number of Terminals this Circuit Element"""
function NumTerminals(dss::DSSContext)::Int
return @checked Lib.CktElement_Get_NumTerminals(dss.ctx)
end
NumTerminals() = NumTerminals(DSS_DEFAULT_CTX)
"""Index into Controller list of OCP Device controlling this CktElement"""
function OCPDevIndex(dss::DSSContext)::Int
return @checked Lib.CktElement_Get_OCPDevIndex(dss.ctx)
end
OCPDevIndex() = OCPDevIndex(DSS_DEFAULT_CTX)
"""0=None; 1=Fuse; 2=Recloser; 3=Relay; Type of OCP controller device"""
function OCPDevType(dss::DSSContext)::Int
return @checked Lib.CktElement_Get_OCPDevType(dss.ctx)
end
OCPDevType() = OCPDevType(DSS_DEFAULT_CTX)
"""Complex array of losses by phase"""
function PhaseLosses(dss::DSSContext)::Vector{ComplexF64}
return get_complex64_array(Lib.CktElement_Get_PhaseLosses, dss.ctx)
end
PhaseLosses() = PhaseLosses(DSS_DEFAULT_CTX)
"""Complex array of powers into each conductor of each terminal"""
function Powers(dss::DSSContext)::Vector{ComplexF64}
return get_complex64_array(Lib.CktElement_Get_Powers, dss.ctx)
end
Powers() = Powers(DSS_DEFAULT_CTX)
"""Residual currents for each terminal: (mag, angle)"""
function Residuals(dss::DSSContext)::Array{Float64,2}
r = get_float64_array(Lib.CktElement_Get_Residuals, dss.ctx)
return reshape(r, (2, Int(length(r) / 2)))
end
Residuals() = Residuals(DSS_DEFAULT_CTX)
"""Double array of symmetrical component currents into each 3-phase terminal"""
function SeqCurrents(dss::DSSContext)::Vector{Float64}
return get_float64_array(Lib.CktElement_Get_SeqCurrents, dss.ctx)
end
SeqCurrents() = SeqCurrents(DSS_DEFAULT_CTX)
"""Double array of sequence powers into each 3-phase teminal"""
function SeqPowers(dss::DSSContext)::Vector{ComplexF64}
return get_complex64_array(Lib.CktElement_Get_SeqPowers, dss.ctx)
end
SeqPowers() = SeqPowers(DSS_DEFAULT_CTX)
"""Double array of symmetrical component voltages at each 3-phase terminal"""
function SeqVoltages(dss::DSSContext)::Vector{Float64}
return get_float64_array(Lib.CktElement_Get_SeqVoltages, dss.ctx)
end
SeqVoltages() = SeqVoltages(DSS_DEFAULT_CTX)
"""Returns the total powers (complex) at ALL terminals of the active circuit element."""
function TotalPowers(dss::DSSContext)::Vector{ComplexF64}
return get_complex64_array(Lib.CktElement_Get_TotalPowers, dss.ctx)
end
TotalPowers() = TotalPowers(DSS_DEFAULT_CTX)
"""Complex array of voltages at terminals"""
function Voltages(dss::DSSContext)::Vector{ComplexF64}
return get_complex64_array(Lib.CktElement_Get_Voltages, dss.ctx)
end
Voltages() = Voltages(DSS_DEFAULT_CTX)
"""Voltages at each conductor in magnitude, angle form as array of doubles."""
function VoltagesMagAng(dss::DSSContext)::Array{Float64,2}
r = get_float64_array(Lib.CktElement_Get_VoltagesMagAng, dss.ctx)
return reshape(r, (2, Int(length(r) / 2)))
end
VoltagesMagAng() = VoltagesMagAng(DSS_DEFAULT_CTX)
"""YPrim matrix, column order, complex numbers (paired)"""
function YPrim(dss::DSSContext)::Array{ComplexF64,2}
r = get_complex64_array(Lib.CktElement_Get_Yprim, dss.ctx)
# TODO: should we transpose here?
return reshape(r, (Int(sqrt(length(r))), Int(sqrt(length(r)))))
end
YPrim() = YPrim(DSS_DEFAULT_CTX)
"""Returns true if the current active element is isolated.
Note that this only fetches the current value. See also the Topology interface."""
function IsIsolated(dss::DSSContext)::Bool
return (@checked Lib.CktElement_Get_IsIsolated(dss.ctx)) != 0
end
IsIsolated() = IsIsolated(DSS_DEFAULT_CTX)
"""Array of integers, a copy of the internal NodeRef of the CktElement.
(API Extension)"""
function NodeRef(dss::DSSContext)::Vector{Int}
return get_int32_array(Lib.CktElement_Get_NodeRef, dss.ctx)
end
NodeRef() = NodeRef(DSS_DEFAULT_CTX)
end