Skip to content

Conversation

dmarek-flex
Copy link
Contributor

@dmarek-flex dmarek-flex commented Sep 29, 2025

Small refactoring so that the TerminalComponentModelerData class caches some results that are needed repeatedly for antenna metrics calculations

Greptile Overview

Updated On: 2025-09-29 19:23:47 UTC

Summary

This PR introduces performance optimizations to the antenna metrics calculation by implementing caching mechanisms in the TerminalComponentModelerData class. The key changes include:

  • Added cached properties: New @cached_property decorators for port_voltage_currents, port_pseudo_waves, and port_power_waves to cache expensive computations
  • Refactored computation flow: The terminal_construct_smatrix function now uses pre-computed cached wave amplitude data instead of recalculating it for each S-matrix construction
  • Method signature updates: The _monitor_data_at_port_amplitude method now takes an additional a_raw_port parameter to avoid redundant calculations
  • Helper function extraction: Split voltage/current computation and wave amplitude conversion into separate helper functions for better modularity

The caching strategy significantly reduces redundant calculations when multiple antenna metrics are computed, as the expensive port voltage/current and wave amplitude computations are now performed only once and reused across subsequent calls.

Confidence Score: 4/5

  • This PR is generally safe to merge with minor typos that should be corrected
  • The PR implements a straightforward performance optimization using well-established caching patterns with Python's @cached_property decorator. The logic changes are minimal and mostly involve using pre-computed cached results instead of recalculating them. The test updates are appropriate and maintain existing functionality. Two minor typos were found in docstrings.
  • tidy3d/plugins/smatrix/data/terminal.py needs typo corrections in docstrings on lines 385 and 390

Important Files Changed

File Analysis

Filename        Score        Overview
tidy3d/plugins/smatrix/data/terminal.py 5/5 Added cached properties for port voltages/currents and wave amplitudes to improve performance through caching
tidy3d/plugins/smatrix/analysis/terminal.py 4/5 Refactored to use cached wave amplitude data from TerminalComponentModelerData class, extracted helper functions for modularity
tidy3d/plugins/smatrix/analysis/antenna.py 4/5 Modified to use cached port_power_waves data and updated method signature for _monitor_data_at_port_amplitude to include a_raw_port parameter
tests/test_plugins/smatrix/test_terminal_component_modeler.py 5/5 Updated test to match new method signature requiring a_raw_port parameter and fixed port type mismatch

Sequence Diagram

sequenceDiagram
    participant Client
    participant TerminalComponentModelerData
    participant CachedProps as Cached Properties
    participant Terminal as terminal.py helpers

    Client->>TerminalComponentModelerData: get_antenna_metrics_data()
    TerminalComponentModelerData->>TerminalComponentModelerData: port_power_waves (cached_property)
    
    alt First access (cache miss)
        TerminalComponentModelerData->>TerminalComponentModelerData: compute_port_wave_amplitudes()
        TerminalComponentModelerData->>TerminalComponentModelerData: port_voltage_currents (cached_property)
        
        alt First VI access (cache miss)
            TerminalComponentModelerData->>Terminal: _compute_port_voltages_currents()
            Terminal-->>TerminalComponentModelerData: voltage_array, current_array
            TerminalComponentModelerData->>CachedProps: cache VI data
        else VI cache hit
            CachedProps-->>TerminalComponentModelerData: cached VI data
        end
        
        TerminalComponentModelerData->>Terminal: _compute_wave_amplitudes_from_VI()
        Terminal-->>TerminalComponentModelerData: a_array, b_array
        TerminalComponentModelerData->>CachedProps: cache wave amplitude data
    else Wave cache hit
        CachedProps-->>TerminalComponentModelerData: cached wave amplitudes
    end
    
    TerminalComponentModelerData->>TerminalComponentModelerData: _monitor_data_at_port_amplitude()
    TerminalComponentModelerData-->>Client: AntennaMetricsData
Loading

@dmarek-flex dmarek-flex linked an issue Sep 29, 2025 that may be closed by this pull request
@dmarek-flex dmarek-flex self-assigned this Sep 29, 2025
@dmarek-flex dmarek-flex added the RF label Sep 29, 2025
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.

4 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Copy link
Contributor

Diff Coverage

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

  • tidy3d/plugins/smatrix/analysis/antenna.py (100%)
  • tidy3d/plugins/smatrix/analysis/terminal.py (100%)
  • tidy3d/plugins/smatrix/data/terminal.py (100%)

Summary

  • Total: 60 lines
  • Missing: 0 lines
  • Coverage: 100%

@dmarek-flex dmarek-flex force-pushed the dmarek/improve-perf-antenna-metrices branch from cec125b to 126a777 Compare September 29, 2025 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize antenna metrics calculation for multiple port feeding
2 participants