Skip to content

Commit

Permalink
Refactor CRM adjustment so that it's written only once
Browse files Browse the repository at this point in the history
  • Loading branch information
cfe316 committed Dec 1, 2023
1 parent e6d2b03 commit 2b7bce7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 86 deletions.
116 changes: 30 additions & 86 deletions src/model/resources/fusion/fusion_capacity_reserve_margin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,109 +17,54 @@ function fusion_capacity_reserve_margin_adjustment!(EP::Model,
end

Check warning on line 17 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L15-L17

Added lines #L15 - L17 were not covered by tests
end

# inner-loop function
# inner-loop function: loops over Capacity Reserve Margin zones, for one resource
# and actually adjusts the eCapResMarBalance expression
function _fusion_capacity_reserve_margin_adjustment!(EP::Model,

Check warning on line 22 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L22

Added line #L22 was not covered by tests
inputs::Dict,
resource_component,
r_id::Int)
y::Int)

T = inputs["T"]
dfGen = inputs["dfGen"]
timesteps = collect(1:T)
ncapres = inputs["NCapacityReserveMargin"]

Check warning on line 29 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L27-L29

Added lines #L27 - L29 were not covered by tests

y = r_id

capresfactor(capres) = dfGen[y, Symbol("CapRes_$capres")]

eCapResMarBalance = EP[:eCapResMarBalance]

Check warning on line 31 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L31

Added line #L31 was not covered by tests

get_from_model(f::Function) = EP[Symbol(f(resource_component))]

ePassive = get_from_model(fusion_parasitic_passive_name)
eActive = get_from_model(fusion_parasitic_active_name)
eStartPower = get_from_model(fusion_pulse_start_power_name)

fusion_adj = @expression(EP, [capres in 1:ncapres, t in 1:T],
_fusion_crm_adjustment(capresfactor(capres), ePassive[t], eActive[t], eStartPower[t])
)
add_similar_to_expression!(eCapResMarBalance, fusion_adj)
for capres_zone in 1:ncapres
adjustment = fusion_capacity_reserve_margin_adjustment(EP, inputs, resource_component, y, capres_zone, timesteps)
add_similar_to_expression!(eCapResMarBalance[capres_zone, :], adjustment)
end
return

Check warning on line 37 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L33-L37

Added lines #L33 - L37 were not covered by tests
end

@doc raw"""
fusion_crm(capresfactor::Float64,
eTotalCap::AffExpr,
cap_size::Float64,
passive_power::AffExpr,
active_power::AffExpr,
start_power::AffExpr,
vMDOWN::VariableRef)
# Get the amount for one resource component in one CRM zone
function fusion_capacity_reserve_margin_adjustment(EP::Model,

Check warning on line 41 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L41

Added line #L41 was not covered by tests
inputs::Dict,
resource_component::AbstractString,
y::Int,
capres_zone::Int,
timesteps::Vector{Int})

Capacity reserve margin contributions for a whole plant, with maintenance.
dfGen = inputs["dfGen"]

Check warning on line 48 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L48

Added line #L48 was not covered by tests

capresfactor: Factor associated with the reliability of a plant's ability to produce power.
Must logically be between 0 to 1.
eTotalCap: Capacity of the plants of this type in a zone.
cap_size: Power per plant.
passive_power: Expression for parasitic passive recirculating power.
active_power: Expression for parasitic active recirculating power.
start_power: Expression for parasitic pulse start (peak) power.
vMDOWN: Variable for number of plants under maintenance.
"""
function fusion_crm(capresfactor::Float64,
eTotalCap::AffExpr,
cap_size::Float64,
passive_power::AffExpr,
active_power::AffExpr,
start_power::AffExpr,
vMDOWN::VariableRef)
return fusion_crm(capresfactor, eTotalCap, passive_power, active_power, start_power) +
_maintenance_crm_adjustment(capresfactor, cap_size, maintenance_down)
end
capresfactor = dfGen[y, Symbol("CapRes_" * string(capres_zone))]

Check warning on line 50 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L50

Added line #L50 was not covered by tests

