{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# KLayout DRC plugin quickstart\n", "\n", "This module provides integration between [Tidy3D](https://docs.flexcompute.com/projects/tidy3d/en/latest/) and [KLayout](https://www.klayout.de)'s [Design Rule Check (DRC) engine](https://www.klayout.de/doc/manual/drc.html), allowing you to perform design rule checks on GDS files and Tidy3D objects.\n", "\n", "## Features\n", "\n", "- Run DRC on GDS files or Tidy3D objects ([Geometry](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Geometry.html), [Structure](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Structure.html#tidy3d.Structure), or [Simulation](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Simulation.html#tidy3d.Simulation)) with [DRCRunner.run()](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.drc.html#tidy3d.plugins.klayout.drc.DRCRunner.run).\n", "- Load DRC results into a [DRCResults](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.drc.html#tidy3d.plugins.klayout.drc.DRCResults) data structure with [DRCResults.load()](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.drc.html#tidy3d.plugins.klayout.drc.DRCResults).\n", "\n", "## Prerequisites:\n", "\n", "1. Have the full KLayout application [installed](https://www.klayout.de/build.html) and added to your system PATH.\n", "2. Provide a KLayout DRC [runset script](https://www.klayout.de/doc/manual/drc_runsets.html). A simple example script is included with this notebook.\n", "\n", "Please see the [README](https://github.com/flexcompute/tidy3d/blob/develop/tidy3d/plugins/klayout/drc/README.md) for more information."]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Imports:"]}, {"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\""]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Running DRC\n", "\n", "To run DRC, create an instance of [DRCRunner](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.drc.html#tidy3d.plugins.klayout.drc.DRCRunner) and use [DRCRunner.run()](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.drc.html#tidy3d.plugins.klayout.drc.DRCRunner.run).\n", "\n", "We can run DRC on a GDS layout file, or a Tidy3D [Geometry](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Geometry.html), [Structure](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Structure.html#tidy3d.Structure), or [Simulation](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Simulation.html#tidy3d.Simulation). In the case of a Tidy3D [Geometry](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Geometry.html), [Structure](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Structure.html#tidy3d.Structure), or [Simulation](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.Simulation.html#tidy3d.Simulation), the Tidy3D object will first be exported to a GDS file and then DRC will be ran on the exported file.\n", "\n", "The rules and operations to run are defined in a separate runset file, following the KLayout runset syntax. A simple example runset is included. Please refer to (the KLayout documentation) for more information.\n", "\n", "Here is a quick example of running DRC on a [PolySlab](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.PolySlab.html#tidy3d.PolySlab):"]}, {"cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [{"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\">12:28:40 EDT </span>INFO: Writing Tidy3D object to GDS file <span style=\"color: #008000; text-decoration-color: #008000\">'layout.gds'</span>.              \n", "</pre>\n"], "text/plain": ["\u001b[2;36m12:28:40 EDT\u001b[0m\u001b[2;36m \u001b[0mINFO: Writing Tidy3D object to GDS file \u001b[32m'layout.gds'\u001b[0m.              \n"]}, "metadata": {}, "output_type": "display_data"}, {"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>INFO: 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>\n"], "text/plain": ["\u001b[2;36m            \u001b[0m\u001b[2;36m \u001b[0mINFO: Running KLayout DRC on GDS file \u001b[32m'layout.gds'\u001b[0m with runset     \n", "\u001b[2;36m             \u001b[0m\u001b[32m'misc/drc_runset.drc'\u001b[0m and saving results to \u001b[32m'drc_results.lyrdb'\u001b[0m\u001b[33m...\u001b[0m \n"]}, "metadata": {}, "output_type": "display_data"}, {"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\">12:28:43 EDT </span>INFO: KLayout DRC completed successfully.                          \n", "</pre>\n"], "text/plain": ["\u001b[2;36m12:28:43 EDT\u001b[0m\u001b[2;36m \u001b[0mINFO: KLayout DRC completed successfully.                          \n"]}, "metadata": {}, "output_type": "display_data"}], "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)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Loading Results"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The output is a [DRCResults](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.drc.html#tidy3d.plugins.klayout.drc.DRCResults) object that contains the DRC rules and markers. The user can check if DRC passed with [DRCResults.is_clean](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.drc.html#tidy3d.plugins.klayout.drc.DRCResults.is_clean):"]}, {"cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["DRC did not pass!\n", "\n"]}], "source": ["if results.is_clean:\n", "    print(\"DRC passed!\\n\")\n", "else:\n", "    print(\"DRC did not pass!\\n\")"]}, {"cell_type": "markdown", "metadata": {}, "source": ["A summary of DRC violations can be printed:"]}, {"cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "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", "\n"]}], "source": ["print(results)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The names of all the checked categories can be retrieved with [DRCResults.categories](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.drc.html#tidy3d.plugins.klayout.drc.DRCResults.categories)"]}, {"cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [{"data": {"text/plain": ["('min_width', 'min_gap', 'min_area', 'min_hole')"]}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": ["results.categories"]}, {"cell_type": "markdown", "metadata": {}, "source": ["To get the markers for a specific category, index [DRCResults](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.drc.html#tidy3d.plugins.klayout.drc.DRCResults) by key:"]}, {"cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["2\n", "(EdgePairMarker(attrs={}, edge_pair=(((-1.717, -0.141), (-2.0, 0.0)), ((-2.0, 0.0), (-1.776, 0.224))), type='EdgePairMarker'), EdgePairMarker(attrs={}, edge_pair=(((2.0, 0.0), (1.717, -0.141)), ((1.776, 0.224), (2.0, 0.0))), type='EdgePairMarker'))\n"]}], "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)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Results can also be loaded from a KLayout DRC database file with [DRCResults.load(resultsfile)](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.plugins.klayout.drc.html#tidy3d.plugins.klayout.drc.DRCResults.load)"]}, {"cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "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", "\n"]}], "source": ["print(DRCResults.load(\"drc_results.lyrdb\"))"]}], "metadata": {"kernelspec": {"display_name": ".venv", "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.13.4"}, "title": "How to run klayout drc checks using tidy3d | flexcompute", "keywords": "design rule check, GDS verification, layout validation, Tidy3D, FDTD", "description": "This notebook demonstrates how to perform design rule checks on GDS files and simulation objects using Tidy3D."}, "nbformat": 4, "nbformat_minor": 2}