Skip to content

Commit

Permalink
CustomGridBoundaries to precisely replicate grids from a simulation o…
Browse files Browse the repository at this point in the history
…f identical size
  • Loading branch information
weiliangjin2021 committed Jul 17, 2024
1 parent f82685e commit 37e22bc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
8 changes: 0 additions & 8 deletions tidy3d/components/data/monitor_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,14 +684,6 @@ def dot(
# Drop size-1 dimensions in the other data
fields_other = {key: field.squeeze(drop=True) for key, field in fields_other.items()}

# alignment for very close coordinate
for key, field in fields_other.items():
for coord in "xyz":
if coord in field.dims:
fields_self.update(
{key: fields_self[key].reindex(**{coord: field[coord]}, method="nearest")}
)

# Cross products of fields
dim1, dim2 = self._tangential_dims
e_self_x_h_other = fields_self["E" + dim1] * fields_other["H" + dim2]
Expand Down
51 changes: 50 additions & 1 deletion tidy3d/components/grid/grid_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,47 @@ def _make_coords_initial(
return center - size / 2 + np.arange(num_cells + 1) * dl_snapped


class CustomGridBoundaries(GridSpec1d):
"""Custom 1D grid supplied as a list of grid cell boundary coordinates. The grid must span
exactly the simulation domain size along the dimension, including PML layers, symmetric regions.
Example
-------
>>> grid_1d = CustomGridCoords(boundaries=[-0.2, 0.0, 0.2, 0.4, 0.5, 0.6, 0.7])
"""

coords: Coords1D = pd.Field(
...,
title="Grid boundary coordinates",
description="Grid boundariy coordinate which must span exactly the simulation domain size, "
"including PML layers. It is mostly used to duplicate grids to another simulation of the same size.",
units=MICROMETER,
)

def _make_coords_initial(
self,
axis: Axis,
structures: List[StructureType],
**kwargs,
) -> Coords1D:
"""Unused. Override ``make_coords`` directly as the
grid coordiate for the complete simulation domain is provided already.
"""

def make_coords(
self,
axis: Axis,
structures: List[StructureType],
symmetry: Tuple[Symmetry, Symmetry, Symmetry],
periodic: bool,
wavelength: pd.PositiveFloat,
num_pml_layers: Tuple[pd.NonNegativeInt, pd.NonNegativeInt],
snapping_points: Tuple[Coordinate, ...],
) -> Coords1D:
"""Generate 1D coords to be used as grid boundaries."""
return self.coords


class CustomGrid(GridSpec1d):
"""Custom 1D grid supplied as a list of grid cell sizes centered on the simulation center.
Expand Down Expand Up @@ -454,7 +495,7 @@ def _make_coords_initial(
return np.array(bound_coords)


GridType = Union[UniformGrid, CustomGrid, AutoGrid]
GridType = Union[UniformGrid, CustomGrid, AutoGrid, CustomGridBoundaries]


class GridSpec(Tidy3dBaseModel):
Expand Down Expand Up @@ -657,6 +698,14 @@ def make_grid(
coords = Coords(**coords_dict)
return Grid(boundaries=coords)

@classmethod
def imported(cls, grid: Grid) -> GridSpec:
"""Import from another simulation's grids."""
grid_dict = {}
for dim in "xyz":
grid_dict["grid_" + dim] = CustomGridBoundaries(coords=grid.boundaries.to_dict[dim])
return cls(**grid_dict)

@classmethod
def auto(
cls,
Expand Down
8 changes: 2 additions & 6 deletions tidy3d/components/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from .geometry.utils import flatten_groups, traverse_geometries
from .geometry.utils_2d import get_bounds, get_thickened_geom, snap_coordinate_to_grid, subdivide
from .grid.grid import Coords, Coords1D, Grid
from .grid.grid_spec import AutoGrid, CustomGrid, GridSpec, UniformGrid
from .grid.grid_spec import AutoGrid, GridSpec, UniformGrid
from .lumped_element import LumpedElementType
from .medium import (
AbstractCustomMedium,
Expand Down Expand Up @@ -4260,11 +4260,7 @@ def subsection(
elif isinstance(grid_spec, str) and grid_spec == "identical":
# create a custom grid from existing one
grids_1d = self.grid.boundaries.to_list
new_grids = [
CustomGrid(dl=tuple(np.diff(grids_1d[dim])), custom_offset=grids_1d[dim][0])
for dim in range(3)
]
grid_spec = GridSpec(grid_x=new_grids[0], grid_y=new_grids[1], grid_z=new_grids[2])
grid_spec = GridSpec.imported(self.grid)

# adjust region bounds to perfectly coincide with the grid
# note, sometimes (when a box already seems to perfrecty align with the grid)
Expand Down

0 comments on commit 37e22bc

Please sign in to comment.