Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of basic incidence matrix computation #946

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PowerModels.jl Change Log
=========================

### Staged
- nothing
- Improve performance of `calc_basic_incidence_matrix` (#946)

### v0.21.3
- Fix no-buses bug in `calc_connected_components` (#933)
Expand Down
24 changes: 12 additions & 12 deletions src/core/data_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,18 @@ function calc_basic_incidence_matrix(data::Dict{String,<:Any})
Memento.warn(_LOGGER, "calc_basic_incidence_matrix requires basic network data and given data may be incompatible. make_basic_network can be used to transform data into the appropriate form.")
end

I = Int[]
J = Int[]
V = Int[]

b = [branch for (i,branch) in data["branch"] if branch["br_status"] != 0]
branch_ordered = sort(b, by=(x) -> x["index"])
for (i,branch) in enumerate(branch_ordered)
push!(I, i); push!(J, branch["f_bus"]); push!(V, 1)
push!(I, i); push!(J, branch["t_bus"]); push!(V, -1)
end

return sparse(I,J,V)
E, N = length(data["branch"])::Int, length(data["bus"])::Int
# I = [..., e, e, ...]
I = repeat(1:E; inner=2)
J = zeros(Int, 2 * E)
for e in 1:E
branch = data["branch"]["$e"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type annotation here didn't seem to yield any improvement, so I didn't include it to keep the code simple.

J[2*e-1] = branch["f_bus"]::Int
J[2*e] = branch["t_bus"]::Int
end
# V = [..., 1, -1, ...]
V = repeat([1, -1]; outer=E)
return sparse(I, J, V, E, N)
end

"""
Expand Down
Loading