Skip to content
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

Feature/sim bench #53

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
98 changes: 98 additions & 0 deletions docs/examples/sim_bench_example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "96e4fce1",
"metadata": {},
"outputs": [],
"source": [
"# Imports\n",
"from pathlib import Path\n",
"from power_grid_model import PowerGridModel\n",
"from power_grid_model_io.converters.pgm_json_converter import PgmJsonConverter\n",
"from power_grid_model_io.converters.sim_bench_converter import SimBenchConverter"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf28c2c6",
"metadata": {},
"outputs": [],
"source": [
"# Load source\n",
"simbench_code = \"1-complete_data-mixed-all-0-sw\"\n",
"simbench_converter = SimBenchConverter(simbench_code)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fcedddf5",
"metadata": {},
"outputs": [],
"source": [
"# Convert to PGM\n",
"input_data, extra_info = simbench_converter.load_input_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "237b3557",
"metadata": {},
"outputs": [],
"source": [
"# Store the source data in JSON format\n",
"converter = PgmJsonConverter(destination_file=f\"data/sim-bench/{simbench_code}_input.json\")\n",
"converter.save(data=input_data, extra_info=extra_info)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3cf1dab7",
"metadata": {},
"outputs": [],
"source": [
"# Perform power flow calculation\n",
"grid = PowerGridModel(input_data=input_data)\n",
"output_data = grid.calculate_power_flow()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "315649ed",
"metadata": {},
"outputs": [],
"source": [
"# Store the result data in JSON format\n",
"converter = PgmJsonConverter(destination_file=f\"data/sim-bench/{simbench_code}_output.json\")\n",
"converter.save(data=output_data, extra_info=extra_info)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 3 additions & 0 deletions docs/examples/sim_bench_example.ipynb.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2022 Contributors to the Power Grid Model project <dynamic.grid.calculation@alliander.com>

SPDX-License-Identifier: MPL-2.0
25 changes: 25 additions & 0 deletions src/power_grid_model_io/config/csv/sim_bench.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: 2022 Contributors to the Power Grid Model project <dynamic.grid.calculation@alliander.com>
#
# SPDX-License-Identifier: MPL-2.0
---
grid:
Node:
node:
id:
auto_id:
key: id
Line:
line:
id:
auto_id:
key: id
from_node:
auto_id:
table: Node
key:
id: nodeA
to_node:
auto_id:
table: Node
key:
id: nodeB
51 changes: 51 additions & 0 deletions src/power_grid_model_io/converters/sim_bench_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# SPDX-FileCopyrightText: 2022 Contributors to the Power Grid Model project <dynamic.grid.calculation@alliander.com>
#
# SPDX-License-Identifier: MPL-2.0
"""
Sim Bench Converter: Download, extract and convert a sim bench dataset to PGM
"""

from pathlib import Path
from tempfile import gettempdir
from typing import Optional

from power_grid_model_io.converters.tabular_converter import TabularConverter
from power_grid_model_io.data_stores.csv_dir_store import CsvDirStore
from power_grid_model_io.data_types import TabularData
from power_grid_model_io.utils.download import download_and_extract

DEFAULT_MAPPING_FILE = Path(__file__).parent.parent / "config" / "csv" / "sim_bench.yaml"
DEFAULT_DOWNLOAD_URL = "http://141.51.193.167/simbench/gui/usecase/download/?simbench_code={simbench_code:s}&format=csv"


class SimBenchConverter(TabularConverter):
"""
Sim Bench Converter: Download, extract and convert a sim bench dataset to PGM
"""

__slots__ = ("_simbench_code", "_download_url", "_download_dir")

def __init__(self, simbench_code: Optional[str] = None, download_dir: Optional[Path] = None):
super().__init__(mapping_file=DEFAULT_MAPPING_FILE)
self.simbench_code: Optional[str] = simbench_code
self._download_url: Optional[str] = None
self._download_dir: Path = download_dir or Path(gettempdir())
if simbench_code is not None:
self.set_download_url(DEFAULT_DOWNLOAD_URL.format(simbench_code=simbench_code))

def set_download_url(self, url: str):
"""
Use a custom sim bench URL
"""
self._download_url = url

def _load_data(self, data: Optional[TabularData]) -> TabularData:
if data is None and self._source is None and self._download_url is not None:
try:
csv_dir = download_and_extract(self._download_url, dir_path=self._download_dir)
except ValueError as ex:
if str(ex).endswith(".download"):
raise ValueError(f"Invalid SimBench dataset URL: {self._download_url}") from None
raise ex
self._source = CsvDirStore(csv_dir, delimiter=";")
return super()._load_data(data=data)