Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,29 +152,56 @@ uv run temoa tutorial # Create tutorial files
You can use Temoa as a Python library:

```python
import temoa
from logging import getLogger
from pathlib import Path
from temoa import TemoaModel, TemoaConfig, TemoaMode
from temoa._internal.temoa_sequencer import TemoaSequencer
from temoa.core.config import TemoaConfig
from temoa.core.modes import TemoaMode
from temoa.cli import setup_logging
from pyomo.environ import value, check_optimal_termination

# Create configuration
output_path = Path("output_files/my_run")
output_path.mkdir(parents=True, exist_ok=True)

setup_logging(output_path, debug=False, silent=False)

logger = getLogger(__name__)

# ======================================================================
# Choose ONE of the following two options for configuring the model run:
# ======================================================================

# OPTION 1: build the config in code
config = TemoaConfig(
scenario="my_scenario",
scenario="zulu",
scenario_mode=TemoaMode.PERFECT_FORESIGHT,
input_database=Path("path/to/input.db"),
output_database=Path("path/to/output.db"),
output_path=Path("path/to/output"),
solver_name="appsi_highs"
time_sequencing="seasonal_timeslices",
input_database="tutorial_database.sqlite",
output_database="tutorial_database.sqlite",
solver_name="appsi_highs",
output_path=output_path,
silent=False,
)
# OPTION 2: load the config from a file
config = TemoaConfig.build_config(
config_file=Path("tutorial_config.toml"),
output_path=output_path,
silent=False,
)

# Build and solve model
model = TemoaModel(config)
result = model.run() # Equivalent to: temoa run tutorial_config.toml
ts = TemoaSequencer(config=config)
ts.start() # TemoaSequencer runs the model and handles results

# Results are saved to your output_database but can also be accessed in code, here
solved_instance = ts.pf_solved_instance # solved Temoa instance with solution values
solver_results = ts.pf_results # information from the solver

# Check if run was successful
if result:
print("Model solved successfully!")
else:
print("Model failed to solve")
# Example code usage of the solved instance and solver results
logger.info(f"Solver results: {solver_results["solver"]}")
if check_optimal_termination(solver_results):
logger.info(f"Model solved! Objective value: {value(solved_instance.total_cost):.2f}")
built_capacity = value(solved_instance.v_new_capacity["utopia", "E51", 2010])
logger.info(f"Built {built_capacity:.2f} GW of storage in 2010")
```

## Database Setup
Expand Down
4 changes: 2 additions & 2 deletions temoa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _create_output_folder() -> Path:
return output_path


def _setup_logging(output_path: Path, debug: bool = False, silent: bool = False) -> None:
def setup_logging(output_path: Path, debug: bool = False, silent: bool = False) -> None:
"""Set up logging with different levels for console and file."""
# The root logger should be set to the most verbose level required by any handler.
# The file handler will always be more verbose than the console in silent mode.
Expand Down Expand Up @@ -90,7 +90,7 @@ def _setup_sequencer(
final_output_path.mkdir(parents=True, exist_ok=True)

# Pass the silent flag to the logging setup
_setup_logging(final_output_path, debug=debug, silent=silent)
setup_logging(final_output_path, debug=debug, silent=silent)

config = TemoaConfig.build_config(
config_file=config_file, output_path=final_output_path, silent=silent
Expand Down
2 changes: 1 addition & 1 deletion temoa/tutorial_assets/config_sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ solver_name = "appsi_highs"
save_excel = true

# save the duals in the output Database (may slow execution slightly?)
save_duals = true
save_duals = false

# save storage levels by time slice (may be a large amount of data)
save_storage_levels = true
Expand Down
Loading