From c05fe7dea6574ec5a8d36b9d1e24bad09184b4d6 Mon Sep 17 00:00:00 2001 From: Abel Soares Siqueira Date: Thu, 12 Dec 2024 10:56:20 +0100 Subject: [PATCH] Move some of the active checks earlier in the workflow Relates to #960 --- src/constraints/create.jl | 42 ++++++--------------------------------- src/tmp.jl | 29 ++++++++++++++++++++------- 2 files changed, 28 insertions(+), 43 deletions(-) diff --git a/src/constraints/create.jl b/src/constraints/create.jl index c40a5f98..71506a83 100644 --- a/src/constraints/create.jl +++ b/src/constraints/create.jl @@ -58,13 +58,8 @@ function _create_constraints_tables(connection) FROM t_highest_all_flows AS t_high LEFT JOIN asset ON t_high.asset = asset.asset - LEFT JOIN asset_both - ON t_high.asset = asset_both.asset - AND t_high.year = asset_both.milestone_year - AND t_high.year = asset_both.commission_year WHERE - asset_both.active = true - AND asset.type = 'consumer'; + asset.type = 'consumer'; ", ) @@ -78,13 +73,8 @@ function _create_constraints_tables(connection) FROM t_highest_all_flows AS t_high LEFT JOIN asset ON t_high.asset = asset.asset - LEFT JOIN asset_both - ON t_high.asset = asset_both.asset - AND t_high.year = asset_both.milestone_year - AND t_high.year = asset_both.commission_year WHERE - asset_both.active = true - AND asset.type = 'hub'; + asset.type = 'hub'; ", ) @@ -98,13 +88,8 @@ function _create_constraints_tables(connection) FROM t_highest_in_flows AS t_high LEFT JOIN asset ON t_high.asset = asset.asset - LEFT JOIN asset_both - ON t_high.asset = asset_both.asset - AND t_high.year = asset_both.milestone_year - AND t_high.year = asset_both.commission_year WHERE - asset_both.active = true - AND asset.type in ('storage')", + asset.type in ('storage')", ) DuckDB.query( @@ -117,13 +102,8 @@ function _create_constraints_tables(connection) FROM t_highest_out_flows AS t_high LEFT JOIN asset ON t_high.asset = asset.asset - LEFT JOIN asset_both - ON t_high.asset = asset_both.asset - AND t_high.year = asset_both.milestone_year - AND t_high.year = asset_both.commission_year WHERE - asset_both.active = true - AND asset.type in ('producer', 'storage', 'conversion')", + asset.type in ('producer', 'storage', 'conversion')", ) DuckDB.query( @@ -136,13 +116,8 @@ function _create_constraints_tables(connection) FROM t_highest_assets_and_out_flows AS t_high LEFT JOIN asset ON t_high.asset = asset.asset - LEFT JOIN asset_both - ON t_high.asset = asset_both.asset - AND t_high.year = asset_both.milestone_year - AND t_high.year = asset_both.commission_year WHERE - asset_both.active = true - AND asset.type in ('producer', 'conversion') + asset.type in ('producer', 'conversion') AND asset.unit_commitment = true; ", ) @@ -157,13 +132,8 @@ function _create_constraints_tables(connection) FROM t_highest_out_flows AS t_high LEFT JOIN asset ON t_high.asset = asset.asset - LEFT JOIN asset_both - ON t_high.asset = asset_both.asset - AND t_high.year = asset_both.milestone_year - AND t_high.year = asset_both.commission_year WHERE - asset_both.active = true - AND asset.type in ('producer', 'storage', 'conversion')", + asset.type in ('producer', 'storage', 'conversion')", ) DuckDB.query( diff --git a/src/tmp.jl b/src/tmp.jl index bab41203..72e5bfd8 100644 --- a/src/tmp.jl +++ b/src/tmp.jl @@ -375,8 +375,17 @@ function tmp_create_lowest_resolution_table(connection) current_group = ("", 0, 0) @timeit to "append $table_name rows" for row in DuckDB.query( connection, - "SELECT * FROM $union_table - ORDER BY asset, year, rep_period, time_block_start + "SELECT t_union.* FROM $union_table AS t_union + LEFT JOIN asset_both + ON asset_both.asset = t_union.asset + AND asset_both.milestone_year = t_union.year + AND asset_both.commission_year = t_union.year + WHERE asset_both.active = true + ORDER BY + t_union.asset, + t_union.year, + t_union.rep_period, + t_union.time_block_start ", ) if (row.asset, row.year, row.rep_period) != current_group @@ -418,6 +427,7 @@ function tmp_create_highest_resolution_table(connection) # - keep all unique time_block_start # - create corresponing time_block_end + # filtering by active for union_table in ("t_union_" * x for x in ("in_flows", "out_flows", "assets_and_out_flows", "all_flows")) table_name = replace(union_table, "t_union" => "t_highest") @@ -425,21 +435,26 @@ function tmp_create_highest_resolution_table(connection) connection, "CREATE OR REPLACE TABLE $table_name AS SELECT - asset, + t_union.asset, t_union.year, t_union.rep_period, - time_block_start, - lead(time_block_start - 1, 1, rep_periods_data.num_timesteps) - OVER (PARTITION BY asset, t_union.year, t_union.rep_period ORDER BY time_block_start) + t_union.time_block_start, + lead(t_union.time_block_start - 1, 1, rep_periods_data.num_timesteps) + OVER (PARTITION BY t_union.asset, t_union.year, t_union.rep_period ORDER BY t_union.time_block_start) AS time_block_end FROM ( SELECT DISTINCT asset, year, rep_period, time_block_start FROM $union_table ) AS t_union + LEFT JOIN asset_both + ON asset_both.asset = t_union.asset + AND asset_both.milestone_year = t_union.year + AND asset_both.commission_year = t_union.year LEFT JOIN rep_periods_data ON t_union.year = rep_periods_data.year AND t_union.rep_period = rep_periods_data.rep_period - ORDER BY asset, t_union.year, t_union.rep_period, time_block_start + WHERE asset_both.active = true + ORDER BY t_union.asset, t_union.year, t_union.rep_period, time_block_start ", ) end