Skip to content

Commit

Permalink
added sanity check as method, which is turned off in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarek-flex committed Sep 12, 2024
1 parent a5d961c commit 83b7b15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
5 changes: 3 additions & 2 deletions tests/test_plugins/smatrix/test_terminal_component_modeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ def run_component_modeler(monkeypatch, modeler: TerminalComponentModeler):
)
monkeypatch.setattr(
TerminalComponentModeler,
"port_reference_impedances",
lambda matrix: 1.0 / (2.0 * np.sqrt(np.abs(matrix) + 1e-4)),
"_check_port_impedance_sign",
lambda self, Z_numpy: (),
)

s_matrix = modeler._construct_smatrix()
return s_matrix

Expand Down
23 changes: 13 additions & 10 deletions tidy3d/plugins/smatrix/component_modelers/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def sim_dict(self) -> Dict[str, Simulation]:

@cached_property
def _source_time(self):
"""Helper to create a time domain pulse for the frequeny range of interest."""
"""Helper to create a time domain pulse for the frequency range of interest."""
freq0 = np.mean(self.freqs)
fdiff = max(self.freqs) - min(self.freqs)
fwidth = max(fdiff, freq0 * FWIDTH_FRAC)
Expand Down Expand Up @@ -239,15 +239,8 @@ def port_VI(port_out: AbstractTerminalPort, sim_data: SimulationData):
(len(self.freqs), len(port_names), 1)
)

# Sanity check for consistent sign of real part of Z for each port across all frequencies
for port_idx in range(Z_numpy.shape[1]):
port_Z = Z_numpy[:, port_idx, 0]
signs = np.sign(np.real(port_Z))
if not np.all(signs == signs[0]):
print(port_Z)
raise ValueError(
f"Inconsistent sign of real part of Z detected for port {port_idx}"
)
# Check to make sure sign is consistent for all impedance values
self._check_port_impedance_sign(Z_numpy)

# Check for negative real part of port impedance and flip the V and Z signs accordingly
negative_real_Z = np.real(Z_numpy) < 0
Expand Down Expand Up @@ -401,3 +394,13 @@ def _set_port_data_array_attributes(data_array: PortDataArray) -> PortDataArray:
"""Helper to set additional metadata for ``PortDataArray``."""
data_array.name = "Z0"
return data_array.assign_attrs(units=OHM, long_name="characteristic impedance")

def _check_port_impedance_sign(self, Z_numpy: np.ndarray):
"""Sanity check for consistent sign of real part of Z for each port across all frequencies."""
for port_idx in range(Z_numpy.shape[1]):
port_Z = Z_numpy[:, port_idx, 0]
signs = np.sign(np.real(port_Z))
if not np.all(signs == signs[0]):
raise ValueError(
f"Inconsistent sign of real part of Z detected for port {port_idx}."
)

0 comments on commit 83b7b15

Please sign in to comment.