Skip to content

Commit 9458b70

Browse files
committed
Update _profile_aggregate to avoid defining over_clustered_years as Parameters
1 parent 730a2f4 commit 9458b70

File tree

4 files changed

+35
-24
lines changed

4 files changed

+35
-24
lines changed

src/TulipaEnergyModel.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ using TimerOutputs: TimerOutput, @timeit
2626
const to = TimerOutput()
2727

2828
# Definitions and auxiliary files
29-
include("utils.jl")
3029
include("run-scenario.jl")
3130
include("model-parameters.jl")
3231
include("structures.jl")
32+
include("utils.jl")
3333

3434
# Data
3535
include("input-schemas.jl")

src/model-preparation.jl

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -599,19 +599,17 @@ function prepare_profiles_structure(connection)
599599
)
600600

601601
over_clustered_year = Dict(
602-
(row.profile_name, row.year) => ProfileWithRollingHorizon(
603-
Float64[
604-
row.value for row in DuckDB.query(
605-
connection,
606-
"SELECT profile.value
607-
FROM profiles_timeframe AS profile
608-
WHERE
609-
profile.profile_name = '$(row.profile_name)'
610-
AND profile.year = $(row.year)
611-
",
612-
)
613-
],
614-
) for row in DuckDB.query(
602+
(row.profile_name, row.year) => Float64[
603+
row.value for row in DuckDB.query(
604+
connection,
605+
"SELECT profile.value
606+
FROM profiles_timeframe AS profile
607+
WHERE
608+
profile.profile_name = '$(row.profile_name)'
609+
AND profile.year = $(row.year)
610+
",
611+
)
612+
] for row in DuckDB.query(
615613
connection,
616614
"SELECT DISTINCT
617615
profiles.profile_name,
@@ -621,7 +619,6 @@ function prepare_profiles_structure(connection)
621619
)
622620
)
623621

624-
# TODO: Decide where to put this (leave here?)
625622
# Creating over_clustered_year profiles of inflows using the inflows
626623
# profiles of rep_periods and asset_milestone.storage_inflows
627624
for row in DuckDB.query(
@@ -672,7 +669,7 @@ function prepare_profiles_structure(connection)
672669
)
673670
]
674671
if length(values) > 0
675-
over_clustered_year[(profile_name, year)] = ProfileWithRollingHorizon(values)
672+
over_clustered_year[(profile_name, year)] = values
676673
end
677674
end
678675

src/structures.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,12 @@ Structure to hold the dictionaries of profiles.
224224
"""
225225
mutable struct ProfileLookup
226226
# The integers here are Int32 because they are obtained directly from DuckDB
227-
#
227+
228228
# rep_period[(asset, year, rep_period)]
229229
rep_period::Dict{Tuple{String,Int32,Int32},ProfileWithRollingHorizon}
230230

231231
# over_clustered_year[(asset, year)]
232-
over_clustered_year::Dict{Tuple{String,Int32},ProfileWithRollingHorizon}
232+
over_clustered_year::Dict{Tuple{String,Int32},Vector{Float64}}
233233
end
234234

235235
"""

src/utils.jl

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,36 @@ If `profiles[tuple_key]` exists, then this function computes the aggregation of
2727
over the range `time_block` using the aggregator `agg_function`, i.e., `agg_function(V[time_block])`.
2828
If it does not exist, then `V[time_block]` is substituted by a vector of the corresponding size and `default_value`.
2929
"""
30-
function _profile_aggregate(profiles, tuple_key::Tuple, time_block, agg_function, default_value)
30+
function _profile_aggregate(
31+
profiles::Dict, # either rep_period or over_clustered_year
32+
tuple_key::Tuple,
33+
time_block,
34+
agg_function,
35+
default_value,
36+
)
3137
if any(ismissing, tuple_key) || !haskey(profiles, tuple_key)
3238
return agg_function(Iterators.repeated(default_value, length(time_block)))
3339
end
3440
profile_object = profiles[tuple_key]
3541

42+
return _profile_aggregate(profile_object, time_block, agg_function)
43+
end
44+
45+
function _profile_aggregate(profile_object::Vector{Float64}, time_block, agg_function)
46+
return agg_function(skipmissing(profile_object[time_block]))
47+
end
48+
49+
function _profile_aggregate(profile_object::ProfileWithRollingHorizon, time_block, agg_function)
3650
# Rolling horizon is inferred by the existence of rolling_horizon_variables
3751
is_rolling_horizon = length(profile_object.rolling_horizon_variables) > 0
3852

39-
if is_rolling_horizon
40-
profile_value = profile_object.rolling_horizon_variables
41-
return agg_function(skipmissing(profile_value[time_block]))
53+
profile_value = if is_rolling_horizon
54+
profile_object.rolling_horizon_variables
4255
else
43-
profile_value = profile_object.values
44-
return agg_function(skipmissing(profile_value[time_block]))
56+
profile_object.values
4557
end
58+
59+
return agg_function(skipmissing(profile_value[time_block]))
4660
end
4761

4862
"""

0 commit comments

Comments
 (0)