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 7 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
86 changes: 7 additions & 79 deletions src/multi_optimization_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -154,71 +158,11 @@ function _finalize_jump_model!(
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
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 @@ -242,29 +186,13 @@ function init_optimization_container!(

_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

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)

return
end

Expand Down
92 changes: 72 additions & 20 deletions src/multiproblem_template.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,50 @@
struct MultiProblemTemplate
struct MultiProblemTemplate <: PSI.AbstractProblemTemplate
base_template::PSI.ProblemTemplate
sub_templates::Dict{String, PSI.ProblemTemplate}
end

PSI.get_device_models(template::MultiProblemTemplate) = template.base_template.devices
PSI.get_branch_models(template::MultiProblemTemplate) = template.base_template.branches
PSI.get_service_models(template::MultiProblemTemplate) = template.base_template.services
PSI.get_network_model(template::MultiProblemTemplate) = template.base_template.network_model

function MultiProblemTemplate(
base_template::PSI.ProblemTemplate,
problem_keys::Vector{String},
)
sub_templates = Dict{String, PSI.ProblemTemplate}(k => deepcopy(base_template) for k in problem_keys)
sub_templates = Dict{String, PSI.ProblemTemplate}(
k => deepcopy(base_template) for k in problem_keys
)
return MultiProblemTemplate(base_template, sub_templates)
end

function MultiProblemTemplate(
network::PSI.NetworkModel{T},
problem_keys::Vector{String},) where {T <: PM.AbstractPowerModel}
problem_keys::Vector{String},
) where {T <: PM.AbstractPowerModel}
return MultiProblemTemplate(PSI.ProblemTemplate(network), problem_keys)
end

function Base.isempty(template::MultiProblemTemplate)
for template in values(template.sub_templates)
if !isempty(template.sub_templates)
return false
end
end
return isempty(template.base_template)
end

function PSI.get_network_formulation(template::MultiProblemTemplate)
bt = template.base_template
return PSI.get_network_formulation(PSI.get_network_model(bt))
end

function get_sub_templates(template::MultiProblemTemplate)
return values(template.sub_templates)
return template.sub_templates
end

function get_sub_problem_keys(template::MultiProblemTemplate)
return sort!(collect(keys(get_sub_templates(template))))
end

"""
Expand All @@ -29,7 +55,8 @@ function PSI.set_network_model!(
model::PSI.NetworkModel{<:PM.AbstractPowerModel},
)
PSI.set_network_model!(template.base_template, model)
for sub_template in get_sub_templates(template)
for (id, sub_template) in get_sub_templates(template)
PSI.set_subsystem!(model, id)
PSI.set_network_model!(sub_template, model)
end
return
Expand All @@ -44,9 +71,14 @@ function PSI.set_device_model!(
component_type::Type{<:PSY.Device},
formulation::Type{<:PSI.AbstractDeviceFormulation},
)
PSI.set_device_model!(template.base_template, PSI.DeviceModel(component_type, formulation))
for sub_template in get_sub_templates(template)
PSI.set_device_model!(sub_template, PSI.DeviceModel(component_type, formulation))
PSI.set_device_model!(
template.base_template,
PSI.DeviceModel(component_type, formulation),
)
for (id, sub_template) in get_sub_templates(template)
network_model = PSI.DeviceModel(component_type, formulation)
PSI.set_subsystem!(network_model, id)
PSI.set_device_model!(sub_template, network_model)
end
return
end
Expand All @@ -59,7 +91,8 @@ function PSI.set_device_model!(
model::PSI.DeviceModel{<:PSY.Device, <:PSI.AbstractDeviceFormulation},
)
PSI.set_device_model!(template.base_template, model)
for sub_template in get_sub_templates(template)
for (id, sub_template) in get_sub_templates(template)
PSI.set_subsystem!(model, id)
PSI.set_device_model!(sub_template, model)
end
return
Expand All @@ -70,7 +103,8 @@ function PSI.set_device_model!(
model::PSI.DeviceModel{<:PSY.Branch, <:PSI.AbstractDeviceFormulation},
)
PSI.set_device_model!(template.base_template, model)
for sub_template in get_sub_templates(template)
for (id, sub_template) in get_sub_templates(template)
PSI.set_subsystem!(model, id)
PSI.set_device_model!(sub_template, PSI.DeviceModel(component_type, formulation))
end
return
Expand All @@ -89,13 +123,15 @@ function PSI.set_service_model!(
PSI.set_service_model!(
template.base_template,
service_name,
ServiceModel(service_type, formulation; use_service_name = true),
ServiceModel(service_type, formulation; use_service_name=true),
)
for sub_template in get_sub_templates(template)
for (id, sub_template) in get_sub_templates(template)
service_model = ServiceModel(service_type, formulation; use_service_name=true)
PSI.set_subsystem!(service_model, id)
PSI.set_service_model!(
sub_template,
service_name,
ServiceModel(service_type, formulation; use_service_name = true),
service_model,
)
end
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
return
Expand All @@ -109,12 +145,17 @@ function PSI.set_service_model!(
service_type::Type{<:PSY.Service},
formulation::Type{<:PSI.AbstractServiceFormulation},
)
PSI.set_service_model!(template.base_template, PSI.ServiceModel(service_type, formulation))
for sub_template in get_sub_templates(template)
PSI.set_service_model!(
template.base_template,
PSI.ServiceModel(service_type, formulation),
)
for (id, sub_template) in get_sub_templates(template)
service_model = ServiceModel(service_type, formulation)
PSI.set_subsystem!(service_model, id)
PSI.set_service_model!(
sub_template,
service_name,
PSI.ServiceModel(service_type, formulation),
service_model,
)
end
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
return
Expand All @@ -126,8 +167,9 @@ function PSI.set_service_model!(
model::PSI.ServiceModel{<:PSY.Service, <:PSI.AbstractServiceFormulation},
)
PSI.set_service_model!(template.base_template, service_name, model)
for sub_template in get_sub_templates(template)
PSI.set_service_model!(sub_template, service_name, model)
for (id, sub_template) in get_sub_templates(template)
PSI.set_subsystem!(model, id)
PSI.set_service_model!(sub_template, service_name, deepcopy(model))
end
return
end
Expand All @@ -137,8 +179,18 @@ function PSI.set_service_model!(
model::PSI.ServiceModel{<:PSY.Service, <:PSI.AbstractServiceFormulation},
)
PSI.set_service_model!(template.base_template, model)
for sub_template in get_sub_templates(template)
PSI.set_service_model!(sub_template, model)
for (id, sub_template) in get_sub_templates(template)
PSI.set_subsystem!(model, id)
PSI.set_service_model!(sub_template, deepcopy(model))
end
return
end

function finalize_template!(template::MultiProblemTemplate, sys::PSY.System)
PSI.finalize_template!(template.base_template, sys)
for (ix, sub_template) in get_sub_templates(template)
@debug "Finalizing template for sub probem $ix"
PSI.finalize_template!(sub_template, sys)
end
return
end
7 changes: 7 additions & 0 deletions src/print.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function Base.show(io::IO, ::MIME"text/plain", input::MultiProblemTemplate)
println(io, "Print somenthing clever here. Template")
end

function Base.show(io::IO, ::MIME"text/plain", input::PSI.DecisionModel{MultiRegionProblem})
println(io, "Print somenthing clever here. Problem")
end
Loading
Loading