diff --git a/src/core/base.jl b/src/core/base.jl index fa8dad8..6a1e855 100755 --- a/src/core/base.jl +++ b/src/core/base.jl @@ -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 diff --git a/src/form_decomposition/acp.jl b/src/form_decomposition/acp.jl index 7ab1033..8460ddf 100644 --- a/src/form_decomposition/acp.jl +++ b/src/form_decomposition/acp.jl @@ -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) @@ -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) @@ -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 @@ -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 diff --git a/src/form_decomposition/acr.jl b/src/form_decomposition/acr.jl index fefb83c..cae03a5 100644 --- a/src/form_decomposition/acr.jl +++ b/src/form_decomposition/acr.jl @@ -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) @@ -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) @@ -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 diff --git a/src/form_decomposition/fbs.jl b/src/form_decomposition/fbs.jl index a247e23..1b153b4 100644 --- a/src/form_decomposition/fbs.jl +++ b/src/form_decomposition/fbs.jl @@ -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) @@ -74,9 +97,9 @@ 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) @@ -84,19 +107,11 @@ function generate_boundary_linking_vars(pm::_PM.ACRPowerModel, pmd::_PMD.FBSUBFP 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 diff --git a/src/form_decomposition/fotp.jl b/src/form_decomposition/fotp.jl index c82a941..c520085 100644 --- a/src/form_decomposition/fotp.jl +++ b/src/form_decomposition/fotp.jl @@ -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) @@ -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) @@ -81,9 +96,9 @@ 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) @@ -91,20 +106,11 @@ function generate_boundary_linking_vars(pm::_PM.ACPPowerModel, pmd::_PMD.FOTPUPo 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 diff --git a/src/form_decomposition/fotr.jl b/src/form_decomposition/fotr.jl index 592bda8..11ea2a8 100644 --- a/src/form_decomposition/fotr.jl +++ b/src/form_decomposition/fotr.jl @@ -51,7 +51,6 @@ function constraint_boundary_voltage_angle(pmd::_PMD.FOTRUPowerModel, ::Int, t_b end - # TODO: multinetwork compatibility by using nw info. """ function generate_boundary_linking_vars( @@ -62,11 +61,34 @@ end ) -Generates the ACR-FOTRU 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-FOTRU 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.FOTRUPowerModel, 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.FOTRUPowerModel, + boundary_number::String; + nw::Int = nw_id_default, + export_models::Bool=false + ) + +Generates the FOTRUPowerModel 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.FOTRUPowerModel, boundary_number::String; nw::Int=nw_id_default, export_models::Bool=false) + # Parse to Int boundary_number = parse(Int64, boundary_number) @@ -75,9 +97,9 @@ function generate_boundary_linking_vars(pm::_PM.ACRPowerModel, pmd::_PMD.FOTRUPo 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) @@ -85,19 +107,11 @@ function generate_boundary_linking_vars(pm::_PM.ACRPowerModel, pmd::_PMD.FOTRUPo 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 diff --git a/src/form_decomposition/ivr.jl b/src/form_decomposition/ivr.jl index b9c5ed6..b4921e8 100644 --- a/src/form_decomposition/ivr.jl +++ b/src/form_decomposition/ivr.jl @@ -145,30 +145,46 @@ end export_models::Bool=false ) -Generates the IVR-IVRU 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 IVR-IVRU 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.AbstractIVRModel, pmd::_PMD.IVRUPowerModel, 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.AbstractIVRModel, + boundary_number::String; + nw::Int = nw_id_default, + export_models::Bool=false + ) + +Generates the IVR 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.AbstractIVRModel, 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, vi (subproblem) - vr = _PMD.var(pmd, nw, :vr, t_bus) - vi = _PMD.var(pmd, nw, :vi, t_bus) - - # Transmission: Vr, Vi (master) + # Transmission: Vr and Vi (master) Vr = _PM.var(pm, nw, :vr, f_bus) Vi = _PM.var(pm, nw, :vi, f_bus) @@ -176,11 +192,64 @@ function generate_boundary_linking_vars(pm::_PM.AbstractIVRModel, pmd::_PMD.IVRU 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.IVRUPowerModel, + boundary_number::String; + nw::Int = nw_id_default, + export_models::Bool=false + ) + +Generates the IVRU 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.IVRUPowerModel, 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 diff --git a/src/form_decomposition/lindist3flow.jl b/src/form_decomposition/lindist3flow.jl index ea95e81..1f65ec9 100644 --- a/src/form_decomposition/lindist3flow.jl +++ b/src/form_decomposition/lindist3flow.jl @@ -78,31 +78,47 @@ end pm::_PM.AbstractBFAModel, pmd::_PMD.LPUBFDiagPowerModel, boundary_number::String; - nw::Int = nw_id_default - + nw::Int = nw_id_default, + export_models::Bool=false ) -Generates the BFA-LPUBFDiagPowerModel 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 BFA-LinDistFlow 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.AbstractBFAModel, pmd::_PMD.LPUBFDiagPowerModel, 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.AbstractBFAModel, + boundary_number::String; + nw::Int = nw_id_default, + export_models::Bool=false + ) + +Generates the BFA 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.AbstractBFAModel, 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: w (subproblem) - w = _PMD.var(pmd, nw, :w, t_bus) # Transmission: W (master) W = _PM.var(pm, nw, :w, f_bus) @@ -111,11 +127,63 @@ function generate_boundary_linking_vars(pm::_PM.AbstractBFAModel, pmd::_PMD.LPUB 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], W], [p_aux[1], q_aux[1], w[1]]] + boundary_linking_vars = [[P_load[1], Q_load[1], W]] + + 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.LPUBFDiagPowerModel, + boundary_number::String; + nw::Int = nw_id_default, + export_models::Bool=false + ) + +Generates the LinDistFlow 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.LPUBFDiagPowerModel, 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: w (subproblem) + w = _PMD.var(pmd, nw, :w, t_bus) + + boundary_linking_vars = [[p_aux[1], q_aux[1], w[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 diff --git a/src/form_decomposition/linear.jl b/src/form_decomposition/linear.jl index 7f635aa..a0be08a 100644 --- a/src/form_decomposition/linear.jl +++ b/src/form_decomposition/linear.jl @@ -139,32 +139,100 @@ end export_models::Bool=false ) -Generates the NFA-NFAU 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 NFA-NFAU 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.NFAPowerModel, pmd::_PMD.NFAUPowerModel, 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.NFAPowerModel, + boundary_number::String; + nw::Int = nw_id_default, + export_models::Bool=false + ) + +Generates the NFA 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.NFAPowerModel, 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) # Transmission: Pload & Qload (master) P_load = _PM.var(pm, nw, :pbound_load_scaled, f_idx) - boundary_linking_vars = [[P_load[1]], [p_aux[1]]] + boundary_linking_vars = [[P_load[1]]] + + 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.NFAUPowerModel, + boundary_number::String; + nw::Int = nw_id_default, + export_models::Bool=false + ) + +Generates the NFAU 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.NFAUPowerModel, 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) + + boundary_linking_vars = [[p_aux[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 diff --git a/src/form_decomposition/wmodels.jl b/src/form_decomposition/wmodels.jl index bdd5c64..7474e82 100644 --- a/src/form_decomposition/wmodels.jl +++ b/src/form_decomposition/wmodels.jl @@ -157,31 +157,47 @@ end pm::_PM.AbstractSOCBFModel, pmd::_PMD.SOCNLPUBFPowerModel, boundary_number::String; - nw::Int = nw_id_default - + nw::Int = nw_id_default, + export_models::Bool=false ) -Generates the SOCBF-SOCNLPUBF 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 SOCBF-SOCNL 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.AbstractSOCBFModel, pmd::_PMD.SOCNLPUBFPowerModel, 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.AbstractSOCBFModel, + boundary_number::String; + nw::Int = nw_id_default, + export_models::Bool=false + ) + +Generates the SOCBF 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.AbstractSOCBFModel, 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: w (subproblem) - w = _PMD.var(pmd, nw, :w, t_bus) # Transmission: W (master) W = _PM.var(pm, nw, :w, f_bus) @@ -190,11 +206,63 @@ function generate_boundary_linking_vars(pm::_PM.AbstractSOCBFModel, pmd::_PMD.SO 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], W], [p_aux[1], q_aux[1], w[1]]] + boundary_linking_vars = [[P_load[1], Q_load[1], W]] + + 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.SOCNLPUBFPowerModel, + boundary_number::String; + nw::Int = nw_id_default, + export_models::Bool=false + ) + +Generates the SOCNLPUBF 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.SOCNLPUBFPowerModel, 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: w (subproblem) + w = _PMD.var(pmd, nw, :w, t_bus) + + boundary_linking_vars = [[p_aux[1], q_aux[1], w[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