Skip to content

Commit

Permalink
Change PMG types to support non-numeric component names (PMD)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahrhodes committed Oct 6, 2024
1 parent c012db5 commit 952c15b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
20 changes: 10 additions & 10 deletions src/core/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ four fields: a Graphs.SimpleDiGraph, a map from the node ids to the components,
"""
mutable struct PowerModelsGraph
graph::Graphs.SimpleGraph
node_comp_map::Dict{Int,Tuple{Symbol, Int}}
edge_comp_map::Dict{Tuple{Int,Int},Tuple{Symbol, Int}}
edge_connector_map::Dict{Tuple{Int,Int},Tuple{Symbol, Int}}
node_comp_map::Dict{Int,Tuple{Symbol, Symbol}}
edge_comp_map::Dict{Tuple{Int,Int},Tuple{Symbol, Symbol}}
edge_connector_map::Dict{Tuple{Int,Int},Tuple{Symbol, Symbol}}

function PowerModelsGraph(data::Dict{String,<:Any}, node_components::Array{Symbol,1}, edge_components::Array{Symbol,1}, connected_components::Array{Symbol,1})

graph_node_count = sum(length(keys(get(data,string(comp_type),Dict()))) for comp_type in [node_components...,connected_components...])
G = Graphs.SimpleGraph(graph_node_count) # create graph

node_comp_array = Vector{Tuple{Symbol,Int}}(undef,graph_node_count)
node_comp_array = Vector{Tuple{Symbol,Symbol}}(undef,graph_node_count)
i_1 = 1
for comp_type in [node_components...,connected_components...]
if haskey(data,string(comp_type))
for comp_id in sort(parse.(Int,collect(keys(data[string(comp_type)])))) #sort seems to get better layout results?
for comp_id in Symbol.(sort(collect(keys(data[string(comp_type)])))) #sort seems to get better layout results?
node_comp_array[i_1] = (comp_type,comp_id)
i_1+=1
end
Expand All @@ -41,13 +41,13 @@ mutable struct PowerModelsGraph


edge_comp_count = sum(length(keys(get(data,string(comp_type),Dict()))) for comp_type in edge_components)
edge_comp_array = Vector{Tuple{Symbol,Int}}(undef,edge_comp_count)
edge_comp_array = Vector{Tuple{Symbol,Symbol}}(undef,edge_comp_count)
edge_node_array = Vector{Tuple{Int,Int}}(undef,edge_comp_count)
i_2 = 1
for comp_type in edge_components # add edges
if haskey(data,string(comp_type))
for (comp_id,comp) in data[string(comp_type)]
edge_comp_array[i_2] = (comp_type,parse(Int,comp_id))
edge_comp_array[i_2] = (comp_type,Symbol(comp_id))
#TODO generically identify the node type and keys of source and destination
s = comp_node_map[(:bus,comp["f_bus"])]
d = comp_node_map[(:bus,comp["t_bus"])]
Expand All @@ -61,15 +61,15 @@ mutable struct PowerModelsGraph

edge_connector_count = sum(length(keys(get(data,string(comp_type),Dict()))) for comp_type in connected_components; init=0)

edge_connector_array = Vector{Tuple{Symbol,Int}}(undef,edge_connector_count)
edge_connector_array = Vector{Tuple{Symbol,Symbol}}(undef,edge_connector_count)
edge_node_array = Vector{Tuple{Int,Int}}(undef,edge_connector_count)
i_3 = 1
for comp_type in connected_components #add connectors
if haskey(data,string(comp_type))
for (comp_id,comp) in data[string(comp_type)]
edge_connector_array[i_3] = (comp_type,parse(Int,comp_id))
edge_connector_array[i_3] = (comp_type,Symbol(comp_id))
s = comp_node_map[(:bus,comp["$(string(comp_type))_bus"])]
d = comp_node_map[(comp_type,parse(Int,comp_id))]
d = comp_node_map[(comp_type,Symbol(comp_id))]
edge_node_array[i_3] = (s,d)
Graphs.add_edge!(G, s, d)
i_3+=1
Expand Down
57 changes: 34 additions & 23 deletions test/scratchspace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,50 @@ data = parse_file("$(joinpath(dirname(pathof(PowerModels)), ".."))/test/data/mat
################################################


pmg = PowerModelsGraph(data,[:bus],[:branch],[:gen,:load])
# pmg = PowerModelsGraph(data,[:bus],[:branch],[:gen,:load])

data = layout_network(data; node_components=[:bus], edge_components=[:branch], connected_components=[:gen, :load])
# data = layout_network(data; node_components=[:bus], edge_components=[:branch], connected_components=[:gen, :load])

pmd= PowerModelsDataFrame(data)
# pmd= PowerModelsDataFrame(data)

powerplot(data, connected_components=[:gen,:load,:storage])
# powerplot(data, connected_components=[:gen,:load,:storage])

p = powerplot(data,
bus_data=:index, bus_data_type=:quantitative,
branch_data=:index, branch_data_type=:quantitative,
gen_data=:index, gen_data_type=:quantitative,
height=250, width=250, bus_size=100, gen_size=50, load_size=50,
)
# p = powerplot(data,
# bus_data=:index, bus_data_type=:quantitative,
# branch_data=:index, branch_data_type=:quantitative,
# gen_data=:index, gen_data_type=:quantitative,
# height=250, width=250, bus_size=100, gen_size=50, load_size=50,
# )


powerplot(data; bus=(:color=>"orange"),
gen=(:color=>:purple),
branch=(:color=>"#AFAFAF"),
load=(:color=>:black),
width=500, height=300)
# powerplot(data; bus=(:color=>"orange"),
# gen=(:color=>:purple),
# branch=(:color=>"#AFAFAF"),
# load=(:color=>:black),
# width=500, height=300)


p = powerplot(data,
# gen=[:data=>"pmax", :size=>200, :data_type=>"quantitative", :color=>["#232323","#AAFAFA"]],
# load=[:data=>"pd", :size=>500, :data_type=>"quantitative", :color=>["red","orange"]],
branch=[:show_flow=>true, :show_flow_legend=>true, :data=>"rate_a", :size=>3, :data_type=>"quantitative", :color=>["black","blue"]],
# bus=[:data=>"vmax", :size=>1000, :data_type=>"quantitative", :color=>["yellow","green"]],
width=300, height=300, parallel_edge_offset=0.05
)
# p = powerplot(data,
# # gen=[:data=>"pmax", :size=>200, :data_type=>"quantitative", :color=>["#232323","#AAFAFA"]],
# # load=[:data=>"pd", :size=>500, :data_type=>"quantitative", :color=>["red","orange"]],
# branch=[:show_flow=>true, :show_flow_legend=>true, :data=>"rate_a", :size=>3, :data_type=>"quantitative", :color=>["black","blue"]],
# # bus=[:data=>"vmax", :size=>1000, :data_type=>"quantitative", :color=>["yellow","green"]],
# width=300, height=300, parallel_edge_offset=0.05
# )

# powerplot(data)
# powerplot(data; bus=[:color=>:red], branch=[:color=>["blue", :green]])

# powerplot(data; show_flow_legend=true)
# powerplot(data; bus=[:data=>:ComponentType, :data_type=>:ordinal],
# gen=[:data=>"ComponentType", :data_type=>"nominal"])
# # typeof([:a=>1,:b=>2])::Type{<:Vector{T}} where {T<:Pair}

typeof([:a=>1,:b=>2])::Type{<:Vector{T}} where {T<:Pair}
# distribution networks
using PowerModelsDistribution
eng = PowerModelsDistribution.parse_file("$(joinpath(dirname(pathof(PowerModelsDistribution)), ".."))/test/data/opendss/trans_3w_center_tap.dss")

# ERROR parent component is always "bus", not "gen_bus" or "load_bus"
powerplot(eng)

################################################

0 comments on commit 952c15b

Please sign in to comment.