From c90a3c3b71cf84ec230c91d019738168c87be92b Mon Sep 17 00:00:00 2001 From: Ana Mileva Date: Thu, 21 Nov 2024 17:02:32 -0800 Subject: [PATCH] Flexibly define market prices to vary with iterations (#1179) --- db/csvs_test_examples/csv_structure.csv | 1 + .../markets/prices/1_base_prices.csv | 21 +-------- .../price_profiles/1_base_price_profiles.csv | 19 ++++++++ db/db_schema.sql | 47 ++++++++++++++----- gridpath/system/markets/prices.py | 21 ++++++++- 5 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 db/csvs_test_examples/markets/prices/price_profiles/1_base_price_profiles.csv diff --git a/db/csvs_test_examples/csv_structure.csv b/db/csvs_test_examples/csv_structure.csv index 0ae8ce919..f959ec495 100644 --- a/db/csvs_test_examples/csv_structure.csv +++ b/db/csvs_test_examples/csv_structure.csv @@ -155,6 +155,7 @@ reliability/local_capacity/system_local_capacity_requirement,local_capacity,loca markets/geography_markets,markets,market_scenario_id,geography_markets,simple,,0,,,,, markets/load_zone_markets,markets,load_zone_market_scenario_id,load_zone_markets,simple,,0,,,,, markets/prices,markets,market_price_scenario_id,market_prices,simple,,0,,,,, +markets/prices/price_profiles,markets,market_price_profile_scenario_id,market_price_profiles,simple,,0,,,,, markets/volume,markets,market_volume_scenario_id,market_volume,simple,,0,,,,, water/water_system_params,water,water_system_params_scenario_id,system_water_system_params,simple,,0,,,,, water/water_network,water,water_network_scenario_id,geography_water_network,simple,,0,,,,, diff --git a/db/csvs_test_examples/markets/prices/1_base_prices.csv b/db/csvs_test_examples/markets/prices/1_base_prices.csv index ca575984e..1d14178ac 100644 --- a/db/csvs_test_examples/markets/prices/1_base_prices.csv +++ b/db/csvs_test_examples/markets/prices/1_base_prices.csv @@ -1,19 +1,2 @@ -hydro_iteration,stage_id,timepoint,market,market_price -0,1,20200101,Market_Hub,10 -0,1,20200102,Market_Hub,-10 -0,1,20200201,Market_Hub,10 -0,1,20200202,Market_Hub,-10 -0,1,20200301,Market_Hub,10 -0,1,20200302,Market_Hub,-10 -0,1,20200101,Market_Hub2,1 -0,1,20200102,Market_Hub2,2 -0,1,20200201,Market_Hub2,3 -0,1,20200202,Market_Hub2,2 -0,1,20200301,Market_Hub2,1 -0,1,20200302,Market_Hub2,2 -0,2,20200101,Market_Hub2,3 -0,2,20200102,Market_Hub2,2 -0,2,20200201,Market_Hub2,1 -0,2,20200202,Market_Hub2,2 -0,2,20200301,Market_Hub2,3 -0,2,20200302,Market_Hub2,2 \ No newline at end of file +market_price_profile_scenario_id,varies_by_weather_iteration,varies_by_hydro_iteration +1,0,0 diff --git a/db/csvs_test_examples/markets/prices/price_profiles/1_base_price_profiles.csv b/db/csvs_test_examples/markets/prices/price_profiles/1_base_price_profiles.csv new file mode 100644 index 000000000..b28692212 --- /dev/null +++ b/db/csvs_test_examples/markets/prices/price_profiles/1_base_price_profiles.csv @@ -0,0 +1,19 @@ +weather_iteration,hydro_iteration,stage_id,timepoint,market,market_price +0,0,1,20200101,Market_Hub,10 +0,0,1,20200102,Market_Hub,-10 +0,0,1,20200201,Market_Hub,10 +0,0,1,20200202,Market_Hub,-10 +0,0,1,20200301,Market_Hub,10 +0,0,1,20200302,Market_Hub,-10 +0,0,1,20200101,Market_Hub2,1 +0,0,1,20200102,Market_Hub2,2 +0,0,1,20200201,Market_Hub2,3 +0,0,1,20200202,Market_Hub2,2 +0,0,1,20200301,Market_Hub2,1 +0,0,1,20200302,Market_Hub2,2 +0,0,2,20200101,Market_Hub2,3 +0,0,2,20200102,Market_Hub2,2 +0,0,2,20200201,Market_Hub2,1 +0,0,2,20200202,Market_Hub2,2 +0,0,2,20200301,Market_Hub2,3 +0,0,2,20200302,Market_Hub2,2 diff --git a/db/db_schema.sql b/db/db_schema.sql index 8f219ec5d..76f78ba6a 100644 --- a/db/db_schema.sql +++ b/db/db_schema.sql @@ -932,17 +932,38 @@ CREATE TABLE subscenarios_market_prices DROP TABLE IF EXISTS inputs_market_prices; CREATE TABLE inputs_market_prices ( - market_price_scenario_id INTEGER, - hydro_iteration INTEGER NOT NULL, - stage_id INTEGER, - timepoint INTEGER, - market VARCHAR(32), - market_price FLOAT, - PRIMARY KEY (market_price_scenario_id, hydro_iteration, stage_id, timepoint, market), + market_price_scenario_id INTEGER, + market_price_profile_scenario_id INTEGER, + varies_by_weather_iteration INTEGER, + varies_by_hydro_iteration INTEGER, + PRIMARY KEY (market_price_scenario_id, market_price_profile_scenario_id), FOREIGN KEY (market_price_scenario_id) REFERENCES subscenarios_market_prices (market_price_scenario_id) ); +DROP TABLE IF EXISTS subscenarios_market_price_profiles; +CREATE TABLE subscenarios_market_price_profiles +( + market_price_profile_scenario_id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCHAR(32), + description VARCHAR(128) +); + +DROP TABLE IF EXISTS inputs_market_price_profiles; +CREATE TABLE inputs_market_price_profiles +( + market_price_profile_scenario_id INTEGER, + weather_iteration INTEGER NOT NULL, + hydro_iteration INTEGER NOT NULL, + stage_id INTEGER, + timepoint INTEGER, + market VARCHAR(32), + market_price FLOAT, + PRIMARY KEY (market_price_profile_scenario_id, weather_iteration, + hydro_iteration, stage_id, timepoint, + market) +); + DROP TABLE IF EXISTS subscenarios_market_volume; CREATE TABLE subscenarios_market_volume ( @@ -1093,7 +1114,8 @@ CREATE TABLE inputs_system_water_node_reservoir_exogenous_elevations hydro_iteration INTEGER DEFAULT 0 NOT NULL, timepoint INTEGER, reservoir_exogenous_elevation INTEGER, - PRIMARY KEY (water_node, exogenous_elevation_id, timepoint, hydro_iteration), + PRIMARY KEY (water_node, exogenous_elevation_id, timepoint, + hydro_iteration), FOREIGN KEY (water_node, exogenous_elevation_id) REFERENCES subscenarios_system_water_node_reservoir_exogenous_elevations (water_node, exogenous_elevation_id) @@ -1143,7 +1165,8 @@ CREATE TABLE inputs_system_water_node_reservoirs_target_volumes hydro_iteration INTEGER DEFAULT 0 NOT NULL, timepoint FLOAT, reservoir_target_volume DECIMAL, - PRIMARY KEY (water_node, target_volume_scenario_id, timepoint, hydro_iteration), + PRIMARY KEY (water_node, target_volume_scenario_id, timepoint, + hydro_iteration), FOREIGN KEY (water_node, target_volume_scenario_id) REFERENCES subscenarios_system_water_node_reservoirs_target_volumes (water_node, target_volume_scenario_id) @@ -1235,7 +1258,8 @@ CREATE TABLE inputs_system_water_flows timepoint FLOAT, min_flow_vol_per_second TEXT, max_flow_vol_per_second INTEGER, - PRIMARY KEY (water_flow_scenario_id, water_link, timepoint, hydro_iteration), + PRIMARY KEY (water_flow_scenario_id, water_link, timepoint, + hydro_iteration), FOREIGN KEY (water_flow_scenario_id) REFERENCES subscenarios_system_water_flows (water_flow_scenario_id) ); @@ -1257,7 +1281,8 @@ CREATE TABLE inputs_system_water_inflows hydro_iteration INTEGER DEFAULT 0 NOT NULL, timepoint FLOAT, exogenous_water_inflow_rate_vol_per_sec TEXT, - PRIMARY KEY (water_inflow_scenario_id, water_node, timepoint, hydro_iteration), + PRIMARY KEY (water_inflow_scenario_id, water_node, timepoint, + hydro_iteration), FOREIGN KEY (water_inflow_scenario_id) REFERENCES subscenarios_system_water_inflows (water_inflow_scenario_id) ); diff --git a/gridpath/system/markets/prices.py b/gridpath/system/markets/prices.py index 1467a613c..663e22643 100644 --- a/gridpath/system/markets/prices.py +++ b/gridpath/system/markets/prices.py @@ -85,7 +85,19 @@ def get_inputs_from_database( c = conn.cursor() - prices = c.execute( + (varies_by_weather_iteration, varies_by_hydro_iteration) = c.execute( + f""" + SELECT varies_by_weather_iteration, varies_by_hydro_iteration + FROM inputs_market_prices + WHERE market_price_scenario_id = {subscenarios.MARKET_PRICE_SCENARIO_ID} + """ + ).fetchone() + + weather_iteration_to_use = weather_iteration if varies_by_weather_iteration else 0 + hydro_iteration_to_use = hydro_iteration if varies_by_hydro_iteration else 0 + + c1 = conn.cursor() + prices = c1.execute( f""" SELECT market, timepoint, market_price -- Get prices for included markets only @@ -103,9 +115,14 @@ def get_inputs_from_database( ) as tmp_tbl LEFT OUTER JOIN ( SELECT market, stage_id, timepoint, market_price + FROM inputs_market_price_profiles + WHERE market_price_profile_scenario_id = ( + SELECT market_price_profile_scenario_id FROM inputs_market_prices WHERE market_price_scenario_id = {subscenarios.MARKET_PRICE_SCENARIO_ID} - AND hydro_iteration = {hydro_iteration} + AND hydro_iteration = {hydro_iteration_to_use} + AND weather_iteration = {weather_iteration_to_use} + ) ) as price_tbl USING (market, stage_id, timepoint) ;