Skip to content

Commit

Permalink
add Z,W model
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigomha committed Aug 15, 2024
1 parent d46bd21 commit 9203617
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 9 deletions.
10 changes: 9 additions & 1 deletion src/core/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,17 @@ abstract type SparseVariableType <: VariableType end
"""
Struct to dispatch the creation of HVDC Piecewise Loss Variables
Docs abbreviation: ``h``
Docs abbreviation: ``h`` or ``w``
"""
struct HVDCPiecewiseLossVariable <: SparseVariableType end

"""
Struct to dispatch the creation of HVDC Piecewise Binary Loss Variables
Docs abbreviation: ``z``
"""
struct HVDCPiecewiseBinaryLossVariable <: SparseVariableType end

"""
Struct to dispatch the creation of piecewise linear cost variables for objective function
Expand Down Expand Up @@ -282,6 +289,7 @@ const START_VARIABLES = (HotStartVariable, WarmStartVariable, ColdStartVariable)
should_write_resulting_value(::Type{PieceWiseLinearCostVariable}) = false
should_write_resulting_value(::Type{PieceWiseLinearBlockOffer}) = false
should_write_resulting_value(::Type{HVDCPiecewiseLossVariable}) = false
should_write_resulting_value(::Type{HVDCPiecewiseBinaryLossVariable}) = false
convert_result_to_natural_units(::Type{ActivePowerVariable}) = true
convert_result_to_natural_units(::Type{PowerAboveMinimumVariable}) = true
convert_result_to_natural_units(::Type{ActivePowerInVariable}) = true
Expand Down
14 changes: 10 additions & 4 deletions src/devices_models/device_constructors/branch_constructor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -761,9 +761,12 @@ function construct_device!(
container::OptimizationContainer,
sys::PSY.System,
::ArgumentConstructStage,
model::DeviceModel{T, HVDCTwoTerminalPiecewiseLoss},
model::DeviceModel{T, U},
network_model::NetworkModel{<:AbstractPTDFModel},
) where {T <: TwoTerminalHVDCTypes}
) where {
T <: TwoTerminalHVDCTypes,
U <: Union{HVDCTwoTerminalPiecewiseLoss, HVDCTwoTerminalSOSPiecewiseLoss},
}
devices =
get_available_components(model, sys)
add_variables!(
Expand Down Expand Up @@ -803,9 +806,12 @@ function construct_device!(
container::OptimizationContainer,
sys::PSY.System,
::ModelConstructStage,
model::DeviceModel{T, HVDCTwoTerminalPiecewiseLoss},
model::DeviceModel{T, U},
network_model::NetworkModel{<:AbstractPTDFModel},
) where {T <: TwoTerminalHVDCTypes}
) where {
T <: TwoTerminalHVDCTypes,
U <: Union{HVDCTwoTerminalPiecewiseLoss, HVDCTwoTerminalSOSPiecewiseLoss},
}
devices =
get_available_components(model, sys)
add_constraints!(container, FlowRateConstraintFromTo, devices, model, network_model)
Expand Down
62 changes: 61 additions & 1 deletion src/devices_models/devices/AC_branches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ get_variable_binary(::FlowActivePowerSlackLowerBound, ::Type{<:PSY.ACBranch}, ::
get_variable_binary(::FlowActivePowerSlackUpperBound, ::Type{<:PSY.TwoTerminalHVDCLine}, ::AbstractTwoTerminalDCLineFormulation,) = false
get_variable_binary(::FlowActivePowerSlackLowerBound, ::Type{<:PSY.TwoTerminalHVDCLine}, ::AbstractTwoTerminalDCLineFormulation,) = false
get_variable_binary(::HVDCPiecewiseLossVariable, ::Type{<:PSY.TwoTerminalHVDCLine}, ::AbstractTwoTerminalDCLineFormulation,) = false
get_variable_binary(::HVDCPiecewiseBinaryLossVariable, ::Type{<:PSY.TwoTerminalHVDCLine}, ::AbstractTwoTerminalDCLineFormulation,) = true
get_variable_upper_bound(::FlowActivePowerSlackUpperBound, ::PSY.ACBranch, ::AbstractBranchFormulation) = nothing
get_variable_lower_bound(::FlowActivePowerSlackUpperBound, ::PSY.ACBranch, ::AbstractBranchFormulation) = 0.0
get_variable_upper_bound(::FlowActivePowerSlackLowerBound, ::PSY.ACBranch, ::AbstractBranchFormulation) = nothing
Expand Down Expand Up @@ -248,10 +249,11 @@ function _add_dense_pwl_loss_variables!(
end
end

#SOS Model
function _add_sparse_pwl_loss_variables!(
container::OptimizationContainer,
devices,
model::DeviceModel{D, HVDCTwoTerminalPiecewiseLoss},
::DeviceModel{D, HVDCTwoTerminalSOSPiecewiseLoss},
) where {D <: TwoTerminalHVDCTypes}
# Check if type and length of PWL loss model are the same for all devices
#_check_pwl_loss_model(devices)
Expand Down Expand Up @@ -295,6 +297,64 @@ function _add_sparse_pwl_loss_variables!(
end
end

# Full Binary
function _add_sparse_pwl_loss_variables!(
container::OptimizationContainer,
devices,
::DeviceModel{D, HVDCTwoTerminalPiecewiseLoss},
) where {D <: TwoTerminalHVDCTypes}
# Check if type and length of PWL loss model are the same for all devices
#_check_pwl_loss_model(devices)

# Create Variables
time_steps = get_time_steps(container)
settings = get_settings(container)
formulation = HVDCTwoTerminalPiecewiseLoss()
T = HVDCPiecewiseLossVariable
binary_T = get_variable_binary(T(), D, formulation)
U = HVDCPiecewiseBinaryLossVariable
binary_U = get_variable_binary(U(), D, formulation)
first_loss = PSY.get_loss(first(devices))
if isa(first_loss, PSY.LinearCurve)
len_segments = 3 # 2*1 + 1
elseif isa(first_loss, PSY.PiecewiseIncrementalCurve)
len_segments = 2 * length(PSY.get_slopes(first_loss)) + 1
else
error("Should not be here")
end

var_container = lazy_container_addition!(container, T(), D)
var_container_binary = lazy_container_addition!(container, U(), D)

for d in devices
name = PSY.get_name(d)
for t in time_steps
pwlvars = Array{JuMP.VariableRef}(undef, len_segments)
pwlvars_bin = Array{JuMP.VariableRef}(undef, len_segments)
for i in 1:len_segments
pwlvars[i] =
var_container[(name, i, t)] = JuMP.@variable(
get_jump_model(container),
base_name = "$(T)_$(name)_{pwl_$(i), $(t)}",
binary = binary_T
)
ub = get_variable_upper_bound(T(), d, formulation)
ub !== nothing && JuMP.set_upper_bound(var_container[name, i, t], ub)

lb = get_variable_lower_bound(T(), d, formulation)
lb !== nothing && JuMP.set_lower_bound(var_container[name, i, t], lb)

pwlvars_bin[i] =
var_container_binary[(name, i, t)] = JuMP.@variable(
get_jump_model(container),
base_name = "$(U)_$(name)_{pwl_$(i), $(t)}",
binary = binary_U
)
end
end
end
end

################################## Rate Limits constraint_infos ############################

"""
Expand Down
88 changes: 85 additions & 3 deletions src/devices_models/devices/TwoTerminalDC_branches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ end

