Skip to content

Conversation

@erikfilias
Copy link
Contributor

@erikfilias erikfilias commented Aug 4, 2025

This pull request introduces a major refactoring of the openTEPES.py file, aimed at improving the modularity, readability, and maintainability of the code. These changes also lay the groundwork for future enhancements, such as the implementation of parallel execution.

Key Changes:

  • Decomposition of openTEPES_run: The main openTEPES_run function has been broken down into a series of smaller, single-purpose functions. This makes the overall workflow easier to follow and reduces the cognitive load of understanding the entire process.

  • Introduction of Helper Functions: A number of new helper functions have been introduced (e.g., _build_model, _configure_formulation_flags, _process_stage) that encapsulate specific logic. This promotes code reuse and simplifies the main execution path.

  • Centralized Configuration: The configuration of model formulation and output results is now handled through dedicated functions and data structures (_STAGE_FORMULATIONS, _OUTPUT_FUNCS). This makes it easier to add, remove, or modify stages and output formats without altering the core logic.

  • Refactored Main Loop: The primary loop that iterates over periods, scenarios, and stages has been extracted into its own function (process_stage_loop). This isolates the iteration logic and makes it easier to manage.

Benefits of this Refactoring:

  • Improved Readability and Maintainability: By breaking down the monolithic openTEPES_run function, the code is now more organized and easier to read. This will make it simpler for developers to maintain and debug the code in the future.

  • Enhanced Extensibility: The new modular design makes it easier to add new features or modify existing ones. For example, adding a new formulation step now only requires adding a new function to the _STAGE_FORMULATIONS list.

  • Enabler for Parallel Execution: While this PR does not implement parallel processing, the refactoring is a critical first step. By encapsulating the logic for processing a single stage, it is now possible to parallelize the process_stage_loop to run scenarios or stages concurrently. This will be crucial for improving performance on large-scale models.

This refactoring does not change the functionality of the model but provides a much-needed structural improvement that will benefit the parallel executions and project's long-term development.

@erikfilias erikfilias marked this pull request as draft August 4, 2025 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants