{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "829eea26-3547-4d7a-9865-9e4a132f73ac",
   "metadata": {},
   "source": [
    "# Simulate '%cell%'\n",
    "> source: %source%"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "715620b9-6831-4d0a-8ae8-c7622ea724ef",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "232860d3-ba12-41ca-a7f2-9c19052b83b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from pathlib import Path\n",
    "\n",
    "import yaml\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import sax\n",
    "import %name%\n",
    "import gdsfactoryplus as gfp\n",
    "LIB_PATH = Path(%name%.__file__).resolve().parent"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c56961f9-752d-4634-91bc-4e257a8677ea",
   "metadata": {},
   "source": [
    "## Pdk Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "939ed8c1-2d58-4206-ac2b-93402b957ec0",
   "metadata": {},
   "outputs": [],
   "source": [
    "pdk=gfp.get_pdk()\n",
    "gfp.register_cells()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51631e45-6057-4995-8a88-8ec84ef136b6",
   "metadata": {},
   "source": [
    "## Extract Netlist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6aa23d4-384f-49bf-bc48-7aa7e1c124b2",
   "metadata": {},
   "outputs": [],
   "source": %extractNetlist%
  },
  {
   "cell_type": "markdown",
   "id": "47e3374d-56b3-4e6a-b1bf-2f8bc8d73bf5",
   "metadata": {},
   "source": [
    "## Create SAX Circuit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08be5c09-0cf4-4cf4-b657-e6c8887feeac",
   "metadata": {},
   "outputs": [],
   "source": [
    "circuit, circuit_info = sax.circuit(netlist, models=pdk.models)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d777141-ecfe-4558-8445-47cae8c733fd",
   "metadata": {},
   "source": [
    "## Simulate and get S-Dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ca1e2e8-f628-4030-960e-b8289e7cd703",
   "metadata": {},
   "outputs": [],
   "source": [
    "wl = np.linspace(%wl_min%, %wl_max%, %wl_num%)\n",
    "result = circuit(wl=wl) # you can add extra simulation arguments here...\n",
    "sdict = sax.sdict(result)  # ensure the result is in S-Dictionary format.\n",
    "\n",
    "# optional: filter out zero entries from sdict:\n",
    "sdict = {k: v for k, v in sdict.items() if not (v < 1e-5).all()}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07a1b0a0-bb99-4a0c-845e-038923371bed",
   "metadata": {},
   "source": [
    "## Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eaa55848-a141-481e-902d-13684bbefa5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, (ax_power, ax_phase) = plt.subplots(\n",
    "    nrows=2,\n",
    "    ncols=1,\n",
    "    # sharex=True, # use this if you want both plots to share the x-axis\n",
    "    tight_layout=True,\n",
    ")\n",
    "\n",
    "ax_power.set_title(\"Power\")\n",
    "ax_power.grid(True)\n",
    "ax_power.set_xlabel(\"λ [μm]\")\n",
    "ax_power.set_ylabel(\"Power [dB]\")\n",
    "for (p_in, p_out), value in sdict.items():\n",
    "    ax_power.plot(wl, 20 * np.log10(abs(value)), label=f\"{p_in}↔{p_out}\")\n",
    "\n",
    "ax_phase.set_title(\"Phase\")\n",
    "ax_phase.grid(True)\n",
    "ax_phase.set_xlabel(\"λ [μm]\")\n",
    "ax_phase.set_ylabel(\"Phase [rad]\")\n",
    "for (p_in, p_out), value in sdict.items():\n",
    "    ax_phase.plot(wl, np.unwrap(np.angle(value)))\n",
    "\n",
    "# you can move the legend with bbox_to_anchor:\n",
    "plt.figlegend(bbox_to_anchor=(1.0, 1.0), framealpha=1)\n",
    "# plt.savefig(\"simulate_%cell%.png\", bbox_inches=\"tight\") # uncomment to save figure as well\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "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.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
