{ "cells": [ { "cell_type": "markdown", "id": "ba89da7c", "metadata": {}, "source": [ "# Grid Serialization Examples\n", "\n", "Persist `Grid` instances as JSON, for example when you need reproducible fixtures or want to share datasets across services.\n", "These snippets focus on the developer-facing `Grid.serialize()` / `Grid.deserialize()` workflow.\n" ] }, { "cell_type": "markdown", "id": "cf2bb8db", "metadata": {}, "source": [ "## Build a Minimal Grid\n", "\n", "Start with a compact network so each serialization step stays readable." ] }, { "cell_type": "code", "execution_count": null, "id": "befa7972", "metadata": {}, "outputs": [], "source": [ "from power_grid_model_ds import Grid\n", "from power_grid_model_ds.arrays import LineArray, NodeArray, SourceArray, SymLoadArray\n", "from power_grid_model_ds.enums import NodeType\n", "\n", "grid = Grid.empty()\n", "\n", "substation = NodeArray(id=[1001], u_rated=[10_500.0], node_type=[NodeType.SUBSTATION_NODE.value])\n", "grid.append(substation, check_max_id=False)\n", "\n", "feeders = NodeArray(id=[1002, 1003], u_rated=[10_500.0] * 2, node_type=[NodeType.UNSPECIFIED.value] * 2)\n", "grid.append(feeders, check_max_id=False)\n", "\n", "lines = LineArray(\n", " id=[2001, 2002],\n", " from_node=[1001, 1002],\n", " to_node=[1002, 1003],\n", " from_status=[1, 1],\n", " to_status=[1, 1],\n", " i_n=[200.0, 200.0],\n", " r1=[0.08, 0.12],\n", " x1=[0.03, 0.04],\n", " c1=[0.0, 0.0],\n", " tan1=[0.0, 0.0],\n", ")\n", "grid.append(lines, check_max_id=False)\n", "\n", "load = SymLoadArray(\n", " id=[3001],\n", " node=[1003],\n", " type=[1],\n", " p_specified=[750_000.0],\n", " q_specified=[325_000.0],\n", " status=[1],\n", ")\n", "grid.append(load, check_max_id=False)\n", "\n", "source = SourceArray(id=[4001], node=[1001], status=[1], u_ref=[0.0])\n", "grid.append(source, check_max_id=False)\n", "\n", "grid.check_ids()\n", "grid" ] }, { "cell_type": "markdown", "id": "8478320b", "metadata": {}, "source": [ "## Serialize to JSON\n", "\n", "Use `Grid.serialize()` to dump the container. All JSON settings (indentation, ordering, ASCII handling, etc.) can be forwarded to `json.dump`." ] }, { "cell_type": "code", "execution_count": null, "id": "eef01dad", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "from tempfile import TemporaryDirectory\n", "\n", "workspace = TemporaryDirectory()\n", "json_path = Path(workspace.name) / \"grid_snapshot.json\"\n", "\n", "grid.serialize(json_path, indent=2)\n", "\n", "print(json_path.read_text())" ] }, { "cell_type": "markdown", "id": "68d013e1", "metadata": {}, "source": [ "## Deserialize and Validate\n", "\n", "Load the JSON as a Grid with `Grid.deserialize()` and compare it with the in-memory object to verify that every array survived the round trip.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e2578d9d", "metadata": {}, "outputs": [], "source": [ "reloaded_grid = Grid.deserialize(json_path)\n", "\n", "print(f\"Grids identical: {reloaded_grid == grid}\")\n", "print(f\"Nodes restored: {reloaded_grid.node.size}\")\n", "print(f\"Serialized file: {json_path}\")\n", "\n", "workspace.cleanup()" ] }, { "cell_type": "markdown", "id": "df87cd99", "metadata": {}, "source": [ "## PGM Compatibility\n", "\n", "Use `PowerGridModelInterface` when you need JSON that the core `power-grid-model` solver accepts directly. The sequence is simple: create the solver payload from your grid, dump it with `json_serialize_to_file()`, reload it back into a `Grid`." ] }, { "cell_type": "code", "execution_count": null, "id": "e404e517", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "from tempfile import TemporaryDirectory\n", "\n", "from power_grid_model.utils import json_serialize_to_file\n", "\n", "from power_grid_model_ds import PowerGridModelInterface\n", "\n", "compat_workspace = TemporaryDirectory()\n", "pgm_path = Path(compat_workspace.name) / \"pgm_input.json\"\n", "\n", "pgm_payload = PowerGridModelInterface(grid).create_input_from_grid()\n", "\n", "# Note: replace NaN values before serializing, otherwise json_serialize_to_file will drop those columns entirely.\n", "json_serialize_to_file(pgm_path, pgm_payload)\n", "\n", "restored_grid = Grid.deserialize(pgm_path)\n", "restored_payload = PowerGridModelInterface(restored_grid).create_input_from_grid()\n", "\n", "compat_workspace.cleanup()" ] } ], "metadata": { "kernelspec": { "display_name": ".venv (3.12.6)", "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" } }, "nbformat": 4, "nbformat_minor": 5 }