Skip to content

Commit

Permalink
Raise error early when port source is snapped into PML
Browse files Browse the repository at this point in the history
  • Loading branch information
weiliangjin2021 committed Aug 9, 2024
1 parent 04fdff6 commit 89e8491
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
42 changes: 42 additions & 0 deletions tests/test_plugins/test_terminal_component_modeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,45 @@ 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))
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)
20 changes: 15 additions & 5 deletions tidy3d/plugins/smatrix/component_modelers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,29 @@ 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."
f"Port {port.name} normal is less than 2 cells to the boundary "
f"on -{'xyz'[normal_axis]} side. "
"Please either increase the mesh resolution near the port or "
"move the port away from the boundary."
)

# 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"Port {port.name} normal is tless than 2 cells to the boundary "
f"on +{'xyz'[normal_axis]} side."
"Please either increase the mesh resolution near the port or "
"move the port away from the boundary."
)

new_pos = grid_centers[shifted_index]
Expand Down

0 comments on commit 89e8491

Please sign in to comment.