From 8c884888ed4350b82e2f2a12fa611b167f1a36ab Mon Sep 17 00:00:00 2001 From: Tyler Hughes Date: Sat, 29 Jun 2024 05:12:33 -0400 Subject: [PATCH] fix polyslab autograd when sidewall_angle or dilation --- CHANGELOG.md | 1 + docs/notebooks | 2 +- tests/test_components/test_autograd.py | 2 ++ tidy3d/components/geometry/polyslab.py | 13 +++++++------ 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 439d780da6..a4d9ef100f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Bug where boundary layers would be plotted too small in 2D simulations. +- Errors in `PolySlab` when using autograd differentiation with `sidewall_angle` and `dilation`. ## [2.7.0] - 2024-06-17 diff --git a/docs/notebooks b/docs/notebooks index 9ce0df4863..87f8752516 160000 --- a/docs/notebooks +++ b/docs/notebooks @@ -1 +1 @@ -Subproject commit 9ce0df4863413d8b0137ad47c883173a529c3882 +Subproject commit 87f8752516ce392fa0fe14cc7d563d65f529ff29 diff --git a/tests/test_components/test_autograd.py b/tests/test_components/test_autograd.py index 0ee6d366b2..aebd7dc074 100644 --- a/tests/test_components/test_autograd.py +++ b/tests/test_components/test_autograd.py @@ -239,6 +239,8 @@ def make_structures(params: anp.ndarray) -> dict[str, td.Structure]: vertices=vertices, slab_bounds=(-0.5, 0.5), axis=1, + sidewall_angle=0.01, + dilation=0.01, ), medium=med, ) diff --git a/tidy3d/components/geometry/polyslab.py b/tidy3d/components/geometry/polyslab.py index 778230ed09..814da96549 100644 --- a/tidy3d/components/geometry/polyslab.py +++ b/tidy3d/components/geometry/polyslab.py @@ -2,6 +2,7 @@ from __future__ import annotations +from copy import copy from math import isclose from typing import List, Tuple @@ -1249,9 +1250,9 @@ def cross(u, v): def normalize(v): return v / np.linalg.norm(v, axis=0) - vs_orig = vertices.T.copy() - vs_next = np.roll(vs_orig.copy(), axis=-1, shift=-1) - vs_previous = np.roll(vs_orig.copy(), axis=-1, shift=+1) + vs_orig = copy(vertices.T) + vs_next = np.roll(copy(vs_orig), axis=-1, shift=-1) + vs_previous = np.roll(copy(vs_orig), axis=-1, shift=+1) asp = normalize(vs_next - vs_orig) asm = normalize(vs_orig - vs_previous) @@ -1289,14 +1290,14 @@ def _edge_length_and_reduction_rate(vertices: np.ndarray) -> Tuple[np.ndarray, n """ # edge length - vs_orig = vertices.T.copy() - vs_next = np.roll(vs_orig.copy(), axis=-1, shift=-1) + vs_orig = copy(vertices.T) + vs_next = np.roll(copy(vs_orig), axis=-1, shift=-1) edge_length = np.linalg.norm(vs_next - vs_orig, axis=0) # edge length remaining dist = 1 parallel_shift = PolySlab._shift_vertices(vertices, dist)[1] - parallel_shift_p = np.roll(parallel_shift.copy(), shift=-1) + parallel_shift_p = np.roll(copy(parallel_shift), shift=-1) edge_reduction = -(parallel_shift + parallel_shift_p) return edge_length, edge_reduction