From b3c005126385a5b82da39ef19eb991b3ec1407f3 Mon Sep 17 00:00:00 2001 From: Yannick Augenstein Date: Thu, 24 Oct 2024 11:57:06 +0200 Subject: [PATCH] fix: resolve scalar frequencies in metrics not passing validation --- CHANGELOG.md | 8 +++----- docs/notebooks | 2 +- tests/test_plugins/test_invdes.py | 10 ++++++++++ tidy3d/plugins/expressions/metrics.py | 5 +++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9f7f6cc4..0ad4428b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,18 +9,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Users can manually specify the background medium for a structure to be used for geometry gradient calculations by supplying `Structure.background_permittivity`. This is useful when there are overlapping structures or structures embedded in other mediums. - -### Fixed -- Minor gradient direction and normalization fixes for polyslab, field monitors, and diffraction monitors in autograd. -- Resolved an issue where temporary files for adjoint simulations were not being deleted properly. - Autograd functions can now be called directly on `DataArray` (e.g., `np.sum(data_array)`) in objective functions. ### Changed - Improved autograd tracer handling in `DataArray`, resulting in significant speedups for differentiation involving large monitors. ### Fixed +- Minor gradient direction and normalization fixes for polyslab, field monitors, and diffraction monitors in autograd. +- Resolved an issue where temporary files for adjoint simulations were not being deleted properly. - Resolve several edge cases where autograd boxes were incorrectly converted to numpy arrays. - +- Resolve issue where scalar frequencies in metric definitions (`ModeAmp(f=freq)` instead of `ModeAmp(f=[freq])`) would erroneously fail validation. ## [2.7.5] - 2024-10-16 diff --git a/docs/notebooks b/docs/notebooks index c9eb86526..86b0310c5 160000 --- a/docs/notebooks +++ b/docs/notebooks @@ -1 +1 @@ -Subproject commit c9eb86526c9bc03950b562d6a0978ab3a28f171d +Subproject commit 86b0310c55af92be71c04d7e2c6865d1090c3534 diff --git a/tests/test_plugins/test_invdes.py b/tests/test_plugins/test_invdes.py index 7940c5ef4..75e6b7498 100644 --- a/tests/test_plugins/test_invdes.py +++ b/tests/test_plugins/test_invdes.py @@ -586,6 +586,16 @@ def test_initial_simulation_multi(): ) +def test_metric_scalar_freq(): + invdes = make_invdes() + metric = ModePower(monitor_name=MNT_NAME2, mode_index=0, f=FREQ0) + monitor = mnt2.updated_copy(freqs=[FREQ0, FREQ0 / 2]) + invdes = invdes.updated_copy( + metric=metric, + simulation=simulation.updated_copy(monitors=[monitor]), + ) + + def test_validate_invdes_metric(): """Test the _validate_metric_monitor_name validator.""" invdes = make_invdes() diff --git a/tidy3d/plugins/expressions/metrics.py b/tidy3d/plugins/expressions/metrics.py index 5203a2a51..a20861647 100644 --- a/tidy3d/plugins/expressions/metrics.py +++ b/tidy3d/plugins/expressions/metrics.py @@ -93,7 +93,7 @@ def from_mode_monitor( @property def _validation_data(self) -> Any: """Return dummy data for this metric (complex array of mode amplitudes).""" - f = list(self.f) if self.f is not None else [1.0] + f = np.atleast_1d(self.f).tolist() if self.f is not None else [1.0] amps_data = np.random.rand(len(f)) + 1j * np.random.rand(len(f)) amps = xr.DataArray( amps_data.reshape(1, 1, -1), @@ -111,7 +111,8 @@ def evaluate(self, *args: Any, **kwargs: Any) -> NumberType: direction=self.direction, mode_index=self.mode_index ) if self.f is not None: - amps = amps.sel(f=list(self.f), method="nearest") + f = list(self.f) if isinstance(self.f, tuple) else self.f + amps = amps.sel(f=f, method="nearest") return np.squeeze(amps.data)