Skip to content

Commit 55cb5d4

Browse files
Improve docs
1 parent 02c94a1 commit 55cb5d4

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

tidy3d/plugins/smatrix/utils.py

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,18 @@
1919
def ab_to_s(
2020
a_matrix: TerminalPortDataArray, b_matrix: TerminalPortDataArray
2121
) -> TerminalPortDataArray:
22-
"""Get the scattering matrix given the power wave matrices."""
22+
"""Get the scattering matrix given the power wave matrices.
23+
24+
The scattering matrix S is computed from the incident (a) and reflected (b)
25+
power wave amplitude matrices using the formula :math:`S = b * a^-1`.
26+
27+
Args:
28+
a_matrix: Matrix of incident power wave amplitudes.
29+
b_matrix: Matrix of reflected power wave amplitudes.
30+
31+
Returns:
32+
The computed scattering (S) matrix.
33+
"""
2334
# Ensure dimensions are ordered properly
2435
a_matrix = a_matrix.transpose(*TerminalPortDataArray._dims)
2536
b_matrix = b_matrix.transpose(*TerminalPortDataArray._dims)
@@ -35,7 +46,19 @@ def ab_to_s(
3546

3647

3748
def check_port_impedance_sign(Z_numpy: np.ndarray):
38-
"""Sanity check for consistent sign of real part of Z for each port across all frequencies."""
49+
"""Sanity check for consistent sign of real part of Z for each port.
50+
51+
This check iterates through each port and ensures that the sign of the real
52+
part of its impedance does not change across all frequencies. A sign change
53+
can indicate an unphysical result or numerical instability.
54+
55+
Args:
56+
Z_numpy: NumPy array of impedance values with shape (num_freqs, num_ports).
57+
58+
Raises:
59+
Tidy3dError: If an inconsistent sign of the real part of the impedance
60+
is detected for any port.
61+
"""
3962
for port_idx in range(Z_numpy.shape[1]):
4063
port_Z = Z_numpy[:, port_idx]
4164
signs = np.sign(np.real(port_Z))
@@ -48,8 +71,18 @@ def check_port_impedance_sign(Z_numpy: np.ndarray):
4871

4972

5073
def compute_F(Z_numpy: np.array):
51-
"""Helper to convert port impedance matrix to F, which is used for
52-
computing generalized scattering parameters."""
74+
r"""Helper to convert port impedance matrix to F for generalized S-parameters.
75+
76+
The matrix F is used when converting between S and Z parameters for circuits
77+
with differing port impedances. Its diagonal elements are defined as
78+
:math:`F_{kk} = 1 / (2 * \sqrt{Re(Z_k)})`.
79+
80+
Args:
81+
Z_numpy: NumPy array of complex port impedances.
82+
83+
Returns:
84+
NumPy array containing the computed F values.
85+
"""
5386
return 1.0 / (2.0 * np.sqrt(np.real(Z_numpy)))
5487

5588

@@ -78,8 +111,12 @@ def compute_port_VI(
78111
def compute_power_wave_amplitudes(
79112
port: Union[LumpedPort, CoaxialLumpedPort], sim_data: SimulationData
80113
) -> tuple[FreqDataArray, FreqDataArray]:
81-
"""Compute the incident and reflected power wave amplitudes at a lumped port.
82-
The computed amplitudes have not been normalized.
114+
"""Calculates the unnormalized power wave amplitudes from port voltage (V),
115+
current (I), and impedance (Z0) using:
116+
117+
.. math::
118+
a = (V + Z0*I) / (2 * sqrt(Re(Z0)))
119+
b = (V - Z0*I) / (2 * sqrt(Re(Z0)))
83120
84121
Parameters
85122
----------
@@ -105,6 +142,9 @@ def compute_power_delivered_by_port(
105142
) -> FreqDataArray:
106143
"""Compute the power delivered to the network by a lumped port.
107144
145+
The power is calculated as the incident power minus the reflected power:
146+
P = 0.5 * (|a|^2 - |b|^2).
147+
108148
Parameters
109149
----------
110150
port : Union[:class:`.LumpedPort`, :class:`.CoaxialLumpedPort`]
@@ -123,7 +163,19 @@ def compute_power_delivered_by_port(
123163

124164

125165
def s_to_z(s_matrix: TerminalPortDataArray, reference: Union[complex, PortDataArray]) -> DataArray:
126-
"""Get the impedance matrix given the scattering matrix and a reference impedance."""
166+
"""Get the impedance matrix given the scattering matrix and a reference impedance.
167+
168+
This function converts an S-matrix to a Z-matrix. It handles both a single
169+
uniform reference impedance and generalized per-port reference impedances.
170+
171+
Args:
172+
s_matrix: The scattering (S) matrix to convert.
173+
reference: The reference impedance. Can be a single complex value for all
174+
ports or a PortDataArray for per-port impedances.
175+
176+
Returns:
177+
The computed impedance (Z) matrix as a DataArray.
178+
"""
127179

128180
# Ensure dimensions are ordered properly
129181
z_matrix = s_matrix.transpose(*TerminalPortDataArray._dims).copy(deep=True)

0 commit comments

Comments
 (0)