-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.jl
More file actions
101 lines (86 loc) · 2.51 KB
/
Copy pathutils.jl
File metadata and controls
101 lines (86 loc) · 2.51 KB
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
import Base.Iterators: product
convert_data(data::N, backend) where {names,N<:NamedTuple{names}} =
NamedTuple{names}(ExaModels.convert_array(d, backend) for d in data)
# Get all PV buses
function get_pv_buses(data)
return [b.i for b in data.bus if b.bus_type ∈ [2, 3]]
end
# Get all PQ buses
function get_pq_buses(data)
return [b.i for b in data.bus if b.bus_type == 1]
end
# Get all reference buses
function get_ref_buses(data)
return [b.i for b in data.bus if b.bus_type == 3]
end
# Get generators associated to PV nodes
function get_free_generators(data)
return [g.i for g in data.gen if data.bus[g.bus].bus_type == 2]
end
# Return fixed variables in power flow problem.
function get_index_dof(data, K=1)
nbus = length(data.bus)
ngen = length(data.gen)
# Degrees-of-freedom are:
# 1. voltage angle at reference buses
ref = get_ref_buses(data)
# 2. voltage magnitude at reference and PV buses
pv = get_pv_buses(data)
# 3. active power generation at PV buses
gen = get_free_generators(data)
n_dof = length(ref) + length(pv) + length(gen)
fixed_variables = zeros(Int, n_dof * K)
shift_vm = nbus * K
shift_gen = 2 * nbus * K
cnt = 0
for k in 1:K, i in ref
fixed_variables[cnt += 1] = i + (k-1)*nbus
end
for k in 1:K, i in pv
fixed_variables[cnt += 1] = i + (k-1)*nbus + shift_vm
end
for k in 1:K, i in gen
fixed_variables[cnt += 1] = i + (k-1)*ngen + shift_gen
end
return fixed_variables
end
function get_block_reordering(data, K)
nbus = length(data.bus)
ngen = length(data.gen)
nlines = length(data.branch)
nvar = (2*nbus + 2*ngen + 4*nlines) * K
shift_vm = nbus * K
shift_pg = 2 * nbus * K
shift_qg = 2 * nbus * K + ngen * K
shift_p = 2 * nbus * K + 2 * ngen * K
perm = zeros(Int, nvar)
cnt = 0
for k in 1:K
# Va
for i in 1:nbus
perm[cnt += 1] = i + (k-1)*nbus
end
# Vm
for i in 1:nbus
perm[cnt += 1] = i + (k-1)*nbus + shift_vm
end
# Pg
for i in 1:ngen
perm[cnt += 1] = i + (k-1)*ngen + shift_pg
end
# Qg
for i in 1:ngen
perm[cnt += 1] = i + (k-1)*ngen + shift_qg
end
# P
for i in 1:2*nlines
perm[cnt += 1] = i + (k-1)*2*nlines + shift_p
end
# Q
for i in 1:2*nlines
perm[cnt += 1] = i + (k-1)*2*nlines + shift_p + 2*nlines*K
end
end
return perm
end
nothing