Skip to content

Commit 208e5fa

Browse files
authored
## Type-stable DollarAdjust with typed zero (#1026)
* refactor: make DollarAdjust parametric for type stability - Convert DollarAdjust to parametric type DollarAdjust{T<:Real} - Add type parameters to function signatures using DollarAdjust - Replace literal 0.0 returns with zero(T) for type stability - Add eltype method for DollarAdjust type inference * fix: update DollarAdjust type parameter for cost calculation function
1 parent 585116c commit 208e5fa

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/actors/costing/aries_costing_actor.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ end
259259
260260
Capital cost for each layer in the build
261261
"""
262-
function cost_direct_capital_ARIES(layer::IMAS.build__layer, cst::IMAS.costing, da::DollarAdjust)
262+
function cost_direct_capital_ARIES(layer::IMAS.build__layer, cst::IMAS.costing, da::DollarAdjust{T}) where {T<:Real}
263263
da.year_assessed = 2016
264264
if layer.type == Int(_oh_)
265-
return 0.0 # oh is part of the pf_active calculation
265+
return zero(T) # oh is part of the pf_active calculation
266266
elseif layer.type == Int(_tf_)
267267
build = IMAS.parent(IMAS.parent(layer))
268268
cost = layer.volume * (unit_cost(build.tf.technology, cst) * (1.0 - build.tf.nose_hfs_fraction) .+ unit_cost(Material(:steel), cst) * build.tf.nose_hfs_fraction)
@@ -452,14 +452,14 @@ end
452452
453453
NOTE: ARIES https://cer.ucsd.edu/_files/publications/UCSD-CER-13-01.pdf
454454
"""
455-
function cost_direct_capital_ARIES(::Val{:balance_of_plant_equipment}, power_thermal::Real, power_electric_generated::Real, da::DollarAdjust, dd::IMAS.dd)
455+
function cost_direct_capital_ARIES(::Val{:balance_of_plant_equipment}, power_thermal::Real, power_electric_generated::Real, da::DollarAdjust{T}, dd::IMAS.dd) where {T<:Real}
456456
da.year_assessed = 2009
457457
power_thermal = power_thermal / 1E6
458458
power_electric_generated = power_electric_generated / 1E6
459459
bop = dd.balance_of_plant
460460

461461
if power_thermal <= 0.0
462-
return 0.0
462+
return zero(T)
463463
else
464464
if contains(lowercase(bop.power_plant.power_cycle_type), "rankine")
465465
cost = 350.0 * (power_thermal / 2620.0)^0.7 # Turbine equipment

src/actors/costing/costing_utils.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ end
4848
#= ==================== =#
4949
# Inflation Adjustment #
5050
#= ==================== =#
51-
mutable struct DollarAdjust
52-
future_inflation_rate::Real
53-
construction_start_year::Real
51+
mutable struct DollarAdjust{T<:Real}
52+
future_inflation_rate::T
53+
construction_start_year::T
5454
year_assessed::Union{Missing,Int}
5555
year::Union{Missing,Int}
5656
end
5757

58-
function DollarAdjust(dd::IMAS.dd)
59-
return DollarAdjust(dd.costing.future.inflation_rate, dd.costing.construction_start_year, missing, missing)
58+
function DollarAdjust(dd::IMAS.dd{T}) where {T<:Real}
59+
return DollarAdjust{T}(dd.costing.future.inflation_rate, dd.costing.construction_start_year, missing, missing)
6060
end
61+
Base.eltype(::Type{DollarAdjust{T}}) where {T} = T
6162

6263
"""
6364
load_inflation_rate()
@@ -95,10 +96,10 @@ end
9596
9697
Adjusts costs assessed in a previous year to current or future dollar amount
9798
"""
98-
function future_dollars(dollars::Real, da::DollarAdjust)
99+
function future_dollars(dollars::Real, da::DollarAdjust{T})::T where {T<:Real}
99100

100101
if dollars == 0.0
101-
return 0.0
102+
return zero(T)
102103
end
103104

104105
CPI = load_inflation_rate()
@@ -129,7 +130,7 @@ function future_dollars(dollars::Real, da::DollarAdjust)
129130
# wipe out year_assessed each time to force developer to enter of `da.year_assessed` and avoid using the wrong year
130131
da.year_assessed = missing
131132

132-
return value
133+
return T(value)
133134
end
134135

135136
#= ================== =#

src/actors/costing/sheffield_costing_actor.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,13 @@ function cost_direct_capital_Sheffield(::Val{:buildings}, bd::IMAS.build, da::Do
347347
return future_dollars(cost, da)
348348
end
349349

350-
function cost_direct_capital_Sheffield(::Val{:blanket}, cap::Bool, dd::IMAS.dd, da::DollarAdjust)
350+
function cost_direct_capital_Sheffield(::Val{:blanket}, cap::Bool, dd::IMAS.dd, da::DollarAdjust{T}) where {T<:Real}
351351
da.year_assessed = 2016
352352

353353
cost = 0.0
354354

355355
if cap == false
356-
return 0.0
356+
return zero(T)
357357
else
358358
bd = dd.build
359359
cst = dd.costing

0 commit comments

Comments
 (0)