-
Notifications
You must be signed in to change notification settings - Fork 74
feat(tidy3d): FXC-3296-autograd-support-for-anisotropic-medium-and-custom-anisotropic-medium #3080
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
Merged
marcorudolphflex
merged 1 commit into
develop
from
FXC-3296-autograd-support-for-anisotropic-medium-and-custom-anisotropic-medium
Jan 8, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
tests/test_components/autograd/test_autograd_anisotropic_medium.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import numpy as np | ||
|
|
||
| import tidy3d as td | ||
| from tidy3d.components.autograd.derivative_utils import DerivativeInfo | ||
| from tidy3d.components.medium import AnisotropicMedium | ||
| from tidy3d.constants import EPSILON_0 | ||
|
|
||
|
|
||
| def _scalar_field(value: complex, coords: dict[str, list[float]]): | ||
| """Helper to build a ``ScalarFieldDataArray`` with a single sample.""" | ||
|
|
||
| data = np.array([[[[value]]]], dtype=np.complex128) | ||
| return td.ScalarFieldDataArray(data, coords=coords) | ||
|
|
||
|
|
||
| def _derivative_info(paths: list[tuple[str, ...]], freq: float): | ||
| """Construct a ``DerivativeInfo`` with synthetic field products.""" | ||
|
|
||
| coords = {"x": [0.0], "y": [0.0], "z": [0.0], "f": [freq]} | ||
| ex_val = 1.2 + 0.4j | ||
| ey_val = -0.8 + 0.1j | ||
| ez_val = 0.5 - 0.3j | ||
|
|
||
| field_map = { | ||
| "Ex": _scalar_field(ex_val, coords), | ||
| "Ey": _scalar_field(ey_val, coords), | ||
| "Ez": _scalar_field(ez_val, coords), | ||
| } | ||
|
|
||
| eps_no = _scalar_field(1.0, coords) | ||
| eps_inf = _scalar_field(2.0, coords) | ||
|
|
||
| return DerivativeInfo( | ||
| paths=paths, | ||
| E_der_map=field_map, | ||
| D_der_map={}, | ||
| E_fwd={}, | ||
| D_fwd={}, | ||
| E_adj={}, | ||
| D_adj={}, | ||
| eps_data={}, | ||
| eps_in=2.0, | ||
| eps_out=1.0, | ||
| frequencies=np.array([freq]), | ||
| bounds=((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)), | ||
| bounds_intersect=((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)), | ||
| simulation_bounds=((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)), | ||
| eps_no_structure=eps_no, | ||
| eps_inf_structure=eps_inf, | ||
| ) | ||
|
|
||
|
|
||
| def test_anisotropic_medium_axis_gradients_uniform(): | ||
| """Gradients respect axis-specific contributions for diagonal anisotropy.""" | ||
|
|
||
| freq = 2.0e14 | ||
| paths = [("xx", "permittivity"), ("yy", "conductivity"), ("zz", "permittivity")] | ||
| info = _derivative_info(paths=paths, freq=freq) | ||
|
|
||
| medium = td.AnisotropicMedium( | ||
| xx=td.Medium(permittivity=2.0), | ||
| yy=td.Medium(permittivity=2.5), | ||
| zz=td.Medium(permittivity=3.0), | ||
| ) | ||
|
|
||
| grads = medium._compute_derivatives(info) | ||
|
|
||
| ex_real = np.real(info.E_der_map["Ex"].values).item() | ||
| ey_imag = np.imag(info.E_der_map["Ey"].values).item() | ||
| ez_real = np.real(info.E_der_map["Ez"].values).item() | ||
|
|
||
| assert np.isclose(grads[("xx", "permittivity")], ex_real) | ||
| assert np.isclose(grads[("zz", "permittivity")], ez_real) | ||
|
|
||
| expected_sigma = -ey_imag / (2.0 * np.pi * freq * EPSILON_0) | ||
| assert np.isclose(grads[("yy", "conductivity")], expected_sigma) | ||
|
|
||
|
|
||
| def _linear_variation_coords(shape: tuple[int, int, int]): | ||
| coords = { | ||
| "x": np.linspace(-1.0, 1.0, shape[0]), | ||
| "y": np.linspace(-1.0, 1.0, shape[1]), | ||
| "z": np.linspace(-1.0, 1.0, shape[2]), | ||
| } | ||
| X, Y, Z = np.meshgrid(coords["x"], coords["y"], coords["z"], indexing="ij") | ||
| factors = 1 + 0.2 * (X + Y + Z) / 3.0 | ||
| return coords, factors | ||
|
|
||
|
|
||
| def test_custom_anisotropic_medium_axis_gradients(): | ||
| """Custom anisotropic medium delegates gradients per axis with spatial data.""" | ||
|
|
||
| freq = 1.5e14 | ||
| paths = [("xx", "permittivity"), ("yy", "permittivity"), ("zz", "permittivity")] | ||
| info = _derivative_info(paths=paths, freq=freq) | ||
|
|
||
| coords, factors = _linear_variation_coords((3, 4, 5)) | ||
|
|
||
| def _make_custom_medium(base_val): | ||
| values = base_val * factors | ||
| data = td.SpatialDataArray(values, coords=coords) | ||
| return td.CustomMedium(permittivity=data) | ||
|
|
||
| medium = td.CustomAnisotropicMedium( | ||
| xx=_make_custom_medium(1.8), | ||
| yy=_make_custom_medium(2.0), | ||
| zz=_make_custom_medium(2.2), | ||
| ) | ||
|
|
||
| grads = medium._compute_derivatives(info) | ||
|
|
||
| for comp_name in ("xx", "yy", "zz"): | ||
| comp_info = AnisotropicMedium._component_derivative_info(info, comp_name) | ||
| comp_grad = medium.components[comp_name]._compute_derivatives(comp_info) | ||
| np.testing.assert_allclose(grads[(comp_name, "permittivity")], comp_grad[("permittivity",)]) | ||
|
|
||
|
|
||
| def test_anisotropic_medium_conductivity_uses_projected_d_map(): | ||
| """Conductivity gradients keep only the axis D component.""" | ||
|
|
||
| freq = 2.0e14 | ||
| paths = [("yy", "conductivity")] | ||
|
|
||
| coords = {"x": [0.0], "y": [0.0], "z": [0.0], "f": [freq]} | ||
| e_map = { | ||
| "Ex": _scalar_field(1.0 + 0.0j, coords), | ||
| "Ey": _scalar_field(2.0 - 0.5j, coords), | ||
| "Ez": _scalar_field(3.0 + 0.0j, coords), | ||
| } | ||
| d_map = { | ||
| "Ex": _scalar_field(1.1 + 0.0j, coords), | ||
| "Ey": _scalar_field(2.2 + 0.0j, coords), | ||
| "Ez": _scalar_field(3.3 + 0.0j, coords), | ||
| } | ||
|
|
||
| eps_no = _scalar_field(1.0, coords) | ||
| eps_inf = _scalar_field(2.0, coords) | ||
|
|
||
| info = DerivativeInfo( | ||
| paths=paths, | ||
| E_der_map=e_map, | ||
| D_der_map=d_map, | ||
| E_fwd={}, | ||
| D_fwd={}, | ||
| E_adj={}, | ||
| D_adj={}, | ||
| eps_data={}, | ||
| eps_in=2.0, | ||
| eps_out=1.0, | ||
| frequencies=np.array([freq]), | ||
| bounds=((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)), | ||
| bounds_intersect=((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)), | ||
| simulation_bounds=((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)), | ||
| eps_no_structure=eps_no, | ||
| eps_inf_structure=eps_inf, | ||
| ) | ||
|
|
||
| medium = td.AnisotropicMedium( | ||
| xx=td.Medium(permittivity=2.0), | ||
| yy=td.Medium(permittivity=2.5, conductivity=0.1), | ||
| zz=td.Medium(permittivity=3.0), | ||
| ) | ||
|
|
||
| comp_info = AnisotropicMedium._component_derivative_info(info, "yy") | ||
|
|
||
| assert np.allclose(comp_info.D_der_map["Ex"].values, 0.0) | ||
| assert np.allclose(comp_info.D_der_map["Ez"].values, 0.0) | ||
| assert np.allclose(comp_info.D_der_map["Ey"].values, d_map["Ey"].values) | ||
|
|
||
| grads = medium.components["yy"]._compute_derivatives(comp_info) | ||
| expected_sigma = -np.imag(e_map["Ey"].values).item() / (2.0 * np.pi * freq * EPSILON_0) | ||
|
|
||
| assert np.isclose(grads[("conductivity",)], expected_sigma) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.