@doc raw"""
fusion_crm(capresfactor::Float64,
eTotalCap::AffExpr,
passive_power::AffExpr,
active_power::AffExpr,
start_power::AffExpr)
get_from_model(f::Function) = EP[Symbol(f(resource_component))]

Check warning on line 52 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L52

Added line #L52 was not covered by tests

Capacity reserve margin contributions for a fusion plant.
ePassive = get_from_model(fusion_parasitic_passive_name)
eActive = get_from_model(fusion_parasitic_active_name)
eStartPower = get_from_model(fusion_pulse_start_power_name)

Check warning on line 56 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L54-L56

Added lines #L54 - L56 were not covered by tests

capresfactor: Factor associated with the reliability of a plant's ability to produce power.
Must logically be between 0 to 1.
eTotalCap: Capacity of the plants of this type in a zone.
passive_power: Expression for parasitic passive recirculating power.
active_power: Expression for parasitic active recirculating power.
start_power: Expression for parasitic pulse start (peak) power.
"""
function fusion_crm(capresfactor::Float64,
eTotalCap::AffExpr,
passive_power::AffExpr,
active_power::AffExpr,
start_power::AffExpr)
return capresfactor * eTotalCap +
_fusion_crm_adjustment(capresfactor, passive_power, active_power, start_power)
fusion_adj = _fusion_crm_adjustment.(capresfactor, ePassive[timesteps], eActive[timesteps], eStartPower[timesteps])
return fusion_adj

Check warning on line 59 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L58-L59

Added lines #L58 - L59 were not covered by tests
end

@doc raw"""
_maintenance_crm_adjustment(capresfactor::Float64,
cap_size::Float64,
vMDOWN::VariableRef)
Term to account for plants down due to scheduled maintenance.
(`capresfactor` is needed so that this cancels out eTotalCap in the basic CRM expression.)
capresfactor: Factor associated with the reliability of a plant's ability to produce power.
Must logically be between 0 to 1.
cap_size: Power per plant.
vMDOWN: Variable for number of plants under maintenance.
"""
function _maintenance_crm_adjustment(capresfactor::Float64,
cap_size::Float64,
maintenance_down::VariableRef)
return - capresfactor * cap_size * maintenance_down
end

# alias for better parallelism in effective_capacity.jl
thermal_fusion_capacity_reserve_margin_adjustment(a,b,c,d,e,f) = fusion_capacity_reserve_margin_adjustment(a,b,c,d,e,f)

Check warning on line 64 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L64

Added line #L64 was not covered by tests
#################################
# Where the math actually happens
#################################
@doc raw"""
_fusion_crm_adjustment(capresfactor::Float64,
passive_power::AffExpr,
Expand All @@ -140,7 +85,6 @@ end
function _fusion_crm_adjustment(capresfactor::Float64,

Check warning on line 85 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L85

Added line #L85 was not covered by tests
passive_power::AffExpr,
active_power::AffExpr,
start_power::AffExpr)
start_power::AffExpr)::AffExpr
return -capresfactor * (active_power + start_power) - passive_power

Check warning on line 89 in src/model/resources/fusion/fusion_capacity_reserve_margin.jl

View check run for this annotation

Codecov / codecov/patch

src/model/resources/fusion/fusion_capacity_reserve_margin.jl#L89

Added line #L89 was not covered by tests
end

Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ function thermal_plant_effective_capacity(
effective_capacity = effective_capacity .+ value.(adjustment)
end

if has_fusion(inputs) && y in resources_with_fusion(dfGen)
resource_component = dfGen[y, :Resource]
adjustment = thermal_fusion_capacity_reserve_margin_adjustment(EP, inputs, resource_component, y, capres_zone, timesteps)
effective_capacity = effective_capacity .+ value.(adjustment)

Check warning on line 56 in src/write_outputs/capacity_reserve_margin/effective_capacity.jl

View check run for this annotation

Codecov / codecov/patch

src/write_outputs/capacity_reserve_margin/effective_capacity.jl#L53-L56

Added lines #L53 - L56 were not covered by tests
end

return effective_capacity
end

0 comments on commit 2b7bce7

Please sign in to comment.