Repository navigation
Preserve unit commitment output across myopic windows #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: unstable
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| from temoa._internal.temoa_sequencer import TemoaSequencer | ||
| from temoa.core.config import TemoaConfig | ||
| from temoa.core.model import TemoaModel | ||
| from tests.conftest import TEST_DATA_PATH, _build_test_db | ||
| from tests.legacy_test_values import ExpectedVals, test_vals | ||
|
|
||
| if TYPE_CHECKING: | ||
|
|
@@ -176,6 +177,40 @@ def test_myopic_utopia( | |
| ) | ||
|
|
||
|
|
||
| def test_myopic_unit_commitment_outputs(tmp_path: Path) -> None: | ||
| database = tmp_path / 'uc_myopic.sqlite' | ||
| _build_test_db(database, [TEST_DATA_PATH / 'uc_myopic.sql']) | ||
| config = TemoaConfig( | ||
| scenario='uc_myopic', | ||
| scenario_mode='myopic', | ||
| input_database=database, | ||
| output_database=database, | ||
| output_path=tmp_path, | ||
| solver='gurobi_direct', | ||
| extensions=['unit_commitment'], | ||
| time_sequencing='seasonal_timeslices', | ||
| myopic={'view_depth': 2, 'step_size': 1, 'evolving': False}, | ||
| silent=True, | ||
| ) | ||
|
|
||
| # The windows overlap in 2030. Repeating the run must also replace old output. | ||
| for _ in range(2): | ||
| TemoaSequencer(config=config).start() | ||
| with contextlib.closing(sqlite3.connect(database)) as con: | ||
| rows = con.execute( | ||
| 'SELECT period, COUNT(*), SUM(online_cap), SUM(start_cap), SUM(stop_cap) ' | ||
| 'FROM output_unit_commitment WHERE scenario = ? GROUP BY period ORDER BY period', | ||
| (config.scenario,), | ||
| ).fetchall() | ||
| assert [row[0] for row in rows] == [2020, 2030, 2040] | ||
| for _, count, online, started, stopped in rows: | ||
| assert count == 2 | ||
| # One 1 MW unit supplies both half-year slices at full output. | ||
| assert online == pytest.approx(2.0, rel=1e-5) | ||
| assert started == pytest.approx(0.0, abs=1e-5) | ||
| assert stopped == pytest.approx(0.0, abs=1e-5) | ||
|
Comment on lines
+205
to
+211
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy lift 🔎 Supported by static analysis🏁 Script executed: sed -n '175,220p' tests/test_full_runs.py
rg -n 'clear_results_after|write_results\\(|append|output_unit_commitment' temoa/extensions/myopic/myopic_sequencer.py tests/test_full_runs.py tests/test_table_writer.pyRepository: TemoaProject/temoa Length of output: 2076 🏁 Script executed: set -o pipefail
printf '%s\n' '--- changed diff ---'
git diff --no-ext-diff --unified=80 d5148dbb84669056e4f533eaeb2581f74b7d9ca8 2b6ec3df31516e0a2cb87a04422ba187792e1688 -- tests/test_full_runs.py tests/data/uc_myopic.sql temoa | sed -n '1,320p'
printf '%s\n' '--- fixture locations ---'
rg -n -F --glob '*uc_myopic*' '' . 2>/dev/null || true
printf '%s\n' '--- writer and cleanup references ---'
rg -n -F --glob '*.py' -e 'output_unit_commitment' -e 'clear_results_after' -e 'write_results(' -e 'append' temoa tests || test "$?" -eq 1
printf '%s\n' '--- relevant test references ---'
rg -n -F --glob 'test*.py' -e 'unit_commitment' -e 'output_unit_commitment' -e 'myopic' tests || test "$?" -eq 1Repository: TemoaProject/temoa Length of output: 42648 🏁 Script executed: printf '%s\n' '--- myopic run and cleanup ---'
nl -ba temoa/extensions/myopic/myopic_sequencer.py | sed -n '270,315p;635,720p'
printf '%s\n' '--- unit commitment output writer ---'
nl -ba temoa/extensions/unit_commitment/core/data_puller.py | sed -n '1,115p'
printf '%s\n' '--- capacity writer ---'
nl -ba temoa/_internal/table_writer.py | sed -n '470,565p'
printf '%s\n' '--- table-writer tests ---'
nl -ba tests/test_table_writer.py | sed -n '1,240p'
printf '%s\n' '--- full-run test imports and surrounding tests ---'
nl -ba tests/test_full_runs.py | sed -n '1,225p'Repository: TemoaProject/temoa Length of output: 30198 Make the overlapping-period assertion distinguish the later window. The fixture gives both windows the same unit-commitment totals. The current query can therefore pass when stale 2030 rows remain from the first window. It catches the original scenario-wide deletion bug because 2020 disappears, but it does not verify replacement of the overlapping period. Add a window-dependent expected value or a focused writer test for 2030 replacement. 🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'system_test_run', | ||
| argvalues=myopic_stress_tests, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| REPLACE INTO metadata VALUES('DB_MAJOR',4,'DB major version number'); | ||
| REPLACE INTO metadata VALUES('DB_MINOR',0,'DB minor version number'); | ||
| REPLACE INTO metadata_real VALUES('global_discount_rate',0.05,NULL); | ||
| REPLACE INTO metadata_real VALUES('default_loan_rate',0.05,NULL); | ||
| REPLACE INTO region VALUES('region',NULL); | ||
| REPLACE INTO sector_label VALUES('energy',NULL); | ||
| REPLACE INTO commodity_type VALUES('s','source commodity'); | ||
| REPLACE INTO commodity_type VALUES('d','demand commodity'); | ||
| REPLACE INTO commodity VALUES('source','s',NULL,NULL); | ||
| REPLACE INTO commodity VALUES('demand','d',NULL,NULL); | ||
| REPLACE INTO technology_type VALUES('p','production technology'); | ||
| REPLACE INTO technology(tech,flag,sector) VALUES('generator','p','energy'); | ||
| REPLACE INTO time_period_type VALUES('e','existing vintages'); | ||
| REPLACE INTO time_period_type VALUES('f','future'); | ||
| REPLACE INTO time_period VALUES(0,2020,'f'); | ||
| REPLACE INTO time_period VALUES(1,2030,'f'); | ||
| REPLACE INTO time_period VALUES(2,2040,'f'); | ||
| REPLACE INTO time_period VALUES(3,2050,'f'); | ||
| REPLACE INTO time_season VALUES(0,'year',1.0,NULL); | ||
| REPLACE INTO time_of_day VALUES(0,'day',12.0,NULL); | ||
| REPLACE INTO time_of_day VALUES(1,'night',12.0,NULL); | ||
| REPLACE INTO efficiency VALUES('region','source','generator',2020,'demand',1.0,NULL,NULL); | ||
| REPLACE INTO capacity_to_activity VALUES('region','generator',8760.0,NULL,NULL); | ||
| REPLACE INTO lifetime_tech VALUES('region','generator',40.0,NULL,NULL); | ||
| REPLACE INTO cost_invest VALUES('region','generator',2020,1.0,NULL,NULL); | ||
| REPLACE INTO demand VALUES('region',2020,'demand',8760.0,NULL,NULL); | ||
| REPLACE INTO demand VALUES('region',2030,'demand',8760.0,NULL,NULL); | ||
| REPLACE INTO demand VALUES('region',2040,'demand',8760.0,NULL,NULL); | ||
| REPLACE INTO unit_commitment(region,tech,unit_capacity,min_output_fraction) | ||
| VALUES('region','generator',1.0,1.0); | ||
| REPLACE INTO unit_commitment_startup_cost VALUES('region','generator',1.0,NULL,NULL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: TemoaProject/temoa
Length of output: 14416
🏁 Script executed:
Repository: TemoaProject/temoa
Length of output: 32076
🏁 Script executed:
Repository: TemoaProject/temoa
Length of output: 5539
Use a stale row before the first future period.
The first base year is 2020, so
clear_results_after(2020)removes the proposed 2050 row. Add an existing 2010 period and move the first run's 2040 rows there before the second run. The foreign-key schema permits this row, while period cleanup does not remove it. The second-run assertion will then detect a brokenclear_old_results()implementation.Suggested test adjustment
+REPLACE INTO time_period VALUES(-1,2010,'e'); REPLACE INTO time_period VALUES(0,2020,'f');🤖 Prompt for AI Agents