|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +**GemsPy** is a Python interpreter for the GEMS (Generic Energy Modeling System) framework — a high-level modeling language for simulating energy systems under uncertainty. It allows users to define energy system models via YAML without writing solver code directly. |
| 8 | + |
| 9 | +## Commands |
| 10 | + |
| 11 | +**Install:** |
| 12 | +```bash |
| 13 | +pip install -r requirements.txt -r requirements-dev.txt |
| 14 | +``` |
| 15 | + |
| 16 | +**Test:** |
| 17 | +```bash |
| 18 | +pytest # run all tests |
| 19 | +pytest tests/path/to/test_file.py::test_name # run a single test |
| 20 | +pytest --cov gems --cov-report xml # with coverage |
| 21 | +``` |
| 22 | + |
| 23 | +**Lint & Format:** |
| 24 | +```bash |
| 25 | +black --config pyproject.toml src/ tests/ |
| 26 | +isort --profile black --filter-files src/ tests/ |
| 27 | +mypy |
| 28 | +``` |
| 29 | + |
| 30 | +**Running:** |
| 31 | +```bash |
| 32 | +# CLI entry point |
| 33 | +gemspy \ |
| 34 | + --model-libs path/to/lib1.yml path/to/lib2.yml \ |
| 35 | + --components path/to/components.yml \ |
| 36 | + --timeseries path/to/timeseries/ \ |
| 37 | + --duration 8760 \ |
| 38 | + --scenarios 1 |
| 39 | + |
| 40 | +# Python API (minimal example) |
| 41 | +from gems.main.main import build_problem |
| 42 | +``` |
| 43 | + |
| 44 | +## Architecture |
| 45 | + |
| 46 | +The pipeline flows: **YAML input → parsing → model resolution → network building → optimization problem → OR-Tools solver → results** |
| 47 | + |
| 48 | +### Core Modules (`src/gems/`) |
| 49 | + |
| 50 | +**`model/`** — Immutable model templates. |
| 51 | +- `Model`: defines component behavior (parameters, variables, constraints, ports) |
| 52 | +- `Library`: a collection of models, loaded from YAML |
| 53 | +- Models are never instantiated directly — they are referenced by components |
| 54 | + |
| 55 | +**`expression/`** — Mathematical expression language and AST. |
| 56 | +- `ExpressionNode`: base frozen dataclass for all expression tree nodes |
| 57 | +- Node types cover: arithmetic (`+`, `-`, `*`, `/`), comparisons (`<=`, `>=`, `==`), time/scenario operators (`time_sum()`), and functions (`max()`, `min()`, `ceil()`, `floor()`) |
| 58 | +- Grammar is defined in `/grammar/` and parsed via ANTLR4 (generated files live in `expression/parsing/antlr/`) |
| 59 | +- `ExpressionVisitor` is the dominant pattern for traversing and transforming expression trees (evaluation, linearization, printing, degree analysis) |
| 60 | +- Expressions support operator overloading: `var('x') + 5 * param('p')` |
| 61 | + |
| 62 | +**`study/`** — Study definition and network instantiation. |
| 63 | +- `System`: top-level structure parsed from YAML (before instantiation) |
| 64 | +- `Network`: instantiated graph of `Node`s, `Component`s, and connections |
| 65 | +- `Component`: an instance of a `Model` with concrete parameter values |
| 66 | +- `DataBase`: manages time series and scenario data |
| 67 | + |
| 68 | +**`simulation/`** — Optimization problem construction and solving. |
| 69 | +- `OptimizationProblem`: main interface; translates network + database into OR-Tools constraints |
| 70 | +- `LinearExpression`: the linearized form of model constraints used by the solver |
| 71 | +- `BendersDecomposedProblem`: temporal decomposition strategy for large problems |
| 72 | +- `TimeBlock`: structure for defining temporal decomposition |
| 73 | +- `OutputValues`: result extraction and formatting |
| 74 | + |
| 75 | +### Key Design Patterns |
| 76 | + |
| 77 | +- **Frozen dataclasses** throughout for immutability (models, expressions, constraints) |
| 78 | +- **Visitor pattern** for all expression tree operations (`ExpressionVisitor` subclasses) |
| 79 | +- **Indexing dimensions**: parameters and variables carry time and scenario indices explicitly; expressions track these automatically |
| 80 | +- **`ValueType`** enum (`INTEGER`, `CONTINUOUS`, `BOOLEAN`) for variable typing |
| 81 | + |
| 82 | +### Type Checking |
| 83 | + |
| 84 | +Strict mypy is enforced (`disallow_untyped_defs`, `disallow_untyped_calls`). All new code must be fully typed. Configuration is in `mypy.ini`. |
| 85 | + |
| 86 | +## Further Reading |
| 87 | + |
| 88 | +- [Python Convention](docs/agents/python-convention.md) — Code style, conventions, and agent guardrails |
| 89 | +- [Testing](docs/agents/testing.md) — Testing strategy and layer overview |
| 90 | +- [docs/getting-started.md](docs/getting-started.md) — Installation and first study walkthrough |
| 91 | +- [docs/user-guide.md](docs/user-guide.md) — Full user documentation |
| 92 | +- [docs/developer-guide.md](docs/developer-guide.md) — Contributor guide |
| 93 | +- [grammar/](grammar/) — ANTLR4 grammar source (`Expr.g4`) |
0 commit comments