Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
complex polyslab autograd
Browse files Browse the repository at this point in the history
tylerflex committed Jun 27, 2024
1 parent 3ab33eb commit bf7b50c
Showing 5 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Support for differentiation with respect to `GeometryGroup.geometries` elements.
- Support for differentiation with respect to `ComplexPolySlab.vertices`.
- Users can now export `SimulationData` to MATLAB `.mat` files with the `to_mat_file` method.

### Changed
2 changes: 1 addition & 1 deletion docs/notebooks
30 changes: 29 additions & 1 deletion tests/test_components/test_autograd.py
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
import numpy as np
import pytest
import tidy3d as td
from tidy3d.plugins.polyslab import ComplexPolySlab
from tidy3d.web import run_async
from tidy3d.web.api.autograd.autograd import run

@@ -255,6 +256,31 @@ def make_structures(params: anp.ndarray) -> dict[str, td.Structure]:
medium=td.Medium(permittivity=eps, conductivity=conductivity),
)

# complex polyslab
polyslab_combined = ComplexPolySlab(
vertices=(
(-eps, 0),
(-eps, eps),
(0, eps / 10),
(eps, eps),
(eps, 0),
),
slab_bounds=(-0.5, 0.5),
axis=1,
sidewall_angle=np.pi / 100,
)

polyslab_geometries = []
for sub_polyslab in polyslab_combined.sub_polyslabs:
polyslab_geometries.append(sub_polyslab)

assert len(polyslab_geometries) >= 2, "need more polyslabs for a proper test of ComplexPolySlab"

complex_polyslab_geo_group = td.Structure(
geometry=td.GeometryGroup(geometries=polyslab_geometries),
medium=td.Medium(permittivity=eps, conductivity=conductivity),
)

return dict(
medium=medium,
center_list=center_list,
@@ -263,6 +289,7 @@ def make_structures(params: anp.ndarray) -> dict[str, td.Structure]:
custom_med_vec=custom_med_vec,
polyslab=polyslab,
geo_group=geo_group,
complex_polyslab=complex_polyslab_geo_group,
)


@@ -350,6 +377,7 @@ def plot_sim(sim: td.Simulation, plot_eps: bool = False) -> None:
"custom_med_vec",
"polyslab",
"geo_group",
"complex_polyslab",
)
monitor_keys_ = ("mode", "diff", "field_vol", "field_point")

@@ -370,7 +398,7 @@ def plot_sim(sim: td.Simulation, plot_eps: bool = False) -> None:
args = [("polyslab", "mode")]


# args = [("geo_group", "mode")]
args = [("complex_polyslab", "mode")]


def get_functions(structure_key: str, monitor_key: str) -> typing.Callable:
13 changes: 7 additions & 6 deletions tidy3d/components/geometry/polyslab.py
Original file line number Diff line number Diff line change
@@ -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

5 changes: 3 additions & 2 deletions tidy3d/plugins/autograd/README.md
Original file line number Diff line number Diff line change
@@ -111,6 +111,7 @@ The following components are traceable as inputs to the `td.Simulation`

- `Box.center`
- `Box.size`

- `PolySlab.vertices`

- `Medium.permittivity`
@@ -121,6 +122,8 @@ The following components are traceable as inputs to the `td.Simulation`

- `GeometryGroup.geometries`

- `ComplexPolySlab.vertices`

The following components are traceable as outputs of the `td.SimulationData`

- `ModeData.amps`
@@ -149,8 +152,6 @@ Next on our roadmap (targeting 2.8 and 2.9, summer 2024) is to support:
- `PoleResidue` and other dispersive models.
- custom (spatially-dependent) dispersive models, allowing topology optimization with metals.

- `ComplexPolySlab`

Later this year (2024), we plan to support:

- `TriangleMesh`.

0 comments on commit bf7b50c

Please sign in to comment.