Skip to content

Commit

Permalink
Merge pull request #10 from NREL-Sienna/dt/fix-tests
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
daniel-thom authored Aug 15, 2023
2 parents c5858f8 + 011e484 commit 802ab15
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 119 deletions.
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
GLPK = "60bf3e95-4087-53dc-ae20-288a0d20c6a6"
HydroPowerSimulations = "fc1677e0-6ad7-4515-bf3a-bd6bf20a0b1b"
InfrastructureSystems = "2cd47ed4-ca9b-11e9-27f2-ab636a7671f1"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Expand All @@ -12,6 +13,7 @@ PowerSimulations = "e690365d-45e2-57bb-ac84-44ba829e73c4"
PowerSystemCaseBuilder = "f00506e0-b84f-492a-93c2-c0a9afc4364e"
PowerSystems = "bcd98974-b02a-5e2f-9ee0-a103f5c450dd"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StorageSystemsSimulations = "e2f1a126-19d0-4674-9252-42b2384f8e3c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestSetExtensions = "98d24dd4-01ad-11ea-1b02-c9a08f80db04"
TimeSeries = "9e3dc215-6440-5c97-bce1-76c03772f85e"
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ using PowerAnalytics
using PowerSimulations
using GLPK
using TimeSeries
using StorageSystemsSimulations
using HydroPowerSimulations

const PA = PowerAnalytics
const IS = InfrastructureSystems
Expand Down
257 changes: 138 additions & 119 deletions test/test_data/results_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,138 +62,157 @@ function add_re!(sys)
end

function run_test_sim(result_dir::String)
mkpath(result_dir)
sim_name = "results_sim"
sim_path = joinpath(result_dir, sim_name)

if ispath(sim_path)
@info "Reading UC system from" sim_path
c_sys5_hy_uc = System(
joinpath(sim_path, "..", "c_sys5_hy_uc.json"),
time_series_read_only = true,
)
@info "Reading ED system from" sim_path
c_sys5_hy_ed = System(
joinpath(sim_path, "..", "c_sys5_hy_ed.json"),
time_series_read_only = true,
)

sim_dirs = filter(x -> occursin(sim_name, x), readdir(dirname(sim_path)))
if length(sim_dirs) == 1
sim = joinpath(dirname(sim_path), first(sim_dirs))
else
sim_dirs = filter(x -> occursin(sim_name * "-", x), sim_dirs)
executions =
tryparse.(Int, replace.(replace.(sim_dirs, sim_name => ""), "-" => ""))
sim = sim_path * "-$(maximum(executions))"
results = _try_load_simulation_results(sim_path)
if isnothing(results)
if isdir(sim_path)
rm(sim_path, recursive = true)
end
@info "Reading results from last execution" sim
else
@info "Building UC system from"
c_sys5_hy_uc = PSB.build_system(PSB.PSISystems, "5_bus_hydro_uc_sys")
@info "Building ED system from"
c_sys5_hy_ed = PSB.build_system(PSB.PSISystems, "5_bus_hydro_ed_sys")

@info "Adding extra RE"
add_re!(c_sys5_hy_uc)
add_re!(c_sys5_hy_ed)
to_json(c_sys5_hy_uc, joinpath(sim_path, "..", "c_sys5_hy_uc.json"), force = true)
to_json(c_sys5_hy_ed, joinpath(sim_path, "..", "c_sys5_hy_ed.json"), force = true)

mkpath(result_dir)
GLPK_optimizer =
optimizer_with_attributes(GLPK.Optimizer, "msg_lev" => GLPK.GLP_MSG_OFF)

