Skip to content

Commit

Permalink
Add highest_in to create_constraints_indices (#913)
Browse files Browse the repository at this point in the history
Co-authored-by: Abel Soares Siqueira <[email protected]>
  • Loading branch information
gnawin and abelsiqueira authored Oct 29, 2024
1 parent 4d35a79 commit b2438cc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/model-preparation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ function construct_dataframes(
dataframes[key] = df
end

# WIP: highest_in_out is not included in constraints_partition anymore
tmp_create_constraints_indices(connection)

# WIP: highest_in_out is not included in constraints_partition anymore
dataframes[:highest_in_out] = TulipaIO.get_table(connection, "cons_indices_highest_in_out")
dataframes[:highest_in_out].timesteps_block = map(
r -> r[1]:r[2],
Expand All @@ -72,6 +73,15 @@ function construct_dataframes(
)
dataframes[:highest_in_out].index = 1:size(dataframes[:highest_in_out], 1)

# WIP: highest_in is not included in constraints_partition anymore
tmp_create_constraints_indices(connection)
dataframes[:highest_in] = TulipaIO.get_table(connection, "cons_indices_highest_in")
dataframes[:highest_in].timesteps_block = map(
r -> r[1]:r[2],
zip(dataframes[:highest_in].time_block_start, dataframes[:highest_in].time_block_end),
)
dataframes[:highest_in].index = 1:size(dataframes[:highest_in], 1)

# DataFrame to store the flow variables
dataframes[:flows] = DuckDB.query(
connection,
Expand Down
12 changes: 6 additions & 6 deletions src/time-resolution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ function compute_constraints_partitions(graph, representative_periods, years)
# strategy = :highest,
# asset_filter = (a, y) -> graph[a].type in ["hub", "consumer"],
# ),
(
name = :highest_in,
partitions = _inflows,
strategy = :highest,
asset_filter = (a, y) -> graph[a].type in ["storage"],
),
# ( # WIP: Testing removing this in favor of using table cons_indices_highest_in
# name = :highest_in,
# partitions = _inflows,
# strategy = :highest,
# asset_filter = (a, y) -> graph[a].type in ["storage"],
# ),
(
name = :highest_out,
partitions = _outflows,
Expand Down
34 changes: 34 additions & 0 deletions src/tmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,40 @@ function tmp_create_constraints_indices(connection)
WHERE main.type in ('hub', 'consumer')
",
)

# This follows the same implementation as highest_in_out above, but using
# only the incoming flows.
#
# The query below is trying to replace the following constraints_partitions example:
#= (
# name = :highest_in,
# partitions = _inflows,
# strategy = :highest,
# asset_filter = (a, y) -> graph[a].type in ["storage"],
# ),
=#
DuckDB.execute(
connection,
"CREATE OR REPLACE TABLE cons_indices_highest_in AS
SELECT
main.asset,
main.year,
main.rep_period,
sub.time_block_start,
lead(sub.time_block_start - 1, 1, main.num_timesteps)
OVER (PARTITION BY main.asset, main.year, main.rep_period ORDER BY time_block_start)
AS time_block_end,
FROM t_cons_indices AS main
LEFT JOIN (
SELECT to_asset as asset, year, rep_period, time_block_start
FROM flow_time_resolution
) AS sub
ON main.asset=sub.asset
AND main.year=sub.year
AND main.rep_period=sub.rep_period
WHERE main.type in ('storage')
",
)
end

"""
Expand Down

0 comments on commit b2438cc

Please sign in to comment.