Skip to content

Commit 57001e7

Browse files
Documentation and Greptile comments
1 parent f8f61a0 commit 57001e7

File tree

1 file changed

+87
-19
lines changed

1 file changed

+87
-19
lines changed

tidy3d/plugins/smatrix/local_run.py

Lines changed: 87 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
"""
22
Tool for generating an S matrix automatically from a Tidy3d simulation and lumped port definitions.
3-
4-
One major problem that prevents a stronger use of typing in these functions is the backwards compatibility of
5-
the imports. Ideally, we would do
6-
7-
run(simulation: MySimulation) -> MySimulationData
8-
9-
but because these
103
"""
114

125
from __future__ import annotations
@@ -34,13 +27,37 @@ def run(
3427
batch_data: BatchData = None,
3528
path_dir: str = DEFAULT_DATA_DIR,
3629
) -> TerminalComponentModelerData:
30+
"""
31+
Runs the S-matrix calculation for a given terminal component modeler simulation.
32+
33+
This function orchestrates the entire process of obtaining the S-matrix. It takes the
34+
simulation definition, runs the batch simulations if data is not already provided,
35+
constructs the S-matrix from the results, and packages all relevant data into a
36+
single `TerminalComponentModelerData` object for analysis and export.
37+
38+
Parameters
39+
----------
40+
simulation : TerminalComponentModeler
41+
The simulation setup defining the component, ports, and frequencies.
42+
batch_data : BatchData, optional
43+
Pre-computed simulation results from a Tidy3D batch run. If None, the function
44+
will execute the batch run defined within the `simulation` object.
45+
path_dir : str, optional
46+
Directory path to store simulation data. Defaults to `DEFAULT_DATA_DIR`.
47+
48+
Returns
49+
-------
50+
TerminalComponentModelerData
51+
A data container holding the original simulation definition, the calculated
52+
S-matrix data, and the raw simulation data for each port excitation.
53+
"""
3754
_ = simulation.get_path_dir(path_dir)
3855

3956
if batch_data is None:
4057
# TODO Remove once fully decoupled simulation from batch_data
4158
batch_data = simulation.batch_data
4259

43-
terminal_port_data = construct_smatrix(simulation=simulation)
60+
terminal_port_data = construct_smatrix(simulation=simulation, batch_data=batch_data)
4461

4562
port_to_sim_data_map = {
4663
port_i: batch_data[simulation._task_name(port=port_i)] for port_i in simulation.ports
@@ -55,7 +72,28 @@ def run(
5572
def construct_smatrix(
5673
simulation: TerminalComponentModeler, batch_data: BatchData = None
5774
) -> TerminalPortDataArray:
58-
"""Post process :class:`.BatchData` to generate scattering matrix."""
75+
""" "
76+
Constructs the scattering matrix (S-matrix) from raw simulation data.
77+
78+
This function iterates through each port, treating it as an input source once.
79+
For each input source, it calculates the resulting incident (a) and reflected (b)
80+
power wave amplitudes at all ports. These amplitudes are compiled into matrices,
81+
which are then used to compute the final S-matrix.
82+
83+
Parameters
84+
----------
85+
simulation : TerminalComponentModeler
86+
The simulation setup defining the component, ports, and frequencies.
87+
batch_data : BatchData, optional
88+
Pre-computed simulation results. If None, the function will execute the
89+
batch run defined within the `simulation` object.
90+
91+
Returns
92+
-------
93+
TerminalPortDataArray
94+
The computed S-matrix as a data array with dimensions for frequency,
95+
output port, and input port.
96+
"""
5997
if batch_data is None:
6098
batch_data = simulation.batch_data
6199

@@ -93,8 +131,28 @@ def construct_smatrix(
93131
def port_reference_impedances(
94132
simulation: TerminalComponentModeler, batch_data: BatchData = None
95133
) -> PortDataArray:
96-
"""Tabulates the reference impedance of each port at each frequency using the
97-
supplied :class:`.BatchData`.
134+
"""
135+
Calculates the reference impedance for each port across all frequencies.
136+
137+
This function determines the characteristic impedance for every port in the simulation.
138+
It handles two types of ports differently:
139+
- `WavePort`: The impedance is frequency-dependent and is computed from the modal
140+
properties stored in the simulation data.
141+
- Other ports (e.g., `LumpedPort`): The impedance is a constant value defined by the
142+
user.
143+
144+
Parameters
145+
----------
146+
simulation : TerminalComponentModeler
147+
The simulation setup defining the ports.
148+
batch_data : BatchData, optional
149+
Pre-computed simulation results, required for `WavePort` impedance calculation.
150+
If None, the batch data from the simulation object is used.
151+
152+
Returns
153+
-------
154+
PortDataArray
155+
A data array containing the complex impedance for each port at each frequency.
98156
"""
99157
if batch_data is None:
100158
# TODO Remove once fully decoupled simulation from batch_data
@@ -131,20 +189,30 @@ def compute_power_wave_amplitudes_at_each_port(
131189
port_reference_impedances: PortDataArray,
132190
sim_data: SimulationData,
133191
) -> tuple[PortDataArray, PortDataArray]:
134-
"""Compute the incident and reflected power wave amplitudes at each port.
135-
The computed amplitudes have not been normalized.
192+
"""
193+
Computes the incident (a) and reflected (b) power wave amplitudes at all ports
194+
from a single simulation run where one port was excited.
195+
196+
This function converts the raw voltage (V) and current (I) data into power wave
197+
amplitudes using standard microwave engineering formulas. It also performs a
198+
sanity check to ensure the real part of the reference impedance is positive,
199+
flipping signs of V and Z if necessary to maintain physical consistency.
136200
137201
Parameters
138202
----------
139-
port_reference_impedances : :class:`.PortDataArray`
140-
Reference impedance at each port.
141-
sim_data : :class:`.SimulationData`
142-
Results from the simulation.
203+
simulation : TerminalComponentModeler
204+
The simulation setup defining the ports.
205+
port_reference_impedances : PortDataArray
206+
The characteristic impedance for each port at each frequency.
207+
sim_data : SimulationData
208+
The raw results (fields, currents, etc.) from a single simulation run.
143209
144210
Returns
145211
-------
146-
tuple[:class:`.PortDataArray`, :class:`.PortDataArray`]
147-
Incident (a) and reflected (b) power wave amplitudes at each port.
212+
tuple[PortDataArray, PortDataArray]
213+
A tuple containing two PortDataArrays:
214+
- a: The incident power wave amplitudes at each port.
215+
- b: The reflected power wave amplitudes at each port.
148216
"""
149217
port_names = [port.name for port in simulation.ports]
150218
values = np.zeros(

0 commit comments

Comments
 (0)