template_hydro_st_uc =
ProblemTemplate(NetworkModel(CopperPlatePowerModel, use_slacks = false))
set_device_model!(template_hydro_st_uc, ThermalStandard, ThermalBasicUnitCommitment)
set_device_model!(template_hydro_st_uc, PowerLoad, StaticPowerLoad)
set_device_model!(template_hydro_st_uc, RenewableFix, FixedOutput)
set_device_model!(template_hydro_st_uc, RenewableDispatch, RenewableFullDispatch)
set_device_model!(template_hydro_st_uc, HydroDispatch, FixedOutput)
set_device_model!(template_hydro_st_uc, GenericBattery, BookKeeping)
set_device_model!(
template_hydro_st_uc,
HydroEnergyReservoir,
HydroDispatchReservoirStorage,
)
set_service_model!(template_hydro_st_uc, VariableReserve{ReserveUp}, RangeReserve)
results = _execute_simulation(result_dir, sim_name)
end

results_uc = get_decision_problem_results(results, "UC")
results_ed = get_decision_problem_results(results, "ED")

return results_uc, results_ed
end

function _execute_simulation(base_path, sim_name)
@info "Building UC system from"
c_sys5_hy_uc = PSB.build_system(PSB.PSISystems, "5_bus_hydro_uc_sys")
@info "Building ED system from"
c_sys5_hy_ed = PSB.build_system(PSB.PSISystems, "5_bus_hydro_ed_sys")

@info "Adding extra RE"
add_re!(c_sys5_hy_uc)
add_re!(c_sys5_hy_ed)

GLPK_optimizer =
optimizer_with_attributes(GLPK.Optimizer, "msg_lev" => GLPK.GLP_MSG_OFF)

template_hydro_st_uc =
ProblemTemplate(NetworkModel(CopperPlatePowerModel, use_slacks = false))
set_device_model!(template_hydro_st_uc, ThermalStandard, ThermalBasicUnitCommitment)
set_device_model!(template_hydro_st_uc, PowerLoad, StaticPowerLoad)
set_device_model!(template_hydro_st_uc, RenewableFix, FixedOutput)
set_device_model!(template_hydro_st_uc, RenewableDispatch, RenewableFullDispatch)
set_device_model!(template_hydro_st_uc, HydroDispatch, FixedOutput)
set_device_model!(template_hydro_st_uc, GenericBattery, BookKeeping)
set_device_model!(
template_hydro_st_uc,
HydroEnergyReservoir,
HydroDispatchReservoirStorage,
)
set_service_model!(template_hydro_st_uc, VariableReserve{ReserveUp}, RangeReserve)

template_hydro_st_ed = ProblemTemplate(
NetworkModel(
CopperPlatePowerModel,
use_slacks = true,
duals = [CopperPlateBalanceConstraint],
),
)
set_device_model!(template_hydro_st_ed, ThermalStandard, ThermalBasicDispatch)
set_device_model!(template_hydro_st_ed, PowerLoad, StaticPowerLoad)
set_device_model!(template_hydro_st_ed, RenewableFix, FixedOutput)
set_device_model!(template_hydro_st_ed, RenewableDispatch, RenewableFullDispatch)
set_device_model!(template_hydro_st_ed, HydroDispatch, FixedOutput)
set_device_model!(template_hydro_st_ed, GenericBattery, BookKeeping)
set_device_model!(
template_hydro_st_ed,
HydroEnergyReservoir,
HydroDispatchReservoirStorage,
)

