Skip to content

Commit

Permalink
v0.3.6 add remove_bus!, reduce_tree! and SinglePhase rij, xij
Browse files Browse the repository at this point in the history
  • Loading branch information
NLaws committed Aug 1, 2023
1 parent 4c2f425 commit 7287caf
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CommonOPF Changelog

## v0.3.6
- add `remove_bus!` and `reduce_tree!` from BranchFlowModel (tested there)
- add `SinglePhase` `rij` and `xij` from BFM and LDF

## v0.3.5
- add `get_variable_values(var::Symbol, m::JuMP.AbstractModel, p::Inputs{SinglePhase}; digits=8)`
- moved from BranchFlowModel and used in LinDistFlow v0.5
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CommonOPF"
uuid = "dad7ea18-2b21-482e-81c1-e84414cc4b03"
authors = ["Nick Laws <[email protected]> and contributors"]
version = "0.3.5"
version = "0.3.6"

[deps]
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Expand Down
9 changes: 7 additions & 2 deletions docs/src/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ leaf_busses


# Inputs

```@docs
remove_bus!(j::String, p::Inputs{SinglePhase})
remove_bus!(j::String, p::Inputs{MultiPhase})
reduce_tree!(p::Inputs{SinglePhase})
```

# Types


# Utils

```@docs
trim_tree!
trim_tree_once!
rij(i::AbstractString, j::AbstractString, p::Inputs{SinglePhase})
xij(i::AbstractString, j::AbstractString, p::Inputs{SinglePhase})
```
6 changes: 5 additions & 1 deletion src/CommonOPF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export
delete_bus_j!,
info_max_rpu_xpu,
info_max_Ppu_Qpu,
rij,
xij,
i_to_j,
j_to_k,
get_ij_idx,
Expand All @@ -56,7 +58,9 @@ export
leaf_busses,
trim_tree!,
trim_tree_once!,
get_variable_values
get_variable_values,
remove_bus!,
reduce_tree!


include("graphs.jl")
Expand Down
31 changes: 31 additions & 0 deletions src/inputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,34 @@ function singlephase38linesInputs(;
Ntimesteps = T
)
end


"""
reduce_tree!(p::Inputs{SinglePhase})
combine any line sets with intermediate busses that have indegree == outdegree == 1
and is not a load bus into a single line
"""
function reduce_tree!(p::Inputs{SinglePhase}; keep_regulator_busses=true)
# TODO make graph once in Inputs ?
g = make_graph(p.busses, p.edges)
int_bus_map = get_prop(g, :int_bus_map)
reducable_buses = String[]
load_buses = Set(vcat(collect(keys(p.Pload)), collect(keys(p.Qload))))
if keep_regulator_busses
for bs in keys(p.regulators) # tuple keys of bus pairs, i.e. edges
push!(load_buses, bs...)
end
end
for v in vertices(g)
if indegree(g, v) == outdegree(g, v) == 1 && !(int_bus_map[v] in load_buses)
push!(reducable_buses, int_bus_map[v])
end
end
@debug("Removing the following busses: \n$reducable_buses")
# replace two lines with one
for j in reducable_buses
remove_bus!(j, p)
end
@info("Removed $(length(reducable_buses)) busses.")
end
104 changes: 104 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
# TODO mv MultiPhase rij xij to CommonOPF (requires something new to handle BFM vs. LDF)
"""
rij(i::AbstractString, j::AbstractString, p::Inputs{SinglePhase})
The per-unit resistance of line i->j
"""
function rij(i::AbstractString, j::AbstractString, p::Inputs{SinglePhase})
linecode = get_ijlinecode(i, j, p)
linelength = get_ijlinelength(i, j, p)
rmatrix = p.Zdict[linecode]["rmatrix"] * linelength / p.Zbase
return rmatrix[1] # 1 index b/c single phase
end


"""
xij(i::AbstractString, j::AbstractString, p::Inputs{SinglePhase})
The per-unit reacttance of line i->j
"""
function xij(i::AbstractString, j::AbstractString, p::Inputs{SinglePhase})
linecode = get_ijlinecode(i, j, p)
linelength = get_ijlinelength(i, j, p)
xmatrix = p.Zdict[linecode]["xmatrix"] * linelength / p.Zbase
return xmatrix[1] # 1 index b/c single phase
end


