Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add read_variables for single row variables (no timestamp dependence) #1031

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/operation/operation_model_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,27 @@
read_variable(model::OperationModel, key::VariableKey) = _read_results(model, key)
read_expression(model::OperationModel, key::ExpressionKey) = _read_results(model, key)

function _read_col_name(axes)
if length(axes) == 1
error("Axes of size 1 are not supported")

Check warning on line 326 in src/operation/operation_model_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/operation/operation_model_interface.jl#L326

Added line #L326 was not covered by tests
end
# Currently, variables that don't have timestamps have a dummy axes to keep
# two axes in the Store (HDF or Memory). This if-else is used to decide if a
# dummy axes is being used or not.
if typeof(axes[2]) <: UnitRange{Int}
return axes[1]
elseif typeof(axes[2]) <: Vector{String}
IS.@assert_op length(axes[1]) == 1
return axes[2]
else
error("Second axes in store is not allowed to be $(typeof(axes[2]))")

Check warning on line 337 in src/operation/operation_model_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/operation/operation_model_interface.jl#L337

Added line #L337 was not covered by tests
end
end

function _read_results(model::OperationModel, key::OptimizationContainerKey)
res = read_results(get_store(model), key)
return DataFrames.DataFrame(permutedims(res.data), axes(res)[1])
col_name = _read_col_name(axes(res))
return DataFrames.DataFrame(permutedims(res.data), col_name)
end

read_optimizer_stats(model::OperationModel) = read_optimizer_stats(get_store(model))
Expand Down
24 changes: 17 additions & 7 deletions src/operation/problem_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,23 @@
results = Dict{OptimizationContainerKey, DataFrames.DataFrame}()
for (k, v) in result_values
if k in container_keys
results[k] =
if convert_result_to_natural_units(k)
v[time_ids, :] .* base_power
else
v[time_ids, :]
end
DataFrames.insertcols!(results[k], 1, :DateTime => timestamps)
num_rows = DataFrames.nrow(v)
if num_rows == 1 && num_rows < length(time_ids)
results[k] =
if convert_result_to_natural_units(k)
v .* base_power

Check warning on line 318 in src/operation/problem_results.jl

View check run for this annotation

Codecov / codecov/patch

src/operation/problem_results.jl#L318

Added line #L318 was not covered by tests
else
v
end
else
results[k] =
if convert_result_to_natural_units(k)
v[time_ids, :] .* base_power
else
v[time_ids, :]
end
DataFrames.insertcols!(results[k], 1, :DateTime => timestamps)
end
end
end
return results
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
SCS = "c946c3f1-0d1f-5ce8-9dea-7daa1f7e2d13"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
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
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using PowerNetworkMatrices
using HydroPowerSimulations
import PowerSystemCaseBuilder: PSITestSystems
using PowerNetworkMatrices
using StorageSystemsSimulations

# Test Packages
using Test
Expand Down
28 changes: 28 additions & 0 deletions test/test_model_decision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,31 @@ end
@test build!(UC; output_dir = output_dir) == PSI.BuildStatus.BUILT
@test solve!(UC) == RunStatus.SUCCESSFUL
end

@testset "Test for single row result variables" begin
template = get_thermal_dispatch_template_network()
c_sys5_bat = PSB.build_system(PSITestSystems, "c_sys5_bat_ems"; force_build = true)
device_model = DeviceModel(
BatteryEMS,
StorageDispatchWithReserves;
attributes = Dict{String, Any}(
"reservation" => true,
"cycling_limits" => false,
"energy_target" => true,
"complete_coverage" => false,
"regularization" => false,
),
)
set_device_model!(template, device_model)
output_dir = mktempdir(; cleanup = true)
model = DecisionModel(
template,
c_sys5_bat;
optimizer = GLPK_optimizer,
)
@test build!(model; output_dir = output_dir) == PSI.BuildStatus.BUILT
@test solve!(model) == RunStatus.SUCCESSFUL
res = ProblemResults(model)
shortage = read_variable(res, "StorageEnergyShortageVariable__BatteryEMS")
@test nrow(shortage) == 1
end
Loading