From 4e0717b634b918c7c82a1b4e6af21ec3f8391fb4 Mon Sep 17 00:00:00 2001 From: alefcastelli Date: Wed, 24 Jan 2024 10:00:01 -0700 Subject: [PATCH] afc/some changes to make it build --- Project.toml | 1 - src/algorithms/sequential_algorithm.jl | 19 +-------- src/multi_optimization_container.jl | 57 ++++++++++++++++++++++++++ src/problems/multi_region_problem.jl | 29 ++++++++++++- test/Project.toml | 12 +++--- 5 files changed, 90 insertions(+), 28 deletions(-) diff --git a/Project.toml b/Project.toml index 98fd359..ae41079 100644 --- a/Project.toml +++ b/Project.toml @@ -20,6 +20,5 @@ InfrastructureSystems = "^1.21" JuMP = "1" MathOptInterface = "1" MPI = "^0.20" -PowerSimulations = "^0.25" PowerSystems = "^3" julia = "^1.6" diff --git a/src/algorithms/sequential_algorithm.jl b/src/algorithms/sequential_algorithm.jl index 981b63d..291a8d3 100644 --- a/src/algorithms/sequential_algorithm.jl +++ b/src/algorithms/sequential_algorithm.jl @@ -1,19 +1,3 @@ -function system_modification(sys::PSY.System, index) - for component in get_components(Component, sys) - ext = get_ext(component) - if !haskey(ext, "subsystems") - continue - else - # needs to be careful with he buses becasue buses don't have "available" - if index in ext["subsystems"] - set_available!(component, false) - else - set_available!(component, true) - end - end - return -end - function build_impl!( container::MultiOptimizationContainer{SequentialAlgorithm}, template::PSI.ProblemTemplate, @@ -22,8 +6,7 @@ function build_impl!( for (index, sub_problem) in container.subproblems @debug "Building Subproblem $index" _group = PSI.LOG_GROUP_OPTIMIZATION_CONTAINER - # Temporary - system_modification!(sys, index) + _system_modification!(sys, index) PSI.build_impl!(sub_problem, template, sys) end diff --git a/src/multi_optimization_container.jl b/src/multi_optimization_container.jl index f394633..a905e56 100644 --- a/src/multi_optimization_container.jl +++ b/src/multi_optimization_container.jl @@ -37,6 +37,7 @@ function MultiOptimizationContainer( error("Default Time Series Type $U can't be abstract") end + # define dictionary containing the optimization container for the subregion subproblems = Dict{String, PSI.OptimizationContainer}() for k in sub_problem_keys subproblems[k] = PSI.OptimizationContainer(sys, settings, nothing, U) @@ -147,6 +148,51 @@ function _finalize_jump_model!(container::MultiOptimizationContainer, settings:: return end +function _system_modification!(sys::PSY.System, index) + for component in PSY.get_components(PSY.Component, sys) + if typeof(component) <: PSY.Bus || :available in fieldnames(typeof(component)) + sb_ = PSY.get_ext(component)["subregion"] + if typeof(component) <: PSY.Bus + if "original_type" ∉ keys(PSY.get_ext(component)) + component.ext["original_type"] = PSY.get_bustype(component) + end + if PSY.get_bustype(component) == PSY.ACBusTypes.ISOLATED + if component.ext["original_type"] != PSY.ACBusTypes.ISOLATED + PSY.set_bustype!(component, component.ext["original_type"]) + end + end + if index ∉ sb_ + PSY.set_bustype!(component, PSY.ACBusTypes.ISOLATED) + end + else + if "original_type" ∉ keys(PSY.get_ext(component)) + component.ext["original_available"] = PSY.get_available(component) + end + if index ∈ sb_ + PSY.set_available!(component, true) + else + PSY.set_available!(component, false) + end + if typeof(component) <: PSY.Reserve + @show (index, PSY.get_name(component), PSY.get_available(component)) + end + end + end + end + return +end + +function _restore_system!(sys::PSY.System) + for component in PSY.get_components(PSY.Component, sys) + if typeof(component) <: PSY.Bus + PSY.set_bustype!(component, PSY.get_ext(component)["original_type"]) + elseif :available in fieldnames(typeof(component)) + PSY.set_available!(component, PSY.get_ext(component)["original_available"]) + end + end + return +end + function init_optimization_container!( container::MultiOptimizationContainer, ::Type{T}, @@ -175,12 +221,23 @@ function init_optimization_container!( _finalize_jump_model!(container, settings) + total_number_of_devices = length(PSI.get_available_components(PSY.Device, sys)) + total_number_of_devices += length(PSI.get_available_components(PSY.ACBranch, sys)) + @show total_number_of_devices + for (index, sub_problem) in container.subproblems @debug "Initializing Container Subproblem $index" _group = PSI.LOG_GROUP_OPTIMIZATION_CONTAINER sub_problem.settings = deepcopy(settings) + _system_modification!(sys, index) + total_number_of_devices = length(PSI.get_available_components(PSY.Device, sys)) + total_number_of_devices += length(PSI.get_available_components(PSY.ACBranch, sys)) + @show total_number_of_devices PSI.init_optimization_container!(sub_problem, T, sys) end + # restore original system + _restore_system!(sys) + return end diff --git a/src/problems/multi_region_problem.jl b/src/problems/multi_region_problem.jl index 3a7fb4c..805c926 100644 --- a/src/problems/multi_region_problem.jl +++ b/src/problems/multi_region_problem.jl @@ -1,5 +1,24 @@ struct MultiRegionProblem <: PSI.DecisionProblem end +function get_n_subregions(sys::PSY.System) + # TODO: this function can be improved (less for loops) + subregions_ = String[] + for d in PSY.get_components(PSY.Component, sys) + if typeof(d) ∉ [PSY.Area, PSY.Arc, PSY.LoadZone] + if isempty(PSY.get_ext(d)) + error("Field `ext` must be non empty if spatial decomposition is used.") + else + for sb in PSY.get_ext(d)["subregion"] + if sb ∉ subregions_ + push!(subregions_, sb) + end + end + end + end + end + return subregions_ +end + function PSI.DecisionModel{MultiRegionProblem}( template::PSI.ProblemTemplate, sys::PSY.System, @@ -12,13 +31,19 @@ function PSI.DecisionModel{MultiRegionProblem}( elseif name isa String name = Symbol(name) end - # Get these from the system later with subsystems - region_keys = ["1", "2,"] + + # get number of system subregions + # NOTE: `ext` field for each component must be filled with a "subregion" key in the dictionary + region_keys = get_n_subregions(sys) + + # define the optimization container with master and subproblems internal = PSI.ModelInternal( MultiOptimizationContainer(SequentialAlgorithm, sys, settings, PSY.Deterministic, region_keys), ) template_ = deepcopy(template) PSI.finalize_template!(template_, sys) + + # return multi-region decision model container return PSI.DecisionModel{MultiRegionProblem}( name, template_, diff --git a/test/Project.toml b/test/Project.toml index f8dbb60..d0f103b 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,12 +1,10 @@ [deps] -Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" -JuMP = "4076af6c-e467-56ae-b986-b466b2749572" -Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" +HydroPowerSimulations = "fc1677e0-6ad7-4515-bf3a-bd6bf20a0b1b" +PowerNetworkMatrices = "bed98974-b02a-5e2f-9fe0-a103f5c450dd" PowerSimulations = "e690365d-45e2-57bb-ac84-44ba829e73c4" PowerSimulationsDecomposition = "bed98974-b02a-5e2f-9ee0-a103f5c450dd" PowerSystemCaseBuilder = "f00506e0-b84f-492a-93c2-c0a9afc4364e" PowerSystems = "bcd98974-b02a-5e2f-9ee0-a103f5c450dd" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[compat] -julia = "^1.6" +Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" +StorageSystemsSimulations = "e2f1a126-19d0-4674-9252-42b2384f8e3c" +Xpress = "9e70acf3-d6c9-5be6-b5bd-4e2c73e3e054"