template_hydro_st_ed = ProblemTemplate(
NetworkModel(
CopperPlatePowerModel,
use_slacks = true,
duals = [CopperPlateBalanceConstraint],
models = SimulationModels(
decision_models = [
DecisionModel(
template_hydro_st_uc,
c_sys5_hy_uc,
optimizer = GLPK_optimizer,
name = "UC",
system_to_file = true,
),
)
set_device_model!(template_hydro_st_ed, ThermalStandard, ThermalBasicDispatch)
set_device_model!(template_hydro_st_ed, PowerLoad, StaticPowerLoad)
set_device_model!(template_hydro_st_ed, RenewableFix, FixedOutput)
set_device_model!(template_hydro_st_ed, RenewableDispatch, RenewableFullDispatch)
set_device_model!(template_hydro_st_ed, HydroDispatch, FixedOutput)
set_device_model!(template_hydro_st_ed, GenericBattery, BookKeeping)
set_device_model!(
template_hydro_st_ed,
HydroEnergyReservoir,
HydroDispatchReservoirStorage,
)
DecisionModel(
template_hydro_st_ed,
c_sys5_hy_ed,
optimizer = GLPK_optimizer,
name = "ED",
system_to_file = true,
),
],
)

models = SimulationModels(
decision_models = [
DecisionModel(
template_hydro_st_uc,
c_sys5_hy_uc,
optimizer = GLPK_optimizer,
name = "UC",
system_to_file = false,
),
DecisionModel(
template_hydro_st_ed,
c_sys5_hy_ed,
optimizer = GLPK_optimizer,
name = "ED",
system_to_file = false,
sequence = SimulationSequence(
models = models,
feedforwards = feedforward = Dict(
"ED" => [
SemiContinuousFeedforward(
component_type = ThermalStandard,
source = OnVariable,
affected_values = [ActivePowerVariable],
),
# TODO: restore this when it's fixed in PSI
# EnergyLimitFeedforward(
# component_type = HydroEnergyReservoir,
# source = ActivePowerVariable,
# affected_values = [ActivePowerVariable],
# number_of_periods = 12,
# ),
],
)
),
ini_cond_chronology = InterProblemChronology(),
)
sim = Simulation(
name = sim_name,
steps = 2,
models = models,
sequence = sequence,
simulation_folder = base_path,
)
build!(sim)
execute!(sim)
return SimulationResults(sim)
end

sequence = SimulationSequence(
models = models,
feedforwards = feedforward = Dict(
"ED" => [
SemiContinuousFeedforward(
component_type = ThermalStandard,
source = OnVariable,
affected_values = [ActivePowerVariable],
),
# TODO: restore this when it's fixed in PSI
# EnergyLimitFeedforward(
# component_type = HydroEnergyReservoir,
# source = ActivePowerVariable,
# affected_values = [ActivePowerVariable],
# number_of_periods = 12,
# ),
],
),
ini_cond_chronology = InterProblemChronology(),
)
sim = Simulation(
name = "results_sim",
steps = 2,
models = models,
sequence = sequence,
simulation_folder = result_dir,
)
build!(sim)
execute!(sim)
function _try_load_simulation_results(sim_path)
!isdir(sim_path) && return nothing
c_sys5_hy_uc_path = get_system_file_path(joinpath(sim_path, "problems", "UC"))
isnothing(c_sys5_hy_uc_path) && return nothing
c_sys5_hy_ed_path = get_system_file_path(joinpath(sim_path, "problems", "ED"))
isnothing(c_sys5_hy_ed_path) && return nothing
!isfile(c_sys5_hy_uc_path) || !isfile(c_sys5_hy_ed_path) && return nothing

try
results = SimulationResults(sim_path)
results_uc = get_decision_problem_results(results, "UC")
results_ed = get_decision_problem_results(results, "ED")
@info "Reading UC system from" sim_path
c_sys5_hy_uc = System(c_sys5_hy_uc_path, time_series_read_only = true)
@info "Reading ED system from" sim_path
c_sys5_hy_ed = System(c_sys5_hy_ed_path, time_series_read_only = true)
set_system!(results_uc, c_sys5_hy_uc)
set_system!(results_ed, c_sys5_hy_ed)
return results
catch e
@info "Failed to load the results from $sim_path. The results may be incomplete. $e"
end

results = SimulationResults(sim)
results_uc = get_decision_problem_results(results, "UC")
set_system!(results_uc, c_sys5_hy_uc)
results_ed = get_decision_problem_results(results, "ED")
set_system!(results_ed, c_sys5_hy_ed)
return nothing
end

return results_uc, results_ed
function get_system_file_path(path)
!isdir(path) && return nothing
files = readdir(path)
for filename in files
m = match(r"system-[\w-]+.json", filename)
!isnothing(m) && return joinpath(path, m.match)
end

return nothing
end

function run_test_prob()
Expand Down

0 comments on commit 802ab15

Please sign in to comment.