Skip to content

Conversation

@caseyflex
Copy link
Contributor

@caseyflex caseyflex commented Nov 6, 2025

Interpolation in EME can greatly speed up multi-frequency EME simulations.

Greptile Overview

Greptile Summary

This PR adds frequency interpolation support to EME simulations to significantly speed up multi-frequency simulations. The implementation replaces the deprecated track_freq field in EMEModeSpec with a new interp_spec field that defaults to Chebyshev interpolation with 3 sampling points and data reduction enabled.

Key changes:

  • Core interpolation logic: Mode solver now computes modes at reduced frequency sets (determined by ModeInterpSpec.sampling_points()) and interpolates to full frequency ranges when needed
  • Smart interpolation coordination: New _interpolated_copies_if_needed() method ensures consistent interpolation when computing overlaps between mode datasets with different interpolation specs
  • Policy change: Removed warnings when num_points >= len(freqs) - interpolation is now skipped naturally by returning all frequencies
  • Deprecation: EMEFreqSweep is deprecated in favor of using EMESimulation.freqs directly with interp_spec configuration
  • Storage optimization: Monitor storage calculations updated to account for reduced frequency storage when interpolation is enabled

The implementation properly handles edge cases like when interpolation specs differ between datasets, and includes comprehensive test updates reflecting the new behavior.

Confidence Score: 4/5

  • This PR is safe to merge with one documentation fix needed
  • The implementation is well-structured with comprehensive test coverage and proper handling of interpolation edge cases. The main issue is a docstring that incorrectly references a removed field (track_freq instead of sort_spec.track_freq), which needs correction to avoid user confusion. The removal of validation warnings is intentional and properly tested. The interpolation logic correctly falls back to computing all frequencies when num_points >= len(freqs), and the coordination between different interpolation specs is handled appropriately.
  • tidy3d/components/eme/grid.py needs docstring correction for the removed track_freq field reference

Important Files Changed

File Analysis

Filename Score Overview
tidy3d/components/eme/grid.py 3/5 Replaced track_freq field with interp_spec in EMEModeSpec, defaulting to Chebyshev interpolation with 3 points. Docstring incorrectly references removed track_freq field.
tidy3d/components/mode_spec.py 4/5 Changed condition from > to >= in sampling_points() to allow interpolation even when num_points == len(freqs). Added _same_nontrivial_interp_spec() helper method.
tidy3d/components/eme/data/sim_data.py 4/5 Updated smatrix_in_basis() to handle interpolation: uses _interpolated_copies_if_needed() and applies interpolation to overlap calculations when needed.
tidy3d/components/monitor.py 4/5 Removed validation warning for interpolation num_points. Updated storage size calculation to use _stored_freqs which accounts for interpolation reduction.
tidy3d/components/validators.py 5/5 Removed _warn_interp_num_points() and validate_interp_num_points() functions as warnings are no longer needed with new interpolation behavior.

Sequence Diagram

sequenceDiagram
    participant User
    participant EMESimulation
    participant EMEModeSpec
    participant ModeInterpSpec
    participant ModeSolver
    participant ModeSolverData
    participant EMESimulationData
    
    User->>EMESimulation: Create simulation with freqs
    EMESimulation->>EMEModeSpec: Initialize with default interp_spec
    Note over EMEModeSpec: Default: ModeInterpSpec.cheb(num_points=3, reduce_data=True)
    
    User->>EMESimulation: Run simulation
    EMESimulation->>EMEModeSpec: Get sampling frequencies
    EMEModeSpec->>ModeInterpSpec: sampling_points(freqs)
    
    alt num_points >= len(freqs)
        ModeInterpSpec-->>EMEModeSpec: Return all freqs (no interpolation)
    else num_points < len(freqs)
        ModeInterpSpec-->>EMEModeSpec: Return reduced freq set (Chebyshev points)
    end
    
    EMESimulation->>ModeSolver: Solve modes at sampling freqs only
    ModeSolver-->>EMESimulation: Return mode data (reduced)
    
    User->>EMESimulationData: Access mode data / compute S-matrix
    
    alt Data needs interpolation
        EMESimulationData->>ModeSolverData: _interpolated_copies_if_needed(other)
        
        alt Same nontrivial interp_spec
            ModeSolverData-->>EMESimulationData: Return originals (no interp needed)
        else Different interp_specs
            ModeSolverData->>ModeSolverData: interpolated_copy
            ModeSolverData->>ModeSolverData: interp_in_freq(all freqs)
            ModeSolverData-->>EMESimulationData: Return interpolated copies
        end
        
        EMESimulationData->>EMESimulationData: Compute overlaps with interpolated data
    end
    
    EMESimulationData-->>User: Return S-matrix results
