Skip to content

Commit 2031d79

Browse files
Gregory Robertsmomchil-flex
authored andcommitted
fix[adjoint]: fix magnitude of gradient calculation in CustomMedium when permittivity and medium defined over different number of dimensions
1 parent c64840e commit 2031d79

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
- Potential task name mismatches between forward and adjoint simulations in batch tasks.
12+
- Magnitude of gradient computation in `CustomMedium` by accounting properly for full volume element when permittivity data is defined over fewer dimensions than the medium.
13+
1014
## [2.7.8] - 2024-11-27
1115

1216
### Changed
@@ -15,7 +19,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1519
### Fixed
1620
- Gradient inaccuracy when a multi-frequency monitor is used but a single frequency is selected.
1721
- Revert single cell center approximation for custom medium gradient.
18-
- Potential task name mismatches between forward and adjoint simulations in batch tasks.
1922

2023
## [2.7.7] - 2024-11-15
2124

tidy3d/components/medium.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,7 +2582,17 @@ def _derivative_field_cmp(
25822582

25832583
coords_interp = {key: eps_data.coords[key] for key in "xyz"}
25842584
coords_interp = {key: val for key, val in coords_interp.items() if len(val) > 1}
2585-
dims_sum = [dim for dim in "xyz" if dim not in coords_interp]
2585+
2586+
E_der_dim_interp = E_der_map[f"E{dim}"]
2587+
2588+
for dim_ in "xyz":
2589+
if dim_ not in coords_interp:
2590+
bound_max = np.max(E_der_dim_interp.coords[dim_])
2591+
bound_min = np.min(E_der_dim_interp.coords[dim_])
2592+
dimension_size = bound_max - bound_min
2593+
2594+
if dimension_size > 0.0:
2595+
E_der_dim_interp = E_der_dim_interp.integrate(dim_)
25862596

25872597
# compute sizes along each of the interpolation dimensions
25882598
sizes_list = []
@@ -2610,28 +2620,24 @@ def _derivative_field_cmp(
26102620
d_vol = np.array(1.0)
26112621

26122622
# TODO: probably this could be more robust. eg if the DataArray has weird edge cases
2613-
E_der_dim = E_der_map[f"E{dim}"]
26142623
E_der_dim_interp = (
2615-
E_der_dim.interp(**coords_interp, assume_sorted=True).fillna(0.0).sum(dims_sum).real
2624+
E_der_dim_interp.interp(**coords_interp, assume_sorted=True).fillna(0.0).real.sum("f")
26162625
)
2617-
E_der_dim_interp = E_der_dim_interp.sum("f")
2618-
2619-
vjp_array = np.array(E_der_dim_interp.values, dtype=float)
2620-
2621-
vjp_array = vjp_array.reshape(eps_data.shape)
26222626

2623-
# multiply by volume elements (if possible, being defensive here..)
26242627
try:
2625-
vjp_array *= d_vol.reshape(vjp_array.shape)
2628+
E_der_dim_interp = E_der_dim_interp * d_vol.reshape(E_der_dim_interp.shape)
26262629
except ValueError:
26272630
log.warning(
26282631
"Skipping volume element normalization of 'CustomMedium' gradients. "
26292632
f"Could not reshape the volume elements of shape {d_vol.shape} "
2630-
f"to the shape of the gradient {vjp_array.shape}. "
2633+
f"to the shape of the fields {E_der_dim_interp.shape}. "
26312634
"If you encounter this warning, gradient direction will be accurate but the norm "
26322635
"will be inaccurate. Please raise an issue on the tidy3d front end with this "
26332636
"message and some information about your simulation setup and we will investigate. "
26342637
)
2638+
vjp_array = E_der_dim_interp.values
2639+
vjp_array = vjp_array.reshape(eps_data.shape)
2640+
26352641
return vjp_array
26362642

26372643

0 commit comments

Comments
 (0)