From 952c15b36c4a6e9a5e23bf86fd5d50336f7da26f Mon Sep 17 00:00:00 2001 From: Noah Rhodes Date: Sun, 6 Oct 2024 15:31:28 -0500 Subject: [PATCH] Change PMG types to support non-numeric component names (PMD) --- src/core/types.jl | 20 ++++++++-------- test/scratchspace.jl | 57 ++++++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/core/types.jl b/src/core/types.jl index 66f8a7c..b1d7bd5 100644 --- a/src/core/types.jl +++ b/src/core/types.jl @@ -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 @@ -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"])] @@ -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 diff --git a/test/scratchspace.jl b/test/scratchspace.jl index cd3798d..941477e 100644 --- a/test/scratchspace.jl +++ b/test/scratchspace.jl @@ -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) ################################################ \ No newline at end of file