Refactor in openTEPES.py: Increase flexibility and enable parallel executions #93
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.