function _get_range_segments(::PSY.TwoTerminalHVDCLine, loss::PSY.PiecewiseIncrementalCurve)
loss_factors = PSY.get_slopes(loss)
return 1:length(loss_factors)
return 1:(2 * length(loss_factors) + 2)
end

function _get_pwl_loss_params(d::PSY.TwoTerminalHVDCLine, loss::PSY.LinearCurve)
Expand Down Expand Up @@ -253,6 +253,7 @@ function add_constraints!(
::NetworkModel{<:PM.AbstractPowerModel},
) where {T <: HVDCFlowCalculationConstraint, U <: PSY.TwoTerminalHVDCLine}
var_pwl = get_variable(container, HVDCPiecewiseLossVariable(), U)
var_pwl_bin = get_variable(container, HVDCPiecewiseBinaryLossVariable(), U)
names = PSY.get_name.(devices)
time_steps = get_time_steps(container)
flow_ft = get_variable(container, FlowActivePowerFromToVariable(), U)
Expand All @@ -262,10 +263,83 @@ function add_constraints!(
add_constraints_container!(container, T(), U, names, time_steps; meta = "ft")
constraint_to_from =
add_constraints_container!(container, T(), U, names, time_steps; meta = "tf")
constraint_binary =
add_constraints_container!(container, T(), U, names, time_steps; meta = "bin")
for d in devices
name = PSY.get_name(d)
loss = PSY.get_loss(d)
from_to_params, to_from_params = _get_pwl_loss_params(d, loss)
#@show from_to_params
#@show to_from_params
range_segments = 1:(length(from_to_params) - 1) # 1:(2S+1)
for t in time_steps
## Add Equality Constraints ##
constraint_from_to[name, t] = JuMP.@constraint(
get_jump_model(container),
flow_ft[name, t] ==
sum(
var_pwl_bin[name, ix, t] * from_to_params[ix] for
ix in range_segments
) + sum(
var_pwl[name, ix, t] * (from_to_params[ix + 1] - from_to_params[ix]) for
ix in range_segments
)
)
constraint_to_from[name, t] = JuMP.@constraint(
get_jump_model(container),
flow_tf[name, t] ==
sum(
var_pwl_bin[name, ix, t] * to_from_params[ix] for
ix in range_segments
) + sum(
var_pwl[name, ix, t] * (to_from_params[ix + 1] - to_from_params[ix]) for
ix in range_segments
)
)
## Add Binary Bound ###
constraint_binary[name, t] = JuMP.@constraint(
get_jump_model(container),
sum(var_pwl_bin[name, ix, t] for ix in range_segments) == 1.0
)
## Add Bounds for Continuous ##
for ix in range_segments
JuMP.@constraint(
get_jump_model(container),
var_pwl[name, ix, t] <= var_pwl_bin[name, ix, t]
)
if ix == div(length(range_segments) + 1, 2)
JuMP.fix(var_pwl[name, ix, t], 0.0; force = true)
end
end
end
end
return
end

# SOS Model
function add_constraints!(
container::OptimizationContainer,
::Type{T},
devices::Union{Vector{U}, IS.FlattenIteratorWrapper{U}},
::DeviceModel{U, HVDCTwoTerminalSOSPiecewiseLoss},
::NetworkModel{<:PM.AbstractPowerModel},
) where {T <: HVDCFlowCalculationConstraint, U <: PSY.TwoTerminalHVDCLine}
var_pwl = get_variable(container, HVDCPiecewiseLossVariable(), U)
names = PSY.get_name.(devices)
time_steps = get_time_steps(container)
flow_ft = get_variable(container, FlowActivePowerFromToVariable(), U)
flow_tf = get_variable(container, FlowActivePowerToFromVariable(), U)

constraint_from_to =
add_constraints_container!(container, T(), U, names, time_steps; meta = "ft")
constraint_to_from =
add_constraints_container!(container, T(), U, names, time_steps; meta = "tf")
for d in devices
name = PSY.get_name(d)
loss = PSY.get_loss(d)
from_to_params, to_from_params = _get_pwl_loss_params(d, loss)
#@show from_to_params
#@show to_from_params
segments = _get_range_segments(d, loss)
for t in time_steps
## Add Equality Constraints ##
Expand Down Expand Up @@ -439,7 +513,11 @@ function add_constraints!(
) where {
T <: Union{FlowRateConstraintFromTo, FlowRateConstraintToFrom},
U <: PSY.TwoTerminalHVDCLine,
V <: Union{HVDCTwoTerminalDispatch, HVDCTwoTerminalPiecewiseLoss},
V <: Union{
HVDCTwoTerminalDispatch,
HVDCTwoTerminalPiecewiseLoss,
HVDCTwoTerminalSOSPiecewiseLoss,
},
}
inter_network_branches = U[]
for d in devices
Expand All @@ -464,7 +542,11 @@ function add_constraints!(
) where {
T <: Union{FlowRateConstraintToFrom, FlowRateConstraintFromTo},
U <: PSY.TwoTerminalHVDCLine,
V <: Union{HVDCTwoTerminalDispatch, HVDCTwoTerminalPiecewiseLoss},
V <: Union{
HVDCTwoTerminalDispatch,
HVDCTwoTerminalPiecewiseLoss,
HVDCTwoTerminalSOSPiecewiseLoss,
},
}
_add_hvdc_flow_constraints!(container, devices, T())
return
Expand Down

0 comments on commit 9203617

Please sign in to comment.