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 8, 2024
1 parent 04fdff6 commit 4eb2232
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
44 changes: 44 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,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)
10 changes: 8 additions & 2 deletions tidy3d/plugins/smatrix/component_modelers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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."
Expand Down

0 comments on commit 4eb2232

Please sign in to comment.