From 05d12e30878e985733fc0f122f5762bf881366b5 Mon Sep 17 00:00:00 2001 From: tylerflex Date: Thu, 4 Nov 2021 14:40:18 -0700 Subject: [PATCH 1/3] gds loading polyslab simple --- tidy3d/components/geometry.py | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tidy3d/components/geometry.py b/tidy3d/components/geometry.py index 55d5ae01c1..d860145411 100644 --- a/tidy3d/components/geometry.py +++ b/tidy3d/components/geometry.py @@ -13,6 +13,7 @@ from .types import Literal, Bound, Size, Coordinate, Axis, Coordinate2D from .types import Vertices, Ax, Shapely from .viz import add_ax_if_none +from ..log import Tidy3dKeyError # add this around extents of plots PLOT_BUFFER = 0.3 @@ -801,6 +802,67 @@ def set_center(cls, val, values): values["center"] = cls.unpop_axis(z0, (x0, y0), axis=values.get("axis")) return val + @classmethod + def from_gdspy( #pylint:disable=too-many-arguments + cls, + gds_cell, + gds_layer: int, + gds_dtype: int, + polygon_index: int, + axis: Axis, + slab_bounds: Tuple[float, float], + gds_scale: float = 1.0, + ): + """Import :class:`PolySlab` from a gdspy Cell. + + Parameters + ---------- + gds_cell : gdspy.Cell + gdspy.Cell containing 2D geometric data. + gds_layer : int + List of layer index. + gds_dtype : int + Data type index. + polygon_index : int = 0 + Index into the list of polygons at given gds_layer and gds_dtype. + axis : int + Integer index into the polygon's slab axis. (0,1,2) -> (x,y,z) + slab_bounds: Tuple[float, float] + Minimum and maximum positions of the slab along axis. + gds_scale : float = 1.0 + Length scale used in GDS file in units of micron. + For example, if gds file uses nanometers, set ``gds_scale=1e-3``. + + Returns + ------- + :class:`PolySlab` + Geometry with slab along ``axis`` and geometry defined by gds cell in plane. + """ + + vert_dict = gds_cell.get_polygons(by_spec=True) + try: + key = (gds_layer, gds_dtype) + list_of_vertices = vert_dict[key] + except Exception as e: + raise Tidy3dKeyError( + f"Can't load gds_cell, gds_layer={gds_layer} and gds_dtype={gds_dtype} not found. " + f"Found (layer, dtype) list of {list(vert_dict.keys())} in cell." + ) from e + + # select polygon index + try: + vertices = list_of_vertices[polygon_index] + except Exception as e: + raise Tidy3dError(f"no polygon vertices found at index {polygon_index}. " + f"{len(list_of_vertices)} polygons returned at " + f"gds_layer={gds_layer} and gds_dtype={gds_dtype}." + ) from e + + vertices *= gds_scale + vertices = vertices.tolist() + + return cls(vertices=vertices, axis=axis, slab_bounds=slab_bounds) + def inside(self, x, y, z) -> bool: # pylint:disable=too-many-locals """Returns True if point ``(x,y,z)`` inside volume of geometry. From 8445db34e6ef76b4fc5f95300d450463119759f3 Mon Sep 17 00:00:00 2001 From: tylerflex Date: Thu, 4 Nov 2021 15:35:41 -0700 Subject: [PATCH 2/3] finished gds --- PR.json | 21 -------------------- requirements.txt | 16 +++++++-------- tidy3d/components/geometry.py | 37 +++++++++++++++++++---------------- 3 files changed, 28 insertions(+), 46 deletions(-) delete mode 100644 PR.json diff --git a/PR.json b/PR.json deleted file mode 100644 index e473a34b83..0000000000 --- a/PR.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": null, - "frequency_range": [ - -1e+16, - 1e+16 - ], - "eps_inf": 1.0, - "poles": [ - [ - [ - 1.0, - 1.0 - ], - [ - 0.0, - 2.2 - ] - ] - ], - "type": "PoleResidue" -} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 91fd4edbf3..3850dda8be 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,16 +5,13 @@ h5py rich nlopt matplotlib - -# required to get xarray to not complain -dask - -# required for running tidy3d, may try to remove later +shapely +descartes pydantic PyYAML -# may add later -shapely +# required to get xarray to not complain +dask # required for code quality checking black @@ -31,4 +28,7 @@ nbsphinx ipython autodoc_pydantic sphinx-copybutton -m2r2 \ No newline at end of file +m2r2 + +# required for web +boto3 diff --git a/tidy3d/components/geometry.py b/tidy3d/components/geometry.py index d860145411..9e3936349e 100644 --- a/tidy3d/components/geometry.py +++ b/tidy3d/components/geometry.py @@ -803,35 +803,37 @@ def set_center(cls, val, values): return val @classmethod - def from_gdspy( #pylint:disable=too-many-arguments + def from_gdspy( # pylint:disable=too-many-arguments cls, gds_cell, - gds_layer: int, - gds_dtype: int, - polygon_index: int, axis: Axis, slab_bounds: Tuple[float, float], - gds_scale: float = 1.0, + gds_layer: int, + gds_dtype: int, + polygon_index: pydantic.NonNegativeInt = 1, + gds_scale: pydantic.PositiveFloat = 1.0, ): - """Import :class:`PolySlab` from a gdspy Cell. + """Import :class:`PolySlab` from a ``gdspy.Cell``. Parameters ---------- gds_cell : gdspy.Cell - gdspy.Cell containing 2D geometric data. + ``gdspy.Cell`` containing 2D geometric data. + axis : int + Integer index into the polygon's slab axis. (0,1,2) -> (x,y,z). + slab_bounds: Tuple[float, float] + Minimum and maximum positions of the slab along ``axis``. gds_layer : int - List of layer index. + Layer index in the ``gds_cell``. gds_dtype : int - Data type index. + Data type index in the ``gds_cell``. polygon_index : int = 0 - Index into the list of polygons at given gds_layer and gds_dtype. - axis : int - Integer index into the polygon's slab axis. (0,1,2) -> (x,y,z) - slab_bounds: Tuple[float, float] - Minimum and maximum positions of the slab along axis. + Index into the list of polygons at given ``gds_layer`` and ``gds_dtype``. + Must be non-negative. gds_scale : float = 1.0 Length scale used in GDS file in units of micron. For example, if gds file uses nanometers, set ``gds_scale=1e-3``. + Must be positive. Returns ------- @@ -853,9 +855,10 @@ def from_gdspy( #pylint:disable=too-many-arguments try: vertices = list_of_vertices[polygon_index] except Exception as e: - raise Tidy3dError(f"no polygon vertices found at index {polygon_index}. " - f"{len(list_of_vertices)} polygons returned at " - f"gds_layer={gds_layer} and gds_dtype={gds_dtype}." + raise Tidy3dKeyError( + f"no polygon vertices found at index {polygon_index}. " + f"{len(list_of_vertices)} polygons returned at " + f"gds_layer={gds_layer} and gds_dtype={gds_dtype}." ) from e vertices *= gds_scale From 5dc042b3c7dac18ce0a864ce7c92bbef1b29322d Mon Sep 17 00:00:00 2001 From: tylerflex Date: Thu, 4 Nov 2021 15:35:59 -0700 Subject: [PATCH 3/3] removed extraneous file --- Untitled.ipynb | 59 -------------------------------------------------- 1 file changed, 59 deletions(-) delete mode 100644 Untitled.ipynb diff --git a/Untitled.ipynb b/Untitled.ipynb deleted file mode 100644 index e203c71cf8..0000000000 --- a/Untitled.ipynb +++ /dev/null @@ -1,59 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 2, - "id": "cd7b162c-38b8-493f-87eb-91e6c263f0fe", - "metadata": {}, - "outputs": [], - "source": [ - "import sys; sys.path.append('.')\n", - "import tidy3d as td\n", - "from tidy3d.components.base import Tidy3dBaseModel" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "443f96c7-ae3a-4f6a-8963-e8e76abf7a76", - "metadata": {}, - "outputs": [], - "source": [ - "class Inf(Tidy3dBaseModel):\n", - "\n", - " self.sign : bool = True\n", - " self.shift : \n", - " def __neg__(self):\n", - " if self.is_positive:\n", - " self.is_positive = False\n", - " else:\n", - " self.is_positive = True\n", - " return self\n", - "\n", - " def __lt__(self, other):\n", - " return " - ] - } - ], - "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.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}