1
1
"""
2
2
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
10
3
"""
11
4
12
5
from __future__ import annotations
@@ -34,13 +27,37 @@ def run(
34
27
batch_data : BatchData = None ,
35
28
path_dir : str = DEFAULT_DATA_DIR ,
36
29
) -> 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
+ """
37
54
_ = simulation .get_path_dir (path_dir )
38
55
39
56
if batch_data is None :
40
57
# TODO Remove once fully decoupled simulation from batch_data
41
58
batch_data = simulation .batch_data
42
59
43
- terminal_port_data = construct_smatrix (simulation = simulation )
60
+ terminal_port_data = construct_smatrix (simulation = simulation , batch_data = batch_data )
44
61
45
62
port_to_sim_data_map = {
46
63
port_i : batch_data [simulation ._task_name (port = port_i )] for port_i in simulation .ports
@@ -55,7 +72,28 @@ def run(
55
72
def construct_smatrix (
56
73
simulation : TerminalComponentModeler , batch_data : BatchData = None
57
74
) -> 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
+ """
59
97
if batch_data is None :
60
98
batch_data = simulation .batch_data
61
99
@@ -93,8 +131,28 @@ def construct_smatrix(
93
131
def port_reference_impedances (
94
132
simulation : TerminalComponentModeler , batch_data : BatchData = None
95
133
) -> 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.
98
156
"""
99
157
if batch_data is None :
100
158
# TODO Remove once fully decoupled simulation from batch_data
@@ -131,20 +189,30 @@ def compute_power_wave_amplitudes_at_each_port(
131
189
port_reference_impedances : PortDataArray ,
132
190
sim_data : SimulationData ,
133
191
) -> 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.
136
200
137
201
Parameters
138
202
----------
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.
143
209
144
210
Returns
145
211
-------
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.
148
216
"""
149
217
port_names = [port .name for port in simulation .ports ]
150
218
values = np .zeros (
0 commit comments