From 1a10efe2e2f6219f5481e18ed5297adf76d03cac Mon Sep 17 00:00:00 2001 From: momchil Date: Thu, 23 Jun 2022 14:48:06 -0700 Subject: [PATCH] Making boundaries.to_dict and to_list return numpy arrays --- tests/test_sidewall.py | 2 +- tidy3d/components/grid/grid.py | 41 ++++++++++++++-------------------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/tests/test_sidewall.py b/tests/test_sidewall.py index 0a8883c25e..21538a9e91 100644 --- a/tests/test_sidewall.py +++ b/tests/test_sidewall.py @@ -35,7 +35,7 @@ def convert_valid_polygon(vertices): """ poly = Polygon(vertices).buffer(0) # make sure no intersecting edges if type(poly) is not Polygon: - poly = poly[0] + poly = poly.geoms[0] vertices_n = np.array(poly.exterior.coords[:]) return vertices_n diff --git a/tidy3d/components/grid/grid.py b/tidy3d/components/grid/grid.py index 4702053bc5..9ad54cc035 100644 --- a/tidy3d/components/grid/grid.py +++ b/tidy3d/components/grid/grid.py @@ -5,8 +5,8 @@ import numpy as np import pydantic as pd -from ..base import Tidy3dBaseModel, TYPE_TAG_STR -from ..types import ArrayLike, Axis +from ..base import Tidy3dBaseModel, TYPE_TAG_STR, cached_property +from ..types import ArrayLike, Axis, Array from ..geometry import Box from ...log import SetupError @@ -38,10 +38,15 @@ class Coords(Tidy3dBaseModel): ..., title="Z Coordinates", description="1-dimensional array of z coordinates." ) - @property + @cached_property + def to_dict(self): + """Return a dict of the three Coord1D objects as numpy arrays.""" + return {key: np.array(value) for key, value in self.dict(exclude={TYPE_TAG_STR}).items()} + + @cached_property def to_list(self): - """Return a list of the three Coord1D objects.""" - return list(self.dict(exclude={TYPE_TAG_STR}).values()) + """Return a list of the three Coord1D objects as numpy arrays.""" + return list(self.to_dict.values()) class FieldGrid(Tidy3dBaseModel): @@ -137,15 +142,13 @@ class Grid(Tidy3dBaseModel): ) @staticmethod - def _avg(coords1d: Coords1D): + def _avg(coords1d: Array[float]): """Return average positions of an array of 1D coordinates.""" - coords1d = np.array(coords1d) return (coords1d[1:] + coords1d[:-1]) / 2.0 @staticmethod - def _min(coords1d: Coords1D): + def _min(coords1d: Array[float]): """Return minus positions of 1D coordinates.""" - coords1d = np.array(coords1d) return coords1d[:-1] @property @@ -166,12 +169,7 @@ def centers(self) -> Coords: >>> grid = Grid(boundaries=coords) >>> centers = grid.centers """ - return Coords( - **{ - key: self._avg(val) - for key, val in self.boundaries.dict(exclude={TYPE_TAG_STR}).items() - } - ) + return Coords(**{key: self._avg(val) for key, val in self.boundaries.to_dict.items()}) @property def sizes(self) -> Coords: @@ -191,12 +189,7 @@ def sizes(self) -> Coords: >>> grid = Grid(boundaries=coords) >>> sizes = grid.sizes """ - return Coords( - **{ - key: np.diff(val) - for key, val in self.boundaries.dict(exclude={TYPE_TAG_STR}).items() - } - ) + return Coords(**{key: np.diff(val) for key, val in self.boundaries.to_dict.items()}) @property def num_cells(self) -> Tuple[int, int, int]: @@ -298,7 +291,7 @@ def __getitem__(self, coord_key: str) -> Coords: def _yee_e(self, axis: Axis): """E field yee lattice sites for axis.""" - boundary_coords = self.boundaries.dict(exclude={TYPE_TAG_STR}) + boundary_coords = self.boundaries.to_dict # initially set all to the minus bounds yee_coords = {key: self._min(val) for key, val in boundary_coords.items()} @@ -312,7 +305,7 @@ def _yee_e(self, axis: Axis): def _yee_h(self, axis: Axis): """H field yee lattice sites for axis.""" - boundary_coords = self.boundaries.dict(exclude={TYPE_TAG_STR}) + boundary_coords = self.boundaries.to_dict # initially set all to centers yee_coords = {key: self._avg(val) for key, val in boundary_coords.items()} @@ -393,7 +386,7 @@ def periodic_subspace(self, axis: Axis, ind_beg: int = 0, ind_end: int = 0) -> C The subspace of the grid along ``axis``. """ - coords = np.array(self.boundaries.to_list[axis]) + coords = self.boundaries.to_list[axis] padded_coords = coords num_coords = coords.size num_cells = num_coords - 1