SolUtil 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- SolUtil/__init__.py +5 -0
- SolUtil/_version.py +24 -0
- SolUtil/alternate_solver/__init__.py +1 -0
- SolUtil/alternate_solver/solver.py +127 -0
- SolUtil/energyflow/__init__.py +5 -0
- SolUtil/energyflow/dhs_fault_flow.py +719 -0
- SolUtil/energyflow/dhs_flow.py +395 -0
- SolUtil/energyflow/gas_flow.py +147 -0
- SolUtil/energyflow/hydraulic_mdl.py +232 -0
- SolUtil/energyflow/power_flow.py +338 -0
- SolUtil/energyflow/test/__init__.py +0 -0
- SolUtil/energyflow/test/test_dhs_fault_flow/case_heat_test.xlsx +0 -0
- SolUtil/energyflow/test/test_dhs_fault_flow/case_heat_test0.xlsx +0 -0
- SolUtil/energyflow/test/test_dhs_fault_flow/case_heat_test1.xlsx +0 -0
- SolUtil/energyflow/test/test_dhs_fault_flow.py +27 -0
- SolUtil/energyflow/test/test_dhs_flow/BarryIsland.xlsx +0 -0
- SolUtil/energyflow/test/test_dhs_flow.py +24 -0
- SolUtil/energyflow/test/test_gas_flow/belgium.xlsx +0 -0
- SolUtil/energyflow/test/test_gas_flow.py +20 -0
- SolUtil/energyflow/test/test_load_matpower/case9.m +70 -0
- SolUtil/energyflow/test/test_load_matpower.py +139 -0
- SolUtil/energyflow/test/test_power_flow/case9.xlsx +0 -0
- SolUtil/energyflow/test/test_power_flow.py +98 -0
- SolUtil/error_cal.py +9 -0
- SolUtil/profile.py +9 -0
- SolUtil/sysparser/__init__.py +3 -0
- SolUtil/sysparser/_array_utils.py +6 -0
- SolUtil/sysparser/dhs_function.py +209 -0
- SolUtil/sysparser/eps_function.py +395 -0
- SolUtil/sysparser/ngs_function.py +160 -0
- SolUtil/tf2ss.py +14 -0
- solutil-0.1.0.dist-info/METADATA +69 -0
- solutil-0.1.0.dist-info/RECORD +36 -0
- solutil-0.1.0.dist-info/WHEEL +5 -0
- solutil-0.1.0.dist-info/licenses/LICENSE +21 -0
- solutil-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import warnings
|
|
2
|
+
|
|
3
|
+
from pyomo.environ import (Reals, Var, AbstractModel, Set, Param, Constraint, minimize,
|
|
4
|
+
SolverFactory, Objective, PositiveReals)
|
|
5
|
+
import numpy as np
|
|
6
|
+
from Solverz import Var as SolVar, Param as SolParam, Eqn, Model, made_numerical, nr_method, module_printer, Sign
|
|
7
|
+
from Solverz.num_api.Array import Array
|
|
8
|
+
from networkx import DiGraph, incidence_matrix
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def hydraulic_opt_mdl(alpha):
|
|
12
|
+
m = AbstractModel()
|
|
13
|
+
m.Nodes = Set()
|
|
14
|
+
m.Arcs = Set(dimen=2)
|
|
15
|
+
m.slack_nodes = Set()
|
|
16
|
+
m.non_slack_nodes = Set()
|
|
17
|
+
|
|
18
|
+
def NodesIn_init(m, node):
|
|
19
|
+
for i, j in m.Arcs:
|
|
20
|
+
if j == node:
|
|
21
|
+
yield i
|
|
22
|
+
|
|
23
|
+
m.NodesIn = Set(m.Nodes, initialize=NodesIn_init)
|
|
24
|
+
|
|
25
|
+
def NodesOut_init(m, node):
|
|
26
|
+
for i, j in m.Arcs:
|
|
27
|
+
if i == node:
|
|
28
|
+
yield j
|
|
29
|
+
|
|
30
|
+
m.NodesOut = Set(m.Nodes, initialize=NodesOut_init)
|
|
31
|
+
|
|
32
|
+
m.f = Var(m.Arcs, domain=Reals)
|
|
33
|
+
m.c = Param(m.Arcs, mutable=True)
|
|
34
|
+
m.fs = Param(m.Nodes, mutable=True)
|
|
35
|
+
m.fs_slack = Var(m.slack_nodes, domain=Reals)
|
|
36
|
+
m.fl = Param(m.Nodes, mutable=True)
|
|
37
|
+
m.Hset = Param(m.slack_nodes, mutable=True, domain=Reals)
|
|
38
|
+
m.delta = Param(m.Arcs, domain=Reals)
|
|
39
|
+
|
|
40
|
+
def m_continuity_rule1(m, node):
|
|
41
|
+
return m.fl[node] - m.fs_slack[node] == sum(m.f[i, node] for i in m.NodesIn[node]) - sum(
|
|
42
|
+
m.f[node, j] for j in m.NodesOut[node])
|
|
43
|
+
|
|
44
|
+
m.m_balance1 = Constraint(m.slack_nodes, rule=m_continuity_rule1)
|
|
45
|
+
|
|
46
|
+
def m_continuity_rule2(m, node):
|
|
47
|
+
return m.fl[node] - m.fs[node] == sum(m.f[i, node] for i in m.NodesIn[node]) - sum(
|
|
48
|
+
m.f[node, j] for j in m.NodesOut[node])
|
|
49
|
+
|
|
50
|
+
m.m_balance2 = Constraint(m.non_slack_nodes, rule=m_continuity_rule2)
|
|
51
|
+
|
|
52
|
+
def obj(m):
|
|
53
|
+
obj_ = sum(
|
|
54
|
+
m.c[i, j] * abs(m.f[i, j]) ** (alpha + 1) / (alpha + 1) - m.delta[i, j] * m.f[i, j] for i, j in m.Arcs)
|
|
55
|
+
obj_ -= sum(m.Hset[i] * (m.fs_slack[i] - m.fl[i]) for i in m.slack_nodes)
|
|
56
|
+
return obj_
|
|
57
|
+
|
|
58
|
+
m.Obj = Objective(rule=obj, sense=minimize)
|
|
59
|
+
|
|
60
|
+
return m
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class HydraFlow:
|
|
64
|
+
|
|
65
|
+
def __init__(self,
|
|
66
|
+
slack_nodes,
|
|
67
|
+
non_slack_nodes,
|
|
68
|
+
c,
|
|
69
|
+
fs,
|
|
70
|
+
fl,
|
|
71
|
+
Hset,
|
|
72
|
+
delta,
|
|
73
|
+
pinloop,
|
|
74
|
+
G,
|
|
75
|
+
alpha=2):
|
|
76
|
+
self.mdl = hydraulic_opt_mdl(alpha)
|
|
77
|
+
self.cmdl = None
|
|
78
|
+
self.slack_nodes = Array(slack_nodes, dim=1, dtype=np.int32)
|
|
79
|
+
self.non_slack_nodes = Array(non_slack_nodes, dim=1, dtype=np.int32)
|
|
80
|
+
self.n_node = len(self.slack_nodes) + len(self.non_slack_nodes)
|
|
81
|
+
self.c = c
|
|
82
|
+
self.fs = fs
|
|
83
|
+
self.fl = fl
|
|
84
|
+
self.Hset = Hset
|
|
85
|
+
self.delta = delta
|
|
86
|
+
self.pinloop = pinloop
|
|
87
|
+
self.G: DiGraph = G
|
|
88
|
+
self.A = incidence_matrix(G,
|
|
89
|
+
nodelist=np.arange(self.n_node),
|
|
90
|
+
edgelist=sorted(G.edges(data=True), key=lambda edge: edge[2].get('idx', 1)),
|
|
91
|
+
oriented=True)
|
|
92
|
+
self.f = np.zeros(self.G.number_of_nodes())
|
|
93
|
+
self.H = np.zeros(self.G.number_of_nodes())
|
|
94
|
+
self.res = None
|
|
95
|
+
|
|
96
|
+
arcs = [(i, j) for i, j, k in sorted(self.G.edges(data=True), key=lambda edge: edge[2].get('idx', 1))]
|
|
97
|
+
nodes = np.arange(self.n_node)
|
|
98
|
+
slack_nodes = self.slack_nodes
|
|
99
|
+
non_slack_nodes = self.non_slack_nodes
|
|
100
|
+
c = dict(zip(arcs, self.c))
|
|
101
|
+
fs = dict(zip(nodes, self.fs))
|
|
102
|
+
fl = dict(zip(nodes, self.fl))
|
|
103
|
+
Hset = dict(zip(slack_nodes, self.Hset))
|
|
104
|
+
delta = dict(zip(arcs, self.delta))
|
|
105
|
+
data_dict = {
|
|
106
|
+
None: {
|
|
107
|
+
'Nodes': {None: nodes},
|
|
108
|
+
'slack_nodes': {None: slack_nodes},
|
|
109
|
+
'non_slack_nodes': {None: non_slack_nodes},
|
|
110
|
+
'Arcs': {None: arcs},
|
|
111
|
+
'fs': fs,
|
|
112
|
+
'fl': fl,
|
|
113
|
+
'c': c,
|
|
114
|
+
'Hset': Hset,
|
|
115
|
+
'delta': delta
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
self.cmdl = self.mdl.create_instance(data_dict)
|
|
120
|
+
# self.Hmdl, self.y0 = self.derive_H_mdl()
|
|
121
|
+
|
|
122
|
+
def update_fs(self, fs1):
|
|
123
|
+
for i in range(len(fs1)):
|
|
124
|
+
self.fs[i] = fs1[i]
|
|
125
|
+
self.cmdl.fs[i].value = fs1[i]
|
|
126
|
+
|
|
127
|
+
def update_fl(self, fl1):
|
|
128
|
+
for i in range(len(fl1)):
|
|
129
|
+
self.fl[i] = fl1[i]
|
|
130
|
+
self.cmdl.fl[i].value = fl1[i]
|
|
131
|
+
|
|
132
|
+
def update_Hset(self, H):
|
|
133
|
+
self.Hset = H
|
|
134
|
+
for i in range(len(H)):
|
|
135
|
+
self.cmdl.Hset[self.slack_nodes[i]].value = H[i]
|
|
136
|
+
|
|
137
|
+
def run(self, tee=False):
|
|
138
|
+
opt = SolverFactory('ipopt')
|
|
139
|
+
self.res = opt.solve(self.cmdl, tee=tee)
|
|
140
|
+
|
|
141
|
+
self.f = []
|
|
142
|
+
for i, j in self.cmdl.Arcs:
|
|
143
|
+
self.f.append(self.cmdl.f[i, j].value)
|
|
144
|
+
self.f = np.array(self.f)
|
|
145
|
+
|
|
146
|
+
for i in self.slack_nodes:
|
|
147
|
+
self.fs[i] = self.cmdl.fs_slack[i].value
|
|
148
|
+
|
|
149
|
+
if len(self.slack_nodes) > 0:
|
|
150
|
+
# self.Hmdl.p['f'] = self.f
|
|
151
|
+
# sol = nr_method(self.Hmdl, self.y0)
|
|
152
|
+
# self.H = sol.y['H']
|
|
153
|
+
self.cal_H()
|
|
154
|
+
else:
|
|
155
|
+
warnings.warn("No slack nodes! Cannot calculate H distribution!")
|
|
156
|
+
|
|
157
|
+
def derive_H_mdl(self):
|
|
158
|
+
"""
|
|
159
|
+
The linear algebraic equations about node pressure, with pipe flow given as parameters.
|
|
160
|
+
"""
|
|
161
|
+
m = Model()
|
|
162
|
+
m.f = SolParam('f', self.f)
|
|
163
|
+
m.H = SolVar('H', np.zeros(self.n_node))
|
|
164
|
+
m.c = SolParam('c', self.c)
|
|
165
|
+
m.Hset = SolParam('Hset', self.Hset)
|
|
166
|
+
|
|
167
|
+
for edge in range(self.n_pipe):
|
|
168
|
+
fnode = self.pipe_from[edge]
|
|
169
|
+
tnode = self.pipe_to[edge]
|
|
170
|
+
idx = edge
|
|
171
|
+
include_pipe = True
|
|
172
|
+
|
|
173
|
+
if len(self.pinloop) > 0:
|
|
174
|
+
# if there exists loop, omit the last pipe in the loop
|
|
175
|
+
if np.isclose(idx, np.where(self.pinloop != 0)[0][-1]):
|
|
176
|
+
include_pipe = False
|
|
177
|
+
|
|
178
|
+
if include_pipe:
|
|
179
|
+
Hi = m.H[fnode]
|
|
180
|
+
Hj = m.H[tnode]
|
|
181
|
+
fij = m.f[idx]
|
|
182
|
+
rhs = m.c[idx] * fij ** 2 * Sign(fij) - (Hi - Hj)
|
|
183
|
+
m.__dict__[f'H_f_{fnode}_{tnode}'] = Eqn(f"H_f_{fnode}_{tnode}", rhs)
|
|
184
|
+
|
|
185
|
+
# node pressure equations
|
|
186
|
+
# for multi-slack cases, preserve only the first one, so that the equations would not be overdetermined
|
|
187
|
+
for node in self.slack_nodes[0:1]:
|
|
188
|
+
idx_slack = self.slack_nodes.tolist().index(node)
|
|
189
|
+
m.__dict__[f'H_{node}'] = Eqn(f'H_{node}',
|
|
190
|
+
m.H[node] - m.Hset[idx_slack])
|
|
191
|
+
|
|
192
|
+
sae, y0 = m.create_instance()
|
|
193
|
+
ae = made_numerical(sae, y0, sparse=True)
|
|
194
|
+
|
|
195
|
+
return ae, y0
|
|
196
|
+
|
|
197
|
+
def cal_H(self):
|
|
198
|
+
"""
|
|
199
|
+
calculate H using depth-first search, for graph without self-loop and multiple edges
|
|
200
|
+
"""
|
|
201
|
+
H = dict()
|
|
202
|
+
slack_nodes = self.slack_nodes.tolist()
|
|
203
|
+
H[slack_nodes[0]] = self.Hset[slack_nodes.index(slack_nodes[0])]
|
|
204
|
+
|
|
205
|
+
def dfs(i):
|
|
206
|
+
for neighbor in list(self.G.successors(i)) + list(self.G.predecessors(i)):
|
|
207
|
+
if neighbor not in H:
|
|
208
|
+
if neighbor in self.G.successors(i):
|
|
209
|
+
f = i
|
|
210
|
+
t = neighbor
|
|
211
|
+
fij = self.cmdl.f[f, t].value
|
|
212
|
+
H[t] = H[f] - self.cmdl.c[f, t].value * fij ** 2 * np.sign(fij)
|
|
213
|
+
elif neighbor in self.G.predecessors(i):
|
|
214
|
+
f = neighbor
|
|
215
|
+
t = i
|
|
216
|
+
fij = self.cmdl.f[f, t].value
|
|
217
|
+
H[f] = H[t] + self.cmdl.c[f, t].value * fij ** 2 * np.sign(fij)
|
|
218
|
+
else:
|
|
219
|
+
raise ValueError(f'{neighbor} neither successor nor predecessor!')
|
|
220
|
+
dfs(neighbor)
|
|
221
|
+
|
|
222
|
+
dfs(self.slack_nodes[0])
|
|
223
|
+
|
|
224
|
+
sorted_H = sorted(H.items(), key=lambda item: item[0])
|
|
225
|
+
self.H = [Array(item[1], dim=1) for item in sorted_H]
|
|
226
|
+
|
|
227
|
+
self.H = np.asarray(self.H).reshape(-1, )
|
|
228
|
+
|
|
229
|
+
for i in range(self.n_node):
|
|
230
|
+
if self.H[i] + 1e-10 >= 0 > self.H[i]:
|
|
231
|
+
self.H[i] = 0
|
|
232
|
+
warnings.warn(f"Head of node {i} close to zero.")
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import tempfile
|
|
3
|
+
import sys
|
|
4
|
+
import uuid
|
|
5
|
+
import warnings
|
|
6
|
+
|
|
7
|
+
from Solverz import (Var as SolVar, Param as SolParam, Eqn, Model,
|
|
8
|
+
made_numerical, nr_method, module_printer, sin, cos,
|
|
9
|
+
Set, LoopEqn, Sum)
|
|
10
|
+
from Solverz.solvers.solution import aesol
|
|
11
|
+
from SolUtil.sysparser import load_mpc
|
|
12
|
+
from SolUtil.sysparser.eps_function import bus_injections
|
|
13
|
+
from scipy.sparse import csc_array
|
|
14
|
+
import numpy as np
|
|
15
|
+
import pandas as pd
|
|
16
|
+
|
|
17
|
+
__all__ = ["PowerFlow"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class PowerFlow:
|
|
21
|
+
"""
|
|
22
|
+
The electric power flow with Newton-Raphson method.
|
|
23
|
+
|
|
24
|
+
Parameters
|
|
25
|
+
----------
|
|
26
|
+
file : str or path
|
|
27
|
+
The case: a MATPOWER ``.m`` case file, which is read with
|
|
28
|
+
matpowercaseframes and prepared as MATPOWER ``runpf`` prepares it,
|
|
29
|
+
or a SolUtil ``.xlsx`` case file. ``load_mpc`` parses both, and the
|
|
30
|
+
``.m`` file is much faster to read for large cases.
|
|
31
|
+
mdl : dict or None
|
|
32
|
+
Optional pre-built ``{'mdl': numerical_model, 'y0': Vars}`` to
|
|
33
|
+
skip the symbolic build entirely.
|
|
34
|
+
loopeqn : bool, default True
|
|
35
|
+
Formulate the polar power-flow equations as a small number of
|
|
36
|
+
``LoopEqn`` blocks over a flat full-bus ``Vm`` / ``Va`` state
|
|
37
|
+
(``P_eqn`` over pv+pq, ``Q_eqn`` over pq, plus ``Vm_pin`` /
|
|
38
|
+
``Va_pin`` pinning the ref+pv magnitudes and ref angles),
|
|
39
|
+
instead of expanding ``O(nb)`` scalar ``Eqn``s. The two
|
|
40
|
+
formulations are algebraically identical, but the LoopEqn form
|
|
41
|
+
builds and compiles to a handful of vector kernels rather than
|
|
42
|
+
one scalar ``Eqn`` per bus, so ``create_instance`` and the
|
|
43
|
+
Jacobian assembly scale with the number of equation *blocks*
|
|
44
|
+
rather than the number of buses. This is the default since 0.9.0.
|
|
45
|
+
|
|
46
|
+
``LoopEqn`` is only supported by the Numba-JIT module path
|
|
47
|
+
(``module_printer``), not by the inline ``made_numerical`` path,
|
|
48
|
+
so the model is rendered to a temporary directory and imported.
|
|
49
|
+
This pays a one-off Numba compile cost.
|
|
50
|
+
|
|
51
|
+
``loopeqn=False`` selects the legacy per-bus scalar inline build.
|
|
52
|
+
**It is deprecated and will be removed in a future release**; it
|
|
53
|
+
emits a ``DeprecationWarning``. The scalar build is much slower
|
|
54
|
+
to construct and lambdify on non-trivial networks and exists only
|
|
55
|
+
for backward compatibility.
|
|
56
|
+
"""
|
|
57
|
+
def __init__(self,
|
|
58
|
+
file: str,
|
|
59
|
+
mdl=None,
|
|
60
|
+
loopeqn: bool = True):
|
|
61
|
+
|
|
62
|
+
if not loopeqn and mdl is None:
|
|
63
|
+
warnings.warn(
|
|
64
|
+
"PowerFlow(loopeqn=False) selects the legacy per-bus scalar "
|
|
65
|
+
"inline power-flow build, which is deprecated and will be "
|
|
66
|
+
"removed in a future release. It is significantly slower to "
|
|
67
|
+
"construct and lambdify than the default LoopEqn build. Drop "
|
|
68
|
+
"the loopeqn=False argument to use the LoopEqn formulation.",
|
|
69
|
+
DeprecationWarning,
|
|
70
|
+
stacklevel=2,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
self.loopeqn = loopeqn
|
|
74
|
+
self.Vm = None
|
|
75
|
+
self.Va = None
|
|
76
|
+
self.Pg = None
|
|
77
|
+
self.Pd = None
|
|
78
|
+
self.Qg = None
|
|
79
|
+
self.Qd = None
|
|
80
|
+
self.nb = None
|
|
81
|
+
self.Ybus = None
|
|
82
|
+
self.idx_slack = None
|
|
83
|
+
self.idx_pv = None
|
|
84
|
+
self.idx_pq = None
|
|
85
|
+
self.sol = None
|
|
86
|
+
self.baseMVA = None
|
|
87
|
+
self.U = None
|
|
88
|
+
self.S = None
|
|
89
|
+
self.ux = None
|
|
90
|
+
self.uy = None
|
|
91
|
+
self.ix = None
|
|
92
|
+
self.iy = None
|
|
93
|
+
|
|
94
|
+
self.mpc = load_mpc(file)
|
|
95
|
+
self.__dict__.update(self.mpc)
|
|
96
|
+
self.Gbus = self.Ybus.real
|
|
97
|
+
self.Bbus = self.Ybus.imag
|
|
98
|
+
|
|
99
|
+
self.run_succeed = False
|
|
100
|
+
|
|
101
|
+
if mdl is None:
|
|
102
|
+
if self.loopeqn:
|
|
103
|
+
self.pfmdl, self.y0 = loopeqn_pf_mdl(self)
|
|
104
|
+
else:
|
|
105
|
+
self.pfmdl, self.y0 = inline_pf_mdl(self)
|
|
106
|
+
else:
|
|
107
|
+
self.pfmdl = mdl['mdl']
|
|
108
|
+
self.y0 = mdl['y0']
|
|
109
|
+
|
|
110
|
+
def mdlpf(self):
|
|
111
|
+
if self.loopeqn:
|
|
112
|
+
return self.mdlpf_loopeqn()
|
|
113
|
+
return self.mdlpf_scalar()
|
|
114
|
+
|
|
115
|
+
def mdlpf_scalar(self):
|
|
116
|
+
Vm = self.Vm
|
|
117
|
+
Va = self.Va
|
|
118
|
+
nb = self.nb
|
|
119
|
+
Ybus = self.Ybus
|
|
120
|
+
G = Ybus.real
|
|
121
|
+
B = Ybus.imag
|
|
122
|
+
ref = self.idx_slack.tolist()
|
|
123
|
+
pv = self.idx_pv.tolist()
|
|
124
|
+
pq = self.idx_pq.tolist()
|
|
125
|
+
Pg = self.Pg
|
|
126
|
+
Qg = self.Qg
|
|
127
|
+
Pd = self.Pd
|
|
128
|
+
Qd = self.Qd
|
|
129
|
+
|
|
130
|
+
m = Model()
|
|
131
|
+
m.Va = SolVar('Va', Va[pv + pq])
|
|
132
|
+
m.Vm = SolVar('Vm', Vm[pq])
|
|
133
|
+
m.Pg = SolParam('Pg', Pg)
|
|
134
|
+
m.Qg = SolParam('Qg', Qg)
|
|
135
|
+
m.Pd = SolParam('Pd', Pd)
|
|
136
|
+
m.Qd = SolParam('Qd', Qd)
|
|
137
|
+
|
|
138
|
+
def get_Vm(idx):
|
|
139
|
+
if idx in ref + pv:
|
|
140
|
+
return Vm[idx]
|
|
141
|
+
elif idx in pq:
|
|
142
|
+
return m.Vm[pq.index(idx)]
|
|
143
|
+
|
|
144
|
+
def get_Va(idx):
|
|
145
|
+
if idx in ref:
|
|
146
|
+
return Va[idx]
|
|
147
|
+
elif idx in pv + pq:
|
|
148
|
+
return m.Va[(pv + pq).index(idx)]
|
|
149
|
+
|
|
150
|
+
for i in pv + pq:
|
|
151
|
+
expr = 0
|
|
152
|
+
Vmi = get_Vm(i)
|
|
153
|
+
Vai = get_Va(i)
|
|
154
|
+
for j in range(nb):
|
|
155
|
+
Vmj = get_Vm(j)
|
|
156
|
+
Vaj = get_Va(j)
|
|
157
|
+
expr += Vmi * Vmj * (G[i, j] * cos(Vai - Vaj) + B[i, j] * sin(Vai - Vaj))
|
|
158
|
+
m.__dict__[f'P_eqn_{i}'] = Eqn(f'P_eqn_{i}', expr + m.Pd[i] - m.Pg[i])
|
|
159
|
+
|
|
160
|
+
for i in pq:
|
|
161
|
+
expr = 0
|
|
162
|
+
Vmi = get_Vm(i)
|
|
163
|
+
Vai = get_Va(i)
|
|
164
|
+
for j in range(nb):
|
|
165
|
+
Vmj = get_Vm(j)
|
|
166
|
+
Vaj = get_Va(j)
|
|
167
|
+
expr += Vmi * Vmj * (G[i, j] * sin(Vai - Vaj) - B[i, j] * cos(Vai - Vaj))
|
|
168
|
+
m.__dict__[f'Q_eqn_{i}'] = Eqn(f'Q_eqn_{i}', expr + m.Qd[i] - m.Qg[i])
|
|
169
|
+
|
|
170
|
+
spf, y0 = m.create_instance()
|
|
171
|
+
|
|
172
|
+
return spf, y0
|
|
173
|
+
|
|
174
|
+
def mdlpf_loopeqn(self):
|
|
175
|
+
"""Polar power flow as LoopEqn blocks over a flat full-bus state.
|
|
176
|
+
|
|
177
|
+
``Vm`` / ``Va`` carry every bus. ``P_eqn`` iterates the pv+pq
|
|
178
|
+
buses and ``Q_eqn`` the pq buses; ``Vm_pin`` / ``Va_pin`` pin
|
|
179
|
+
the ref+pv magnitudes and ref angles to their setpoints. The
|
|
180
|
+
inner ``Sum`` over all buses reads the sparse ``Gbus`` / ``Bbus``
|
|
181
|
+
rows, so each LoopEqn compiles to a single vector kernel rather
|
|
182
|
+
than one scalar Eqn per bus.
|
|
183
|
+
"""
|
|
184
|
+
Vm = self.Vm
|
|
185
|
+
Va = self.Va
|
|
186
|
+
nb = self.nb
|
|
187
|
+
Ybus = self.Ybus
|
|
188
|
+
|
|
189
|
+
ref = self.idx_slack.tolist()
|
|
190
|
+
pv = self.idx_pv.tolist()
|
|
191
|
+
pq = self.idx_pq.tolist()
|
|
192
|
+
|
|
193
|
+
pv_pq_arr = np.array(pv + pq, dtype=int)
|
|
194
|
+
pq_arr = np.array(pq, dtype=int)
|
|
195
|
+
ref_pv_arr = np.array(ref + pv, dtype=int)
|
|
196
|
+
ref_arr = np.array(ref, dtype=int)
|
|
197
|
+
|
|
198
|
+
m = Model()
|
|
199
|
+
m.Vm = SolVar('Vm', Vm.copy())
|
|
200
|
+
m.Va = SolVar('Va', Va.copy())
|
|
201
|
+
|
|
202
|
+
m.Gbus = SolParam('Gbus', csc_array(Ybus.real), dim=2, sparse=True)
|
|
203
|
+
m.Bbus = SolParam('Bbus', csc_array(Ybus.imag), dim=2, sparse=True)
|
|
204
|
+
m.Pg = SolParam('Pg', self.Pg)
|
|
205
|
+
m.Qg = SolParam('Qg', self.Qg)
|
|
206
|
+
m.Pd = SolParam('Pd', self.Pd)
|
|
207
|
+
m.Qd = SolParam('Qd', self.Qd)
|
|
208
|
+
|
|
209
|
+
m.Bus = Set('Bus', nb)
|
|
210
|
+
m.PVPQ = Set('PVPQ', pv_pq_arr)
|
|
211
|
+
m.PQ = Set('PQ', pq_arr)
|
|
212
|
+
m.RefPV = Set('RefPV', ref_pv_arr)
|
|
213
|
+
m.Ref = Set('Ref', ref_arr)
|
|
214
|
+
|
|
215
|
+
m.Vm_pinned = SolParam('Vm_pinned', Vm[ref_pv_arr])
|
|
216
|
+
m.Va_pinned = SolParam('Va_pinned', Va[ref_arr])
|
|
217
|
+
|
|
218
|
+
i_p = m.PVPQ.idx('i_p')
|
|
219
|
+
i_q = m.PQ.idx('i_q')
|
|
220
|
+
j = m.Bus.idx('j')
|
|
221
|
+
|
|
222
|
+
body_P = (
|
|
223
|
+
m.Vm[i_p] * Sum(m.Vm[j] * m.Gbus[i_p, j]
|
|
224
|
+
* cos(m.Va[i_p] - m.Va[j]), j)
|
|
225
|
+
+ m.Vm[i_p] * Sum(m.Vm[j] * m.Bbus[i_p, j]
|
|
226
|
+
* sin(m.Va[i_p] - m.Va[j]), j)
|
|
227
|
+
+ m.Pd[i_p] - m.Pg[i_p]
|
|
228
|
+
)
|
|
229
|
+
m.P_eqn = LoopEqn('P_eqn', outer_index=i_p, body=body_P, model=m)
|
|
230
|
+
|
|
231
|
+
body_Q = (
|
|
232
|
+
m.Vm[i_q] * Sum(m.Vm[j] * m.Gbus[i_q, j]
|
|
233
|
+
* sin(m.Va[i_q] - m.Va[j]), j)
|
|
234
|
+
- m.Vm[i_q] * Sum(m.Vm[j] * m.Bbus[i_q, j]
|
|
235
|
+
* cos(m.Va[i_q] - m.Va[j]), j)
|
|
236
|
+
+ m.Qd[i_q] - m.Qg[i_q]
|
|
237
|
+
)
|
|
238
|
+
m.Q_eqn = LoopEqn('Q_eqn', outer_index=i_q, body=body_Q, model=m)
|
|
239
|
+
|
|
240
|
+
i_vp = m.RefPV.idx('i_vp')
|
|
241
|
+
i_vr = m.Ref.idx('i_vr')
|
|
242
|
+
m.Vm_pin = LoopEqn('Vm_pin', outer_index=i_vp,
|
|
243
|
+
body=m.Vm[i_vp] - m.Vm_pinned[i_vp], model=m)
|
|
244
|
+
m.Va_pin = LoopEqn('Va_pin', outer_index=i_vr,
|
|
245
|
+
body=m.Va[i_vr] - m.Va_pinned[i_vr], model=m)
|
|
246
|
+
|
|
247
|
+
spf, y0 = m.create_instance()
|
|
248
|
+
|
|
249
|
+
return spf, y0
|
|
250
|
+
|
|
251
|
+
def run(self):
|
|
252
|
+
self.pfmdl.p['Pg'] = self.Pg
|
|
253
|
+
self.pfmdl.p['Qg'] = self.Qg
|
|
254
|
+
self.pfmdl.p['Pd'] = self.Pd
|
|
255
|
+
self.pfmdl.p['Qd'] = self.Qd
|
|
256
|
+
self.sol = nr_method(self.pfmdl, self.y0)
|
|
257
|
+
if self.sol.stats.succeed:
|
|
258
|
+
self.run_succeed = True
|
|
259
|
+
self.parse_data_post_pf(self.sol)
|
|
260
|
+
|
|
261
|
+
def parse_data_post_pf(self, sol: aesol):
|
|
262
|
+
Vm = self.Vm
|
|
263
|
+
Va = self.Va
|
|
264
|
+
Ybus = self.Ybus
|
|
265
|
+
ref = self.idx_slack.tolist()
|
|
266
|
+
pv = self.idx_pv.tolist()
|
|
267
|
+
pq = self.idx_pq.tolist()
|
|
268
|
+
Pg = self.Pg
|
|
269
|
+
Qg = self.Qg
|
|
270
|
+
Pd = self.Pd
|
|
271
|
+
Qd = self.Qd
|
|
272
|
+
if self.loopeqn:
|
|
273
|
+
# full-bus state; ref / pv pinned by the pin LoopEqns.
|
|
274
|
+
Vm = np.asarray(sol.y['Vm']).copy()
|
|
275
|
+
Va = np.asarray(sol.y['Va']).copy()
|
|
276
|
+
else:
|
|
277
|
+
Vm[pq] = sol.y['Vm']
|
|
278
|
+
Va[pv + pq] = sol.y['Va']
|
|
279
|
+
|
|
280
|
+
# update slack Pg and slack / PV Qg from the net bus injections
|
|
281
|
+
# S = V conj(Ybus V); one sparse matrix-vector product instead of
|
|
282
|
+
# an O(n_gen * n_bus) double loop of sparse scalar lookups.
|
|
283
|
+
P, Q = bus_injections(Ybus, Vm, Va)
|
|
284
|
+
Pg[ref] = P[ref] + Pd[ref]
|
|
285
|
+
Qg[ref + pv] = Q[ref + pv] + Qd[ref + pv]
|
|
286
|
+
|
|
287
|
+
self.Vm = Vm
|
|
288
|
+
self.Va = Va
|
|
289
|
+
self.Pg = Pg
|
|
290
|
+
self.Qg = Qg
|
|
291
|
+
|
|
292
|
+
self.U: np.ndarray = self.Vm * np.exp(1j * self.Va)
|
|
293
|
+
self.S: np.ndarray = (self.Pg - self.Pd) + 1j * (self.Qg - self.Qd)
|
|
294
|
+
I = (self.S / self.U).conjugate()
|
|
295
|
+
self.ux = self.U.real
|
|
296
|
+
self.uy = self.U.imag
|
|
297
|
+
self.ix = I.real
|
|
298
|
+
self.iy = I.imag
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def generate_pf_module(pf: PowerFlow, module_name, jit=True, directory=None):
|
|
302
|
+
spf, y0 = pf.mdlpf()
|
|
303
|
+
pyprinter = module_printer(spf,
|
|
304
|
+
y0,
|
|
305
|
+
module_name,
|
|
306
|
+
directory=directory,
|
|
307
|
+
jit=jit)
|
|
308
|
+
pyprinter.render()
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def inline_pf_mdl(pf: PowerFlow):
|
|
312
|
+
spf, y0 = pf.mdlpf_scalar()
|
|
313
|
+
npf = made_numerical(spf, y0, sparse=True)
|
|
314
|
+
return npf, y0
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def loopeqn_pf_mdl(pf: PowerFlow):
|
|
318
|
+
"""Build and import a LoopEqn power-flow module.
|
|
319
|
+
|
|
320
|
+
``LoopEqn`` is only supported by the module printer path, so the
|
|
321
|
+
model is rendered to a temporary directory and imported back. The
|
|
322
|
+
returned ``mdl`` exposes the usual ``F`` / ``J`` / ``p`` interface
|
|
323
|
+
so ``PowerFlow.run`` works unchanged.
|
|
324
|
+
"""
|
|
325
|
+
spf, y0 = pf.mdlpf_loopeqn()
|
|
326
|
+
tmpdir = tempfile.mkdtemp(prefix='solutil_pf_loopeqn_')
|
|
327
|
+
# One module name per PowerFlow instance. A fixed name made every later
|
|
328
|
+
# PowerFlow() in the same process reuse the first rendered module: the
|
|
329
|
+
# package was found in sys.modules and reloaded, but reload() only
|
|
330
|
+
# re-executes __init__.py, and the cached num_func / dependency
|
|
331
|
+
# submodules kept the first case's F, J, p and y.
|
|
332
|
+
module_name = f'pf_loopeqn_mdl_{uuid.uuid4().hex[:8]}'
|
|
333
|
+
module_printer(spf, y0, module_name, directory=tmpdir, jit=True).render()
|
|
334
|
+
if tmpdir not in sys.path:
|
|
335
|
+
sys.path.insert(0, tmpdir)
|
|
336
|
+
import importlib
|
|
337
|
+
mod = importlib.import_module(module_name)
|
|
338
|
+
return mod.mdl, mod.y
|
|
File without changes
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from SolUtil import DhsFlow, DhsFaultFlow
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
FILES = [
|
|
5
|
+
'case_heat_test.xlsx',
|
|
6
|
+
'case_heat_test0.xlsx',
|
|
7
|
+
'case_heat_test1.xlsx'
|
|
8
|
+
]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# 使用 pytest 的 fixture 来加载 Excel 文件
|
|
12
|
+
@pytest.fixture(params=FILES)
|
|
13
|
+
def test_file_path(request, datadir):
|
|
14
|
+
file_path = datadir / request.param
|
|
15
|
+
return file_path
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_dhs_fault_flow0(test_file_path):
|
|
19
|
+
assert test_file_path.exists(), f"File {test_file_path} does not exist"
|
|
20
|
+
|
|
21
|
+
df = DhsFlow(test_file_path)
|
|
22
|
+
df.run()
|
|
23
|
+
|
|
24
|
+
dff = DhsFaultFlow(df, 0, 0.5, fault_sys='r', dH=1)
|
|
25
|
+
dff.run()
|
|
26
|
+
|
|
27
|
+
assert dff.verify_results() < 1e-5
|
|
Binary file
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pandas as pd
|
|
3
|
+
from ..dhs_flow import DhsFlow
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_dhs_flow(datadir):
|
|
7
|
+
|
|
8
|
+
bench = pd.read_excel(datadir / 'BarryIsland.xlsx',
|
|
9
|
+
sheet_name=None,
|
|
10
|
+
engine='openpyxl',
|
|
11
|
+
index_col=None
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
df = DhsFlow(datadir / 'BarryIsland.xlsx')
|
|
15
|
+
df.phi *= np.asarray(bench['load_fac']['load_fac'])
|
|
16
|
+
df.run(tee=False)
|
|
17
|
+
|
|
18
|
+
for varname in ['m', 'Ts', 'Tr']:
|
|
19
|
+
np.testing.assert_allclose(df.__dict__[varname],
|
|
20
|
+
np.asarray(bench[f'bench_{varname}'][varname]),
|
|
21
|
+
rtol=1e-4,
|
|
22
|
+
atol=1e-5)
|
|
23
|
+
|
|
24
|
+
np.testing.assert_allclose(df.phi[df.slack_node], np.asarray(bench['bench_phi_slack']['phi_slack']))
|
|
Binary file
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pandas as pd
|
|
3
|
+
from ..gas_flow import GasFlow
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_gas_flow(datadir):
|
|
7
|
+
gf = GasFlow(datadir/'belgium.xlsx')
|
|
8
|
+
gf.run()
|
|
9
|
+
|
|
10
|
+
df = pd.read_excel(datadir/'belgium.xlsx',
|
|
11
|
+
sheet_name=None,
|
|
12
|
+
engine='openpyxl',
|
|
13
|
+
index_col=None
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
f_bench = np.asarray(df['bench_f']['f'])
|
|
17
|
+
pi_bench = np.asarray(df['bench_pi']['pi'])
|
|
18
|
+
|
|
19
|
+
np.testing.assert_allclose(gf.Pi, pi_bench)
|
|
20
|
+
np.testing.assert_allclose(gf.f, f_bench)
|