Skip to content

Commit

Permalink
fix polyslab autograd when sidewall_angle or dilation
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerflex committed Jun 29, 2024
1 parent a69d2c4 commit 6000952
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
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 `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

0 comments on commit 6000952

Please sign in to comment.