Skip to content

Commit

Permalink
fix: resolve scalar frequencies in metrics not passing validation
Browse files Browse the repository at this point in the history
  • Loading branch information
yaugenst-flex committed Oct 24, 2024
1 parent 70bd113 commit b3c0051
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
8 changes: 3 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/notebooks
10 changes: 10 additions & 0 deletions tests/test_plugins/test_invdes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions tidy3d/plugins/expressions/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)


Expand Down

0 comments on commit b3c0051

Please sign in to comment.