Skip to content

Commit 9c4b340

Browse files
committed
add finite extent to PML for dims where simulation.size == 0
1 parent 7b45680 commit 9c4b340

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Web functions create the leading directories for the supplied filename if they don't exist.
1515
- Some docstring examples that were giving warnings.
1616
- `web.monitor()` only prints message when condition met.
17+
- PML boxes have non-zero extent along any dimensions where the simulation has 0 size, to fix plotting issues for 2D simulations.
1718

1819
## [2.0.2] - 2023-4-3
1920

tests/test_components/test_simulation.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,3 +1500,34 @@ def test_sim_volumetric_structures():
15001500
)
15011501
with pytest.raises(Exception):
15021502
_ = td.Structure(geometry=td.Sphere(radius=1), medium=box.medium)
1503+
1504+
1505+
@pytest.mark.parametrize("normal_axis", (0, 1, 2))
1506+
def test_pml_boxes_2D(normal_axis):
1507+
"""Ensure pml boxes have non-zero dimension for 2D sim."""
1508+
1509+
sim_size = [1, 1, 1]
1510+
sim_size[normal_axis] = 0
1511+
pml_on_kwargs = {dim: axis != normal_axis for axis, dim in enumerate("xyz")}
1512+
1513+
sim2d = td.Simulation(
1514+
size=sim_size,
1515+
run_time=1e-12,
1516+
grid_spec=td.GridSpec(wavelength=1.0),
1517+
sources=[
1518+
td.PointDipole(
1519+
center=(0, 0, 0),
1520+
polarization="Ex",
1521+
source_time=td.GaussianPulse(
1522+
freq0=1e14,
1523+
fwidth=1e12,
1524+
),
1525+
)
1526+
],
1527+
boundary_spec=td.BoundarySpec.pml(**pml_on_kwargs),
1528+
)
1529+
1530+
pml_boxes = sim2d._make_pml_boxes(normal_axis=normal_axis)
1531+
1532+
for pml_box in pml_boxes:
1533+
assert pml_box.size[normal_axis] > 0, "PML box has size of 0 in normal direction of 2D sim."

tidy3d/components/simulation.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
# equal to this times the grid size
6262
DIST_NEIGHBOR_REL_2D_MED = 1e-5
6363

64+
# height of the PML plotting boxes along any dimensions where sim.size[dim] == 0
65+
PML_HEIGHT_FOR_0_DIMS = 0.02
66+
6467

6568
class Simulation(Box): # pylint:disable=too-many-public-methods
6669
"""Contains all information about Tidy3d simulation.
@@ -1772,7 +1775,16 @@ def _make_pml_box(self, pml_axis: Axis, pml_height: float, sign: int) -> Box:
17721775
rmax[pml_axis] = rmin[pml_axis] + pml_height
17731776
else:
17741777
rmin[pml_axis] = rmax[pml_axis] - pml_height
1775-
return Box.from_bounds(rmin=rmin, rmax=rmax)
1778+
pml_box = Box.from_bounds(rmin=rmin, rmax=rmax)
1779+
1780+
# if any dimension of the sim has size 0, set the PML to a very small size along that dim
1781+
new_size = list(pml_box.size)
1782+
for dim_index, sim_size in enumerate(self.size):
1783+
if sim_size == 0.0:
1784+
new_size[dim_index] = PML_HEIGHT_FOR_0_DIMS
1785+
pml_box = pml_box.updated_copy(size=new_size)
1786+
1787+
return pml_box
17761788

17771789
@equal_aspect
17781790
@add_ax_if_none

0 commit comments

Comments
 (0)