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

Jd/subsystems implementation #1

Merged
merged 18 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 1 addition & 1 deletion .github/workflows/format-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Format Check
on:
push:
branches:
- 'master'
- 'main'
- 'release-'
tags: '*'
pull_request:
Expand Down
3 changes: 2 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ CurrentModule = PowerSimulationsDecomposition

`PowerSimulationsDecomposition .jl` is a [`Julia`](http://www.julialang.org) package that provides blah blah

------------
* * *

PowerSimulationsDecomposition has been developed as part of the Scalable Integrated Infrastructure Planning
(SIIP) initiative at the U.S. Department of Energy's National Renewable Energy
Laboratory ([NREL](https://www.nrel.gov/)).
1 change: 1 addition & 0 deletions src/PowerSimulationsDecomposition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ include("multi_optimization_container.jl")
include("algorithms/sequential_algorithm.jl")
include("algorithms/mpi_parallel_algorithm.jl")
include("problems/multi_region_problem.jl")
include("print.jl")

end
11 changes: 4 additions & 7 deletions src/algorithms/sequential_algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ function build_impl!(
template::MultiProblemTemplate,
sys::PSY.System,
)
sub_templates = get_subtemplates(template)
for (index, sub_problem) in container.subproblems
@debug "Building Subproblem $index" _group = PSI.LOG_GROUP_OPTIMIZATION_CONTAINER
_system_modification!(sys, index)
PSI.build_impl!(sub_problem, sub_templates[index], sys)
for (index, sub_template) in get_sub_templates(template)
@info "Building Subproblem $index" _group = PSI.LOG_GROUP_OPTIMIZATION_CONTAINER
PSI.build_impl!(get_subproblem(container, index), sub_template, sys)
end

build_main_problem!(container, template, sys)
Expand All @@ -23,8 +21,7 @@ function build_main_problem!(
sys::PSY.System,
) end

function write_results_to_main_container(container::MultiOptimizationContainer)
end
function write_results_to_main_container(container::MultiOptimizationContainer) end

function solve_impl!(
container::MultiOptimizationContainer{SequentialAlgorithm},
Expand Down
2 changes: 1 addition & 1 deletion src/multi_decision_model.jl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# ! needs the definition of decision problem that can get type MultiProblemTemplate
# ! needs the definition of decision problem that can get type MultiProblemTemplate
128 changes: 15 additions & 113 deletions src/multi_optimization_container.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mutable struct MultiOptimizationContainer{T <: DecompositionAlgorithm} <:
PSI.AbstractModelContainer
main_JuMPmodel::JuMP.Model
main_problem::PSI.OptimizationContainer
subproblems::Dict{String, PSI.OptimizationContainer}
time_steps::UnitRange{Int}
resolution::Dates.TimePeriod
Expand Down Expand Up @@ -45,7 +45,7 @@ function MultiOptimizationContainer(
end

return MultiOptimizationContainer{T}(
JuMP.Model(),
PSI.OptimizationContainer(sys, settings, nothing, U),
subproblems,
1:1,
IS.time_period_conversion(resolution),
Expand Down Expand Up @@ -88,7 +88,7 @@ PSI.get_initial_conditions_data(container::MultiOptimizationContainer) =
container.initial_conditions_data
PSI.get_initial_time(container::MultiOptimizationContainer) =
PSI.get_initial_time(container.settings)
PSI.get_jump_model(container::MultiOptimizationContainer) = container.main_JuMPmodel
PSI.get_jump_model(container::MultiOptimizationContainer) = PSI.get_jump_model(container.main_problem)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
PSI.get_jump_model(container::MultiOptimizationContainer) = PSI.get_jump_model(container.main_problem)
PSI.get_jump_model(container::MultiOptimizationContainer) =
PSI.get_jump_model(container.main_problem)

PSI.get_metadata(container::MultiOptimizationContainer) = container.metadata
PSI.get_optimizer_stats(container::MultiOptimizationContainer) = container.optimizer_stats
PSI.get_parameters(container::MultiOptimizationContainer) = container.parameters
Expand All @@ -110,6 +110,10 @@ PSI.get_aux_variables(container::MultiOptimizationContainer) = container.aux_var
PSI.get_base_power(container::MultiOptimizationContainer) = container.base_power
PSI.get_constraints(container::MultiOptimizationContainer) = container.constraints

function get_subproblem(container::MultiOptimizationContainer, id::String)
return container.subproblems[id]
end

function check_optimization_container(container::MultiOptimizationContainer)
# Solve main problem
for (index, sub_problem) in container.subproblems
Expand All @@ -122,103 +126,15 @@ function _finalize_jump_model!(
settings::PSI.Settings,
)
@debug "Instantiating the JuMP model" _group = PSI.LOG_GROUP_OPTIMIZATION_CONTAINER
#=
if PSI.built_for_recurrent_solves(container) && PSI.get_optimizer(settings) === nothing
throw(
IS.ConflictingInputsError(
"Optimizer can not be nothing when building for recurrent solves",
),
)
end
=#

if PSI.get_direct_mode_optimizer(settings)
optimizer = () -> MOI.instantiate(PSI.get_optimizer(settings))
container.main_JuMPmodel = JuMP.direct_model(optimizer())
elseif PSI.get_optimizer(settings) === nothing
@debug "The optimization model has no optimizer attached" _group =
LOG_GROUP_OPTIMIZATION_CONTAINER
else
JuMP.set_optimizer(PSI.get_jump_model(container), PSI.get_optimizer(settings))
end

JuMPmodel = PSI.get_jump_model(container)

if PSI.get_optimizer_solve_log_print(settings)
JuMP.unset_silent(JuMPmodel)
@debug "optimizer unset to silent" _group = PSI.LOG_GROUP_OPTIMIZATION_CONTAINER
else
JuMP.set_silent(JuMPmodel)
@debug "optimizer set to silent" _group = PSI.LOG_GROUP_OPTIMIZATION_CONTAINER
end
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_
#@error "Changed $(summary(component)) to ISOLATED"
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_
#@error "Changed $(summary(component)) to True"
PSY.set_available!(component, true)
else
#@error "Changed $(summary(component)) to False"
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
total_number_of_gens =
length(PSY.get_components(x -> PSY.get_available(x), PSY.ThermalStandard, sys))
total_number_of_ac_buses = length(
PSY.get_components(
x -> PSY.get_bustype(x) != PSY.ACBusTypes.ISOLATED,
PSY.ACBus,
sys,
),
)
@show "Inside Mod Function Components using PSY functions"
@show total_number_of_gens
@show total_number_of_ac_buses
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
PSI._finalize_jump_model!(container.main_problem, settings)
return
end

function init_optimization_container!(
container::MultiOptimizationContainer,
::Type{T},
network_model::PSI.NetworkModel{<:PM.AbstractPowerModel},
sys::PSY.System,
) where {T <: PM.AbstractPowerModel}
)
PSY.set_units_base_system!(sys, "SYSTEM_BASE")
# The order of operations matter
settings = PSI.get_settings(container)
Expand All @@ -240,31 +156,17 @@ function init_optimization_container!(
stats = PSI.get_optimizer_stats(container)
stats.detailed_stats = PSI.get_detailed_optimizer_stats(settings)

_finalize_jump_model!(container, settings)

total_number_of_gens = length(PSI.get_available_components(PSY.ThermalStandard, sys))
total_number_of_ac_buses = length(PSI.get_available_components(PSY.ACBus, sys))
@show "Total Components"
@show total_number_of_gens
@show total_number_of_ac_buses
# need a special method for the main problem to initialize the optimization container
# without actually caring about the subnetworks
# PSI.init_optimization_container!(sub_problem, network_model, sys)

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_gens =
length(PSI.get_available_components(PSY.ThermalStandard, sys))
total_number_of_ac_buses = length(PSI.get_available_components(PSY.ACBus, sys))
@show "Before Init number of components using PSI functions"
@show total_number_of_gens
@show total_number_of_ac_buses
PSI.init_optimization_container!(sub_problem, T, sys)
PSI.init_optimization_container!(sub_problem, network_model, sys)
end

# restore original system
_restore_system!(sys)

_finalize_jump_model!(container, settings)
return
end

Expand Down
Loading
Loading