Skip to content

Commit 217e269

Browse files
committed
fix gradient for single freq selection from multi freq mnt
1 parent efdc698 commit 217e269

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
### Fixed
11+
- Gradient inaccuracy when a multi-frequency monitor is used but a single frequency is selected.
12+
1013
## [2.7.7] - 2024-11-15
1114

1215
### Added

tidy3d/components/data/sim_data.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,11 +1115,21 @@ def process_adjoint_sources(self, adj_srcs: list[SourceType]) -> AdjointSourceIn
11151115
hashes_to_src_times[tmp_src_hash].append(src.source_time)
11161116

11171117
num_ports = len(hashes_to_src_times)
1118-
num_unique_freqs = len({src.source_time.freq0 for src in adj_srcs})
1118+
unique_freqs = {src.source_time.freq0 for src in adj_srcs}
1119+
num_unique_freqs = len(unique_freqs)
11191120

11201121
# next, figure out which treatment / normalization to apply
11211122
if num_unique_freqs == 1:
11221123
log.info("Adjoint source creation: one unique frequency, no normalization.")
1124+
freqs_adj = self.simulation.freqs_adjoint
1125+
1126+
# if many adjoint freqs, but only 1 unique, need to mask out the non-contributors
1127+
if len(freqs_adj) > 1:
1128+
coords = dict(f=freqs_adj)
1129+
data = [1 if f == tuple(unique_freqs)[0] else 0 for f in freqs_adj]
1130+
post_norm = xr.DataArray(data, coords=coords)
1131+
return AdjointSourceInfo(sources=adj_srcs, post_norm=post_norm, normalize_sim=True)
1132+
11231133
return AdjointSourceInfo(sources=adj_srcs, post_norm=1.0, normalize_sim=True)
11241134

11251135
if num_ports == 1 and len(adj_srcs) == num_unique_freqs:

0 commit comments

Comments
 (0)