Skip to content

Commit f433bd6

Browse files
committed
tidy basic jacobian feature, remove decoupled
1 parent baf0be8 commit f433bd6

File tree

5 files changed

+12
-109
lines changed

5 files changed

+12
-109
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ PowerModels.jl Change Log
22
=========================
33

44
### Staged
5-
- Improved support in `build_pf` for slack buses with multiple generators
5+
- Added Jacobian calculation of basic network data (#762)
6+
- Improved support in `build_pf` for slack buses with multiple generators (#785)
67
- Fixed bug in `compute_ac_pf` when reporting slack bus `va` values
78
- Fixed bug in `constraint_ohms_yt_from_on_off` for DCP models on branches with negative reactance values
89

docs/src/basic-data-utilities.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ calc_basic_incidence_matrix
4848
calc_basic_admittance_matrix
4949
calc_basic_susceptance_matrix
5050
calc_basic_branch_susceptance_matrix
51+
calc_basic_jacobian_matrix
5152
calc_basic_ptdf_matrix
5253
calc_basic_ptdf_row
5354
```
@@ -104,6 +105,7 @@ basic network data using Julia's native linear equation solver,
104105
compute_basic_dc_pf
105106
```
106107

108+
107109
!!! tip
108110
By default PowerModels uses Julia's SparseArrays to ensure the best
109111
performance of matrix operations on large power network datasets.

src/core/data_basic.jl

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -385,76 +385,6 @@ function calc_basic_ptdf_row(data::Dict{String,<:Any}, branch_index::Int)
385385
return ptdf_column
386386
end
387387

388-
"""
389-
Computes the jacobian submatrices
390-
H = dP/dδ
391-
L = dQ/dV
392-
393-
The derivatives are ordered according to the bus number,
394-
H[1, 1] is ∂P_1 / ∂δ_1
395-
H[1, 2] is ∂P_1 / ∂δ_2
396-
397-
It does not exclude PV buses rows and columns
398-
"""
399-
function calc_basic_decoupled_jacobian_matrices(data::Dict{String,<:Any})
400-
if !get(data, "basic_network", false)
401-
Memento.warn(_LOGGER, "calc_basic_decoupled_jacobian_matrices requires basic network data and given data may be incompatible. make_basic_network can be used to transform data into the appropriate form.")
402-
end
403-
num_bus = length(data["bus"])
404-
v = calc_basic_bus_voltage(data)
405-
vm, va = abs.(v), angle.(v)
406-
Y = calc_basic_admittance_matrix(data)
407-
neighbors = [Set{Int}([i]) for i in 1:num_bus]
408-
I, J, V = findnz(Y)
409-
for nz in eachindex(V)
410-
push!(neighbors[I[nz]], J[nz])
411-
push!(neighbors[J[nz]], I[nz])
412-
end
413-
H0_I = Int64[]; H0_J = Int64[]; H0_V = Float64[];
414-
L0_I = Int64[]; L0_J = Int64[]; L0_V = Float64[];
415-
for i in 1:num_bus
416-
for j in neighbors[i]
417-
push!(H0_I, i); push!(H0_J, j); push!(H0_V, 0.0)
418-
push!(L0_I, i); push!(L0_J, j); push!(L0_V, 0.0)
419-
end
420-
end
421-
H = sparse(H0_I, H0_J, H0_V)
422-
L = sparse(L0_I, L0_J, L0_V)
423-
for i in 1:num_bus
424-
for j in neighbors[i]
425-
bus_type = data["bus"]["$(j)"]["bus_type"]
426-
if bus_type == 1
427-
if i == j
428-
y_ii = Y[i,i]
429-
H[i, j] = + vm[i] * sum( -real(Y[i,k]) * vm[k] * sin(va[i] - va[k]) + imag(Y[i,k]) * vm[k] * cos(va[i] - va[k]) for k in neighbors[i] if k != i )
430-
L[i, j] = - 2*imag(y_ii)*vm[i] + sum( real(Y[i,k]) * vm[k] * sin(va[i] - va[k]) - imag(Y[i,k]) * vm[k] * cos(va[i] - va[k]) for k in neighbors[i] if k != i )
431-
else
432-
y_ij = Y[i,j]
433-
H[i, j] = vm[i] * vm[j] * ( real(y_ij) * sin(va[i] - va[j]) - imag(y_ij) * cos(va[i] - va[j]) )
434-
L[i, j] = vm[i] * ( real(y_ij) * sin(va[i] - va[j]) - imag(y_ij) * cos(va[i] - va[j]) )
435-
end
436-
elseif bus_type == 2
437-
if i == j
438-
y_ii = Y[i,i]
439-
H[i, j] = vm[i] * sum( -real(Y[i,k]) * vm[k] * sin(va[i] - va[k]) + imag(Y[i,k]) * vm[k] * cos(va[i] - va[k]) for k in neighbors[i] if k != i )
440-
L[i, j] = 1.0
441-
else
442-
y_ij = Y[i,j]
443-
H[i, j] = vm[i] * vm[j] * ( real(y_ij) * sin(va[i] - va[j]) - imag(y_ij) * cos(va[i] - va[j]) )
444-
L[i, j] = 0.0
445-
end
446-
elseif bus_type == 3
447-
if i == j
448-
H[i, j] = 1.0
449-
L[i, j] = 1.0
450-
end
451-
else
452-
@assert false
453-
end
454-
end
455-
end
456-
return H, L
457-
end
458388

459389
"""
460390
given a basic network data dict, returns a sparse real valued Jacobian matrix
@@ -465,6 +395,7 @@ function calc_basic_jacobian_matrix(data::Dict{String,<:Any})
465395
if !get(data, "basic_network", false)
466396
Memento.warn(_LOGGER, "calc_basic_jacobian_matrix requires basic network data and given data may be incompatible. make_basic_network can be used to transform data into the appropriate form.")
467397
end
398+
468399
num_bus = length(data["bus"])
469400
v = calc_basic_bus_voltage(data)
470401
vm, va = abs.(v), angle.(v)

test/common.jl

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ end
7272
"""
7373
An AC Power Flow Solver from scratch.
7474
"""
75-
function compute_basic_ac_pf!(data::Dict{String, Any}; decoupled=false)
75+
function compute_basic_ac_pf!(data::Dict{String, Any})
7676
if !get(data, "basic_network", false)
7777
Memento.warn(_LOGGER, "compute_basic_ac_pf requires basic network data and given data may be incompatible. make_basic_network can be used to transform data into the appropriate form.")
7878
end
@@ -104,16 +104,11 @@ function compute_basic_ac_pf!(data::Dict{String, Any}; decoupled=false)
104104
if LinearAlgebra.normInf([delta_P; delta_Q]) < tol
105105
break
106106
end
107+
107108
# STEP 2 and 3: Compute the jacobian and update step
108-
if !decoupled
109-
J = calc_basic_jacobian_matrix(data)
110-
x = J \ [delta_P; delta_Q]
111-
else
112-
H, L = calc_basic_decoupled_jacobian_matrices(data)
113-
va = H \ delta_P
114-
vm = L \ delta_Q
115-
x = [va; vm]
116-
end
109+
J = calc_basic_jacobian_matrix(data)
110+
x = J \ [delta_P; delta_Q]
111+
117112
# STEP 4
118113
# update voltage variables
119114
for i in 1:bus_num

test/data-basic.jl

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ end
195195
for (i, bus) in data["bus"] # All buses PQ
196196
bus["bus_type"] = 1
197197
end
198-
J = PowerModels.calc_basic_jacobian_matrix(data);
198+
J = calc_basic_jacobian_matrix(data)
199199

200200
num_bus = length(data["bus"])
201201

@@ -212,7 +212,7 @@ end
212212

213213
@testset "24-bus-case" begin
214214
data = make_basic_network(PowerModels.parse_file("../test/data/matpower/case24.m"))
215-
J = PowerModels.calc_basic_jacobian_matrix(data)
215+
J = calc_basic_jacobian_matrix(data)
216216

217217
num_bus = length(data["bus"])
218218

@@ -244,15 +244,6 @@ end
244244
@test isapprox(J[3, num_bus+9], -2.1624; atol=1e-4) # dP/dvm non-diagonal
245245
@test isapprox(J[num_bus+3, num_bus+9], -7.9603; atol=1e-4) # dQ/dvm non-diagonal
246246
end
247-
248-
@testset "30-bus-case-decoupled-matrices" begin
249-
data = make_basic_network(PowerModels.parse_file("../test/data/matpower/case24.m"))
250-
H, L = calc_basic_decoupled_jacobian_matrices(data)
251-
J = calc_basic_jacobian_matrix(data)
252-
n = length(data["bus"])
253-
@test isapprox(J[1:n, 1:n], H; atol=1e-6)
254-
@test isapprox(J[n+1:end, n+1:end], L; atol=1e-6)
255-
end
256247
end
257248

258249
@testset "basic ac power flow" begin
@@ -273,23 +264,6 @@ end
273264
end
274265
end
275266

276-
@testset "9-bus-case" begin
277-
data = make_basic_network(PowerModels.parse_file("../test/data/matpower/case9.m"))
278-
solution = compute_ac_pf(data)["solution"]
279-
280-
compute_basic_ac_pf!(data; decoupled=true)
281-
282-
for (i, bus) in data["bus"]
283-
@test isapprox(solution["bus"][i]["va"], bus["va"]; atol=1e-4)
284-
@test isapprox(solution["bus"][i]["vm"], bus["vm"]; atol=1e-4)
285-
end
286-
287-
for (i, gen) in data["gen"]
288-
@test isapprox(solution["gen"][i]["pg"], gen["pg"]; atol=1e-4)
289-
@test isapprox(solution["gen"][i]["qg"], gen["qg"]; atol=1e-4)
290-
end
291-
end
292-
293267
@testset "14-bus-case" begin
294268
data = make_basic_network(PowerModels.parse_file("../test/data/matpower/case14.m"))
295269
solution = compute_ac_pf(data)["solution"]

0 commit comments

Comments
 (0)