{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# 14. Energy Analysis\n\nCreate an OpenStudio energy model from one TopologicPy room and inspect its spaces. Model preparation is kept separate from a full annual simulation.\n\n**Goal:** Complete one small workflow. Run the cells from top to bottom in a fresh kernel.\n\n**Extra dependencies:** `openstudio==3.11.0`. No simulation engine is needed for the cells here.\n\nAll examples use metres unless stated otherwise.\n",
   "id": "tutorial-14-00"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "import os\nos.environ[\"TOPOLOGICPY_CORE_BACKEND\"] = \"pythonocc\"\n\nimport topologicpy\nfrom topologicpy.Core import Core\n\nassert topologicpy.__version__ == \"0.9.86\", \"Use TopologicPy 0.9.86 for these tutorials.\"\nassert type(Core.Backend()).__name__ == \"PythonOCCBackend\", \"Restart with the pythonocc backend.\"\nprint(\"TopologicPy\", topologicpy.__version__, \"—\", type(Core.Backend()).__name__)\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-14-01"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Make the analytical room\n\nThe tutorial uses a supplied office template and London weather/design-day files. Creating a model is quick; running an annual EnergyPlus simulation requires the OpenStudio command-line tools and is beyond this short introduction.\n",
   "id": "tutorial-14-02"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "from pathlib import Path\nfrom topologicpy.Cell import Cell\nfrom topologicpy.EnergyModel import EnergyModel\n\nroom = Cell.Prism(width=4, length=3, height=3, placement=\"bottom\")\ndata = Path(\"data\") / \"energy\"\nmodel = EnergyModel.ByTopology(\n    room,\n    osModelPath=str(data / \"office.osm\"),\n    weatherFilePath=str(data / \"london.epw\"),\n    designDayFilePath=str(data / \"london.ddy\"),\n    floorLevels=[0, 3],\n    glazingRatio=0.2,\n    buildingName=\"Tutorial Room\"\n)\nassert model is not None\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-14-03"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Inspect and save\n\nThe space count and total floor area verify the conversion. The OSM file can be opened in OpenStudio. It contains template assumptions that should be reviewed before any real analysis.\n",
   "id": "tutorial-14-04"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "spaces = model.getSpaces()\nfloor_area = 0\nfor space in spaces:\n    floor_area = floor_area + space.floorArea()\nprint(\"Spaces:\", len(spaces), \"Floor area:\", floor_area)\nassert len(spaces) == 1\nassert abs(floor_area - 12) < 0.001\noutput = Path(\"results\")\noutput.mkdir(exist_ok=True)\nassert EnergyModel.ExportToOSM(model, str(output / \"14_room.osm\"), overwrite=True)\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-14-05"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Try it yourself\n\nChange the room width and confirm the resulting floor area. Later, explore `EnergyModel.Run` with a configured OpenStudio CLI.\n",
   "id": "tutorial-14-06"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (TopologicPy tutorials)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
