Skip to content

Commit

Permalink
REF: generate boundary linking vars functions for all currently suppo…
Browse files Browse the repository at this point in the history
…rted decomposition formulations.
  • Loading branch information
juanjospina committed Jul 31, 2024
1 parent a87ff31 commit a881c18
Show file tree
Hide file tree
Showing 10 changed files with 521 additions and 130 deletions.
4 changes: 3 additions & 1 deletion src/core/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,9 @@ function solve_model(
# Add boundary info. to PMITD dict.
for (b_name, b_data) in result["solution"]["it"][_PMD.pmd_it_name][ckt_name]["boundary"]
result["solution"]["it"][pmitd_it_name]["boundary"][b_name]["pbound_aux"] = b_data["pbound_aux"]
result["solution"]["it"][pmitd_it_name]["boundary"][b_name]["qbound_aux"] = b_data["qbound_aux"]
if haskey(b_data, "qbound_aux")
result["solution"]["it"][pmitd_it_name]["boundary"][b_name]["qbound_aux"] = b_data["qbound_aux"]
end
end

end
Expand Down
36 changes: 25 additions & 11 deletions src/form_decomposition/acp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,10 @@ ACPU boundary bus voltage angle constraints.
"""
function constraint_boundary_voltage_angle(pmd::_PMD.ACPUPowerModel, ::Int, t_bus::Int, ::Vector{Int}, ::Vector{Int}; nw::Int=nw_id_default)

## --- NOTE: These constraints seem to make ACP-ACPU decomposition formulation harder to solve
## if the _PMD.constraint_mc_theta_ref(pmd_model, i) is kept ---

# # --- Either this constraint ---
# _PMD.constraint_mc_theta_ref(pmd, t_bus)

# --- Or these constraints ---.

va_source = _PMD.var(pmd, nw, :va, t_bus)
# Add constraint(s): angles
va_source = _PMD.var(pmd, nw, :va, t_bus)
# JuMP.@constraint(pmd.model, va_source[1] == 0.0)

# Add constraints related to 120 degrees offset for the distribution b and c phases
shift_120degs_rad = deg2rad(120)
# Offset constraints for other phases (-+120 degrees)
Expand Down Expand Up @@ -204,8 +197,8 @@ end
export_models::Bool=false
)
Generates the ACP-ACPU boundary linking vars vector to be used by the IDEC Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as `.nl` files.
Generates the ACP-ACPU boundary linking vars vector to be used by the StsDOpt Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as .txt files.
"""
function generate_boundary_linking_vars(pm::_PM.ACPPowerModel, pmd::_PMD.ACPUPowerModel, boundary_number::String; nw::Int=nw_id_default, export_models::Bool=false)

Expand All @@ -219,6 +212,17 @@ function generate_boundary_linking_vars(pm::_PM.ACPPowerModel, pmd::_PMD.ACPUPow
end


"""
function generate_boundary_linking_vars_transmission(
pm::_PM.ACPPowerModel,
boundary_number::String;
nw::Int = nw_id_default,
export_models::Bool=false
)
Generates the ACP boundary linking vars vector to be used by the StsDOpt Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as .txt files.
"""
function generate_boundary_linking_vars_transmission(pm::_PM.ACPPowerModel, boundary_number::String; nw::Int=nw_id_default, export_models::Bool=false)

# Parse to Int
Expand Down Expand Up @@ -261,7 +265,17 @@ function generate_boundary_linking_vars_transmission(pm::_PM.ACPPowerModel, boun
end


"""
function generate_boundary_linking_vars_distribution(
pmd::_PMD.ACPUPowerModel,
boundary_number::String;
nw::Int = nw_id_default,
export_models::Bool=false
)
Generates the ACPU boundary linking vars vector to be used by the StsDOpt Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as .txt files.
"""
function generate_boundary_linking_vars_distribution(pmd::_PMD.ACPUPowerModel, boundary_number::String; nw::Int=nw_id_default, export_models::Bool=false)

# Parse to Int
Expand Down
97 changes: 82 additions & 15 deletions src/form_decomposition/acr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ ACRU boundary bus voltage angle constraints.
"""
function constraint_boundary_voltage_angle(pmd::_PMD.ACRUPowerModel, ::Int, t_bus::Int, ::Vector{Int}, ::Vector{Int}; nw::Int=nw_id_default)

## --- NOTE: These constraints seem to make ACR-ACRU decomposition formulation harder to solve! ---

vr_source = _PMD.var(pmd, nw, :vr, t_bus)
vi_source = _PMD.var(pmd, nw, :vi, t_bus)

Expand Down Expand Up @@ -167,28 +165,44 @@ end
)
Generates the ACR-ACRU boundary linking vars vector to be used by the IDEC Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as `.nl` files.
Generates the ACR-ACRU boundary linking vars vector to be used by the StsDOpt Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as .txt files.
"""
function generate_boundary_linking_vars(pm::_PM.ACRPowerModel, pmd::_PMD.ACRUPowerModel, boundary_number::String; nw::Int=nw_id_default, export_models::Bool=false)

transmission_linking_vars = generate_boundary_linking_vars_transmission(pm, boundary_number; nw=nw, export_models=export_models)
distribution_linking_vars = generate_boundary_linking_vars_distribution(pmd, boundary_number; nw=nw, export_models=export_models)

boundary_linking_vars = [transmission_linking_vars[1], distribution_linking_vars[1]] # use 1 to extract the vector of linking vars - TODO: see if [1] can be removed maintaining compat.

return boundary_linking_vars

end


"""
function generate_boundary_linking_vars_transmission(
pm::_PM.ACRPowerModel,
boundary_number::String;
nw::Int = nw_id_default,
export_models::Bool=false
)
Generates the ACR boundary linking vars vector to be used by the StsDOpt Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as .txt files.
"""
function generate_boundary_linking_vars_transmission(pm::_PM.ACRPowerModel, boundary_number::String; nw::Int=nw_id_default, export_models::Bool=false)


# Parse to Int
boundary_number = parse(Int64, boundary_number)

# Get boundary info.
boundary = _PMD.ref(pmd, nw, :boundary, boundary_number)
boundary = _PM.ref(pm, nw, :boundary, boundary_number)

f_bus = boundary["f_bus"] # convention: from bus Transmission always!
t_bus = boundary["t_bus"] # convention: to bus Distribution always!

# Distribution: Aux vars (subproblem)
f_idx = (boundary_number, f_bus, t_bus)
p_aux = _PMD.var(pmd, nw, :pbound_aux, f_idx)
q_aux = _PMD.var(pmd, nw, :qbound_aux, f_idx)

# Distribution: vr and vi (subproblem)
vr = _PMD.var(pmd, nw, :vr, t_bus)
vi = _PMD.var(pmd, nw, :vi, t_bus)

# Transmission: Vr and Vi (master)
Vr = _PM.var(pm, nw, :vr, f_bus)
Expand All @@ -198,11 +212,64 @@ function generate_boundary_linking_vars(pm::_PM.ACRPowerModel, pmd::_PMD.ACRUPow
P_load = _PM.var(pm, nw, :pbound_load_scaled, f_idx)
Q_load = _PM.var(pm, nw, :qbound_load_scaled, f_idx)

boundary_linking_vars = [[P_load[1], Q_load[1], Vr, Vi], [p_aux[1], q_aux[1], vr[1], vi[1]]]
boundary_linking_vars = [[P_load[1], Q_load[1], Vr, Vi]]

if (export_models == true)
# Open file where shared vars indices are going to be written
file = open("shared_vars_transmission.txt", "a")
# Loop through the vector of shared variables
for sh_vect in boundary_linking_vars
for sh_var in sh_vect
str_to_write = "Shared Variable ($(sh_var)) Index: $(sh_var.index)\n"
# Write the string to the file
write(file, str_to_write)
end
end
# Close the file
close(file)
end

return boundary_linking_vars

end


"""
function generate_boundary_linking_vars_distribution(
pmd::_PMD.ACRUPowerModel,
boundary_number::String;
nw::Int = nw_id_default,
export_models::Bool=false
)
Generates the ACRU boundary linking vars vector to be used by the StsDOpt Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as .txt files.
"""
function generate_boundary_linking_vars_distribution(pmd::_PMD.ACRUPowerModel, boundary_number::String; nw::Int=nw_id_default, export_models::Bool=false)

# Parse to Int
boundary_number = parse(Int64, boundary_number)

# Get boundary info.
boundary = _PMD.ref(pmd, nw, :boundary, boundary_number)

f_bus = boundary["f_bus"] # convention: from bus Transmission always!
t_bus = boundary["t_bus"] # convention: to bus Distribution always!
f_idx = (boundary_number, f_bus, t_bus)

# Distribution: Aux vars (subproblem)
p_aux = _PMD.var(pmd, nw, :pbound_aux, f_idx)
q_aux = _PMD.var(pmd, nw, :qbound_aux, f_idx)

# Distribution: vr and vi (subproblem)
vr = _PMD.var(pmd, nw, :vr, t_bus)
vi = _PMD.var(pmd, nw, :vi, t_bus)

boundary_linking_vars = [[p_aux[1], q_aux[1], vr[1], vi[1]]]

if (export_models == true)
# Open file where shared vars indices are going to be written
file = open("shared_vars.txt", "a")
file = open("shared_vars_distribution_$(boundary_number).txt", "a")
# Loop through the vector of shared variables
for sh_vect in boundary_linking_vars
for sh_var in sh_vect
Expand Down
41 changes: 28 additions & 13 deletions src/form_decomposition/fbs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,34 @@ end
)
Generates the ACR-FBSUBF boundary linking vars vector to be used by the IDEC Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as `.nl` files.
Generates the ACR-FBSUBF boundary linking vars vector to be used by the StsDOpt Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as .txt files.
"""
function generate_boundary_linking_vars(pm::_PM.ACRPowerModel, pmd::_PMD.FBSUBFPowerModel, boundary_number::String; nw::Int=nw_id_default, export_models::Bool=false)

transmission_linking_vars = generate_boundary_linking_vars_transmission(pm, boundary_number; nw=nw, export_models=export_models)
distribution_linking_vars = generate_boundary_linking_vars_distribution(pmd, boundary_number; nw=nw, export_models=export_models)

boundary_linking_vars = [transmission_linking_vars[1], distribution_linking_vars[1]] # use 1 to extract the vector of linking vars - TODO: see if [1] can be removed maintaining compat.

return boundary_linking_vars

end


"""
function generate_boundary_linking_vars_distribution(
pmd::_PMD.FBSUBFPowerModel,
boundary_number::String;
nw::Int = nw_id_default,
export_models::Bool=false
)
Generates the FBSUBFPowerModel boundary linking vars vector to be used by the StsDOpt Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as .txt files.
"""
function generate_boundary_linking_vars_distribution(pmd::_PMD.FBSUBFPowerModel, boundary_number::String; nw::Int=nw_id_default, export_models::Bool=false)

# Parse to Int
boundary_number = parse(Int64, boundary_number)

Expand All @@ -74,29 +97,21 @@ function generate_boundary_linking_vars(pm::_PM.ACRPowerModel, pmd::_PMD.FBSUBFP

f_bus = boundary["f_bus"] # convention: from bus Transmission always!
t_bus = boundary["t_bus"] # convention: to bus Distribution always!
f_idx = (boundary_number, f_bus, t_bus)

# Distribution: Aux vars (subproblem)
f_idx = (boundary_number, f_bus, t_bus)
p_aux = _PMD.var(pmd, nw, :pbound_aux, f_idx)
q_aux = _PMD.var(pmd, nw, :qbound_aux, f_idx)

# Distribution: vr and vi (subproblem)
vr = _PMD.var(pmd, nw, :vr, t_bus)
vi = _PMD.var(pmd, nw, :vi, t_bus)

# Transmission: Vr and Vi (master)
Vr = _PM.var(pm, nw, :vr, f_bus)
Vi = _PM.var(pm, nw, :vi, f_bus)

# Transmission: Pload & Qload (master)
P_load = _PM.var(pm, nw, :pbound_load_scaled, f_idx)
Q_load = _PM.var(pm, nw, :qbound_load_scaled, f_idx)

boundary_linking_vars = [[P_load[1], Q_load[1], Vr, Vi], [p_aux[1], q_aux[1], vr[1], vi[1]]]
boundary_linking_vars = [[p_aux[1], q_aux[1], vr[1], vi[1]]]

if (export_models == true)
# Open file where shared vars indices are going to be written
file = open("shared_vars.txt", "a")
file = open("shared_vars_distribution_$(boundary_number).txt", "a")
# Loop through the vector of shared variables
for sh_vect in boundary_linking_vars
for sh_var in sh_vect
Expand Down
58 changes: 32 additions & 26 deletions src/form_decomposition/fotp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,10 @@ FOTPU boundary bus voltage angle constraints.
"""
function constraint_boundary_voltage_angle(pmd::_PMD.FOTPUPowerModel, ::Int, t_bus::Int, ::Vector{Int}, ::Vector{Int}; nw::Int=nw_id_default)

## --- NOTE: These constraints seem to make ACP-FOTPU decomposition formulation harder to solve
## if the _PMD.constraint_mc_theta_ref(pmd_model, i) is kept ---

# --- Either this constraint ---
# _PMD.constraint_mc_theta_ref(pmd, t_bus)

# --- Or these constraints ---.

va_source = _PMD.var(pmd, nw, :va, t_bus)
# Add constraint(s): angles
va_source = _PMD.var(pmd, nw, :va, t_bus)
# JuMP.@constraint(pmd.model, va_source[1] == 0.0)

# Add constraints related to 120 degrees offset for the distribution b and c phases
shift_120degs_rad = deg2rad(120)
# Offset constraints for other phases (-+120 degrees)
Expand All @@ -57,22 +50,44 @@ function constraint_boundary_voltage_angle(pmd::_PMD.FOTPUPowerModel, ::Int, t_b
end



# TODO: multinetwork compatibility by using nw info.
"""
function generate_boundary_linking_vars(
pm::_PM.ACPPowerModel,
pmd::_PMD.FOTPUPowerModel,
boundary_number::String;
nw::Int = nw_id_default
nw::Int = nw_id_default,
export_models::Bool=false
)
Generates the ACP-FOTPU boundary linking vars vector to be used by the IDEC Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as `.nl` files.
Generates the ACP-FOTPU boundary linking vars vector to be used by the StsDOpt Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as .txt files.
"""
function generate_boundary_linking_vars(pm::_PM.ACPPowerModel, pmd::_PMD.FOTPUPowerModel, boundary_number::String; nw::Int=nw_id_default, export_models::Bool=false)

transmission_linking_vars = generate_boundary_linking_vars_transmission(pm, boundary_number; nw=nw, export_models=export_models)
distribution_linking_vars = generate_boundary_linking_vars_distribution(pmd, boundary_number; nw=nw, export_models=export_models)

boundary_linking_vars = [transmission_linking_vars[1], distribution_linking_vars[1]] # use 1 to extract the vector of linking vars - TODO: see if [1] can be removed maintaining compat.

return boundary_linking_vars

end


"""
function generate_boundary_linking_vars_distribution(
pmd::_PMD.FOTPUPowerModel,
boundary_number::String;
nw::Int = nw_id_default,
export_models::Bool=false
)
Generates the FOTPU boundary linking vars vector to be used by the StsDOpt Optimizer.
The parameter `export_models` is a boolean that determines if the JuMP models' shared variable indices are exported to the pwd as .txt files.
"""
function generate_boundary_linking_vars_distribution(pmd::_PMD.FOTPUPowerModel, boundary_number::String; nw::Int=nw_id_default, export_models::Bool=false)

# Parse to Int
boundary_number = parse(Int64, boundary_number)

Expand All @@ -81,30 +96,21 @@ function generate_boundary_linking_vars(pm::_PM.ACPPowerModel, pmd::_PMD.FOTPUPo

f_bus = boundary["f_bus"] # convention: from bus Transmission always!
t_bus = boundary["t_bus"] # convention: to bus Distribution always!
f_idx = (boundary_number, f_bus, t_bus)

# Distribution: Aux vars (subproblem)
f_idx = (boundary_number, f_bus, t_bus)
p_aux = _PMD.var(pmd, nw, :pbound_aux, f_idx)
q_aux = _PMD.var(pmd, nw, :qbound_aux, f_idx)

# Distribution: vm (subproblem)
vm = _PMD.var(pmd, nw, :vm, t_bus)
va = _PMD.var(pmd, nw, :va, t_bus)

# Transmission: Vm (master)
Vm = _PM.var(pm, nw, :vm, f_bus)
Va = _PM.var(pm, nw, :va, f_bus)

# Transmission: Pload & Qload (master)
P_load = _PM.var(pm, nw, :pbound_load_scaled, f_idx)
Q_load = _PM.var(pm, nw, :qbound_load_scaled, f_idx)

# boundary_linking_vars = [[P_load[1], Q_load[1], Vm], [p_aux[1], q_aux[1], vm[1]]]
boundary_linking_vars = [[P_load[1], Q_load[1], Vm, Va], [p_aux[1], q_aux[1], vm[1], va[1]]]
boundary_linking_vars = [[p_aux[1], q_aux[1], vm[1], va[1]]]

if (export_models == true)
# Open file where shared vars indices are going to be written
file = open("shared_vars.txt", "a")
file = open("shared_vars_distribution_$(boundary_number).txt", "a")
# Loop through the vector of shared variables
for sh_vect in boundary_linking_vars
for sh_var in sh_vect
Expand Down
Loading

0 comments on commit a881c18

Please sign in to comment.