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

Corner cases catches #70

Merged
merged 10 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
22 changes: 14 additions & 8 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,25 +307,22 @@
"""
!!! MISSING DOCUMENTATION !!!
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
"""
function assign_reference_buses(
function assign_reference_buses!(
subnetworks::Dict{Int, Set{Int}},
ref_bus_positions::Set{Int},
ref_buses::Vector{Int},

Choose a reason for hiding this comment

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

Why is this now a Vector rather than a Set?

Copy link
Member Author

Choose a reason for hiding this comment

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

because before it was incorrectly using the ref buses positions and not the ref buses values.

)
if isempty(ref_bus_positions) || length(ref_bus_positions) != length(subnetworks)
if isempty(ref_buses) || length(ref_buses) != length(subnetworks)
@warn "The reference bus positions are not consistent with the subnetworks. References buses will be assigned arbitrarily"
return deepcopy(subnetworks)
end
bus_groups = Dict{Int, Set{Int}}()
for (bus_key, subnetwork_buses) in subnetworks
ref_bus = intersect(ref_bus_positions, subnetwork_buses)
ref_bus = intersect(ref_buses, subnetwork_buses)
if length(ref_bus) == 1
bus_groups[first(ref_bus)] = pop!(subnetworks, bus_key)
continue
elseif length(ref_bus) == 0
@warn "No reference bus in the subnetwork associated with bus $bus_key. References buses will be assigned arbitrarily"
return subnetworks
@warn "No reference bus in the subnetwork associated with bus $bus_key. Reference bus assigned arbitrarily"

Check warning on line 324 in src/common.jl

View check run for this annotation

Codecov / codecov/patch

src/common.jl#L324

Added line #L324 was not covered by tests
elseif length(ref_bus) > 1
# TODO: still to implement
error(
"More than one reference bus in the subnetwork associated with bus $bus_key",
)
Expand All @@ -336,6 +333,15 @@
return bus_groups
end

function assign_reference_buses!(

Choose a reason for hiding this comment

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

Ideally we'd get a test case that demonstrates what breaks without this new layer of indirection — if too complicated to do now but potentially feasible in the future, add a # TODO or GitHub Issue

subnetworks::Dict{Int, Set{Int}},
ref_bus_positions::Set{Int},
bus_lookup::Dict{Int, Int},
)
ref_buses = [k for (k, v) in bus_lookup if v in ref_bus_positions]
assign_reference_buses!(subnetworks, ref_buses)
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
end

"""
Finds the subnetworks present in the considered System. This is evaluated by taking
a the ABA or Adjacency Matrix.
Expand Down
18 changes: 15 additions & 3 deletions src/network_radial_reduction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@
reverse_bus_map,
)
new_parent_bus_number = reverse_bus_map[new_parent_val]
# This check is meant to capture cases of a full radial network which can happen in
# system with small islands that represent larger interconnected areas.
if length(SparseArrays.nzrange(A, new_parent_val)) < 2
@warn "Bus $parent_bus_number Parent $new_parent_bus_number is a leaf node. Indicating there is an island."
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
push!(bus_reduction_map_index[parent_bus_number], new_parent_bus_number)

Choose a reason for hiding this comment

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

Would it be possible to get a test case of the element of the public interface that depends on this that fails without this change and passes with it? Nothing fails when I comment this line out.

Choose a reason for hiding this comment

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

or a # TODO or issue if this is too complicated to do on the current timeframe

Choose a reason for hiding this comment

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

Codecov concurs and points out a few other untested regions

Copy link
Member Author

Choose a reason for hiding this comment

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

I will create an issue to implement the test. This is a corner case catch.

return

Check warning on line 107 in src/network_radial_reduction.jl

View check run for this annotation

Codecov / codecov/patch

src/network_radial_reduction.jl#L105-L107

Added lines #L105 - L107 were not covered by tests
end
new_set = push!(pop!(bus_reduction_map_index, parent_bus_number), parent_bus_number)
union!(bus_reduction_map_index[new_parent_bus_number], new_set)
_new_parent(
Expand All @@ -120,7 +127,11 @@
reverse_line_map::Dict{Int64, String},
radial_branches::Set{String},
reverse_bus_map::Dict{Int, Int},
ref_bus_positions::Set{Int},
)
if j ∈ ref_bus_positions
Copy link

@GabrielKS GabrielKS Jan 18, 2024

Choose a reason for hiding this comment

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

I don't really understand why we're doing this, but that may be due to my lack of familiarity with PNM

Copy link
Member Author

Choose a reason for hiding this comment

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

We never want to reduce the reference buses. It creates issues depending on the model once we remove tha bus that is meant to absorb all the power differences.

return
end
j_bus_number = reverse_bus_map[j]
pop!(bus_reduction_map_index, j_bus_number)
reducion_set = Set{Int}(j_bus_number)
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -134,6 +145,9 @@
)
parent_bus_number = reverse_bus_map[parent]
union!(bus_reduction_map_index[parent_bus_number], reducion_set)
if parent ∈ ref_bus_positions
return

Check warning on line 149 in src/network_radial_reduction.jl

View check run for this annotation

Codecov / codecov/patch

src/network_radial_reduction.jl#L149

Added line #L149 was not covered by tests
end
_new_parent(
A,
parent,
Expand Down Expand Up @@ -181,9 +195,6 @@
reverse_bus_map = Dict(reverse(kv) for kv in bus_map)
bus_reduction_map_index = Dict{Int, Set{Int}}(k => Set{Int}() for k in keys(bus_map))
Threads.@threads for j in 1:buscount
if j ∈ ref_bus_positions
continue
end
if length(SparseArrays.nzrange(A, j)) == 1
lock(lk) do
_reverse_search(
Expand All @@ -193,6 +204,7 @@
reverse_line_map,
radial_branches,
reverse_bus_map,
ref_bus_positions,
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/ptdf_calculations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ function PTDF(
subnetworks = find_subnetworks(M, bus_ax)
if length(subnetworks) > 1
@info "Network is not connected, using subnetworks"
subnetworks = assign_reference_buses(subnetworks, ref_bus_positions)
subnetworks = assign_reference_buses!(subnetworks, ref_bus_positions, bus_ax_ref)
end
look_up = (bus_ax_ref, make_ax_ref(line_ax))
S, _ = _buildptdf(
Expand Down
9 changes: 5 additions & 4 deletions src/system_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ function _find_subnetworks(sys::PSY.System)
branches = get_ac_branches(sys)
@info "Validating connectivity with depth first search (network traversal)"
M, bus_lookup = calculate_adjacency(branches, buses)
slack_positions = find_slack_positions(buses, bus_lookup)
return find_subnetworks(M, PSY.get_number.(buses)), slack_positions
ref_positions = find_slack_positions(buses, bus_lookup)
ref_buses = [k for (k, v) in bus_lookup if v in ref_positions]
return find_subnetworks(M, PSY.get_number.(buses)), ref_buses
end

"""
Finds the subnetworks in a system using Depth First Search (DFS). Returns a dictionary keyed
by the reference bus of the subnetworks if they exist
"""
function find_subnetworks(sys::PSY.System)
sbn, ref_bus_positions = _find_subnetworks(sys)
return assign_reference_buses(sbn, ref_bus_positions)
sbn, ref_buses = _find_subnetworks(sys)
return assign_reference_buses!(sbn, ref_buses)

Choose a reason for hiding this comment

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

All the other existing assign_reference_buses calls have been edited to add a bus_ax_ref — this one doesn't need one?

Copy link
Member Author

Choose a reason for hiding this comment

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

in some cases we already have the references buses so there is no need to recalculate them based on the ref_ax and location.

end
2 changes: 1 addition & 1 deletion src/virtual_lodf_calculations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function VirtualLODF(
# check subnetworks
if length(subnetworks) > 1
@info "Network is not connected, using subnetworks"
subnetworks = assign_reference_buses(subnetworks, ref_bus_positions)
subnetworks = assign_reference_buses!(subnetworks, ref_bus_positions, bus_ax_ref)
end
# get diagonal of PTDF
temp_data = zeros(length(bus_ax))
Expand Down
2 changes: 1 addition & 1 deletion src/virtual_ptdf_calculations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function VirtualPTDF(
subnetworks = find_subnetworks(M, bus_ax)
if length(subnetworks) > 1
@info "Network is not connected, using subnetworks"
subnetworks = assign_reference_buses(subnetworks, ref_bus_positions)
subnetworks = assign_reference_buses!(subnetworks, ref_bus_positions, bus_ax_ref)
end
temp_data = zeros(length(bus_ax))

Expand Down
Loading