"""
function i_to_j(j::AbstractString, p::Inputs)
find all busses upstream of bus j
Expand Down Expand Up @@ -228,3 +255,80 @@ function trim_tree!(p::Inputs)
@info("Removed $(n_edges_before - n_edges_after) edges.")
true
end


"""
remove_bus!(j::String, p::Inputs{SinglePhase})
Remove bus `j` in the line i->j->k from the model by making an equivalent line from busses i->k
"""
function remove_bus!(j::String, p::Inputs{SinglePhase})
# get all the old values
i, k = i_to_j(j, p)[1], j_to_k(j, p)[1]
ij_idx, jk_idx = get_ij_idx(i, j, p), get_ij_idx(j, k, p)
ij_len, jk_len = p.linelengths[ij_idx], p.linelengths[jk_idx]
ij_linecode, jk_linecode = get_ijlinecode(i,j,p), get_ijlinecode(j,k,p)
r_ij, x_ij, r_jk, x_jk = rij(i,j,p)*p.Zbase, xij(i,j,p)*p.Zbase, rij(j,k,p)*p.Zbase, xij(j,k,p)*p.Zbase
# make the new values
r_ik = r_ij + r_jk
x_ik = x_ij + x_jk
ik_len = ij_len + jk_len
ik_linecode = ik_key = i * "-" * k
ik_amps = minimum([p.Isquared_up_bounds[ij_linecode], p.Isquared_up_bounds[jk_linecode]])
# delete the old values
delete_edge_ij!(i, j, p)
delete_edge_ij!(j, k, p)
delete_bus_j!(j, p)
# add the new values
push!(p.edges, (i, k))
push!(p.linecodes, ik_linecode)
push!(p.phases, [1])
push!(p.linelengths, ik_len)
push!(p.edge_keys, ik_key)
p.Zdict[ik_linecode] = Dict(
"nphases" => 1,
"name" => ik_linecode,
"rmatrix" => [r_ik / ik_len],
"xmatrix" => [x_ik / ik_len],
)
p.Isquared_up_bounds[ik_linecode] = ik_amps
end


"""
remove_bus!(j::String, p::Inputs{MultiPhase})
Remove bus `j` in the line i->j->k from the model by making an equivalent line from busses i->k
"""
function remove_bus!(j::String, p::Inputs{MultiPhase})
# get all the old values
i, k = i_to_j(j, p)[1], j_to_k(j, p)[1]
ij_idx, jk_idx = get_ij_idx(i, j, p), get_ij_idx(j, k, p)
ij_len, jk_len = p.linelengths[ij_idx], p.linelengths[jk_idx]
ij_linecode, jk_linecode = get_ijlinecode(i,j,p), get_ijlinecode(j,k,p)
r_ij, x_ij, r_jk, x_jk = rij(i,j,p)*p.Zbase, xij(i,j,p)*p.Zbase, rij(j,k,p)*p.Zbase, xij(j,k,p)*p.Zbase
phases = p.phases[ij_idx]
# make the new values
r_ik = r_ij .+ r_jk
x_ik = x_ij .+ x_jk
ik_len = ij_len + jk_len
ik_linecode = ik_key = i * "-" * k
ik_amps = minimum([p.Isquared_up_bounds[ij_linecode], p.Isquared_up_bounds[jk_linecode]])
# delete the old values
delete_edge_ij!(i, j, p)
delete_edge_ij!(j, k, p)
delete_bus_j!(j, p)
# add the new values
push!(p.edges, (i, k))
push!(p.linecodes, ik_linecode)
push!(p.phases, phases)
push!(p.linelengths, ik_len)
push!(p.edge_keys, ik_key)
p.Zdict[ik_linecode] = Dict(
"nphases" => length(phases),
"name" => ik_linecode,
"rmatrix" => r_ik ./ ik_len,
"xmatrix" => x_ik ./ ik_len,
)
p.Isquared_up_bounds[ik_linecode] = ik_amps
end

2 comments on commit 7287caf

@NLaws
Copy link
Owner Author

@NLaws NLaws commented on 7287caf Aug 1, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/88754

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.6 -m "<description of version>" 7287caf9ba0da36f5026ef13c8ecda8b07fa7ac3
git push origin v0.3.6

Please sign in to comment.