Skip to content

Commit 27d92c1

Browse files
committed
add selective simulation capabilities to TerminalComponentModeler
1 parent 7c093b1 commit 27d92c1

File tree

6 files changed

+274
-65
lines changed

6 files changed

+274
-65
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- Automatically apply `matplotlib` styles when importing `tidy3d` which can be reverted via the `td.restore_matplotlib_rcparams()` function.
2020
- Added `TriangleMesh.from_height_expression` class method to create a mesh from an analytical height function defined on a 2D grid and `TriangleMesh.from_height_grid` class method to create a mesh from height values sampled on a 2D grid.
2121
- Added heat sources with custom spatial dependence. It is now possible to add a `SpatialDataArray` as the `rate` in a `HeatSource`.
22-
- Added Transient Heat simulations. It is now possible to run transient Heat simulations. This can be done by specifying `analysis_spec` of `HeatChargeSimulation` object as `UnsteadyHeatAnalysis`.
22+
- Added Transient Heat simulations. It is now possible to run transient Heat simulations. This can be done by specifying `analysis_spec` of `HeatChargeSimulation` object as `UnsteadyHeatAnalysis`.
23+
- Selective simulation capabilities to `TerminalComponentModeler` via `run_only` and `element_mappings` fields, allowing users to run fewer simulations and extract only needed scattering matrix elements.
2324

2425
### Fixed
2526
- Fixed bug in broadband adjoint source creation when forward simulation had a pulse amplitude greater than 1 or a nonzero pulse phase.

tests/test_plugins/smatrix/terminal_component_modeler_def.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def make_port(center, direction, type, name) -> Union[CoaxialLumpedPort, WavePor
276276
inner_diameter=2 * Rinner,
277277
normal_axis=2,
278278
direction=direction,
279-
name=name,
279+
name="coax" + name,
280280
num_grid_cells=port_cells,
281281
impedance=reference_impedance,
282282
)
@@ -309,7 +309,7 @@ def make_port(center, direction, type, name) -> Union[CoaxialLumpedPort, WavePor
309309
center=center,
310310
size=[2 * Router, 2 * Router, 0],
311311
direction=direction,
312-
name=name,
312+
name="wave" + name,
313313
mode_spec=td.ModeSpec(num_modes=1),
314314
mode_index=0,
315315
voltage_integral=voltage_integral,
@@ -318,9 +318,9 @@ def make_port(center, direction, type, name) -> Union[CoaxialLumpedPort, WavePor
318318
return port
319319

320320
center_src1 = [0, 0, -length / 2]
321-
port_1 = make_port(center_src1, direction="+", type=port_types[0], name="coax_port_1")
321+
port_1 = make_port(center_src1, direction="+", type=port_types[0], name="_1")
322322
center_src2 = [0, 0, length / 2]
323-
port_2 = make_port(center_src2, direction="-", type=port_types[1], name="coax_port_2")
323+
port_2 = make_port(center_src2, direction="-", type=port_types[1], name="_2")
324324
ports = [port_1, port_2]
325325
freqs = np.linspace(freq_start, freq_stop, 100)
326326

tests/test_plugins/smatrix/test_terminal_component_modeler.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,3 +833,44 @@ def test_get_combined_antenna_parameters_data(monkeypatch, tmp_path):
833833
assert not np.allclose(
834834
antenna_params.radiation_efficiency, single_port_params.radiation_efficiency
835835
)
836+
837+
838+
def test_run_only_and_element_mappings(monkeypatch, tmp_path):
839+
"""Checks the terminal component modeler works when running with a subset of excitations."""
840+
z_grid = td.UniformGrid(dl=1 * 1e3)
841+
xy_grid = td.UniformGrid(dl=0.1 * 1e3)
842+
grid_spec = td.GridSpec(grid_x=xy_grid, grid_y=xy_grid, grid_z=z_grid)
843+
modeler = make_coaxial_component_modeler(
844+
path_dir=str(tmp_path), port_types=(CoaxialLumpedPort, WavePort), grid_spec=grid_spec
845+
)
846+
port0_idx = modeler.network_index(modeler.ports[0])
847+
port1_idx = modeler.network_index(modeler.ports[1])
848+
modeler_run1 = modeler.updated_copy(run_only=(port0_idx,))
849+
850+
# Make sure the smatrix and impedance calculations work for reduced simulations
851+
s_matrix = run_component_modeler(monkeypatch, modeler_run1)
852+
with pytest.raises(ValueError):
853+
TerminalComponentModeler._validate_square_matrix(s_matrix, "test_method")
854+
_ = modeler_run1.port_reference_impedances
855+
856+
assert len(modeler_run1.sim_dict) == 1
857+
S11 = (port0_idx, port0_idx)
858+
S21 = (port1_idx, port0_idx)
859+
S12 = (port0_idx, port1_idx)
860+
S22 = (port1_idx, port1_idx)
861+
element_mappings = ((S11, S22, 1),)
862+
modeler_with_mappings = modeler.updated_copy(element_mappings=element_mappings)
863+
assert len(modeler_with_mappings.sim_dict) == 2
864+
865+
# Column 1 is mapped to column 2, resulting in one simulation
866+
element_mappings = ((S11, S22, 1), (S21, S12, 1))
867+
modeler_with_mappings = modeler.updated_copy(element_mappings=element_mappings)
868+
s_matrix = run_component_modeler(monkeypatch, modeler_with_mappings)
869+
assert np.all(s_matrix.values[:, 0, 0] == s_matrix.values[:, 1, 1])
870+
assert np.all(s_matrix.values[:, 0, 1] == s_matrix.values[:, 1, 0])
871+
assert len(modeler_with_mappings.sim_dict) == 1
872+
873+
# Mapping is incomplete, so two simulations are run
874+
element_mappings = ((S11, S22, 1), (S12, S21, 1))
875+
modeler_with_mappings = modeler.updated_copy(element_mappings=element_mappings)
876+
assert len(modeler_with_mappings.sim_dict) == 2

tidy3d/plugins/smatrix/component_modelers/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from tidy3d.components.data.sim_data import SimulationData
1515
from tidy3d.components.simulation import Simulation
1616
from tidy3d.components.types import FreqArray
17+
from tidy3d.components.validators import assert_unique_names
1718
from tidy3d.config import config
1819
from tidy3d.constants import HERTZ
1920
from tidy3d.exceptions import SetupError, Tidy3dKeyError
@@ -308,3 +309,5 @@ def sim_data_by_task_name(self, task_name: str) -> SimulationData:
308309
sim_data = self.batch_data[task_name]
309310
config.logging_level = log_level_cache
310311
return sim_data
312+
313+
_unique_port_names = assert_unique_names("ports")

tidy3d/plugins/smatrix/component_modelers/modal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def matrix_indices_source(self) -> tuple[MatrixIndex, ...]:
132132
def matrix_indices_run_sim(self) -> tuple[MatrixIndex, ...]:
133133
"""Tuple of all the source matrix indices (port, mode_index) in the Component Modeler."""
134134

135-
if self.element_mappings is None or self.element_mappings == {}:
135+
if not self.element_mappings:
136136
return self.matrix_indices_source
137137

138138
# all the (i, j) pairs in `S_ij` that are tagged as covered by `element_mappings`

0 commit comments

Comments
 (0)