Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix PolySlab in autograd gradient when sidewall_angle or dilation used #1792

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bug where boundary layers would be plotted too small in 2D simulations.
- Bug when plotting transformed geometries.
- Bug when snapping `CoaxialLumpedPort` to grid cell boundaries.
- Errors in `PolySlab` when using autograd differentiation with non-zero `sidewall_angle` and `dilation`.

## [2.7.0] - 2024-06-17

Expand Down
2 changes: 2 additions & 0 deletions tests/test_components/test_autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
13 changes: 7 additions & 6 deletions tidy3d/components/geometry/polyslab.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from copy import copy
from math import isclose
from typing import List, Tuple

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
Loading