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

Update ORDC model #1115

Merged
merged 7 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 29 additions & 2 deletions src/devices_models/devices/common/add_to_expression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ function add_expressions!(
W <: AbstractReservesFormulation,
} where {D <: PSY.Component}
time_steps = get_time_steps(container)
names = [PSY.get_name(d) for d in devices]
add_expression_container!(container, T(), D, names, time_steps)
@assert length(devices) == 1
add_expression_container!(
container,
T(),
D,
PSY.get_name.(devices),
time_steps;
meta = PSY.get_name(first(devices)),
)
return
end

Expand Down Expand Up @@ -1406,6 +1413,25 @@ function add_to_expression!(
return
end

function add_to_expression!(
container::OptimizationContainer,
::Type{S},
cost_expression::JuMP.AbstractJuMPScalar,
component::T,
time_period::Int,
) where {S <: CostExpressions, T <: PSY.ReserveDemandCurve}
if has_container_key(container, S, T, PSY.get_name(component))
device_cost_expression = get_expression(container, S(), T, PSY.get_name(component))
component_name = PSY.get_name(component)
JuMP.add_to_expression!(
device_cost_expression[component_name, time_period],
cost_expression,
)
end
return
end

#=
function add_to_expression!(
container::OptimizationContainer,
::Type{T},
Expand Down Expand Up @@ -1461,3 +1487,4 @@ function add_to_expression!(
end
return
end
=#
107 changes: 106 additions & 1 deletion src/devices_models/devices/common/objective_function/market_bid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function _add_pwl_variables!(
pwlvars[i] =
var_container[(component_name, i, time_period)] = JuMP.@variable(
get_jump_model(container),
base_name = "PieceWiseLinearBlockOffer_$(component_name)_supply_{pwl_$(i), $time_period}",
base_name = "PieceWiseLinearBlockOffer_$(component_name)_{pwl_$(i), $time_period}",
lower_bound = 0.0,
)
end
Expand Down Expand Up @@ -80,6 +80,49 @@ function _add_pwl_constraint!(
return
end

"""
Implement the constraints for PWL Block Offer variables for ORDC. That is:

```math
\\sum_{k\\in\\mathcal{K}} \\delta_{k,t} = p_t \\\\
\\sum_{k\\in\\mathcal{K}} \\delta_{k,t} <= P_{k+1,t}^{max} - P_{k,t}^{max}
```
"""
function _add_pwl_constraint!(
container::OptimizationContainer,
component::T,
::U,
break_points::Vector{Float64},
sos_status::SOSStatusVariable,
period::Int,
) where {T <: PSY.ReserveDemandCurve, U <: ServiceRequirementVariable}
name = PSY.get_name(component)
variables = get_variable(container, U(), T, name)
const_container = lazy_container_addition!(
container,
PieceWiseLinearBlockOfferConstraint(),
T,
axes(variables)...;
meta = name,
)
len_cost_data = length(break_points) - 1
jump_model = get_jump_model(container)
pwl_vars = get_variable(container, PieceWiseLinearBlockOffer(), T)
const_container[name, period] = JuMP.@constraint(
jump_model,
variables[name, period] ==
sum(pwl_vars[name, ix, period] for ix in 1:len_cost_data)
)

for ix in 1:len_cost_data
JuMP.@constraint(
jump_model,
pwl_vars[name, ix, period] <= break_points[ix + 1] - break_points[ix]
)
end
return
end

##################################################
################ PWL Expressions #################
##################################################
Expand Down Expand Up @@ -136,6 +179,29 @@ function _get_pwl_cost_expression(
)
end

"""
Get cost expression for StepwiseCostReserve
"""
function _get_pwl_cost_expression(
container::OptimizationContainer,
component::T,
time_period::Int,
cost_data::PSY.PiecewiseStepData,
multiplier::Float64,
) where {T <: PSY.ReserveDemandCurve}
name = PSY.get_name(component)
pwl_var_container = get_variable(container, PieceWiseLinearBlockOffer(), T)
slopes = PSY.get_y_coords(cost_data)
ordc_cost = JuMP.AffExpr(0.0)
for i in 1:length(slopes)
JuMP.add_to_expression!(
ordc_cost,
slopes[i] * multiplier * pwl_var_container[(name, i, time_period)],
)
end
return ordc_cost
end

#=
# For Market Bid
function _add_pwl_variables!(
Expand Down Expand Up @@ -269,6 +335,45 @@ function _add_pwl_term!(
return pwl_cost_expressions
end

##################################################
########## PWL for StepwiseCostReserve ##########
##################################################

function _add_pwl_term!(
container::OptimizationContainer,
component::T,
cost_data::PSY.CostCurve{PSY.PiecewiseIncrementalCurve},
::U,
::V,
) where {T <: PSY.Component, U <: VariableType, V <: AbstractServiceFormulation}
multiplier = objective_function_multiplier(U(), V())
resolution = get_resolution(container)
dt = Dates.value(Dates.Second(resolution)) / SECONDS_IN_HOUR
base_power = get_base_power(container)
value_curve = PSY.get_value_curve(cost_data)
power_units = PSY.get_power_units(cost_data)
cost_component = PSY.get_function_data(value_curve)
device_base_power = PSY.get_base_power(component)
data = get_piecewise_incrementalcurve_per_system_unit(
cost_component,
power_units,
base_power,
device_base_power,
)
name = PSY.get_name(component)
time_steps = get_time_steps(container)
pwl_cost_expressions = Vector{JuMP.AffExpr}(undef, time_steps[end])
sos_val = _get_sos_value(container, V, component)
for t in time_steps
break_points = PSY.get_x_coords(data)
_add_pwl_variables!(container, T, name, t, data)
_add_pwl_constraint!(container, component, U(), break_points, sos_val, t)
pwl_cost = _get_pwl_cost_expression(container, component, t, data, multiplier * dt)
pwl_cost_expressions[t] = pwl_cost
end
return pwl_cost_expressions
end

#=
"""
Add PWL cost terms for data coming from the MarketBidCost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,38 +311,6 @@ function _get_pwl_cost_expression(
)
end

##################################################
########## PWL for StepwiseCostReserve ##########
##################################################

function _add_pwl_term!(
container::OptimizationContainer,
component::T,
cost_data::AbstractVector{PSY.PiecewiseStepData},
::U,
::V,
) where {T <: PSY.Component, U <: VariableType, V <: AbstractServiceFormulation}
multiplier = objective_function_multiplier(U(), V())
resolution = get_resolution(container)
dt = Dates.value(Dates.Second(resolution)) / SECONDS_IN_HOUR
base_power = get_base_power(container)
# Re-scale breakpoints by Basepower
name = PSY.get_name(component)
time_steps = get_time_steps(container)
pwl_cost_expressions = Vector{JuMP.AffExpr}(undef, time_steps[end])
sos_val = _get_sos_value(container, V, component)
for t in time_steps
data = cost_data[t]
break_points = PSY.get_x_coords(data) ./ base_power
_add_pwl_variables!(container, T, name, t, data)
_add_pwl_constraint!(container, component, U(), break_points, sos_val, t)
_add_pwl_sos_constraint!(container, component, U(), break_points, sos_val, t)
pwl_cost = _get_pwl_cost_expression(container, component, t, data, multiplier * dt)
pwl_cost_expressions[t] = pwl_cost
end
return pwl_cost_expressions
end

##################################################
######## CostCurve: PiecewisePointCurve ##########
##################################################
Expand Down
81 changes: 53 additions & 28 deletions src/services_models/reserves.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ get_multiplier_value(::RequirementTimeSeriesParameter, d::PSY.ReserveNonSpinning
get_parameter_multiplier(::VariableValueParameter, d::Type{<:PSY.AbstractReserve}, ::AbstractReservesFormulation) = 1.0
get_initial_parameter_value(::VariableValueParameter, d::Type{<:PSY.AbstractReserve}, ::AbstractReservesFormulation) = 0.0

objective_function_multiplier(::ServiceRequirementVariable, ::StepwiseCostReserve) = 1.0
objective_function_multiplier(::ServiceRequirementVariable, ::StepwiseCostReserve) = -1.0
sos_status(::PSY.ReserveDemandCurve, ::StepwiseCostReserve)=SOSStatusVariable.NO_VARIABLE
uses_compact_power(::PSY.ReserveDemandCurve, ::StepwiseCostReserve)=false
#! format: on
Expand Down Expand Up @@ -87,6 +87,40 @@ function get_default_attributes(
return Dict{String, Any}()
end

"""
Add variables for ServiceRequirementVariable for StepWiseCostReserve
"""
function add_variable!(
container::OptimizationContainer,
variable_type::T,
service::D,
formulation,
) where {
T <: ServiceRequirementVariable,
D <: PSY.ReserveDemandCurve,
}
time_steps = get_time_steps(container)
service_name = PSY.get_name(service)
variable = add_variable_container!(
container,
variable_type,
D,
[service_name],
time_steps;
meta = service_name,
)

for t in time_steps
variable[service_name, t] = JuMP.@variable(
get_jump_model(container),
base_name = "$(T)_$(D)_$(service_name)_{$(service_name), $(t)}",
lower_bound = 0.0,
)
end

return
end

################################## Reserve Requirement Constraint ##########################
function add_constraints!(
container::OptimizationContainer,
Expand Down Expand Up @@ -276,7 +310,8 @@ function add_constraints!(
)
reserve_variable =
get_variable(container, ActivePowerReserveVariable(), SR, service_name)
requirement_variable = get_variable(container, ServiceRequirementVariable(), SR)
requirement_variable =
get_variable(container, ServiceRequirementVariable(), SR, service_name)
jump_model = get_jump_model(container)
for t in time_steps
constraint[service_name, t] = JuMP.@constraint(
Expand Down Expand Up @@ -479,36 +514,26 @@ function _add_variable_cost_to_objective!(
@debug "PWL Variable Cost" _group = LOG_GROUP_COST_FUNCTIONS component_name
# If array is full of tuples with zeros return 0.0
time_steps = get_time_steps(container)
variable_cost_forecast = get_time_series(container, component, "variable_cost")
variable_cost_forecast_values = TimeSeries.values(variable_cost_forecast)
parameter_container = _get_cost_function_parameter_container(
container,
CostFunctionParameter(),
component,
T(),
U(),
eltype(variable_cost_forecast_values),
)
variable_cost = PSY.get_variable(component)
if variable_cost isa Nothing
error("ReserveDemandCurve $(component.name) does not have cost data.")
elseif typeof(variable_cost) <: PSY.TimeSeriesKey
error(
"Timeseries curve for ReserveDemandCurve $(component.name) is not supported yet.",
)
end

pwl_cost_expressions =
_add_pwl_term!(container, component, variable_cost_forecast_values, T(), U())
jump_model = get_jump_model(container)
_add_pwl_term!(container, component, variable_cost, T(), U())
for t in time_steps
set_multiplier!(
parameter_container,
# Using 1.0 here since we want to reuse the existing code that adds the mulitpler
# of base power times the time delta.
1.0,
component_name,
t,
)
set_parameter!(
parameter_container,
jump_model,
variable_cost_forecast_values[t],
component_name,
add_to_expression!(
container,
ProductionCostExpression,
pwl_cost_expressions[t],
component,
t,
)
add_to_objective_variant_expression!(container, pwl_cost_expressions[t])
add_to_objective_invariant_expression!(container, pwl_cost_expressions[t])
end
return
end
Expand Down
2 changes: 1 addition & 1 deletion src/services_models/services_constructor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function construct_service!(
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
contributing_devices = get_contributing_devices(model)
add_variable!(container, ServiceRequirementVariable(), [service], StepwiseCostReserve())
add_variable!(container, ServiceRequirementVariable(), service, StepwiseCostReserve())
add_variables!(
container,
ActivePowerReserveVariable,
Expand Down
2 changes: 1 addition & 1 deletion test/test_network_constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ end
PSI.ModelBuildStatus.BUILT
@test solve!(ps_model) == PSI.RunStatus.SUCCESSFULLY_FINALIZED

moi_tests(ps_model, 576, 0, 552, 552, 360, false)
moi_tests(ps_model, 576, 0, 576, 576, 360, false)

opt_container = PSI.get_optimization_container(ps_model)
copper_plate_constraints =
Expand Down
Loading
Loading