Skip to content

Commit

Permalink
afc/some changes to make it build
Browse files Browse the repository at this point in the history
  • Loading branch information
alefcastelli committed Jan 24, 2024
1 parent 5fa13ea commit 4e0717b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 28 deletions.
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ InfrastructureSystems = "^1.21"
JuMP = "1"
MathOptInterface = "1"
MPI = "^0.20"
PowerSimulations = "^0.25"
PowerSystems = "^3"
julia = "^1.6"
19 changes: 1 addition & 18 deletions src/algorithms/sequential_algorithm.jl
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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

Expand Down
57 changes: 57 additions & 0 deletions src/multi_optimization_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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

Expand Down
29 changes: 27 additions & 2 deletions src/problems/multi_region_problem.jl
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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_,
Expand Down
12 changes: 5 additions & 7 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 4e0717b

Please sign in to comment.