From 4eb2232a18e8a68c65530bc02119d0d5b600d5ed Mon Sep 17 00:00:00 2001 From: Weiliang Jin Date: Thu, 8 Aug 2024 11:00:04 -0700 Subject: [PATCH] Raise error early when port source is snapped into PML --- .../test_terminal_component_modeler.py | 44 +++++++++++++++++++ .../smatrix/component_modelers/base.py | 10 ++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/tests/test_plugins/test_terminal_component_modeler.py b/tests/test_plugins/test_terminal_component_modeler.py index 7e33a0beae..651c97121c 100644 --- a/tests/test_plugins/test_terminal_component_modeler.py +++ b/tests/test_plugins/test_terminal_component_modeler.py @@ -509,3 +509,47 @@ def test_wave_port_to_mode_solver(tmp_path): path_dir=str(tmp_path), port_types=(WavePort, WavePort) ) _ = modeler.ports[0].to_mode_solver(modeler.simulation, freqs=[1e9, 2e9, 3e9]) + + +def test_port_source_snapped_to_PML(tmp_path): + """Raise meaningful error message when source is snapped into PML because the port is too close + to the boundary. + """ + modeler = make_component_modeler(planar_pec=True, path_dir=str(tmp_path)) + print(modeler.simulation.bounds) + print(modeler.simulation.grid.boundaries.x) + port_pos = 5e4 + voltage_path = VoltageIntegralAxisAligned( + center=(port_pos, 0, 0), + size=(0, 1e3, 0), + sign="+", + ) + port = WavePort( + center=(port_pos, 0, 0), + size=(0, 1e3, 1e3), + name="wave_port", + mode_spec=td.ModeSpec(num_modes=1), + direction="-", + voltage_integral=voltage_path, + current_integral=None, + ) + modeler = modeler.updated_copy(ports=[port]) + + # Error because port is snapped to PML layers; but the error message might not + # be very informative, e.g. "simulation.sources[0]' is outside of the simulation domain". + # So we also check where error should be raised immediately + with pytest.raises(SetupError): + modeler.sim_dict + + with pytest.raises(SetupError): + modeler._shift_value_signed(port) + + # also validate the negative side + voltage_path = voltage_path.updated_copy(center=(-port_pos, 0, 0)) + port = port.updated_copy(direction="+", center=(-port_pos, 0, 0), voltage_integral=voltage_path) + modeler = modeler.updated_copy(ports=[port]) + with pytest.raises(SetupError): + modeler.sim_dict + + with pytest.raises(SetupError): + modeler._shift_value_signed(port) diff --git a/tidy3d/plugins/smatrix/component_modelers/base.py b/tidy3d/plugins/smatrix/component_modelers/base.py index 1541965aaf..1a44d4d697 100644 --- a/tidy3d/plugins/smatrix/component_modelers/base.py +++ b/tidy3d/plugins/smatrix/component_modelers/base.py @@ -254,7 +254,10 @@ def _shift_value_signed(self, port: Union[Port, WavePort]) -> float: # shift the port to the left if port.direction == "+": shifted_index = port_index - 2 - if shifted_index < 0: + if ( + shifted_index < 0 + or grid_centers[shifted_index] <= self.simulation.bounds[0][normal_axis] + ): raise SetupError( f"Port {port.name} normal is too close to boundary " f"on -{'xyz'[normal_axis]} side." @@ -263,7 +266,10 @@ def _shift_value_signed(self, port: Union[Port, WavePort]) -> float: # shift the port to the right else: shifted_index = port_index + 2 - if shifted_index >= len(grid_centers): + if ( + shifted_index >= len(grid_centers) + or grid_centers[shifted_index] >= self.simulation.bounds[1][normal_axis] + ): raise SetupError( f"Port {port.name} normal is too close to boundary " f"on +{'xyz'[normal_axis]} side."