Skip to content

Commit

Permalink
Fixing inf bounds handling in PolySlab
Browse files Browse the repository at this point in the history
  • Loading branch information
weiliangjin2021 authored and momchil-flex committed Sep 11, 2023
1 parent f94efba commit f4a5cff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
16 changes: 16 additions & 0 deletions tests/test_components/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import gdstk
import gdspy
import trimesh
import warnings

import tidy3d as td
from tidy3d.exceptions import SetupError, Tidy3dKeyError, ValidationError
Expand Down Expand Up @@ -243,10 +244,25 @@ def test_box_from_bounds():


def test_polyslab_center_axis():
"""Test the handling of center_axis in a polyslab having (-td.inf, td.inf) bounds."""
ps = POLYSLAB.copy(update=dict(slab_bounds=(-td.inf, td.inf)))
assert ps.center_axis == 0


@pytest.mark.parametrize(
"lower_bound, upper_bound", ((-td.inf, td.inf), (-1, td.inf), (-td.inf, 1))
)
def test_polyslab_inf_bounds(lower_bound, upper_bound):
"""Test the handling of various operations in a polyslab having inf bounds."""
ps = POLYSLAB.copy(update=dict(slab_bounds=(lower_bound, upper_bound)))
# catch any runtime warning related to inf operations
with warnings.catch_warnings():
warnings.simplefilter("error")
bounds = ps.bounds
ps.intersections_plane(x=0.5)
ps.intersections_plane(z=0)


def test_polyslab_bounds():
with pytest.raises(pydantic.ValidationError):
td.PolySlab(vertices=((0, 0), (1, 0), (1, 1)), slab_bounds=(0.5, -0.5), axis=2)
Expand Down
29 changes: 16 additions & 13 deletions tidy3d/components/geometry/polyslab.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,9 @@ def _intersections_normal(self, z: float):
For more details refer to
`Shapely's Documentaton <https://shapely.readthedocs.io/en/stable/project.html>`_.
"""
if isclose(self.sidewall_angle, 0):
return [shapely.Polygon(self.reference_polygon)]

z0 = self.center_axis
z_local = z - z0 # distance to the middle
dist = -z_local * self._tanq
Expand Down Expand Up @@ -590,19 +593,18 @@ def _intersections_side(self, position, axis) -> list:
h_length = h_top - h_base

# coordinate of each subsection
z_min, z_max = z_base + h_base, z_base + h_top

# vertices for the base of each subsection
# move up by `fp_eps` in case vertices are degenerate at the base.
dist = -(h_base - self.length_axis / 2 + fp_eps) * self._tanq
vertices = self._shift_vertices(self.middle_polygon, dist)[0]
z_min = z_base + h_base
z_max = np.inf if np.isposinf(h_top) else z_base + h_top

# for vertical sidewall, no need for complications
if isclose(self.sidewall_angle, 0):
ints_y, ints_angle = self._find_intersecting_ys_angle_vertical(
vertices, position, axis_ordered
self.reference_polygon, position, axis_ordered
)
else:
# for slanted sidewall, move up by `fp_eps` in case vertices are degenerate at the base.
dist = -(h_base - self.length_axis / 2 + fp_eps) * self._tanq
vertices = self._shift_vertices(self.middle_polygon, dist)[0]
ints_y, ints_angle = self._find_intersecting_ys_angle_slant(
vertices, position, axis_ordered
)
Expand Down Expand Up @@ -884,12 +886,13 @@ def bounds(self) -> Bound:

# check for the maximum possible contribution from dilation/slant on each side
max_offset = self.dilation
if self.reference_plane == "bottom":
max_offset += max(0, -self._tanq * self.length_axis)
elif self.reference_plane == "top":
max_offset += max(0, self._tanq * self.length_axis)
elif self.reference_plane == "middle":
max_offset += max(0, abs(self._tanq) * self.length_axis / 2)
if not isclose(self.sidewall_angle, 0):
if self.reference_plane == "bottom":
max_offset += max(0, -self._tanq * self.length_axis)
elif self.reference_plane == "top":
max_offset += max(0, self._tanq * self.length_axis)
elif self.reference_plane == "middle":
max_offset += max(0, abs(self._tanq) * self.length_axis / 2)

# special care when dilated
if max_offset > 0:
Expand Down

0 comments on commit f4a5cff

Please sign in to comment.