Skip to content

Commit

Permalink
Add prefixes, adjust imports to make standalone scripts work
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielKS committed Jul 15, 2024
1 parent f4b5197 commit d5e054a
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 167 deletions.
5 changes: 5 additions & 0 deletions src/PowerFlows.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ include("post_processing.jl")

# PSSE Exporter
import PowerSystems: System
import DataFrames: DataFrame
import Dates
import DataStructures: OrderedDict
import DelimitedFiles
import JSON
include("psse_exporter/support_tools.jl")
include("psse_exporter/psse_exporter.jl")

Expand Down
43 changes: 24 additions & 19 deletions src/psse_exporter/compare_psse_results.jl
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
# NOTE this is a standalone script -- neither a part of the library codebase nor a part of the standard test suite.
# Invoke with `include("path/to/compare_psse_results.jl")` in an appropriate environment.
# TODO remove when superseded by encapsulated utility functions and/or standard test suite.

using Revise
using Logging
using DataFrames

using PowerSystems
using PowerSystemCaseBuilder
using PowerFlows

PF = PowerFlows
const PF = PowerFlows

function PSY.get_reactive_power_limits(gen::PSY.RenewableFix)
gen_pf = PSY.get_power_factor(gen)
function PowerSystems.get_reactive_power_limits(gen::RenewableNonDispatch)
gen_pf = get_power_factor(gen)

gen_q = PSY.get_max_active_power(gen) * sqrt((1 / gen_pf^2) - 1)
gen_q = get_max_active_power(gen) * sqrt((1 / gen_pf^2) - 1)

return (min = 0.0, max = gen_q)
end

# Import test cases
# sys = build_system(PSIDSystems, "WECC 240 Bus")
file_dir = joinpath(DATA_DIR, "twofortybus", "Marenas")
file_dir = joinpath(PF.DATA_DIR, "twofortybus", "Marenas")
sys = with_logger(SimpleLogger(Error)) do # Suppress system loading warnings
System(joinpath(file_dir, "system_240[32].json"))
end
set_units_base_system!(sys, PSY.IS.UnitSystem.SYSTEM_BASE)
set_units_base_system!(sys, UnitSystem.SYSTEM_BASE)

# Load from .raw file
file_dir2 = joinpath(DATA_DIR, "Raw_Export", "basic", "2024")
# Load from .raw file (this gets written in `test_powerflow.jl`)
file_dir2 = joinpath(PF.DATA_DIR, "export", "Raw_Export", "basic", "2024")
sys2 = with_logger(SimpleLogger(Error)) do # Suppress system loading warnings
System(joinpath(file_dir2, "basic 4_solved2.raw"))
System(joinpath(file_dir2, "basic.raw"))
end
set_units_base_system!(sys2, PSY.IS.UnitSystem.SYSTEM_BASE)
set_units_base_system!(sys2, UnitSystem.SYSTEM_BASE)

# DC Powerflow testing
orig_results = solve_powerflow(DCPowerFlow(), sys)
old_bus_results = Bus_states(sys)
old_branch_results = Branch_states(sys)
old_bus_results = PF.Bus_states(sys)
old_branch_results = PF.Branch_states(sys)
orig_flow_results =
sort!(orig_results["1"]["flow_results"], [:bus_from, :bus_to, :line_name])
orig_bus_results = orig_results["1"]["bus_results"]

psse_bus_results = Bus_states(sys2)
psse_branch_results = Branch_states(sys2)
psse_bus_results = PF.Bus_states(sys2)
psse_branch_results = PF.Branch_states(sys2)
new_results = solve_powerflow(DCPowerFlow(), sys2)
new_flows = sort!(new_results["1"]["flow_results"], [:bus_from, :bus_to, :line_name])
new_bus_results = new_results["1"]["bus_results"]
Expand Down Expand Up @@ -82,19 +87,19 @@ quant_del_y_bus = DataFrame(; Real = Float64[], Imag = Float64[])
for i in del_y_bus
del_r = real(orig_y_bus[i] - new_y_bus[i])
del_i = imag(orig_y_bus[i] - new_y_bus[i])
push!(quant_del, [del_r, del_i])
push!(quant_del_y_bus, [del_r, del_i])
end

@show quant_del
@show quant_del_y_bus

# gen_busses = ThermalStandard_states(sys2)
# show(gen_busses, allrows=true)

# gen_busses = sort!(append!(Generator_states(sys), Source_states(sys)), [:bus_number, :active_power])
# show(gen_busses, allrows=true)

