Skip to content

Commit f4a5cff

Browse files
weiliangjin2021momchil-flex
authored andcommitted
Fixing inf bounds handling in PolySlab
1 parent f94efba commit f4a5cff

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

tests/test_components/test_geometry.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import gdstk
99
import gdspy
1010
import trimesh
11+
import warnings
1112

1213
import tidy3d as td
1314
from tidy3d.exceptions import SetupError, Tidy3dKeyError, ValidationError
@@ -243,10 +244,25 @@ def test_box_from_bounds():
243244

244245

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

249251

252+
@pytest.mark.parametrize(
253+
"lower_bound, upper_bound", ((-td.inf, td.inf), (-1, td.inf), (-td.inf, 1))
254+
)
255+
def test_polyslab_inf_bounds(lower_bound, upper_bound):
256+
"""Test the handling of various operations in a polyslab having inf bounds."""
257+
ps = POLYSLAB.copy(update=dict(slab_bounds=(lower_bound, upper_bound)))
258+
# catch any runtime warning related to inf operations
259+
with warnings.catch_warnings():
260+
warnings.simplefilter("error")
261+
bounds = ps.bounds
262+
ps.intersections_plane(x=0.5)
263+
ps.intersections_plane(z=0)
264+
265+
250266
def test_polyslab_bounds():
251267
with pytest.raises(pydantic.ValidationError):
252268
td.PolySlab(vertices=((0, 0), (1, 0), (1, 1)), slab_bounds=(0.5, -0.5), axis=2)

tidy3d/components/geometry/polyslab.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ def _intersections_normal(self, z: float):
534534
For more details refer to
535535
`Shapely's Documentaton <https://shapely.readthedocs.io/en/stable/project.html>`_.
536536
"""
537+
if isclose(self.sidewall_angle, 0):
538+
return [shapely.Polygon(self.reference_polygon)]
539+
537540
z0 = self.center_axis
538541
z_local = z - z0 # distance to the middle
539542
dist = -z_local * self._tanq
@@ -590,19 +593,18 @@ def _intersections_side(self, position, axis) -> list:
590593
h_length = h_top - h_base
591594

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

600599
# for vertical sidewall, no need for complications
601600
if isclose(self.sidewall_angle, 0):
602601
ints_y, ints_angle = self._find_intersecting_ys_angle_vertical(
603-
vertices, position, axis_ordered
602+
self.reference_polygon, position, axis_ordered
604603
)
605604
else:
605+
# for slanted sidewall, move up by `fp_eps` in case vertices are degenerate at the base.
606+
dist = -(h_base - self.length_axis / 2 + fp_eps) * self._tanq
607+
vertices = self._shift_vertices(self.middle_polygon, dist)[0]
606608
ints_y, ints_angle = self._find_intersecting_ys_angle_slant(
607609
vertices, position, axis_ordered
608610
)
@@ -884,12 +886,13 @@ def bounds(self) -> Bound:
884886

885887
# check for the maximum possible contribution from dilation/slant on each side
886888
max_offset = self.dilation
887-
if self.reference_plane == "bottom":
888-
max_offset += max(0, -self._tanq * self.length_axis)
889-
elif self.reference_plane == "top":
890-
max_offset += max(0, self._tanq * self.length_axis)
891-
elif self.reference_plane == "middle":
892-
max_offset += max(0, abs(self._tanq) * self.length_axis / 2)
889+
if not isclose(self.sidewall_angle, 0):
890+
if self.reference_plane == "bottom":
891+
max_offset += max(0, -self._tanq * self.length_axis)
892+
elif self.reference_plane == "top":
893+
max_offset += max(0, self._tanq * self.length_axis)
894+
elif self.reference_plane == "middle":
895+
max_offset += max(0, abs(self._tanq) * self.length_axis / 2)
893896

894897
# special care when dilated
895898
if max_offset > 0:

0 commit comments

Comments
 (0)