Skip to content

Commit

Permalink
reorg Network struct into types.jl, mv some load methods from network…
Browse files Browse the repository at this point in the history
….jl to loads.jl
  • Loading branch information
NLaws committed Nov 16, 2024
1 parent beaaadc commit 4e8f5af
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 63 deletions.
9 changes: 5 additions & 4 deletions src/CommonOPF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export
connect_subgraphs_at_busses


include("bounds.jl")
include("types.jl")
include("graphs.jl")
include("io.jl")
Expand All @@ -128,17 +129,17 @@ include("edges/conductors.jl")
include("edges/transformers.jl")
include("edges/voltage_regulators.jl")

include("bounds.jl")

include("network.jl")
include("edges/impedances.jl") # Network type in signatures, move the struct to types?
include("edges/admittances.jl") # Network type in signatures, move the struct to types?
include("busses/shunt_admittances.jl") # Network type in signatures
include("edges/impedances.jl")
include("edges/admittances.jl")
include("busses/shunt_admittances.jl")
include("network_reduction.jl")
include("decomposition.jl")

include("variables.jl")
include("results.jl")
include("opendss.jl")


end
27 changes: 27 additions & 0 deletions src/busses/loads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,30 @@ end
function load_from_csv(load::Load)
throw("NotImplementedError")
end


load_busses(net::AbstractNetwork) = collect(b for b in busses(net) if haskey(net[b], :Load))


real_load_busses(net::Network{SinglePhase}) = collect(b for b in load_busses(net) if !ismissing(net[b][:Load].kws1))


real_load_busses(net::Network{MultiPhase}) = collect(
b for b in load_busses(net)
if !ismissing(net[b][:Load].kws1) || !ismissing(net[b][:Load].kws2) || !ismissing(net[b][:Load].kws3)
)


reactive_load_busses(net::Network{SinglePhase}) = collect(b for b in load_busses(net) if !ismissing(net[b][:Load].kvars1))


reactive_load_busses(net::Network{MultiPhase}) = collect(
b for b in load_busses(net)
if !ismissing(net[b][:Load].kvars1) || !ismissing(net[b][:Load].kvars2) || !ismissing(net[b][:Load].kvars3)
)


total_load_kw(net::Network{SinglePhase}) = sum(net[load_bus][:Load].kws1 for load_bus in real_load_busses(net))


total_load_kvar(net::Network{SinglePhase}) = sum(net[load_bus][:Load].kvars1 for load_bus in real_load_busses(net))
60 changes: 2 additions & 58 deletions src/network.jl
Original file line number Diff line number Diff line change
@@ -1,42 +1,5 @@
"""
struct Network <: AbstractNetwork
graph::MetaGraphsNext.AbstractGraph
substation_bus::String
Sbase::Real
Vbase::Real
Zbase::Real
v0::Union{Real, AbstractVecOrMat{<:Number}}
Ntimesteps::Int
bounds::VariableBounds
var_names::AbstractVector{Symbol}
end
The `Network` model is used to store all the inputs required to create power flow and optimal power
flow models. Underlying the Network model is a `MetaGraphsNext.MetaGraph` that stores the edge and
node data in the network.
We leverage the `AbstractNetwork` type to make an intuitive interface for the Network model. For
example, `edges(network)` returns an iterator of edge tuples with bus name values; (but if we used
`Graphs.edges(MetaGraph)` we would get an iterator of Graphs.SimpleGraphs.SimpleEdge with integer
values).
A Network can be created directly, via a `Dict`, or a filepath. The minimum inputs must have a
vector of [Conductor](@ref) specifications and a `Network` key containing at least the
`substation_bus`. See [Input Formats](@ref) for more details.
`var_names` is empty be default. It is used in the results getters like `opf_results`.
"""
mutable struct Network{T<:Phases} <: AbstractNetwork
graph::MetaGraphsNext.AbstractGraph
substation_bus::String
Sbase::Real
Vbase::Real
Zbase::Real
v0::Union{Real, AbstractVecOrMat{<:Number}}
Ntimesteps::Int
bounds::VariableBounds
var_names::AbstractVector{Symbol}
end
# the Network struct is defined in types.jl so that we can use it in function signatures throughout
# the src code (by including types.jl first in the module).


