Skip to content

Commit

Permalink
move model internal to IS
Browse files Browse the repository at this point in the history
  • Loading branch information
jd-lara committed Mar 1, 2024
1 parent 74cefc9 commit 8f392bf
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 126 deletions.
44 changes: 4 additions & 40 deletions src/core/optimization_container.jl
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
"""
Optimization Container construction stage
"""
abstract type ConstructStage end

struct ArgumentConstructStage <: ConstructStage end
struct ModelConstructStage <: ConstructStage end

struct OptimizationContainerMetadata
container_key_lookup::Dict{String, <:IS.OptimizationContainerKey}
end

function OptimizationContainerMetadata()
return OptimizationContainerMetadata(Dict{String, IS.OptimizationContainerKey}())
end

function deserialize_metadata(
::Type{OptimizationContainerMetadata},
output_dir::String,
model_name,
)
filename = _make_metadata_filename(model_name, output_dir)
return Serialization.deserialize(filename)
end

function deserialize_key(metadata::OptimizationContainerMetadata, name::AbstractString)
!haskey(metadata.container_key_lookup, name) && error("$name is not stored")
return metadata.container_key_lookup[name]
end

add_container_key!(x::OptimizationContainerMetadata, key, val) =
x.container_key_lookup[key] = val
get_container_key(x::OptimizationContainerMetadata, key) = x.container_key_lookup[key]
has_container_key(x::OptimizationContainerMetadata, key) =
haskey(x.container_key_lookup, key)

struct PrimalValuesCache
variables_cache::Dict{IS.VariableKey, AbstractArray}
expressions_cache::Dict{IS.ExpressionKey, AbstractArray}
Expand Down Expand Up @@ -87,7 +51,7 @@ function ObjectiveFunction()
)
end

mutable struct OptimizationContainer <: IS.AbstractModelContainer
mutable struct OptimizationContainer <: IS.IS.AbstractOptimizationContainer
JuMPmodel::JuMP.Model
time_steps::UnitRange{Int}
resolution::Dates.TimePeriod
Expand All @@ -108,7 +72,7 @@ mutable struct OptimizationContainer <: IS.AbstractModelContainer
base_power::Float64
optimizer_stats::IS.OptimizerStats
built_for_recurrent_solves::Bool
metadata::OptimizationContainerMetadata
metadata::IS.OptimizationContainerMetadata
default_time_series_type::Type{<:PSY.TimeSeriesData}
end

Expand Down Expand Up @@ -152,7 +116,7 @@ function OptimizationContainer(
PSY.get_base_power(sys),
IS.OptimizerStats(),
false,
OptimizationContainerMetadata(),
IS.OptimizationContainerMetadata(),
T,
)
end
Expand Down Expand Up @@ -813,7 +777,7 @@ function deserialize_metadata!(
)
merge!(
container.metadata.container_key_lookup,
deserialize_metadata(OptimizationContainerMetadata, output_dir, model_name),
deserialize_metadata(IS.OptimizationContainerMetadata, output_dir, model_name),
)
return
end
Expand Down
4 changes: 2 additions & 2 deletions src/operation/decision_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mutable struct DecisionModel{M <: DecisionProblem} <: OperationModel
name::Symbol
template::AbstractProblemTemplate
sys::PSY.System
internal::Union{Nothing, ModelInternal}
internal::Union{Nothing, IS.ModelInternal}
store::DecisionModelStore
ext::Dict{String, Any}
end
Expand Down Expand Up @@ -72,7 +72,7 @@ function DecisionModel{M}(
elseif name isa String
name = Symbol(name)
end
internal = ModelInternal(
internal = IS.ModelInternal(
OptimizationContainer(sys, settings, jump_model, PSY.Deterministic),
)
template_ = deepcopy(template)
Expand Down
2 changes: 1 addition & 1 deletion src/operation/decision_model_store.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end

function initialize_storage!(
store::DecisionModelStore,
container::IS.AbstractModelContainer,
container::IS.IS.AbstractOptimizationContainer,
params::ModelStoreParams,
)
num_of_executions = get_num_executions(params)
Expand Down
4 changes: 2 additions & 2 deletions src/operation/emulation_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mutable struct EmulationModel{M <: EmulationProblem} <: OperationModel
name::Symbol
template::AbstractProblemTemplate
sys::PSY.System
internal::ModelInternal
internal::IS.ModelInternal
store::EmulationModelStore # might be extended to other stores for simulation
ext::Dict{String, Any}

Expand All @@ -71,7 +71,7 @@ mutable struct EmulationModel{M <: EmulationProblem} <: OperationModel
name = Symbol(name)
end
finalize_template!(template, sys)
internal = ModelInternal(
internal = IS.ModelInternal(
OptimizationContainer(sys, settings, jump_model, PSY.SingleTimeSeries),
)
new{M}(name, template, sys, internal, EmulationModelStore(), Dict{String, Any}())
Expand Down
76 changes: 0 additions & 76 deletions src/operation/model_internal.jl

This file was deleted.

4 changes: 2 additions & 2 deletions src/operation/model_store_params.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct ModelStoreParams
resolution::Dates.Millisecond
base_power::Float64
system_uuid::Base.UUID
container_metadata::OptimizationContainerMetadata
container_metadata::IS.OptimizationContainerMetadata

function ModelStoreParams(
num_executions,
Expand All @@ -32,7 +32,7 @@ struct ModelStoreParams
resolution,
base_power,
system_uuid,
container_metadata = OptimizationContainerMetadata(),
container_metadata = IS.OptimizationContainerMetadata(),
)
new(
num_executions,
Expand Down
2 changes: 1 addition & 1 deletion src/operation/problem_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mutable struct ProblemResults <: IS.Results
parameter_values::Dict{IS.ParameterKey, DataFrames.DataFrame}
expression_values::Dict{IS.ExpressionKey, DataFrames.DataFrame}
optimizer_stats::DataFrames.DataFrame
optimization_container_metadata::OptimizationContainerMetadata
optimization_container_metadata::IS.OptimizationContainerMetadata
model_type::String
output_dir::String
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_simulation_results_export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PowerSimulations:
should_export_dual,
should_export_parameter,
should_export_variable,
OptimizationContainerMetadata
IS.OptimizationContainerMetadata

function _make_params()
sim = Dict(
Expand All @@ -32,7 +32,7 @@ function _make_params()
"system_uuid" => Base.UUID("4076af6c-e467-56ae-b986-b466b2749572"),
),
)
container_metadata = OptimizationContainerMetadata(
container_metadata = IS.OptimizationContainerMetadata(
Dict(
"ActivePowerVariable__ThermalStandard" =>
PSI.IS.VariableKey(ActivePowerVariable, ThermalStandard),
Expand Down

0 comments on commit 8f392bf

Please sign in to comment.