Fix: Bug in WavePort when requesting more than one mode in the ModeSpec #2861
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.
Greptile Overview
Updated On: 2025-10-01 20:35:07 UTC
Summary
This PR fixes a critical bug in the `WavePort` class that occurred when users requested more than one mode in the `ModeSpec` during S-parameter calculations. The issue was in the mode selection logic within the wave port implementation.The root cause was that mode selection was happening too early in the calculation pipeline - specifically in the
_mode_voltage_coefficientsand_mode_current_coefficientsmethods. This premature filtering prevented other parts of the code from accessing the full mode data needed for proper multi-mode calculations.The fix relocates the mode selection from the coefficient calculation methods to the final voltage/current computation methods (
compute_voltageandcompute_current). Additionally, explicitmode_indexselection is added when extracting forward and backward amplitudes. This architectural change ensures that:The changes integrate seamlessly with the existing S-matrix calculation infrastructure in the microwave plugin, maintaining backward compatibility while fixing the multi-mode scenario. A comprehensive test case has been added to prevent regression, and the fix is properly documented in the changelog.
Changed Files
Confidence score: 4/5
Sequence Diagram
sequenceDiagram participant User participant TerminalComponentModeler as TCM participant WavePort participant ModeMonitor participant ModeSource participant Simulation participant ModeData participant ImpedanceCalculator as ImpCalc User->>TCM: "Create modeler with WavePort(mode_spec=ModeSpec(num_modes>1))" TCM->>WavePort: "Initialize with multi-mode ModeSpec" WavePort->>WavePort: "Store mode_spec and mode_index" User->>TCM: "Generate simulation dictionary" TCM->>WavePort: "to_source(source_time)" WavePort->>ModeSource: "Create source with mode_spec and mode_index" ModeSource-->>WavePort: "Return configured source" WavePort-->>TCM: "Return source" TCM->>WavePort: "to_monitors(freqs)" WavePort->>ModeMonitor: "Create monitor with full mode_spec" Note over ModeMonitor: BUG: Monitor requests all modes<br/>but only mode_index is used later ModeMonitor-->>WavePort: "Return monitor" WavePort-->>TCM: "Return [monitor]" TCM->>Simulation: "Create simulation with sources and monitors" Simulation-->>TCM: "Return configured simulation" User->>TCM: "Run simulation" TCM->>Simulation: "Execute FDTD simulation" Simulation->>ModeData: "Compute mode amplitudes for all modes" ModeData-->>Simulation: "Return multi-mode data" Simulation-->>TCM: "Return SimulationData" TCM->>WavePort: "compute_voltage(sim_data)" WavePort->>ModeData: "_isel(mode_index=[self.mode_index])" Note over ModeData: Filter to specific mode only ModeData-->>WavePort: "Return single mode data" WavePort->>WavePort: "_mode_voltage_coefficients(mode_data)" WavePort->>WavePort: "Calculate voltage from amplitudes" WavePort-->>TCM: "Return voltage array" TCM->>WavePort: "compute_current(sim_data)" WavePort->>ModeData: "_isel(mode_index=[self.mode_index])" ModeData-->>WavePort: "Return single mode data" WavePort->>WavePort: "_mode_current_coefficients(mode_data)" WavePort->>WavePort: "Calculate current from amplitudes" WavePort-->>TCM: "Return current array" TCM->>WavePort: "compute_port_impedance(sim_data)" WavePort->>ModeData: "_isel(mode_index=[self.mode_index])" ModeData-->>WavePort: "Return single mode data" WavePort->>ImpCalc: "compute_impedance(mode_data)" ImpCalc-->>WavePort: "Return impedance array" WavePort-->>TCM: "Return port impedance" TCM->>TCM: "Construct S-parameter matrix" TCM-->>User: "Return TerminalComponentModelerData"