Skip to content

Commit 16948af

Browse files
committed
Add branch power constraint in opf example
1 parent ae311fe commit 16948af

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

docs/source/examples/optimal_power_flow.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ Next, we will use PJM 5-bus system as an example to demonstrate the implementati
7575
```{code-cell}
7676
7777
branches = [
78-
# (from, to, R, X, B, angmin, angmax)
79-
(0, 1, 0.00281, 0.0281, 0.00712, -30.0, 30.0),
80-
(0, 3, 0.00304, 0.0304, 0.00658, -30.0, 30.0),
81-
(0, 4, 0.00064, 0.0064, 0.03126, -30.0, 30.0),
82-
(1, 2, 0.00108, 0.0108, 0.01852, -30.0, 30.0),
83-
(2, 3, 0.00297, 0.0297, 0.00674, -30.0, 30.0),
84-
(3, 4, 0.00297, 0.0297, 0.00674, -30.0, 30.0),
78+
# (from, to, R, X, B, angmin, angmax, Smax)
79+
(0, 1, 0.00281, 0.0281, 0.00712, -30.0, 30.0, 4.00),
80+
(0, 3, 0.00304, 0.0304, 0.00658, -30.0, 30.0, 4.26),
81+
(0, 4, 0.00064, 0.0064, 0.03126, -30.0, 30.0, 4.26),
82+
(1, 2, 0.00108, 0.0108, 0.01852, -30.0, 30.0, 4.26),
83+
(2, 3, 0.00297, 0.0297, 0.00674, -30.0, 30.0, 4.26),
84+
(3, 4, 0.00297, 0.0297, 0.00674, -30.0, 30.0, 2.40),
8585
]
8686
8787
buses = [
@@ -220,6 +220,14 @@ for k in range(N_branch):
220220
angmax = branch[6] / 180 * math.pi
221221
222222
model.add_linear_constraint(theta_i - theta_j, poi.In, angmin, angmax)
223+
224+
Smax = branch[7]
225+
Pij = Pbr_from[k]
226+
Qij = Qbr_from[k]
227+
Pji = Pbr_to[k]
228+
Qji = Qbr_to[k]
229+
model.add_quadratic_constraint(Pij * Pij + Qij * Qij, poi.Leq, Smax * Smax)
230+
model.add_quadratic_constraint(Pji * Pji + Qji * Qji, poi.Leq, Smax * Smax)
223231
```
224232

225233
Finally, we set the objective function:
@@ -248,5 +256,3 @@ print("Optimal active power output of the generators:")
248256
for i in range(N_gen):
249257
print(f"Generator {i}: {P_value[i]}")
250258
```
251-
252-
As shown by the result, the generator with less cost efficient has the highest active power output. The total generation power is also greater than the total demand due to the network loss.

tests/test_nlp_opf.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ def branch_flow(vars, params):
3434
bf = model.register_function(branch_flow)
3535

3636
branches = [
37-
# (from, to, R, X, B, angmin, angmax)
38-
(0, 1, 0.00281, 0.0281, 0.00712, -30.0, 30.0),
39-
(0, 3, 0.00304, 0.0304, 0.00658, -30.0, 30.0),
40-
(0, 4, 0.00064, 0.0064, 0.03126, -30.0, 30.0),
41-
(1, 2, 0.00108, 0.0108, 0.01852, -30.0, 30.0),
42-
(2, 3, 0.00297, 0.0297, 0.00674, -30.0, 30.0),
43-
(3, 4, 0.00297, 0.0297, 0.00674, -30.0, 30.0),
37+
# (from, to, R, X, B, angmin, angmax, Smax)
38+
(0, 1, 0.00281, 0.0281, 0.00712, -30.0, 30.0, 4.00),
39+
(0, 3, 0.00304, 0.0304, 0.00658, -30.0, 30.0, 4.26),
40+
(0, 4, 0.00064, 0.0064, 0.03126, -30.0, 30.0, 4.26),
41+
(1, 2, 0.00108, 0.0108, 0.01852, -30.0, 30.0, 4.26),
42+
(2, 3, 0.00297, 0.0297, 0.00674, -30.0, 30.0, 4.26),
43+
(3, 4, 0.00297, 0.0297, 0.00674, -30.0, 30.0, 2.40),
4444
]
4545

4646
buses = [
@@ -176,6 +176,14 @@ def branch_flow(vars, params):
176176

177177
model.add_linear_constraint(theta_i - theta_j, poi.In, angmin, angmax)
178178

179+
Smax = branch[7]
180+
Pij = Pbr_from[k]
181+
Qij = Qbr_from[k]
182+
Pji = Pbr_to[k]
183+
Qji = Qbr_to[k]
184+
model.add_quadratic_constraint(Pij * Pij + Qij * Qij, poi.Leq, Smax * Smax)
185+
model.add_quadratic_constraint(Pji * Pji + Qji * Qji, poi.Leq, Smax * Smax)
186+
179187
cost = poi.ExprBuilder()
180188
for i in range(N_gen):
181189
a, b, c = generators[i][5], generators[i][6], generators[i][7]
@@ -196,7 +204,8 @@ def branch_flow(vars, params):
196204

197205
assert P_value_sum > total_load_p
198206

199-
assert P_value[0] == pytest.approx(0.4, abs=1e-6)
200-
assert P_value[1] == pytest.approx(1.7, abs=1e-6)
201-
assert P_value[3] == pytest.approx(0.0, abs=1e-6)
202-
assert P_value[4] == pytest.approx(6.0, abs=1e-6)
207+
208+
if __name__ == "__main__":
209+
from pyoptinterface import ipopt
210+
211+
test_acopf(ipopt.Model)

0 commit comments

Comments
 (0)