Skip to content

Commit

Permalink
Merge pull request #1136 from NREL-Sienna/rh/fix_multiple_bugs
Browse files Browse the repository at this point in the history
Fix multiple issues
  • Loading branch information
jd-lara authored Sep 4, 2024
2 parents 666ec92 + bd8b958 commit 82f893e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1534,23 +1534,6 @@ function construct_device!(
model,
network_model,
)
add_to_expression!(
container,
ReactivePowerBalance,
ReactivePowerVariable,
devices,
model,
network_model,
)
add_to_expression!(
container,
ActivePowerBalance,
OnStatusParameter,
devices,
model,
network_model,
)

add_to_expression!(
container,
ActivePowerRangeExpressionLB,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,21 +408,33 @@ function _add_pwl_term!(
end

# Compact PWL data does not exists anymore

if slopes[1] != 0.0
@debug "PWL has no 0.0 intercept for generator $(component_name)"
# adds a first intercept a x = 0.0 and y below the intercept of the first tuple to make convex equivalent
intercept_point = (x = 0.0, y = first(data).y - COST_EPSILON)
data = PSY.PiecewiseLinearData(vcat(intercept_point, get_points(data)))
@assert PSY.is_convex(slopes)
x_coords = PSY.get_x_coords(data)
if x_coords[1] != 0.0
y_coords = PSY.get_y_coords(data)
x_first = round(x_coords[1]; digits = 3)
y_first = round(y_coords[1]; digits = 3)
slope_first = round(slopes[1]; digits = 3)
guess_y_zero = y_coords[1] - slopes[1] * x_coords[1]
@warn(

Check warning on line 418 in src/devices_models/devices/common/objective_function/piecewise_linear.jl

View check run for this annotation

Codecov / codecov/patch

src/devices_models/devices/common/objective_function/piecewise_linear.jl#L411-L418

Added lines #L411 - L418 were not covered by tests
"PWL has no 0.0 intercept for generator $(name). First point is given at (x = $(x_first), y = $(y_first)). Adding a first intercept at (x = 0.0, y = $(round(guess_y_zero, digits = 3)) to have equal initial slope $(slope_first)"
)
if guess_y_zero < 0.0
error(

Check warning on line 422 in src/devices_models/devices/common/objective_function/piecewise_linear.jl

View check run for this annotation

Codecov / codecov/patch

src/devices_models/devices/common/objective_function/piecewise_linear.jl#L421-L422

Added lines #L421 - L422 were not covered by tests
"Added zero intercept has negative cost for generator $(name). Consider using other formulation or improve data.",
)
end
# adds a first intercept a x = 0.0 and y above the intercept of the first tuple to make convex equivalent (avoid floating point issues of almost equal slopes)
intercept_point = (x = 0.0, y = guess_y_zero + COST_EPSILON)
data = PSY.PiecewiseLinearData(vcat(intercept_point, PSY.get_points(data)))
@assert PSY.is_convex(data)

Check warning on line 429 in src/devices_models/devices/common/objective_function/piecewise_linear.jl

View check run for this annotation

Codecov / codecov/patch

src/devices_models/devices/common/objective_function/piecewise_linear.jl#L427-L429

Added lines #L427 - L429 were not covered by tests
end

time_steps = get_time_steps(container)
pwl_cost_expressions = Vector{JuMP.AffExpr}(undef, time_steps[end])
break_points = PSY.get_x_coords(data)
sos_val = _get_sos_value(container, V, component)
for t in time_steps
_add_pwl_variables!(container, T, component_name, t, data)
_add_pwl_variables!(container, T, name, t, data)

Check warning on line 437 in src/devices_models/devices/common/objective_function/piecewise_linear.jl

View check run for this annotation

Codecov / codecov/patch

src/devices_models/devices/common/objective_function/piecewise_linear.jl#L437

Added line #L437 was not covered by tests
_add_pwl_constraint!(container, component, U(), break_points, sos_val, t)
pwl_cost =
_get_pwl_cost_expression(container, component, t, cost_function, U(), V())
Expand Down
4 changes: 2 additions & 2 deletions src/devices_models/devices/electric_loads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ get_variable_multiplier(_, ::Type{<:PSY.ElectricLoad}, ::AbstractLoadFormulation

get_variable_binary(::ActivePowerVariable, ::Type{<:PSY.ElectricLoad}, ::AbstractLoadFormulation) = false
get_variable_lower_bound(::ActivePowerVariable, d::PSY.ElectricLoad, ::AbstractLoadFormulation) = 0.0
get_variable_upper_bound(::ActivePowerVariable, d::PSY.ElectricLoad, ::AbstractLoadFormulation) = PSY.get_active_power(d)
get_variable_upper_bound(::ActivePowerVariable, d::PSY.ElectricLoad, ::AbstractLoadFormulation) = PSY.get_max_active_power(d)

########################### ReactivePowerVariable, ElectricLoad ####################################

get_variable_binary(::ReactivePowerVariable, ::Type{<:PSY.ElectricLoad}, ::AbstractLoadFormulation) = false

get_variable_lower_bound(::ReactivePowerVariable, d::PSY.ElectricLoad, ::AbstractLoadFormulation) = 0.0
get_variable_upper_bound(::ReactivePowerVariable, d::PSY.ElectricLoad, ::AbstractLoadFormulation) = PSY.get_reactive_power(d)
get_variable_upper_bound(::ReactivePowerVariable, d::PSY.ElectricLoad, ::AbstractLoadFormulation) = PSY.get_max_reactive_power(d)

########################### ReactivePowerVariable, ElectricLoad ####################################

Expand Down
16 changes: 16 additions & 0 deletions src/services_models/services_constructor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function construct_service!(
) where {SR <: PSY.Reserve}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
add_parameters!(container, RequirementTimeSeriesParameter, service, model)
contributing_devices = get_contributing_devices(model)

Expand All @@ -130,6 +131,7 @@ function construct_service!(
) where {SR <: PSY.Reserve}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
contributing_devices = get_contributing_devices(model)

add_constraints!(container, RequirementConstraint, service, contributing_devices, model)
Expand Down Expand Up @@ -160,6 +162,7 @@ function construct_service!(
) where {SR <: PSY.ConstantReserve}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
contributing_devices = get_contributing_devices(model)

add_variables!(
Expand All @@ -185,6 +188,7 @@ function construct_service!(
) where {SR <: PSY.ConstantReserve}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
contributing_devices = get_contributing_devices(model)

add_constraints!(container, RequirementConstraint, service, contributing_devices, model)
Expand Down Expand Up @@ -214,6 +218,7 @@ function construct_service!(
) where {SR <: PSY.Reserve}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
contributing_devices = get_contributing_devices(model)
add_variable!(container, ServiceRequirementVariable(), service, StepwiseCostReserve())
add_variables!(
Expand All @@ -239,6 +244,7 @@ function construct_service!(
) where {SR <: PSY.Reserve}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
contributing_devices = get_contributing_devices(model)

add_constraints!(container, RequirementConstraint, service, contributing_devices, model)
Expand All @@ -251,6 +257,7 @@ function construct_service!(
return
end

#=
function construct_service!(
container::OptimizationContainer,
sys::PSY.System,
Expand Down Expand Up @@ -343,6 +350,7 @@ function construct_service!(
objective_function!(container, services, model)
return
end
=#

"""
Constructs a service for ConstantReserveGroup.
Expand All @@ -358,6 +366,7 @@ function construct_service!(
) where {SR <: PSY.ConstantReserveGroup}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
contributing_services = PSY.get_contributing_services(service)
# check if variables exist
check_activeservice_variables(container, contributing_services)
Expand All @@ -376,6 +385,7 @@ function construct_service!(
) where {SR <: PSY.ConstantReserveGroup}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
contributing_services = PSY.get_contributing_services(service)

add_constraints!(
Expand All @@ -401,6 +411,7 @@ function construct_service!(
) where {SR <: PSY.Reserve}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
contributing_devices = get_contributing_devices(model)
add_parameters!(container, RequirementTimeSeriesParameter, service, model)

Expand All @@ -427,6 +438,7 @@ function construct_service!(
) where {SR <: PSY.Reserve}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
contributing_devices = get_contributing_devices(model)

add_constraints!(container, RequirementConstraint, service, contributing_devices, model)
Expand Down Expand Up @@ -458,6 +470,7 @@ function construct_service!(
) where {SR <: PSY.ReserveNonSpinning}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
contributing_devices = get_contributing_devices(model)
add_parameters!(container, RequirementTimeSeriesParameter, service, model)

Expand All @@ -483,6 +496,7 @@ function construct_service!(
) where {SR <: PSY.ReserveNonSpinning}
name = get_service_name(model)
service = PSY.get_component(SR, sys, name)
!PSY.get_available(service) && return
contributing_devices = get_contributing_devices(model)

add_constraints!(container, RequirementConstraint, service, contributing_devices, model)
Expand Down Expand Up @@ -549,6 +563,7 @@ function construct_service!(
) where {T <: PSY.TransmissionInterface}
name = get_service_name(model)
service = PSY.get_component(T, sys, name)
!PSY.get_available(service) && return

add_to_expression!(
container,
Expand Down Expand Up @@ -640,6 +655,7 @@ function construct_service!(
) where {T <: PSY.TransmissionInterface}
name = get_service_name(model)
service = PSY.get_component(T, sys, name)
!PSY.get_available(service) && return

add_to_expression!(
container,
Expand Down

0 comments on commit 82f893e

Please sign in to comment.