Skip to content

Commit 75fd628

Browse files
committed
Fix: Bug in WavePort when requesting more than one mode in the ModeSpec
Jira-Ticket: FXC-3371
1 parent ef3d257 commit 75fd628

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

CHANGELOG.md

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

1717
### Fixed
1818
- More robust `Sellmeier` and `Debye` material model, and prevent very large pole parameters in `PoleResidue` material model.
19-
19+
- Bug in `WavePort` when more than one mode is requested in the `ModeSpec`.
2020

2121
## [v2.10.0rc2] - 2025-10-01
2222

tests/test_plugins/smatrix/test_terminal_component_modeler.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,25 @@ def test_run_coaxial_component_modeler_with_wave_ports(
780780
s_matrix.sel(**coords_in).sel(**coords_out).values.shape == shape_both_ports
781781
), "monitor index not present in S matrix"
782782

783+
# Another run with more modes in the mode spec
784+
mode_spec = td.ModeSpec(num_modes=2)
785+
modeler = modeler.updated_copy(path="ports/0/", mode_spec=mode_spec)
786+
s_matrix = get_terminal_port_data_array(monkeypatch, modeler)
787+
788+
shape_one_port = (len(modeler.freqs), len(modeler.ports))
789+
shape_both_ports = (len(modeler.freqs),)
790+
for port_in in modeler.ports:
791+
for port_out in modeler.ports:
792+
coords_in = {"port_in": port_in.name}
793+
coords_out = {"port_out": port_out.name}
794+
795+
assert np.all(s_matrix.sel(**coords_in).values.shape == shape_one_port), (
796+
"source index not present in S matrix"
797+
)
798+
assert np.all(
799+
s_matrix.sel(**coords_in).sel(**coords_out).values.shape == shape_both_ports
800+
), "monitor index not present in S matrix"
801+
783802

784803
def test_run_mixed_component_modeler_with_wave_ports(monkeypatch, tmp_path):
785804
"""Checks the terminal component modeler will allow mixed ports."""

tidy3d/plugins/smatrix/ports/wave.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ def _mode_voltage_coefficients(self, mode_data: ModeData) -> FreqModeDataArray:
103103
to the total port voltage.
104104
"""
105105
flux_sign = 1 if self.direction == "+" else -1
106-
107-
mode_data = mode_data._isel(mode_index=[self.mode_index])
108106
if self.voltage_integral is None:
109107
flux_sign = 1 if mode_data.monitor.store_fields_direction == "+" else -1
110108
current_coeffs = self.current_integral.compute_current(mode_data)
@@ -118,7 +116,6 @@ def _mode_current_coefficients(self, mode_data: ModeData) -> FreqModeDataArray:
118116
to the total port current.
119117
"""
120118
flux_sign = 1 if self.direction == "+" else -1
121-
mode_data = mode_data._isel(mode_index=[self.mode_index])
122119
if self.current_integral is None:
123120
flux_sign = 1 if mode_data.monitor.store_fields_direction == "+" else -1
124121
voltage_coeffs = self.voltage_integral.compute_voltage(mode_data)
@@ -221,20 +218,20 @@ def to_absorber(
221218

222219
def compute_voltage(self, sim_data: SimulationData) -> FreqDataArray:
223220
"""Helper to compute voltage across the port."""
224-
mode_data = sim_data[self._mode_monitor_name]
221+
mode_data = sim_data[self._mode_monitor_name]._isel(mode_index=[self.mode_index])
225222
voltage_coeffs = self._mode_voltage_coefficients(mode_data)
226223
amps = mode_data.amps
227-
fwd_amps = amps.sel(direction="+").squeeze()
228-
bwd_amps = amps.sel(direction="-").squeeze()
224+
fwd_amps = amps.sel(direction="+", mode_index=self.mode_index).squeeze()
225+
bwd_amps = amps.sel(direction="-", mode_index=self.mode_index).squeeze()
229226
return voltage_coeffs * (fwd_amps + bwd_amps)
230227

231228
def compute_current(self, sim_data: SimulationData) -> FreqDataArray:
232229
"""Helper to compute current flowing through the port."""
233-
mode_data = sim_data[self._mode_monitor_name]
230+
mode_data = sim_data[self._mode_monitor_name]._isel(mode_index=[self.mode_index])
234231
current_coeffs = self._mode_current_coefficients(mode_data)
235232
amps = mode_data.amps
236-
fwd_amps = amps.sel(direction="+").squeeze()
237-
bwd_amps = amps.sel(direction="-").squeeze()
233+
fwd_amps = amps.sel(direction="+", mode_index=self.mode_index).squeeze()
234+
bwd_amps = amps.sel(direction="-", mode_index=self.mode_index).squeeze()
238235
# In ModeData, fwd_amps and bwd_amps are not relative to
239236
# the direction fields are stored
240237
sign = 1.0

0 commit comments

Comments
 (0)