avaialabe_gens =
DataFrame("gen_name" => collect(get_name.(get_components(RenewableFix, sys))))
show(avaialabe_gens; allrows = true)
available_gens =
DataFrame("gen_name" => collect(get_name.(get_components(RenewableNonDispatch, sys))))
show(available_gens; allrows = true)

print(get_component(Source, sys, "generator-4242-ND"))
51 changes: 22 additions & 29 deletions src/psse_exporter/psse_exporter.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using Dates
using DataStructures
using PowerSystems
using DelimitedFiles
using JSON

PSY = PowerSystems

# TODO document this function
function Write_Sienna2PSSE(sys::System, scenario_name::String, year::Int64;
export_location::Union{Nothing, String} = nothing, base_case = false, setpoint = false,
setpoint_ts::Union{Nothing, Dates.DateTime} = nothing,
Expand All @@ -21,6 +14,7 @@ function Write_Sienna2PSSE(sys::System, scenario_name::String, year::Int64;
else
export_location = joinpath(export_location, "Raw_Export")
end
@info "Exporting to $export_location"

if (setpoint)
if (setpoint_ts === nothing || results_dir === nothing)
Expand Down Expand Up @@ -49,7 +43,6 @@ function Write_Sienna2PSSE(sys::System, scenario_name::String, year::Int64;
PSY.set_units_base_system!(sys, PSY.IS.UnitSystem.NATURAL_UNITS)

export_ts =

if (setpoint)
populate_setpoints!(sys, results_dir, setpoint_ts)
end
Expand All @@ -64,12 +57,12 @@ function Write_Sienna2PSSE(sys::System, scenario_name::String, year::Int64;
if (v33)
push!(
raw_file,
"0, $(get_base_power(sys)), 33, 0, 1, 60.00 / PSS(R)E 33 RAW created by Sienna2PF.jl $(dt_now)",
"0, $(PSY.get_base_power(sys)), 33, 0, 1, 60.00 / PSS(R)E 33 RAW created by Sienna2PF.jl $(dt_now)",
)
else
push!(
raw_file,
"0, $(get_base_power(sys)), 34, 0, 1, 60.00 / PSS(R)E 34 RAW created by Sienna2PF.jl $(dt_now)",
"0, $(PSY.get_base_power(sys)), 34, 0, 1, 60.00 / PSS(R)E 34 RAW created by Sienna2PF.jl $(dt_now)",
)
end

Expand Down Expand Up @@ -212,8 +205,8 @@ function Write_Sienna2PSSE(sys::System, scenario_name::String, year::Int64;
i = PSY.get_number(PSY.get_bus(shunt))
id = uppercase(first(last(split(PSY.get_name(shunt), "-")), 2))
stat = get_PSSE_status(PSY.get_available(shunt))
gl = real(PSY.get_Y(shunt)) * get_base_power(sys) # Sienna expects system base, but PSS/e expects MW
bl = imag(PSY.get_Y(shunt)) * get_base_power(sys) # Sienna expects system base, but PSS/e expects MW
gl = real(PSY.get_Y(shunt)) * PSY.get_base_power(sys) # Sienna expects system base, but PSS/e expects MW
bl = imag(PSY.get_Y(shunt)) * PSY.get_base_power(sys) # Sienna expects system base, but PSS/e expects MW

if (v33)
shunt_entry = "$(i), $(id), $(stat), $(gl), $(bl)"
Expand All @@ -237,33 +230,33 @@ function Write_Sienna2PSSE(sys::System, scenario_name::String, year::Int64;
psy_gens = collect(PSY.get_components(PSY.Generator, sys))

for gen in psy_gens
if typeof(gen) == ThermalStandard
if gen isa PSY.ThermalStandard
gen_bus_num = PSY.get_number(PSY.get_bus(gen))
if (haskey(bus_mapping, gen_bus_num))
gen_bus_num = bus_mapping[gen_bus_num]
end
gen_id = uppercase(first(last(split(PSY.get_name(gen), "-")), 2))
p_g = base_case ? 0.0 : PSY.get_active_power(gen)
q_g = base_case ? 0.0 : PSY.get_reactive_power(gen)
if get_reactive_power_limits(gen)[:max] > get_rating(gen)
if PSY.get_reactive_power_limits(gen)[:max] > PSY.get_rating(gen)
q_t = 999.0
q_b = -999.0
else
q_t = get_reactive_power_limits(gen)[:max]
q_b = get_reactive_power_limits(gen)[:min]
q_t = PSY.get_reactive_power_limits(gen)[:max]
q_b = PSY.get_reactive_power_limits(gen)[:min]
end
v_s = PSY.get_magnitude(PSY.get_bus(gen))
ireg = 0 # DEFAULT
mbase = get_base_power(gen)
mbase = PSY.get_base_power(gen)
z_r = 0.0 # DEFAULT
z_x = 1.0 # DEFAULT
r_t = 0.0 # DEFAULT
x_t = 0.0 # DEFAULT
gtap = 1.0 # DEFAULT
stat = get_PSSE_status(PSY.get_available(gen))
rmpct = 100.0 # DEFAULT
p_t = get_active_power_limits(gen)[:max]
p_b = get_active_power_limits(gen)[:min]
p_t = PSY.get_active_power_limits(gen)[:max]
p_b = PSY.get_active_power_limits(gen)[:min]
o_i = 1 # DEFAULT
f_i = 1.0 # DEFAULT
wmod = get_psse_gen_wmod(gen)
Expand All @@ -288,19 +281,19 @@ function Write_Sienna2PSSE(sys::System, scenario_name::String, year::Int64;
gen_id = uppercase(first(last(split(PSY.get_name(gen), "-")), 2))
p_g = base_case ? 0.0 : PSY.get_active_power(gen)
q_g = base_case ? 0.0 : PSY.get_reactive_power(gen)
q_t = get_rating(gen)cos/ 4)
q_t = PSY.get_rating(gen)cos/ 4)
q_b = 0.0
v_s = PSY.get_magnitude(PSY.get_bus(gen))
ireg = 0 # DEFAULT
mbase = get_base_power(gen)
mbase = PSY.get_base_power(gen)
z_r = 0.0 # DEFAULT
z_x = 1.0 # DEFAULT
r_t = 0.0 # DEFAULT
x_t = 0.0 # DEFAULT
gtap = 1.0 # DEFAULT
stat = get_PSSE_status(PSY.get_available(gen))
rmpct = 100.0 # DEFAULT
p_t = get_rating(gen)cos/ 4)
p_t = PSY.get_rating(gen)cos/ 4)
p_b = 0.0
o_i = 1 # DEFAULT
f_i = 1.0 # DEFAULT
Expand All @@ -326,13 +319,13 @@ function Write_Sienna2PSSE(sys::System, scenario_name::String, year::Int64;
gen_bus_num = bus_mapping[gen_bus_num]
end
gen_id = uppercase(first(last(split(PSY.get_name(source), "-")), 2))
p_g = base_case ? 0.0 : PSY.get_active_power(source) * get_base_power(sys)
q_g = base_case ? 0.0 : PSY.get_reactive_power(source) * get_base_power(sys)
p_g = base_case ? 0.0 : PSY.get_active_power(source) * PSY.get_base_power(sys)
q_g = base_case ? 0.0 : PSY.get_reactive_power(source) * PSY.get_base_power(sys)
q_t = 10000 * cos/ 4)
q_b = 0.0
v_s = PSY.get_internal_voltage(source)
ireg = 0 # DEFAULT
mbase = get_base_power(source)
mbase = PSY.get_base_power(source)
z_r = 0.0 # DEFAULT
z_x = 1.0 # DEFAULT
r_t = 0.0 # DEFAULT
Expand Down Expand Up @@ -379,7 +372,7 @@ function Write_Sienna2PSSE(sys::System, scenario_name::String, year::Int64;

# line_dict = Dict{Tuple{Int64, Int64}, Int64}()
for branch in psy_branches
if typeof(branch) == Line
if branch isa PSY.Line
i = PSY.get_number(PSY.get_from_bus(branch))
if (haskey(bus_mapping, i))
i = bus_mapping[i]
Expand Down Expand Up @@ -455,7 +448,7 @@ function Write_Sienna2PSSE(sys::System, scenario_name::String, year::Int64;
# twowind_xfmr_dict = Dict{Tuple{Int64, Int64}, Int64}()
# tap_xfmr_dict = Dict{Tuple{Int64, Int64}, Int64}()
for branch in psy_branches
if typeof(branch) == Transformer2W
if branch isa PSY.Transformer2W
i = PSY.get_number(PSY.get_from_bus(branch))
if (haskey(bus_mapping, i))
i = bus_mapping[i]
Expand Down Expand Up @@ -534,7 +527,7 @@ function Write_Sienna2PSSE(sys::System, scenario_name::String, year::Int64;
push!(raw_file, branch_entry3)
push!(raw_file, branch_entry4)

elseif typeof(branch) == TapTransformer
elseif branch isa PSY.TapTransformer
i = PSY.get_number(PSY.get_from_bus(branch))
if (haskey(bus_mapping, i))
i = bus_mapping[i]
Expand Down
Loading

0 comments on commit d5e054a

Please sign in to comment.