Skip to content

Commit

Permalink
In-place _renumber_components and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtanneau3 committed Jun 17, 2024
1 parent 20a1730 commit 5205166
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/core/data_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function make_basic_network(data::Dict{String,<:Any})
# re-number non-bus component ids
for comp_key in keys(pm_component_status)
if comp_key != "bus"
data[comp_key] = _renumber_components(data[comp_key])
data[comp_key] = _renumber_components!(data[comp_key])
end
end

Expand Down Expand Up @@ -148,17 +148,18 @@ end
given a component dict returns a new dict where components have been renumbered
from 1-to-n ordered by the increasing values of the orginal component id.
"""
function _renumber_components(comp_dict::Dict{String,<:Any})
renumbered_dict = Dict{String,Any}()
function _renumber_components!(comp_dict::Dict{String,<:Any})
comp_ordered = sort([(comp["index"], comp) for (i, comp) in comp_dict], by=(x) -> x[1])

comp_ordered = sort([comp for (i,comp) in comp_dict], by=(x) -> x["index"])

for (i,comp) in enumerate(comp_ordered)
comp["index"] = i
renumbered_dict["$i"] = comp
# Delete existing keys
empty!(comp_dict)
# Update component indices and re-build the dict keys
for (i_new, (i_old, comp)) in enumerate(comp_ordered)
comp["index"] = i_new
comp_dict["$(i_new)"] = comp
end

return renumbered_dict
return comp_dict
end


Expand Down
22 changes: 22 additions & 0 deletions test/data-basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@
@test isapprox(result["objective"], 16551.7; atol=1e0)
end

@testset "Re-number components in-place" begin
data = make_basic_network(PowerModels.parse_file("../test/data/matpower/case7_tplgy.m"))

# Scramble indices before renumbering
v = [data["gen"]["$i"] for i in 1:3]
data["gen"]["1"]["index"] = 2
data["gen"]["2"]["index"] = 3
data["gen"]["3"]["index"] = 1

d = PowerModels._renumber_components!(data["gen"])
@test length(data["gen"]) == 3
# Check index consistency
@test data["gen"]["1"]["index"] == 1
@test data["gen"]["2"]["index"] == 2
@test data["gen"]["3"]["index"] == 3
# Check that modifications were done in-place
@test d === data["gen"]
@test data["gen"]["1"] === v[3]
@test data["gen"]["2"] === v[1]
@test data["gen"]["3"] === v[2]
end

end


Expand Down

0 comments on commit 5205166

Please sign in to comment.