network_phase_type(net::Network{T}) where {T} = T
Expand Down Expand Up @@ -229,29 +192,10 @@ j_to_k(j::String, net::Network) = collect(outneighbors(net::Network, j::String))
busses(net::AbstractNetwork) = collect(MetaGraphsNext.labels(net.graph))


load_busses(net::AbstractNetwork) = collect(b for b in busses(net) if haskey(net[b], :Load))


voltage_regulator_edges(net::AbstractNetwork) = collect(e for e in edges(net) if isa(net[e], VoltageRegulator))
# TODO account for reverse flow voltage regulation?


real_load_busses(net::Network{SinglePhase}) = collect(b for b in load_busses(net) if !ismissing(net[b][:Load].kws1))
real_load_busses(net::Network{MultiPhase}) = collect(
b for b in load_busses(net)
if !ismissing(net[b][:Load].kws1) || !ismissing(net[b][:Load].kws2) || !ismissing(net[b][:Load].kws3)
)


reactive_load_busses(net::Network{SinglePhase}) = collect(b for b in load_busses(net) if !ismissing(net[b][:Load].kvars1))
reactive_load_busses(net::Network{MultiPhase}) = collect(
b for b in load_busses(net)
if !ismissing(net[b][:Load].kvars1) || !ismissing(net[b][:Load].kvars2) || !ismissing(net[b][:Load].kvars3)
)

total_load_kw(net::Network{SinglePhase}) = sum(net[load_bus][:Load].kws1 for load_bus in real_load_busses(net))
total_load_kvar(net::Network{SinglePhase}) = sum(net[load_bus][:Load].kvars1 for load_bus in real_load_busses(net))


"""
leaf_busses(net::Network)
Expand Down
43 changes: 42 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,45 @@ MultiPhaseVariableContainerType = Dict{Int64, Dict{String, AbstractVecOrMat}}

abstract type AbstractNetwork end
abstract type AbstractEdge end
abstract type AbstractBus end
abstract type AbstractBus end


"""
struct Network <: AbstractNetwork
graph::MetaGraphsNext.AbstractGraph
substation_bus::String
Sbase::Real
Vbase::Real
Zbase::Real
v0::Union{Real, AbstractVecOrMat{<:Number}}
Ntimesteps::Int
bounds::VariableBounds
var_names::AbstractVector{Symbol}
end
The `Network` model is used to store all the inputs required to create power flow and optimal power
flow models. Underlying the Network model is a `MetaGraphsNext.MetaGraph` that stores the edge and
node data in the network.
We leverage the `AbstractNetwork` type to make an intuitive interface for the Network model. For
example, `edges(network)` returns an iterator of edge tuples with bus name values; (but if we used
`Graphs.edges(MetaGraph)` we would get an iterator of Graphs.SimpleGraphs.SimpleEdge with integer
values).
A Network can be created directly, via a `Dict`, or a filepath. The minimum inputs must have a
vector of [Conductor](@ref) specifications and a `Network` key containing at least the
`substation_bus`. See [Input Formats](@ref) for more details.
`var_names` is empty be default. It is used in the results getters like `opf_results`.
"""
mutable struct Network{T<:Phases} <: AbstractNetwork
graph::MetaGraphsNext.AbstractGraph
substation_bus::String
Sbase::Real
Vbase::Real
Zbase::Real
v0::Union{Real, AbstractVecOrMat{<:Number}}
Ntimesteps::Int
bounds::VariableBounds
var_names::AbstractVector{Symbol}
end

0 comments on commit 4e8f5af

Please sign in to comment.