{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# KLayout DRC plugin quickstart\n",
        "\n",
        "This module provides integration between\n",
        "[Tidy3D](https://docs.flexcompute.com/projects/tidy3d/en/latest/) and\n",
        "[KLayout](https://www.klayout.de)’s [Design Rule Check (DRC)\n",
        "engine](https://www.klayout.de/doc/manual/drc.html), allowing you to\n",
        "perform design rule checks on GDS files and Tidy3D objects.\n",
        "\n",
        "## Features\n",
        "\n",
        "- Run DRC on GDS files or Tidy3D objects\n",
        "  ([Geometry](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Geometry.html),\n",
        "  [Structure](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Structure.html#tidy3d.Structure),\n",
        "  or\n",
        "  [Simulation](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Simulation.html#tidy3d.Simulation))\n",
        "  with\n",
        "  [DRCRunner.run()](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.DRCRunner.html#tidy3d.plugins.klayout.DRCRunner.run).\n",
        "- Load DRC results into a\n",
        "  [DRCResults](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.DRCResults.html#tidy3d.plugins.klayout.DRCResults)\n",
        "  data structure with\n",
        "  [DRCResults.load()](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.DRCResults.html#tidy3d.plugins.klayout.DRCResults.load).\n",
        "\n",
        "## Prerequisites:\n",
        "\n",
        "1.  Have the full KLayout application\n",
        "    [installed](https://www.klayout.de/build.html) and added to your\n",
        "    system PATH.\n",
        "2.  Provide a KLayout DRC [runset\n",
        "    script](https://www.klayout.de/doc/manual/drc_runsets.html). A\n",
        "    simple example script is included with this notebook.\n",
        "\n",
        "Please see the\n",
        "[README](https://github.com/flexcompute/tidy3d/blob/develop/tidy3d/plugins/klayout/drc/README.md)\n",
        "for more information.\n",
        "\n",
        "### Imports:"
      ],
      "id": "f7219a8a-4ab4-41fb-8cb9-015e58afbe34"
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pathlib import Path\n",
        "\n",
        "import tidy3d as td\n",
        "from tidy3d.plugins.klayout.drc import DRCResults, DRCRunner\n",
        "\n",
        "td.config.logging.level = \"INFO\""
      ],
      "id": "15e71ec0"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Running DRC\n",
        "\n",
        "To run DRC, create an instance of\n",
        "[DRCRunner](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.DRCRunner.html#tidy3d.plugins.klayout.DRCRunner)\n",
        "and use\n",
        "[DRCRunner.run()](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.DRCRunner.html#tidy3d.plugins.klayout.DRCRunner.run).\n",
        "\n",
        "We can run DRC on a GDS layout file, or a Tidy3D\n",
        "[Geometry](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Geometry.html),\n",
        "[Structure](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Structure.html#tidy3d.Structure),\n",
        "or\n",
        "[Simulation](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Simulation.html#tidy3d.Simulation).\n",
        "In the case of a Tidy3D\n",
        "[Geometry](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Geometry.html),\n",
        "[Structure](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Structure.html#tidy3d.Structure),\n",
        "or\n",
        "[Simulation](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Simulation.html#tidy3d.Simulation),\n",
        "the Tidy3D object will first be exported to a GDS file and then DRC will\n",
        "be ran on the exported file.\n",
        "\n",
        "The rules and operations to run are defined in a separate runset file,\n",
        "following the KLayout runset syntax. A simple example runset is\n",
        "included. Please refer to the [KLayout\n",
        "documentation](https://www.klayout.de/doc/manual/drc_runsets.html) for\n",
        "more information.\n",
        "\n",
        "Here is a quick example of running DRC on a\n",
        "[PolySlab](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.PolySlab.html#tidy3d.PolySlab):"
      ],
      "id": "6c26fa23-19dc-4e57-bb0e-7aade4495c16"
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">13:54:11 UTC </span>Writing Tidy3D object to GDS file <span style=\"color: #008000; text-decoration-color: #008000\">'layout.gds'</span>.                    \n",
              "</pre>"
            ]
          }
        },
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">             </span>Running KLayout DRC on GDS file <span style=\"color: #008000; text-decoration-color: #008000\">'layout.gds'</span> with runset           \n",
              "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">             </span><span style=\"color: #008000; text-decoration-color: #008000\">'misc/drc_runset.drc'</span> and saving results to <span style=\"color: #008000; text-decoration-color: #008000\">'drc_results.lyrdb'</span><span style=\"color: #808000; text-decoration-color: #808000\">...</span> \n",
              "</pre>"
            ]
          }
        },
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">13:54:15 UTC </span>KLayout DRC completed successfully.                                \n",
              "</pre>"
            ]
          }
        }
      ],
      "source": [
        "# Create a simply polyslab geometry\n",
        "vertices = [(-2, 0), (-1, 1), (0, 0.5), (1, 1), (2, 0), (0, -1)]\n",
        "geom = td.PolySlab(vertices=vertices, slab_bounds=(0, 0.22), axis=2)\n",
        "\n",
        "# Run DRC\n",
        "runner = DRCRunner(drc_runset=Path(\"misc\") / \"drc_runset.drc\", verbose=True)\n",
        "results = runner.run(source=geom, z=0.1, gds_layer=0, gds_dtype=0)"
      ],
      "id": "6631354c"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Loading Results\n",
        "\n",
        "The output is a\n",
        "[DRCResults](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.DRCResults.html#tidy3d.plugins.klayout.DRCResults)\n",
        "object that contains the DRC rules and markers. The user can check if\n",
        "DRC passed with\n",
        "[DRCResults.is_clean](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.DRCResults.html#tidy3d.plugins.klayout.DRCResults.is_clean):"
      ],
      "id": "e5a214f5-6aab-433a-b56b-3d5c7812d665"
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "DRC did not pass!\n"
          ]
        }
      ],
      "source": [
        "if results.is_clean:\n",
        "    print(\"DRC passed!\\n\")\n",
        "else:\n",
        "    print(\"DRC did not pass!\\n\")"
      ],
      "id": "f2f715cc"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "A summary of DRC violations can be printed:"
      ],
      "id": "d1c31318-da2d-49e6-84e1-e1407bea0ba2"
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "DRC results summary\n",
            "--------------------------------\n",
            "Total violations: 3\n",
            "\n",
            "Violations by category:\n",
            "min_width: 2\n",
            "min_gap: 0\n",
            "min_area: 1\n",
            "min_hole: 0\n"
          ]
        }
      ],
      "source": [
        "print(results)"
      ],
      "id": "e4dad069"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The names of all the checked categories can be retrieved with\n",
        "[DRCResults.categories](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.DRCResults.html#tidy3d.plugins.klayout.DRCResults.categories)"
      ],
      "id": "1a429133-5e0c-4c19-b392-bed02a38b980"
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "('min_width', 'min_gap', 'min_area', 'min_hole')"
            ]
          }
        }
      ],
      "source": [
        "results.categories"
      ],
      "id": "b36d0f5c"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To get the markers for a specific category, index\n",
        "[DRCResults](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.DRCResults.html#tidy3d.plugins.klayout.DRCResults)\n",
        "by key:"
      ],
      "id": "83d6e8cb-0198-4356-8687-c3ea5f9efe86"
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "2\n",
            "(EdgePairMarker(cell='MAIN',edge_pair=(((-1.717, -0.141), (-2.0, 0.0)), ((-2.0, 0.0), (-1.776, 0.224))), symmetric=True), EdgePairMarker(cell='MAIN',edge_pair=(((2.0, 0.0), (1.717, -0.141)), ((1.776, 0.224), (2.0, 0.0))), symmetric=True))"
          ]
        }
      ],
      "source": [
        "# This will show how many violation shapes were found for the 'min_width' rule.\n",
        "print(results[\"min_width\"].count)\n",
        "\n",
        "# This will show all of the violation marker shapes for the 'min_width' rule\n",
        "print(results[\"min_width\"].markers)"
      ],
      "id": "361c6f46"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Results can also be loaded from a KLayout DRC database file with\n",
        "[DRCResults.load(resultsfile)](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.DRCResults.html#tidy3d.plugins.klayout.DRCResults.load)"
      ],
      "id": "5d628308-435f-461c-992d-ed1472087018"
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "DRC results summary\n",
            "--------------------------------\n",
            "Total violations: 3\n",
            "\n",
            "Violations by category:\n",
            "min_width: 2\n",
            "min_gap: 0\n",
            "min_area: 1\n",
            "min_hole: 0\n"
          ]
        }
      ],
      "source": [
        "print(DRCResults.load(\"drc_results.lyrdb\"))"
      ],
      "id": "539f3057"
    }
  ],
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    }
  }
}