Skip to content

Commit 382f204

Browse files
committed
fix[adjoint]: Pass correct frequency for epsilon calculation in adjoint postprocessing
1 parent eeaa764 commit 382f204

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
### Fixed
1616
- Compatibility with `xarray>=2025.03`.
17+
- Inaccurate gradient when auto-grabbing permittivities for structures using `td.PolySlab` when using dispersive material models.
1718

1819
## [2.8.1] - 2025-03-20
1920

tests/test_components/test_autograd.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,17 @@ def make_structures(params: anp.ndarray) -> dict[str, td.Structure]:
380380
medium=med,
381381
)
382382

383+
polyslab_dispersive = td.Structure(
384+
geometry=td.PolySlab(
385+
vertices=vertices,
386+
slab_bounds=slab_bounds,
387+
axis=POLYSLAB_AXIS,
388+
sidewall_angle=0.00,
389+
dilation=0.00,
390+
),
391+
medium=td.material_library["Si3N4"]["Philipp1973Sellmeier"],
392+
)
393+
383394
# geometry group
384395
geo_group = td.Structure(
385396
geometry=td.GeometryGroup(
@@ -469,6 +480,7 @@ def make_structures(params: anp.ndarray) -> dict[str, td.Structure]:
469480
custom_med=custom_med,
470481
custom_med_vec=custom_med_vec,
471482
polyslab=polyslab,
483+
polyslab_dispersive=polyslab_dispersive,
472484
geo_group=geo_group,
473485
complex_polyslab=complex_polyslab_geo_group,
474486
pole_res=pole_res,
@@ -2117,3 +2129,24 @@ def objective(params):
21172129

21182130
grad_no_flux_monitors = ag.grad(objective_with_monitors(monitors_just_field))(params0)
21192131
grad_with_flux_monitors = ag.grad(objective_with_monitors(monitors_with_flux))(params0)
2132+
2133+
2134+
def test_dispersive_no_inf(use_emulated_run):
2135+
"""Test that automatic permittivity grabbing uses the correct freq_adj to
2136+
retrieve permittivity in dispersive material models.
2137+
"""
2138+
2139+
fn_dict = get_functions(args[0][0], args[0][1])
2140+
make_sim = fn_dict["sim"]
2141+
postprocess = fn_dict["postprocess"]
2142+
2143+
def objective(args):
2144+
structure_traced = make_structures(args)["polyslab_dispersive"]
2145+
sim = make_sim(args).updated_copy(structures=[structure_traced])
2146+
sim_data = run(sim, task_name="adjoint_test", verbose=False)
2147+
return postprocess(sim_data)
2148+
2149+
# the following will raise a warning (and fail) if the dispersive material
2150+
# model is called without a frequency
2151+
with AssertLogLevel("INFO"):
2152+
grad = ag.grad(objective)(params0)

tidy3d/web/api/autograd/autograd.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,9 @@ def postprocess_adj(
10101010
structs_no_struct = list(sim_orig.structures)
10111011
structs_no_struct.pop(structure_index)
10121012
sim_no_structure = sim_orig.updated_copy(structures=structs_no_struct)
1013-
eps_no_structure = sim_no_structure.epsilon(box=plane_eps, coord_key="centers")
1013+
eps_no_structure = sim_no_structure.epsilon(
1014+
box=plane_eps, coord_key="centers", freq=freq_adj
1015+
)
10141016

10151017
# get permittivity with structures on top of an infinite version of this structure
10161018
structs_inf_struct = list(sim_orig.structures)[structure_index + 1 :]
@@ -1019,7 +1021,9 @@ def postprocess_adj(
10191021
medium=structure.medium,
10201022
monitors=[],
10211023
)
1022-
eps_inf_structure = sim_inf_structure.epsilon(box=plane_eps, coord_key="centers")
1024+
eps_inf_structure = sim_inf_structure.epsilon(
1025+
box=plane_eps, coord_key="centers", freq=freq_adj
1026+
)
10231027

10241028
else:
10251029
eps_no_structure = eps_inf_structure = None

0 commit comments

Comments
 (0)