Loading

Context used:

  • Rule from dashboard - Keep docstrings and comments synchronized with code changes to ensure they are always accurate and n... (source)

@dbochkov-flexcompute dbochkov-flexcompute force-pushed the daniil/FXC-3351-Downsample-frequencies-in-mode-monitors branch 2 times, most recently from bdd7b09 to aedc3a9 Compare November 11, 2025 02:19
@dbochkov-flexcompute dbochkov-flexcompute force-pushed the daniil/FXC-3351-Downsample-frequencies-in-mode-monitors branch 4 times, most recently from 3571d01 to 73e8ac0 Compare November 12, 2025 17:31
Base automatically changed from daniil/FXC-3351-Downsample-frequencies-in-mode-monitors to develop November 12, 2025 19:29
@caseyflex caseyflex force-pushed the casey/emeinterp3 branch 4 times, most recently from 0c72776 to 3676cf9 Compare November 12, 2025 23:26
@caseyflex caseyflex marked this pull request as ready for review November 12, 2025 23:32
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, 4 comments

Edit Code Review Agent Settings | Greptile

@caseyflex caseyflex force-pushed the casey/emeinterp3 branch 2 times, most recently from 455b032 to 5a642c9 Compare November 12, 2025 23:44
Copy link
Contributor

@dbochkov-flexcompute dbochkov-flexcompute left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, don't really have anything to add

@caseyflex caseyflex changed the title Add interpolation to EME feat: add interpolation to EME (FXC-4152) Nov 14, 2025
@caseyflex
Copy link
Contributor Author

@greptile

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@caseyflex
Copy link
Contributor Author

@greptile

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@github-actions
Copy link
Contributor

github-actions bot commented Nov 14, 2025

Diff Coverage

Diff: origin/develop...HEAD, staged and unstaged changes

  • tidy3d/components/data/dataset.py (50.0%): Missing lines 144
  • tidy3d/components/data/monitor_data.py (100%)
  • tidy3d/components/eme/data/sim_data.py (100%)
  • tidy3d/components/eme/grid.py (100%)
  • tidy3d/components/eme/simulation.py (20.0%): Missing lines 1018-1023,1025-1026
  • tidy3d/components/mode_spec.py (100%)
  • tidy3d/components/monitor.py (75.0%): Missing lines 434

Summary

  • Total: 43 lines
  • Missing: 10 lines
  • Coverage: 76%

tidy3d/components/data/dataset.py

  140             Interpolated data array with the same structure but new frequency points.
  141         """
  142         # if dataarray is already stored at the correct frequencies, do nothing
  143         if np.array_equal(freqs, data.f):
! 144             return data
  145 
  146         # Map 'poly' to xarray's 'barycentric' method
  147         xr_method = "barycentric" if method == "poly" else method

tidy3d/components/eme/simulation.py

  1014         return list(monitor.freqs)
  1015 
  1016     def _monitor_mode_freqs(self, monitor: EMEModeSolverMonitor) -> list[pd.NonNegativeFloat]:
  1017         """Monitor frequencies."""
! 1018         freqs = set()
! 1019         cell_inds = self._monitor_eme_cell_indices(monitor=monitor)
! 1020         for cell_ind in cell_inds:
! 1021             interp_spec = self.eme_grid.mode_specs[cell_ind].interp_spec
! 1022             if interp_spec is None:
! 1023                 freqs |= set(self.freqs)
  1024             else:
! 1025                 freqs |= set(interp_spec.sampling_points(self.freqs))
! 1026         return list(freqs)
  1027 
  1028     def _monitor_num_freqs(self, monitor: Monitor) -> int:
  1029         """Total number of freqs included in monitor."""
  1030         return len(self._monitor_freqs(monitor=monitor))

tidy3d/components/monitor.py

  430 
  431     @property
  432     def _stored_freqs(self) -> list[float]:
  433         """Return actually stored frequencies of the data."""
! 434         return self.mode_spec._sampling_freqs_mode_solver_data(freqs=self.freqs)
  435 
  436     def _storage_size_solver(self, num_cells: int, tmesh: ArrayFloat1D) -> int:
  437         """Size of intermediate data recorded by the monitor during a solver run."""
  438         # Need to store all fields on the mode surface

Copy link
Collaborator

@momchil-flex momchil-flex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@caseyflex caseyflex enabled auto-merge November 18, 2025 17:57
@caseyflex caseyflex added this pull request to the merge queue Nov 18, 2025
Merged via the queue into develop with commit 4bc3c7b Nov 18, 2025
26 checks passed
@caseyflex caseyflex deleted the casey/emeinterp3 branch November 18, 2025 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants