Skip to content

Commit

Permalink
Fix corner filtering in LayerRefinementSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
weiliangjin2021 committed Mar 11, 2025
1 parent 4436ee0 commit b86c792
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

### Fixed
- Bug in `LayerRefinementSpec` that refines grids in the region outside the layer when one in-plane dimension has size infinity.

## [2.8.0] - 2025-03-04

Expand Down
6 changes: 4 additions & 2 deletions tests/test_components/test_layerrefinement.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,19 @@ def test_layerrefinement():
assert layer.size[axis] == 1
assert layer.size[(axis + 1) % 3] == td.inf
assert layer.size[(axis + 2) % 3] == td.inf
assert layer._is_inplane_unbounded
assert not layer._is_inplane_bounded

layer = LayerRefinementSpec.from_bounds(axis=axis, rmin=(0, 0, 0), rmax=(1, 2, 3))
layer = LayerRefinementSpec.from_bounds(rmin=(0, 0, 0), rmax=(1, 2, 3))
assert layer.axis == 0
assert np.isclose(layer.length_axis, 1)
assert np.isclose(layer.center_axis, 0.5)
assert not layer._is_inplane_unbounded
assert layer._is_inplane_bounded

# from structures
structures = [td.Structure(geometry=td.Box(size=(td.inf, 2, 3)), medium=td.Medium())]
layer = LayerRefinementSpec.from_structures(structures)
assert layer._is_inplane_bounded
assert layer.axis == 1

with pytest.raises(pydantic.ValidationError):
Expand All @@ -134,6 +135,7 @@ def test_layerrefinement():
def test_layerrefinement_inplane_inside():
# inplane inside
layer = LayerRefinementSpec.from_layer_bounds(axis=2, bounds=(0, 1))
assert not layer._is_inplane_bounded
assert layer._inplane_inside([3e3, 4e4])
layer = LayerRefinementSpec(axis=1, size=(1, 0, 1))
assert layer._inplane_inside([0, 0])
Expand Down
12 changes: 7 additions & 5 deletions tidy3d/components/grid/grid_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,9 +1226,11 @@ def center_axis(self) -> float:
return self.center[self.axis]

@cached_property
def _is_inplane_unbounded(self) -> bool:
"""Whether the layer is unbounded in any of the inplane dimensions."""
return np.isinf(self.size[(self.axis + 1) % 3]) or np.isinf(self.size[(self.axis + 2) % 3])
def _is_inplane_bounded(self) -> bool:
"""Whether the layer is bounded in at least one of the inplane dimensions."""
return np.isfinite(self.size[(self.axis + 1) % 3]) or np.isfinite(
self.size[(self.axis + 2) % 3]
)

def _unpop_axis(self, ax_coord: float, plane_coord: Any) -> CoordinateOptional:
"""Combine coordinate along axis with identical coordinates on the plane tangential to the axis.
Expand Down Expand Up @@ -1321,14 +1323,14 @@ def _corners(self, structure_list: List[Structure]) -> List[CoordinateOptional]:

# filter structures outside the layer
structures_intersect = structure_list
if not self._is_inplane_unbounded:
if self._is_inplane_bounded:
structures_intersect = [s for s in structure_list if self.intersects(s.geometry)]
inplane_points = self.corner_finder.corners(
self.axis, self.center_axis, structures_intersect
)

# filter corners outside the inplane bounds
if not self._is_inplane_unbounded:
if self._is_inplane_bounded:
inplane_points = [point for point in inplane_points if self._inplane_inside(point)]

# convert 2d points to 3d
Expand Down

0 comments on commit b86c792

Please sign in to comment.